blob: 3daa58f338dca8884c2139f0d1e79116a7b0bb65 [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
Tao Bao91a649a2018-05-21 16:05:56 -0700162TEST(CommandsTest, Parse_ABORT_Allowed) {
163 Command::abort_allowed_ = true;
164
165 const std::string input{ "abort" };
166 std::string err;
167 Command command = Command::Parse(input, 0, &err);
168 ASSERT_TRUE(command);
169
170 ASSERT_EQ(TargetInfo(), command.target());
171 ASSERT_EQ(SourceInfo(), command.source());
172 ASSERT_EQ(StashInfo(), command.stash());
173 ASSERT_EQ(PatchInfo(), command.patch());
174}
175
176TEST(CommandsTest, Parse_ABORT_NotAllowed) {
177 const std::string input{ "abort" };
178 std::string err;
179 Command command = Command::Parse(input, 0, &err);
180 ASSERT_FALSE(command);
181}
182
Tao Bao6a7e4af2018-06-14 21:57:43 -0700183TEST(CommandsTest, Parse_BSDIFF) {
184 const std::string input{
185 "bsdiff 0 148 "
186 "f201a4e04bd3860da6ad47b957ef424d58a58f8c 9d5d223b4bc5c45dbd25a799c4f1a98466731599 "
187 "4,565704,565752,566779,566799 "
188 "68 4,64525,64545,565704,565752"
189 };
190 std::string err;
191 Command command = Command::Parse(input, 1, &err);
192 ASSERT_TRUE(command);
193
194 ASSERT_EQ(Command::Type::BSDIFF, command.type());
195 ASSERT_EQ(1, command.index());
196 ASSERT_EQ(input, command.cmdline());
197
198 ASSERT_EQ(TargetInfo("9d5d223b4bc5c45dbd25a799c4f1a98466731599",
199 RangeSet({ { 565704, 565752 }, { 566779, 566799 } })),
200 command.target());
201 ASSERT_EQ(SourceInfo("f201a4e04bd3860da6ad47b957ef424d58a58f8c",
202 RangeSet({ { 64525, 64545 }, { 565704, 565752 } }), RangeSet(), {}),
203 command.source());
204 ASSERT_EQ(StashInfo(), command.stash());
205 ASSERT_EQ(PatchInfo(0, 148), command.patch());
206}
207
208TEST(CommandsTest, Parse_ERASE) {
209 const std::string input{ "erase 2,5,10" };
210 std::string err;
211 Command command = Command::Parse(input, 2, &err);
212 ASSERT_TRUE(command);
213
214 ASSERT_EQ(Command::Type::ERASE, command.type());
215 ASSERT_EQ(2, command.index());
216 ASSERT_EQ(input, command.cmdline());
217
218 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 5, 10 } })), command.target());
219 ASSERT_EQ(SourceInfo(), command.source());
220 ASSERT_EQ(StashInfo(), command.stash());
221 ASSERT_EQ(PatchInfo(), command.patch());
222}
223
224TEST(CommandsTest, Parse_FREE) {
225 const std::string input{ "free hash1" };
226 std::string err;
227 Command command = Command::Parse(input, 3, &err);
228 ASSERT_TRUE(command);
229
230 ASSERT_EQ(Command::Type::FREE, command.type());
231 ASSERT_EQ(3, command.index());
232 ASSERT_EQ(input, command.cmdline());
233
234 ASSERT_EQ(TargetInfo(), command.target());
235 ASSERT_EQ(SourceInfo(), command.source());
236 ASSERT_EQ(StashInfo("hash1", RangeSet()), command.stash());
237 ASSERT_EQ(PatchInfo(), command.patch());
238}
239
240TEST(CommandsTest, Parse_IMGDIFF) {
241 const std::string input{
242 "imgdiff 29629269 185 "
243 "a6b1c49aed1b57a2aab1ec3e1505b945540cd8db 51978f65035f584a8ef7afa941dacb6d5e862164 "
244 "2,90851,90852 "
245 "1 2,90851,90852"
246 };
247 std::string err;
248 Command command = Command::Parse(input, 4, &err);
249 ASSERT_TRUE(command);
250
251 ASSERT_EQ(Command::Type::IMGDIFF, command.type());
252 ASSERT_EQ(4, command.index());
253 ASSERT_EQ(input, command.cmdline());
254
255 ASSERT_EQ(TargetInfo("51978f65035f584a8ef7afa941dacb6d5e862164", RangeSet({ { 90851, 90852 } })),
256 command.target());
257 ASSERT_EQ(SourceInfo("a6b1c49aed1b57a2aab1ec3e1505b945540cd8db", RangeSet({ { 90851, 90852 } }),
258 RangeSet(), {}),
259 command.source());
260 ASSERT_EQ(StashInfo(), command.stash());
261 ASSERT_EQ(PatchInfo(29629269, 185), command.patch());
262}
263
264TEST(CommandsTest, Parse_MOVE) {
265 const std::string input{
266 "move 1d74d1a60332fd38cf9405f1bae67917888da6cb "
267 "4,569884,569904,591946,592043 117 4,566779,566799,591946,592043"
268 };
269 std::string err;
270 Command command = Command::Parse(input, 5, &err);
271 ASSERT_TRUE(command);
272
273 ASSERT_EQ(Command::Type::MOVE, command.type());
274 ASSERT_EQ(5, command.index());
275 ASSERT_EQ(input, command.cmdline());
276
277 ASSERT_EQ(TargetInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
278 RangeSet({ { 569884, 569904 }, { 591946, 592043 } })),
279 command.target());
280 ASSERT_EQ(SourceInfo("1d74d1a60332fd38cf9405f1bae67917888da6cb",
281 RangeSet({ { 566779, 566799 }, { 591946, 592043 } }), RangeSet(), {}),
282 command.source());
283 ASSERT_EQ(StashInfo(), command.stash());
284 ASSERT_EQ(PatchInfo(), command.patch());
285}
286
287TEST(CommandsTest, Parse_NEW) {
288 const std::string input{ "new 4,3,5,10,12" };
289 std::string err;
290 Command command = Command::Parse(input, 6, &err);
291 ASSERT_TRUE(command);
292
293 ASSERT_EQ(Command::Type::NEW, command.type());
294 ASSERT_EQ(6, command.index());
295 ASSERT_EQ(input, command.cmdline());
296
297 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 3, 5 }, { 10, 12 } })), command.target());
298 ASSERT_EQ(SourceInfo(), command.source());
299 ASSERT_EQ(StashInfo(), command.stash());
300 ASSERT_EQ(PatchInfo(), command.patch());
301}
302
303TEST(CommandsTest, Parse_STASH) {
304 const std::string input{ "stash hash1 2,5,10" };
305 std::string err;
306 Command command = Command::Parse(input, 7, &err);
307 ASSERT_TRUE(command);
308
309 ASSERT_EQ(Command::Type::STASH, command.type());
310 ASSERT_EQ(7, command.index());
311 ASSERT_EQ(input, command.cmdline());
312
313 ASSERT_EQ(TargetInfo(), command.target());
314 ASSERT_EQ(SourceInfo(), command.source());
315 ASSERT_EQ(StashInfo("hash1", RangeSet({ { 5, 10 } })), command.stash());
316 ASSERT_EQ(PatchInfo(), command.patch());
317}
318
319TEST(CommandsTest, Parse_ZERO) {
320 const std::string input{ "zero 2,1,5" };
321 std::string err;
322 Command command = Command::Parse(input, 8, &err);
323 ASSERT_TRUE(command);
324
325 ASSERT_EQ(Command::Type::ZERO, command.type());
326 ASSERT_EQ(8, command.index());
327 ASSERT_EQ(input, command.cmdline());
328
329 ASSERT_EQ(TargetInfo("unknown-hash", RangeSet({ { 1, 5 } })), command.target());
330 ASSERT_EQ(SourceInfo(), command.source());
331 ASSERT_EQ(StashInfo(), command.stash());
332 ASSERT_EQ(PatchInfo(), command.patch());
333}
Tao Bao92f33932018-06-25 12:11:53 -0700334
335TEST(CommandsTest, Parse_InvalidNumberOfArgs) {
Tao Bao91a649a2018-05-21 16:05:56 -0700336 Command::abort_allowed_ = true;
337
Tao Bao92f33932018-06-25 12:11:53 -0700338 // Note that the case of having excess args in BSDIFF, IMGDIFF and MOVE is covered by
339 // ParseTargetInfoAndSourceInfo_InvalidInput.
340 std::vector<std::string> inputs{
Tao Bao91a649a2018-05-21 16:05:56 -0700341 "abort foo",
Tao Bao92f33932018-06-25 12:11:53 -0700342 "bsdiff",
343 "erase",
344 "erase 4,3,5,10,12 hash1",
345 "free",
346 "free id1 id2",
347 "imgdiff",
348 "move",
349 "new",
350 "new 4,3,5,10,12 hash1",
351 "stash",
352 "stash id1",
353 "stash id1 4,3,5,10,12 id2",
354 "zero",
355 "zero 4,3,5,10,12 hash2",
356 };
357 for (const auto& input : inputs) {
358 std::string err;
359 ASSERT_FALSE(Command::Parse(input, 0, &err));
360 }
361}