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