blob: 4ec40990882610283fe1228e2d1c6d71e1d73f8d [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>
Tao Baof2784b62017-04-19 12:37:12 -070031#include <vintf/VintfObjectRecovery.h>
Tao Bao1d866052017-04-10 16:55:57 -070032#include <ziparchive/zip_archive.h>
33#include <ziparchive/zip_writer.h>
34
xunchang24788852019-03-22 16:08:52 -070035#include "install/install.h"
Tao Bao7f19d102019-04-26 22:56:56 -070036#include "install/wipe_device.h"
Tao Baocf60a442018-06-18 14:56:20 -070037#include "otautil/paths.h"
xunchang24788852019-03-22 16:08:52 -070038#include "private/setup_commands.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
53TEST(InstallTest, verify_package_compatibility_no_entry) {
54 TemporaryFile temp_file;
55 // The archive must have something to be opened correctly.
56 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao1d866052017-04-10 16:55:57 -070057
58 // Doesn't contain compatibility zip entry.
59 ZipArchiveHandle zip;
60 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
61 ASSERT_TRUE(verify_package_compatibility(zip));
62 CloseArchive(zip);
63}
64
65TEST(InstallTest, verify_package_compatibility_invalid_entry) {
66 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070067 BuildZipArchive({ { "compatibility.zip", "" } }, temp_file.release(), kCompressStored);
Tao Bao1d866052017-04-10 16:55:57 -070068
69 // Empty compatibility zip entry.
70 ZipArchiveHandle zip;
71 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
72 ASSERT_FALSE(verify_package_compatibility(zip));
73 CloseArchive(zip);
74}
Tao Baobc4b1fe2017-04-17 16:46:05 -070075
Tao Bao8a7afcc2017-04-18 22:05:50 -070076TEST(InstallTest, read_metadata_from_package_smoke) {
77 TemporaryFile temp_file;
Tianjie Xu93b5bf22018-10-25 10:39:01 -070078 const std::string content("abc=defg");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070079 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(),
80 kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070081
82 ZipArchiveHandle zip;
83 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -070084 std::map<std::string, std::string> metadata;
85 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
86 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070087 CloseArchive(zip);
88
89 TemporaryFile temp_file2;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070090 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(),
91 kCompressDeflated);
Tao Bao8a7afcc2017-04-18 22:05:50 -070092
93 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
94 metadata.clear();
Tianjie Xu93b5bf22018-10-25 10:39:01 -070095 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
96 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070097 CloseArchive(zip);
98}
99
100TEST(InstallTest, read_metadata_from_package_no_entry) {
101 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700102 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -0700103
104 ZipArchiveHandle zip;
105 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700106 std::map<std::string, std::string> metadata;
107 ASSERT_FALSE(ReadMetadataFromPackage(zip, &metadata));
Tao Bao8a7afcc2017-04-18 22:05:50 -0700108 CloseArchive(zip);
109}
110
xunchange0d991c2019-03-05 14:50:51 -0800111TEST(InstallTest, read_wipe_ab_partition_list) {
112 std::vector<std::string> partition_list = {
113 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
114 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
115 "/dev/block/bootdevice/by-name/userdata", "# Wipe the boot partitions last",
116 "/dev/block/bootdevice/by-name/boot_a", "/dev/block/bootdevice/by-name/boot_b",
117 };
118 TemporaryFile temp_file;
119 BuildZipArchive({ { "recovery.wipe", android::base::Join(partition_list, '\n') } },
120 temp_file.release(), kCompressDeflated);
121 std::string wipe_package;
122 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &wipe_package));
123
xunchang55e3d222019-03-11 11:28:41 -0700124 auto package = Package::CreateMemoryPackage(
125 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
126
127 auto read_partition_list = GetWipePartitionList(package.get());
xunchange0d991c2019-03-05 14:50:51 -0800128 std::vector<std::string> expected = {
129 "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b",
130 "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b",
131 "/dev/block/bootdevice/by-name/userdata", "/dev/block/bootdevice/by-name/boot_a",
132 "/dev/block/bootdevice/by-name/boot_b",
133 };
134 ASSERT_EQ(expected, read_partition_list);
135}
136
Tao Baof2784b62017-04-19 12:37:12 -0700137TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) {
138 TemporaryFile compatibility_zip_file;
Tao Baof2784b62017-04-19 12:37:12 -0700139 std::string malformed_xml = "malformed";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700140 BuildZipArchive({ { "system_manifest.xml", malformed_xml } }, compatibility_zip_file.release(),
141 kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700142
143 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700144 std::string compatibility_zip_content;
145 ASSERT_TRUE(
146 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700147 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
148 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700149
150 ZipArchiveHandle zip;
151 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
152 std::vector<std::string> compatibility_info;
153 compatibility_info.push_back(malformed_xml);
154 // Malformed compatibility zip is expected to be rejected by libvintf. But we defer that to
155 // libvintf.
156 std::string err;
157 bool result =
158 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
159 ASSERT_EQ(result, verify_package_compatibility(zip));
160 CloseArchive(zip);
161}
162
163TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml) {
164 static constexpr const char* system_manifest_xml_path = "/system/manifest.xml";
165 if (access(system_manifest_xml_path, R_OK) == -1) {
166 GTEST_LOG_(INFO) << "Test skipped on devices w/o /system/manifest.xml.";
167 return;
168 }
169 std::string system_manifest_xml_content;
170 ASSERT_TRUE(
171 android::base::ReadFileToString(system_manifest_xml_path, &system_manifest_xml_content));
172 TemporaryFile compatibility_zip_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700173 BuildZipArchive({ { "system_manifest.xml", system_manifest_xml_content } },
174 compatibility_zip_file.release(), kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700175
176 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700177 std::string compatibility_zip_content;
178 ASSERT_TRUE(
179 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700180 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
181 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700182
183 ZipArchiveHandle zip;
184 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
185 std::vector<std::string> compatibility_info;
186 compatibility_info.push_back(system_manifest_xml_content);
187 std::string err;
188 bool result =
189 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
190 // Make sure the result is consistent with libvintf library.
191 ASSERT_EQ(result, verify_package_compatibility(zip));
192 CloseArchive(zip);
193}
194
Tao Baocf60a442018-06-18 14:56:20 -0700195TEST(InstallTest, SetUpNonAbUpdateCommands) {
196 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700197 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700198 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700199
200 ZipArchiveHandle zip;
201 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
202 int status_fd = 10;
203 std::string package = "/path/to/update.zip";
204 TemporaryDir td;
205 std::string binary_path = std::string(td.path) + "/update_binary";
206 Paths::Get().set_temporary_update_binary(binary_path);
207 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700208 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700209 ASSERT_EQ(4U, cmd.size());
210 ASSERT_EQ(binary_path, cmd[0]);
211 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
212 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
213 ASSERT_EQ(package, cmd[3]);
214 struct stat sb;
215 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
216 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
217
218 // With non-zero retry count. update_binary will be removed automatically.
219 cmd.clear();
Tao Bao36c72762019-04-30 00:25:41 -0700220 ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700221 ASSERT_EQ(5U, cmd.size());
222 ASSERT_EQ(binary_path, cmd[0]);
223 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
224 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
225 ASSERT_EQ(package, cmd[3]);
226 ASSERT_EQ("retry", cmd[4]);
227 sb = {};
228 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
229 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
230
231 CloseArchive(zip);
232}
233
234TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
235 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700236 // The archive must have something to be opened correctly.
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700237 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700238
239 // Missing update binary.
240 ZipArchiveHandle zip;
241 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
242 int status_fd = 10;
243 std::string package = "/path/to/update.zip";
244 TemporaryDir td;
245 Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary");
246 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700247 ASSERT_FALSE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
Tao Baocf60a442018-06-18 14:56:20 -0700248 CloseArchive(zip);
249}
250
251static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700252 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700253
Tao Baobc4b1fe2017-04-17 16:46:05 -0700254 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700255 std::string device = android::base::GetProperty("ro.product.device", "");
256 ASSERT_NE("", device);
257 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
258 ASSERT_NE("", timestamp);
Tianjie Xu69b96492017-08-17 16:42:57 -0700259
260 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
261 "post-timestamp=" + timestamp };
262 if (!serialno.empty()) {
263 meta.push_back("serialno=" + serialno);
264 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700265 std::string metadata_string = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700266
267 BuildZipArchive({ { "payload.bin", "" },
268 { "payload_properties.txt", properties },
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700269 { "META-INF/com/android/metadata", metadata_string } },
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700270 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700271
272 ZipArchiveHandle zip;
273 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tao Bao00d57572017-05-02 15:48:54 -0700274 ZipEntry payload_entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700275 ASSERT_EQ(0, FindEntry(zip, "payload.bin", &payload_entry));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700276
277 std::map<std::string, std::string> metadata;
278 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
Tianjie Xu69b96492017-08-17 16:42:57 -0700279 if (success) {
Tao Bao36c72762019-04-30 00:25:41 -0700280 ASSERT_TRUE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700281
282 int status_fd = 10;
283 std::string package = "/path/to/update.zip";
284 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700285 ASSERT_TRUE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700286 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700287 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700288 ASSERT_EQ("--payload=file://" + package, cmd[1]);
289 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
290 ASSERT_EQ("--headers=" + properties, cmd[3]);
291 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
292 } else {
Tao Bao36c72762019-04-30 00:25:41 -0700293 ASSERT_FALSE(CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu69b96492017-08-17 16:42:57 -0700294 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700295 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700296}
Tianjie Xu69b96492017-08-17 16:42:57 -0700297
Tao Baocf60a442018-06-18 14:56:20 -0700298TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700299 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700300 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700301}
302
Tao Baocf60a442018-06-18 14:56:20 -0700303TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700304 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700305
Tao Baobc4b1fe2017-04-17 16:46:05 -0700306 std::string device = android::base::GetProperty("ro.product.device", "");
307 ASSERT_NE("", device);
308 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
309 ASSERT_NE("", timestamp);
310 std::string metadata = android::base::Join(
311 std::vector<std::string>{
312 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
313 },
314 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700315
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700316 BuildZipArchive(
317 {
318 { "payload.bin", "" },
319 { "META-INF/com/android/metadata", metadata },
320 },
321 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700322
323 ZipArchiveHandle zip;
324 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
325 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700326 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700327 std::vector<std::string> cmd;
Tao Bao36c72762019-04-30 00:25:41 -0700328 ASSERT_FALSE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700329 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700330}
Tianjie Xu69b96492017-08-17 16:42:57 -0700331
Tao Baocf60a442018-06-18 14:56:20 -0700332TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700333 std::string serialno = android::base::GetProperty("ro.serialno", "");
334 ASSERT_NE("", serialno);
335
336 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700337 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700338
339 static constexpr char alphabet[] =
340 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
341 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
342
343 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700344 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700345 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700346 generate_n(back_inserter(random_serialno), serialno.size(), generator);
347 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700348 }
349 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700350 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700351
Tao Baocf60a442018-06-18 14:56:20 -0700352 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700353 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700354 generate_n(back_inserter(long_serialno), serialno.size(), generator);
355 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700356 }
357 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700358 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700359}
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700360
Tao Bao36c72762019-04-30 00:25:41 -0700361static void TestCheckPackageMetadata(const std::string& metadata_string, OtaType ota_type,
362 bool exptected_result) {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700363 TemporaryFile temp_file;
364 BuildZipArchive(
365 {
366 { "META-INF/com/android/metadata", metadata_string },
367 },
368 temp_file.release(), kCompressStored);
369
370 ZipArchiveHandle zip;
371 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
372
373 std::map<std::string, std::string> metadata;
374 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
375 ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type));
376 CloseArchive(zip);
377}
378
379TEST(InstallTest, CheckPackageMetadata_ota_type) {
380 std::string device = android::base::GetProperty("ro.product.device", "");
381 ASSERT_NE("", device);
382
383 // ota-type must be present
384 std::string metadata = android::base::Join(
385 std::vector<std::string>{
386 "pre-device=" + device,
387 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
388 },
389 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700390 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700391
392 // Checks if ota-type matches
393 metadata = android::base::Join(
394 std::vector<std::string>{
395 "ota-type=AB",
396 "pre-device=" + device,
397 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
398 },
399 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700400 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700401
Tao Bao36c72762019-04-30 00:25:41 -0700402 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700403}
404
405TEST(InstallTest, CheckPackageMetadata_device_type) {
406 // device type can not be empty
407 std::string metadata = android::base::Join(
408 std::vector<std::string>{
409 "ota-type=BRICK",
410 },
411 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700412 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700413
414 // device type mismatches
415 metadata = android::base::Join(
416 std::vector<std::string>{
417 "ota-type=BRICK",
418 "pre-device=dummy_device_type",
419 },
420 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700421 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700422}
423
424TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) {
425 std::string device = android::base::GetProperty("ro.product.device", "");
426 ASSERT_NE("", device);
427
428 // Serial number doesn't need to exist
429 std::string metadata = android::base::Join(
430 std::vector<std::string>{
431 "ota-type=BRICK",
432 "pre-device=" + device,
433 },
434 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700435 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700436
437 // Serial number mismatches
438 metadata = android::base::Join(
439 std::vector<std::string>{
440 "ota-type=BRICK",
441 "pre-device=" + device,
442 "serialno=dummy_serial",
443 },
444 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700445 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700446
447 std::string serialno = android::base::GetProperty("ro.serialno", "");
448 ASSERT_NE("", serialno);
449 metadata = android::base::Join(
450 std::vector<std::string>{
451 "ota-type=BRICK",
452 "pre-device=" + device,
453 "serialno=" + serialno,
454 },
455 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700456 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700457}
458
459TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) {
460 std::string device = android::base::GetProperty("ro.product.device", "");
461 ASSERT_NE("", device);
462
463 std::string serialno = android::base::GetProperty("ro.serialno", "");
464 ASSERT_NE("", serialno);
465
466 std::vector<std::string> serial_numbers;
467 // Creates a dummy serial number string.
xunchang7b08a5a2019-02-05 12:44:53 -0800468 for (char c = 'a'; c <= 'z'; c++) {
469 serial_numbers.emplace_back(serialno.size(), c);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700470 }
471
472 // No matched serialno found.
473 std::string metadata = android::base::Join(
474 std::vector<std::string>{
475 "ota-type=BRICK",
476 "pre-device=" + device,
477 "serialno=" + android::base::Join(serial_numbers, '|'),
478 },
479 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700480 TestCheckPackageMetadata(metadata, OtaType::BRICK, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700481
482 serial_numbers.emplace_back(serialno);
483 std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine());
484 metadata = android::base::Join(
485 std::vector<std::string>{
486 "ota-type=BRICK",
487 "pre-device=" + device,
488 "serialno=" + android::base::Join(serial_numbers, '|'),
489 },
490 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700491 TestCheckPackageMetadata(metadata, OtaType::BRICK, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700492}
493
494TEST(InstallTest, CheckPackageMetadata_ab_build_version) {
495 std::string device = android::base::GetProperty("ro.product.device", "");
496 ASSERT_NE("", device);
497
498 std::string build_version = android::base::GetProperty("ro.build.version.incremental", "");
499 ASSERT_NE("", build_version);
500
501 std::string metadata = android::base::Join(
502 std::vector<std::string>{
503 "ota-type=AB",
504 "pre-device=" + device,
505 "pre-build-incremental=" + build_version,
506 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
507 },
508 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700509 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700510
511 metadata = android::base::Join(
512 std::vector<std::string>{
513 "ota-type=AB",
514 "pre-device=" + device,
515 "pre-build-incremental=dummy_build",
516 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
517 },
518 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700519 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700520}
521
522TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) {
523 std::string device = android::base::GetProperty("ro.product.device", "");
524 ASSERT_NE("", device);
525
526 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
527 ASSERT_NE("", finger_print);
528
529 std::string metadata = android::base::Join(
530 std::vector<std::string>{
531 "ota-type=AB",
532 "pre-device=" + device,
533 "pre-build=" + finger_print,
534 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
535 },
536 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700537 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700538
539 metadata = android::base::Join(
540 std::vector<std::string>{
541 "ota-type=AB",
542 "pre-device=" + device,
543 "pre-build=dummy_build_fingerprint",
544 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
545 },
546 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700547 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700548}
549
550TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) {
551 std::string device = android::base::GetProperty("ro.product.device", "");
552 ASSERT_NE("", device);
553
554 // post timestamp is required for upgrade.
555 std::string metadata = android::base::Join(
556 std::vector<std::string>{
557 "ota-type=AB",
558 "pre-device=" + device,
559 },
560 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700561 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700562
563 // post timestamp should be larger than the timestamp on device.
564 metadata = android::base::Join(
565 std::vector<std::string>{
566 "ota-type=AB",
567 "pre-device=" + device,
568 "post-timestamp=0",
569 },
570 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700571 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700572
573 // fingerprint is required for downgrade
574 metadata = android::base::Join(
575 std::vector<std::string>{
576 "ota-type=AB",
577 "pre-device=" + device,
578 "post-timestamp=0",
579 "ota-downgrade=yes",
580 },
581 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700582 TestCheckPackageMetadata(metadata, OtaType::AB, false);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700583
584 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
585 ASSERT_NE("", finger_print);
586
587 metadata = android::base::Join(
588 std::vector<std::string>{
589 "ota-type=AB",
590 "pre-device=" + device,
591 "post-timestamp=0",
592 "pre-build=" + finger_print,
593 "ota-downgrade=yes",
594 },
595 "\n");
Tao Bao36c72762019-04-30 00:25:41 -0700596 TestCheckPackageMetadata(metadata, OtaType::AB, true);
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700597}