blob: f8cc727455fceecafaf5e7770d1b18c3d750e3ee [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"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venturecc7d1602018-11-15 13:58:33 -08006
7#include <vector>
8
9#include <gtest/gtest.h>
10
11namespace blobs
12{
13using ::testing::Eq;
14using ::testing::Return;
15
Patrick Venture3a036312019-05-17 18:56:41 -070016class FirmwareSessionStateTest : public ::testing::Test
17{
18 protected:
19 ImageHandlerMock imageMock1, imageMock2;
20 std::vector<HandlerPack> blobs;
21
22 void SetUp() override
23 {
24 blobs = {
25 {hashBlobId, &imageMock1},
26 {"asdf", &imageMock2},
27 };
28 }
29};
30
31TEST_F(FirmwareSessionStateTest, DataTypeIpmiNoMetadata)
Patrick Venturecc7d1602018-11-15 13:58:33 -080032{
33 /* Verifying running stat if the type of data session is IPMI returns no
34 * metadata.
35 */
Patrick Venturecc7d1602018-11-15 13:58:33 -080036 std::vector<DataHandlerPack> data = {
37 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
38 };
39
Patrick Venture4eb55952018-11-16 15:36:24 -080040 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070041 blobs, data, CreateVerifyMock());
Patrick Venturecc7d1602018-11-15 13:58:33 -080042
43 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
44
45 EXPECT_TRUE(handler->open(
46 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
47
48 int size = 512;
49 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
50
51 struct BlobMeta meta;
52 EXPECT_TRUE(handler->stat(0, &meta));
53 EXPECT_EQ(meta.blobState,
54 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi);
55 EXPECT_EQ(meta.size, size);
56 EXPECT_EQ(meta.metadata.size(), 0);
57}
58
Patrick Venture3a036312019-05-17 18:56:41 -070059TEST_F(FirmwareSessionStateTest, DataTypeP2AReturnsMetadata)
Patrick Venturecc7d1602018-11-15 13:58:33 -080060{
61 /* Really any type that isn't IPMI can return metadata, but we only expect
62 * P2A to for now. Later, LPC may have reason to provide data, and can by
63 * simply implementing read().
64 */
Patrick Venturecc7d1602018-11-15 13:58:33 -080065 DataHandlerMock dataMock;
66
67 std::vector<DataHandlerPack> data = {
68 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
69 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
70 };
71
Patrick Venture4eb55952018-11-16 15:36:24 -080072 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070073 blobs, data, CreateVerifyMock());
Patrick Venturecc7d1602018-11-15 13:58:33 -080074
75 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
76 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
77
78 EXPECT_TRUE(handler->open(
79 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
80
81 int size = 512;
82 EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
83 std::vector<std::uint8_t> mBytes = {0x01, 0x02};
Patrick Venture74304642019-01-17 09:31:04 -080084 EXPECT_CALL(dataMock, readMeta()).WillOnce(Return(mBytes));
Patrick Venturecc7d1602018-11-15 13:58:33 -080085
86 struct BlobMeta meta;
87 EXPECT_TRUE(handler->stat(0, &meta));
88 EXPECT_EQ(meta.blobState,
89 OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc);
90 EXPECT_EQ(meta.size, size);
91 EXPECT_EQ(meta.metadata.size(), mBytes.size());
92 EXPECT_EQ(meta.metadata[0], mBytes[0]);
93 EXPECT_EQ(meta.metadata[1], mBytes[1]);
94}
95
96} // namespace blobs