blob: 40201d76fc0792fed99ad9a548f66bfee9b0df49 [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 Baof2784b62017-04-19 12:37:12 -070018#include <unistd.h>
Tao Bao1d866052017-04-10 16:55:57 -070019
Tao Baobc4b1fe2017-04-17 16:46:05 -070020#include <string>
21#include <vector>
22
Tao Baof2784b62017-04-19 12:37:12 -070023#include <android-base/file.h>
Tao Baobc4b1fe2017-04-17 16:46:05 -070024#include <android-base/properties.h>
25#include <android-base/strings.h>
Tao Bao1d866052017-04-10 16:55:57 -070026#include <android-base/test_utils.h>
27#include <gtest/gtest.h>
Tao Baof2784b62017-04-19 12:37:12 -070028#include <vintf/VintfObjectRecovery.h>
Tao Bao1d866052017-04-10 16:55:57 -070029#include <ziparchive/zip_archive.h>
30#include <ziparchive/zip_writer.h>
31
32#include "install.h"
Tao Baobc4b1fe2017-04-17 16:46:05 -070033#include "private/install.h"
Tao Bao1d866052017-04-10 16:55:57 -070034
35TEST(InstallTest, verify_package_compatibility_no_entry) {
36 TemporaryFile temp_file;
37 FILE* zip_file = fdopen(temp_file.fd, "w");
38 ZipWriter writer(zip_file);
39 // The archive must have something to be opened correctly.
40 ASSERT_EQ(0, writer.StartEntry("dummy_entry", 0));
41 ASSERT_EQ(0, writer.FinishEntry());
42 ASSERT_EQ(0, writer.Finish());
43 ASSERT_EQ(0, fclose(zip_file));
44
45 // Doesn't contain compatibility zip entry.
46 ZipArchiveHandle zip;
47 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
48 ASSERT_TRUE(verify_package_compatibility(zip));
49 CloseArchive(zip);
50}
51
52TEST(InstallTest, verify_package_compatibility_invalid_entry) {
53 TemporaryFile temp_file;
54 FILE* zip_file = fdopen(temp_file.fd, "w");
55 ZipWriter writer(zip_file);
56 ASSERT_EQ(0, writer.StartEntry("compatibility.zip", 0));
57 ASSERT_EQ(0, writer.FinishEntry());
58 ASSERT_EQ(0, writer.Finish());
59 ASSERT_EQ(0, fclose(zip_file));
60
61 // Empty compatibility zip entry.
62 ZipArchiveHandle zip;
63 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
64 ASSERT_FALSE(verify_package_compatibility(zip));
65 CloseArchive(zip);
66}
Tao Baobc4b1fe2017-04-17 16:46:05 -070067
Tao Bao8a7afcc2017-04-18 22:05:50 -070068TEST(InstallTest, read_metadata_from_package_smoke) {
69 TemporaryFile temp_file;
70 FILE* zip_file = fdopen(temp_file.fd, "w");
71 ZipWriter writer(zip_file);
72 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
73 const std::string content("abcdefg");
74 ASSERT_EQ(0, writer.WriteBytes(content.data(), content.size()));
75 ASSERT_EQ(0, writer.FinishEntry());
76 ASSERT_EQ(0, writer.Finish());
77 ASSERT_EQ(0, fclose(zip_file));
78
79 ZipArchiveHandle zip;
80 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
81 std::string metadata;
82 ASSERT_TRUE(read_metadata_from_package(zip, &metadata));
83 ASSERT_EQ(content, metadata);
84 CloseArchive(zip);
85
86 TemporaryFile temp_file2;
87 FILE* zip_file2 = fdopen(temp_file2.fd, "w");
88 ZipWriter writer2(zip_file2);
89 ASSERT_EQ(0, writer2.StartEntry("META-INF/com/android/metadata", kCompressDeflated));
90 ASSERT_EQ(0, writer2.WriteBytes(content.data(), content.size()));
91 ASSERT_EQ(0, writer2.FinishEntry());
92 ASSERT_EQ(0, writer2.Finish());
93 ASSERT_EQ(0, fclose(zip_file2));
94
95 ASSERT_EQ(0, OpenArchive(temp_file2.path, &zip));
96 metadata.clear();
97 ASSERT_TRUE(read_metadata_from_package(zip, &metadata));
98 ASSERT_EQ(content, metadata);
99 CloseArchive(zip);
100}
101
102TEST(InstallTest, read_metadata_from_package_no_entry) {
103 TemporaryFile temp_file;
104 FILE* zip_file = fdopen(temp_file.fd, "w");
105 ZipWriter writer(zip_file);
106 ASSERT_EQ(0, writer.StartEntry("dummy_entry", kCompressStored));
107 ASSERT_EQ(0, writer.FinishEntry());
108 ASSERT_EQ(0, writer.Finish());
109 ASSERT_EQ(0, fclose(zip_file));
110
111 ZipArchiveHandle zip;
112 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
113 std::string metadata;
114 ASSERT_FALSE(read_metadata_from_package(zip, &metadata));
115 CloseArchive(zip);
116}
117
Tao Baof2784b62017-04-19 12:37:12 -0700118TEST(InstallTest, verify_package_compatibility_with_libvintf_malformed_xml) {
119 TemporaryFile compatibility_zip_file;
120 FILE* compatibility_zip = fdopen(compatibility_zip_file.fd, "w");
121 ZipWriter compatibility_zip_writer(compatibility_zip);
122 ASSERT_EQ(0, compatibility_zip_writer.StartEntry("system_manifest.xml", kCompressDeflated));
123 std::string malformed_xml = "malformed";
124 ASSERT_EQ(0, compatibility_zip_writer.WriteBytes(malformed_xml.data(), malformed_xml.size()));
125 ASSERT_EQ(0, compatibility_zip_writer.FinishEntry());
126 ASSERT_EQ(0, compatibility_zip_writer.Finish());
127 ASSERT_EQ(0, fclose(compatibility_zip));
128
129 TemporaryFile temp_file;
130 FILE* zip_file = fdopen(temp_file.fd, "w");
131 ZipWriter writer(zip_file);
132 ASSERT_EQ(0, writer.StartEntry("compatibility.zip", kCompressStored));
133 std::string compatibility_zip_content;
134 ASSERT_TRUE(
135 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
136 ASSERT_EQ(0,
137 writer.WriteBytes(compatibility_zip_content.data(), compatibility_zip_content.size()));
138 ASSERT_EQ(0, writer.FinishEntry());
139 ASSERT_EQ(0, writer.Finish());
140 ASSERT_EQ(0, fclose(zip_file));
141
142 ZipArchiveHandle zip;
143 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
144 std::vector<std::string> compatibility_info;
145 compatibility_info.push_back(malformed_xml);
146 // Malformed compatibility zip is expected to be rejected by libvintf. But we defer that to
147 // libvintf.
148 std::string err;
149 bool result =
150 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
151 ASSERT_EQ(result, verify_package_compatibility(zip));
152 CloseArchive(zip);
153}
154
155TEST(InstallTest, verify_package_compatibility_with_libvintf_system_manifest_xml) {
156 static constexpr const char* system_manifest_xml_path = "/system/manifest.xml";
157 if (access(system_manifest_xml_path, R_OK) == -1) {
158 GTEST_LOG_(INFO) << "Test skipped on devices w/o /system/manifest.xml.";
159 return;
160 }
161 std::string system_manifest_xml_content;
162 ASSERT_TRUE(
163 android::base::ReadFileToString(system_manifest_xml_path, &system_manifest_xml_content));
164 TemporaryFile compatibility_zip_file;
165 FILE* compatibility_zip = fdopen(compatibility_zip_file.fd, "w");
166 ZipWriter compatibility_zip_writer(compatibility_zip);
167 ASSERT_EQ(0, compatibility_zip_writer.StartEntry("system_manifest.xml", kCompressDeflated));
168 ASSERT_EQ(0, compatibility_zip_writer.WriteBytes(system_manifest_xml_content.data(),
169 system_manifest_xml_content.size()));
170 ASSERT_EQ(0, compatibility_zip_writer.FinishEntry());
171 ASSERT_EQ(0, compatibility_zip_writer.Finish());
172 ASSERT_EQ(0, fclose(compatibility_zip));
173
174 TemporaryFile temp_file;
175 FILE* zip_file = fdopen(temp_file.fd, "w");
176 ZipWriter writer(zip_file);
177 ASSERT_EQ(0, writer.StartEntry("compatibility.zip", kCompressStored));
178 std::string compatibility_zip_content;
179 ASSERT_TRUE(
180 android::base::ReadFileToString(compatibility_zip_file.path, &compatibility_zip_content));
181 ASSERT_EQ(0,
182 writer.WriteBytes(compatibility_zip_content.data(), compatibility_zip_content.size()));
183 ASSERT_EQ(0, writer.FinishEntry());
184 ASSERT_EQ(0, writer.Finish());
185 ASSERT_EQ(0, fclose(zip_file));
186
187 ZipArchiveHandle zip;
188 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
189 std::vector<std::string> compatibility_info;
190 compatibility_info.push_back(system_manifest_xml_content);
191 std::string err;
192 bool result =
193 android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err) == 0;
194 // Make sure the result is consistent with libvintf library.
195 ASSERT_EQ(result, verify_package_compatibility(zip));
196 CloseArchive(zip);
197}
198
Tao Baobc4b1fe2017-04-17 16:46:05 -0700199TEST(InstallTest, update_binary_command_smoke) {
200#ifdef AB_OTA_UPDATER
201 TemporaryFile temp_file;
202 FILE* zip_file = fdopen(temp_file.fd, "w");
203 ZipWriter writer(zip_file);
204 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
205 ASSERT_EQ(0, writer.FinishEntry());
206 ASSERT_EQ(0, writer.StartEntry("payload_properties.txt", kCompressStored));
207 const std::string properties = "some_properties";
208 ASSERT_EQ(0, writer.WriteBytes(properties.data(), properties.size()));
209 ASSERT_EQ(0, writer.FinishEntry());
210 // A metadata entry is mandatory.
211 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
212 std::string device = android::base::GetProperty("ro.product.device", "");
213 ASSERT_NE("", device);
214 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
215 ASSERT_NE("", timestamp);
216 std::string metadata = android::base::Join(
217 std::vector<std::string>{
218 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
219 },
220 "\n");
221 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
222 ASSERT_EQ(0, writer.FinishEntry());
223 ASSERT_EQ(0, writer.Finish());
224 ASSERT_EQ(0, fclose(zip_file));
225
226 ZipArchiveHandle zip;
227 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
228 int status_fd = 10;
229 std::string path = "/path/to/update.zip";
230 std::vector<std::string> cmd;
231 ASSERT_EQ(0, update_binary_command(path, zip, 0, status_fd, &cmd));
232 ASSERT_EQ("/sbin/update_engine_sideload", cmd[0]);
233 ASSERT_EQ("--payload=file://" + path, cmd[1]);
234 ASSERT_EQ("--headers=" + properties, cmd[3]);
235 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
236 CloseArchive(zip);
237#else
238 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
239 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
240#endif // AB_OTA_UPDATER
241}
242
243TEST(InstallTest, update_binary_command_invalid) {
244#ifdef AB_OTA_UPDATER
245 TemporaryFile temp_file;
246 FILE* zip_file = fdopen(temp_file.fd, "w");
247 ZipWriter writer(zip_file);
248 // Missing payload_properties.txt.
249 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
250 ASSERT_EQ(0, writer.FinishEntry());
251 // A metadata entry is mandatory.
252 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
253 std::string device = android::base::GetProperty("ro.product.device", "");
254 ASSERT_NE("", device);
255 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
256 ASSERT_NE("", timestamp);
257 std::string metadata = android::base::Join(
258 std::vector<std::string>{
259 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
260 },
261 "\n");
262 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
263 ASSERT_EQ(0, writer.FinishEntry());
264 ASSERT_EQ(0, writer.Finish());
265 ASSERT_EQ(0, fclose(zip_file));
266
267 ZipArchiveHandle zip;
268 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
269 int status_fd = 10;
270 std::string path = "/path/to/update.zip";
271 std::vector<std::string> cmd;
272 ASSERT_EQ(INSTALL_CORRUPT, update_binary_command(path, zip, 0, status_fd, &cmd));
273 CloseArchive(zip);
274#else
275 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
276 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
277#endif // AB_OTA_UPDATER
278}