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