blob: dd962562739bd9dc9538af5cc6b015309ea4004a [file] [log] [blame]
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +00001//
2// Copyright (C) 2021 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000017#include <filesystem>
Colin Cross26b82b12021-12-22 10:09:19 -080018#include <utility>
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000019
20#include "update_engine/aosp/apex_handler_android.h"
21
22#include <android-base/file.h>
23#include <android-base/strings.h>
24#include <gtest/gtest.h>
25
26using android::base::EndsWith;
27
28namespace chromeos_update_engine {
29
30namespace fs = std::filesystem;
31
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000032ApexInfo CreateApexInfo(const std::string& package_name,
33 int version,
34 bool is_compressed,
35 int decompressed_size) {
36 ApexInfo result;
37 result.set_package_name(package_name);
38 result.set_version(version);
39 result.set_is_compressed(is_compressed);
40 result.set_decompressed_size(decompressed_size);
Kelvin Zhang8251dc02022-06-14 09:46:46 -070041 return result;
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000042}
43
Nikita Ioffeaedfef32021-04-28 13:54:14 +010044TEST(ApexHandlerAndroidTest, CalculateSizeUpdatableApex) {
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000045 ApexHandlerAndroid apex_handler;
46 std::vector<ApexInfo> apex_infos;
47 ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
48 ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
49 ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
50 apex_infos.push_back(compressed_apex_1);
51 apex_infos.push_back(compressed_apex_2);
52 apex_infos.push_back(uncompressed_apex);
53 auto result = apex_handler.CalculateSize(apex_infos);
54 ASSERT_TRUE(result.ok());
Nikita Ioffeaedfef32021-04-28 13:54:14 +010055 ASSERT_EQ(*result, 3u);
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000056}
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000057
Nikita Ioffeaedfef32021-04-28 13:54:14 +010058TEST(ApexHandlerAndroidTest, AllocateSpaceUpdatableApex) {
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000059 ApexHandlerAndroid apex_handler;
60 std::vector<ApexInfo> apex_infos;
61 ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
62 ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
63 ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
64 apex_infos.push_back(compressed_apex_1);
65 apex_infos.push_back(compressed_apex_2);
66 apex_infos.push_back(uncompressed_apex);
Nikita Ioffeaedfef32021-04-28 13:54:14 +010067 ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos));
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000068
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000069 // Should be able to pass empty list
Nikita Ioffeaedfef32021-04-28 13:54:14 +010070 ASSERT_TRUE(apex_handler.AllocateSpace({}));
71}
72
73TEST(ApexHandlerAndroidTest, CalculateSizeFlattenedApex) {
74 FlattenedApexHandlerAndroid apex_handler;
75 std::vector<ApexInfo> apex_infos;
76 ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
77 ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
78 ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
79 apex_infos.push_back(compressed_apex_1);
80 apex_infos.push_back(compressed_apex_2);
81 apex_infos.push_back(uncompressed_apex);
82 auto result = apex_handler.CalculateSize(apex_infos);
83 ASSERT_TRUE(result.ok());
84 ASSERT_EQ(*result, 0u);
85}
86
87TEST(ApexHandlerAndroidTest, AllocateSpaceFlattenedApex) {
88 FlattenedApexHandlerAndroid apex_handler;
89 std::vector<ApexInfo> apex_infos;
90 ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
91 ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
92 ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
93 apex_infos.push_back(compressed_apex_1);
94 apex_infos.push_back(compressed_apex_2);
95 apex_infos.push_back(uncompressed_apex);
96 ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos));
97
98 // Should be able to pass empty list
99 ASSERT_TRUE(apex_handler.AllocateSpace({}));
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000100}
101
102} // namespace chromeos_update_engine