1 #include <blobs-ipmid/manager.hpp>
2 #include <blobs-ipmid/test/blob_mock.hpp>
3 #include <vector>
4 
5 #include <gtest/gtest.h>
6 
7 namespace blobs
8 {
9 
10 using ::testing::_;
11 using ::testing::Return;
12 
13 TEST(ManagerReadTest, ReadNoSessionReturnsFalse)
14 {
15     // Calling Read on a session that doesn't exist should return false.
16 
17     BlobManager mgr;
18     uint16_t sess = 1;
19     uint32_t ofs = 0x54;
20     uint32_t requested = 0x100;
21 
22     std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
23     EXPECT_EQ(0, result.size());
24 }
25 
26 TEST(ManagerReadTest, ReadFromWriteOnlyFails)
27 {
28     // The session manager will not route a Read call to a blob if the session
29     // was opened as write-only.
30 
31     BlobManager mgr;
32     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
33     auto m1ptr = m1.get();
34     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
35 
36     uint16_t sess = 1;
37     uint32_t ofs = 0x54;
38     uint32_t requested = 0x100;
39     uint16_t flags = OpenFlags::write;
40     std::string path = "/asdf/asdf";
41 
42     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
43     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
44     EXPECT_TRUE(mgr.open(flags, path, &sess));
45 
46     std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
47     EXPECT_EQ(0, result.size());
48 }
49 
50 TEST(ManagerReadTest, ReadFromHandlerReturnsData)
51 {
52     // There is no logic in this as it's just as a pass-thru command, however
53     // we want to verify this behavior doesn't change.
54 
55     BlobManager mgr;
56     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
57     auto m1ptr = m1.get();
58     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
59 
60     uint16_t sess = 1;
61     uint32_t ofs = 0x54;
62     uint32_t requested = 0x100;
63     uint16_t flags = OpenFlags::read;
64     std::string path = "/asdf/asdf";
65     std::vector<uint8_t> data = {0x12, 0x14, 0x15, 0x16};
66 
67     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
68     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
69     EXPECT_TRUE(mgr.open(flags, path, &sess));
70 
71     EXPECT_CALL(*m1ptr, read(sess, ofs, requested)).WillOnce(Return(data));
72 
73     std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
74     EXPECT_EQ(data.size(), result.size());
75     EXPECT_EQ(result, data);
76 }
77 } // namespace blobs
78