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 | |
| 39 | TEST(InstallTest, verify_package_compatibility_no_entry) { |
| 40 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 41 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 42 | ZipWriter writer(zip_file); |
| 43 | // The archive must have something to be opened correctly. |
| 44 | ASSERT_EQ(0, writer.StartEntry("dummy_entry", 0)); |
| 45 | ASSERT_EQ(0, writer.FinishEntry()); |
| 46 | ASSERT_EQ(0, writer.Finish()); |
| 47 | ASSERT_EQ(0, fclose(zip_file)); |
| 48 | |
| 49 | // Doesn't contain compatibility zip entry. |
| 50 | ZipArchiveHandle zip; |
| 51 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 52 | ASSERT_TRUE(verify_package_compatibility(zip)); |
| 53 | CloseArchive(zip); |
| 54 | } |
| 55 | |
| 56 | TEST(InstallTest, verify_package_compatibility_invalid_entry) { |
| 57 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 58 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 59 | ZipWriter writer(zip_file); |
| 60 | ASSERT_EQ(0, writer.StartEntry("compatibility.zip", 0)); |
| 61 | ASSERT_EQ(0, writer.FinishEntry()); |
| 62 | ASSERT_EQ(0, writer.Finish()); |
| 63 | ASSERT_EQ(0, fclose(zip_file)); |
| 64 | |
| 65 | // Empty compatibility zip entry. |
| 66 | ZipArchiveHandle zip; |
| 67 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 68 | ASSERT_FALSE(verify_package_compatibility(zip)); |
| 69 | CloseArchive(zip); |
| 70 | } |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 71 | |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 72 | TEST(InstallTest, read_metadata_from_package_smoke) { |
| 73 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 74 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 75 | ZipWriter writer(zip_file); |
| 76 | ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored)); |
| 77 | const std::string content("abcdefg"); |
| 78 | ASSERT_EQ(0, writer.WriteBytes(content.data(), content.size())); |
| 79 | ASSERT_EQ(0, writer.FinishEntry()); |
| 80 | ASSERT_EQ(0, writer.Finish()); |
| 81 | ASSERT_EQ(0, fclose(zip_file)); |
| 82 | |
| 83 | ZipArchiveHandle zip; |
| 84 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 85 | std::string metadata; |
| 86 | ASSERT_TRUE(read_metadata_from_package(zip, &metadata)); |
| 87 | ASSERT_EQ(content, metadata); |
| 88 | CloseArchive(zip); |
| 89 | |
| 90 | TemporaryFile temp_file2; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 91 | FILE* zip_file2 = fdopen(temp_file2.release(), "w"); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 92 | ZipWriter writer2(zip_file2); |
| 93 | ASSERT_EQ(0, writer2.StartEntry("META-INF/com/android/metadata", kCompressDeflated)); |
| 94 | ASSERT_EQ(0, writer2.WriteBytes(content.data(), content.size())); |
| 95 | ASSERT_EQ(0, writer2.FinishEntry()); |
| 96 | ASSERT_EQ(0, writer2.Finish()); |
| 97 | ASSERT_EQ(0, fclose(zip_file2)); |
| 98 | |
| 99 | ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip)); |
| 100 | metadata.clear(); |
| 101 | ASSERT_TRUE(read_metadata_from_package(zip, &metadata)); |
| 102 | ASSERT_EQ(content, metadata); |
| 103 | CloseArchive(zip); |
| 104 | } |
| 105 | |
| 106 | TEST(InstallTest, read_metadata_from_package_no_entry) { |
| 107 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 108 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 109 | ZipWriter writer(zip_file); |
| 110 | ASSERT_EQ(0, writer.StartEntry("dummy_entry", kCompressStored)); |
| 111 | ASSERT_EQ(0, writer.FinishEntry()); |
| 112 | ASSERT_EQ(0, writer.Finish()); |
| 113 | ASSERT_EQ(0, fclose(zip_file)); |
| 114 | |
| 115 | ZipArchiveHandle zip; |
| 116 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 117 | std::string metadata; |
| 118 | ASSERT_FALSE(read_metadata_from_package(zip, &metadata)); |
| 119 | CloseArchive(zip); |
| 120 | } |
| 121 | |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 122 | TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) { |
| 123 | TemporaryFile compatibility_zip_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 124 | FILE* compatibility_zip = fdopen(compatibility_zip_file.release(), "w"); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 125 | ZipWriter compatibility_zip_writer(compatibility_zip); |
| 126 | ASSERT_EQ(0, compatibility_zip_writer.StartEntry("system_manifest.xml", kCompressDeflated)); |
| 127 | std::string malformed_xml = "malformed"; |
| 128 | ASSERT_EQ(0, compatibility_zip_writer.WriteBytes(malformed_xml.data(), malformed_xml.size())); |
| 129 | ASSERT_EQ(0, compatibility_zip_writer.FinishEntry()); |
| 130 | ASSERT_EQ(0, compatibility_zip_writer.Finish()); |
| 131 | ASSERT_EQ(0, fclose(compatibility_zip)); |
| 132 | |
| 133 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 134 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 135 | ZipWriter writer(zip_file); |
| 136 | ASSERT_EQ(0, writer.StartEntry("compatibility.zip", kCompressStored)); |
| 137 | std::string compatibility_zip_content; |
| 138 | ASSERT_TRUE( |
| 139 | android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content)); |
| 140 | ASSERT_EQ(0, |
| 141 | writer.WriteBytes(compatibility_zip_content.data(), compatibility_zip_content.size())); |
| 142 | ASSERT_EQ(0, writer.FinishEntry()); |
| 143 | ASSERT_EQ(0, writer.Finish()); |
| 144 | ASSERT_EQ(0, fclose(zip_file)); |
| 145 | |
| 146 | ZipArchiveHandle zip; |
| 147 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 148 | std::vector<std::string> compatibility_info; |
| 149 | compatibility_info.push_back(malformed_xml); |
| 150 | // Malformed compatibility zip is expected to be rejected by libvintf. But we defer that to |
| 151 | // libvintf. |
| 152 | std::string err; |
| 153 | bool result = |
| 154 | android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0; |
| 155 | ASSERT_EQ(result, verify_package_compatibility(zip)); |
| 156 | CloseArchive(zip); |
| 157 | } |
| 158 | |
| 159 | TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml) { |
| 160 | static constexpr const char* system_manifest_xml_path = "/system/manifest.xml"; |
| 161 | if (access(system_manifest_xml_path, R_OK) == -1) { |
| 162 | GTEST_LOG_(INFO) << "Test skipped on devices w/o /system/manifest.xml."; |
| 163 | return; |
| 164 | } |
| 165 | std::string system_manifest_xml_content; |
| 166 | ASSERT_TRUE( |
| 167 | android::base::ReadFileToString(system_manifest_xml_path, &system_manifest_xml_content)); |
| 168 | TemporaryFile compatibility_zip_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 169 | FILE* compatibility_zip = fdopen(compatibility_zip_file.release(), "w"); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 170 | ZipWriter compatibility_zip_writer(compatibility_zip); |
| 171 | ASSERT_EQ(0, compatibility_zip_writer.StartEntry("system_manifest.xml", kCompressDeflated)); |
| 172 | ASSERT_EQ(0, compatibility_zip_writer.WriteBytes(system_manifest_xml_content.data(), |
| 173 | system_manifest_xml_content.size())); |
| 174 | ASSERT_EQ(0, compatibility_zip_writer.FinishEntry()); |
| 175 | ASSERT_EQ(0, compatibility_zip_writer.Finish()); |
| 176 | ASSERT_EQ(0, fclose(compatibility_zip)); |
| 177 | |
| 178 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 179 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 180 | ZipWriter writer(zip_file); |
| 181 | ASSERT_EQ(0, writer.StartEntry("compatibility.zip", kCompressStored)); |
| 182 | std::string compatibility_zip_content; |
| 183 | ASSERT_TRUE( |
| 184 | android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content)); |
| 185 | ASSERT_EQ(0, |
| 186 | writer.WriteBytes(compatibility_zip_content.data(), compatibility_zip_content.size())); |
| 187 | ASSERT_EQ(0, writer.FinishEntry()); |
| 188 | ASSERT_EQ(0, writer.Finish()); |
| 189 | ASSERT_EQ(0, fclose(zip_file)); |
| 190 | |
| 191 | ZipArchiveHandle zip; |
| 192 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 193 | std::vector<std::string> compatibility_info; |
| 194 | compatibility_info.push_back(system_manifest_xml_content); |
| 195 | std::string err; |
| 196 | bool result = |
| 197 | android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0; |
| 198 | // Make sure the result is consistent with libvintf library. |
| 199 | ASSERT_EQ(result, verify_package_compatibility(zip)); |
| 200 | CloseArchive(zip); |
| 201 | } |
| 202 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 203 | TEST(InstallTest, SetUpNonAbUpdateCommands) { |
| 204 | TemporaryFile temp_file; |
| 205 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
| 206 | ZipWriter writer(zip_file); |
| 207 | static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary"; |
| 208 | ASSERT_EQ(0, writer.StartEntry(UPDATE_BINARY_NAME, kCompressStored)); |
| 209 | ASSERT_EQ(0, writer.FinishEntry()); |
| 210 | ASSERT_EQ(0, writer.Finish()); |
| 211 | ASSERT_EQ(0, fclose(zip_file)); |
| 212 | |
| 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 | std::string binary_path = std::string(td.path) + "/update_binary"; |
| 219 | Paths::Get().set_temporary_update_binary(binary_path); |
| 220 | std::vector<std::string> cmd; |
| 221 | ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd)); |
| 222 | ASSERT_EQ(4U, cmd.size()); |
| 223 | ASSERT_EQ(binary_path, cmd[0]); |
| 224 | ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION |
| 225 | ASSERT_EQ(std::to_string(status_fd), cmd[2]); |
| 226 | ASSERT_EQ(package, cmd[3]); |
| 227 | struct stat 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 | // With non-zero retry count. update_binary will be removed automatically. |
| 232 | cmd.clear(); |
| 233 | ASSERT_EQ(0, SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd)); |
| 234 | ASSERT_EQ(5U, cmd.size()); |
| 235 | ASSERT_EQ(binary_path, cmd[0]); |
| 236 | ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION |
| 237 | ASSERT_EQ(std::to_string(status_fd), cmd[2]); |
| 238 | ASSERT_EQ(package, cmd[3]); |
| 239 | ASSERT_EQ("retry", cmd[4]); |
| 240 | sb = {}; |
| 241 | ASSERT_EQ(0, stat(binary_path.c_str(), &sb)); |
| 242 | ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
| 243 | |
| 244 | CloseArchive(zip); |
| 245 | } |
| 246 | |
| 247 | TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) { |
| 248 | TemporaryFile temp_file; |
| 249 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
| 250 | ZipWriter writer(zip_file); |
| 251 | // The archive must have something to be opened correctly. |
| 252 | ASSERT_EQ(0, writer.StartEntry("dummy_entry", 0)); |
| 253 | ASSERT_EQ(0, writer.FinishEntry()); |
| 254 | ASSERT_EQ(0, writer.Finish()); |
| 255 | ASSERT_EQ(0, fclose(zip_file)); |
| 256 | |
| 257 | // Missing update binary. |
| 258 | ZipArchiveHandle zip; |
| 259 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 260 | int status_fd = 10; |
| 261 | std::string package = "/path/to/update.zip"; |
| 262 | TemporaryDir td; |
| 263 | Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary"); |
| 264 | std::vector<std::string> cmd; |
| 265 | ASSERT_EQ(INSTALL_CORRUPT, SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd)); |
| 266 | CloseArchive(zip); |
| 267 | } |
| 268 | |
| 269 | static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 270 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 271 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 272 | ZipWriter writer(zip_file); |
| 273 | ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored)); |
| 274 | ASSERT_EQ(0, writer.FinishEntry()); |
| 275 | ASSERT_EQ(0, writer.StartEntry("payload_properties.txt", kCompressStored)); |
| 276 | const std::string properties = "some_properties"; |
| 277 | ASSERT_EQ(0, writer.WriteBytes(properties.data(), properties.size())); |
| 278 | ASSERT_EQ(0, writer.FinishEntry()); |
| 279 | // A metadata entry is mandatory. |
| 280 | ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored)); |
| 281 | 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); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 285 | |
| 286 | std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device, |
| 287 | "post-timestamp=" + timestamp }; |
| 288 | if (!serialno.empty()) { |
| 289 | meta.push_back("serialno=" + serialno); |
| 290 | } |
| 291 | std::string metadata = android::base::Join(meta, "\n"); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 292 | ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size())); |
| 293 | ASSERT_EQ(0, writer.FinishEntry()); |
| 294 | ASSERT_EQ(0, writer.Finish()); |
| 295 | ASSERT_EQ(0, fclose(zip_file)); |
| 296 | |
| 297 | ZipArchiveHandle zip; |
| 298 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 299 | ZipString payload_name("payload.bin"); |
| 300 | ZipEntry payload_entry; |
| 301 | ASSERT_EQ(0, FindEntry(zip, payload_name, &payload_entry)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 302 | int status_fd = 10; |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 303 | std::string package = "/path/to/update.zip"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 304 | std::vector<std::string> cmd; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 305 | if (success) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 306 | ASSERT_EQ(0, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 307 | ASSERT_EQ(5U, cmd.size()); |
Tao Bao | 2cc9bbb | 2018-08-14 12:34:46 -0700 | [diff] [blame] | 308 | ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 309 | ASSERT_EQ("--payload=file://" + package, cmd[1]); |
| 310 | ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]); |
| 311 | ASSERT_EQ("--headers=" + properties, cmd[3]); |
| 312 | ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]); |
| 313 | } else { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 314 | ASSERT_EQ(INSTALL_ERROR, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 315 | } |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 316 | CloseArchive(zip); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 317 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 318 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 319 | TEST(InstallTest, SetUpAbUpdateCommands) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 320 | // Empty serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 321 | VerifyAbUpdateCommands({}); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 324 | TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 325 | TemporaryFile temp_file; |
Tao Bao | f29ed3e | 2017-10-24 16:48:32 -0700 | [diff] [blame] | 326 | FILE* zip_file = fdopen(temp_file.release(), "w"); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 327 | ZipWriter writer(zip_file); |
| 328 | // Missing payload_properties.txt. |
| 329 | ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored)); |
| 330 | ASSERT_EQ(0, writer.FinishEntry()); |
| 331 | // A metadata entry is mandatory. |
| 332 | ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored)); |
| 333 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 334 | ASSERT_NE("", device); |
| 335 | std::string timestamp = android::base::GetProperty("ro.build.date.utc", ""); |
| 336 | ASSERT_NE("", timestamp); |
| 337 | std::string metadata = android::base::Join( |
| 338 | std::vector<std::string>{ |
| 339 | "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp, |
| 340 | }, |
| 341 | "\n"); |
| 342 | ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size())); |
| 343 | ASSERT_EQ(0, writer.FinishEntry()); |
| 344 | ASSERT_EQ(0, writer.Finish()); |
| 345 | ASSERT_EQ(0, fclose(zip_file)); |
| 346 | |
| 347 | ZipArchiveHandle zip; |
| 348 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 349 | int status_fd = 10; |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 350 | std::string package = "/path/to/update.zip"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 351 | std::vector<std::string> cmd; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 352 | ASSERT_EQ(INSTALL_CORRUPT, SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 353 | CloseArchive(zip); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 354 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 355 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 356 | TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 357 | std::string serialno = android::base::GetProperty("ro.serialno", ""); |
| 358 | ASSERT_NE("", serialno); |
| 359 | |
| 360 | // Single matching serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 361 | VerifyAbUpdateCommands(serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 362 | |
| 363 | static constexpr char alphabet[] = |
| 364 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 365 | auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; }; |
| 366 | |
| 367 | // Generate 900 random serial numbers. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 368 | std::string random_serialno; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 369 | for (size_t i = 0; i < 900; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 370 | generate_n(back_inserter(random_serialno), serialno.size(), generator); |
| 371 | random_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 372 | } |
| 373 | // Random serialnos should fail the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 374 | VerifyAbUpdateCommands(random_serialno, false); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 375 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 376 | std::string long_serialno = random_serialno + serialno + "|"; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 377 | for (size_t i = 0; i < 99; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 378 | generate_n(back_inserter(long_serialno), serialno.size(), generator); |
| 379 | long_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 380 | } |
| 381 | // String with the matching serialno should pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 382 | VerifyAbUpdateCommands(long_serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 383 | } |