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