blob: 5c6d58472be94515871c1960c98ad7fa0be9b6cc [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 <android-base/test_utils.h>
31#include <gtest/gtest.h>
Tao Baof2784b62017-04-19 12:37:12 -070032#include <vintf/VintfObjectRecovery.h>
Tao Bao1d866052017-04-10 16:55:57 -070033#include <ziparchive/zip_archive.h>
34#include <ziparchive/zip_writer.h>
35
36#include "install.h"
Tao Baocf60a442018-06-18 14:56:20 -070037#include "otautil/paths.h"
Tao Baobc4b1fe2017-04-17 16:46:05 -070038#include "private/install.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
Tao Baof2784b62017-04-19 12:37:12 -0700111TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) {
112 TemporaryFile compatibility_zip_file;
Tao Baof2784b62017-04-19 12:37:12 -0700113 std::string malformed_xml = "malformed";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700114 BuildZipArchive({ { "system_manifest.xml", malformed_xml } }, compatibility_zip_file.release(),
115 kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700116
117 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700118 std::string compatibility_zip_content;
119 ASSERT_TRUE(
120 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700121 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
122 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700123
124 ZipArchiveHandle zip;
125 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
126 std::vector<std::string> compatibility_info;
127 compatibility_info.push_back(malformed_xml);
128 // Malformed compatibility zip is expected to be rejected by libvintf. But we defer that to
129 // libvintf.
130 std::string err;
131 bool result =
132 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
133 ASSERT_EQ(result, verify_package_compatibility(zip));
134 CloseArchive(zip);
135}
136
137TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml) {
138 static constexpr const char* system_manifest_xml_path = "/system/manifest.xml";
139 if (access(system_manifest_xml_path, R_OK) == -1) {
140 GTEST_LOG_(INFO) << "Test skipped on devices w/o /system/manifest.xml.";
141 return;
142 }
143 std::string system_manifest_xml_content;
144 ASSERT_TRUE(
145 android::base::ReadFileToString(system_manifest_xml_path, &system_manifest_xml_content));
146 TemporaryFile compatibility_zip_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700147 BuildZipArchive({ { "system_manifest.xml", system_manifest_xml_content } },
148 compatibility_zip_file.release(), kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700149
150 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700151 std::string compatibility_zip_content;
152 ASSERT_TRUE(
153 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700154 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
155 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700156
157 ZipArchiveHandle zip;
158 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
159 std::vector<std::string> compatibility_info;
160 compatibility_info.push_back(system_manifest_xml_content);
161 std::string err;
162 bool result =
163 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
164 // Make sure the result is consistent with libvintf library.
165 ASSERT_EQ(result, verify_package_compatibility(zip));
166 CloseArchive(zip);
167}
168
Tao Baocf60a442018-06-18 14:56:20 -0700169TEST(InstallTest, SetUpNonAbUpdateCommands) {
170 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700171 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700172 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700173
174 ZipArchiveHandle zip;
175 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
176 int status_fd = 10;
177 std::string package = "/path/to/update.zip";
178 TemporaryDir td;
179 std::string binary_path = std::string(td.path) + "/update_binary";
180 Paths::Get().set_temporary_update_binary(binary_path);
181 std::vector<std::string> cmd;
182 ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
183 ASSERT_EQ(4U, cmd.size());
184 ASSERT_EQ(binary_path, cmd[0]);
185 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
186 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
187 ASSERT_EQ(package, cmd[3]);
188 struct stat sb;
189 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
190 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
191
192 // With non-zero retry count. update_binary will be removed automatically.
193 cmd.clear();
194 ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd));
195 ASSERT_EQ(5U, cmd.size());
196 ASSERT_EQ(binary_path, cmd[0]);
197 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
198 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
199 ASSERT_EQ(package, cmd[3]);
200 ASSERT_EQ("retry", cmd[4]);
201 sb = {};
202 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
203 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
204
205 CloseArchive(zip);
206}
207
208TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
209 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700210 // The archive must have something to be opened correctly.
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700211 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700212
213 // Missing update binary.
214 ZipArchiveHandle zip;
215 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
216 int status_fd = 10;
217 std::string package = "/path/to/update.zip";
218 TemporaryDir td;
219 Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary");
220 std::vector<std::string> cmd;
221 ASSERT_EQ(INSTALL_CORRUPT, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
222 CloseArchive(zip);
223}
224
225static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700226 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700227
Tao Baobc4b1fe2017-04-17 16:46:05 -0700228 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700229 std::string device = android::base::GetProperty("ro.product.device", "");
230 ASSERT_NE("", device);
231 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
232 ASSERT_NE("", timestamp);
Tianjie Xu69b96492017-08-17 16:42:57 -0700233
234 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
235 "post-timestamp=" + timestamp };
236 if (!serialno.empty()) {
237 meta.push_back("serialno=" + serialno);
238 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700239 std::string metadata_string = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700240
241 BuildZipArchive({ { "payload.bin", "" },
242 { "payload_properties.txt", properties },
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700243 { "META-INF/com/android/metadata", metadata_string } },
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700244 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700245
246 ZipArchiveHandle zip;
247 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tao Bao00d57572017-05-02 15:48:54 -0700248 ZipString payload_name("payload.bin");
249 ZipEntry payload_entry;
250 ASSERT_EQ(0, FindEntry(zip, payload_name, &payload_entry));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700251
252 std::map<std::string, std::string> metadata;
253 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
Tianjie Xu69b96492017-08-17 16:42:57 -0700254 if (success) {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700255 ASSERT_EQ(0, CheckPackageMetadata(metadata, OtaType::AB));
256
257 int status_fd = 10;
258 std::string package = "/path/to/update.zip";
259 std::vector<std::string> cmd;
Tao Baocf60a442018-06-18 14:56:20 -0700260 ASSERT_EQ(0, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700261 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700262 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700263 ASSERT_EQ("--payload=file://" + package, cmd[1]);
264 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
265 ASSERT_EQ("--headers=" + properties, cmd[3]);
266 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
267 } else {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700268 ASSERT_EQ(INSTALL_ERROR, CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu69b96492017-08-17 16:42:57 -0700269 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700270 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700271}
Tianjie Xu69b96492017-08-17 16:42:57 -0700272
Tao Baocf60a442018-06-18 14:56:20 -0700273TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700274 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700275 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700276}
277
Tao Baocf60a442018-06-18 14:56:20 -0700278TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700279 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700280
Tao Baobc4b1fe2017-04-17 16:46:05 -0700281 std::string device = android::base::GetProperty("ro.product.device", "");
282 ASSERT_NE("", device);
283 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
284 ASSERT_NE("", timestamp);
285 std::string metadata = android::base::Join(
286 std::vector<std::string>{
287 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
288 },
289 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700290
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700291 BuildZipArchive(
292 {
293 { "payload.bin", "" },
294 { "META-INF/com/android/metadata", metadata },
295 },
296 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700297
298 ZipArchiveHandle zip;
299 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
300 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700301 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700302 std::vector<std::string> cmd;
Tao Baocf60a442018-06-18 14:56:20 -0700303 ASSERT_EQ(INSTALL_CORRUPT, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700304 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700305}
Tianjie Xu69b96492017-08-17 16:42:57 -0700306
Tao Baocf60a442018-06-18 14:56:20 -0700307TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700308 std::string serialno = android::base::GetProperty("ro.serialno", "");
309 ASSERT_NE("", serialno);
310
311 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700312 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700313
314 static constexpr char alphabet[] =
315 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
316 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
317
318 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700319 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700320 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700321 generate_n(back_inserter(random_serialno), serialno.size(), generator);
322 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700323 }
324 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700325 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700326
Tao Baocf60a442018-06-18 14:56:20 -0700327 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700328 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700329 generate_n(back_inserter(long_serialno), serialno.size(), generator);
330 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700331 }
332 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700333 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700334}
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700335
336static void test_check_package_metadata(const std::string& metadata_string, OtaType ota_type,
337 int exptected_result) {
338 TemporaryFile temp_file;
339 BuildZipArchive(
340 {
341 { "META-INF/com/android/metadata", metadata_string },
342 },
343 temp_file.release(), kCompressStored);
344
345 ZipArchiveHandle zip;
346 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
347
348 std::map<std::string, std::string> metadata;
349 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
350 ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type));
351 CloseArchive(zip);
352}
353
354TEST(InstallTest, CheckPackageMetadata_ota_type) {
355 std::string device = android::base::GetProperty("ro.product.device", "");
356 ASSERT_NE("", device);
357
358 // ota-type must be present
359 std::string metadata = android::base::Join(
360 std::vector<std::string>{
361 "pre-device=" + device,
362 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
363 },
364 "\n");
365 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
366
367 // Checks if ota-type matches
368 metadata = android::base::Join(
369 std::vector<std::string>{
370 "ota-type=AB",
371 "pre-device=" + device,
372 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
373 },
374 "\n");
375 test_check_package_metadata(metadata, OtaType::AB, 0);
376
377 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
378}
379
380TEST(InstallTest, CheckPackageMetadata_device_type) {
381 // device type can not be empty
382 std::string metadata = android::base::Join(
383 std::vector<std::string>{
384 "ota-type=BRICK",
385 },
386 "\n");
387 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
388
389 // device type mismatches
390 metadata = android::base::Join(
391 std::vector<std::string>{
392 "ota-type=BRICK",
393 "pre-device=dummy_device_type",
394 },
395 "\n");
396 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
397}
398
399TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) {
400 std::string device = android::base::GetProperty("ro.product.device", "");
401 ASSERT_NE("", device);
402
403 // Serial number doesn't need to exist
404 std::string metadata = android::base::Join(
405 std::vector<std::string>{
406 "ota-type=BRICK",
407 "pre-device=" + device,
408 },
409 "\n");
410 test_check_package_metadata(metadata, OtaType::BRICK, 0);
411
412 // Serial number mismatches
413 metadata = android::base::Join(
414 std::vector<std::string>{
415 "ota-type=BRICK",
416 "pre-device=" + device,
417 "serialno=dummy_serial",
418 },
419 "\n");
420 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
421
422 std::string serialno = android::base::GetProperty("ro.serialno", "");
423 ASSERT_NE("", serialno);
424 metadata = android::base::Join(
425 std::vector<std::string>{
426 "ota-type=BRICK",
427 "pre-device=" + device,
428 "serialno=" + serialno,
429 },
430 "\n");
431 test_check_package_metadata(metadata, OtaType::BRICK, 0);
432}
433
434TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) {
435 std::string device = android::base::GetProperty("ro.product.device", "");
436 ASSERT_NE("", device);
437
438 std::string serialno = android::base::GetProperty("ro.serialno", "");
439 ASSERT_NE("", serialno);
440
441 std::vector<std::string> serial_numbers;
442 // Creates a dummy serial number string.
443 for (size_t c = 'a'; c <= 'z'; c++) {
444 serial_numbers.emplace_back(c, serialno.size());
445 }
446
447 // No matched serialno found.
448 std::string metadata = android::base::Join(
449 std::vector<std::string>{
450 "ota-type=BRICK",
451 "pre-device=" + device,
452 "serialno=" + android::base::Join(serial_numbers, '|'),
453 },
454 "\n");
455 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
456
457 serial_numbers.emplace_back(serialno);
458 std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine());
459 metadata = android::base::Join(
460 std::vector<std::string>{
461 "ota-type=BRICK",
462 "pre-device=" + device,
463 "serialno=" + android::base::Join(serial_numbers, '|'),
464 },
465 "\n");
466 test_check_package_metadata(metadata, OtaType::BRICK, 0);
467}
468
469TEST(InstallTest, CheckPackageMetadata_ab_build_version) {
470 std::string device = android::base::GetProperty("ro.product.device", "");
471 ASSERT_NE("", device);
472
473 std::string build_version = android::base::GetProperty("ro.build.version.incremental", "");
474 ASSERT_NE("", build_version);
475
476 std::string metadata = android::base::Join(
477 std::vector<std::string>{
478 "ota-type=AB",
479 "pre-device=" + device,
480 "pre-build-incremental=" + build_version,
481 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
482 },
483 "\n");
484 test_check_package_metadata(metadata, OtaType::AB, 0);
485
486 metadata = android::base::Join(
487 std::vector<std::string>{
488 "ota-type=AB",
489 "pre-device=" + device,
490 "pre-build-incremental=dummy_build",
491 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
492 },
493 "\n");
494 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
495}
496
497TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) {
498 std::string device = android::base::GetProperty("ro.product.device", "");
499 ASSERT_NE("", device);
500
501 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
502 ASSERT_NE("", finger_print);
503
504 std::string metadata = android::base::Join(
505 std::vector<std::string>{
506 "ota-type=AB",
507 "pre-device=" + device,
508 "pre-build=" + finger_print,
509 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
510 },
511 "\n");
512 test_check_package_metadata(metadata, OtaType::AB, 0);
513
514 metadata = android::base::Join(
515 std::vector<std::string>{
516 "ota-type=AB",
517 "pre-device=" + device,
518 "pre-build=dummy_build_fingerprint",
519 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
520 },
521 "\n");
522 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
523}
524
525TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) {
526 std::string device = android::base::GetProperty("ro.product.device", "");
527 ASSERT_NE("", device);
528
529 // post timestamp is required for upgrade.
530 std::string metadata = android::base::Join(
531 std::vector<std::string>{
532 "ota-type=AB",
533 "pre-device=" + device,
534 },
535 "\n");
536 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
537
538 // post timestamp should be larger than the timestamp on device.
539 metadata = android::base::Join(
540 std::vector<std::string>{
541 "ota-type=AB",
542 "pre-device=" + device,
543 "post-timestamp=0",
544 },
545 "\n");
546 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
547
548 // fingerprint is required for downgrade
549 metadata = android::base::Join(
550 std::vector<std::string>{
551 "ota-type=AB",
552 "pre-device=" + device,
553 "post-timestamp=0",
554 "ota-downgrade=yes",
555 },
556 "\n");
557 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
558
559 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
560 ASSERT_NE("", finger_print);
561
562 metadata = android::base::Join(
563 std::vector<std::string>{
564 "ota-type=AB",
565 "pre-device=" + device,
566 "post-timestamp=0",
567 "pre-build=" + finger_print,
568 "ota-downgrade=yes",
569 },
570 "\n");
571 test_check_package_metadata(metadata, OtaType::AB, 0);
572}