Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 18 | #include <sys/stat.h> |
| 19 | #include <sys/types.h> |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 21 | |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 22 | #include <algorithm> |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 27 | #include <android-base/properties.h> |
| 28 | #include <android-base/strings.h> |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 29 | #include <android-base/test_utils.h> |
| 30 | #include <gtest/gtest.h> |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 31 | #include <vintf/VintfObjectRecovery.h> |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 32 | #include <ziparchive/zip_archive.h> |
| 33 | #include <ziparchive/zip_writer.h> |
| 34 | |
| 35 | #include "install.h" |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 36 | #include "otautil/paths.h" |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 37 | #include "private/install.h" |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 38 | |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 39 | static 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 Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 42 | ZipWriter writer(zip_file); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 43 | 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 Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 48 | ASSERT_EQ(0, writer.Finish()); |
| 49 | ASSERT_EQ(0, fclose(zip_file)); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(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 Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 56 | |
| 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 | |
| 64 | TEST(InstallTest, verify_package_compatibility_invalid_entry) { |
| 65 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 66 | BuildZipArchive({ { "compatibility.zip", "" } }, temp_file.release(), kCompressStored); |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 67 | |
| 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 Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 74 | |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 75 | TEST(InstallTest, read_metadata_from_package_smoke) { |
| 76 | TemporaryFile temp_file; |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 77 | const std::string content("abcdefg"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 78 | BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(), |
| 79 | kCompressStored); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 80 | |
| 81 | ZipArchiveHandle zip; |
| 82 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 83 | std::string metadata; |
| 84 | ASSERT_TRUE(read_metadata_from_package(zip, &metadata)); |
| 85 | ASSERT_EQ(content, metadata); |
| 86 | CloseArchive(zip); |
| 87 | |
| 88 | TemporaryFile temp_file2; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 89 | BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(), |
| 90 | kCompressDeflated); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 91 | |
| 92 | ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip)); |
| 93 | metadata.clear(); |
| 94 | ASSERT_TRUE(read_metadata_from_package(zip, &metadata)); |
| 95 | ASSERT_EQ(content, metadata); |
| 96 | CloseArchive(zip); |
| 97 | } |
| 98 | |
| 99 | TEST(InstallTest, read_metadata_from_package_no_entry) { |
| 100 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 101 | BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 102 | |
| 103 | ZipArchiveHandle zip; |
| 104 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 105 | std::string metadata; |
| 106 | ASSERT_FALSE(read_metadata_from_package(zip, &metadata)); |
| 107 | CloseArchive(zip); |
| 108 | } |
| 109 | |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 110 | TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) { |
| 111 | TemporaryFile compatibility_zip_file; |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 112 | std::string malformed_xml = "malformed"; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 113 | BuildZipArchive({ { "system_manifest.xml", malformed_xml } }, compatibility_zip_file.release(), |
| 114 | kCompressDeflated); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 115 | |
| 116 | TemporaryFile temp_file; |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 117 | std::string compatibility_zip_content; |
| 118 | ASSERT_TRUE( |
| 119 | android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content)); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 120 | BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(), |
| 121 | kCompressStored); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 122 | |
| 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 | |
| 136 | TEST(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 Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 146 | BuildZipArchive({ { "system_manifest.xml", system_manifest_xml_content } }, |
| 147 | compatibility_zip_file.release(), kCompressDeflated); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 148 | |
| 149 | TemporaryFile temp_file; |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 150 | std::string compatibility_zip_content; |
| 151 | ASSERT_TRUE( |
| 152 | android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content)); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 153 | BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(), |
| 154 | kCompressStored); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 155 | |
| 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 Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 168 | TEST(InstallTest, SetUpNonAbUpdateCommands) { |
| 169 | TemporaryFile temp_file; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 170 | static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary"; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 171 | BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 172 | |
| 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 | |
| 207 | TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) { |
| 208 | TemporaryFile temp_file; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 209 | // The archive must have something to be opened correctly. |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 210 | BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 211 | |
| 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 | |
| 224 | static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 225 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 226 | |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 227 | const std::string properties = "some_properties"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 228 | 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 Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 232 | |
| 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 | } |
| 238 | std::string metadata = android::base::Join(meta, "\n"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 239 | |
| 240 | BuildZipArchive({ { "payload.bin", "" }, |
| 241 | { "payload_properties.txt", properties }, |
| 242 | { "META-INF/com/android/metadata", metadata } }, |
| 243 | temp_file.release(), kCompressStored); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 244 | |
| 245 | ZipArchiveHandle zip; |
| 246 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 247 | ZipString payload_name("payload.bin"); |
| 248 | ZipEntry payload_entry; |
| 249 | ASSERT_EQ(0, FindEntry(zip, payload_name, &payload_entry)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 250 | int status_fd = 10; |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 251 | std::string package = "/path/to/update.zip"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 252 | std::vector<std::string> cmd; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 253 | if (success) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 254 | ASSERT_EQ(0, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 255 | ASSERT_EQ(5U, cmd.size()); |
Tao Bao | 2cc9bbb | 2018-08-14 12:34:46 -0700 | [diff] [blame] | 256 | ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 257 | ASSERT_EQ("--payload=file://" + package, cmd[1]); |
| 258 | ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]); |
| 259 | ASSERT_EQ("--headers=" + properties, cmd[3]); |
| 260 | ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]); |
| 261 | } else { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 262 | ASSERT_EQ(INSTALL_ERROR, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 263 | } |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 264 | CloseArchive(zip); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 265 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 266 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 267 | TEST(InstallTest, SetUpAbUpdateCommands) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 268 | // Empty serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 269 | VerifyAbUpdateCommands({}); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 272 | TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 273 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 274 | |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 275 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 276 | ASSERT_NE("", device); |
| 277 | std::string timestamp = android::base::GetProperty("ro.build.date.utc", ""); |
| 278 | ASSERT_NE("", timestamp); |
| 279 | std::string metadata = android::base::Join( |
| 280 | std::vector<std::string>{ |
| 281 | "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp, |
| 282 | }, |
| 283 | "\n"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 284 | |
| 285 | BuildZipArchive({ { "payload.bin", "" }, { "META-INF/com/android/metadata", metadata } }, |
| 286 | temp_file.release(), kCompressStored); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 287 | |
| 288 | ZipArchiveHandle zip; |
| 289 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 290 | int status_fd = 10; |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 291 | std::string package = "/path/to/update.zip"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 292 | std::vector<std::string> cmd; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 293 | ASSERT_EQ(INSTALL_CORRUPT, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 294 | CloseArchive(zip); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 295 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 296 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 297 | TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 298 | std::string serialno = android::base::GetProperty("ro.serialno", ""); |
| 299 | ASSERT_NE("", serialno); |
| 300 | |
| 301 | // Single matching serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 302 | VerifyAbUpdateCommands(serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 303 | |
| 304 | static constexpr char alphabet[] = |
| 305 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 306 | auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; }; |
| 307 | |
| 308 | // Generate 900 random serial numbers. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 309 | std::string random_serialno; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 310 | for (size_t i = 0; i < 900; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 311 | generate_n(back_inserter(random_serialno), serialno.size(), generator); |
| 312 | random_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 313 | } |
| 314 | // Random serialnos should fail the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 315 | VerifyAbUpdateCommands(random_serialno, false); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 316 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 317 | std::string long_serialno = random_serialno + serialno + "|"; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 318 | for (size_t i = 0; i < 99; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 319 | generate_n(back_inserter(long_serialno), serialno.size(), generator); |
| 320 | long_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 321 | } |
| 322 | // String with the matching serialno should pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 323 | VerifyAbUpdateCommands(long_serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 324 | } |