blob: 27a01cb32f9f7fdd16f4179f137ab0378b8a8f2e [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
35#include "install.h"
Tao Baocf60a442018-06-18 14:56:20 -070036#include "otautil/paths.h"
Tao Baobc4b1fe2017-04-17 16:46:05 -070037#include "private/install.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
52TEST(InstallTest, verify_package_compatibility_no_entry) {
53 TemporaryFile temp_file;
54 // The archive must have something to be opened correctly.
55 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao1d866052017-04-10 16:55:57 -070056
57 // Doesn't contain compatibility zip entry.
58 ZipArchiveHandle zip;
59 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
60 ASSERT_TRUE(verify_package_compatibility(zip));
61 CloseArchive(zip);
62}
63
64TEST(InstallTest, verify_package_compatibility_invalid_entry) {
65 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070066 BuildZipArchive({ { "compatibility.zip", "" } }, temp_file.release(), kCompressStored);
Tao Bao1d866052017-04-10 16:55:57 -070067
68 // Empty compatibility zip entry.
69 ZipArchiveHandle zip;
70 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
71 ASSERT_FALSE(verify_package_compatibility(zip));
72 CloseArchive(zip);
73}
Tao Baobc4b1fe2017-04-17 16:46:05 -070074
Tao Bao8a7afcc2017-04-18 22:05:50 -070075TEST(InstallTest, read_metadata_from_package_smoke) {
76 TemporaryFile temp_file;
Tianjie Xu93b5bf22018-10-25 10:39:01 -070077 const std::string content("abc=defg");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070078 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(),
79 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_TRUE(ReadMetadataFromPackage(zip, &metadata));
85 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070086 CloseArchive(zip);
87
88 TemporaryFile temp_file2;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070089 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(),
90 kCompressDeflated);
Tao Bao8a7afcc2017-04-18 22:05:50 -070091
92 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
93 metadata.clear();
Tianjie Xu93b5bf22018-10-25 10:39:01 -070094 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
95 ASSERT_EQ("defg", metadata["abc"]);
Tao Bao8a7afcc2017-04-18 22:05:50 -070096 CloseArchive(zip);
97}
98
99TEST(InstallTest, read_metadata_from_package_no_entry) {
100 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700101 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -0700102
103 ZipArchiveHandle zip;
104 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700105 std::map<std::string, std::string> metadata;
106 ASSERT_FALSE(ReadMetadataFromPackage(zip, &metadata));
Tao Bao8a7afcc2017-04-18 22:05:50 -0700107 CloseArchive(zip);
108}
109
Tao Baof2784b62017-04-19 12:37:12 -0700110TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) {
111 TemporaryFile compatibility_zip_file;
Tao Baof2784b62017-04-19 12:37:12 -0700112 std::string malformed_xml = "malformed";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700113 BuildZipArchive({ { "system_manifest.xml", malformed_xml } }, compatibility_zip_file.release(),
114 kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700115
116 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700117 std::string compatibility_zip_content;
118 ASSERT_TRUE(
119 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700120 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
121 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700122
123 ZipArchiveHandle zip;
124 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
125 std::vector<std::string> compatibility_info;
126 compatibility_info.push_back(malformed_xml);
127 // Malformed compatibility zip is expected to be rejected by libvintf. But we defer that to
128 // libvintf.
129 std::string err;
130 bool result =
131 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
132 ASSERT_EQ(result, verify_package_compatibility(zip));
133 CloseArchive(zip);
134}
135
136TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml) {
137 static constexpr const char* system_manifest_xml_path = "/system/manifest.xml";
138 if (access(system_manifest_xml_path, R_OK) == -1) {
139 GTEST_LOG_(INFO) << "Test skipped on devices w/o /system/manifest.xml.";
140 return;
141 }
142 std::string system_manifest_xml_content;
143 ASSERT_TRUE(
144 android::base::ReadFileToString(system_manifest_xml_path, &system_manifest_xml_content));
145 TemporaryFile compatibility_zip_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700146 BuildZipArchive({ { "system_manifest.xml", system_manifest_xml_content } },
147 compatibility_zip_file.release(), kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700148
149 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700150 std::string compatibility_zip_content;
151 ASSERT_TRUE(
152 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700153 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
154 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700155
156 ZipArchiveHandle zip;
157 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
158 std::vector<std::string> compatibility_info;
159 compatibility_info.push_back(system_manifest_xml_content);
160 std::string err;
161 bool result =
162 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
163 // Make sure the result is consistent with libvintf library.
164 ASSERT_EQ(result, verify_package_compatibility(zip));
165 CloseArchive(zip);
166}
167
Tao Baocf60a442018-06-18 14:56:20 -0700168TEST(InstallTest, SetUpNonAbUpdateCommands) {
169 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700170 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700171 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700172
173 ZipArchiveHandle zip;
174 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
175 int status_fd = 10;
176 std::string package = "/path/to/update.zip";
177 TemporaryDir td;
178 std::string binary_path = std::string(td.path) + "/update_binary";
179 Paths::Get().set_temporary_update_binary(binary_path);
180 std::vector<std::string> cmd;
181 ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
182 ASSERT_EQ(4U, cmd.size());
183 ASSERT_EQ(binary_path, cmd[0]);
184 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
185 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
186 ASSERT_EQ(package, cmd[3]);
187 struct stat sb;
188 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
189 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
190
191 // With non-zero retry count. update_binary will be removed automatically.
192 cmd.clear();
193 ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd));
194 ASSERT_EQ(5U, cmd.size());
195 ASSERT_EQ(binary_path, cmd[0]);
196 ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION
197 ASSERT_EQ(std::to_string(status_fd), cmd[2]);
198 ASSERT_EQ(package, cmd[3]);
199 ASSERT_EQ("retry", cmd[4]);
200 sb = {};
201 ASSERT_EQ(0, stat(binary_path.c_str(), &sb));
202 ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
203
204 CloseArchive(zip);
205}
206
207TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
208 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700209 // The archive must have something to be opened correctly.
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700210 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700211
212 // Missing update binary.
213 ZipArchiveHandle zip;
214 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
215 int status_fd = 10;
216 std::string package = "/path/to/update.zip";
217 TemporaryDir td;
218 Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary");
219 std::vector<std::string> cmd;
220 ASSERT_EQ(INSTALL_CORRUPT, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd));
221 CloseArchive(zip);
222}
223
224static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700225 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700226
Tao Baobc4b1fe2017-04-17 16:46:05 -0700227 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700228 std::string device = android::base::GetProperty("ro.product.device", "");
229 ASSERT_NE("", device);
230 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
231 ASSERT_NE("", timestamp);
Tianjie Xu69b96492017-08-17 16:42:57 -0700232
233 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
234 "post-timestamp=" + timestamp };
235 if (!serialno.empty()) {
236 meta.push_back("serialno=" + serialno);
237 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700238 std::string metadata_string = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700239
240 BuildZipArchive({ { "payload.bin", "" },
241 { "payload_properties.txt", properties },
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700242 { "META-INF/com/android/metadata", metadata_string } },
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700243 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700244
245 ZipArchiveHandle zip;
246 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tao Bao00d57572017-05-02 15:48:54 -0700247 ZipString payload_name("payload.bin");
248 ZipEntry payload_entry;
249 ASSERT_EQ(0, FindEntry(zip, payload_name, &payload_entry));
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700250
251 std::map<std::string, std::string> metadata;
252 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
Tianjie Xu69b96492017-08-17 16:42:57 -0700253 if (success) {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700254 ASSERT_EQ(0, CheckPackageMetadata(metadata, OtaType::AB));
255
256 int status_fd = 10;
257 std::string package = "/path/to/update.zip";
258 std::vector<std::string> cmd;
Tao Baocf60a442018-06-18 14:56:20 -0700259 ASSERT_EQ(0, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700260 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700261 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700262 ASSERT_EQ("--payload=file://" + package, cmd[1]);
263 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
264 ASSERT_EQ("--headers=" + properties, cmd[3]);
265 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
266 } else {
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700267 ASSERT_EQ(INSTALL_ERROR, CheckPackageMetadata(metadata, OtaType::AB));
Tianjie Xu69b96492017-08-17 16:42:57 -0700268 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700269 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700270}
Tianjie Xu69b96492017-08-17 16:42:57 -0700271
Tao Baocf60a442018-06-18 14:56:20 -0700272TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700273 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700274 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700275}
276
Tao Baocf60a442018-06-18 14:56:20 -0700277TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700278 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700279
Tao Baobc4b1fe2017-04-17 16:46:05 -0700280 std::string device = android::base::GetProperty("ro.product.device", "");
281 ASSERT_NE("", device);
282 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
283 ASSERT_NE("", timestamp);
284 std::string metadata = android::base::Join(
285 std::vector<std::string>{
286 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
287 },
288 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700289
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700290 BuildZipArchive(
291 {
292 { "payload.bin", "" },
293 { "META-INF/com/android/metadata", metadata },
294 },
295 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700296
297 ZipArchiveHandle zip;
298 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
299 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700300 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700301 std::vector<std::string> cmd;
Tao Baocf60a442018-06-18 14:56:20 -0700302 ASSERT_EQ(INSTALL_CORRUPT, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700303 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700304}
Tianjie Xu69b96492017-08-17 16:42:57 -0700305
Tao Baocf60a442018-06-18 14:56:20 -0700306TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700307 std::string serialno = android::base::GetProperty("ro.serialno", "");
308 ASSERT_NE("", serialno);
309
310 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700311 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700312
313 static constexpr char alphabet[] =
314 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
315 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
316
317 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700318 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700319 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700320 generate_n(back_inserter(random_serialno), serialno.size(), generator);
321 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700322 }
323 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700324 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700325
Tao Baocf60a442018-06-18 14:56:20 -0700326 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700327 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700328 generate_n(back_inserter(long_serialno), serialno.size(), generator);
329 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700330 }
331 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700332 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700333}
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700334
335static void test_check_package_metadata(const std::string& metadata_string, OtaType ota_type,
336 int exptected_result) {
337 TemporaryFile temp_file;
338 BuildZipArchive(
339 {
340 { "META-INF/com/android/metadata", metadata_string },
341 },
342 temp_file.release(), kCompressStored);
343
344 ZipArchiveHandle zip;
345 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
346
347 std::map<std::string, std::string> metadata;
348 ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata));
349 ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type));
350 CloseArchive(zip);
351}
352
353TEST(InstallTest, CheckPackageMetadata_ota_type) {
354 std::string device = android::base::GetProperty("ro.product.device", "");
355 ASSERT_NE("", device);
356
357 // ota-type must be present
358 std::string metadata = android::base::Join(
359 std::vector<std::string>{
360 "pre-device=" + device,
361 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
362 },
363 "\n");
364 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
365
366 // Checks if ota-type matches
367 metadata = android::base::Join(
368 std::vector<std::string>{
369 "ota-type=AB",
370 "pre-device=" + device,
371 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
372 },
373 "\n");
374 test_check_package_metadata(metadata, OtaType::AB, 0);
375
376 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
377}
378
379TEST(InstallTest, CheckPackageMetadata_device_type) {
380 // device type can not be empty
381 std::string metadata = android::base::Join(
382 std::vector<std::string>{
383 "ota-type=BRICK",
384 },
385 "\n");
386 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
387
388 // device type mismatches
389 metadata = android::base::Join(
390 std::vector<std::string>{
391 "ota-type=BRICK",
392 "pre-device=dummy_device_type",
393 },
394 "\n");
395 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
396}
397
398TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) {
399 std::string device = android::base::GetProperty("ro.product.device", "");
400 ASSERT_NE("", device);
401
402 // Serial number doesn't need to exist
403 std::string metadata = android::base::Join(
404 std::vector<std::string>{
405 "ota-type=BRICK",
406 "pre-device=" + device,
407 },
408 "\n");
409 test_check_package_metadata(metadata, OtaType::BRICK, 0);
410
411 // Serial number mismatches
412 metadata = android::base::Join(
413 std::vector<std::string>{
414 "ota-type=BRICK",
415 "pre-device=" + device,
416 "serialno=dummy_serial",
417 },
418 "\n");
419 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
420
421 std::string serialno = android::base::GetProperty("ro.serialno", "");
422 ASSERT_NE("", serialno);
423 metadata = android::base::Join(
424 std::vector<std::string>{
425 "ota-type=BRICK",
426 "pre-device=" + device,
427 "serialno=" + serialno,
428 },
429 "\n");
430 test_check_package_metadata(metadata, OtaType::BRICK, 0);
431}
432
433TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) {
434 std::string device = android::base::GetProperty("ro.product.device", "");
435 ASSERT_NE("", device);
436
437 std::string serialno = android::base::GetProperty("ro.serialno", "");
438 ASSERT_NE("", serialno);
439
440 std::vector<std::string> serial_numbers;
441 // Creates a dummy serial number string.
442 for (size_t c = 'a'; c <= 'z'; c++) {
443 serial_numbers.emplace_back(c, serialno.size());
444 }
445
446 // No matched serialno found.
447 std::string metadata = android::base::Join(
448 std::vector<std::string>{
449 "ota-type=BRICK",
450 "pre-device=" + device,
451 "serialno=" + android::base::Join(serial_numbers, '|'),
452 },
453 "\n");
454 test_check_package_metadata(metadata, OtaType::BRICK, INSTALL_ERROR);
455
456 serial_numbers.emplace_back(serialno);
457 std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine());
458 metadata = android::base::Join(
459 std::vector<std::string>{
460 "ota-type=BRICK",
461 "pre-device=" + device,
462 "serialno=" + android::base::Join(serial_numbers, '|'),
463 },
464 "\n");
465 test_check_package_metadata(metadata, OtaType::BRICK, 0);
466}
467
468TEST(InstallTest, CheckPackageMetadata_ab_build_version) {
469 std::string device = android::base::GetProperty("ro.product.device", "");
470 ASSERT_NE("", device);
471
472 std::string build_version = android::base::GetProperty("ro.build.version.incremental", "");
473 ASSERT_NE("", build_version);
474
475 std::string metadata = android::base::Join(
476 std::vector<std::string>{
477 "ota-type=AB",
478 "pre-device=" + device,
479 "pre-build-incremental=" + build_version,
480 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
481 },
482 "\n");
483 test_check_package_metadata(metadata, OtaType::AB, 0);
484
485 metadata = android::base::Join(
486 std::vector<std::string>{
487 "ota-type=AB",
488 "pre-device=" + device,
489 "pre-build-incremental=dummy_build",
490 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
491 },
492 "\n");
493 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
494}
495
496TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) {
497 std::string device = android::base::GetProperty("ro.product.device", "");
498 ASSERT_NE("", device);
499
500 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
501 ASSERT_NE("", finger_print);
502
503 std::string metadata = android::base::Join(
504 std::vector<std::string>{
505 "ota-type=AB",
506 "pre-device=" + device,
507 "pre-build=" + finger_print,
508 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
509 },
510 "\n");
511 test_check_package_metadata(metadata, OtaType::AB, 0);
512
513 metadata = android::base::Join(
514 std::vector<std::string>{
515 "ota-type=AB",
516 "pre-device=" + device,
517 "pre-build=dummy_build_fingerprint",
518 "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()),
519 },
520 "\n");
521 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
522}
523
524TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) {
525 std::string device = android::base::GetProperty("ro.product.device", "");
526 ASSERT_NE("", device);
527
528 // post timestamp is required for upgrade.
529 std::string metadata = android::base::Join(
530 std::vector<std::string>{
531 "ota-type=AB",
532 "pre-device=" + device,
533 },
534 "\n");
535 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
536
537 // post timestamp should be larger than the timestamp on device.
538 metadata = android::base::Join(
539 std::vector<std::string>{
540 "ota-type=AB",
541 "pre-device=" + device,
542 "post-timestamp=0",
543 },
544 "\n");
545 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
546
547 // fingerprint is required for downgrade
548 metadata = android::base::Join(
549 std::vector<std::string>{
550 "ota-type=AB",
551 "pre-device=" + device,
552 "post-timestamp=0",
553 "ota-downgrade=yes",
554 },
555 "\n");
556 test_check_package_metadata(metadata, OtaType::AB, INSTALL_ERROR);
557
558 std::string finger_print = android::base::GetProperty("ro.build.fingerprint", "");
559 ASSERT_NE("", finger_print);
560
561 metadata = android::base::Join(
562 std::vector<std::string>{
563 "ota-type=AB",
564 "pre-device=" + device,
565 "post-timestamp=0",
566 "pre-build=" + finger_print,
567 "ota-downgrade=yes",
568 },
569 "\n");
570 test_check_package_metadata(metadata, OtaType::AB, 0);
571}