blob: 49d40a0a137d78685386de3caca14d7dc067d842 [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
Patrick Venture72388d72018-11-07 15:06:36 -08005#include <vector>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10namespace blobs
11{
12using ::testing::Return;
13
14TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
15{
16 /* The client passes write set, and a transport that's supported, and a
17 * firmware image blob_id that's supported. */
18
19 ImageHandlerMock imageMock;
20
21 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -080022 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080023 {"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 = {
Patrick Venture18235e62018-11-08 10:21:09 -080046 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080047 {"asdf", &imageMock},
48 };
49 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080050 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080051 };
52
53 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
54
55 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
56
57 EXPECT_FALSE(handler->open(
Patrick Venture9158dcf2018-11-08 09:44:28 -080058 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi,
Patrick Venture72388d72018-11-07 15:06:36 -080059 "asdf"));
60}
61
62TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
63{
64 /* The client must set the file write bit. */
65
66 ImageHandlerMock imageMock;
67
68 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -080069 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080070 {"asdf", &imageMock},
71 };
72 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080073 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080074 };
75
76 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
77
Patrick Venture9158dcf2018-11-08 09:44:28 -080078 EXPECT_FALSE(handler->open(
79 0, FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -080080}
81
82TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
83{
84 /* The client sends a request with a transport mechanism not supported. */
85
86 ImageHandlerMock imageMock;
87
88 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -080089 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080090 {"asdf", &imageMock},
91 };
92 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -080093 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080094 };
95
96 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
97
98 EXPECT_FALSE(handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::lpc,
99 "asdf"));
100}
101
102TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
103{
104 /* The client sends a request with an invalid image blob_id. */
105
106 ImageHandlerMock imageMock;
107
108 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800109 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800110 {"asdf", &imageMock},
111 };
112 std::vector<DataHandlerPack> data = {
Patrick Venture9158dcf2018-11-08 09:44:28 -0800113 {FirmwareBlobHandler::FirmwareUpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800114 };
115
116 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
117
118 EXPECT_FALSE(handler->open(
Patrick Venture9158dcf2018-11-08 09:44:28 -0800119 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::ipmi,
Patrick Venture72388d72018-11-07 15:06:36 -0800120 "bcdf"));
121}
122
123/* TODO: The client sends a request during verification. */
124/* TODO: The client sends a second request to open when there is already an open
125 * file.
126 */
127/* TODO: The client sends a request to open active image. */
128/* TODO: The client sends a request to open active hash. */
129
130} // namespace blobs