Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 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 | |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 17 | #include <filesystem> |
Colin Cross | 26b82b1 | 2021-12-22 10:09:19 -0800 | [diff] [blame] | 18 | #include <utility> |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 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 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 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); |
Kelvin Zhang | 8251dc0 | 2022-06-14 09:46:46 -0700 | [diff] [blame^] | 41 | return result; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 44 | TEST(ApexHandlerAndroidTest, CalculateSizeUpdatableApex) { |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 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()); |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 55 | ASSERT_EQ(*result, 3u); |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 56 | } |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 57 | |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 58 | TEST(ApexHandlerAndroidTest, AllocateSpaceUpdatableApex) { |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 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); |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 67 | ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos)); |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 68 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 69 | // Should be able to pass empty list |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 70 | ASSERT_TRUE(apex_handler.AllocateSpace({})); |
| 71 | } |
| 72 | |
| 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 | |
| 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({})); |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace chromeos_update_engine |