manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 1 | #include "ibm/locks.hpp" |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 2 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 3 | #include <cstdint> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 4 | #include <string> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 5 | #include <tuple> |
| 6 | #include <utility> |
| 7 | #include <variant> |
| 8 | #include <vector> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 10 | #include <gmock/gmock.h> // IWYU pragma: keep |
| 11 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 12 | |
| 13 | // IWYU pragma: no_include <gtest/gtest-message.h> |
| 14 | // IWYU pragma: no_include <gtest/gtest-test-part.h> |
| 15 | // IWYU pragma: no_include "gtest/gtest_pred_impl.h" |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 16 | |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 17 | namespace crow::ibm_mc_lock |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 18 | { |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 19 | namespace |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 20 | { |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 21 | |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 22 | using SType = std::string; |
| 23 | using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>; |
| 24 | using LockRequests = std::vector<LockRequest>; |
| 25 | using Rc = |
| 26 | std::pair<bool, std::variant<uint32_t, std::pair<uint32_t, LockRequest>>>; |
| 27 | using RcRelaseLock = std::pair<bool, std::pair<uint32_t, LockRequest>>; |
| 28 | using RcGetLockList = |
| 29 | std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>; |
| 30 | using ListOfTransactionIds = std::vector<uint32_t>; |
| 31 | using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>; |
| 32 | using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>; |
| 33 | using SessionFlags = std::pair<SType, SType>; |
| 34 | using ListOfSessionIds = std::vector<std::string>; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 35 | using ::testing::IsEmpty; |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 36 | |
| 37 | class LockTest : public ::testing::Test |
| 38 | { |
| 39 | protected: |
| 40 | LockRequests request; |
| 41 | LockRequests request1, request2; |
| 42 | LockRequest record; |
| 43 | |
| 44 | public: |
Ed Tanous | b31cef6 | 2022-06-30 17:51:46 -0700 | [diff] [blame] | 45 | LockTest() : |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 46 | // lockrequest with multiple lockrequests |
Ed Tanous | b31cef6 | 2022-06-30 17:51:46 -0700 | [diff] [blame] | 47 | request{{"xxxxx", |
| 48 | "hmc-id", |
| 49 | "Read", |
| 50 | 234, |
| 51 | {{"DontLock", 2}, {"DontLock", 4}}}, |
| 52 | {"xxxxx", |
| 53 | "hmc-id", |
| 54 | "Read", |
| 55 | 234, |
| 56 | {{"DontLock", 2}, {"DontLock", 4}}}}, |
| 57 | request1{{"xxxxx", |
| 58 | "hmc-id", |
| 59 | "Read", |
| 60 | 234, |
| 61 | {{"DontLock", 2}, {"DontLock", 4}}}}, |
| 62 | request2{{"xxxxx", |
| 63 | "hmc-id", |
| 64 | "Write", |
| 65 | 234, |
| 66 | {{"LockAll", 2}, {"DontLock", 4}}}}, |
| 67 | record{ |
| 68 | "xxxxx", "hmc-id", "Read", 234, {{"DontLock", 2}, {"DontLock", 4}}} |
| 69 | {} |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 70 | |
| 71 | ~LockTest() override = default; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 72 | |
| 73 | LockTest(const LockTest&) = delete; |
| 74 | LockTest(LockTest&&) = delete; |
| 75 | LockTest& operator=(const LockTest&) = delete; |
| 76 | LockTest& operator=(const LockTest&&) = delete; |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class MockLock : public crow::ibm_mc_lock::Lock |
| 80 | { |
| 81 | public: |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 82 | bool isValidLockRequest(const LockRequest& record1) override |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 83 | { |
| 84 | bool status = Lock::isValidLockRequest(record1); |
| 85 | return status; |
| 86 | } |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 87 | bool isConflictRequest(const LockRequests& request) override |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 88 | { |
| 89 | bool status = Lock::isConflictRequest(request); |
| 90 | return status; |
| 91 | } |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 92 | Rc isConflictWithTable(const LockRequests& request) override |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 93 | { |
| 94 | auto conflict = Lock::isConflictWithTable(request); |
| 95 | return conflict; |
| 96 | } |
| 97 | uint32_t generateTransactionId() override |
| 98 | { |
| 99 | uint32_t tid = Lock::generateTransactionId(); |
| 100 | return tid; |
| 101 | } |
| 102 | |
| 103 | bool validateRids(const ListOfTransactionIds& tids) override |
| 104 | { |
| 105 | bool status = Lock::validateRids(tids); |
| 106 | return status; |
| 107 | } |
| 108 | RcRelaseLock isItMyLock(const ListOfTransactionIds& tids, |
| 109 | const SessionFlags& ids) override |
| 110 | { |
| 111 | auto status = Lock::isItMyLock(tids, ids); |
| 112 | return status; |
| 113 | } |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 114 | friend class LockTest; |
| 115 | }; |
| 116 | |
| 117 | TEST_F(LockTest, ValidationGoodTestCase) |
| 118 | { |
| 119 | MockLock lockManager; |
| 120 | const LockRequest& t = record; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 121 | EXPECT_TRUE(lockManager.isValidLockRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(LockTest, ValidationBadTestWithLocktype) |
| 125 | { |
| 126 | MockLock lockManager; |
| 127 | // Corrupt the lock type |
| 128 | std::get<2>(record) = "rwrite"; |
| 129 | const LockRequest& t = record; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 130 | EXPECT_FALSE(lockManager.isValidLockRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(LockTest, ValidationBadTestWithlockFlags) |
| 134 | { |
| 135 | MockLock lockManager; |
| 136 | // Corrupt the lockflag |
| 137 | std::get<4>(record)[0].first = "lock"; |
| 138 | const LockRequest& t = record; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 139 | EXPECT_FALSE(lockManager.isValidLockRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST_F(LockTest, ValidationBadTestWithSegmentlength) |
| 143 | { |
| 144 | MockLock lockManager; |
| 145 | // Corrupt the Segment length |
| 146 | std::get<4>(record)[0].second = 7; |
| 147 | const LockRequest& t = record; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 148 | EXPECT_FALSE(lockManager.isValidLockRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(LockTest, MultiRequestWithoutConflict) |
| 152 | { |
| 153 | MockLock lockManager; |
| 154 | const LockRequests& t = request; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 155 | EXPECT_FALSE(lockManager.isConflictRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentLength) |
| 159 | { |
| 160 | MockLock lockManager; |
| 161 | // Corrupt the locktype |
| 162 | std::get<2>(request[0]) = "Write"; |
| 163 | // Match the segment lengths to points them to lock similar kind of |
| 164 | // resource |
| 165 | std::get<4>(request[0])[0].first = "LockAll"; |
| 166 | const LockRequests& t = request; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 167 | EXPECT_TRUE(lockManager.isConflictRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 168 | } |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 169 | |
| 170 | TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentLength) |
| 171 | { |
| 172 | MockLock lockManager; |
| 173 | // Corrupt the locktype |
| 174 | std::get<2>(request[0]) = "Write"; |
| 175 | // Match the segment lengths to points them to lock similar kind of |
| 176 | // resource |
| 177 | std::get<4>(request[0])[0].first = "LockSame"; |
| 178 | // Change the segment length , so that the requests are trying to lock |
| 179 | // two different kind of resources |
| 180 | std::get<4>(request[0])[0].second = 3; |
| 181 | const LockRequests& t = request; |
| 182 | // Return No Conflict |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 183 | EXPECT_FALSE(lockManager.isConflictRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktype) |
| 187 | { |
| 188 | MockLock lockManager; |
| 189 | // Match the segment lengths to points them to lock similar kind of |
| 190 | // resource |
| 191 | std::get<4>(request[0])[0].first = "LockAll"; |
| 192 | const LockRequests& t = request; |
| 193 | // Return No Conflict |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 194 | EXPECT_FALSE(lockManager.isConflictRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktypeAndLockall) |
| 198 | { |
| 199 | MockLock lockManager; |
| 200 | // Match the segment lengths to points them to lock similar kind of |
| 201 | // resource |
| 202 | std::get<4>(request[0])[0].first = "LockAll"; |
| 203 | std::get<4>(request[0])[1].first = "LockAll"; |
| 204 | const LockRequests& t = request; |
| 205 | // Return No Conflict |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 206 | EXPECT_FALSE(lockManager.isConflictRequest(t)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 207 | } |
| 208 | |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 209 | TEST_F(LockTest, RequestNotConflictedWithLockTableEntries) |
| 210 | { |
| 211 | MockLock lockManager; |
| 212 | const LockRequests& t = request1; |
| 213 | // Insert the request1 into the lock table |
| 214 | auto rc1 = lockManager.isConflictWithTable(t); |
| 215 | // Corrupt the lock type |
| 216 | std::get<2>(request[0]) = "Read"; |
| 217 | // Corrupt the lockflag |
| 218 | std::get<4>(request[0])[1].first = "LockAll"; |
| 219 | const LockRequests& p = request; |
| 220 | auto rc2 = lockManager.isConflictWithTable(p); |
| 221 | // Return No Conflict |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 222 | EXPECT_FALSE(rc2.first); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(LockTest, TestGenerateTransactionIDFunction) |
| 226 | { |
| 227 | MockLock lockManager; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 228 | uint32_t transactionId1 = lockManager.generateTransactionId(); |
| 229 | uint32_t transactionId2 = lockManager.generateTransactionId(); |
| 230 | EXPECT_EQ(transactionId2, ++transactionId1); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST_F(LockTest, ValidateTransactionIDsGoodTestCase) |
| 234 | { |
| 235 | MockLock lockManager; |
| 236 | const LockRequests& t = request1; |
| 237 | // Insert the request1 into the lock table |
| 238 | auto rc1 = lockManager.isConflictWithTable(t); |
| 239 | std::vector<uint32_t> tids = {1}; |
| 240 | const std::vector<uint32_t>& p = tids; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 241 | EXPECT_TRUE(lockManager.validateRids(p)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(LockTest, ValidateTransactionIDsBadTestCase) |
| 245 | { |
| 246 | MockLock lockManager; |
| 247 | // Insert the request1 into the lock table |
| 248 | const LockRequests& t = request1; |
| 249 | auto rc1 = lockManager.isConflictWithTable(t); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 250 | std::vector<uint32_t> tids = {10}; |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 251 | const std::vector<uint32_t>& p = tids; |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 252 | EXPECT_FALSE(lockManager.validateRids(p)); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F(LockTest, ValidateisItMyLockGoodTestCase) |
| 256 | { |
| 257 | MockLock lockManager; |
| 258 | // Insert the request1 into the lock table |
| 259 | const LockRequests& t = request1; |
| 260 | auto rc1 = lockManager.isConflictWithTable(t); |
| 261 | std::vector<uint32_t> tids = {1}; |
| 262 | const std::vector<uint32_t>& p = tids; |
| 263 | std::string hmcid = "hmc-id"; |
| 264 | std::string sessionid = "xxxxx"; |
| 265 | std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid); |
| 266 | auto rc = lockManager.isItMyLock(p, ids); |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 267 | EXPECT_TRUE(rc.first); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | TEST_F(LockTest, ValidateisItMyLockBadTestCase) |
| 271 | { |
| 272 | MockLock lockManager; |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 273 | // Corrupt the client identifier |
| 274 | std::get<1>(request1[0]) = "randomid"; |
| 275 | // Insert the request1 into the lock table |
| 276 | const LockRequests& t = request1; |
| 277 | auto rc1 = lockManager.isConflictWithTable(t); |
| 278 | std::vector<uint32_t> tids = {1}; |
| 279 | const std::vector<uint32_t>& p = tids; |
| 280 | std::string hmcid = "hmc-id"; |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 281 | std::string sessionid = "random"; |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 282 | std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid); |
| 283 | auto rc = lockManager.isItMyLock(p, ids); |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 284 | EXPECT_FALSE(rc.first); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(LockTest, ValidateSessionIDForGetlocklistBadTestCase) |
| 288 | { |
| 289 | MockLock lockManager; |
| 290 | // Insert the request1 into the lock table |
| 291 | const LockRequests& t = request1; |
| 292 | auto rc1 = lockManager.isConflictWithTable(t); |
| 293 | std::vector<std::string> sessionid = {"random"}; |
| 294 | auto status = lockManager.getLockList(sessionid); |
| 295 | auto result = |
| 296 | std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 297 | EXPECT_THAT(result, IsEmpty()); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(LockTest, ValidateSessionIDForGetlocklistGoodTestCase) |
| 301 | { |
| 302 | MockLock lockManager; |
| 303 | // Insert the request1 into the lock table |
| 304 | const LockRequests& t = request1; |
| 305 | auto rc1 = lockManager.isConflictWithTable(t); |
| 306 | std::vector<std::string> sessionid = {"xxxxx"}; |
| 307 | auto status = lockManager.getLockList(sessionid); |
| 308 | auto result = |
| 309 | std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status); |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 310 | EXPECT_EQ(result.size(), 1); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 311 | } |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 312 | |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 313 | } // namespace |
| 314 | } // namespace crow::ibm_mc_lock |