blob: 2143dd7bbcc5976b302eec4cefd9e0ba774184b2 [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>
18
Tao Baobc4b1fe2017-04-17 16:46:05 -070019#include <string>
20#include <vector>
21
22#include <android-base/properties.h>
23#include <android-base/strings.h>
Tao Bao1d866052017-04-10 16:55:57 -070024#include <android-base/test_utils.h>
25#include <gtest/gtest.h>
26#include <ziparchive/zip_archive.h>
27#include <ziparchive/zip_writer.h>
28
29#include "install.h"
Tao Baobc4b1fe2017-04-17 16:46:05 -070030#include "private/install.h"
Tao Bao1d866052017-04-10 16:55:57 -070031
32TEST(InstallTest, verify_package_compatibility_no_entry) {
33 TemporaryFile temp_file;
34 FILE* zip_file = fdopen(temp_file.fd, "w");
35 ZipWriter writer(zip_file);
36 // The archive must have something to be opened correctly.
37 ASSERT_EQ(0, writer.StartEntry("dummy_entry", 0));
38 ASSERT_EQ(0, writer.FinishEntry());
39 ASSERT_EQ(0, writer.Finish());
40 ASSERT_EQ(0, fclose(zip_file));
41
42 // Doesn't contain compatibility zip entry.
43 ZipArchiveHandle zip;
44 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
45 ASSERT_TRUE(verify_package_compatibility(zip));
46 CloseArchive(zip);
47}
48
49TEST(InstallTest, verify_package_compatibility_invalid_entry) {
50 TemporaryFile temp_file;
51 FILE* zip_file = fdopen(temp_file.fd, "w");
52 ZipWriter writer(zip_file);
53 ASSERT_EQ(0, writer.StartEntry("compatibility.zip", 0));
54 ASSERT_EQ(0, writer.FinishEntry());
55 ASSERT_EQ(0, writer.Finish());
56 ASSERT_EQ(0, fclose(zip_file));
57
58 // Empty compatibility zip entry.
59 ZipArchiveHandle zip;
60 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
61 ASSERT_FALSE(verify_package_compatibility(zip));
62 CloseArchive(zip);
63}
Tao Baobc4b1fe2017-04-17 16:46:05 -070064
Tao Bao8a7afcc2017-04-18 22:05:50 -070065TEST(InstallTest, read_metadata_from_package_smoke) {
66 TemporaryFile temp_file;
67 FILE* zip_file = fdopen(temp_file.fd, "w");
68 ZipWriter writer(zip_file);
69 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
70 const std::string content("abcdefg");
71 ASSERT_EQ(0, writer.WriteBytes(content.data(), content.size()));
72 ASSERT_EQ(0, writer.FinishEntry());
73 ASSERT_EQ(0, writer.Finish());
74 ASSERT_EQ(0, fclose(zip_file));
75
76 ZipArchiveHandle zip;
77 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
78 std::string metadata;
79 ASSERT_TRUE(read_metadata_from_package(zip, &metadata));
80 ASSERT_EQ(content, metadata);
81 CloseArchive(zip);
82
83 TemporaryFile temp_file2;
84 FILE* zip_file2 = fdopen(temp_file2.fd, "w");
85 ZipWriter writer2(zip_file2);
86 ASSERT_EQ(0, writer2.StartEntry("META-INF/com/android/metadata", kCompressDeflated));
87 ASSERT_EQ(0, writer2.WriteBytes(content.data(), content.size()));
88 ASSERT_EQ(0, writer2.FinishEntry());
89 ASSERT_EQ(0, writer2.Finish());
90 ASSERT_EQ(0, fclose(zip_file2));
91
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;
101 FILE* zip_file = fdopen(temp_file.fd, "w");
102 ZipWriter writer(zip_file);
103 ASSERT_EQ(0, writer.StartEntry("dummy_entry", kCompressStored));
104 ASSERT_EQ(0, writer.FinishEntry());
105 ASSERT_EQ(0, writer.Finish());
106 ASSERT_EQ(0, fclose(zip_file));
107
108 ZipArchiveHandle zip;
109 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
110 std::string metadata;
111 ASSERT_FALSE(read_metadata_from_package(zip, &metadata));
112 CloseArchive(zip);
113}
114
Tao Baobc4b1fe2017-04-17 16:46:05 -0700115TEST(InstallTest, update_binary_command_smoke) {
116#ifdef AB_OTA_UPDATER
117 TemporaryFile temp_file;
118 FILE* zip_file = fdopen(temp_file.fd, "w");
119 ZipWriter writer(zip_file);
120 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
121 ASSERT_EQ(0, writer.FinishEntry());
122 ASSERT_EQ(0, writer.StartEntry("payload_properties.txt", kCompressStored));
123 const std::string properties = "some_properties";
124 ASSERT_EQ(0, writer.WriteBytes(properties.data(), properties.size()));
125 ASSERT_EQ(0, writer.FinishEntry());
126 // A metadata entry is mandatory.
127 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
128 std::string device = android::base::GetProperty("ro.product.device", "");
129 ASSERT_NE("", device);
130 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
131 ASSERT_NE("", timestamp);
132 std::string metadata = android::base::Join(
133 std::vector<std::string>{
134 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
135 },
136 "\n");
137 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.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 int status_fd = 10;
145 std::string path = "/path/to/update.zip";
146 std::vector<std::string> cmd;
147 ASSERT_EQ(0, update_binary_command(path, zip, 0, status_fd, &cmd));
148 ASSERT_EQ("/sbin/update_engine_sideload", cmd[0]);
149 ASSERT_EQ("--payload=file://" + path, cmd[1]);
150 ASSERT_EQ("--headers=" + properties, cmd[3]);
151 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
152 CloseArchive(zip);
153#else
154 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
155 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
156#endif // AB_OTA_UPDATER
157}
158
159TEST(InstallTest, update_binary_command_invalid) {
160#ifdef AB_OTA_UPDATER
161 TemporaryFile temp_file;
162 FILE* zip_file = fdopen(temp_file.fd, "w");
163 ZipWriter writer(zip_file);
164 // Missing payload_properties.txt.
165 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
166 ASSERT_EQ(0, writer.FinishEntry());
167 // A metadata entry is mandatory.
168 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
169 std::string device = android::base::GetProperty("ro.product.device", "");
170 ASSERT_NE("", device);
171 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
172 ASSERT_NE("", timestamp);
173 std::string metadata = android::base::Join(
174 std::vector<std::string>{
175 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
176 },
177 "\n");
178 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
179 ASSERT_EQ(0, writer.FinishEntry());
180 ASSERT_EQ(0, writer.Finish());
181 ASSERT_EQ(0, fclose(zip_file));
182
183 ZipArchiveHandle zip;
184 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
185 int status_fd = 10;
186 std::string path = "/path/to/update.zip";
187 std::vector<std::string> cmd;
188 ASSERT_EQ(INSTALL_CORRUPT, update_binary_command(path, zip, 0, status_fd, &cmd));
189 CloseArchive(zip);
190#else
191 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
192 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
193#endif // AB_OTA_UPDATER
194}