blob: 50ea7a4d7e88e9dd8b23a63540032baa0c67019f [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "blob_mock.hpp"
Patrick Ventureaceb4ba2018-09-27 14:50:37 -07002
3#include <blobs-ipmid/manager.hpp>
Patrick Ventureef3aead2018-09-12 08:53:29 -07004
5#include <gtest/gtest.h>
6
7using ::testing::_;
8using ::testing::Return;
9
10namespace blobs
11{
12
13TEST(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
25TEST(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
48TEST(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
69TEST(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