blob: 6583c96f04a6237745a69bcf5e86fa17682a3248 [file] [log] [blame]
Tao Bao1d866052017-04-10 16:55:57 -07001/*
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 Bao00d57572017-05-02 15:48:54 -070018#include <sys/stat.h>
19#include <sys/types.h>
Tao Baof2784b62017-04-19 12:37:12 -070020#include <unistd.h>
Tao Bao1d866052017-04-10 16:55:57 -070021
Tianjie Xu69b96492017-08-17 16:42:57 -070022#include <algorithm>
Tao Baobc4b1fe2017-04-17 16:46:05 -070023#include <string>
24#include <vector>
25
Tao Baof2784b62017-04-19 12:37:12 -070026#include <android-base/file.h>
Tao Baobc4b1fe2017-04-17 16:46:05 -070027#include <android-base/properties.h>
28#include <android-base/strings.h>
Tao Bao1d866052017-04-10 16:55:57 -070029#include <android-base/test_utils.h>
30#include <gtest/gtest.h>
Tao Baof2784b62017-04-19 12:37:12 -070031#include <vintf/VintfObjectRecovery.h>
Tao Bao1d866052017-04-10 16:55:57 -070032#include <ziparchive/zip_archive.h>
33#include <ziparchive/zip_writer.h>
34
35#include "install.h"
Tao Baocf60a442018-06-18 14:56:20 -070036#include "otautil/paths.h"
Tao Baobc4b1fe2017-04-17 16:46:05 -070037#include "private/install.h"
Tao Bao1d866052017-04-10 16:55:57 -070038
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070039static 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 Bao1d866052017-04-10 16:55:57 -070042 ZipWriter writer(zip_file);
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070043 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 Bao1d866052017-04-10 16:55:57 -070048 ASSERT_EQ(0, writer.Finish());
49 ASSERT_EQ(0, fclose(zip_file));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070050}
51
52TEST(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 Bao1d866052017-04-10 16:55:57 -070056
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
64TEST(InstallTest, verify_package_compatibility_invalid_entry) {
65 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070066 BuildZipArchive({ { "compatibility.zip", "" } }, temp_file.release(), kCompressStored);
Tao Bao1d866052017-04-10 16:55:57 -070067
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 Baobc4b1fe2017-04-17 16:46:05 -070074
Tao Bao8a7afcc2017-04-18 22:05:50 -070075TEST(InstallTest, read_metadata_from_package_smoke) {
76 TemporaryFile temp_file;
Tao Bao8a7afcc2017-04-18 22:05:50 -070077 const std::string content("abcdefg");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070078 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file.release(),
79 kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -070080
81 ZipArchiveHandle zip;
82 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
83 std::string metadata;
84 ASSERT_TRUE(read_metadata_from_package(zip, &metadata));
85 ASSERT_EQ(content, metadata);
86 CloseArchive(zip);
87
88 TemporaryFile temp_file2;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -070089 BuildZipArchive({ { "META-INF/com/android/metadata", content } }, temp_file2.release(),
90 kCompressDeflated);
Tao Bao8a7afcc2017-04-18 22:05:50 -070091
92 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
93 metadata.clear();
94 ASSERT_TRUE(read_metadata_from_package(zip, &metadata));
95 ASSERT_EQ(content, metadata);
96 CloseArchive(zip);
97}
98
99TEST(InstallTest, read_metadata_from_package_no_entry) {
100 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700101 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Bao8a7afcc2017-04-18 22:05:50 -0700102
103 ZipArchiveHandle zip;
104 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
105 std::string metadata;
106 ASSERT_FALSE(read_metadata_from_package(zip, &metadata));
107 CloseArchive(zip);
108}
109
Tao Baof2784b62017-04-19 12:37:12 -0700110TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) {
111 TemporaryFile compatibility_zip_file;
Tao Baof2784b62017-04-19 12:37:12 -0700112 std::string malformed_xml = "malformed";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700113 BuildZipArchive({ { "system_manifest.xml", malformed_xml } }, compatibility_zip_file.release(),
114 kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700115
116 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700117 std::string compatibility_zip_content;
118 ASSERT_TRUE(
119 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700120 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
121 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700122
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
136TEST(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 Xuf2fb49a2018-10-26 15:16:50 -0700146 BuildZipArchive({ { "system_manifest.xml", system_manifest_xml_content } },
147 compatibility_zip_file.release(), kCompressDeflated);
Tao Baof2784b62017-04-19 12:37:12 -0700148
149 TemporaryFile temp_file;
Tao Baof2784b62017-04-19 12:37:12 -0700150 std::string compatibility_zip_content;
151 ASSERT_TRUE(
152 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700153 BuildZipArchive({ { "compatibility.zip", compatibility_zip_content } }, temp_file.release(),
154 kCompressStored);
Tao Baof2784b62017-04-19 12:37:12 -0700155
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 Baocf60a442018-06-18 14:56:20 -0700168TEST(InstallTest, SetUpNonAbUpdateCommands) {
169 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700170 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700171 BuildZipArchive({ { UPDATE_BINARY_NAME, "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700172
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
207TEST(InstallTest, SetUpNonAbUpdateCommands_MissingUpdateBinary) {
208 TemporaryFile temp_file;
Tao Baocf60a442018-06-18 14:56:20 -0700209 // The archive must have something to be opened correctly.
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700210 BuildZipArchive({ { "dummy_entry", "" } }, temp_file.release(), kCompressStored);
Tao Baocf60a442018-06-18 14:56:20 -0700211
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
224static void VerifyAbUpdateCommands(const std::string& serialno, bool success = true) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700225 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700226
Tao Baobc4b1fe2017-04-17 16:46:05 -0700227 const std::string properties = "some_properties";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700228 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 Xu69b96492017-08-17 16:42:57 -0700232
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 }
238 std::string metadata = android::base::Join(meta, "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700239
240 BuildZipArchive({ { "payload.bin", "" },
241 { "payload_properties.txt", properties },
242 { "META-INF/com/android/metadata", metadata } },
243 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700244
245 ZipArchiveHandle zip;
246 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
Tao Bao00d57572017-05-02 15:48:54 -0700247 ZipString payload_name("payload.bin");
248 ZipEntry payload_entry;
249 ASSERT_EQ(0, FindEntry(zip, payload_name, &payload_entry));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700250 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700251 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700252 std::vector<std::string> cmd;
Tianjie Xu69b96492017-08-17 16:42:57 -0700253 if (success) {
Tao Baocf60a442018-06-18 14:56:20 -0700254 ASSERT_EQ(0, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700255 ASSERT_EQ(5U, cmd.size());
Tao Bao2cc9bbb2018-08-14 12:34:46 -0700256 ASSERT_EQ("/system/bin/update_engine_sideload", cmd[0]);
Tianjie Xu69b96492017-08-17 16:42:57 -0700257 ASSERT_EQ("--payload=file://" + package, cmd[1]);
258 ASSERT_EQ("--offset=" + std::to_string(payload_entry.offset), cmd[2]);
259 ASSERT_EQ("--headers=" + properties, cmd[3]);
260 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
261 } else {
Tao Baocf60a442018-06-18 14:56:20 -0700262 ASSERT_EQ(INSTALL_ERROR, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tianjie Xu69b96492017-08-17 16:42:57 -0700263 }
Tao Baobc4b1fe2017-04-17 16:46:05 -0700264 CloseArchive(zip);
Tianjie Xu69b96492017-08-17 16:42:57 -0700265}
Tianjie Xu69b96492017-08-17 16:42:57 -0700266
Tao Baocf60a442018-06-18 14:56:20 -0700267TEST(InstallTest, SetUpAbUpdateCommands) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700268 // Empty serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700269 VerifyAbUpdateCommands({});
Tao Baobc4b1fe2017-04-17 16:46:05 -0700270}
271
Tao Baocf60a442018-06-18 14:56:20 -0700272TEST(InstallTest, SetUpAbUpdateCommands_MissingPayloadPropertiesTxt) {
Tao Baobc4b1fe2017-04-17 16:46:05 -0700273 TemporaryFile temp_file;
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700274
Tao Baobc4b1fe2017-04-17 16:46:05 -0700275 std::string device = android::base::GetProperty("ro.product.device", "");
276 ASSERT_NE("", device);
277 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
278 ASSERT_NE("", timestamp);
279 std::string metadata = android::base::Join(
280 std::vector<std::string>{
281 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
282 },
283 "\n");
Tianjie Xuf2fb49a2018-10-26 15:16:50 -0700284
285 BuildZipArchive({ { "payload.bin", "" }, { "META-INF/com/android/metadata", metadata } },
286 temp_file.release(), kCompressStored);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700287
288 ZipArchiveHandle zip;
289 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
290 int status_fd = 10;
Tao Bao00d57572017-05-02 15:48:54 -0700291 std::string package = "/path/to/update.zip";
Tao Baobc4b1fe2017-04-17 16:46:05 -0700292 std::vector<std::string> cmd;
Tao Baocf60a442018-06-18 14:56:20 -0700293 ASSERT_EQ(INSTALL_CORRUPT, SetUpAbUpdateCommands(package, zip, status_fd, &cmd));
Tao Baobc4b1fe2017-04-17 16:46:05 -0700294 CloseArchive(zip);
Tao Baobc4b1fe2017-04-17 16:46:05 -0700295}
Tianjie Xu69b96492017-08-17 16:42:57 -0700296
Tao Baocf60a442018-06-18 14:56:20 -0700297TEST(InstallTest, SetUpAbUpdateCommands_MultipleSerialnos) {
Tianjie Xu69b96492017-08-17 16:42:57 -0700298 std::string serialno = android::base::GetProperty("ro.serialno", "");
299 ASSERT_NE("", serialno);
300
301 // Single matching serialno will pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700302 VerifyAbUpdateCommands(serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700303
304 static constexpr char alphabet[] =
305 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
306 auto generator = []() { return alphabet[rand() % (sizeof(alphabet) - 1)]; };
307
308 // Generate 900 random serial numbers.
Tao Baocf60a442018-06-18 14:56:20 -0700309 std::string random_serialno;
Tianjie Xu69b96492017-08-17 16:42:57 -0700310 for (size_t i = 0; i < 900; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700311 generate_n(back_inserter(random_serialno), serialno.size(), generator);
312 random_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700313 }
314 // Random serialnos should fail the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700315 VerifyAbUpdateCommands(random_serialno, false);
Tianjie Xu69b96492017-08-17 16:42:57 -0700316
Tao Baocf60a442018-06-18 14:56:20 -0700317 std::string long_serialno = random_serialno + serialno + "|";
Tianjie Xu69b96492017-08-17 16:42:57 -0700318 for (size_t i = 0; i < 99; i++) {
Tao Baocf60a442018-06-18 14:56:20 -0700319 generate_n(back_inserter(long_serialno), serialno.size(), generator);
320 long_serialno.append("|");
Tianjie Xu69b96492017-08-17 16:42:57 -0700321 }
322 // String with the matching serialno should pass the verification.
Tao Baocf60a442018-06-18 14:56:20 -0700323 VerifyAbUpdateCommands(long_serialno);
Tianjie Xu69b96492017-08-17 16:42:57 -0700324}