1 //
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
17 #include <utility>
18 #include <filesystem>
19
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
26 using android::base::EndsWith;
27
28 namespace chromeos_update_engine {
29
30 namespace fs = std::filesystem;
31
CreateApexInfo(const std::string & package_name,int version,bool is_compressed,int decompressed_size)32 ApexInfo 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);
41 return std::move(result);
42 }
43
TEST(ApexHandlerAndroidTest,CalculateSizeUpdatableApex)44 TEST(ApexHandlerAndroidTest, CalculateSizeUpdatableApex) {
45 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());
55 ASSERT_EQ(*result, 3u);
56 }
57
TEST(ApexHandlerAndroidTest,AllocateSpaceUpdatableApex)58 TEST(ApexHandlerAndroidTest, AllocateSpaceUpdatableApex) {
59 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);
67 ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos));
68
69 // Should be able to pass empty list
70 ASSERT_TRUE(apex_handler.AllocateSpace({}));
71 }
72
TEST(ApexHandlerAndroidTest,CalculateSizeFlattenedApex)73 TEST(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
TEST(ApexHandlerAndroidTest,AllocateSpaceFlattenedApex)87 TEST(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({}));
100 }
101
102 } // namespace chromeos_update_engine
103