blob: d77bafca3ba170ebf12235e60badbd696b4695bf [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 = {
26 {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
27 };
28
29 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
30
31 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
32
33 EXPECT_TRUE(handler->open(
34 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
35 "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 = {
49 {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
50 };
51
52 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
53
54 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
55
56 EXPECT_FALSE(handler->open(
57 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
58 "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 = {
71 {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
72 };
73
74 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
75
76 EXPECT_FALSE(
77 handler->open(0, FirmwareBlobHandler::FirmwareUpdateFlags::bt, "asdf"));
78}
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 = {
90 {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
91 };
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 = {
109 {FirmwareBlobHandler::FirmwareUpdateFlags::bt, nullptr},
110 };
111
112 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
113
114 EXPECT_FALSE(handler->open(
115 0, OpenFlags::write | FirmwareBlobHandler::FirmwareUpdateFlags::bt,
116 "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