Make update_engine reserve space for decompression via apexd

Bug: 172911822
Test: atest ApexHandlerAndroidTest (checked that file was created)
Change-Id: I8024695ebba1a9c1796c05b27a0eec3da3b3d1bc
diff --git a/aosp/apex_handler_android_unittest.cc b/aosp/apex_handler_android_unittest.cc
index 3a99f79..981ae9d 100644
--- a/aosp/apex_handler_android_unittest.cc
+++ b/aosp/apex_handler_android_unittest.cc
@@ -29,81 +29,45 @@
 
 namespace fs = std::filesystem;
 
-class ApexHandlerAndroidTest : public ::testing::Test {
- protected:
-  ApexHandlerAndroidTest() = default;
-
-  android::sp<android::apex::IApexService> GetApexService() const {
-    return apex_handler_.GetApexService();
-  }
-
-  uint64_t CalculateSize(
-      const std::vector<ApexInfo>& apex_infos,
-      android::sp<android::apex::IApexService> apex_service) const {
-    return apex_handler_.CalculateSize(apex_infos, apex_service);
-  }
-
-  bool AllocateSpace(const uint64_t size_required,
-                     const std::string& dir_path) const {
-    return apex_handler_.AllocateSpace(size_required, dir_path);
-  }
-
-  ApexInfo CreateApexInfo(const std::string& package_name,
-                          int version,
-                          bool is_compressed,
-                          int decompressed_size) {
-    ApexInfo result;
-    result.set_package_name(package_name);
-    result.set_version(version);
-    result.set_is_compressed(is_compressed);
-    result.set_decompressed_size(decompressed_size);
-    return std::move(result);
-  }
-
-  ApexHandlerAndroid apex_handler_;
-};
-
-// TODO(b/172911822): Once apexd has more optimized response for CalculateSize,
-//  improve this test
-TEST_F(ApexHandlerAndroidTest, CalculateSize) {
-  std::vector<ApexInfo> apex_infos;
-  ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 10);
-  ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 20);
-  apex_infos.push_back(compressed_apex_1);
-  apex_infos.push_back(compressed_apex_2);
-  auto apex_service = GetApexService();
-  EXPECT_TRUE(apex_service != nullptr) << "Apexservice not found";
-  int required_size = CalculateSize(apex_infos, apex_service);
-  EXPECT_EQ(required_size, 30);
+ApexInfo CreateApexInfo(const std::string& package_name,
+                        int version,
+                        bool is_compressed,
+                        int decompressed_size) {
+  ApexInfo result;
+  result.set_package_name(package_name);
+  result.set_version(version);
+  result.set_is_compressed(is_compressed);
+  result.set_decompressed_size(decompressed_size);
+  return std::move(result);
 }
 
-TEST_F(ApexHandlerAndroidTest, AllocateSpace) {
-  // Allocating 0 space should be a no op
-  TemporaryDir td;
-  EXPECT_TRUE(AllocateSpace(0, td.path));
-  EXPECT_TRUE(fs::is_empty(td.path));
+TEST(ApexHandlerAndroidTest, CalculateSize) {
+  ApexHandlerAndroid apex_handler;
+  std::vector<ApexInfo> apex_infos;
+  ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
+  ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
+  ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
+  apex_infos.push_back(compressed_apex_1);
+  apex_infos.push_back(compressed_apex_2);
+  apex_infos.push_back(uncompressed_apex);
+  auto result = apex_handler.CalculateSize(apex_infos);
+  ASSERT_TRUE(result.ok());
+  EXPECT_EQ(*result, 3u);
+}
 
-  // Allocating non-zero space should create a file with tmp suffix
-  EXPECT_TRUE(AllocateSpace(2 << 20, td.path));
-  EXPECT_FALSE(fs::is_empty(td.path));
-  int num_of_file = 0;
-  for (const auto& entry : fs::directory_iterator(td.path)) {
-    num_of_file++;
-    EXPECT_TRUE(EndsWith(entry.path().string(), ".tmp"));
-    EXPECT_EQ(fs::file_size(entry.path()), 2u << 20);
-  }
-  EXPECT_EQ(num_of_file, 1);
+TEST(ApexHandlerAndroidTest, AllocateSpace) {
+  ApexHandlerAndroid apex_handler;
+  std::vector<ApexInfo> apex_infos;
+  ApexInfo compressed_apex_1 = CreateApexInfo("sample1", 1, true, 1);
+  ApexInfo compressed_apex_2 = CreateApexInfo("sample2", 2, true, 2);
+  ApexInfo uncompressed_apex = CreateApexInfo("uncompressed", 1, false, 4);
+  apex_infos.push_back(compressed_apex_1);
+  apex_infos.push_back(compressed_apex_2);
+  apex_infos.push_back(uncompressed_apex);
+  EXPECT_TRUE(apex_handler.AllocateSpace(apex_infos));
 
-  // AllocateSpace should be safe to call twice
-  EXPECT_TRUE(AllocateSpace(100, td.path));
-  EXPECT_FALSE(fs::is_empty(td.path));
-  num_of_file = 0;
-  for (const auto& entry : fs::directory_iterator(td.path)) {
-    num_of_file++;
-    EXPECT_TRUE(EndsWith(entry.path().string(), ".tmp"));
-    EXPECT_EQ(fs::file_size(entry.path()), 100u);
-  }
-  EXPECT_EQ(num_of_file, 1);
+  // Should be able to pass empty list
+  EXPECT_TRUE(apex_handler.AllocateSpace({}));
 }
 
 }  // namespace chromeos_update_engine