Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame^] | 1 | #include "blob_mock.hpp" |
| 2 | #include "manager.hpp" |
| 3 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 4 | #include <vector> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | namespace blobs |
| 9 | { |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::Return; |
| 13 | |
| 14 | TEST(ManagerCommitTest, CommitNoSessionReturnsFalse) |
| 15 | { |
| 16 | // Calling Commit on a session that doesn't exist should return false. |
| 17 | |
| 18 | BlobManager mgr; |
| 19 | uint16_t sess = 1; |
| 20 | std::vector<uint8_t> data; |
| 21 | |
| 22 | EXPECT_FALSE(mgr.commit(sess, data)); |
| 23 | } |
| 24 | |
| 25 | TEST(ManagerCommitTest, CommitSessionFoundButHandlerReturnsFalse) |
| 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 | |
| 37 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true)); |
| 38 | EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true)); |
| 39 | EXPECT_TRUE(mgr.open(flags, path, &sess)); |
| 40 | |
| 41 | std::vector<uint8_t> data; |
| 42 | EXPECT_CALL(*m1ptr, commit(sess, data)).WillOnce(Return(false)); |
| 43 | |
| 44 | EXPECT_FALSE(mgr.commit(sess, data)); |
| 45 | } |
| 46 | |
| 47 | TEST(ManagerCommitTest, CommitSessionFoundAndHandlerReturnsSuccess) |
| 48 | { |
| 49 | // The handler was found and returned success. |
| 50 | |
| 51 | BlobManager mgr; |
| 52 | std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>(); |
| 53 | auto m1ptr = m1.get(); |
| 54 | EXPECT_TRUE(mgr.registerHandler(std::move(m1))); |
| 55 | |
| 56 | uint16_t flags = OpenFlags::write, sess; |
| 57 | std::string path = "/asdf/asdf"; |
| 58 | |
| 59 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true)); |
| 60 | EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true)); |
| 61 | EXPECT_TRUE(mgr.open(flags, path, &sess)); |
| 62 | |
| 63 | std::vector<uint8_t> data; |
| 64 | EXPECT_CALL(*m1ptr, commit(sess, data)).WillOnce(Return(true)); |
| 65 | |
| 66 | EXPECT_TRUE(mgr.commit(sess, data)); |
| 67 | } |
| 68 | } // namespace blobs |