blob: 370fbdcc58659bd0eb3ca1b99e931dfff65f68ca [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"
Tao Bao1d866052017-04-10 16:55:57 -070038
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070039static void BuildZipArchive(const std::map<std::string, std::string>& file_map, int fd,
40 int compression_type) {
41 FILE* zip_file = fdopen(fd, "w");
Tao Bao1d866052017-04-10 16:55:57 -070042 ZipWriter writer(zip_file);
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070043 for (const auto& [name, content] : file_map) {
44 ASSERT_EQ(0, writer.StartEntry(name.c_str(), compression_type));
45 ASSERT_EQ(0, writer.WriteBytes(content.data(), content.size()));
46 ASSERT_EQ(0, writer.FinishEntry());
47 }
Tao Bao1d866052017-04-10 16:55:57 -070048 ASSERT_EQ(0, writer.Finish());
49 ASSERT_EQ(0, fclose(zip_file));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070050}
51
Tao Bao8a7afcc2017-04-18 22:05:50 -070052TEST(InstallTest, read_metadata_from_package_smoke) {
53 TemporaryFile temp_file;
Tianjie Xu93b5bf22018-10-25 10:39:01 -070054 const std::string content("abc=defg");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070055 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(),
56 kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070057
58 ZipArchiveHandle zip;
59 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -070060 std::map<std::string, std::string> metadata;
61 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
62 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070063 CloseArchive(zip);
64
65 TemporaryFile temp_file2;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070066 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(),
67 kCompressDeflated);
Tao Bao8a7afcc2017-04-18 22:05:50 -070068
69 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
70 metadata.clear();
Tianjie Xu93b5bf22018-10-25 10:39:01 -070071 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
72 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070073 CloseArchive(zip);
74}
75
76TEST(InstallTest, read_metadata_from_package_no_entry) {
77 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070078 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070079
80 ZipArchiveHandle zip;
81 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -070082 std::map<std::string, std::string> metadata;
83 ASSERT_FALSE(ReadMetadataFromPackage(zip, &metadata));
Tao Bao8a7afcc2017-04-18 22:05:50 -070084 CloseArchive(zip);
85}
86
xunchange0d991c2019-03-05 14:50:51 -080087TEST(InstallTest, read_wipe_ab_partition_list) {
88 std::vector<std::string> partition_list = {
89 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
90 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
91 "/dev/block/bootdevice/by-name/userdata", "# Wipe the boot partitions last",
92 "/dev/block/bootdevice/by-name/boot_a", "/dev/block/bootdevice/by-name/boot_b",
93 };
94 TemporaryFile temp_file;
95 BuildZipArchive({ { "recovery.wipe", android::base::Join(partition_list, '\n') } },
96 temp_file.release(), kCompressDeflated);
97 std::string wipe_package;
98 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &wipe_package));
99
xunchang55e3d222019-03-11 11:28:41 -0700100 auto package = Package::CreateMemoryPackage(
101 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
102
103 auto read_partition_list = GetWipePartitionList(package.get());
xunchange0d991c2019-03-05 14:50:51 -0800104 std::vector<std::string> expected = {
105 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
106 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
107 "/dev/block/bootdevice/by-name/userdata", "/dev/block/bootdevice/by-name/boot_a",
108 "/dev/block/bootdevice/by-name/boot_b",
109 };
110 ASSERT_EQ(expected, read_partition_list);
111}
112
Tao Baocf60a442018-06-18 14:56:20 -0700113TEST(InstallTest, SetUpNonAbUpdateCommands) {
114 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700115 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700116 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700117
118 ZipArchiveHandle zip;
119 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
120 int status_fd = 10;
121 std::string package = "/path/to/update.zip";
122 TemporaryDir td;
123 std::string binary_path = std::string(td.path) + "/update_binary";
124 Paths::Get().set_temporary_update_binary(binary_path);
125 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700126 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700127 ASSERT_EQ(4U, cmd.size());
128 ASSERT_EQ(binary_path, cmd[0]);
129 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
130 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
131 ASSERT_EQ(package, cmd[3]);
132 struct stat sb;
133 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
134 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
135
136 // With non-zero retry count. update_binary will be removed automatically.
137 cmd.clear();
Tao Bao36c72762019-04-30 00:25:41 -0700138 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700139 ASSERT_EQ(5U, cmd.size());
140 ASSERT_EQ(binary_path, cmd[0]);
141 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
142 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
143 ASSERT_EQ(package, cmd[3]);
144 ASSERT_EQ("retry", cmd[4]);
145 sb = {};
146 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
147 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
148
149 CloseArchive(zip);
150}
151
152TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
153 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700154 // The archive must have something to be opened correctly.
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700155 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700156
157 // Missing update binary.
158 ZipArchiveHandle zip;
159 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
160 int status_fd = 10;
161 std::string package = "/path/to/update.zip";
162 TemporaryDir td;
163 Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary");
164 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700165 ASSERT_FALSE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700166 CloseArchive(zip);
167}
168
169static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700170 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700171
Tao Baobc4b1fe2017-04-17 16:46:05 -0700172 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700173 std::string device = android::base::GetProperty("ro.product.device", "");
174 ASSERT_NE("", device);
175 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
176 ASSERT_NE("", timestamp);
Tianjie Xu69b96492017-08-17 16:42:57 -0700177
178 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
179 "post-timestamp=" + timestamp };
180 if (!serialno.empty()) {
181 meta.push_back("serialno=" + serialno);
182 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700183 std::string metadata_string = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700184
185 BuildZipArchive({ { "payload.bin", "" },
186 { "payload_properties.txt", properties },
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700187 { "META-INF/com/android/metadata", metadata_string } },
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700188 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700189
190 ZipArchiveHandle zip;
191 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tao Bao00d57572017-05-02 15:48:54 -0700192 ZipEntry payload_entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700193 ASSERT_EQ(0, FindEntry(zip, "payload.bin", &payload_entry));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700194
195 std::map<std::string, std::string> metadata;
196 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
Tianjie Xu69b96492017-08-17 16:42:57 -0700197 if (success) {
Tao Bao36c72762019-04-30 00:25:41 -0700198 ASSERT_TRUE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700199
200 int status_fd = 10;
201 std::string package = "/path/to/update.zip";
202 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700203 ASSERT_TRUE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700204 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700205 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700206 ASSERT_EQ("--payload=file://" + package, cmd[1]);
207 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
208 ASSERT_EQ("--headers=" + properties, cmd[3]);
209 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
210 } else {
Tao Bao36c72762019-04-30 00:25:41 -0700211 ASSERT_FALSE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu69b96492017-08-17 16:42:57 -0700212 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700213 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700214}
Tianjie Xu69b96492017-08-17 16:42:57 -0700215
Tao Baocf60a442018-06-18 14:56:20 -0700216TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700217 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700218 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700219}
220
Tao Baocf60a442018-06-18 14:56:20 -0700221TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700222 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700223
Tao Baobc4b1fe2017-04-17 16:46:05 -0700224 std::string device = android::base::GetProperty("ro.product.device", "");
225 ASSERT_NE("", device);
226 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
227 ASSERT_NE("", timestamp);
228 std::string metadata = android::base::Join(
229 std::vector<std::string>{
230 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
231 },
232 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700233
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700234 BuildZipArchive(
235 {
236 { "payload.bin", "" },
237 { "META-INF/com/android/metadata", metadata },
238 },
239 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700240
241 ZipArchiveHandle zip;
242 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
243 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700244 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700245 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700246 ASSERT_FALSE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700247 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700248}
Tianjie Xu69b96492017-08-17 16:42:57 -0700249
Tao Baocf60a442018-06-18 14:56:20 -0700250TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700251 std::string serialno = android::base::GetProperty("ro.serialno", "");
252 ASSERT_NE("", serialno);
253
254 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700255 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700256
257 static constexpr char alphabet[] =
258 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
259 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
260
261 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700262 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700263 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700264 generate_n(back_inserter(random_serialno), serialno.size(), generator);
265 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700266 }
267 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700268 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700269
Tao Baocf60a442018-06-18 14:56:20 -0700270 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700271 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700272 generate_n(back_inserter(long_serialno), serialno.size(), generator);
273 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700274 }
275 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700276 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700277}
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700278
Tao Bao36c72762019-04-30 00:25:41 -0700279static void TestCheckPackageMetadata(const std::string& metadata_string, OtaType ota_type,
280 bool exptected_result) {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700281 TemporaryFile temp_file;
282 BuildZipArchive(
283 {
284 { "META-INF/com/android/metadata", metadata_string },
285 },
286 temp_file.release(), kCompressStored);
287
288 ZipArchiveHandle zip;
289 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
290
291 std::map<std::string, std::string> metadata;
292 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
293 ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type));
294 CloseArchive(zip);
295}
296
297TEST(InstallTest, CheckPackageMetadata_ota_type) {
298 std::string device = android::base::GetProperty("ro.product.device", "");
299 ASSERT_NE("", device);
300
301 // ota-type must be present
302 std::string metadata = android::base::Join(
303 std::vector<std::string>{
304 "pre-device=" + device,
305 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
306 },
307 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700308 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700309
310 // Checks if ota-type matches
311 metadata = android::base::Join(
312 std::vector<std::string>{
313 "ota-type=AB",
314 "pre-device=" + device,
315 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
316 },
317 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700318 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700319
Tao Bao36c72762019-04-30 00:25:41 -0700320 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700321}
322
323TEST(InstallTest, CheckPackageMetadata_device_type) {
324 // device type can not be empty
325 std::string metadata = android::base::Join(
326 std::vector<std::string>{
327 "ota-type=BRICK",
328 },
329 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700330 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700331
332 // device type mismatches
333 metadata = android::base::Join(
334 std::vector<std::string>{
335 "ota-type=BRICK",
336 "pre-device=dummy_device_type",
337 },
338 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700339 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700340}
341
342TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) {
343 std::string device = android::base::GetProperty("ro.product.device", "");
344 ASSERT_NE("", device);
345
346 // Serial number doesn't need to exist
347 std::string metadata = android::base::Join(
348 std::vector<std::string>{
349 "ota-type=BRICK",
350 "pre-device=" + device,
351 },
352 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700353 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700354
355 // Serial number mismatches
356 metadata = android::base::Join(
357 std::vector<std::string>{
358 "ota-type=BRICK",
359 "pre-device=" + device,
360 "serialno=dummy_serial",
361 },
362 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700363 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700364
365 std::string serialno = android::base::GetProperty("ro.serialno", "");
366 ASSERT_NE("", serialno);
367 metadata = android::base::Join(
368 std::vector<std::string>{
369 "ota-type=BRICK",
370 "pre-device=" + device,
371 "serialno=" + serialno,
372 },
373 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700374 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700375}
376
377TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) {
378 std::string device = android::base::GetProperty("ro.product.device", "");
379 ASSERT_NE("", device);
380
381 std::string serialno = android::base::GetProperty("ro.serialno", "");
382 ASSERT_NE("", serialno);
383
384 std::vector<std::string> serial_numbers;
385 // Creates a dummy serial number string.
xunchang7b08a5a2019-02-05 12:44:53 -0800386 for (char c = 'a'; c <= 'z'; c++) {
387 serial_numbers.emplace_back(serialno.size(), c);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700388 }
389
390 // No matched serialno found.
391 std::string metadata = android::base::Join(
392 std::vector<std::string>{
393 "ota-type=BRICK",
394 "pre-device=" + device,
395 "serialno=" + android::base::Join(serial_numbers, '|'),
396 },
397 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700398 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700399
400 serial_numbers.emplace_back(serialno);
401 std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine());
402 metadata = android::base::Join(
403 std::vector<std::string>{
404 "ota-type=BRICK",
405 "pre-device=" + device,
406 "serialno=" + android::base::Join(serial_numbers, '|'),
407 },
408 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700409 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700410}
411
412TEST(InstallTest, CheckPackageMetadata_ab_build_version) {
413 std::string device = android::base::GetProperty("ro.product.device", "");
414 ASSERT_NE("", device);
415
416 std::string build_version = android::base::GetProperty("ro.build.version.incremental", "");
417 ASSERT_NE("", build_version);
418
419 std::string metadata = android::base::Join(
420 std::vector<std::string>{
421 "ota-type=AB",
422 "pre-device=" + device,
423 "pre-build-incremental=" + build_version,
424 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
425 },
426 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700427 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700428
429 metadata = android::base::Join(
430 std::vector<std::string>{
431 "ota-type=AB",
432 "pre-device=" + device,
433 "pre-build-incremental=dummy_build",
434 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
435 },
436 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700437 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700438}
439
440TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) {
441 std::string device = android::base::GetProperty("ro.product.device", "");
442 ASSERT_NE("", device);
443
444 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
445 ASSERT_NE("", finger_print);
446
447 std::string metadata = android::base::Join(
448 std::vector<std::string>{
449 "ota-type=AB",
450 "pre-device=" + device,
451 "pre-build=" + finger_print,
452 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
453 },
454 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700455 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700456
457 metadata = android::base::Join(
458 std::vector<std::string>{
459 "ota-type=AB",
460 "pre-device=" + device,
461 "pre-build=dummy_build_fingerprint",
462 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
463 },
464 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700465 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700466}
467
468TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) {
469 std::string device = android::base::GetProperty("ro.product.device", "");
470 ASSERT_NE("", device);
471
472 // post timestamp is required for upgrade.
473 std::string metadata = android::base::Join(
474 std::vector<std::string>{
475 "ota-type=AB",
476 "pre-device=" + device,
477 },
478 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700479 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700480
481 // post timestamp should be larger than the timestamp on device.
482 metadata = android::base::Join(
483 std::vector<std::string>{
484 "ota-type=AB",
485 "pre-device=" + device,
486 "post-timestamp=0",
487 },
488 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700489 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700490
491 // fingerprint is required for downgrade
492 metadata = android::base::Join(
493 std::vector<std::string>{
494 "ota-type=AB",
495 "pre-device=" + device,
496 "post-timestamp=0",
497 "ota-downgrade=yes",
498 },
499 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700500 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700501
502 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
503 ASSERT_NE("", finger_print);
504
505 metadata = android::base::Join(
506 std::vector<std::string>{
507 "ota-type=AB",
508 "pre-device=" + device,
509 "post-timestamp=0",
510 "pre-build=" + finger_print,
511 "ota-downgrade=yes",
512 },
513 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700514 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700515}