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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426 | // Copyright (C) 2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/testutils/concrete_lease_mgr.h>
#include <dhcpsrv/testutils/generic_lease_mgr_unittest.h>
#include <exceptions/exceptions.h>
#include <functional><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::asiolink;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
namespace ph = std::placeholders;
namespace {
/// @brief Test fixture class for @c TrackingLeaseMgr.
class TrackingLeaseMgrTest : public GenericLeaseMgrTest {
public:
/// @brief Reopen the database
///
/// No-op implementation. We need to provide concrete implementation,
/// as this is a pure virtual method in GenericLeaseMgrTest.
virtual void reopen(Universe) {}
/// @brief Convenience function creating a lease instance.
///
/// We don't use the @c initializeLease4 function here because it uses
/// tons of parameters we don't really need here. We simply need the
/// subnet id and the address.
///
/// @param subnet_id subnet identifier for the lease.
/// @param address leased address.
Lease4Ptr initializeLease(int subnet_id, std::string address) const {
auto lease = boost::make_shared<Lease4>();
lease->subnet_id_ = SubnetID(subnet_id);
lease->addr_ = IOAddress(address);
return (lease);
}
/// @brief Convenience function creating a lease instance.
///
/// We don't use the initializeLease6 function here because it uses
/// tons of parameters we don't really need here. We simply need the
/// subnet id, lease type and the address.
///
/// @param subnet_id subnet identifier for the lease.
/// @param lease_type lease type.
/// @param address leased address.
Lease6Ptr initializeLease(int subnet_id, Lease::Type lease_type, std::string address) const {
auto lease = boost::make_shared<Lease6>();
lease->subnet_id_ = SubnetID(subnet_id);
lease->addr_ = IOAddress(address);
lease->type_ = lease_type;
return (lease);
}
};
/// Tests that leases can be locked and unlocked. When a lease is locked
/// an attempt to lock it again fails.
TEST_F(TrackingLeaseMgrTest, tryLock) {<--- syntax error
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
// An attempt to lock an already locked lease should fail.
EXPECT_TRUE(mgr.tryLock(initializeLease(1, "192.0.2.1")));
EXPECT_FALSE(mgr.tryLock(initializeLease(1, "192.0.2.1")));
EXPECT_TRUE(mgr.isLocked(initializeLease(1, "192.0.2.1")));
// We can lock another lease but we cannot lock an already locked one.
EXPECT_TRUE(mgr.tryLock(initializeLease(1, "192.0.2.2")));
EXPECT_FALSE(mgr.tryLock(initializeLease(1, "192.0.2.1")));
EXPECT_FALSE(mgr.tryLock(initializeLease(2, "192.0.2.2")));
EXPECT_TRUE(mgr.isLocked(initializeLease(1, "192.0.2.1")));
EXPECT_TRUE(mgr.isLocked(initializeLease(2, "192.0.2.2")));
// If we unlock the lease, it can be locked again. However, unlocking
// the lease should not affect other locks.
mgr.unlock(initializeLease(1, "192.0.2.1"));
EXPECT_FALSE(mgr.isLocked(initializeLease(1, "192.0.2.1")));
EXPECT_TRUE(mgr.isLocked(initializeLease(2, "192.0.2.2")));
EXPECT_FALSE(mgr.tryLock(initializeLease(2, "192.0.2.2")));
EXPECT_TRUE(mgr.tryLock(initializeLease(1, "192.0.2.1")));
}
/// Tests registering the callbacks.
TEST_F(TrackingLeaseMgrTest, registerCallbacks) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
// Callback for lease add and global subnet id.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// Callback for lease add and subnet id 1.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq", 1,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
1, ph::_1)));
// Callback for lease add and subnet id 2.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq", 2,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
1, ph::_1)));
// Callback for lease update and global subnet id.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// Callback for lease delete and global subnet id.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_DELETE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// This call should trigger the lease add callbacks for subnet id 0 and 1.
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, "192.0.2.1")));
EXPECT_EQ(2, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_V4));
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, 1, Lease::TYPE_V4));
// This call should trigger the lease add callback for subnet id 0 only. That's
// because we have no callback for the subnet id 3.
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(3, "192.0.2.1")));
EXPECT_EQ(3, logs_.size());
EXPECT_EQ(2, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_V4));
}
/// Test that registering the callbacks of the same type, for the same subnet id by the
/// same owner fails.
TEST_F(TrackingLeaseMgrTest, registerCallbacksConflicts) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
// Add the callback for lease add and subnet id 0.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// Another attempt should fail.
EXPECT_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)),
InvalidOperation);
// It should succeed for a different owner.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "qlf",
SUBNET_ID_GLOBAL,
Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// It should also succeed for a different subnet id.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "qlf", 5,
Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
5, ph::_1)));
// But, another attempt for the subnet id should fail.
EXPECT_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "qlf", 5,
Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
5, ph::_1)),
InvalidOperation);
// However, changing a lease type should make it succeed.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "qlf", 5,
Lease::TYPE_PD,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
5, ph::_1)));
}
/// Test invoking the registered lease add callbacks.
TEST_F(TrackingLeaseMgrTest, trackAddLeaseDifferentLeaseTypes) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_NA,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_PD,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, "192.0.2.1")));
EXPECT_EQ(1, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_V4));
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, Lease::TYPE_NA, "2001:db8:1::1")));
EXPECT_EQ(2, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_NA));
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, Lease::TYPE_PD, "3000::")));
EXPECT_EQ(3, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_PD));
}
/// Test invoking the registered lease update callbacks.
TEST_F(TrackingLeaseMgrTest, trackUpdateLease) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq",
SUBNET_ID_GLOBAL,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_DELETE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.trackUpdateLease(initializeLease(1, "192.0.2.1")));
EXPECT_EQ(1, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_UPDATE_LEASE, 0, Lease::TYPE_V4));
}
/// Test invoking the registered lease delete callbacks.
TEST_F(TrackingLeaseMgrTest, trackDeleteLease) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_DELETE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.trackDeleteLease(initializeLease(1, "192.0.2.1")));
EXPECT_EQ(1, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_DELETE_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_V4));
}
// Test unregistering the callbacks by subnet id.
TEST_F(TrackingLeaseMgrTest, unregisterCallbacksBySubnetID) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
// Register different callback types for different subnet identifiers.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq", 1,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
1, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq", 2,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
2, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq", 1,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
1, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq", 2,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
2, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, "flq", 1,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_DELETE_LEASE,
1, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_DELETE_LEASE, "flq", 2,
Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_DELETE_LEASE,
2, ph::_1)));
// Unregister the callbacks for subnet id 1.
EXPECT_NO_THROW(mgr.unregisterCallbacks(SubnetID(1), Lease::TYPE_V4));
// Invoke the remaining callbacks for the subnet id 1.
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, "192.0.2.1")));
EXPECT_NO_THROW(mgr.trackUpdateLease(initializeLease(1, "192.0.2.1")));
EXPECT_NO_THROW(mgr.trackDeleteLease(initializeLease(1, "192.0.2.1")));
// It should only run the callback for the global subnet id that is still
// registered.
EXPECT_EQ(1, logs_.size());
EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, SUBNET_ID_GLOBAL, Lease::TYPE_V4));
// Unregister this callback.
EXPECT_NO_THROW(mgr.unregisterCallbacks(SUBNET_ID_GLOBAL, Lease::TYPE_V4));
// Make sure it is no longer invoked.
EXPECT_NO_THROW(mgr.trackAddLease(initializeLease(1, "192.0.2.1")));
EXPECT_EQ(1, logs_.size());
// Unregistering it again should be no-op.
EXPECT_NO_THROW(mgr.unregisterCallbacks(SUBNET_ID_GLOBAL, Lease::TYPE_V4));
}
/// Test unregistering all callbacks.
TEST_F(TrackingLeaseMgrTest, unregisterAllCallbacks) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
// Register some callbacks.
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_UPDATE_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_UPDATE_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
// Make sure they have been registered.
EXPECT_TRUE(mgr.hasCallbacks());
// Unregister them and make sure it was successful.
EXPECT_NO_THROW(mgr.unregisterAllCallbacks());
EXPECT_FALSE(mgr.hasCallbacks());
}
/// Test the function checking if any callbacks have been registered.
TEST_F(TrackingLeaseMgrTest, hasCallbacks) {
DatabaseConnection::ParameterMap pmap;
ConcreteLeaseMgr mgr(pmap);
EXPECT_FALSE(mgr.hasCallbacks());
EXPECT_NO_THROW(mgr.registerCallback(TrackingLeaseMgr::TRACK_ADD_LEASE, "flq",
SUBNET_ID_GLOBAL, Lease::TYPE_V4,
std::bind(&TrackingLeaseMgrTest::logCallback,
this,
TrackingLeaseMgr::TRACK_ADD_LEASE,
SUBNET_ID_GLOBAL, ph::_1)));
EXPECT_TRUE(mgr.hasCallbacks());
}
/// Test conversion of the callback type to string.
TEST_F(TrackingLeaseMgrTest, callbackTypeToString) {
EXPECT_EQ("add_lease", ConcreteLeaseMgr::callbackTypeToString(TrackingLeaseMgr::TRACK_ADD_LEASE));
EXPECT_EQ("update_lease", ConcreteLeaseMgr::callbackTypeToString(TrackingLeaseMgr::TRACK_UPDATE_LEASE));
EXPECT_EQ("delete_lease", ConcreteLeaseMgr::callbackTypeToString(TrackingLeaseMgr::TRACK_DELETE_LEASE));
}
} // end of anonymous namespace
|