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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (C) 2023-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file This file contains tests which exercise the PingContextStore class.

#include <config.h>
#include <ping_context_store.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <asiolink/io_address.h>
#include <testutils/gtest_utils.h>
#include <testutils/multi_threading_utils.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::ping_check;
using namespace isc::test;
using namespace std::chrono;

namespace {

/// @brief Text fixture class for @c PingContextStore
///
/// In order to facilitate single and multi threaded testing,
/// individual tests are implemented as methods that are called
/// from within TEST_F bodies rather than in TEST_F bodies.
class PingContextStoreTest : public ::testing::Test {
public:

    /// @brief Constructor
    PingContextStoreTest() {
    }

    /// @brief Destructor
    virtual ~PingContextStoreTest() = default;

    /// @brief Verifies that contexts can be added to the store given valid leases and queries.
    /// Also verifies that they can be fetched by address.
    void addContextTest() {
        PingContextStore store;
        PingContextPtr context;

        // Add three contexts, one for each lease/query.
        auto now = PingContext::now();
        for (int i = 0; i < leases_.size(); ++i) {
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 2, 300));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());

            // Check initial values.
            EXPECT_EQ(PingContext::WAITING_TO_SEND, context->getState());
            EXPECT_LE(now, context->getSendWaitStart());
            EXPECT_EQ(2, context->getMinEchos());
            EXPECT_EQ(300, context->getReplyTimeout());
        }

        // Make sure they can be fetched by address and by query individually.
        for (int i = 0; i < leases_.size(); ++i) {
            ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[i]->addr_));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());

            ASSERT_NO_THROW_LOG(context = store.getContextByQuery(queries_[i]));
            ASSERT_TRUE(context);
            EXPECT_EQ(queries_[i], context->getQuery());
        }
    }

    /// @brief Verifies that the store only allows once entry per IP address.
    void addContextDuplicateTest() {
        PingContextStore store;
        PingContextPtr context;

        ASSERT_NO_THROW_LOG(context = store.addContext(leases_[0], queries_[0], 1, 100));
        ASSERT_TRUE(context);
        ASSERT_THROW_MSG(store.addContext(leases_[0], queries_[0], 1, 100), DuplicateContext,
                         "PingContextStore::addContex: context already exists for: 192.0.2.1");
    }

    /// @brief Verify that addContext fails given invalid input.
    void addContextInvalidTest() {
        PingContextStore store;

        // Verify that given an empty lease the add will fail.
        Lease4Ptr empty_lease;
        ASSERT_THROW_MSG(store.addContext(empty_lease, queries_[0], 1, 100), BadValue,
                                          "PingContextStore::addContext failed:"
                                          " PingContext ctor - lease cannot be empty");

        // Verify that given an empty query the add will fail.
        Pkt4Ptr empty_query;
        ASSERT_THROW_MSG(store.addContext(leases_[0], empty_query, 1, 100), BadValue,
                                          "PingContextStore::addContext failed:"
                                          " PingContext ctor - query cannot be empty");
    }

    /// @brief Verify that contexts can be deleted from the store.
    void deleteContextTest() {
        PingContextStore store;

        // Add contexts to store.
        for (int i = 0; i < leases_.size(); ++i) {
            PingContextPtr context;
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 1, 100));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
        }

        // Fetch the second context.
        PingContextPtr orig_context;
        ASSERT_NO_THROW_LOG(orig_context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_TRUE(orig_context);
        EXPECT_EQ(leases_[1], orig_context->getLease());

        // Delete it.
        ASSERT_NO_THROW_LOG(store.deleteContext(orig_context));

        // Try to fetch it, shouldn't find it.
        PingContextPtr context;
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_FALSE(context);

        // Deleting it again should do no harm.
        ASSERT_NO_THROW_LOG(store.deleteContext(orig_context));
    }

    /// @brief Verify that contexts in the store can be updated.
    void updateContextTest() {
        PingContextStore store;
        PingContextPtr context;

        // Try to update a context that doesn't exist. It should throw.
        ASSERT_NO_THROW_LOG(context.reset(new PingContext(leases_[0], queries_[0])));
        ASSERT_THROW_MSG(store.updateContext(context), InvalidOperation,
                         "PingContextStore::updateContext failed for address:"
                         " 192.0.2.1, not in store");

        auto test_start = PingContext::now();

        // Add contexts to store.
        for (int i = 0; i < leases_.size(); ++i) {
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 1, 100));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
        }

        // Fetch the second context.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_TRUE(context);
        ASSERT_EQ(leases_[1], context->getLease());
        ASSERT_EQ(queries_[1], context->getQuery());

        // Check initial values for state and expiration.
        EXPECT_EQ(PingContext::WAITING_TO_SEND, context->getState());
        EXPECT_LE(test_start, context->getSendWaitStart());
        EXPECT_LE(PingContext::EMPTY_TIME(), context->getNextExpiry());

        // Modify the state and expiration, then update the context.
        auto wait_start = PingContext::now();
        context->beginWaitingForReply(wait_start);
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Fetch the context and verify the values are correct.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_TRUE(context);
        EXPECT_EQ(PingContext::WAITING_FOR_REPLY, context->getState());
        EXPECT_LE(wait_start + milliseconds(context->getReplyTimeout()), context->getNextExpiry());
    }

    /// @brief Verify that contexts can be fetched based on when they entered WAITING_TO_SEND
    /// by getNextToSend().
    void getNextToSendTest() {
        PingContextStore store;
        PingContextPtr context;

        // Capture time now.
        auto start_time = PingContext::now();

        // Add contexts to store.
        for (int i = 0; i < leases_.size(); ++i) {
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 1, 100));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
            usleep(1000);
        }

        // Fetching the next context to send should return the first context as
        // it has the oldest send wait start time.
        context.reset();
        ASSERT_NO_THROW(context = store.getNextToSend());
        ASSERT_TRUE(context);
        EXPECT_EQ(leases_[0], context->getLease());
        EXPECT_EQ(queries_[0], context->getQuery());
        EXPECT_LE(start_time, context->getSendWaitStart());

        // Update the first context's state  to TARGET_FREE which should
        // disqualify it from being returned as next to send.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[0]->addr_));
        ASSERT_TRUE(context);
        ASSERT_EQ(PingContext::WAITING_TO_SEND, context->getState());
        context->setState(PingContext::TARGET_FREE);
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Update the send wait start of the second context making it the
        // youngest send wait start time.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_TRUE(context);
        ASSERT_EQ(PingContext::WAITING_TO_SEND, context->getState());
        context->setSendWaitStart(start_time + milliseconds(1000));
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Update the send wait start of the third context, making it the oldest.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[2]->addr_));
        ASSERT_TRUE(context);
        ASSERT_EQ(PingContext::WAITING_TO_SEND, context->getState());
        context->setSendWaitStart(start_time + milliseconds(500));
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Fetching the next context to send should return the third context.
        context.reset();
        ASSERT_NO_THROW(context = store.getNextToSend());
        ASSERT_TRUE(context);
        EXPECT_EQ(leases_[2], context->getLease());
        EXPECT_EQ(queries_[2], context->getQuery());
        EXPECT_EQ(start_time + milliseconds(500), context->getSendWaitStart());
    }

    /// @brief Verify that contexts can be fetched based on when they expire using
    /// getExpiresNext() and getExpiredSince().
    void getByExpirationTest() {
        PingContextStore store;
        PingContextPtr context;

        // Add contexts to store.
        for (int i = 0; i < leases_.size(); ++i) {
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 1, 100));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
        }

        // Capture time now.
        auto start_time = PingContext::now();

        // Update the state and expiration of the first context.
        // State set to TARGET_FREE should disqualify if from
        // fetch by expiration even though it has the soonest expiration
        // time.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[0]->addr_));
        ASSERT_TRUE(context);
        context->setState(PingContext::TARGET_FREE);
        context->setNextExpiry(start_time + milliseconds(1));
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Update the state and expiration of the second context giving it
        // the youngest expiration time.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[1]->addr_));
        ASSERT_TRUE(context);
        context->setState(PingContext::WAITING_FOR_REPLY);
        context->setNextExpiry(start_time + milliseconds(1000));
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Update the state and expiration of the third context, make it the
        // soonest qualified expiration time.
        ASSERT_NO_THROW_LOG(context = store.getContextByAddress(leases_[2]->addr_));
        ASSERT_TRUE(context);
        context->setState(PingContext::WAITING_FOR_REPLY);
        context->setNextExpiry(start_time + milliseconds(500));
        ASSERT_NO_THROW_LOG(store.updateContext(context));

        // Fetching the context that expires next should return the third context.
        context.reset();
        ASSERT_NO_THROW(context = store.getExpiresNext());
        ASSERT_TRUE(context);
        EXPECT_EQ(leases_[2], context->getLease());
        EXPECT_EQ(queries_[2], context->getQuery());
        EXPECT_EQ(start_time + milliseconds(500), context->getNextExpiry());

        // Fetch all that have expired since current time.  Should be none.
        PingContextCollectionPtr expired_since;
        ASSERT_NO_THROW_LOG(expired_since = store.getExpiredSince());
        ASSERT_TRUE(expired_since);
        EXPECT_EQ(0, expired_since->size());

        // Fetch all that have expired since start time + 750 ms, should be third context.
        ASSERT_NO_THROW_LOG(expired_since = store.getExpiredSince(start_time + milliseconds(750)));
        ASSERT_TRUE(expired_since);
        EXPECT_EQ(1, expired_since->size());
        context = (*expired_since)[0];
        EXPECT_EQ(leases_[2], context->getLease());
        EXPECT_EQ(queries_[2], context->getQuery());
        EXPECT_EQ(start_time + milliseconds(500), context->getNextExpiry());

        // Fetch all that have expired since start time + 1500 ms
        // Should be the third and second contexts
        ASSERT_NO_THROW_LOG(expired_since = store.getExpiredSince(start_time + milliseconds(1500)));
        ASSERT_TRUE(expired_since);
        EXPECT_EQ(2, expired_since->size());

        // First in list should be the third context.
        context = (*expired_since)[0];
        EXPECT_EQ(leases_[2], context->getLease());
        EXPECT_EQ(queries_[2], context->getQuery());
        EXPECT_EQ(start_time + milliseconds(500), context->getNextExpiry());

        // The last one in the list should be the second context.
        context = (*expired_since)[1];
        EXPECT_EQ(leases_[1], context->getLease());
        EXPECT_EQ(queries_[1], context->getQuery());
        EXPECT_EQ(start_time + milliseconds(1000), context->getNextExpiry());
    }

    /// @brief Verifies that getAll() and clear() work properly.
    void getAllAndClearTest() {
        PingContextStore store;

        // Add contexts to store.
        for (int i = 0; i < leases_.size(); ++i) {
            PingContextPtr context;
            ASSERT_NO_THROW_LOG(context = store.addContext(leases_[i], queries_[i], 1, 100));
            ASSERT_TRUE(context);
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
        }

        // Fetch them all.
        PingContextCollectionPtr contexts;
        ASSERT_NO_THROW_LOG(contexts = store.getAll());
        ASSERT_EQ(leases_.size(), contexts->size());

        // Verify we got them all in order.
        int i = 0;
        for (auto const& context : *contexts) {
            EXPECT_EQ(leases_[i], context->getLease());
            EXPECT_EQ(queries_[i], context->getQuery());
            ++i;
        }

        // Now clear the store. Verify it's empty.
        ASSERT_NO_THROW_LOG(store.clear());
        ASSERT_NO_THROW_LOG(contexts = store.getAll());
        ASSERT_EQ(0, contexts->size());

        // Verify clearing an empty store does no harm.
        ASSERT_NO_THROW_LOG(store.clear());
    }

private:
    /// @brief Prepares the class for a test.
    virtual void SetUp() {
        Lease4Ptr lease;
        lease.reset(new Lease4());
        lease->addr_ = IOAddress("192.0.2.1");
        leases_.push_back(lease);

        lease.reset(new Lease4());
        lease->addr_ = IOAddress("192.0.2.2");
        leases_.push_back(lease);

        lease.reset(new Lease4());
        lease->addr_ = IOAddress("192.0.2.3");
        leases_.push_back(lease);

        Pkt4Ptr query;
        query.reset(new Pkt4(DHCPDISCOVER, 101));
        queries_.push_back(query);

        query.reset(new Pkt4(DHCPDISCOVER, 102));
        queries_.push_back(query);

        query.reset(new Pkt4(DHCPDISCOVER, 103));
        queries_.push_back(query);

        ASSERT_EQ(leases_.size(), queries_.size());
    }

public:
    /// @brief List of pre-made leases.
    std::vector<Lease4Ptr> leases_;

    /// @brief List of pre-made queries.
    std::vector<Pkt4Ptr> queries_;
};

TEST_F(PingContextStoreTest, addContext) {<--- syntax error
    addContextTest();
}

TEST_F(PingContextStoreTest, addContextMultiThreading) {
    MultiThreadingTest mt;
    addContextTest();
}

TEST_F(PingContextStoreTest, addContextDuplicate) {
    addContextDuplicateTest();
}

TEST_F(PingContextStoreTest, addContextDuplicateMultiThreading) {
    MultiThreadingTest mt;
    addContextDuplicateTest();
}

TEST_F(PingContextStoreTest, addContextInvalid) {
    addContextInvalidTest();
}

TEST_F(PingContextStoreTest, addContextInvalidMultiThreading) {
    MultiThreadingTest mt;
    addContextInvalidTest();
}

TEST_F(PingContextStoreTest, deleteContext) {
    deleteContextTest();
}

TEST_F(PingContextStoreTest, deleteContextMultiThreading) {
    MultiThreadingTest mt;
    deleteContextTest();
}

TEST_F(PingContextStoreTest, updateContext) {
    updateContextTest();
}

TEST_F(PingContextStoreTest, updateContextMultiThreading) {
    MultiThreadingTest mt;
    updateContextTest();
}

TEST_F(PingContextStoreTest, getNextToSend) {
    getNextToSendTest();
}

TEST_F(PingContextStoreTest, getNextToSendMultiThreading) {
    MultiThreadingTest mt;
    getNextToSendTest();
}

TEST_F(PingContextStoreTest, getByExpiration) {
    getByExpirationTest();
}

TEST_F(PingContextStoreTest, getByExpirationMultiThreading) {
    MultiThreadingTest mt;
    getByExpirationTest();
}

TEST_F(PingContextStoreTest, getAllAndClear) {
    getAllAndClearTest();
}

TEST_F(PingContextStoreTest, getAllAndClearMultiThreading) {
    MultiThreadingTest mt;
    getAllAndClearTest();
}

} // end of anonymous namespace