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 | |
| 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 | |
| 32 | class ApexHandlerAndroidTest : public ::testing::Test { |
| 33 | protected: |
| 34 | ApexHandlerAndroidTest() = default; |
| 35 | |
| 36 | android::sp<android::apex::IApexService> GetApexService() const { |
| 37 | return apex_handler_.GetApexService(); |
| 38 | } |
| 39 | |
| 40 | uint64_t CalculateSize( |
| 41 | const std::vector<ApexInfo>& apex_infos, |
| 42 | android::sp<android::apex::IApexService> apex_service) const { |
| 43 | return apex_handler_.CalculateSize(apex_infos, apex_service); |
| 44 | } |
| 45 | |
| 46 | bool AllocateSpace(const uint64_t size_required, |
| 47 | const std::string& dir_path) const { |
| 48 | return apex_handler_.AllocateSpace(size_required, dir_path); |
| 49 | } |
| 50 | |
| 51 | ApexInfo CreateApexInfo(const std::string& package_name, |
| 52 | int version, |
| 53 | bool is_compressed, |
| 54 | int decompressed_size) { |
| 55 | ApexInfo result; |
| 56 | result.set_package_name(package_name); |
| 57 | result.set_version(version); |
| 58 | result.set_is_compressed(is_compressed); |
| 59 | result.set_decompressed_size(decompressed_size); |
| 60 | return std::move(result); |
| 61 | } |
| 62 | |
| 63 | ApexHandlerAndroid apex_handler_; |
| 64 | }; |
| 65 | |
| 66 | // TODO(b/172911822): Once apexd has more optimized response for CalculateSize, |
| 67 | // improve this test |
| 68 | TEST_F(ApexHandlerAndroidTest, CalculateSize) { |
| 69 | std::vector<ApexInfo> apex_infos; |
| 70 | ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 10); |
| 71 | ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 20); |
| 72 | apex_infos.push_back(compressed_apex_1); |
| 73 | apex_infos.push_back(compressed_apex_2); |
| 74 | auto apex_service = GetApexService(); |
| 75 | EXPECT_TRUE(apex_service != nullptr) << "Apexservice not found"; |
| 76 | int required_size = CalculateSize(apex_infos, apex_service); |
| 77 | EXPECT_EQ(required_size, 30); |
| 78 | } |
| 79 | |
| 80 | TEST_F(ApexHandlerAndroidTest, AllocateSpace) { |
| 81 | // Allocating 0 space should be a no op |
| 82 | TemporaryDir td; |
| 83 | EXPECT_TRUE(AllocateSpace(0, td.path)); |
| 84 | EXPECT_TRUE(fs::is_empty(td.path)); |
| 85 | |
| 86 | // Allocating non-zero space should create a file with tmp suffix |
| 87 | EXPECT_TRUE(AllocateSpace(2 << 20, td.path)); |
| 88 | EXPECT_FALSE(fs::is_empty(td.path)); |
| 89 | int num_of_file = 0; |
| 90 | for (const auto& entry : fs::directory_iterator(td.path)) { |
| 91 | num_of_file++; |
| 92 | EXPECT_TRUE(EndsWith(entry.path().string(), ".tmp")); |
| 93 | EXPECT_EQ(fs::file_size(entry.path()), 2u << 20); |
| 94 | } |
| 95 | EXPECT_EQ(num_of_file, 1); |
| 96 | |
| 97 | // AllocateSpace should be safe to call twice |
| 98 | EXPECT_TRUE(AllocateSpace(100, td.path)); |
| 99 | EXPECT_FALSE(fs::is_empty(td.path)); |
| 100 | num_of_file = 0; |
| 101 | for (const auto& entry : fs::directory_iterator(td.path)) { |
| 102 | num_of_file++; |
| 103 | EXPECT_TRUE(EndsWith(entry.path().string(), ".tmp")); |
| 104 | EXPECT_EQ(fs::file_size(entry.path()), 100u); |
| 105 | } |
| 106 | EXPECT_EQ(num_of_file, 1); |
| 107 | } |
| 108 | |
| 109 | } // namespace chromeos_update_engine |