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