blob: ef42ea62d6f83276fc23a8088248dd0675f36fce [file] [log] [blame]
Kun Yi91beea62018-11-26 15:23:14 -08001#include "handler_unittest.hpp"
2
Kun Yi64dc05c2018-12-19 13:19:03 -08003#include <algorithm>
4#include <memory>
5#include <string>
6#include <vector>
7
Kun Yi68c81142018-12-18 11:17:14 -08008using ::testing::_;
Kun Yi64dc05c2018-12-19 13:19:03 -08009using ::testing::AtLeast;
10using ::testing::ElementsAreArray;
11using ::testing::IsEmpty;
Kun Yi68c81142018-12-18 11:17:14 -080012using ::testing::Return;
13using ::testing::StrEq;
14using ::testing::StrNe;
Kun Yi64dc05c2018-12-19 13:19:03 -080015using ::testing::UnorderedElementsAreArray;
16
Kun Yi68c81142018-12-18 11:17:14 -080017using namespace std::string_literals;
18using namespace binstore;
19
Kun Yi91beea62018-11-26 15:23:14 -080020namespace blobs
21{
22
Kun Yi68c81142018-12-18 11:17:14 -080023class BinaryStoreBlobHandlerBasicTest : public BinaryStoreBlobHandlerTest
24{
25 protected:
26 static inline std::string basicTestBaseId = "/test/"s;
27 static inline std::string basicTestBlobId = "/test/blob0"s;
Kun Yi64dc05c2018-12-19 13:19:03 -080028
29 static const std::vector<std::string> basicTestBaseIdList;
Kun Yi68c81142018-12-18 11:17:14 -080030 static inline std::string basicTestInvalidBlobId = "/invalid/blob0"s;
Kun Yi64dc05c2018-12-19 13:19:03 -080031
32 void addAllBaseIds()
33 {
34 for (size_t i = 0; i < basicTestBaseIdList.size(); ++i)
35 {
36 auto store = defaultMockStore(basicTestBaseIdList[i]);
37
38 EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1));
39
40 handler.addNewBinaryStore(std::move(store));
41 }
42 }
Kun Yi68c81142018-12-18 11:17:14 -080043};
44
Kun Yi64dc05c2018-12-19 13:19:03 -080045const std::vector<std::string>
46 BinaryStoreBlobHandlerBasicTest::basicTestBaseIdList = {
47 BinaryStoreBlobHandlerBasicTest::basicTestBaseId, "/another/"s,
48 "/null\0/"s};
49
Kun Yi68c81142018-12-18 11:17:14 -080050TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobZeroStoreFail)
51{
52 // Cannot handle since there is no store. Shouldn't crash.
53 EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId));
54}
55
Kun Yi64dc05c2018-12-19 13:19:03 -080056TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobChecksName)
Kun Yi91beea62018-11-26 15:23:14 -080057{
Kun Yi64dc05c2018-12-19 13:19:03 -080058 auto store = defaultMockStore(basicTestBaseId);
Kun Yi68c81142018-12-18 11:17:14 -080059
Kun Yi64dc05c2018-12-19 13:19:03 -080060 EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1));
61
62 handler.addNewBinaryStore(std::move(store));
Kun Yi68c81142018-12-18 11:17:14 -080063
Kun Yi91beea62018-11-26 15:23:14 -080064 // Verify canHandleBlob checks and returns false on an invalid name.
Kun Yi68c81142018-12-18 11:17:14 -080065 EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId));
Kun Yi68c81142018-12-18 11:17:14 -080066 // Verify canHandleBlob return true for a blob id that it can handle
Kun Yi68c81142018-12-18 11:17:14 -080067 EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId));
68}
69
Kun Yi64dc05c2018-12-19 13:19:03 -080070TEST_F(BinaryStoreBlobHandlerBasicTest, GetBlobIdEqualsConcatenationsOfBaseIds)
Kun Yi68c81142018-12-18 11:17:14 -080071{
Kun Yi64dc05c2018-12-19 13:19:03 -080072 addAllBaseIds();
Kun Yi68c81142018-12-18 11:17:14 -080073
Kun Yi64dc05c2018-12-19 13:19:03 -080074 // When there is no other blob id, ids are just base ids (might be
75 // re-ordered).
76 EXPECT_THAT(handler.getBlobIds(),
77 UnorderedElementsAreArray(basicTestBaseIdList));
Kun Yi68c81142018-12-18 11:17:14 -080078}
79
Kun Yi64dc05c2018-12-19 13:19:03 -080080TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobInvalidNames)
Kun Yi68c81142018-12-18 11:17:14 -080081{
Kun Yi64dc05c2018-12-19 13:19:03 -080082 addAllBaseIds();
Kun Yi68c81142018-12-18 11:17:14 -080083
Kun Yi64dc05c2018-12-19 13:19:03 -080084 const std::vector<std::string> invalidNames = {
85 basicTestInvalidBlobId,
86 "/"s,
87 "//"s,
88 "/test"s, // Cannot handle the base path
89 "/test/"s,
90 "/test/this/blob"s, // Cannot handle nested path
91 };
Kun Yi68c81142018-12-18 11:17:14 -080092
Kun Yi64dc05c2018-12-19 13:19:03 -080093 // Unary helper for algorithm
94 auto canHandle = [this](const std::string& blobId) {
95 return handler.canHandleBlob(blobId);
96 };
Kun Yi68c81142018-12-18 11:17:14 -080097
Kun Yi64dc05c2018-12-19 13:19:03 -080098 EXPECT_TRUE(
99 std::none_of(invalidNames.cbegin(), invalidNames.cend(), canHandle));
100}
101
102TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobValidNames)
103{
104 addAllBaseIds();
105
106 const std::vector<std::string> validNames = {
107 basicTestBlobId, "/test/test"s, "/test/xyz.abc"s,
108 "/another/blob"s, "/null\0/\0\0zer\0\0"s,
109 };
110
111 // Unary helper for algorithm
112 auto canHandle = [this](const std::string& blobId) {
113 return handler.canHandleBlob(blobId);
114 };
115
116 // Verify canHandleBlob can handle a valid blob under the path.
117 EXPECT_TRUE(std::all_of(validNames.cbegin(), validNames.cend(), canHandle));
Kun Yi91beea62018-11-26 15:23:14 -0800118}
119
Kun Yic0adbc32018-12-18 22:35:29 -0800120TEST_F(BinaryStoreBlobHandlerBasicTest, DeleteReturnsWhatStoreReturns)
121{
Kun Yi64dc05c2018-12-19 13:19:03 -0800122 auto store = defaultMockStore(basicTestBaseId);
Kun Yic0adbc32018-12-18 22:35:29 -0800123
Kun Yi64dc05c2018-12-19 13:19:03 -0800124 EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1));
125 EXPECT_CALL(*store, deleteBlob(StrEq(basicTestBlobId)))
Kun Yic0adbc32018-12-18 22:35:29 -0800126 .WillOnce(Return(false))
127 .WillOnce(Return(true));
Kun Yi64dc05c2018-12-19 13:19:03 -0800128 handler.addNewBinaryStore(std::move(store));
129
130 // Unary helper for algorithm
131 auto canHandle = [this](const std::string& blobId) {
132 return handler.canHandleBlob(blobId);
133 };
Kun Yic0adbc32018-12-18 22:35:29 -0800134
135 // Verify canHandleBlob return true for a blob id that it can handle
Kun Yi64dc05c2018-12-19 13:19:03 -0800136 EXPECT_FALSE(canHandle(basicTestInvalidBlobId));
137 EXPECT_TRUE(canHandle(basicTestBlobId));
Kun Yic0adbc32018-12-18 22:35:29 -0800138 EXPECT_FALSE(handler.deleteBlob(basicTestInvalidBlobId));
139 EXPECT_FALSE(handler.deleteBlob(basicTestBlobId));
140 EXPECT_TRUE(handler.deleteBlob(basicTestBlobId));
141}
142
Kun Yi91beea62018-11-26 15:23:14 -0800143} // namespace blobs