blob: 7dca2b4d24cfeb484e756c6cc1b9da64be7cd57d [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{
Patrick Venturecabc1172018-11-16 16:14:26 -080012using ::testing::_;
13using ::testing::IsNull;
14using ::testing::NotNull;
Patrick Ventureffcc5502018-11-16 12:32:38 -080015using ::testing::Return;
Patrick Venturecabc1172018-11-16 16:14:26 -080016using ::testing::StrEq;
17using ::testing::StrictMock;
Patrick Ventureffcc5502018-11-16 12:32:38 -080018
19TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage)
20{
21 /* Verify the flash image returns failure on this command. It's a fairly
22 * artificial test.
23 */
24 ImageHandlerMock imageMock1, imageMock2;
25 std::vector<HandlerPack> blobs = {
26 {FirmwareBlobHandler::hashBlobID, &imageMock1},
27 {"asdf", &imageMock2},
28 };
29
30 std::vector<DataHandlerPack> data = {
31 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
32 };
33
Patrick Venture4eb55952018-11-16 15:36:24 -080034 sdbusplus::SdBusMock sdbus_mock;
35 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
36
37 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
38 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080039
40 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
41
42 EXPECT_TRUE(handler->open(
43 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
44
45 EXPECT_FALSE(handler->commit(0, {}));
46}
47
48TEST(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
49{
50 /* Verify the hash file returns failure on this command. It's a fairly
51 * artificial test.
52 */
53 ImageHandlerMock imageMock1, imageMock2;
54 std::vector<HandlerPack> blobs = {
55 {FirmwareBlobHandler::hashBlobID, &imageMock1},
56 {"asdf", &imageMock2},
57 };
58
59 std::vector<DataHandlerPack> data = {
60 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
61 };
62
Patrick Venture4eb55952018-11-16 15:36:24 -080063 sdbusplus::SdBusMock sdbus_mock;
64 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
65
66 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
67 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080068
69 EXPECT_CALL(imageMock1, open(FirmwareBlobHandler::hashBlobID))
70 .WillOnce(Return(true));
71
72 EXPECT_TRUE(handler->open(
73 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
74 FirmwareBlobHandler::hashBlobID));
75
76 EXPECT_FALSE(handler->commit(0, {}));
77}
78
79TEST(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob)
80{
81 /* Verify the verify blob lets you call this command, and it returns
82 * success.
83 */
84 ImageHandlerMock imageMock1, imageMock2;
85 std::vector<HandlerPack> blobs = {
86 {FirmwareBlobHandler::hashBlobID, &imageMock1},
87 {"asdf", &imageMock2},
88 };
89
90 std::vector<DataHandlerPack> data = {
91 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
92 };
93
Patrick Venture4eb55952018-11-16 15:36:24 -080094 sdbusplus::SdBusMock sdbus_mock;
95 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
96
97 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
98 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -080099
100 EXPECT_TRUE(
101 handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID));
102
103 EXPECT_TRUE(handler->commit(0, {}));
104}
105
106TEST(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnce)
107{
108 /* Verify you cannot call the commit() command once verification is started.
109 */
110 ImageHandlerMock imageMock1, imageMock2;
111 std::vector<HandlerPack> blobs = {
112 {FirmwareBlobHandler::hashBlobID, &imageMock1},
113 {"asdf", &imageMock2},
114 };
115
116 std::vector<DataHandlerPack> data = {
117 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
118 };
119
Patrick Venturecabc1172018-11-16 16:14:26 -0800120 StrictMock<sdbusplus::SdBusMock> sdbus_mock;
Patrick Venture4eb55952018-11-16 15:36:24 -0800121 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
122
123 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
124 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -0800125
126 EXPECT_TRUE(
127 handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID));
128
Patrick Venturecabc1172018-11-16 16:14:26 -0800129 /* Note: I used StrictMock<> here to just catch all the calls. However, the
130 * unit-tests pass if we don't use StrictMock and ignore the calls.
131 */
132 EXPECT_CALL(sdbus_mock,
133 sd_bus_message_new_method_call(
134 IsNull(), NotNull(), StrEq("org.freedesktop.systemd1"),
135 StrEq("/org/freedesktop/systemd1"),
136 StrEq("org.freedesktop.systemd1.Manager"),
137 StrEq("StartUnit")))
138 .WillOnce(Return(0));
139 EXPECT_CALL(sdbus_mock,
140 sd_bus_message_append_basic(IsNull(), 's', NotNull()))
141 .Times(2)
142 .WillRepeatedly(Return(0));
143 EXPECT_CALL(sdbus_mock, sd_bus_call(_, _, _, _, _)).WillOnce(Return(0));
144
Patrick Ventureffcc5502018-11-16 12:32:38 -0800145 EXPECT_TRUE(handler->commit(0, {}));
146 EXPECT_FALSE(handler->commit(0, {}));
147}
148
149} // namespace blobs