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