blob: fd3b28b070f6de5bef866e0b3dd5d9b65973aec8 [file] [log] [blame]
Tao Bao62e0bc72017-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 Baoa233a892017-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 Bao62e0bc72017-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 Baoa233a892017-04-17 16:46:05 -070030#include "private/install.h"
Tao Bao62e0bc72017-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 Baoa233a892017-04-17 16:46:05 -070064
65TEST(InstallTest, update_binary_command_smoke) {
66#ifdef AB_OTA_UPDATER
67 TemporaryFile temp_file;
68 FILE* zip_file = fdopen(temp_file.fd, "w");
69 ZipWriter writer(zip_file);
70 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
71 ASSERT_EQ(0, writer.FinishEntry());
72 ASSERT_EQ(0, writer.StartEntry("payload_properties.txt", kCompressStored));
73 const std::string properties = "some_properties";
74 ASSERT_EQ(0, writer.WriteBytes(properties.data(), properties.size()));
75 ASSERT_EQ(0, writer.FinishEntry());
76 // A metadata entry is mandatory.
77 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
78 std::string device = android::base::GetProperty("ro.product.device", "");
79 ASSERT_NE("", device);
80 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
81 ASSERT_NE("", timestamp);
82 std::string metadata = android::base::Join(
83 std::vector<std::string>{
84 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
85 },
86 "\n");
87 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
88 ASSERT_EQ(0, writer.FinishEntry());
89 ASSERT_EQ(0, writer.Finish());
90 ASSERT_EQ(0, fclose(zip_file));
91
92 ZipArchiveHandle zip;
93 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
94 int status_fd = 10;
95 std::string path = "/path/to/update.zip";
96 std::vector<std::string> cmd;
97 ASSERT_EQ(0, update_binary_command(path, zip, 0, status_fd, &cmd));
98 ASSERT_EQ("/sbin/update_engine_sideload", cmd[0]);
99 ASSERT_EQ("--payload=file://" + path, cmd[1]);
100 ASSERT_EQ("--headers=" + properties, cmd[3]);
101 ASSERT_EQ("--status_fd=" + std::to_string(status_fd), cmd[4]);
102 CloseArchive(zip);
103#else
104 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
105 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
106#endif // AB_OTA_UPDATER
107}
108
109TEST(InstallTest, update_binary_command_invalid) {
110#ifdef AB_OTA_UPDATER
111 TemporaryFile temp_file;
112 FILE* zip_file = fdopen(temp_file.fd, "w");
113 ZipWriter writer(zip_file);
114 // Missing payload_properties.txt.
115 ASSERT_EQ(0, writer.StartEntry("payload.bin", kCompressStored));
116 ASSERT_EQ(0, writer.FinishEntry());
117 // A metadata entry is mandatory.
118 ASSERT_EQ(0, writer.StartEntry("META-INF/com/android/metadata", kCompressStored));
119 std::string device = android::base::GetProperty("ro.product.device", "");
120 ASSERT_NE("", device);
121 std::string timestamp = android::base::GetProperty("ro.build.date.utc", "");
122 ASSERT_NE("", timestamp);
123 std::string metadata = android::base::Join(
124 std::vector<std::string>{
125 "ota-type=AB", "pre-device=" + device, "post-timestamp=" + timestamp,
126 },
127 "\n");
128 ASSERT_EQ(0, writer.WriteBytes(metadata.data(), metadata.size()));
129 ASSERT_EQ(0, writer.FinishEntry());
130 ASSERT_EQ(0, writer.Finish());
131 ASSERT_EQ(0, fclose(zip_file));
132
133 ZipArchiveHandle zip;
134 ASSERT_EQ(0, OpenArchive(temp_file.path, &zip));
135 int status_fd = 10;
136 std::string path = "/path/to/update.zip";
137 std::vector<std::string> cmd;
138 ASSERT_EQ(INSTALL_CORRUPT, update_binary_command(path, zip, 0, status_fd, &cmd));
139 CloseArchive(zip);
140#else
141 // Cannot test update_binary_command() because it tries to extract update-binary to /tmp.
142 GTEST_LOG_(INFO) << "Test skipped on non-A/B device.";
143#endif // AB_OTA_UPDATER
144}