1 /*
2 * Copyright (C) 2016 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 #include <stdlib.h>
19
20 #include <string>
21 #include <vector>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26 #include <bsdiff/bsdiff.h>
27 #include <gtest/gtest.h>
28 #include <openssl/sha.h>
29
30 #include "applypatch/applypatch_modes.h"
31 #include "common/test_constants.h"
32 #include "otautil/paths.h"
33 #include "otautil/print_sha1.h"
34 #include "otautil/sysutil.h"
35
36 using namespace std::string_literals;
37
38 // Loads a given partition and returns a string of form "EMMC:name:size:hash".
GetEmmcTargetString(const std::string & filename,const std::string & display_name="")39 static std::string GetEmmcTargetString(const std::string& filename,
40 const std::string& display_name = "") {
41 std::string data;
42 if (!android::base::ReadFileToString(filename, &data)) {
43 PLOG(ERROR) << "Failed to read " << filename;
44 return {};
45 }
46
47 uint8_t digest[SHA_DIGEST_LENGTH];
48 SHA1(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), digest);
49
50 return "EMMC:"s + (display_name.empty() ? filename : display_name) + ":" +
51 std::to_string(data.size()) + ":" + print_sha1(digest);
52 }
53
54 class ApplyPatchModesTest : public ::testing::Test {
55 protected:
SetUp()56 void SetUp() override {
57 source = GetEmmcTargetString(from_testdata_base("boot.img"));
58 ASSERT_FALSE(source.empty());
59
60 std::string recovery_file = from_testdata_base("recovery.img");
61 recovery = GetEmmcTargetString(recovery_file);
62 ASSERT_FALSE(recovery.empty());
63
64 ASSERT_TRUE(android::base::WriteStringToFile("", patched_file_.path));
65 target = GetEmmcTargetString(recovery_file, patched_file_.path);
66 ASSERT_FALSE(target.empty());
67
68 Paths::Get().set_cache_temp_source(cache_source_.path);
69 }
70
71 std::string source;
72 std::string target;
73 std::string recovery;
74
75 private:
76 TemporaryFile cache_source_;
77 TemporaryFile patched_file_;
78 };
79
InvokeApplyPatchModes(const std::vector<std::string> & args)80 static int InvokeApplyPatchModes(const std::vector<std::string>& args) {
81 auto args_to_call = StringVectorToNullTerminatedArray(args);
82 return applypatch_modes(args_to_call.size() - 1, args_to_call.data());
83 }
84
VerifyPatchedTarget(const std::string & target)85 static void VerifyPatchedTarget(const std::string& target) {
86 std::vector<std::string> pieces = android::base::Split(target, ":");
87 ASSERT_EQ(4, pieces.size());
88 ASSERT_EQ("EMMC", pieces[0]);
89
90 std::string patched_emmc = GetEmmcTargetString(pieces[1]);
91 ASSERT_FALSE(patched_emmc.empty());
92 ASSERT_EQ(target, patched_emmc);
93 }
94
TEST_F(ApplyPatchModesTest,InvalidArgs)95 TEST_F(ApplyPatchModesTest, InvalidArgs) {
96 // At least two args (including the filename).
97 ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch" }));
98
99 // Unrecognized args.
100 ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch", "-x" }));
101 }
102
TEST_F(ApplyPatchModesTest,PatchModeEmmcTarget)103 TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) {
104 std::vector<std::string> args{
105 "applypatch",
106 "--bonus",
107 from_testdata_base("bonus.file"),
108 "--patch",
109 from_testdata_base("recovery-from-boot.p"),
110 "--target",
111 target,
112 "--source",
113 source,
114 };
115 ASSERT_EQ(0, InvokeApplyPatchModes(args));
116 VerifyPatchedTarget(target);
117 }
118
119 // Tests patching an eMMC target without a separate bonus file (i.e. recovery-from-boot patch has
120 // everything).
TEST_F(ApplyPatchModesTest,PatchModeEmmcTargetWithoutBonusFile)121 TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) {
122 std::vector<std::string> args{
123 "applypatch", "--patch", from_testdata_base("recovery-from-boot-with-bonus.p"),
124 "--target", target, "--source",
125 source,
126 };
127
128 ASSERT_EQ(0, InvokeApplyPatchModes(args));
129 VerifyPatchedTarget(target);
130 }
131
132 // Ensures that applypatch works with a bsdiff based recovery-from-boot.p.
TEST_F(ApplyPatchModesTest,PatchModeEmmcTargetWithBsdiffPatch)133 TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) {
134 // Generate the bsdiff patch of recovery-from-boot.p.
135 std::string src_content;
136 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("boot.img"), &src_content));
137
138 std::string tgt_content;
139 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("recovery.img"), &tgt_content));
140
141 TemporaryFile patch_file;
142 ASSERT_EQ(0,
143 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
144 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
145 patch_file.path, nullptr));
146
147 std::vector<std::string> args{
148 "applypatch", "--patch", patch_file.path, "--target", target, "--source", source,
149 };
150 ASSERT_EQ(0, InvokeApplyPatchModes(args));
151 VerifyPatchedTarget(target);
152 }
153
TEST_F(ApplyPatchModesTest,PatchModeInvalidArgs)154 TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) {
155 // Invalid bonus file.
156 std::vector<std::string> args{
157 "applypatch", "--bonus", "/doesntexist", "--patch", from_testdata_base("recovery-from-boot.p"),
158 "--target", target, "--source", source,
159 };
160 ASSERT_NE(0, InvokeApplyPatchModes(args));
161
162 // With bonus file, but missing args.
163 ASSERT_NE(0,
164 InvokeApplyPatchModes({ "applypatch", "--bonus", from_testdata_base("bonus.file") }));
165 }
166
TEST_F(ApplyPatchModesTest,FlashMode)167 TEST_F(ApplyPatchModesTest, FlashMode) {
168 std::vector<std::string> args{
169 "applypatch", "--flash", from_testdata_base("recovery.img"), "--target", target,
170 };
171 ASSERT_EQ(0, InvokeApplyPatchModes(args));
172 VerifyPatchedTarget(target);
173 }
174
TEST_F(ApplyPatchModesTest,FlashModeInvalidArgs)175 TEST_F(ApplyPatchModesTest, FlashModeInvalidArgs) {
176 std::vector<std::string> args{
177 "applypatch", "--bonus", from_testdata_base("bonus.file"), "--flash", source,
178 "--target", target,
179 };
180 ASSERT_NE(0, InvokeApplyPatchModes(args));
181 }
182
TEST_F(ApplyPatchModesTest,CheckMode)183 TEST_F(ApplyPatchModesTest, CheckMode) {
184 ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--check", recovery }));
185 ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--check", source }));
186 }
187
TEST_F(ApplyPatchModesTest,CheckModeInvalidArgs)188 TEST_F(ApplyPatchModesTest, CheckModeInvalidArgs) {
189 ASSERT_EQ(2, InvokeApplyPatchModes({ "applypatch", "--check" }));
190 }
191
TEST_F(ApplyPatchModesTest,CheckModeNonEmmcTarget)192 TEST_F(ApplyPatchModesTest, CheckModeNonEmmcTarget) {
193 ASSERT_NE(0, InvokeApplyPatchModes({ "applypatch", "--check", from_testdata_base("boot.img") }));
194 }
195
TEST_F(ApplyPatchModesTest,ShowLicenses)196 TEST_F(ApplyPatchModesTest, ShowLicenses) {
197 ASSERT_EQ(0, InvokeApplyPatchModes({ "applypatch", "--license" }));
198 }
199