hooks_unittest.cc 75 KB
Newer Older
1
// Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3
4
5
// 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/.
6
7
8
9
10
11

#include <config.h>

#include <asiolink/io_address.h>
#include <dhcp/dhcp6.h>
#include <dhcp/duid.h>
12
#include <dhcp6/json_config_parser.h>
13
14
15
16
17
18
19
20
21
22
#include <dhcp/dhcp6.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/utils.h>
#include <util/buffer.h>
#include <util/range_utilities.h>
#include <hooks/server_hooks.h>

#include <dhcp6/tests/dhcp6_test_utils.h>
23
#include <dhcp6/tests/dhcp6_client.h>
24
#include <dhcp/tests/iface_mgr_test_config.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
25
#include <dhcp/tests/pkt_captures.h>
26
#include <cc/command_interpreter.h>
27
28
#include <dhcp6/tests/marker_file.h>
#include <dhcp6/tests/test_libraries.h>
29

30
31
32
33
34
35
36
37
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <sstream>

using namespace isc;
38
using namespace isc::data;
39
using namespace isc::dhcp::test;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::util;
using namespace isc::hooks;
using namespace std;

// namespace has to be named, because friends are defined in Dhcpv6Srv class
// Maybe it should be isc::test?
namespace {

// Checks if hooks are implemented properly.
TEST_F(Dhcpv6SrvTest, Hooks) {
    NakedDhcpv6Srv srv(0);

    // check if appropriate hooks are registered
55
56
57
58
    int hook_index_buffer6_receive = -1;
    int hook_index_buffer6_send    = -1;
    int hook_index_lease6_renew    = -1;
    int hook_index_lease6_release  = -1;
59
60
    int hook_index_lease6_rebind   = -1;
    int hook_index_lease6_decline  = -1;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
61
62
63
    int hook_index_pkt6_received   = -1;
    int hook_index_select_subnet   = -1;
    int hook_index_pkt6_send       = -1;
64
65

    // check if appropriate indexes are set
66
67
68
69
70
71
72
73
    EXPECT_NO_THROW(hook_index_buffer6_receive = ServerHooks::getServerHooks()
                    .getIndex("buffer6_receive"));
    EXPECT_NO_THROW(hook_index_buffer6_send = ServerHooks::getServerHooks()
                    .getIndex("buffer6_send"));
    EXPECT_NO_THROW(hook_index_lease6_renew = ServerHooks::getServerHooks()
                    .getIndex("lease6_renew"));
    EXPECT_NO_THROW(hook_index_lease6_release = ServerHooks::getServerHooks()
                    .getIndex("lease6_release"));
74
75
76
77
    EXPECT_NO_THROW(hook_index_lease6_rebind = ServerHooks::getServerHooks()
                    .getIndex("lease6_rebind"));
    EXPECT_NO_THROW(hook_index_lease6_decline = ServerHooks::getServerHooks()
                    .getIndex("lease6_decline"));
78
79
80
81
82
83
84
    EXPECT_NO_THROW(hook_index_pkt6_received = ServerHooks::getServerHooks()
                    .getIndex("pkt6_receive"));
    EXPECT_NO_THROW(hook_index_select_subnet = ServerHooks::getServerHooks()
                    .getIndex("subnet6_select"));
    EXPECT_NO_THROW(hook_index_pkt6_send     = ServerHooks::getServerHooks()
                    .getIndex("pkt6_send"));

Tomek Mrugalski's avatar
Tomek Mrugalski committed
85
86
87
    EXPECT_TRUE(hook_index_pkt6_received   > 0);
    EXPECT_TRUE(hook_index_select_subnet   > 0);
    EXPECT_TRUE(hook_index_pkt6_send       > 0);
88
    EXPECT_TRUE(hook_index_buffer6_receive > 0);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
89
90
91
    EXPECT_TRUE(hook_index_buffer6_send    > 0);
    EXPECT_TRUE(hook_index_lease6_renew    > 0);
    EXPECT_TRUE(hook_index_lease6_release  > 0);
92
93
    EXPECT_TRUE(hook_index_lease6_rebind   > 0);
    EXPECT_TRUE(hook_index_lease6_decline  > 0);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
}

/// @brief a class dedicated to Hooks testing in DHCPv6 server
///
/// This class has a number of static members, because each non-static
/// method has implicit 'this' parameter, so it does not match callout
/// signature and couldn't be registered. Furthermore, static methods
/// can't modify non-static members (for obvious reasons), so many
/// fields are declared static. It is still better to keep them as
/// one class rather than unrelated collection of global objects.
class HooksDhcpv6SrvTest : public Dhcpv6SrvTest {

public:

    /// @brief creates Dhcpv6Srv and prepares buffers for callouts
    HooksDhcpv6SrvTest() {

        // Allocate new DHCPv6 Server
112
        srv_.reset(new NakedDhcpv6Srv(0));
113

Tomek Mrugalski's avatar
Tomek Mrugalski committed
114
        // Clear static buffers
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        resetCalloutBuffers();
    }

    /// @brief destructor (deletes Dhcpv6Srv)
    ~HooksDhcpv6SrvTest() {
    }

    /// @brief creates an option with specified option code
    ///
    /// This method is static, because it is used from callouts
    /// that do not have a pointer to HooksDhcpv6SSrvTest object
    ///
    /// @param option_code code of option to be created
    ///
    /// @return pointer to create option object
    static OptionPtr createOption(uint16_t option_code) {

Tomek Mrugalski's avatar
Tomek Mrugalski committed
132
        uint8_t payload[] = {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
            0xa, 0xb, 0xc, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14
        };

        OptionBuffer tmp(payload, payload + sizeof(payload));
        return OptionPtr(new Option(Option::V6, option_code, tmp));
    }

    /// test callback that stores received callout name and pkt6 value
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_receive_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("pkt6_receive");

147
        callout_handle.getArgument("query6", callback_qry_pkt6_);
148
149
150
151
152
153
154
155
156
157
158
159
160
161

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    /// test callback that changes client-id value
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_receive_change_clientid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("query6", pkt);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
162
        // Get rid of the old client-id
163
164
        pkt->delOption(D6O_CLIENTID);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
165
        // Add a new option
166
167
        pkt->addOption(createOption(D6O_CLIENTID));

Tomek Mrugalski's avatar
Tomek Mrugalski committed
168
        // Carry on as usual
169
170
171
        return pkt6_receive_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
172
    /// Test callback that deletes client-id
173
174
175
176
177
178
179
180
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_receive_delete_clientid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("query6", pkt);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
181
        // Get rid of the old client-id
182
183
        pkt->delOption(D6O_CLIENTID);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
184
        // Carry on as usual
185
186
187
        return pkt6_receive_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
188
    /// Test callback that sets skip flag
189
190
191
192
193
194
195
196
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_receive_skip(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("query6", pkt);

197
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
198

Tomek Mrugalski's avatar
Tomek Mrugalski committed
199
        // Carry on as usual
200
201
202
        return pkt6_receive_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
203
    /// Test callback that stores received callout name and pkt6 value
204
205
206
207
208
209
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    buffer6_receive_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("buffer6_receive");

210
        callout_handle.getArgument("query6", callback_qry_pkt6_);
211
212
213
214
215

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
216
    /// Test callback that changes first byte of client-id value
217
218
219
220
221
222
223
224
225
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    buffer6_receive_change_clientid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("query6", pkt);

        // If there is at least one option with data
Tomek Mrugalski's avatar
Tomek Mrugalski committed
226
227
228
229
        if (pkt->data_.size() > Pkt6::DHCPV6_PKT_HDR_LEN + Option::OPTION6_HDR_LEN) {
            // Offset of the first byte of the first option. Let's set this byte
            // to some new value that we could later check
            pkt->data_[Pkt6::DHCPV6_PKT_HDR_LEN + Option::OPTION6_HDR_LEN] = 0xff;
230
231
        }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
232
        // Carry on as usual
233
234
235
        return buffer6_receive_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
236
    /// Test callback that deletes client-id
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
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    buffer6_receive_delete_clientid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("query6", pkt);

        // this is modified SOLICIT (with missing mandatory client-id)
        uint8_t data[] = {
        1,  // type 1 = SOLICIT
        0xca, 0xfe, 0x01, // trans-id = 0xcafe01
        0, 3, // option type 3 (IA_NA)
        0, 12, // option length 12
        0, 0, 0, 1, // iaid = 1
        0, 0, 0, 0, // T1 = 0
        0, 0, 0, 0  // T2 = 0
        };

        OptionBuffer modifiedMsg(data, data + sizeof(data));

        pkt->data_ = modifiedMsg;

        // carry on as usual
        return buffer6_receive_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
264
    /// Test callback that sets skip flag
265
266
267
268
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    buffer6_receive_skip(CalloutHandle& callout_handle) {
269
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
270

Tomek Mrugalski's avatar
Tomek Mrugalski committed
271
        // Carry on as usual
272
273
274
        return buffer6_receive_callout(callout_handle);
    }

275
276
277
278
279
280
281
    /// Test callback that stores received callout name and pkt6 value
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_send_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("pkt6_send");

282
283
284
        callout_handle.getArgument("response6", callback_resp_pkt6_);

        callout_handle.getArgument("query6", callback_qry_pkt6_);
285
286
287
288
289
290
291
292
293
294
295
296
297
298

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    // Test callback that changes server-id
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_send_change_serverid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("response6", pkt);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
299
        // Get rid of the old server-id
300
301
        pkt->delOption(D6O_SERVERID);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
302
        // Add a new option
303
304
        pkt->addOption(createOption(D6O_SERVERID));

Tomek Mrugalski's avatar
Tomek Mrugalski committed
305
        // Carry on as usual
306
307
308
        return pkt6_send_callout(callout_handle);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
309
    /// Test callback that deletes server-id
310
311
312
313
314
315
316
317
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_send_delete_serverid(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("response6", pkt);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
318
        // Get rid of the old client-id
319
320
        pkt->delOption(D6O_SERVERID);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
321
        // Carry on as usual
322
323
324
325
326
327
328
329
330
331
332
333
        return pkt6_send_callout(callout_handle);
    }

    /// Test callback that sets skip flag
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    pkt6_send_skip(CalloutHandle& callout_handle) {

        Pkt6Ptr pkt;
        callout_handle.getArgument("response6", pkt);

334
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
335
336
337
338
339
340
341
342
343
344
345
346

        // carry on as usual
        return pkt6_send_callout(callout_handle);
    }

    /// Test callback that stores received callout name and subnet6 values
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    subnet6_select_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("subnet6_select");

347
        callout_handle.getArgument("query6", callback_qry_pkt6_);
348
349
350
351
352
353
354
355
356
357
358
359
360
        callout_handle.getArgument("subnet6", callback_subnet6_);
        callout_handle.getArgument("subnet6collection", callback_subnet6collection_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    /// Test callback that picks the other subnet if possible.
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    subnet6_select_different_subnet_callout(CalloutHandle& callout_handle) {

Francis Dupont's avatar
Francis Dupont committed
361
        // Call the basic callout to record all passed values
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        subnet6_select_callout(callout_handle);

        const Subnet6Collection* subnets;
        Subnet6Ptr subnet;
        callout_handle.getArgument("subnet6", subnet);
        callout_handle.getArgument("subnet6collection", subnets);

        // Let's change to a different subnet
        if (subnets->size() > 1) {
            subnet = (*subnets)[1]; // Let's pick the other subnet
            callout_handle.setArgument("subnet6", subnet);
        }

        return (0);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
378
    /// Test callback that stores received callout name and pkt6 value
379
380
381
382
383
384
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_renew_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_renew");

385
        callout_handle.getArgument("query6", callback_qry_pkt6_);
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
        callout_handle.getArgument("lease6", callback_lease6_);
        callout_handle.getArgument("ia_na", callback_ia_na_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    /// The following values are used by the callout to override
    /// renewed lease parameters
    static const uint32_t override_iaid_;
    static const uint32_t override_t1_;
    static const uint32_t override_t2_;
    static const uint32_t override_preferred_;
    static const uint32_t override_valid_;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
401
    /// Test callback that overrides received lease. It updates
402
403
404
405
406
407
408
    /// T1, T2, preferred and valid lifetimes
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_renew_update_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_renew");

409
        callout_handle.getArgument("query6", callback_qry_pkt6_);
410
411
412
        callout_handle.getArgument("lease6", callback_lease6_);
        callout_handle.getArgument("ia_na", callback_ia_na_);

413
414
        // Should be an ASSERT but it is not allowed here
        EXPECT_TRUE(callback_lease6_);
415
416
417
418
419
420
421
        // Let's override some values in the lease
        callback_lease6_->iaid_          = override_iaid_;
        callback_lease6_->t1_            = override_t1_;
        callback_lease6_->t2_            = override_t2_;
        callback_lease6_->preferred_lft_ = override_preferred_;
        callback_lease6_->valid_lft_     = override_valid_;

422
423
        // Should be an ASSERT but it is not allowed here
        EXPECT_TRUE(callback_ia_na_);
424
425
426
427
428
429
430
431
432
        // Override the values to be sent to the client as well
        callback_ia_na_->setIAID(override_iaid_);
        callback_ia_na_->setT1(override_t1_);
        callback_ia_na_->setT2(override_t2_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
433
    /// Test callback that sets the skip flag
434
435
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
436
437
438
439
    static int
    lease6_renew_skip_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_renew");

440
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
441
442
443
444

        return (0);
    }

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    /// Test callback that stores received callout name and pkt6 value
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_rebind_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_rebind");

        callout_handle.getArgument("query6", callback_qry_pkt6_);
        callout_handle.getArgument("lease6", callback_lease6_);
        callout_handle.getArgument("ia_na", callback_ia_na_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    /// Test callback that overrides received lease. It updates
    /// T1, T2, preferred and valid lifetimes
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_rebind_update_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_rebind");

        callout_handle.getArgument("query6", callback_qry_pkt6_);
        callout_handle.getArgument("lease6", callback_lease6_);
        callout_handle.getArgument("ia_na", callback_ia_na_);

472
473
        // Should be an ASSERT but it is not allowed here
        EXPECT_TRUE(callback_lease6_);
474
475
476
477
478
479
480
        // Let's override some values in the lease
        callback_lease6_->iaid_          = override_iaid_;
        callback_lease6_->t1_            = override_t1_;
        callback_lease6_->t2_            = override_t2_;
        callback_lease6_->preferred_lft_ = override_preferred_;
        callback_lease6_->valid_lft_     = override_valid_;

481
482
        // Should be an ASSERT but it is not allowed here
        EXPECT_TRUE(callback_ia_na_);
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        // Override the values to be sent to the client as well
        callback_ia_na_->setIAID(override_iaid_);
        callback_ia_na_->setT1(override_t1_);
        callback_ia_na_->setT2(override_t2_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

    /// Lease6_rebind callout that sets status to SKIP
    ///
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_rebind_skip_callout(CalloutHandle& callout_handle) {
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);

        return (lease6_rebind_callout(callout_handle));
    }

    /// Lease6_rebind callout that sets status to DROP
    ///
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_rebind_drop_callout(CalloutHandle& callout_handle) {
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_DROP);

        return (lease6_rebind_callout(callout_handle));
    }


Tomek Mrugalski's avatar
Tomek Mrugalski committed
515
    /// Test callback that stores received callout name passed parameters
516
517
518
519
520
521
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_release_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_release");

522
        callout_handle.getArgument("query6", callback_qry_pkt6_);
523
524
525
526
527
528
        callout_handle.getArgument("lease6", callback_lease6_);

        callback_argument_names_ = callout_handle.getArgumentNames();
        return (0);
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
529
    /// Test callback that sets the skip flag
530
531
532
533
534
535
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_release_skip_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_release");

536
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
537
538
539
540

        return (0);
    }

541
542
543
544
545
546
547
548
549
    /// Lease6_decline test callback
    ///
    /// Stores all parameters in callback_* fields.
    ///
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_decline_callout(CalloutHandle& callout_handle) {
        callback_name_ = string("lease6_decline");
550
        callout_handle.getArgument("query6", callback_qry_pkt6_);
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
        callout_handle.getArgument("lease6", callback_lease6_);

        return (0);
    }

    /// Lease6_decline callout that sets status to SKIP
    ///
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_decline_skip_callout(CalloutHandle& callout_handle) {
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);

        return (lease6_decline_callout(callout_handle));
    }

    /// Lease6_decline callout that sets status to DROP
    ///
    /// @param callout_handle handle passed by the hooks framework
    /// @return always 0
    static int
    lease6_decline_drop_callout(CalloutHandle& callout_handle) {
        callout_handle.setStatus(CalloutHandle::NEXT_STEP_DROP);

        return (lease6_decline_callout(callout_handle));
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
578
    /// Resets buffers used to store data received by callouts
579
580
    void resetCalloutBuffers() {
        callback_name_ = string("");
581
582
        callback_qry_pkt6_.reset();
        callback_resp_pkt6_.reset();
583
        callback_subnet6_.reset();
584
585
        callback_lease6_.reset();
        callback_ia_na_.reset();
586
587
588
589
        callback_subnet6collection_ = NULL;
        callback_argument_names_.clear();
    }

Tomek Mrugalski's avatar
Tomek Mrugalski committed
590
    /// Pointer to Dhcpv6Srv that is used in tests
591
    boost::scoped_ptr<NakedDhcpv6Srv> srv_;
592
593
594
595
596
597

    // The following fields are used in testing pkt6_receive_callout

    /// String name of the received callout
    static string callback_name_;

598
599
600
601
602
    /// Client's query Pkt6 structure returned in the callout
    static Pkt6Ptr callback_qry_pkt6_;

    /// Server's response Pkt6 structure returned in the callout
    static Pkt6Ptr callback_resp_pkt6_;
603

604
605
606
    /// Pointer to lease6
    static Lease6Ptr callback_lease6_;

607
    /// Pointer to IA_NA option being renewed or rebound
608
609
    static boost::shared_ptr<Option6IA> callback_ia_na_;

610
611
612
613
614
615
616
617
618
619
    /// Pointer to a subnet received by callout
    static Subnet6Ptr callback_subnet6_;

    /// A list of all available subnets (received by callout)
    static const Subnet6Collection* callback_subnet6collection_;

    /// A list of all received arguments
    static vector<string> callback_argument_names_;
};

620
621
622
623
624
625
626
627
// The following parameters are used by callouts to override
// renewed lease parameters
const uint32_t HooksDhcpv6SrvTest::override_iaid_ = 1000;
const uint32_t HooksDhcpv6SrvTest::override_t1_ = 1001;
const uint32_t HooksDhcpv6SrvTest::override_t2_ = 1002;
const uint32_t HooksDhcpv6SrvTest::override_preferred_ = 1003;
const uint32_t HooksDhcpv6SrvTest::override_valid_ = 1004;

628
629
630
// The following fields are used in testing pkt6_receive_callout.
// See fields description in the class for details
string HooksDhcpv6SrvTest::callback_name_;
631
632
Pkt6Ptr HooksDhcpv6SrvTest::callback_qry_pkt6_;
Pkt6Ptr HooksDhcpv6SrvTest::callback_resp_pkt6_;
633
634
635
Subnet6Ptr HooksDhcpv6SrvTest::callback_subnet6_;
const Subnet6Collection* HooksDhcpv6SrvTest::callback_subnet6collection_;
vector<string> HooksDhcpv6SrvTest::callback_argument_names_;
636
637
Lease6Ptr HooksDhcpv6SrvTest::callback_lease6_;
boost::shared_ptr<Option6IA> HooksDhcpv6SrvTest::callback_ia_na_;
638

639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/// @brief Fixture class used to do basic library load/unload tests
class LoadUnloadDhcpv6SrvTest : public ::testing::Test {
public:
    /// @brief Pointer to the tested server object
    boost::shared_ptr<NakedDhcpv6Srv> server_;

    LoadUnloadDhcpv6SrvTest() {
        reset();
    }

    /// @brief Destructor
    ~LoadUnloadDhcpv6SrvTest() {
        server_.reset();
        reset();
    };

    /// @brief Reset hooks data
    ///
    /// Resets the data for the hooks-related portion of the test by ensuring
    /// that no libraries are loaded and that any marker files are deleted.
    void reset() {
        // Unload any previously-loaded libraries.
        HooksManager::unloadLibraries();

        // Get rid of any marker files.
        static_cast<void>(remove(LOAD_MARKER_FILE));
        static_cast<void>(remove(UNLOAD_MARKER_FILE));

        CfgMgr::instance().clear();
    }
};

671
672
673
674
675
// Checks if callouts installed on pkt6_receive are indeed called and the
// all necessary parameters are passed.
//
// Note that the test name does not follow test naming convention,
// but the proper hook name is "buffer6_receive".
Tomek Mrugalski's avatar
Tomek Mrugalski committed
676
TEST_F(HooksDhcpv6SrvTest, simpleBuffer6Receive) {
677
678
679
680
681
682

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "buffer6_receive", buffer6_receive_callout));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
683
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
684
685
686
687
688
689
690

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
691
    // In particular, it should call registered buffer6_receive callback.
692
693
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
694
    // Check that the callback called is indeed the one we installed
695
696
    EXPECT_EQ("buffer6_receive", callback_name_);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
697
    // Check that pkt6 argument passing was successful and returned proper value
698
    EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
699
700
701
702
703
704
705
706

    // Check that all expected parameters are there
    vector<string> expected_argument_names;
    expected_argument_names.push_back(string("query6"));

    EXPECT_TRUE(expected_argument_names == callback_argument_names_);
}

707
// Checks if callouts installed on buffer6_receive is able to change
708
// the values and the parameters are indeed used by the server.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
709
TEST_F(HooksDhcpv6SrvTest, valueChangeBuffer6Receive) {
710
711
712
713
714
715

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "buffer6_receive", buffer6_receive_change_clientid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
716
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
717
718
719
720
721
722
723
724
725
726

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Francis Dupont's avatar
Francis Dupont committed
727
    // Check that the server did send a response
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
    ASSERT_EQ(1, srv_->fake_sent_.size());

    // Make sure that we received a response
    Pkt6Ptr adv = srv_->fake_sent_.front();
    ASSERT_TRUE(adv);

    // Get client-id...
    OptionPtr clientid = adv->getOption(D6O_CLIENTID);

    ASSERT_TRUE(clientid);

    // ... and check if it is the modified value
    EXPECT_EQ(0xff, clientid->getData()[0]);
}

// Checks if callouts installed on buffer6_receive is able to delete
// existing options and that change impacts server processing (mandatory
// client-id option is deleted, so the packet is expected to be dropped)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
746
TEST_F(HooksDhcpv6SrvTest, deleteClientIdBuffer6Receive) {
747
748
749
750
751
752

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "buffer6_receive", buffer6_receive_delete_clientid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
753
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

    // Check that the server dropped the packet and did not send a response
    ASSERT_EQ(0, srv_->fake_sent_.size());
}

// Checks if callouts installed on buffer6_received is able to set skip flag that
// will cause the server to not process the packet (drop), even though it is valid.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
770
TEST_F(HooksDhcpv6SrvTest, skipBuffer6Receive) {
771
772
773
774
775
776

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "buffer6_receive", buffer6_receive_skip));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
777
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
778
779
780
781
782
783
784
785
786
787

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
788
    // Check that the server dropped the packet and did not produce any response
789
790
791
792
    ASSERT_EQ(0, srv_->fake_sent_.size());
}

// Checks if callouts installed on pkt6_receive are indeed called and the
793
794
795
796
// all necessary parameters are passed.
//
// Note that the test name does not follow test naming convention,
// but the proper hook name is "pkt6_receive".
Tomek Mrugalski's avatar
Tomek Mrugalski committed
797
TEST_F(HooksDhcpv6SrvTest, simplePkt6Receive) {
798
799
800
801
802
803

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_receive", pkt6_receive_callout));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
804
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
805
806
807
808
809
810
811
812
813
814

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
815
    // Check that the callback called is indeed the one we installed
816
817
    EXPECT_EQ("pkt6_receive", callback_name_);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
818
    // Check that pkt6 argument passing was successful and returned proper value
819
    EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
820
821
822
823
824
825
826
827
828
829

    // Check that all expected parameters are there
    vector<string> expected_argument_names;
    expected_argument_names.push_back(string("query6"));

    EXPECT_TRUE(expected_argument_names == callback_argument_names_);
}

// Checks if callouts installed on pkt6_received is able to change
// the values and the parameters are indeed used by the server.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
830
TEST_F(HooksDhcpv6SrvTest, valueChangePkt6Receive) {
831
832
833
834
835
836

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_receive", pkt6_receive_change_clientid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
837
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
838
839
840
841
842
843
844
845
846
847

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Francis Dupont's avatar
Francis Dupont committed
848
    // Check that the server did send a response
849
850
851
852
853
854
855
856
857
858
859
    ASSERT_EQ(1, srv_->fake_sent_.size());

    // Make sure that we received a response
    Pkt6Ptr adv = srv_->fake_sent_.front();
    ASSERT_TRUE(adv);

    // Get client-id...
    OptionPtr clientid = adv->getOption(D6O_CLIENTID);

    // ... and check if it is the modified value
    OptionPtr expected = createOption(D6O_CLIENTID);
860
    EXPECT_TRUE(clientid->equals(expected));
861
862
863
864
865
}

// Checks if callouts installed on pkt6_received is able to delete
// existing options and that change impacts server processing (mandatory
// client-id option is deleted, so the packet is expected to be dropped)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
866
TEST_F(HooksDhcpv6SrvTest, deleteClientIdPkt6Receive) {
867
868
869
870
871
872

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_receive", pkt6_receive_delete_clientid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
873
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

    // Check that the server dropped the packet and did not send a response
    ASSERT_EQ(0, srv_->fake_sent_.size());
}

// Checks if callouts installed on pkt6_received is able to set skip flag that
// will cause the server to not process the packet (drop), even though it is valid.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
890
TEST_F(HooksDhcpv6SrvTest, skipPkt6Receive) {
891
892
893
894
895
896

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_receive", pkt6_receive_skip));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
897
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
898
899
900
901
902
903
904
905
906
907

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
908
    // Check that the server dropped the packet and did not produce any response
909
910
911
912
913
914
    ASSERT_EQ(0, srv_->fake_sent_.size());
}


// Checks if callouts installed on pkt6_send are indeed called and the
// all necessary parameters are passed.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
915
TEST_F(HooksDhcpv6SrvTest, simplePkt6Send) {
916
917
918
919
920
921

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_send", pkt6_send_callout));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
922
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

    // Check that the callback called is indeed the one we installed
    EXPECT_EQ("pkt6_send", callback_name_);

    // Check that there is one packet sent
    ASSERT_EQ(1, srv_->fake_sent_.size());
    Pkt6Ptr adv = srv_->fake_sent_.front();

940
941
942
943
    // Check that pkt6 argument passing was successful and returned proper
    // values
    EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
    EXPECT_TRUE(callback_resp_pkt6_.get() == adv.get());
944
945
946

    // Check that all expected parameters are there
    vector<string> expected_argument_names;
947
    expected_argument_names.push_back(string("query6"));
948
949
950
951
952
953
    expected_argument_names.push_back(string("response6"));
    EXPECT_TRUE(expected_argument_names == callback_argument_names_);
}

// Checks if callouts installed on pkt6_send is able to change
// the values and the packet sent contains those changes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
954
TEST_F(HooksDhcpv6SrvTest, valueChangePkt6Send) {
955
956
957
958
959
960

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_send", pkt6_send_change_serverid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
961
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
962
963
964
965
966
967
968
969
970
971

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
972
    // Check that the server did send a response
973
974
975
976
977
978
979
980
981
982
983
    ASSERT_EQ(1, srv_->fake_sent_.size());

    // Make sure that we received a response
    Pkt6Ptr adv = srv_->fake_sent_.front();
    ASSERT_TRUE(adv);

    // Get client-id...
    OptionPtr clientid = adv->getOption(D6O_SERVERID);

    // ... and check if it is the modified value
    OptionPtr expected = createOption(D6O_SERVERID);
984
    EXPECT_TRUE(clientid->equals(expected));
985
986
987
988
989
990
}

// Checks if callouts installed on pkt6_send is able to delete
// existing options and that server applies those changes. In particular,
// we are trying to send a packet without server-id. The packet should
// be sent
Tomek Mrugalski's avatar
Tomek Mrugalski committed
991
TEST_F(HooksDhcpv6SrvTest, deleteServerIdPkt6Send) {
992
993
994
995
996
997

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_send", pkt6_send_delete_serverid));

    // Let's create a simple SOLICIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
998
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

    // Check that the server indeed sent a malformed ADVERTISE
    ASSERT_EQ(1, srv_->fake_sent_.size());

    // Get that ADVERTISE
    Pkt6Ptr adv = srv_->fake_sent_.front();
    ASSERT_TRUE(adv);

    // Make sure that it does not have server-id
    EXPECT_FALSE(adv->getOption(D6O_SERVERID));
}

// Checks if callouts installed on pkt6_skip is able to set skip flag that
// will cause the server to not process the packet (drop), even though it is valid.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1022
TEST_F(HooksDhcpv6SrvTest, skipPkt6Send) {
1023
1024
1025
1026
1027
1028

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "pkt6_send", pkt6_send_skip));

    // Let's create a simple REQUEST
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1029
    Pkt6Ptr sol = Pkt6Ptr(PktCaptures::captureSimpleSolicit());
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039

    // Simulate that we have received that traffic
    srv_->fakeReceive(sol);

    // Server will now process to run its normal loop, but instead of calling
    // IfaceMgr::receive6(), it will read all packets from the list set by
    // fakeReceive()
    // In particular, it should call registered pkt6_receive callback.
    srv_->run();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1040
    // Check that the server send the packet
1041
1042
    ASSERT_EQ(1, srv_->fake_sent_.size());

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1043
    // But the sent packet should have 0 length (we told the server to
1044
1045
1046
1047
1048
    // skip pack(), but did not do packing outselves)
    Pkt6Ptr sent = srv_->fake_sent_.front();

    // The actual size of sent packet should be 0
    EXPECT_EQ(0, sent->getBuffer().getLength());
1049
1050
1051
1052
}

// This test checks if subnet6_select callout is triggered and reports
// valid parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1053
TEST_F(HooksDhcpv6SrvTest, subnet6Select) {
1054
1055
1056
1057
1058
1059
1060

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "subnet6_select", subnet6_select_callout));

    // Configure 2 subnets, both directly reachable over local interface
    // (let's not complicate the matter with relays)
1061
1062
1063
    string config = "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
1064
1065
1066
1067
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ { "
1068
        "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
1069
1070
1071
        "    \"subnet\": \"2001:db8:1::/48\", "
        "    \"interface\": \"" + valid_iface_ + "\" "
        " }, {"
1072
        "    \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
        "    \"subnet\": \"2001:db8:2::/48\" "
        " } ],"
        "\"valid-lifetime\": 4000 }";

    ElementPtr json = Element::fromJSON(config);
    ConstElementPtr status;

    // Configure the server and make sure the config is accepted
    EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
    ASSERT_TRUE(status);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1083
    comment_ = isc::config::parseAnswer(rcode_, status);
1084
1085
    ASSERT_EQ(0, rcode_);

1086
1087
    CfgMgr::instance().commit();

1088
1089
1090
1091
    // Prepare solicit packet. Server should select first subnet for it
    Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
    sol->setRemoteAddr(IOAddress("fe80::abcd"));
    sol->setIface(valid_iface_);
1092
    sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
1093
1094
1095
1096
1097
1098
    OptionPtr clientid = generateClientId();
    sol->addOption(clientid);

    // Pass it to the server and get an advertise
    Pkt6Ptr adv = srv_->processSolicit(sol);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1099
    // Check if we get response at all
1100
1101
1102
1103
1104
1105
    ASSERT_TRUE(adv);

    // Check that the callback called is indeed the one we installed
    EXPECT_EQ("subnet6_select", callback_name_);

    // Check that pkt6 argument passing was successful and returned proper value
1106
    EXPECT_TRUE(callback_qry_pkt6_.get() == sol.get());
1107

1108
1109
    const Subnet6Collection* exp_subnets =
        CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125

    // The server is supposed to pick the first subnet, because of matching
    // interface. Check that the value is reported properly.
    ASSERT_TRUE(callback_subnet6_);
    EXPECT_EQ(callback_subnet6_.get(), exp_subnets->front().get());

    // Server is supposed to report two subnets
    ASSERT_EQ(exp_subnets->size(), callback_subnet6collection_->size());

    // Compare that the available subnets are reported as expected
    EXPECT_TRUE((*exp_subnets)[0].get() == (*callback_subnet6collection_)[0].get());
    EXPECT_TRUE((*exp_subnets)[1].get() == (*callback_subnet6collection_)[1].get());
}

// This test checks if callout installed on subnet6_select hook point can pick
// a different subnet.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1126
TEST_F(HooksDhcpv6SrvTest, subnet6SselectChange) {
1127
1128
1129
1130
1131
1132
1133

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "subnet6_select", subnet6_select_different_subnet_callout));

    // Configure 2 subnets, both directly reachable over local interface
    // (let's not complicate the matter with relays)
1134
1135
1136
    string config = "{ \"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
1137
1138
1139
1140
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ { "
1141
        "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
1142
1143
1144
        "    \"subnet\": \"2001:db8:1::/48\", "
        "    \"interface\": \"" + valid_iface_ + "\" "
        " }, {"
1145
        "    \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
        "    \"subnet\": \"2001:db8:2::/48\" "
        " } ],"
        "\"valid-lifetime\": 4000 }";

    ElementPtr json = Element::fromJSON(config);
    ConstElementPtr status;

    // Configure the server and make sure the config is accepted
    EXPECT_NO_THROW(status = configureDhcp6Server(*srv_, json));
    ASSERT_TRUE(status);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1156
    comment_ = isc::config::parseAnswer(rcode_, status);
1157
1158
    ASSERT_EQ(0, rcode_);

1159
1160
    CfgMgr::instance().commit();

1161
1162
1163
1164
    // Prepare solicit packet. Server should select first subnet for it
    Pkt6Ptr sol = Pkt6Ptr(new Pkt6(DHCPV6_SOLICIT, 1234));
    sol->setRemoteAddr(IOAddress("fe80::abcd"));
    sol->setIface(valid_iface_);
1165
    sol->addOption(generateIA(D6O_IA_NA, 234, 1500, 3000));
1166
1167
1168
1169
1170
1171
    OptionPtr clientid = generateClientId();
    sol->addOption(clientid);

    // Pass it to the server and get an advertise
    Pkt6Ptr adv = srv_->processSolicit(sol);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
1172
    // Check if we get response at all
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
    ASSERT_TRUE(adv);

    // The response should have an address from second pool, so let's check it
    OptionPtr tmp = adv->getOption(D6O_IA_NA);
    ASSERT_TRUE(tmp);
    boost::shared_ptr<Option6IA> ia = boost::dynamic_pointer_cast<Option6IA>(tmp);
    ASSERT_TRUE(ia);
    tmp = ia->getOption(D6O_IAADDR);
    ASSERT_TRUE(tmp);
    boost::shared_ptr<Option6IAAddr> addr_opt =
        boost::dynamic_pointer_cast<Option6IAAddr>(tmp);
    ASSERT_TRUE(addr_opt);

    // Get all subnets and use second subnet for verification
1187
1188
    const Subnet6Collection* subnets =
        CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
1189
1190
1191
1192
1193
    ASSERT_EQ(2, subnets->size());

    // Advertised address must belong to the second pool (in subnet's range,
    // in dynamic pool)
    EXPECT_TRUE((*subnets)[1]->inRange(addr_opt->getAddress()));
1194
    EXPECT_TRUE((*subnets)[1]->inPool(Lease::TYPE_NA, addr_opt->getAddress()));
1195
1196
}

1197
1198
// This test verifies that incoming (positive) RENEW can be handled properly,
// and the lease6_renew callouts are triggered.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1199
TEST_F(HooksDhcpv6SrvTest, basicLease6Renew) {
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
    NakedDhcpv6Srv srv(0);

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "lease6_renew", lease6_renew_callout));

    const IOAddress addr("2001:db8:1:1::cafe:babe");
    const uint32_t iaid = 234;

    // Generate client-id also duid_
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1213
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
1214
1215
1216

    // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
    // value on purpose. They should be updated during RENEW.
1217
    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
1218
1219
                               501, 502, 503, 504, subnet_->getID(),
                               HWAddrPtr(), 0));
1220
1221
1222
1223
    lease->cltt_ = 1234;
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // Check that the lease is really in the database
1224
    Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
1225
                                                        addr);
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
    ASSERT_TRUE(l);

    // Check that T1, T2, preferred, valid and cltt really set and not using
    // previous (500, 501, etc.) values
    EXPECT_NE(l->t1_, subnet_->getT1());
    EXPECT_NE(l->t2_, subnet_->getT2());
    EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
    EXPECT_NE(l->valid_lft_, subnet_->getValid());
    EXPECT_NE(l->cltt_, time(NULL));

    // Let's create a RENEW
    Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
    req->setRemoteAddr(IOAddress("fe80::abcd"));
1239
    req->setIface("eth0");
1240
    boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256

    OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
    ia->addOption(renewed_addr_opt);
    req->addOption(ia);
    req->addOption(clientid);

    // Server-id is mandatory in RENEW
    req->addOption(srv.getServerID());

    // Pass it to the server and hope for a REPLY
    Pkt6Ptr reply = srv.processRenew(req);
    ASSERT_TRUE(reply);

    // Check that the callback called is indeed the one we installed
    EXPECT_EQ("lease6_renew", callback_name_);

1257
    // Check that appropriate parameters are passed to the callouts
1258
    EXPECT_TRUE(callback_qry_pkt6_);
1259
1260
1261
    EXPECT_TRUE(callback_lease6_);
    EXPECT_TRUE(callback_ia_na_);

1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
    // Check if all expected parameters were really received
    vector<string> expected_argument_names;
    expected_argument_names.push_back("query6");
    expected_argument_names.push_back("lease6");
    expected_argument_names.push_back("ia_na");

    sort(callback_argument_names_.begin(), callback_argument_names_.end());
    sort(expected_argument_names.begin(), expected_argument_names.end());

    EXPECT_TRUE(callback_argument_names_ == expected_argument_names);

    // Check if we get response at all
    checkResponse(reply, DHCPV6_REPLY, 1234);

    OptionPtr tmp = reply->getOption(D6O_IA_NA);
    ASSERT_TRUE(tmp);

    // Check that IA_NA was returned and that there's an address included
    boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 234, subnet_->getT1(),
                                                           subnet_->getT2());

    ASSERT_TRUE(addr_opt);
    // Check that the lease is really in the database
    l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
    ASSERT_TRUE(l);

    // Check that the lease has been returned
    ASSERT_TRUE(callback_lease6_);

    // Check that the returned lease6 in callout is the same as the one in the
    // database
    EXPECT_TRUE(*callback_lease6_ == *l);
}

// This test verifies that incoming (positive) RENEW can be handled properly,
// and the lease6_renew callouts are able to change the lease being updated.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1298
TEST_F(HooksDhcpv6SrvTest, leaseUpdateLease6Renew) {
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
    NakedDhcpv6Srv srv(0);

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "lease6_renew", lease6_renew_update_callout));

    const IOAddress addr("2001:db8:1:1::cafe:babe");
    const uint32_t iaid = 234;

    // Generate client-id also duid_
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1312
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
1313
1314
1315

    // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
    // value on purpose. They should be updated during RENEW.
1316
    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
1317
1318
                               501, 502, 503, 504, subnet_->getID(),
                               HWAddrPtr(), 0));
1319
1320
1321
1322
    lease->cltt_ = 1234;
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // Check that the lease is really in the database
1323
    Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
1324
                                                        addr);
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
    ASSERT_TRUE(l);

    // Check that T1, T2, preferred, valid and cltt really set and not using
    // previous (500, 501, etc.) values
    EXPECT_NE(l->t1_, subnet_->getT1());
    EXPECT_NE(l->t2_, subnet_->getT2());
    EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
    EXPECT_NE(l->valid_lft_, subnet_->getValid());
    EXPECT_NE(l->cltt_, time(NULL));

    // Let's create a RENEW
    Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
    req->setRemoteAddr(IOAddress("fe80::abcd"));
1338
    req->setIface("eth0");
1339
    boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381

    OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
    ia->addOption(renewed_addr_opt);
    req->addOption(ia);
    req->addOption(clientid);

    // Server-id is mandatory in RENEW
    req->addOption(srv.getServerID());

    // Pass it to the server and hope for a REPLY
    Pkt6Ptr reply = srv.processRenew(req);
    ASSERT_TRUE(reply);

    // Check if we get response at all
    checkResponse(reply, DHCPV6_REPLY, 1234);

    OptionPtr tmp = reply->getOption(D6O_IA_NA);
    ASSERT_TRUE(tmp);

    // Check that IA_NA was returned and that there's an address included
    boost::shared_ptr<Option6IAAddr> addr_opt = checkIA_NA(reply, 1000, 1001, 1002);

    ASSERT_TRUE(addr_opt);
    // Check that the lease is really in the database
    l = checkLease(duid_, reply->getOption(D6O_IA_NA), addr_opt);
    ASSERT_TRUE(l);

    // Check that we chose the distinct override values
    ASSERT_NE(override_t1_,        subnet_->getT1());
    ASSERT_NE(override_t2_,        subnet_->getT2());
    ASSERT_NE(override_preferred_, subnet_->getPreferred());
    EXPECT_NE(override_valid_,     subnet_->getValid());

    // Check that T1, T2, preferred, valid were overridden the the callout
    EXPECT_EQ(override_t1_, l->t1_);
    EXPECT_EQ(override_t2_, l->t2_);
    EXPECT_EQ(override_preferred_, l->preferred_lft_);
    EXPECT_EQ(override_valid_, l->valid_lft_);

    // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
    int32_t cltt = static_cast<int32_t>(l->cltt_);
    int32_t expected = static_cast<int32_t>(time(NULL));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1382
    // Equality or difference by 1 between cltt and expected is ok.
1383
1384
1385
1386
1387
    EXPECT_GE(1, abs(cltt - expected));

    EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr_opt->getAddress()));
}

1388
1389
1390
// This test verifies that incoming (positive) RENEW can be handled properly,
// and the lease6_renew callouts are able to set the skip flag that will
// reject the renewal
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1391
TEST_F(HooksDhcpv6SrvTest, skipLease6Renew) {
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
    NakedDhcpv6Srv srv(0);

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "lease6_renew", lease6_renew_skip_callout));

    const IOAddress addr("2001:db8:1:1::cafe:babe");
    const uint32_t iaid = 234;

    // Generate client-id also duid_
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1405
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
1406
1407
1408

    // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
    // value on purpose. They should be updated during RENEW.
1409
    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
1410
1411
                               501, 502, 503, 504, subnet_->getID(),
                               HWAddrPtr(), 0));
1412
1413
1414
1415
    lease->cltt_ = 1234;
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // Check that the lease is really in the database
1416
    Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
1417
                                                        addr);
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
    ASSERT_TRUE(l);

    // Check that T1, T2, preferred, valid and cltt really set and not using
    // previous (500, 501, etc.) values
    EXPECT_NE(l->t1_, subnet_->getT1());
    EXPECT_NE(l->t2_, subnet_->getT2());
    EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
    EXPECT_NE(l->valid_lft_, subnet_->getValid());
    EXPECT_NE(l->cltt_, time(NULL));

    // Let's create a RENEW
    Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RENEW, 1234));
    req->setRemoteAddr(IOAddress("fe80::abcd"));
1431
    req->setIface("eth0");
1432
    boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448

    OptionPtr renewed_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
    ia->addOption(renewed_addr_opt);
    req->addOption(ia);
    req->addOption(clientid);

    // Server-id is mandatory in RENEW
    req->addOption(srv.getServerID());

    // Pass it to the server and hope for a REPLY
    Pkt6Ptr reply = srv.processRenew(req);
    ASSERT_TRUE(reply);

    // Check that our callback was called
    EXPECT_EQ("lease6_renew", callback_name_);

1449
    l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459

    // Check that the old values are still there and they were not
    // updated by the renewal
    EXPECT_NE(l->t1_, subnet_->getT1());
    EXPECT_NE(l->t2_, subnet_->getT2());
    EXPECT_NE(l->preferred_lft_, subnet_->getPreferred());
    EXPECT_NE(l->valid_lft_, subnet_->getValid());
    EXPECT_NE(l->cltt_, time(NULL));
}

1460
1461
1462
1463
1464
1465
1466
1467
1468
// This test verifies that incoming (positive) RELEASE can be handled properly,
// that a REPLY is generated, that the response has status code and that the
// lease is indeed removed from the database.
//
// expected:
// - returned REPLY message has copy of client-id
// - returned REPLY message has server-id
// - returned REPLY message has IA that does not include an IAADDR
// - lease is actually removed from LeaseMgr
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1469
TEST_F(HooksDhcpv6SrvTest, basicLease6Release) {
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
    NakedDhcpv6Srv srv(0);

    // Install pkt6_receive_callout
    EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
                        "lease6_release", lease6_release_callout));

    const IOAddress addr("2001:db8:1:1::cafe:babe");
    const uint32_t iaid = 234;

    // Generate client-id also duid_
    OptionPtr clientid = generateClientId();

    // Check that the address we are about to use is indeed in pool
1483
    ASSERT_TRUE(subnet_->inPool(Lease::TYPE_NA, addr));
1484
1485
1486

    // Note that preferred, valid, T1 and T2 timers and CLTT are set to invalid
    // value on purpose. They should be updated during RENEW.
1487
    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid_, iaid,
1488
1489
                               501, 502, 503, 504, subnet_->getID(),
                               HWAddrPtr(), 0));
1490
1491
1492
1493
    lease->cltt_ = 1234;
    ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));

    // Check that the lease is really in the database
1494
    Lease6Ptr l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
1495
                                                        addr);
1496
1497
1498
1499
1500
    ASSERT_TRUE(l);

    // Let's create a RELEASE
    Pkt6Ptr req = Pkt6Ptr(new Pkt6(DHCPV6_RELEASE, 1234));
    req->setRemoteAddr(IOAddress("fe80::abcd"));
1501
    boost::shared_ptr<Option6IA> ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519

    OptionPtr released_addr_opt(new Option6IAAddr(D6O_IAADDR, addr, 300, 500));
    ia->addOption(released_addr_opt);
    req->addOption(ia);
    req->addOption(clientid);

    // Server-id is mandatory in RELEASE
    req->addOption(srv.getServerID());

    // Pass it to the server and hope for a REPLY
    Pkt6Ptr reply = srv.processRelease(req);

    ASSERT_TRUE(reply);

    // Check that the callback called is indeed the one we installed
    EXPECT_EQ("lease6_release", callback_name_);

    // Check that appropriate parameters are passed to the callouts
1520
    EXPECT_TRUE(callback_qry_pkt6_);
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
    EXPECT_TRUE(callback_lease6_);

    // Check if all expected parameters were really received
    vector<string> expected_argument_names;
    expected_argument_names.push_back("query6");
    expected_argument_names.push_back("lease6");
    sort(callback_argument_names_.begin(), callback_argument_names_.end());
    sort(expected_argument_names.begin(), expected_argument_names.end());
    EXPECT_TRUE(callback_argument_names_ == expected_argument_names);

    // Check that the lease is really gone in the database
    // get lease by address
1533
    l = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
1534
<