blob: 38bfda664a40a49f4d3af1b3e32dfb2d1e2abc56 [file] [log] [blame]
Patrick Venture298930a2019-07-03 11:44:52 -07001#include "buildjson.hpp"
Patrick Venture0caec992019-08-05 09:58:27 -07002#include "update_systemd.hpp"
Patrick Ventureee614ec2019-08-05 12:08:44 -07003#include "verify_systemd.hpp"
Patrick Venture298930a2019-07-03 11:44:52 -07004
5#include <nlohmann/json.hpp>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10namespace ipmi_flash
11{
12namespace
13{
14using ::testing::IsEmpty;
15
16using json = nlohmann::json;
17
18TEST(FirmwareJsonTest, InvalidHandlerType)
19{
20 auto j2 = R"(
21 [{
22 "blob" : "/flash/image",
23 "handler" : {
24 "type" : "unsupported",
25 "path" : "/run/initramfs/bmc-image"
26 },
27 "actions" : {
28 "preparation" : {
29 "type" : "systemd",
30 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
31 },
32 "verification" : {
33 "type" : "fileSystemdVerify",
34 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
35 "path" : "/tmp/bmc.verify"
36 },
37 "update" : {
38 "type" : "reboot"
39 }
40 }
41 }]
42 )"_json;
43
44 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
45}
46
47TEST(FirmwareJsonTest, InvalidPreparationType)
48{
49 auto j2 = R"(
50 [{
51 "blob" : "/flash/image",
52 "handler" : {
53 "type" : "file",
54 "path" : "/run/initramfs/bmc-image"
55 },
56 "actions" : {
57 "preparation" : {
58 "type" : "superfun",
59 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
60 },
61 "verification" : {
62 "type" : "fileSystemdVerify",
63 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
64 "path" : "/tmp/bmc.verify"
65 },
66 "update" : {
67 "type" : "reboot"
68 }
69 }
70 }]
71 )"_json;
72
73 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
74}
75
76TEST(FirmwareJsonTest, InvalidVerificationType)
77{
78 auto j2 = R"(
79 [{
80 "blob" : "/flash/image",
81 "handler" : {
82 "type" : "file",
83 "path" : "/run/initramfs/bmc-image"
84 },
85 "actions" : {
86 "preparation" : {
87 "type" : "systemd",
88 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
89 },
90 "verification" : {
91 "type" : "funtimes",
92 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
93 "path" : "/tmp/bmc.verify"
94 },
95 "update" : {
96 "type" : "reboot"
97 }
98 }
99 }]
100 )"_json;
101
102 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
103}
104
105TEST(FirmwareJsonTest, InvalidUpdateType)
106{
107 auto j2 = R"(
108 [{
109 "blob" : "/flash/image",
110 "handler" : {
111 "type" : "file",
112 "path" : "/run/initramfs/bmc-image"
113 },
114 "actions" : {
115 "preparation" : {
116 "type" : "systemd",
117 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
118 },
119 "verification" : {
120 "type" : "fileSystemdVerify",
121 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
122 "path" : "/tmp/bmc.verify"
123 },
124 "update" : {
125 "type" : "systemd"
126 }
127 }
128 }]
129 )"_json;
130
131 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
132}
133
134TEST(FirmwareJsonTest, MissingHandler)
135{
136 auto j2 = R"(
137 [{
138 "blob" : "/flash/image",
139 "actions" : {
140 "preparation" : {
141 "type" : "systemd",
142 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
143 },
144 "verification" : {
145 "type" : "fileSystemdVerify",
146 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
147 "path" : "/tmp/bmc.verify"
148 },
149 "update" : {
150 "type" : "reboot"
151 }
152 }
153 }]
154 )"_json;
155
156 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
157}
158
159TEST(FirmwareJsonTest, MissingActions)
160{
161 auto j2 = R"(
162 [{
163 "blob" : "/flash/image",
164 "handler" : {
165 "type" : "file",
166 "path" : "/run/initramfs/bmc-image"
167 }
168 }]
169 )"_json;
170
171 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
172}
173
174TEST(FirmwareJsonTest, MissingActionPreparation)
175{
176 auto j2 = R"(
177 [{
178 "blob" : "/flash/image",
179 "handler" : {
180 "type" : "file",
181 "path" : "/run/initramfs/bmc-image"
182 },
183 "actions" : {
184 "verification" : {
185 "type" : "fileSystemdVerify",
186 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
187 "path" : "/tmp/bmc.verify"
188 },
189 "update" : {
190 "type" : "reboot"
191 }
192 }
193 }]
194 )"_json;
195
196 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
197}
198
199TEST(FirmwareJsonTest, MissingActionVerification)
200{
201 auto j2 = R"(
202 [{
203 "blob" : "/flash/image",
204 "handler" : {
205 "type" : "file",
206 "path" : "/run/initramfs/bmc-image"
207 },
208 "actions" : {
209 "preparation" : {
210 "type" : "systemd",
211 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
212 },
213 "update" : {
214 "type" : "reboot"
215 }
216 }
217 }]
218 )"_json;
219
220 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
221}
222
223TEST(FirmwareJsonTest, MissingActionUpdate)
224{
225 auto j2 = R"(
226 [{
227 "blob" : "/flash/image",
228 "handler" : {
229 "type" : "file",
230 "path" : "/run/initramfs/bmc-image"
231 },
232 "actions" : {
233 "preparation" : {
234 "type" : "systemd",
235 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
236 },
237 "verification" : {
238 "type" : "fileSystemdVerify",
239 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
240 "path" : "/tmp/bmc.verify"
241 }
242 }
243 }]
244 )"_json;
245
246 EXPECT_THAT(buildHandlerFromJson(j2), IsEmpty());
247}
248
249TEST(FirmwareJsonTest, TwoConfigsOneInvalidReturnsValid)
250{
251 auto j2 = R"(
252 [{
253 "blob" : "/flash/image",
254 "actions" : {
255 "preparation" : {
256 "type" : "systemd",
257 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
258 },
259 "verification" : {
260 "type" : "fileSystemdVerify",
261 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
262 "path" : "/tmp/bmc.verify"
263 },
264 "update" : {
265 "type" : "reboot"
266 }
267 }
268 },
269 {
270 "blob" : "/flash/image2",
271 "handler" : {
272 "type" : "file",
273 "path" : "/run/initramfs/bmc-image"
274 },
275 "actions" : {
276 "preparation" : {
277 "type" : "systemd",
278 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
279 },
280 "verification" : {
281 "type" : "fileSystemdVerify",
282 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
283 "path" : "/tmp/bmc.verify"
284 },
285 "update" : {
286 "type" : "reboot"
287 }
288 }
289 }]
290 )"_json;
291
292 auto h = buildHandlerFromJson(j2);
293 EXPECT_EQ(h[0].blobId, "/flash/image2");
294 EXPECT_EQ(h.size(), 1);
295}
296
297/*
298 * TODO: It may be worth individually using builders per type, and testing
299 * those.
300 *
301 * TODO: Only allow unique handler blob paths (tested at a higher level).
302 */
303
304TEST(FirmwareJsonTest, VerifySystemdWithReboot)
305{
306 auto j2 = R"(
307 [{
308 "blob" : "/flash/image",
309 "handler" : {
310 "type" : "file",
311 "path" : "/run/initramfs/bmc-image"
312 },
313 "actions" : {
314 "preparation" : {
315 "type" : "systemd",
316 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
317 },
318 "verification" : {
319 "type" : "fileSystemdVerify",
320 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
321 "path" : "/tmp/bmc.verify"
322 },
323 "update" : {
324 "type" : "reboot"
325 }
326 }
327 }]
328 )"_json;
329
330 auto h = buildHandlerFromJson(j2);
331 EXPECT_EQ(h[0].blobId, "/flash/image");
332 EXPECT_FALSE(h[0].handler == nullptr);
333 EXPECT_FALSE(h[0].actions == nullptr);
334 EXPECT_FALSE(h[0].actions->preparation == nullptr);
335 EXPECT_FALSE(h[0].actions->verification == nullptr);
336 EXPECT_FALSE(h[0].actions->update == nullptr);
337}
338
339TEST(FirmwareJsonTest, VerifyMultipleHandlersReturned)
340{
341 auto j2 = R"(
342 [{
343 "blob" : "/flash/image",
344 "handler" : {
345 "type" : "file",
346 "path" : "/run/initramfs/bmc-image"
347 },
348 "actions" : {
349 "preparation" : {
350 "type" : "systemd",
351 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
352 },
353 "verification" : {
354 "type" : "fileSystemdVerify",
355 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
356 "path" : "/tmp/bmc.verify"
357 },
358 "update" : {
359 "type" : "systemd",
360 "unit" : "phosphor-ipmi-flash-bmc-update.target"
361 }
362 }
363 },
364 {
365 "blob" : "/flash/bios",
366 "handler" : {
367 "type" : "file",
368 "path" : "/run/initramfs/bmc-image"
369 },
370 "actions" : {
371 "preparation" : {
372 "type" : "systemd",
373 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
374 },
375 "verification" : {
376 "type" : "fileSystemdVerify",
377 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
378 "path" : "/tmp/bmc.verify"
379 },
380 "update" : {
381 "type" : "systemd",
382 "unit" : "phosphor-ipmi-flash-bmc-update.target"
383 }
384 }
385 }]
386 )"_json;
387
388 auto h = buildHandlerFromJson(j2);
389 EXPECT_EQ(h.size(), 2);
390 EXPECT_EQ(h[0].blobId, "/flash/image");
391 EXPECT_EQ(h[1].blobId, "/flash/bios");
392}
393
394TEST(FirmwareJsonTest, VerifyValidSingleNonReboot)
395{
396 auto j2 = R"(
397 [{
398 "blob" : "/flash/image",
399 "handler" : {
400 "type" : "file",
401 "path" : "/run/initramfs/bmc-image"
402 },
403 "actions" : {
404 "preparation" : {
405 "type" : "systemd",
406 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
407 },
408 "verification" : {
409 "type" : "fileSystemdVerify",
410 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
411 "path" : "/tmp/bmc.verify"
412 },
413 "update" : {
414 "type" : "systemd",
415 "unit" : "phosphor-ipmi-flash-bmc-update.target"
416 }
417 }
418 }]
419 )"_json;
420
421 auto h = buildHandlerFromJson(j2);
422 EXPECT_EQ(h[0].blobId, "/flash/image");
423 EXPECT_FALSE(h[0].handler == nullptr);
424 EXPECT_FALSE(h[0].actions == nullptr);
425 EXPECT_FALSE(h[0].actions->preparation == nullptr);
426 EXPECT_FALSE(h[0].actions->verification == nullptr);
Patrick Venture29af1e32019-08-05 13:42:28 -0700427 auto verifier = reinterpret_cast<SystemdWithStatusFile*>(
Patrick Ventureee614ec2019-08-05 12:08:44 -0700428 h[0].actions->verification.get());
429 EXPECT_THAT(verifier->getMode(), "replace");
Patrick Venture298930a2019-07-03 11:44:52 -0700430 EXPECT_FALSE(h[0].actions->update == nullptr);
Patrick Venture0caec992019-08-05 09:58:27 -0700431 auto updater =
432 reinterpret_cast<SystemdUpdateMechanism*>(h[0].actions->update.get());
433 EXPECT_THAT(updater->getMode(), "replace");
434}
435
Patrick Venture984d94d2019-08-05 12:23:59 -0700436TEST(FirmwareJsonTest, VerifyValidWithModes)
Patrick Venture0caec992019-08-05 09:58:27 -0700437{
438 auto j2 = R"(
439 [{
440 "blob" : "/flash/image",
441 "handler" : {
442 "type" : "file",
443 "path" : "/run/initramfs/bmc-image"
444 },
445 "actions" : {
446 "preparation" : {
447 "type" : "systemd",
448 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
449 },
450 "verification" : {
451 "type" : "fileSystemdVerify",
452 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
Patrick Venture984d94d2019-08-05 12:23:59 -0700453 "path" : "/tmp/bmc.verify",
454 "mode" : "replace-nope"
Patrick Venture0caec992019-08-05 09:58:27 -0700455 },
456 "update" : {
457 "type" : "systemd",
458 "mode" : "replace-fake",
459 "unit" : "phosphor-ipmi-flash-bmc-update.target"
460 }
461 }
462 }]
463 )"_json;
464
465 auto h = buildHandlerFromJson(j2);
466 EXPECT_EQ(h[0].blobId, "/flash/image");
467 EXPECT_FALSE(h[0].handler == nullptr);
468 EXPECT_FALSE(h[0].actions == nullptr);
469 EXPECT_FALSE(h[0].actions->preparation == nullptr);
470 EXPECT_FALSE(h[0].actions->verification == nullptr);
Patrick Venture29af1e32019-08-05 13:42:28 -0700471 auto verifier = reinterpret_cast<SystemdWithStatusFile*>(
Patrick Venture984d94d2019-08-05 12:23:59 -0700472 h[0].actions->verification.get());
473 EXPECT_THAT(verifier->getMode(), "replace-nope");
Patrick Venture0caec992019-08-05 09:58:27 -0700474 EXPECT_FALSE(h[0].actions->update == nullptr);
475 auto updater =
476 reinterpret_cast<SystemdUpdateMechanism*>(h[0].actions->update.get());
477 EXPECT_THAT(updater->getMode(), "replace-fake");
Patrick Venture298930a2019-07-03 11:44:52 -0700478}
479
Patrick Venturec2baac92019-08-05 13:30:38 -0700480TEST(FirmwareJsonTest, VerifyValidUpdateWithFilePath)
481{
482 auto j2 = R"(
483 [{
484 "blob" : "/flash/image",
485 "handler" : {
486 "type" : "file",
487 "path" : "/run/initramfs/bmc-image"
488 },
489 "actions" : {
490 "preparation" : {
491 "type" : "systemd",
492 "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
493 },
494 "verification" : {
495 "type" : "fileSystemdVerify",
496 "unit" : "phosphor-ipmi-flash-bmc-verify.target",
497 "path" : "/tmp/bmc.verify",
498 "mode" : "replace-nope"
499 },
500 "update" : {
501 "type" : "fileSystemdUpdate",
502 "mode" : "replace-fake",
503 "unit" : "phosphor-ipmi-flash-bmc-update.target",
504 "path" : "/tmp/update.verify"
505 }
506 }
507 }]
508 )"_json;
509
510 auto h = buildHandlerFromJson(j2);
511 EXPECT_EQ(h[0].blobId, "/flash/image");
512 EXPECT_FALSE(h[0].handler == nullptr);
513 EXPECT_FALSE(h[0].actions == nullptr);
514 EXPECT_FALSE(h[0].actions->preparation == nullptr);
515 EXPECT_FALSE(h[0].actions->verification == nullptr);
Patrick Venture29af1e32019-08-05 13:42:28 -0700516 auto verifier = reinterpret_cast<SystemdWithStatusFile*>(
Patrick Venturec2baac92019-08-05 13:30:38 -0700517 h[0].actions->verification.get());
518 EXPECT_THAT(verifier->getMode(), "replace-nope");
519 EXPECT_FALSE(h[0].actions->update == nullptr);
520 auto updater =
Patrick Venture29af1e32019-08-05 13:42:28 -0700521 reinterpret_cast<SystemdWithStatusFile*>(h[0].actions->update.get());
Patrick Venturec2baac92019-08-05 13:30:38 -0700522 EXPECT_THAT(updater->getMode(), "replace-fake");
523}
524
Patrick Venture298930a2019-07-03 11:44:52 -0700525} // namespace
526} // namespace ipmi_flash