blob: bbc83b9a8d33fbb9e0fded6dd2e64be1f91920c5 [file] [log] [blame]
Tao Baoc3901232018-05-21 16:05:56 -07001/*
2 * Copyright (C) 2018 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 agreed 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 <string>
18
19#include <gtest/gtest.h>
20
Tao Bao6a7e4af2018-06-14 21:57:43 -070021#include "otautil/rangeset.h"
Tao Baoc3901232018-05-21 16:05:56 -070022#include "private/commands.h"
23
24TEST(CommandsTest, ParseType) {
25 ASSERT_EQ(Command::Type::ZERO, Command::ParseType("zero"));
26 ASSERT_EQ(Command::Type::NEW, Command::ParseType("new"));
27 ASSERT_EQ(Command::Type::ERASE, Command::ParseType("erase"));
28 ASSERT_EQ(Command::Type::MOVE, Command::ParseType("move"));
29 ASSERT_EQ(Command::Type::BSDIFF, Command::ParseType("bsdiff"));
30 ASSERT_EQ(Command::Type::IMGDIFF, Command::ParseType("imgdiff"));
31 ASSERT_EQ(Command::Type::STASH, Command::ParseType("stash"));
32 ASSERT_EQ(Command::Type::FREE, Command::ParseType("free"));
33}
34
35TEST(CommandsTest, ParseType_InvalidCommand) {
36 ASSERT_EQ(Command::Type::LAST, Command::ParseType("foo"));
37 ASSERT_EQ(Command::Type::LAST, Command::ParseType("bar"));
38}
Tao Bao6a7e4af2018-06-14 21:57:43 -070039
40TEST(CommandsTest, ParseTargetInfoAndSourceInfo_SourceBlocksOnly) {
41 const std::vector<std::string> tokens{
42 "4,569884,569904,591946,592043",
43 "117",
44 "4,566779,566799,591946,592043",
45 };
46 TargetInfo target;
47 SourceInfo source;
48 std::string err;
49 ASSERT_TRUE(Command::ParseTargetInfoAndSourceInfo(
50 tokens, "1d74d1a60332fd38cf9405f1bae67917888da6cb", &target,
51 "1d74d1a60332fd38cf9405f1bae67917888da6cb", &source, &err));
52 ASSERT_EQ(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
53 RangeSet({ { 569884, 569904 }, { 591946, 592043 } })),
54 target);
55 ASSERT_EQ(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
56 RangeSet({ { 566779, 566799 }, { 591946, 592043 } }), {}, {}),
57 source);
58 ASSERT_EQ(117, source.blocks());
59}
60
61TEST(CommandsTest, ParseTargetInfoAndSourceInfo_StashesOnly) {
62 const std::vector<std::string> tokens{
63 "2,350729,350731",
64 "2",
65 "-",
66 "6ebcf8cf1f6be0bc49e7d4a864214251925d1d15:2,0,2",
67 };
68 TargetInfo target;
69 SourceInfo source;
70 std::string err;
71 ASSERT_TRUE(Command::ParseTargetInfoAndSourceInfo(
72 tokens, "6ebcf8cf1f6be0bc49e7d4a864214251925d1d15", &target,
73 "1c25ba04d3278d6b65a1b9f17abac78425ec8b8d", &source, &err));
74 ASSERT_EQ(
75 TargetInfo("6ebcf8cf1f6be0bc49e7d4a864214251925d1d15", RangeSet({ { 350729, 350731 } })),
76 target);
77 ASSERT_EQ(
78 SourceInfo("1c25ba04d3278d6b65a1b9f17abac78425ec8b8d", {}, {},
79 {
80 StashInfo("6ebcf8cf1f6be0bc49e7d4a864214251925d1d15", RangeSet({ { 0, 2 } })),
81 }),
82 source);
83 ASSERT_EQ(2, source.blocks());
84}
85
86TEST(CommandsTest, ParseTargetInfoAndSourceInfo_SourceBlocksAndStashes) {
87 const std::vector<std::string> tokens{
88 "4,611641,611643,636981,637075",
89 "96",
90 "4,636981,637075,770665,770666",
91 "4,0,94,95,96",
92 "9eedf00d11061549e32503cadf054ec6fbfa7a23:2,94,95",
93 };
94 TargetInfo target;
95 SourceInfo source;
96 std::string err;
97 ASSERT_TRUE(Command::ParseTargetInfoAndSourceInfo(
98 tokens, "4734d1b241eb3d0f993714aaf7d665fae43772b6", &target,
99 "a6cbdf3f416960f02189d3a814ec7e9e95c44a0d", &source, &err));
100 ASSERT_EQ(TargetInfo("4734d1b241eb3d0f993714aaf7d665fae43772b6",
101 RangeSet({ { 611641, 611643 }, { 636981, 637075 } })),
102 target);
103 ASSERT_EQ(SourceInfo(
104 "a6cbdf3f416960f02189d3a814ec7e9e95c44a0d",
105 RangeSet({ { 636981, 637075 }, { 770665, 770666 } }), // source ranges
106 RangeSet({ { 0, 94 }, { 95, 96 } }), // source location
107 {
108 StashInfo("9eedf00d11061549e32503cadf054ec6fbfa7a23", RangeSet({ { 94, 95 } })),
109 }),
110 source);
111 ASSERT_EQ(96, source.blocks());
112}
113
114TEST(CommandsTest, ParseTargetInfoAndSourceInfo_InvalidInput) {
115 const std::vector<std::string> tokens{
116 "4,611641,611643,636981,637075",
117 "96",
118 "4,636981,637075,770665,770666",
119 "4,0,94,95,96",
120 "9eedf00d11061549e32503cadf054ec6fbfa7a23:2,94,95",
121 };
122 TargetInfo target;
123 SourceInfo source;
124 std::string err;
125
126 // Mismatching block count.
127 {
128 std::vector<std::string> tokens_copy(tokens);
129 tokens_copy[1] = "97";
130 ASSERT_FALSE(Command::ParseTargetInfoAndSourceInfo(
131 tokens_copy, "1d74d1a60332fd38cf9405f1bae67917888da6cb", &target,
132 "1d74d1a60332fd38cf9405f1bae67917888da6cb", &source, &err));
133 }
134
135 // Excess stashes (causing block count mismatch).
136 {
137 std::vector<std::string> tokens_copy(tokens);
138 tokens_copy.push_back("e145a2f83a33334714ac65e34969c1f115e54a6f:2,0,22");
139 ASSERT_FALSE(Command::ParseTargetInfoAndSourceInfo(
140 tokens_copy, "1d74d1a60332fd38cf9405f1bae67917888da6cb", &target,
141 "1d74d1a60332fd38cf9405f1bae67917888da6cb", &source, &err));
142 }
143
144 // Invalid args.
145 for (size_t i = 0; i < tokens.size(); i++) {
146 TargetInfo target;
147 SourceInfo source;
148 std::string err;
149 ASSERT_FALSE(Command::ParseTargetInfoAndSourceInfo(
150 std::vector<std::string>(tokens.cbegin() + i + 1, tokens.cend()),
151 "1d74d1a60332fd38cf9405f1bae67917888da6cb", &target,
152 "1d74d1a60332fd38cf9405f1bae67917888da6cb", &source, &err));
153 }
154}
155
156TEST(CommandsTest, Parse_EmptyInput) {
157 std::string err;
158 ASSERT_FALSE(Command::Parse("", 0, &err));
159 ASSERT_EQ("invalid type", err);
160}
161
162TEST(CommandsTest, Parse_BSDIFF) {
163 const std::string input{
164 "bsdiff 0 148 "
165 "f201a4e04bd3860da6ad47b957ef424d58a58f8c 9d5d223b4bc5c45dbd25a799c4f1a98466731599 "
166 "4,565704,565752,566779,566799 "
167 "68 4,64525,64545,565704,565752"
168 };
169 std::string err;
170 Command command = Command::Parse(input, 1, &err);
171 ASSERT_TRUE(command);
172
173 ASSERT_EQ(Command::Type::BSDIFF, command.type());
174 ASSERT_EQ(1, command.index());
175 ASSERT_EQ(input, command.cmdline());
176
177 ASSERT_EQ(TargetInfo("9d5d223b4bc5c45dbd25a799c4f1a98466731599",
178 RangeSet({ { 565704, 565752 }, { 566779, 566799 } })),
179 command.target());
180 ASSERT_EQ(SourceInfo("f201a4e04bd3860da6ad47b957ef424d58a58f8c",
181 RangeSet({ { 64525, 64545 }, { 565704, 565752 } }), RangeSet(), {}),
182 command.source());
183 ASSERT_EQ(StashInfo(), command.stash());
184 ASSERT_EQ(PatchInfo(0, 148), command.patch());
185}
186
187TEST(CommandsTest, Parse_ERASE) {
188 const std::string input{ "erase 2,5,10" };
189 std::string err;
190 Command command = Command::Parse(input, 2, &err);
191 ASSERT_TRUE(command);
192
193 ASSERT_EQ(Command::Type::ERASE, command.type());
194 ASSERT_EQ(2, command.index());
195 ASSERT_EQ(input, command.cmdline());
196
197 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 5, 10 } })), command.target());
198 ASSERT_EQ(SourceInfo(), command.source());
199 ASSERT_EQ(StashInfo(), command.stash());
200 ASSERT_EQ(PatchInfo(), command.patch());
201}
202
203TEST(CommandsTest, Parse_FREE) {
204 const std::string input{ "free hash1" };
205 std::string err;
206 Command command = Command::Parse(input, 3, &err);
207 ASSERT_TRUE(command);
208
209 ASSERT_EQ(Command::Type::FREE, command.type());
210 ASSERT_EQ(3, command.index());
211 ASSERT_EQ(input, command.cmdline());
212
213 ASSERT_EQ(TargetInfo(), command.target());
214 ASSERT_EQ(SourceInfo(), command.source());
215 ASSERT_EQ(StashInfo("hash1", RangeSet()), command.stash());
216 ASSERT_EQ(PatchInfo(), command.patch());
217}
218
219TEST(CommandsTest, Parse_IMGDIFF) {
220 const std::string input{
221 "imgdiff 29629269 185 "
222 "a6b1c49aed1b57a2aab1ec3e1505b945540cd8db 51978f65035f584a8ef7afa941dacb6d5e862164 "
223 "2,90851,90852 "
224 "1 2,90851,90852"
225 };
226 std::string err;
227 Command command = Command::Parse(input, 4, &err);
228 ASSERT_TRUE(command);
229
230 ASSERT_EQ(Command::Type::IMGDIFF, command.type());
231 ASSERT_EQ(4, command.index());
232 ASSERT_EQ(input, command.cmdline());
233
234 ASSERT_EQ(TargetInfo("51978f65035f584a8ef7afa941dacb6d5e862164", RangeSet({ { 90851, 90852 } })),
235 command.target());
236 ASSERT_EQ(SourceInfo("a6b1c49aed1b57a2aab1ec3e1505b945540cd8db", RangeSet({ { 90851, 90852 } }),
237 RangeSet(), {}),
238 command.source());
239 ASSERT_EQ(StashInfo(), command.stash());
240 ASSERT_EQ(PatchInfo(29629269, 185), command.patch());
241}
242
243TEST(CommandsTest, Parse_MOVE) {
244 const std::string input{
245 "move 1d74d1a60332fd38cf9405f1bae67917888da6cb "
246 "4,569884,569904,591946,592043 117 4,566779,566799,591946,592043"
247 };
248 std::string err;
249 Command command = Command::Parse(input, 5, &err);
250 ASSERT_TRUE(command);
251
252 ASSERT_EQ(Command::Type::MOVE, command.type());
253 ASSERT_EQ(5, command.index());
254 ASSERT_EQ(input, command.cmdline());
255
256 ASSERT_EQ(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
257 RangeSet({ { 569884, 569904 }, { 591946, 592043 } })),
258 command.target());
259 ASSERT_EQ(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
260 RangeSet({ { 566779, 566799 }, { 591946, 592043 } }), RangeSet(), {}),
261 command.source());
262 ASSERT_EQ(StashInfo(), command.stash());
263 ASSERT_EQ(PatchInfo(), command.patch());
264}
265
266TEST(CommandsTest, Parse_NEW) {
267 const std::string input{ "new 4,3,5,10,12" };
268 std::string err;
269 Command command = Command::Parse(input, 6, &err);
270 ASSERT_TRUE(command);
271
272 ASSERT_EQ(Command::Type::NEW, command.type());
273 ASSERT_EQ(6, command.index());
274 ASSERT_EQ(input, command.cmdline());
275
276 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 3, 5 }, { 10, 12 } })), command.target());
277 ASSERT_EQ(SourceInfo(), command.source());
278 ASSERT_EQ(StashInfo(), command.stash());
279 ASSERT_EQ(PatchInfo(), command.patch());
280}
281
282TEST(CommandsTest, Parse_STASH) {
283 const std::string input{ "stash hash1 2,5,10" };
284 std::string err;
285 Command command = Command::Parse(input, 7, &err);
286 ASSERT_TRUE(command);
287
288 ASSERT_EQ(Command::Type::STASH, command.type());
289 ASSERT_EQ(7, command.index());
290 ASSERT_EQ(input, command.cmdline());
291
292 ASSERT_EQ(TargetInfo(), command.target());
293 ASSERT_EQ(SourceInfo(), command.source());
294 ASSERT_EQ(StashInfo("hash1", RangeSet({ { 5, 10 } })), command.stash());
295 ASSERT_EQ(PatchInfo(), command.patch());
296}
297
298TEST(CommandsTest, Parse_ZERO) {
299 const std::string input{ "zero 2,1,5" };
300 std::string err;
301 Command command = Command::Parse(input, 8, &err);
302 ASSERT_TRUE(command);
303
304 ASSERT_EQ(Command::Type::ZERO, command.type());
305 ASSERT_EQ(8, command.index());
306 ASSERT_EQ(input, command.cmdline());
307
308 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 1, 5 } })), command.target());
309 ASSERT_EQ(SourceInfo(), command.source());
310 ASSERT_EQ(StashInfo(), command.stash());
311 ASSERT_EQ(PatchInfo(), command.patch());
312}