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