blob: 9dbf909a27b451fb520069d084314fe2bc338be4 [file] [log] [blame]
Patrick Venturecc7d1602018-11-15 13:58:33 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
4
5#include <vector>
6
7#include <gtest/gtest.h>
8
9namespace blobs
10{
11using ::testing::Eq;
12using ::testing::Return;
13
14TEST(FirmwareSessionStateTest, DataTypeIpmiNoMetadata)
15{
16 /* Verifying running stat if the type of data session is IPMI returns no
17 * metadata.
18 */
19 ImageHandlerMock imageMock1, imageMock2;
20 std::vector<HandlerPack> blobs = {
21 {FirmwareBlobHandler::hashBlobID, &imageMock1},
22 {"asdf", &imageMock2},
23 };
24
25 std::vector<DataHandlerPack> data = {
26 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
27 };
28
29 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
30
31 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
32
33 EXPECT_TRUE(handler->open(
34 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
35
36 int size = 512;
37 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
38
39 struct BlobMeta meta;
40 EXPECT_TRUE(handler->stat(0, &meta));
41 EXPECT_EQ(meta.blobState,
42 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi);
43 EXPECT_EQ(meta.size, size);
44 EXPECT_EQ(meta.metadata.size(), 0);
45}
46
47TEST(FirmwareSessionStateTest, DataTypeP2AReturnsMetadata)
48{
49 /* Really any type that isn't IPMI can return metadata, but we only expect
50 * P2A to for now. Later, LPC may have reason to provide data, and can by
51 * simply implementing read().
52 */
53 ImageHandlerMock imageMock1, imageMock2;
54 std::vector<HandlerPack> blobs = {
55 {FirmwareBlobHandler::hashBlobID, &imageMock1},
56 {"asdf", &imageMock2},
57 };
58
59 DataHandlerMock dataMock;
60
61 std::vector<DataHandlerPack> data = {
62 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
63 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
64 };
65
66 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
67
68 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
69 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
70
71 EXPECT_TRUE(handler->open(
72 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
73
74 int size = 512;
75 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
76 std::vector<std::uint8_t> mBytes = {0x01, 0x02};
77 EXPECT_CALL(dataMock, read()).WillOnce(Return(mBytes));
78
79 struct BlobMeta meta;
80 EXPECT_TRUE(handler->stat(0, &meta));
81 EXPECT_EQ(meta.blobState,
82 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc);
83 EXPECT_EQ(meta.size, size);
84 EXPECT_EQ(meta.metadata.size(), mBytes.size());
85 EXPECT_EQ(meta.metadata[0], mBytes[0]);
86 EXPECT_EQ(meta.metadata[1], mBytes[1]);
87}
88
89} // namespace blobs