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