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> |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 23 | #include <random> |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Tao Bao | f2784b6 | 2017-04-19 12:37:12 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 28 | #include <android-base/properties.h> |
| 29 | #include <android-base/strings.h> |
Tao Bao | 1d86605 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 30 | #include <gtest/gtest.h> |
| 31 | #include <ziparchive/zip_archive.h> |
| 32 | #include <ziparchive/zip_writer.h> |
| 33 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 34 | #include "install/install.h" |
Tao Bao | 7f19d10 | 2019-04-26 22:56:56 -0700 | [diff] [blame] | 35 | #include "install/wipe_device.h" |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 36 | #include "otautil/paths.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 37 | #include "private/setup_commands.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 | |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 52 | TEST(InstallTest, read_metadata_from_package_smoke) { |
| 53 | TemporaryFile temp_file; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 54 | const std::string content("abc=defg"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 55 | BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(), |
| 56 | kCompressStored); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 57 | |
| 58 | ZipArchiveHandle zip; |
| 59 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 60 | std::map<std::string, std::string> metadata; |
| 61 | ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata)); |
| 62 | ASSERT_EQ("defg", metadata["abc"]); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 63 | CloseArchive(zip); |
| 64 | |
| 65 | TemporaryFile temp_file2; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 66 | BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(), |
| 67 | kCompressDeflated); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 68 | |
| 69 | ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip)); |
| 70 | metadata.clear(); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 71 | ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata)); |
| 72 | ASSERT_EQ("defg", metadata["abc"]); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 73 | CloseArchive(zip); |
| 74 | } |
| 75 | |
| 76 | TEST(InstallTest, read_metadata_from_package_no_entry) { |
| 77 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 78 | BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 79 | |
| 80 | ZipArchiveHandle zip; |
| 81 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 82 | std::map<std::string, std::string> metadata; |
| 83 | ASSERT_FALSE(ReadMetadataFromPackage(zip, &metadata)); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 84 | CloseArchive(zip); |
| 85 | } |
| 86 | |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 87 | TEST(InstallTest, read_wipe_ab_partition_list) { |
| 88 | std::vector<std::string> partition_list = { |
| 89 | "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b", |
| 90 | "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b", |
| 91 | "/dev/block/bootdevice/by-name/userdata", "# Wipe the boot partitions last", |
| 92 | "/dev/block/bootdevice/by-name/boot_a", "/dev/block/bootdevice/by-name/boot_b", |
| 93 | }; |
| 94 | TemporaryFile temp_file; |
| 95 | BuildZipArchive({ { "recovery.wipe", android::base::Join(partition_list, '\n') } }, |
| 96 | temp_file.release(), kCompressDeflated); |
| 97 | std::string wipe_package; |
| 98 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &wipe_package)); |
| 99 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 100 | auto package = Package::CreateMemoryPackage( |
| 101 | std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr); |
| 102 | |
| 103 | auto read_partition_list = GetWipePartitionList(package.get()); |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 104 | std::vector<std::string> expected = { |
| 105 | "/dev/block/bootdevice/by-name/system_a", "/dev/block/bootdevice/by-name/system_b", |
| 106 | "/dev/block/bootdevice/by-name/vendor_a", "/dev/block/bootdevice/by-name/vendor_b", |
| 107 | "/dev/block/bootdevice/by-name/userdata", "/dev/block/bootdevice/by-name/boot_a", |
| 108 | "/dev/block/bootdevice/by-name/boot_b", |
| 109 | }; |
| 110 | ASSERT_EQ(expected, read_partition_list); |
| 111 | } |
| 112 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 113 | TEST(InstallTest, SetUpNonAbUpdateCommands) { |
| 114 | TemporaryFile temp_file; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 115 | 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] | 116 | BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 117 | |
| 118 | ZipArchiveHandle zip; |
| 119 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 120 | int status_fd = 10; |
| 121 | std::string package = "/path/to/update.zip"; |
| 122 | TemporaryDir td; |
| 123 | std::string binary_path = std::string(td.path) + "/update_binary"; |
| 124 | Paths::Get().set_temporary_update_binary(binary_path); |
| 125 | std::vector<std::string> cmd; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 126 | ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd)); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 127 | ASSERT_EQ(4U, cmd.size()); |
| 128 | ASSERT_EQ(binary_path, cmd[0]); |
| 129 | ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION |
| 130 | ASSERT_EQ(std::to_string(status_fd), cmd[2]); |
| 131 | ASSERT_EQ(package, cmd[3]); |
| 132 | struct stat sb; |
| 133 | ASSERT_EQ(0, stat(binary_path.c_str(), &sb)); |
| 134 | ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
| 135 | |
| 136 | // With non-zero retry count. update_binary will be removed automatically. |
| 137 | cmd.clear(); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 138 | ASSERT_TRUE(SetUpNonAbUpdateCommands(package, zip, 2, status_fd, &cmd)); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 139 | ASSERT_EQ(5U, cmd.size()); |
| 140 | ASSERT_EQ(binary_path, cmd[0]); |
| 141 | ASSERT_EQ("3", cmd[1]); // RECOVERY_API_VERSION |
| 142 | ASSERT_EQ(std::to_string(status_fd), cmd[2]); |
| 143 | ASSERT_EQ(package, cmd[3]); |
| 144 | ASSERT_EQ("retry", cmd[4]); |
| 145 | sb = {}; |
| 146 | ASSERT_EQ(0, stat(binary_path.c_str(), &sb)); |
| 147 | ASSERT_EQ(static_cast<mode_t>(0755), sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); |
| 148 | |
| 149 | CloseArchive(zip); |
| 150 | } |
| 151 | |
| 152 | TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) { |
| 153 | TemporaryFile temp_file; |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 154 | // The archive must have something to be opened correctly. |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 155 | BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 156 | |
| 157 | // Missing update binary. |
| 158 | ZipArchiveHandle zip; |
| 159 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 160 | int status_fd = 10; |
| 161 | std::string package = "/path/to/update.zip"; |
| 162 | TemporaryDir td; |
| 163 | Paths::Get().set_temporary_update_binary(std::string(td.path) + "/update_binary"); |
| 164 | std::vector<std::string> cmd; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 165 | ASSERT_FALSE(SetUpNonAbUpdateCommands(package, zip, 0, status_fd, &cmd)); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 166 | CloseArchive(zip); |
| 167 | } |
| 168 | |
| 169 | static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 170 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 171 | |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 172 | const std::string properties = "some_properties"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 173 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 174 | ASSERT_NE("", device); |
| 175 | std::string timestamp = android::base::GetProperty("ro.build.date.utc", ""); |
| 176 | ASSERT_NE("", timestamp); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 177 | |
| 178 | std::vector<std::string> meta{ "ota-type=AB", "pre-device=" + device, |
| 179 | "post-timestamp=" + timestamp }; |
| 180 | if (!serialno.empty()) { |
| 181 | meta.push_back("serialno=" + serialno); |
| 182 | } |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 183 | std::string metadata_string = android::base::Join(meta, "\n"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 184 | |
| 185 | BuildZipArchive({ { "payload.bin", "" }, |
| 186 | { "payload_properties.txt", properties }, |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 187 | { "META-INF/com/android/metadata", metadata_string } }, |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 188 | temp_file.release(), kCompressStored); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 189 | |
| 190 | ZipArchiveHandle zip; |
| 191 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 192 | ZipEntry payload_entry; |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 193 | ASSERT_EQ(0, FindEntry(zip, "payload.bin", &payload_entry)); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 194 | |
| 195 | std::map<std::string, std::string> metadata; |
| 196 | ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 197 | if (success) { |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 198 | ASSERT_TRUE(CheckPackageMetadata(metadata, OtaType::AB)); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 199 | |
| 200 | int status_fd = 10; |
| 201 | std::string package = "/path/to/update.zip"; |
| 202 | std::vector<std::string> cmd; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 203 | ASSERT_TRUE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 204 | ASSERT_EQ(5U, cmd.size()); |
Tao Bao | 2cc9bbb | 2018-08-14 12:34:46 -0700 | [diff] [blame] | 205 | ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 206 | ASSERT_EQ("--payload=file://" + package, cmd[1]); |
| 207 | ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]); |
| 208 | ASSERT_EQ("--headers=" + properties, cmd[3]); |
| 209 | ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]); |
| 210 | } else { |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 211 | ASSERT_FALSE(CheckPackageMetadata(metadata, OtaType::AB)); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 212 | } |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 213 | CloseArchive(zip); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 214 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 215 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 216 | TEST(InstallTest, SetUpAbUpdateCommands) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 217 | // Empty serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 218 | VerifyAbUpdateCommands({}); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 221 | TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 222 | TemporaryFile temp_file; |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 223 | |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 224 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 225 | ASSERT_NE("", device); |
| 226 | std::string timestamp = android::base::GetProperty("ro.build.date.utc", ""); |
| 227 | ASSERT_NE("", timestamp); |
| 228 | std::string metadata = android::base::Join( |
| 229 | std::vector<std::string>{ |
| 230 | "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp, |
| 231 | }, |
| 232 | "\n"); |
Tianjie Xu | f2fb49a | 2018-10-26 15:16:50 -0700 | [diff] [blame] | 233 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 234 | BuildZipArchive( |
| 235 | { |
| 236 | { "payload.bin", "" }, |
| 237 | { "META-INF/com/android/metadata", metadata }, |
| 238 | }, |
| 239 | temp_file.release(), kCompressStored); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 240 | |
| 241 | ZipArchiveHandle zip; |
| 242 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 243 | int status_fd = 10; |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 244 | std::string package = "/path/to/update.zip"; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 245 | std::vector<std::string> cmd; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 246 | ASSERT_FALSE(SetUpAbUpdateCommands(package, zip, status_fd, &cmd)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 247 | CloseArchive(zip); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 248 | } |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 249 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 250 | TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) { |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 251 | std::string serialno = android::base::GetProperty("ro.serialno", ""); |
| 252 | ASSERT_NE("", serialno); |
| 253 | |
| 254 | // Single matching serialno will pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 255 | VerifyAbUpdateCommands(serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 256 | |
| 257 | static constexpr char alphabet[] = |
| 258 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 259 | auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; }; |
| 260 | |
| 261 | // Generate 900 random serial numbers. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 262 | std::string random_serialno; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 263 | for (size_t i = 0; i < 900; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 264 | generate_n(back_inserter(random_serialno), serialno.size(), generator); |
| 265 | random_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 266 | } |
| 267 | // Random serialnos should fail the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 268 | VerifyAbUpdateCommands(random_serialno, false); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 269 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 270 | std::string long_serialno = random_serialno + serialno + "|"; |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 271 | for (size_t i = 0; i < 99; i++) { |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 272 | generate_n(back_inserter(long_serialno), serialno.size(), generator); |
| 273 | long_serialno.append("|"); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 274 | } |
| 275 | // String with the matching serialno should pass the verification. |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 276 | VerifyAbUpdateCommands(long_serialno); |
Tianjie Xu | 69b9649 | 2017-08-17 16:42:57 -0700 | [diff] [blame] | 277 | } |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 278 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 279 | static void TestCheckPackageMetadata(const std::string& metadata_string, OtaType ota_type, |
| 280 | bool exptected_result) { |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 281 | TemporaryFile temp_file; |
| 282 | BuildZipArchive( |
| 283 | { |
| 284 | { "META-INF/com/android/metadata", metadata_string }, |
| 285 | }, |
| 286 | temp_file.release(), kCompressStored); |
| 287 | |
| 288 | ZipArchiveHandle zip; |
| 289 | ASSERT_EQ(0, OpenArchive(temp_file.path, &zip)); |
| 290 | |
| 291 | std::map<std::string, std::string> metadata; |
| 292 | ASSERT_TRUE(ReadMetadataFromPackage(zip, &metadata)); |
| 293 | ASSERT_EQ(exptected_result, CheckPackageMetadata(metadata, ota_type)); |
| 294 | CloseArchive(zip); |
| 295 | } |
| 296 | |
| 297 | TEST(InstallTest, CheckPackageMetadata_ota_type) { |
| 298 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 299 | ASSERT_NE("", device); |
| 300 | |
| 301 | // ota-type must be present |
| 302 | std::string metadata = android::base::Join( |
| 303 | std::vector<std::string>{ |
| 304 | "pre-device=" + device, |
| 305 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 306 | }, |
| 307 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 308 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 309 | |
| 310 | // Checks if ota-type matches |
| 311 | metadata = android::base::Join( |
| 312 | std::vector<std::string>{ |
| 313 | "ota-type=AB", |
| 314 | "pre-device=" + device, |
| 315 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 316 | }, |
| 317 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 318 | TestCheckPackageMetadata(metadata, OtaType::AB, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 319 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 320 | TestCheckPackageMetadata(metadata, OtaType::BRICK, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST(InstallTest, CheckPackageMetadata_device_type) { |
| 324 | // device type can not be empty |
| 325 | std::string metadata = android::base::Join( |
| 326 | std::vector<std::string>{ |
| 327 | "ota-type=BRICK", |
| 328 | }, |
| 329 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 330 | TestCheckPackageMetadata(metadata, OtaType::BRICK, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 331 | |
| 332 | // device type mismatches |
| 333 | metadata = android::base::Join( |
| 334 | std::vector<std::string>{ |
| 335 | "ota-type=BRICK", |
| 336 | "pre-device=dummy_device_type", |
| 337 | }, |
| 338 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 339 | TestCheckPackageMetadata(metadata, OtaType::BRICK, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST(InstallTest, CheckPackageMetadata_serial_number_smoke) { |
| 343 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 344 | ASSERT_NE("", device); |
| 345 | |
| 346 | // Serial number doesn't need to exist |
| 347 | std::string metadata = android::base::Join( |
| 348 | std::vector<std::string>{ |
| 349 | "ota-type=BRICK", |
| 350 | "pre-device=" + device, |
| 351 | }, |
| 352 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 353 | TestCheckPackageMetadata(metadata, OtaType::BRICK, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 354 | |
| 355 | // Serial number mismatches |
| 356 | metadata = android::base::Join( |
| 357 | std::vector<std::string>{ |
| 358 | "ota-type=BRICK", |
| 359 | "pre-device=" + device, |
| 360 | "serialno=dummy_serial", |
| 361 | }, |
| 362 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 363 | TestCheckPackageMetadata(metadata, OtaType::BRICK, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 364 | |
| 365 | std::string serialno = android::base::GetProperty("ro.serialno", ""); |
| 366 | ASSERT_NE("", serialno); |
| 367 | metadata = android::base::Join( |
| 368 | std::vector<std::string>{ |
| 369 | "ota-type=BRICK", |
| 370 | "pre-device=" + device, |
| 371 | "serialno=" + serialno, |
| 372 | }, |
| 373 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 374 | TestCheckPackageMetadata(metadata, OtaType::BRICK, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST(InstallTest, CheckPackageMetadata_multiple_serial_number) { |
| 378 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 379 | ASSERT_NE("", device); |
| 380 | |
| 381 | std::string serialno = android::base::GetProperty("ro.serialno", ""); |
| 382 | ASSERT_NE("", serialno); |
| 383 | |
| 384 | std::vector<std::string> serial_numbers; |
| 385 | // Creates a dummy serial number string. |
xunchang | 7b08a5a | 2019-02-05 12:44:53 -0800 | [diff] [blame] | 386 | for (char c = 'a'; c <= 'z'; c++) { |
| 387 | serial_numbers.emplace_back(serialno.size(), c); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // No matched serialno found. |
| 391 | std::string metadata = android::base::Join( |
| 392 | std::vector<std::string>{ |
| 393 | "ota-type=BRICK", |
| 394 | "pre-device=" + device, |
| 395 | "serialno=" + android::base::Join(serial_numbers, '|'), |
| 396 | }, |
| 397 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 398 | TestCheckPackageMetadata(metadata, OtaType::BRICK, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 399 | |
| 400 | serial_numbers.emplace_back(serialno); |
| 401 | std::shuffle(serial_numbers.begin(), serial_numbers.end(), std::default_random_engine()); |
| 402 | metadata = android::base::Join( |
| 403 | std::vector<std::string>{ |
| 404 | "ota-type=BRICK", |
| 405 | "pre-device=" + device, |
| 406 | "serialno=" + android::base::Join(serial_numbers, '|'), |
| 407 | }, |
| 408 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 409 | TestCheckPackageMetadata(metadata, OtaType::BRICK, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | TEST(InstallTest, CheckPackageMetadata_ab_build_version) { |
| 413 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 414 | ASSERT_NE("", device); |
| 415 | |
| 416 | std::string build_version = android::base::GetProperty("ro.build.version.incremental", ""); |
| 417 | ASSERT_NE("", build_version); |
| 418 | |
| 419 | std::string metadata = android::base::Join( |
| 420 | std::vector<std::string>{ |
| 421 | "ota-type=AB", |
| 422 | "pre-device=" + device, |
| 423 | "pre-build-incremental=" + build_version, |
| 424 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 425 | }, |
| 426 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 427 | TestCheckPackageMetadata(metadata, OtaType::AB, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 428 | |
| 429 | metadata = android::base::Join( |
| 430 | std::vector<std::string>{ |
| 431 | "ota-type=AB", |
| 432 | "pre-device=" + device, |
| 433 | "pre-build-incremental=dummy_build", |
| 434 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 435 | }, |
| 436 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 437 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | TEST(InstallTest, CheckPackageMetadata_ab_fingerprint) { |
| 441 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 442 | ASSERT_NE("", device); |
| 443 | |
| 444 | std::string finger_print = android::base::GetProperty("ro.build.fingerprint", ""); |
| 445 | ASSERT_NE("", finger_print); |
| 446 | |
| 447 | std::string metadata = android::base::Join( |
| 448 | std::vector<std::string>{ |
| 449 | "ota-type=AB", |
| 450 | "pre-device=" + device, |
| 451 | "pre-build=" + finger_print, |
| 452 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 453 | }, |
| 454 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 455 | TestCheckPackageMetadata(metadata, OtaType::AB, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 456 | |
| 457 | metadata = android::base::Join( |
| 458 | std::vector<std::string>{ |
| 459 | "ota-type=AB", |
| 460 | "pre-device=" + device, |
| 461 | "pre-build=dummy_build_fingerprint", |
| 462 | "post-timestamp=" + std::to_string(std::numeric_limits<int64_t>::max()), |
| 463 | }, |
| 464 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 465 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | TEST(InstallTest, CheckPackageMetadata_ab_post_timestamp) { |
| 469 | std::string device = android::base::GetProperty("ro.product.device", ""); |
| 470 | ASSERT_NE("", device); |
| 471 | |
| 472 | // post timestamp is required for upgrade. |
| 473 | std::string metadata = android::base::Join( |
| 474 | std::vector<std::string>{ |
| 475 | "ota-type=AB", |
| 476 | "pre-device=" + device, |
| 477 | }, |
| 478 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 479 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 480 | |
| 481 | // post timestamp should be larger than the timestamp on device. |
| 482 | metadata = android::base::Join( |
| 483 | std::vector<std::string>{ |
| 484 | "ota-type=AB", |
| 485 | "pre-device=" + device, |
| 486 | "post-timestamp=0", |
| 487 | }, |
| 488 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 489 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 490 | |
| 491 | // fingerprint is required for downgrade |
| 492 | metadata = android::base::Join( |
| 493 | std::vector<std::string>{ |
| 494 | "ota-type=AB", |
| 495 | "pre-device=" + device, |
| 496 | "post-timestamp=0", |
| 497 | "ota-downgrade=yes", |
| 498 | }, |
| 499 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 500 | TestCheckPackageMetadata(metadata, OtaType::AB, false); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 501 | |
| 502 | std::string finger_print = android::base::GetProperty("ro.build.fingerprint", ""); |
| 503 | ASSERT_NE("", finger_print); |
| 504 | |
| 505 | metadata = android::base::Join( |
| 506 | std::vector<std::string>{ |
| 507 | "ota-type=AB", |
| 508 | "pre-device=" + device, |
| 509 | "post-timestamp=0", |
| 510 | "pre-build=" + finger_print, |
| 511 | "ota-downgrade=yes", |
| 512 | }, |
| 513 | "\n"); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 514 | TestCheckPackageMetadata(metadata, OtaType::AB, true); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 515 | } |