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