1 #include "create_action_map.hpp"
2 #include "data_mock.hpp"
3 #include "firmware_handler.hpp"
4 #include "flags.hpp"
5 #include "image_mock.hpp"
6 #include "util.hpp"
7
8 #include <memory>
9 #include <vector>
10
11 #include <gtest/gtest.h>
12
13 namespace ipmi_flash
14 {
15 namespace
16 {
17
18 using ::testing::Return;
19 using ::testing::StrEq;
20
21 class FirmwareHandlerCommitTest : public ::testing::Test
22 {
23 protected:
24 ImageHandlerMock *imageMock1, *imageMock2;
25 std::vector<HandlerPack> blobs;
26 std::vector<DataHandlerPack> data;
27
SetUp()28 void SetUp() override
29 {
30 std::unique_ptr<ImageHandlerInterface> image =
31 std::make_unique<ImageHandlerMock>();
32 imageMock1 = reinterpret_cast<ImageHandlerMock*>(image.get());
33 blobs.emplace_back(hashBlobId, std::move(image));
34
35 image = std::make_unique<ImageHandlerMock>();
36 imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
37 blobs.emplace_back("asdf", std::move(image));
38
39 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
40 }
41 };
42
TEST_F(FirmwareHandlerCommitTest,VerifyCannotCommitOnFlashImage)43 TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage)
44 {
45 /* Verify the flash image returns failure on this command. It's a fairly
46 * artificial test.
47 */
48 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
49 std::move(blobs), std::move(data), CreateActionMap("asdf"));
50
51 EXPECT_CALL(*imageMock2, open("asdf", std::ios::out))
52 .WillOnce(Return(true));
53
54 EXPECT_TRUE(
55 handler->open(0,
56 static_cast<std::uint16_t>(blobs::OpenFlags::write) |
57 FirmwareFlags::UpdateFlags::ipmi,
58 "asdf"));
59
60 EXPECT_FALSE(handler->commit(0, {}));
61 }
62
TEST_F(FirmwareHandlerCommitTest,VerifyCannotCommitOnHashFile)63 TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile)
64 {
65 /* Verify the hash file returns failure on this command. It's a fairly
66 * artificial test.
67 */
68 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
69 std::move(blobs), std::move(data), CreateActionMap("asdf"));
70
71 EXPECT_CALL(*imageMock1, open(StrEq(hashBlobId), std::ios::out))
72 .WillOnce(Return(true));
73
74 EXPECT_TRUE(
75 handler->open(0,
76 static_cast<std::uint16_t>(blobs::OpenFlags::write) |
77 FirmwareFlags::UpdateFlags::ipmi,
78 hashBlobId));
79
80 EXPECT_FALSE(handler->commit(0, {}));
81 }
82
83 } // namespace
84 } // namespace ipmi_flash
85