xref: /openbmc/phosphor-ipmi-blobs/test/manager_write_unittest.cpp (revision cd8dab491d3f78124be800252186a32a90c884b8)
1 #include "blob_mock.hpp"
2 #include "manager.hpp"
3 
4 #include <gtest/gtest.h>
5 
6 using ::testing::_;
7 using ::testing::Return;
8 
9 namespace blobs
10 {
11 
TEST(ManagerWriteTest,WriteNoSessionReturnsFalse)12 TEST(ManagerWriteTest, WriteNoSessionReturnsFalse)
13 {
14     // Calling Write on a session that doesn't exist should return false.
15 
16     BlobManager mgr;
17     uint16_t sess = 1;
18     uint32_t ofs = 0x54;
19     std::vector<uint8_t> data = {0x11, 0x22};
20 
21     EXPECT_FALSE(mgr.write(sess, ofs, data));
22 }
23 
TEST(ManagerWriteTest,WriteSessionFoundButHandlerReturnsFalse)24 TEST(ManagerWriteTest, WriteSessionFoundButHandlerReturnsFalse)
25 {
26     // The handler was found but it returned failure.
27 
28     BlobManager mgr;
29     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
30     auto m1ptr = m1.get();
31     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
32 
33     uint16_t flags = OpenFlags::write, sess;
34     std::string path = "/asdf/asdf";
35     uint32_t ofs = 0x54;
36     std::vector<uint8_t> data = {0x11, 0x22};
37 
38     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
39     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
40     EXPECT_TRUE(mgr.open(flags, path, &sess));
41 
42     EXPECT_CALL(*m1ptr, write(sess, ofs, data)).WillOnce(Return(false));
43 
44     EXPECT_FALSE(mgr.write(sess, ofs, data));
45 }
46 
TEST(ManagerWriteTest,WriteFailsBecauseFileOpenedReadOnly)47 TEST(ManagerWriteTest, WriteFailsBecauseFileOpenedReadOnly)
48 {
49     // The manager will not route a write call to a file opened read-only.
50 
51     BlobManager mgr;
52     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
53     auto m1ptr = m1.get();
54     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
55 
56     uint16_t flags = OpenFlags::read, sess;
57     std::string path = "/asdf/asdf";
58     uint32_t ofs = 0x54;
59     std::vector<uint8_t> data = {0x11, 0x22};
60 
61     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
62     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
63     EXPECT_TRUE(mgr.open(flags, path, &sess));
64 
65     EXPECT_FALSE(mgr.write(sess, ofs, data));
66 }
67 
TEST(ManagerWriteTest,WriteSessionFoundAndHandlerReturnsSuccess)68 TEST(ManagerWriteTest, WriteSessionFoundAndHandlerReturnsSuccess)
69 {
70     // The handler was found and returned success.
71 
72     BlobManager mgr;
73     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
74     auto m1ptr = m1.get();
75     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
76 
77     uint16_t flags = OpenFlags::write, sess;
78     std::string path = "/asdf/asdf";
79     uint32_t ofs = 0x54;
80     std::vector<uint8_t> data = {0x11, 0x22};
81 
82     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
83     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
84     EXPECT_TRUE(mgr.open(flags, path, &sess));
85 
86     EXPECT_CALL(*m1ptr, write(sess, ofs, data)).WillOnce(Return(true));
87 
88     EXPECT_TRUE(mgr.write(sess, ofs, data));
89 }
90 } // namespace blobs
91