summaryrefslogtreecommitdiff
path: root/test/include/ibm/lock_test.cpp
blob: 087a84b431422844f588cd6a0b9e3048ecada7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "ibm/locks.hpp"

#include <cstdint>
#include <string>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>

#include <gmock/gmock.h> // IWYU pragma: keep
#include <gtest/gtest.h> // IWYU pragma: keep

// IWYU pragma: no_include <gtest/gtest-message.h>
// IWYU pragma: no_include <gtest/gtest-test-part.h>
// IWYU pragma: no_include "gtest/gtest_pred_impl.h"

namespace crow::ibm_mc_lock
{
namespace
{

using SType = std::string;
using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
using LockRequests = std::vector<LockRequest>;
using Rc =
    std::pair<bool, std::variant<uint32_t, std::pair<uint32_t, LockRequest>>>;
using RcRelaseLock = std::pair<bool, std::pair<uint32_t, LockRequest>>;
using RcGetLockList =
    std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
using ListOfTransactionIds = std::vector<uint32_t>;
using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>;
using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>;
using SessionFlags = std::pair<SType, SType>;
using ListOfSessionIds = std::vector<std::string>;
using ::testing::IsEmpty;

class LockTest : public ::testing::Test
{
  protected:
    LockRequests request;
    LockRequests request1, request2;
    LockRequest record;

  public:
    LockTest() :
        // lockrequest with multiple lockrequests
        request{{"xxxxx",
                 "hmc-id",
                 "Read",
                 234,
                 {{"DontLock", 2}, {"DontLock", 4}}},
                {"xxxxx",
                 "hmc-id",
                 "Read",
                 234,
                 {{"DontLock", 2}, {"DontLock", 4}}}},
        request1{{"xxxxx",
                  "hmc-id",
                  "Read",
                  234,
                  {{"DontLock", 2}, {"DontLock", 4}}}},
        request2{{"xxxxx",
                  "hmc-id",
                  "Write",
                  234,
                  {{"LockAll", 2}, {"DontLock", 4}}}},
        record{
            "xxxxx", "hmc-id", "Read", 234, {{"DontLock", 2}, {"DontLock", 4}}}
    {}

    ~LockTest() override = default;

    LockTest(const LockTest&) = delete;
    LockTest(LockTest&&) = delete;
    LockTest& operator=(const LockTest&) = delete;
    LockTest& operator=(const LockTest&&) = delete;
};

class MockLock : public crow::ibm_mc_lock::Lock
{
  public:
    bool isValidLockRequest(const LockRequest& record1) override
    {
        bool status = Lock::isValidLockRequest(record1);
        return status;
    }
    bool isConflictRequest(const LockRequests& request) override
    {
        bool status = Lock::isConflictRequest(request);
        return status;
    }
    Rc isConflictWithTable(const LockRequests& request) override
    {
        auto conflict = Lock::isConflictWithTable(request);
        return conflict;
    }
    uint32_t generateTransactionId() override
    {
        uint32_t tid = Lock::generateTransactionId();
        return tid;
    }

    bool validateRids(const ListOfTransactionIds& tids) override
    {
        bool status = Lock::validateRids(tids);
        return status;
    }
    RcRelaseLock isItMyLock(const ListOfTransactionIds& tids,
                            const SessionFlags& ids) override
    {
        auto status = Lock::isItMyLock(tids, ids);
        return status;
    }
    friend class LockTest;
};

TEST_F(LockTest, ValidationGoodTestCase)
{
    MockLock lockManager;
    const LockRequest& t = record;
    EXPECT_TRUE(lockManager.isValidLockRequest(t));
}

TEST_F(LockTest, ValidationBadTestWithLocktype)
{
    MockLock lockManager;
    // Corrupt the lock type
    std::get<2>(record) = "rwrite";
    const LockRequest& t = record;
    EXPECT_FALSE(lockManager.isValidLockRequest(t));
}

TEST_F(LockTest, ValidationBadTestWithlockFlags)
{
    MockLock lockManager;
    // Corrupt the lockflag
    std::get<4>(record)[0].first = "lock";
    const LockRequest& t = record;
    EXPECT_FALSE(lockManager.isValidLockRequest(t));
}

TEST_F(LockTest, ValidationBadTestWithSegmentlength)
{
    MockLock lockManager;
    // Corrupt the Segment length
    std::get<4>(record)[0].second = 7;
    const LockRequest& t = record;
    EXPECT_FALSE(lockManager.isValidLockRequest(t));
}

TEST_F(LockTest, MultiRequestWithoutConflict)
{
    MockLock lockManager;
    const LockRequests& t = request;
    EXPECT_FALSE(lockManager.isConflictRequest(t));
}

TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentLength)
{
    MockLock lockManager;
    // Corrupt the locktype
    std::get<2>(request[0]) = "Write";
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "LockAll";
    const LockRequests& t = request;
    EXPECT_TRUE(lockManager.isConflictRequest(t));
}
#if 0 // Comment out due to bad code in include/ibm/locks.hpp
TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentData)
{
    MockLock lockManager;
    // Corrupt the locktype
    std::get<2>(request[0]) = "Write";
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "DontLock";
    std::get<4>(request[0])[1].first = "LockAll";

    // Change the resource id(2nd byte) of first record, so the locks are
    // different so no conflict
    std::get<3>(request[0]) = 216179379183550464; // HEX 03 00 06 00 00 00 00 00
    std::get<3>(request[1]) = 288236973221478400; // HEX 04 00 06 00 00 00 00 00
    const LockRequests& t = request;
    EXPECT_FALSE(lockManager.isConflictRequest(t));
}

TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentData)
{
    MockLock lockManager;
    // Corrupt the locktype
    std::get<2>(request[0]) = "Write";
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "DontLock";
    std::get<4>(request[0])[1].first = "LockAll";
    // Dont Change the resource id(1st & 2nd byte) at all, so that the
    // conflict occurs from the second segment which is trying to lock all
    // the resources.
    std::get<3>(request[0]) = 216173882346831872; // 03 00 01 00 2B 00 00 00
    std::get<3>(request[1]) = 216173882346831872; // 03 00 01 00 2B 00 00 00
    const LockRequests& t = request;
    EXPECT_TRUE(lockManager.isConflictRequest(t));
}
#endif

TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentLength)
{
    MockLock lockManager;
    // Corrupt the locktype
    std::get<2>(request[0]) = "Write";
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "LockSame";
    // Change the segment length , so that the requests are trying to lock
    // two different kind of resources
    std::get<4>(request[0])[0].second = 3;
    const LockRequests& t = request;
    // Return No Conflict
    EXPECT_FALSE(lockManager.isConflictRequest(t));
}

TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktype)
{
    MockLock lockManager;
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "LockAll";
    const LockRequests& t = request;
    // Return No Conflict
    EXPECT_FALSE(lockManager.isConflictRequest(t));
}

TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktypeAndLockall)
{
    MockLock lockManager;
    // Match the segment lengths to points them to lock similar kind of
    // resource
    std::get<4>(request[0])[0].first = "LockAll";
    std::get<4>(request[0])[1].first = "LockAll";
    const LockRequests& t = request;
    // Return No Conflict
    EXPECT_FALSE(lockManager.isConflictRequest(t));
}

#if 0 // Comment out due to bad code in include/ibm/locks.hpp
TEST_F(LockTest, RequestConflictedWithLockTableEntries)
{
    MockLock lockManager;
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    // Corrupt the lock type
    std::get<2>(request[0]) = "Write";
    // Corrupt the lockflag
    std::get<4>(request[0])[1].first = "LockAll";
    const LockRequests& p = request;
    auto rc2 = lockManager.isConflictWithTable(p);
    // Return a Conflict
    EXPECT_TRUE(rc2.first);
}
#endif

TEST_F(LockTest, RequestNotConflictedWithLockTableEntries)
{
    MockLock lockManager;
    const LockRequests& t = request1;
    // Insert the request1 into the lock table
    auto rc1 = lockManager.isConflictWithTable(t);
    // Corrupt the lock type
    std::get<2>(request[0]) = "Read";
    // Corrupt the lockflag
    std::get<4>(request[0])[1].first = "LockAll";
    const LockRequests& p = request;
    auto rc2 = lockManager.isConflictWithTable(p);
    // Return No Conflict
    EXPECT_FALSE(rc2.first);
}

TEST_F(LockTest, TestGenerateTransactionIDFunction)
{
    MockLock lockManager;
    uint32_t transactionId1 = lockManager.generateTransactionId();
    uint32_t transactionId2 = lockManager.generateTransactionId();
    EXPECT_EQ(transactionId2, ++transactionId1);
}

TEST_F(LockTest, ValidateTransactionIDsGoodTestCase)
{
    MockLock lockManager;
    const LockRequests& t = request1;
    // Insert the request1 into the lock table
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<uint32_t> tids = {1};
    const std::vector<uint32_t>& p = tids;
    EXPECT_TRUE(lockManager.validateRids(p));
}

TEST_F(LockTest, ValidateTransactionIDsBadTestCase)
{
    MockLock lockManager;
    // Insert the request1 into the lock table
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<uint32_t> tids = {10};
    const std::vector<uint32_t>& p = tids;
    EXPECT_FALSE(lockManager.validateRids(p));
}

TEST_F(LockTest, ValidateisItMyLockGoodTestCase)
{
    MockLock lockManager;
    // Insert the request1 into the lock table
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<uint32_t> tids = {1};
    const std::vector<uint32_t>& p = tids;
    std::string hmcid = "hmc-id";
    std::string sessionid = "xxxxx";
    std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid);
    auto rc = lockManager.isItMyLock(p, ids);
    EXPECT_TRUE(rc.first);
}

TEST_F(LockTest, ValidateisItMyLockBadTestCase)
{
    MockLock lockManager;
    // Corrupt the client identifier
    std::get<1>(request1[0]) = "randomid";
    // Insert the request1 into the lock table
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<uint32_t> tids = {1};
    const std::vector<uint32_t>& p = tids;
    std::string hmcid = "hmc-id";
    std::string sessionid = "random";
    std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid);
    auto rc = lockManager.isItMyLock(p, ids);
    EXPECT_FALSE(rc.first);
}

TEST_F(LockTest, ValidateSessionIDForGetlocklistBadTestCase)
{
    MockLock lockManager;
    // Insert the request1 into the lock table
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<std::string> sessionid = {"random"};
    auto status = lockManager.getLockList(sessionid);
    auto result =
        std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
    EXPECT_THAT(result, IsEmpty());
}

TEST_F(LockTest, ValidateSessionIDForGetlocklistGoodTestCase)
{
    MockLock lockManager;
    // Insert the request1 into the lock table
    const LockRequests& t = request1;
    auto rc1 = lockManager.isConflictWithTable(t);
    std::vector<std::string> sessionid = {"xxxxx"};
    auto status = lockManager.getLockList(sessionid);
    auto result =
        std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
    EXPECT_EQ(result.size(), 1);
}

} // namespace
} // namespace crow::ibm_mc_lock