blob: 425e7b92176da6f80a5a0b753c96b985a58a2b95 [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
Patrick Venturebe198702019-05-15 09:46:02 -0700106TEST(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800107{
Patrick Venturebe198702019-05-15 09:46:02 -0700108 /* Verify you cannot call the commit() command once verification is
109 * started, after which it will just return true.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800110 */
111 ImageHandlerMock imageMock1, imageMock2;
112 std::vector<HandlerPack> blobs = {
113 {FirmwareBlobHandler::hashBlobID, &imageMock1},
114 {"asdf", &imageMock2},
115 };
116
117 std::vector<DataHandlerPack> data = {
118 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
119 };
120
Patrick Venturecabc1172018-11-16 16:14:26 -0800121 StrictMock<sdbusplus::SdBusMock> sdbus_mock;
Patrick Venture4eb55952018-11-16 15:36:24 -0800122 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
123
124 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
125 std::move(bus_mock), blobs, data);
Patrick Ventureffcc5502018-11-16 12:32:38 -0800126
127 EXPECT_TRUE(
128 handler->open(0, OpenFlags::write, FirmwareBlobHandler::verifyBlobID));
129
Patrick Venturecabc1172018-11-16 16:14:26 -0800130 /* Note: I used StrictMock<> here to just catch all the calls. However, the
131 * unit-tests pass if we don't use StrictMock and ignore the calls.
132 */
133 EXPECT_CALL(sdbus_mock,
134 sd_bus_message_new_method_call(
135 IsNull(), NotNull(), StrEq("org.freedesktop.systemd1"),
136 StrEq("/org/freedesktop/systemd1"),
137 StrEq("org.freedesktop.systemd1.Manager"),
138 StrEq("StartUnit")))
139 .WillOnce(Return(0));
140 EXPECT_CALL(sdbus_mock,
141 sd_bus_message_append_basic(IsNull(), 's', NotNull()))
142 .Times(2)
143 .WillRepeatedly(Return(0));
144 EXPECT_CALL(sdbus_mock, sd_bus_call(_, _, _, _, _)).WillOnce(Return(0));
145
Patrick Ventureffcc5502018-11-16 12:32:38 -0800146 EXPECT_TRUE(handler->commit(0, {}));
Patrick Venturebe198702019-05-15 09:46:02 -0700147 EXPECT_TRUE(handler->commit(0, {}));
Patrick Ventureffcc5502018-11-16 12:32:38 -0800148}
149
150} // namespace blobs