summaryrefslogtreecommitdiff
path: root/aosp/apex_handler_android_unittest.cc
blob: 847ccaade60b068cf17dda64af8769c8c057b59d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <utility>
#include <filesystem>

#include "update_engine/aosp/apex_handler_android.h"

#include <android-base/file.h>
#include <android-base/strings.h>
#include <gtest/gtest.h>

using android::base::EndsWith;

namespace chromeos_update_engine {

namespace fs = std::filesystem;

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(ApexHandlerAndroidTest, CalculateSizeUpdatableApex) {
  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());
  ASSERT_EQ(*result, 3u);
}

TEST(ApexHandlerAndroidTest, AllocateSpaceUpdatableApex) {
  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);
  ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos));

  // Should be able to pass empty list
  ASSERT_TRUE(apex_handler.AllocateSpace({}));
}

TEST(ApexHandlerAndroidTest, CalculateSizeFlattenedApex) {
  FlattenedApexHandlerAndroid 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());
  ASSERT_EQ(*result, 0u);
}

TEST(ApexHandlerAndroidTest, AllocateSpaceFlattenedApex) {
  FlattenedApexHandlerAndroid 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);
  ASSERT_TRUE(apex_handler.AllocateSpace(apex_infos));

  // Should be able to pass empty list
  ASSERT_TRUE(apex_handler.AllocateSpace({}));
}

}  // namespace chromeos_update_engine