blob: 35f48d6c3c2167b4cad876da7ff51ffc9aa2da42 [file] [log] [blame]
Patrick Ventureffcc5502018-11-16 12:32:38 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
4
Patrick Venture4eb55952018-11-16 15:36:24 -08005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Ventureffcc5502018-11-16 12:32:38 -08006#include <vector>
7
8#include <gtest/gtest.h>
9
10namespace blobs
11{
12using ::testing::Return;
13
14TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage)
15{
16 /* Verify the flash image returns failure on this command. It's a fairly
17 * artificial test.
18 */
19 ImageHandlerMock imageMock1, imageMock2;
20 std::vector<HandlerPack> blobs = {
21 {FirmwareBlobHandler::hashBlobID, &imageMock1},
22 {"asdf", &imageMock2},
23 };
24
25 std::vector<DataHandlerPack> data = {
26 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
27 };
28
Patrick Venture4eb55952018-11-16 15:36:24 -080029 sdbusplus::SdBusMock sdbus_mock;
30 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
31
32 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
33 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080034
35 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
36
37 EXPECT_TRUE(handler->open(
38 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
39
40 EXPECT_FALSE(handler->commit(0, {}));
41}
42
43TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
44{
45 /* Verify the hash file returns failure on this command. It's a fairly
46 * artificial test.
47 */
48 ImageHandlerMock imageMock1, imageMock2;
49 std::vector<HandlerPack> blobs = {
50 {FirmwareBlobHandler::hashBlobID, &imageMock1},
51 {"asdf", &imageMock2},
52 };
53
54 std::vector<DataHandlerPack> data = {
55 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
56 };
57
Patrick Venture4eb55952018-11-16 15:36:24 -080058 sdbusplus::SdBusMock sdbus_mock;
59 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
60
61 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
62 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080063
64 EXPECT_CALL(imageMock1, open(FirmwareBlobHandler::hashBlobID))
65 .WillOnce(Return(true));
66
67 EXPECT_TRUE(handler->open(
68 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
69 FirmwareBlobHandler::hashBlobID));
70
71 EXPECT_FALSE(handler->commit(0, {}));
72}
73
74TEST(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob)
75{
76 /* Verify the verify blob lets you call this command, and it returns
77 * success.
78 */
79 ImageHandlerMock imageMock1, imageMock2;
80 std::vector<HandlerPack> blobs = {
81 {FirmwareBlobHandler::hashBlobID, &imageMock1},
82 {"asdf", &imageMock2},
83 };
84
85 std::vector<DataHandlerPack> data = {
86 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
87 };
88
Patrick Venture4eb55952018-11-16 15:36:24 -080089 sdbusplus::SdBusMock sdbus_mock;
90 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
91
92 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
93 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080094
95 EXPECT_TRUE(
96 handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID));
97
98 EXPECT_TRUE(handler->commit(0, {}));
99}
100
101TEST(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnce)
102{
103 /* Verify you cannot call the commit() command once verification is started.
104 */
105 ImageHandlerMock imageMock1, imageMock2;
106 std::vector<HandlerPack> blobs = {
107 {FirmwareBlobHandler::hashBlobID, &imageMock1},
108 {"asdf", &imageMock2},
109 };
110
111 std::vector<DataHandlerPack> data = {
112 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
113 };
114
Patrick Venture4eb55952018-11-16 15:36:24 -0800115 sdbusplus::SdBusMock sdbus_mock;
116 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
117
118 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
119 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -0800120
121 EXPECT_TRUE(
122 handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID));
123
124 EXPECT_TRUE(handler->commit(0, {}));
125 EXPECT_FALSE(handler->commit(0, {}));
126}
127
128} // namespace blobs