xref: /openbmc/phosphor-ipmi-blobs/test/manager_open_unittest.cpp (revision cd8dab491d3f78124be800252186a32a90c884b8)
1*cd8dab49SPatrick Venture #include "blob_mock.hpp"
2*cd8dab49SPatrick Venture #include "manager.hpp"
3*cd8dab49SPatrick Venture 
4ef3aeadcSPatrick Venture #include <string>
5ef3aeadcSPatrick Venture 
6ef3aeadcSPatrick Venture #include <gtest/gtest.h>
7ef3aeadcSPatrick Venture 
8ef3aeadcSPatrick Venture namespace blobs
9ef3aeadcSPatrick Venture {
10ef3aeadcSPatrick Venture 
11ef3aeadcSPatrick Venture using ::testing::_;
12ef3aeadcSPatrick Venture using ::testing::Return;
13ef3aeadcSPatrick Venture 
TEST(ManagerOpenTest,OpenButNoHandler)14ef3aeadcSPatrick Venture TEST(ManagerOpenTest, OpenButNoHandler)
15ef3aeadcSPatrick Venture {
16ef3aeadcSPatrick Venture     // No handler claims to be able to open the file.
17ef3aeadcSPatrick Venture 
18ef3aeadcSPatrick Venture     BlobManager mgr;
19ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
20ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
21ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
22ef3aeadcSPatrick Venture 
23ef3aeadcSPatrick Venture     uint16_t flags = OpenFlags::read, sess;
24ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
25ef3aeadcSPatrick Venture 
26ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(false));
27ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.open(flags, path, &sess));
28ef3aeadcSPatrick Venture }
29ef3aeadcSPatrick Venture 
TEST(ManagerOpenTest,OpenButHandlerFailsOpen)30ef3aeadcSPatrick Venture TEST(ManagerOpenTest, OpenButHandlerFailsOpen)
31ef3aeadcSPatrick Venture {
32ef3aeadcSPatrick Venture     // The handler is found but Open fails.
33ef3aeadcSPatrick Venture 
34ef3aeadcSPatrick Venture     BlobManager mgr;
35ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
36ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
37ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
38ef3aeadcSPatrick Venture 
39ef3aeadcSPatrick Venture     uint16_t flags = OpenFlags::read, sess;
40ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
41ef3aeadcSPatrick Venture 
42ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
43ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(false));
44ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.open(flags, path, &sess));
45ef3aeadcSPatrick Venture }
46ef3aeadcSPatrick Venture 
TEST(ManagerOpenTest,OpenFailsMustSupplyAtLeastReadOrWriteFlag)47ef3aeadcSPatrick Venture TEST(ManagerOpenTest, OpenFailsMustSupplyAtLeastReadOrWriteFlag)
48ef3aeadcSPatrick Venture {
49ef3aeadcSPatrick Venture     // One must supply either read or write in the flags for the session to
50ef3aeadcSPatrick Venture     // open.
51ef3aeadcSPatrick Venture 
52ef3aeadcSPatrick Venture     BlobManager mgr;
53ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
54ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
55ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
56ef3aeadcSPatrick Venture 
57ef3aeadcSPatrick Venture     uint16_t flags = 0, sess;
58ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
59ef3aeadcSPatrick Venture 
60ef3aeadcSPatrick Venture     /* It checks if someone can handle the blob before it checks the flags. */
61ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
62ef3aeadcSPatrick Venture 
63ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.open(flags, path, &sess));
64ef3aeadcSPatrick Venture }
65ef3aeadcSPatrick Venture 
TEST(ManagerOpenTest,OpenSucceeds)66ef3aeadcSPatrick Venture TEST(ManagerOpenTest, OpenSucceeds)
67ef3aeadcSPatrick Venture {
68ef3aeadcSPatrick Venture     // The handler is found and Open succeeds.
69ef3aeadcSPatrick Venture 
70ef3aeadcSPatrick Venture     BlobManager mgr;
71ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
72ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
73ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
74ef3aeadcSPatrick Venture 
75ef3aeadcSPatrick Venture     uint16_t flags = OpenFlags::read, sess;
76ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
77ef3aeadcSPatrick Venture 
78ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
79ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
80ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.open(flags, path, &sess));
81ef3aeadcSPatrick Venture 
82ef3aeadcSPatrick Venture     // TODO(venture): Need a way to verify the session is associated with it,
83ef3aeadcSPatrick Venture     // maybe just call Read() or SessionStat()
84ef3aeadcSPatrick Venture }
85ef3aeadcSPatrick Venture } // namespace blobs
86