blob: c3415479dfc2225f3c7c9fe3c2cc2f0fe507781d [file] [log] [blame]
Tao Bao1d866052017-04-10 16:55:57 -07001/*
2 * Copyright (C) 2017 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 agree 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 <stdio.h>
Tao Bao00d57572017-05-02 15:48:54 -070018#include <sys/stat.h>
19#include <sys/types.h>
Tao Baof2784b62017-04-19 12:37:12 -070020#include <unistd.h>
Tao Bao1d866052017-04-10 16:55:57 -070021
Tianjie Xu69b96492017-08-17 16:42:57 -070022#include <algorithm>
Tianjie Xu93b5bf22018-10-25 10:39:01 -070023#include <random>
Tao Baobc4b1fe2017-04-17 16:46:05 -070024#include <string>
25#include <vector>
26
Tao Baof2784b62017-04-19 12:37:12 -070027#include <android-base/file.h>
Tao Baobc4b1fe2017-04-17 16:46:05 -070028#include <android-base/properties.h>
29#include <android-base/strings.h>
Tao Bao1d866052017-04-10 16:55:57 -070030#include <gtest/gtest.h>
31#include <ziparchive/zip_archive.h>
32#include <ziparchive/zip_writer.h>
33
xunchang24788852019-03-22 16:08:52 -070034#include "install/install.h"
Tao Bao7f19d102019-04-26 22:56:56 -070035#include "install/wipe_device.h"
Tao Baocf60a442018-06-18 14:56:20 -070036#include "otautil/paths.h"
xunchang24788852019-03-22 16:08:52 -070037#include "private/setup_commands.h"
Tianjie Xucd8faf72019-08-06 12:32:05 -070038#include "recovery_utils/roots.h"
Tao Bao1d866052017-04-10 16:55:57 -070039
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070040static void BuildZipArchive(const std::map<std::string, std::string>& file_map, int fd,
41 int compression_type) {
42 FILE* zip_file = fdopen(fd, "w");
Tao Bao1d866052017-04-10 16:55:57 -070043 ZipWriter writer(zip_file);
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070044 for (const auto& [name, content] : file_map) {
45 ASSERT_EQ(0, writer.StartEntry(name.c_str(), compression_type));
46 ASSERT_EQ(0, writer.WriteBytes(content.data(), content.size()));
47 ASSERT_EQ(0, writer.FinishEntry());
48 }
Tao Bao1d866052017-04-10 16:55:57 -070049 ASSERT_EQ(0, writer.Finish());
50 ASSERT_EQ(0, fclose(zip_file));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070051}
52
Tao Bao8a7afcc2017-04-18 22:05:50 -070053TEST(InstallTest, read_metadata_from_package_smoke) {
54 TemporaryFile temp_file;
Tianjie Xu93b5bf22018-10-25 10:39:01 -070055 const std::string content("abc=defg");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070056 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(),
57 kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070058
59 ZipArchiveHandle zip;
60 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -070061 std::map<std::string, std::string> metadata;
62 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
63 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070064 CloseArchive(zip);
65
66 TemporaryFile temp_file2;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070067 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(),
68 kCompressDeflated);
Tao Bao8a7afcc2017-04-18 22:05:50 -070069
70 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
71 metadata.clear();
Tianjie Xu93b5bf22018-10-25 10:39:01 -070072 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
73 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070074 CloseArchive(zip);
75}
76
77TEST(InstallTest, read_metadata_from_package_no_entry) {
78 TemporaryFile temp_file;
Tianjie78d15142020-07-22 17:40:09 -070079 BuildZipArchive({ { "fake_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070080
81 ZipArchiveHandle zip;
82 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -070083 std::map<std::string, std::string> metadata;
84 ASSERT_FALSE(ReadMetadataFromPackage(zip, &metadata));
Tao Bao8a7afcc2017-04-18 22:05:50 -070085 CloseArchive(zip);
86}
87
xunchange0d991c2019-03-05 14:50:51 -080088TEST(InstallTest, read_wipe_ab_partition_list) {
89 std::vector<std::string> partition_list = {
90 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
91 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
92 "/dev/block/bootdevice/by-name/userdata", "# Wipe the boot partitions last",
93 "/dev/block/bootdevice/by-name/boot_a", "/dev/block/bootdevice/by-name/boot_b",
94 };
95 TemporaryFile temp_file;
96 BuildZipArchive({ { "recovery.wipe", android::base::Join(partition_list, '\n') } },
97 temp_file.release(), kCompressDeflated);
98 std::string wipe_package;
99 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &wipe_package));
100
xunchang55e3d222019-03-11 11:28:41 -0700101 auto package = Package::CreateMemoryPackage(
102 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
103
104 auto read_partition_list = GetWipePartitionList(package.get());
xunchange0d991c2019-03-05 14:50:51 -0800105 std::vector<std::string> expected = {
106 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
107 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
108 "/dev/block/bootdevice/by-name/userdata", "/dev/block/bootdevice/by-name/boot_a",
109 "/dev/block/bootdevice/by-name/boot_b",
110 };
111 ASSERT_EQ(expected, read_partition_list);
112}
113
Tao Baocf60a442018-06-18 14:56:20 -0700114TEST(InstallTest, SetUpNonAbUpdateCommands) {
115 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700116 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700117 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700118
119 ZipArchiveHandle zip;
120 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
121 int status_fd = 10;
122 std::string package = "/path/to/update.zip";
123 TemporaryDir td;
124 std::string binary_path = std::string(td.path) + "/update_binary";
125 Paths::Get().set_temporary_update_binary(binary_path);
126 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700127 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700128 ASSERT_EQ(4U, cmd.size());
129 ASSERT_EQ(binary_path, cmd[0]);
130 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
131 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
132 ASSERT_EQ(package, cmd[3]);
133 struct stat sb;
134 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
135 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
136
137 // With non-zero retry count. update_binary will be removed automatically.
138 cmd.clear();
Tao Bao36c72762019-04-30 00:25:41 -0700139 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700140 ASSERT_EQ(5U, cmd.size());
141 ASSERT_EQ(binary_path, cmd[0]);
142 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
143 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
144 ASSERT_EQ(package, cmd[3]);
145 ASSERT_EQ("retry", cmd[4]);
146 sb = {};
147 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
148 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
149
150 CloseArchive(zip);
151}
152
153TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
154 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700155 // The archive must have something to be opened correctly.
Tianjie78d15142020-07-22 17:40:09 -0700156 BuildZipArchive({ { "fake_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700157
158 // Missing update binary.
159 ZipArchiveHandle zip;
160 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
161 int status_fd = 10;
162 std::string package = "/path/to/update.zip";
163 TemporaryDir td;
164 Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary");
165 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700166 ASSERT_FALSE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700167 CloseArchive(zip);
168}
169
170static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700171 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700172
Tao Baobc4b1fe2017-04-17 16:46:05 -0700173 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700174 std::string device = android::base::GetProperty("ro.product.device", "");
175 ASSERT_NE("", device);
176 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
177 ASSERT_NE("", timestamp);
Tianjie Xu69b96492017-08-17 16:42:57 -0700178
179 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
180 "post-timestamp=" + timestamp };
181 if (!serialno.empty()) {
182 meta.push_back("serialno=" + serialno);
183 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700184 std::string metadata_string = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700185
186 BuildZipArchive({ { "payload.bin", "" },
187 { "payload_properties.txt", properties },
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700188 { "META-INF/com/android/metadata", metadata_string } },
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700189 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700190
191 ZipArchiveHandle zip;
192 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Kelvin Zhang4f811302020-09-16 14:06:12 -0400193 ZipEntry64 payload_entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700194 ASSERT_EQ(0, FindEntry(zip, "payload.bin", &payload_entry));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700195
196 std::map<std::string, std::string> metadata;
197 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
Tianjie Xu69b96492017-08-17 16:42:57 -0700198 if (success) {
Tao Bao36c72762019-04-30 00:25:41 -0700199 ASSERT_TRUE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700200
201 int status_fd = 10;
202 std::string package = "/path/to/update.zip";
203 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700204 ASSERT_TRUE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700205 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700206 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700207 ASSERT_EQ("--payload=file://" + package, cmd[1]);
208 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
209 ASSERT_EQ("--headers=" + properties, cmd[3]);
210 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
211 } else {
Tao Bao36c72762019-04-30 00:25:41 -0700212 ASSERT_FALSE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu69b96492017-08-17 16:42:57 -0700213 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700214 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700215}
Tianjie Xu69b96492017-08-17 16:42:57 -0700216
Tao Baocf60a442018-06-18 14:56:20 -0700217TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700218 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700219 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700220}
221
Tao Baocf60a442018-06-18 14:56:20 -0700222TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700223 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700224
Tao Baobc4b1fe2017-04-17 16:46:05 -0700225 std::string device = android::base::GetProperty("ro.product.device", "");
226 ASSERT_NE("", device);
227 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
228 ASSERT_NE("", timestamp);
229 std::string metadata = android::base::Join(
230 std::vector<std::string>{
231 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
232 },
233 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700234
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700235 BuildZipArchive(
236 {
237 { "payload.bin", "" },
238 { "META-INF/com/android/metadata", metadata },
239 },
240 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700241
242 ZipArchiveHandle zip;
243 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
244 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700245 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700246 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700247 ASSERT_FALSE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700248 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700249}
Tianjie Xu69b96492017-08-17 16:42:57 -0700250
Tao Baocf60a442018-06-18 14:56:20 -0700251TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700252 std::string serialno = android::base::GetProperty("ro.serialno", "");
253 ASSERT_NE("", serialno);
254
255 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700256 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700257
258 static constexpr char alphabet[] =
259 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
260 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
261
262 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700263 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700264 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700265 generate_n(back_inserter(random_serialno), serialno.size(), generator);
266 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700267 }
268 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700269 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700270
Tao Baocf60a442018-06-18 14:56:20 -0700271 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700272 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700273 generate_n(back_inserter(long_serialno), serialno.size(), generator);
274 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700275 }
276 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700277 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700278}
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700279
Tao Bao36c72762019-04-30 00:25:41 -0700280static void TestCheckPackageMetadata(const std::string& metadata_string, OtaType ota_type,
281 bool exptected_result) {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700282 TemporaryFile temp_file;
283 BuildZipArchive(
284 {
285 { "META-INF/com/android/metadata", metadata_string },
286 },
287 temp_file.release(), kCompressStored);
288
289 ZipArchiveHandle zip;
290 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
291
292 std::map<std::string, std::string> metadata;
293 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
294 ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type));
295 CloseArchive(zip);
296}
297
298TEST(InstallTest, CheckPackageMetadata_ota_type) {
299 std::string device = android::base::GetProperty("ro.product.device", "");
300 ASSERT_NE("", device);
301
302 // ota-type must be present
303 std::string metadata = android::base::Join(
304 std::vector<std::string>{
305 "pre-device=" + device,
306 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
307 },
308 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700309 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700310
311 // Checks if ota-type matches
312 metadata = android::base::Join(
313 std::vector<std::string>{
314 "ota-type=AB",
315 "pre-device=" + device,
316 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
317 },
318 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700319 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700320
Tao Bao36c72762019-04-30 00:25:41 -0700321 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700322}
323
324TEST(InstallTest, CheckPackageMetadata_device_type) {
325 // device type can not be empty
326 std::string metadata = android::base::Join(
327 std::vector<std::string>{
328 "ota-type=BRICK",
329 },
330 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700331 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700332
333 // device type mismatches
334 metadata = android::base::Join(
335 std::vector<std::string>{
336 "ota-type=BRICK",
Tianjie78d15142020-07-22 17:40:09 -0700337 "pre-device=fake_device_type",
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700338 },
339 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700340 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700341}
342
343TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) {
344 std::string device = android::base::GetProperty("ro.product.device", "");
345 ASSERT_NE("", device);
346
347 // Serial number doesn't need to exist
348 std::string metadata = android::base::Join(
349 std::vector<std::string>{
350 "ota-type=BRICK",
351 "pre-device=" + device,
352 },
353 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700354 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700355
356 // Serial number mismatches
357 metadata = android::base::Join(
358 std::vector<std::string>{
359 "ota-type=BRICK",
360 "pre-device=" + device,
Tianjie78d15142020-07-22 17:40:09 -0700361 "serialno=fake_serial",
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700362 },
363 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700364 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700365
366 std::string serialno = android::base::GetProperty("ro.serialno", "");
367 ASSERT_NE("", serialno);
368 metadata = android::base::Join(
369 std::vector<std::string>{
370 "ota-type=BRICK",
371 "pre-device=" + device,
372 "serialno=" + serialno,
373 },
374 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700375 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700376}
377
378TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) {
379 std::string device = android::base::GetProperty("ro.product.device", "");
380 ASSERT_NE("", device);
381
382 std::string serialno = android::base::GetProperty("ro.serialno", "");
383 ASSERT_NE("", serialno);
384
385 std::vector<std::string> serial_numbers;
Tianjie78d15142020-07-22 17:40:09 -0700386 // Creates a fake serial number string.
xunchang7b08a5a2019-02-05 12:44:53 -0800387 for (char c = 'a'; c <= 'z'; c++) {
388 serial_numbers.emplace_back(serialno.size(), c);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700389 }
390
391 // No matched serialno found.
392 std::string metadata = android::base::Join(
393 std::vector<std::string>{
394 "ota-type=BRICK",
395 "pre-device=" + device,
396 "serialno=" + android::base::Join(serial_numbers, '|'),
397 },
398 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700399 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700400
401 serial_numbers.emplace_back(serialno);
402 std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine());
403 metadata = android::base::Join(
404 std::vector<std::string>{
405 "ota-type=BRICK",
406 "pre-device=" + device,
407 "serialno=" + android::base::Join(serial_numbers, '|'),
408 },
409 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700410 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700411}
412
413TEST(InstallTest, CheckPackageMetadata_ab_build_version) {
414 std::string device = android::base::GetProperty("ro.product.device", "");
415 ASSERT_NE("", device);
416
417 std::string build_version = android::base::GetProperty("ro.build.version.incremental", "");
418 ASSERT_NE("", build_version);
419
420 std::string metadata = android::base::Join(
421 std::vector<std::string>{
422 "ota-type=AB",
423 "pre-device=" + device,
424 "pre-build-incremental=" + build_version,
425 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
426 },
427 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700428 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700429
430 metadata = android::base::Join(
431 std::vector<std::string>{
432 "ota-type=AB",
433 "pre-device=" + device,
Tianjie78d15142020-07-22 17:40:09 -0700434 "pre-build-incremental=fake_build",
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700435 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
436 },
437 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700438 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700439}
440
441TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) {
442 std::string device = android::base::GetProperty("ro.product.device", "");
443 ASSERT_NE("", device);
444
445 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
446 ASSERT_NE("", finger_print);
447
448 std::string metadata = android::base::Join(
449 std::vector<std::string>{
450 "ota-type=AB",
451 "pre-device=" + device,
452 "pre-build=" + finger_print,
453 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
454 },
455 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700456 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700457
458 metadata = android::base::Join(
459 std::vector<std::string>{
460 "ota-type=AB",
461 "pre-device=" + device,
Tianjie78d15142020-07-22 17:40:09 -0700462 "pre-build=fake_build_fingerprint",
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700463 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
464 },
465 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700466 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700467}
468
Kelvin Zhange1ae78c2020-06-26 11:06:00 -0400469TEST(InstallTest, CheckPackageMetadata_dynamic_fingerprint) {
470 std::string device = android::base::GetProperty("ro.product.device", "");
471 ASSERT_FALSE(device.empty());
472
473 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
474 ASSERT_FALSE(finger_print.empty());
475
476 std::string metadata = android::base::Join(
477 std::vector<std::string>{
478 "ota-type=AB",
479 "pre-device=please|work|" + device + "|please|work",
480 "pre-build=" + finger_print = "pass|this|test",
481 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
482 },
483 "\n");
484 TestCheckPackageMetadata(metadata, OtaType::AB, true);
485
486 metadata = android::base::Join(
487 std::vector<std::string>{
488 "ota-type=AB",
489 "pre-device=" + device,
Tianjie78d15142020-07-22 17:40:09 -0700490 "pre-build=fake_build_fingerprint",
Kelvin Zhange1ae78c2020-06-26 11:06:00 -0400491 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
492 },
493 "\n");
494 TestCheckPackageMetadata(metadata, OtaType::AB, false);
495}
496
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700497TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) {
498 std::string device = android::base::GetProperty("ro.product.device", "");
499 ASSERT_NE("", device);
500
501 // post timestamp is required for upgrade.
502 std::string metadata = android::base::Join(
503 std::vector<std::string>{
504 "ota-type=AB",
505 "pre-device=" + device,
506 },
507 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700508 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700509
510 // post timestamp should be larger than the timestamp on device.
511 metadata = android::base::Join(
512 std::vector<std::string>{
513 "ota-type=AB",
514 "pre-device=" + device,
515 "post-timestamp=0",
516 },
517 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700518 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700519
520 // fingerprint is required for downgrade
521 metadata = android::base::Join(
522 std::vector<std::string>{
523 "ota-type=AB",
524 "pre-device=" + device,
525 "post-timestamp=0",
526 "ota-downgrade=yes",
527 },
528 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700529 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700530
531 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
532 ASSERT_NE("", finger_print);
533
534 metadata = android::base::Join(
535 std::vector<std::string>{
536 "ota-type=AB",
537 "pre-device=" + device,
538 "post-timestamp=0",
539 "pre-build=" + finger_print,
540 "ota-downgrade=yes",
541 },
542 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700543 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700544}
Tianjie Xucd8faf72019-08-06 12:32:05 -0700545
546TEST(InstallTest, SetupPackageMount_package_path) {
547 load_volume_table();
548 bool install_with_fuse;
549
550 // Setup should fail if the input path doesn't exist.
551 ASSERT_FALSE(SetupPackageMount("/does_not_exist", &install_with_fuse));
552
553 // Package should be installed with fuse if it's not in /cache.
554 TemporaryDir temp_dir;
555 TemporaryFile update_package(temp_dir.path);
556 ASSERT_TRUE(SetupPackageMount(update_package.path, &install_with_fuse));
557 ASSERT_TRUE(install_with_fuse);
558
559 // Setup should fail if the input path isn't canonicalized.
560 std::string uncanonical_package_path = android::base::Join(
561 std::vector<std::string>{
562 temp_dir.path,
563 "..",
564 android::base::Basename(temp_dir.path),
565 android::base::Basename(update_package.path),
566 },
567 '/');
568
569 ASSERT_EQ(0, access(uncanonical_package_path.c_str(), R_OK));
570 ASSERT_FALSE(SetupPackageMount(uncanonical_package_path, &install_with_fuse));
571}