aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTianjie Xu2017-08-22 18:23:07 -0500
committerandroid-build-merger2017-08-22 18:23:07 -0500
commit7379a5290dd96e9382232c1be625e5a3431f05b0 (patch)
treec68f0d6b6fdf9023e70981049484223d18f41bcf
parent0d0b6a0d2ad2cfffcdb1084de733c22c5b8f97a9 (diff)
parent968ebdeefdd2dffd81d5a0300222e110d6791426 (diff)
downloadplatform-bootable-recovery-7379a5290dd96e9382232c1be625e5a3431f05b0.tar.gz
platform-bootable-recovery-7379a5290dd96e9382232c1be625e5a3431f05b0.tar.xz
platform-bootable-recovery-7379a5290dd96e9382232c1be625e5a3431f05b0.zip
Merge "Allow comparison against multi serial nums for A/B package" am: 3810046a55 am: e8b02d68e5
am: 968ebdeefd Change-Id: I21de76c870001cb5faf145d5f39f8fb2cfa58a66
-rw-r--r--install.cpp20
-rw-r--r--tests/component/install_test.cpp71
2 files changed, 73 insertions, 18 deletions
diff --git a/install.cpp b/install.cpp
index 7fbf5c01..1220c6ab 100644
--- a/install.cpp
+++ b/install.cpp
@@ -148,13 +148,23 @@ static int check_newer_ab_build(ZipArchiveHandle zip) {
148 return INSTALL_ERROR; 148 return INSTALL_ERROR;
149 } 149 }
150 150
151 // We allow the package to not have any serialno, but if it has a non-empty 151 // We allow the package to not have any serialno; and we also allow it to carry multiple serial
152 // value it should match. 152 // numbers split by "|"; e.g. serialno=serialno1|serialno2|serialno3 ... We will fail the
153 // verification if the device's serialno doesn't match any of these carried numbers.
153 value = android::base::GetProperty("ro.serialno", ""); 154 value = android::base::GetProperty("ro.serialno", "");
154 const std::string& pkg_serial_no = metadata["serialno"]; 155 const std::string& pkg_serial_no = metadata["serialno"];
155 if (!pkg_serial_no.empty() && pkg_serial_no != value) { 156 if (!pkg_serial_no.empty()) {
156 LOG(ERROR) << "Package is for serial " << pkg_serial_no; 157 bool match = false;
157 return INSTALL_ERROR; 158 for (const std::string& number : android::base::Split(pkg_serial_no, "|")) {
159 if (value == android::base::Trim(number)) {
160 match = true;
161 break;
162 }
163 }
164 if (!match) {
165 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
166 return INSTALL_ERROR;
167 }
158 } 168 }
159 169
160 if (metadata["ota-type"] != "AB") { 170 if (metadata["ota-type"] != "AB") {
diff --git a/tests/component/install_test.cpp b/tests/component/install_test.cpp
index 968196fc..7bb49606 100644
--- a/tests/component/install_test.cpp
+++ b/tests/component/install_test.cpp
@@ -19,6 +19,7 @@
19#include <sys/types.h> 19#include <sys/types.h>
20#include <unistd.h> 20#include <unistd.h>
21 21
22#include <algorithm>
22#include <string> 23#include <string>
23#include <vector> 24#include <vector>
24 25
@@ -198,8 +199,8 @@ TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml
198 CloseArchive(zip); 199 CloseArchive(zip);
199} 200}
200 201
201TEST(InstallTest, update_binary_command_smoke) {
202#ifdef AB_OTA_UPDATER 202#ifdef AB_OTA_UPDATER
203static void VerifyAbUpdateBinaryCommand(const std::string& serialno, bool success = true) {
203 TemporaryFile temp_file; 204 TemporaryFile temp_file;
204 FILE* zip_file = fdopen(temp_file.fd, "w"); 205 FILE* zip_file = fdopen(temp_file.fd, "w");
205 ZipWriter writer(zip_file); 206 ZipWriter writer(zip_file);
@@ -215,11 +216,13 @@ TEST(InstallTest, update_binary_command_smoke) {
215 ASSERT_NE("", device); 216 ASSERT_NE("", device);
216 std::string timestamp = android::base::GetProperty("ro.build.date.utc", ""); 217 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
217 ASSERT_NE("", timestamp); 218 ASSERT_NE("", timestamp);
218 std::string metadata = android::base::Join( 219
219 std::vector<std::string>{ 220 std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device,
220 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp, 221 "post-timestamp=" + timestamp };
221 }, 222 if (!serialno.empty()) {
222 "\n"); 223 meta.push_back("serialno=" + serialno);
224 }
225 std::string metadata = android::base::Join(meta, "\n");
223 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size())); 226 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
224 ASSERT_EQ(0, writer.FinishEntry()); 227 ASSERT_EQ(0, writer.FinishEntry());
225 ASSERT_EQ(0, writer.Finish()); 228 ASSERT_EQ(0, writer.Finish());
@@ -234,14 +237,25 @@ TEST(InstallTest, update_binary_command_smoke) {
234 std::string package = "/path/to/update.zip"; 237 std::string package = "/path/to/update.zip";
235 std::string binary_path = "/sbin/update_engine_sideload"; 238 std::string binary_path = "/sbin/update_engine_sideload";
236 std::vector<std::string> cmd; 239 std::vector<std::string> cmd;
237 ASSERT_EQ(0, update_binary_command(package, zip, binary_path, 0, status_fd, &cmd)); 240 if (success) {
238 ASSERT_EQ(5U, cmd.size()); 241 ASSERT_EQ(0, update_binary_command(package, zip, binary_path, 0, status_fd, &cmd));
239 ASSERT_EQ(binary_path, cmd[0]); 242 ASSERT_EQ(5U, cmd.size());
240 ASSERT_EQ("--payload=file://" + package, cmd[1]); 243 ASSERT_EQ(binary_path, cmd[0]);
241 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]); 244 ASSERT_EQ("--payload=file://" + package, cmd[1]);
242 ASSERT_EQ("--headers=" + properties, cmd[3]); 245 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
243 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]); 246 ASSERT_EQ("--headers=" + properties, cmd[3]);
247 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
248 } else {
249 ASSERT_EQ(INSTALL_ERROR, update_binary_command(package, zip, binary_path, 0, status_fd, &cmd));
250 }
244 CloseArchive(zip); 251 CloseArchive(zip);
252}
253#endif // AB_OTA_UPDATER
254
255TEST(InstallTest, update_binary_command_smoke) {
256#ifdef AB_OTA_UPDATER
257 // Empty serialno will pass the verification.
258 VerifyAbUpdateBinaryCommand({});
245#else 259#else
246 TemporaryFile temp_file; 260 TemporaryFile temp_file;
247 FILE* zip_file = fdopen(temp_file.fd, "w"); 261 FILE* zip_file = fdopen(temp_file.fd, "w");
@@ -340,3 +354,34 @@ TEST(InstallTest, update_binary_command_invalid) {
340 CloseArchive(zip); 354 CloseArchive(zip);
341#endif // AB_OTA_UPDATER 355#endif // AB_OTA_UPDATER
342} 356}
357
358#ifdef AB_OTA_UPDATER
359TEST(InstallTest, update_binary_command_multiple_serialno) {
360 std::string serialno = android::base::GetProperty("ro.serialno", "");
361 ASSERT_NE("", serialno);
362
363 // Single matching serialno will pass the verification.
364 VerifyAbUpdateBinaryCommand(serialno);
365
366 static constexpr char alphabet[] =
367 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
368 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
369
370 // Generate 900 random serial numbers.
371 std::string random_serial;
372 for (size_t i = 0; i < 900; i++) {
373 generate_n(back_inserter(random_serial), serialno.size(), generator);
374 random_serial.append("|");
375 }
376 // Random serialnos should fail the verification.
377 VerifyAbUpdateBinaryCommand(random_serial, false);
378
379 std::string long_serial = random_serial + serialno + "|";
380 for (size_t i = 0; i < 99; i++) {
381 generate_n(back_inserter(long_serial), serialno.size(), generator);
382 long_serial.append("|");
383 }
384 // String with the matching serialno should pass the verification.
385 VerifyAbUpdateBinaryCommand(long_serial);
386}
387#endif // AB_OTA_UPDATER