1 #include "handler_unittest.hpp"
2 
3 #include <blobs-ipmid/blobs.hpp>
4 
5 #include <cstdint>
6 #include <vector>
7 
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10 
11 using ::testing::IsEmpty;
12 
13 namespace blobs
14 {
15 
16 class SmbiosBlobHandlerReadWriteTest : public SmbiosBlobHandlerTest
17 {};
18 
TEST_F(SmbiosBlobHandlerReadWriteTest,InvalidSessionWriteIsRejected)19 TEST_F(SmbiosBlobHandlerReadWriteTest, InvalidSessionWriteIsRejected)
20 {
21     // Verify the handler checks for a valid session.
22 
23     std::vector<uint8_t> data = {0x1, 0x2};
24     EXPECT_FALSE(handler.write(session, 0, data));
25 }
26 
TEST_F(SmbiosBlobHandlerReadWriteTest,NoWriteFlagRejected)27 TEST_F(SmbiosBlobHandlerReadWriteTest, NoWriteFlagRejected)
28 {
29     // Verify the handler checks the write flag;
30 
31     EXPECT_TRUE(handler.open(session, 0, expectedBlobId));
32 
33     std::vector<uint8_t> data = {0x1, 0x2};
34     EXPECT_FALSE(handler.write(session, 0, data));
35 }
36 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingTooMuchByOneByteFails)37 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingTooMuchByOneByteFails)
38 {
39     int bytes = handlerMaxBufferSize + 1;
40     std::vector<uint8_t> data(bytes, 0x11);
41 
42     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
43     EXPECT_FALSE(handler.write(session, 0, data));
44 }
45 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingTooMuchByOffsetOfOne)46 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingTooMuchByOffsetOfOne)
47 {
48     std::vector<uint8_t> data(handlerMaxBufferSize, 0x11);
49 
50     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
51     EXPECT_FALSE(handler.write(session, 1, data));
52 }
53 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingOneByteBeyondEndFromOffsetFails)54 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingOneByteBeyondEndFromOffsetFails)
55 {
56     std::vector<uint8_t> data = {0x01, 0x02};
57 
58     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
59     EXPECT_FALSE(handler.write(session, handlerMaxBufferSize - 1, data));
60 }
61 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingOneByteAtOffsetBeyondEndFails)62 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingOneByteAtOffsetBeyondEndFails)
63 {
64     std::vector<uint8_t> data = {0x01};
65 
66     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
67     EXPECT_FALSE(handler.write(session, handlerMaxBufferSize, data));
68 }
69 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingFullBufferAtOffsetZeroSucceeds)70 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingFullBufferAtOffsetZeroSucceeds)
71 {
72     std::vector<uint8_t> data(handlerMaxBufferSize, 0x01);
73 
74     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
75     EXPECT_TRUE(handler.write(session, 0, data));
76 }
77 
TEST_F(SmbiosBlobHandlerReadWriteTest,WritingOneByteToTheLastOffsetSucceeds)78 TEST_F(SmbiosBlobHandlerReadWriteTest, WritingOneByteToTheLastOffsetSucceeds)
79 {
80     std::vector<uint8_t> data = {0x01};
81 
82     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
83     EXPECT_TRUE(handler.write(session, handlerMaxBufferSize - 1, data));
84 }
85 
TEST_F(SmbiosBlobHandlerReadWriteTest,ReadAlwaysReturnsEmpty)86 TEST_F(SmbiosBlobHandlerReadWriteTest, ReadAlwaysReturnsEmpty)
87 {
88     const uint32_t testOffset = 0;
89     const std::vector<uint8_t> testData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
90 
91     EXPECT_TRUE(handler.open(session, blobs::OpenFlags::write, expectedBlobId));
92     EXPECT_TRUE(handler.write(session, testOffset, testData));
93 
94     EXPECT_THAT(handler.read(session, testOffset, testData.size()), IsEmpty());
95 
96     for (size_t i = 0; i < testData.size(); ++i)
97     {
98         EXPECT_THAT(handler.read(session, i, 1), IsEmpty());
99     }
100 }
101 
102 } // namespace blobs
103