blob: bc6ece2b44c489882eb41d7d4851b981d274ba89 [file] [log] [blame]
Patrick Venture72388d72018-11-07 15:06:36 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
4
5#include <memory>
6#include <vector>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace blobs
12{
13using ::testing::Return;
14
15TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
16{
17 /* The client passes write set, and a transport that's supported, and a
18 * firmware image blob_id that's supported. */
19
20 ImageHandlerMock imageMock;
21
22 std::vector<HandlerPack> blobs = {
23 {"asdf", &imageMock},
24 };
25 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080026 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080027 };
28
29 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
30
31 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
32
33 EXPECT_TRUE(handler->open(
Patrick Venture9158dcf2018-11-08 09:44:28 -080034 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi,
Patrick Venture72388d72018-11-07 15:06:36 -080035 "asdf"));
36}
37
38TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
39{
40 /* The image handler for a specific type of image is allowed to return
41 * failure on open. let's simulate that. */
42
43 ImageHandlerMock imageMock;
44
45 std::vector<HandlerPack> blobs = {
46 {"asdf", &imageMock},
47 };
48 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080049 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080050 };
51
52 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
53
54 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
55
56 EXPECT_FALSE(handler->open(
Patrick Venture9158dcf2018-11-08 09:44:28 -080057 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi,
Patrick Venture72388d72018-11-07 15:06:36 -080058 "asdf"));
59}
60
61TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
62{
63 /* The client must set the file write bit. */
64
65 ImageHandlerMock imageMock;
66
67 std::vector<HandlerPack> blobs = {
68 {"asdf", &imageMock},
69 };
70 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080071 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080072 };
73
74 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
75
Patrick Venture9158dcf2018-11-08 09:44:28 -080076 EXPECT_FALSE(handler->open(
77 0, FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -080078}
79
80TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
81{
82 /* The client sends a request with a transport mechanism not supported. */
83
84 ImageHandlerMock imageMock;
85
86 std::vector<HandlerPack> blobs = {
87 {"asdf", &imageMock},
88 };
89 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080090 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080091 };
92
93 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
94
95 EXPECT_FALSE(handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::lpc,
96 "asdf"));
97}
98
99TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
100{
101 /* The client sends a request with an invalid image blob_id. */
102
103 ImageHandlerMock imageMock;
104
105 std::vector<HandlerPack> blobs = {
106 {"asdf", &imageMock},
107 };
108 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -0800109 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800110 };
111
112 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
113
114 EXPECT_FALSE(handler->open(
Patrick Venture9158dcf2018-11-08 09:44:28 -0800115 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi,
Patrick Venture72388d72018-11-07 15:06:36 -0800116 "bcdf"));
117}
118
119/* TODO: The client sends a request during verification. */
120/* TODO: The client sends a second request to open when there is already an open
121 * file.
122 */
123/* TODO: The client sends a request to open active image. */
124/* TODO: The client sends a request to open active hash. */
125
126} // namespace blobs