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
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 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
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 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
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 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
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
// Copyright (C) 2014-2024 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 <dhcp/dhcp4.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcp/testutils/pkt_filter_test_stub.h>
#include <dhcp/testutils/pkt_filter6_test_stub.h>
#include <dhcpsrv/cfg_iface.h>
#include <asiolink/io_service.h>
#include <asiolink/asio_wrapper.h>
#include <asiolink/interval_timer.h>
#include <dhcpsrv/timer_mgr.h>
#include <testutils/test_to_element.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::test;
using namespace isc::data;
using namespace isc::util;

namespace {

/// @brief Test fixture class for testing the @c CfgIface class.
class CfgIfaceTest : public ::testing::Test {
public:

    /// @brief Constructor.
    ///
    /// By initializing the @c IfaceMgrTestConfig object it creates a set of
    /// fake interfaces: lo, eth0, eth1.
    CfgIfaceTest() :
        iface_mgr_test_config_(true) {
    }

    /// @brief Checks if socket of the specified family is opened on interface.
    ///
    /// @param iface_name Interface name.
    /// @param family One of: AF_INET or AF_INET6
    bool socketOpen(const std::string& iface_name, const int family) const;

    /// @brief Checks if socket is opened on the specified interface and bound
    /// to a specific IPv4 address.
    ///
    /// @param iface_name Interface name.
    /// @param address Address that the socket should be bound to.
    bool socketOpen(const std::string& iface_name,
                    const std::string& address) const;

    /// @brief Checks if unicast socket is opened on interface.
    ///
    /// @param iface_name Interface name.
    bool unicastOpen(const std::string& iface_name) const;

    /// @brief Wait for specific timeout.
    ///
    /// @param timeout Wait timeout in milliseconds.
    void doWait(const long timeout);

    /// @brief Interrupt the current wait (if one).
    void stopWait();

    /// @brief Holds a fake configuration of the interfaces.
    IfaceMgrTestConfig iface_mgr_test_config_;

    /// @brief Pointer to IO service used by the tests.
    asiolink::IOServicePtr io_service_;

private:

    /// @brief Prepares the class for a test.
    virtual void SetUp();

    /// @brief Cleans up after the test.
    virtual void TearDown();
};

void
CfgIfaceTest::SetUp() {
    IfaceMgr::instance().setTestMode(true);
    io_service_.reset(new asiolink::IOService());
    TimerMgr::instance()->setIOService(io_service_);
}

void
CfgIfaceTest::TearDown() {
    // Remove all timers.
    TimerMgr::instance()->unregisterTimers();

    IfaceMgr::instance().setTestMode(false);
    IfaceMgr::instance().closeSockets();
    IfaceMgr::instance().clearIfaces();
    IfaceMgr::instance().detectIfaces();

    // Reset global handlers
    CfgIface::open_sockets_failed_callback_ = 0;

    // Handle all remaining callbacks.
    io_service_->stopAndPoll();
}

bool
CfgIfaceTest::socketOpen(const std::string& iface_name,
                         const int family) const {
    return (iface_mgr_test_config_.socketOpen(iface_name, family));
}

bool
CfgIfaceTest::socketOpen(const std::string& iface_name,
                         const std::string& address) const {
    return (iface_mgr_test_config_.socketOpen(iface_name, address));
}

bool
CfgIfaceTest::unicastOpen(const std::string& iface_name) const {
    return (iface_mgr_test_config_.unicastOpen(iface_name));
}

void
CfgIfaceTest::doWait(const long timeout) {
    asiolink::IntervalTimer timer(io_service_);
    timer.setup([this]() {
        io_service_->stop();
    }, timeout, asiolink::IntervalTimer::ONE_SHOT);

    io_service_->run();
    io_service_->stop();
    io_service_->restart();
}

void
CfgIfaceTest::stopWait() {
    // Post it so we don't stop in the middle of a callback
    io_service_->post([this]() {
        io_service_->stop();
    });
}

// This test checks that the interface names can be explicitly selected
// by their names and IPv4 sockets are opened on these interfaces.
TEST_F(CfgIfaceTest, explicitNamesV4) {<--- syntax error
    CfgIface cfg;
    // Specify valid interface names. There should be no error.
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth0"));
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));

    // Open sockets on specified interfaces.
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    // Sockets should be now open on eth0 and eth1, but not on loopback.
    EXPECT_TRUE(socketOpen("eth0", AF_INET));
    EXPECT_TRUE(socketOpen("eth1", AF_INET));
    EXPECT_FALSE(socketOpen("lo", AF_INET));

    // No IPv6 sockets should be present because we wanted IPv4 sockets.
    EXPECT_FALSE(socketOpen("eth0", AF_INET6));
    EXPECT_FALSE(socketOpen("eth1", AF_INET6));
    EXPECT_FALSE(socketOpen("lo", AF_INET6));

    // Close all sockets and make sure they are really closed.
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("eth0", AF_INET));
    ASSERT_FALSE(socketOpen("eth1", AF_INET));
    ASSERT_FALSE(socketOpen("lo", AF_INET));

    // Reset configuration and select only one interface this time.
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));

    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    // Socket should be open on eth1 only.
    EXPECT_FALSE(socketOpen("eth0", AF_INET));
    EXPECT_TRUE(socketOpen("eth1", AF_INET));
    EXPECT_FALSE(socketOpen("lo", AF_INET));
}

// This test checks that it is possible to specify an interface and address
// on this interface to which the socket should be bound. The sockets should
// not be opened on other addresses on this interface.
TEST_F(CfgIfaceTest, explicitNamesAndAddressesV4) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth0/10.0.0.1"));
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"));

    // Open sockets on specified interfaces and addresses.
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    EXPECT_TRUE(socketOpen("eth0", "10.0.0.1"));
    EXPECT_TRUE(socketOpen("eth1", "192.0.2.3"));
    EXPECT_FALSE(socketOpen("eth1", "192.0.2.5"));

    // Close all sockets and make sure they are really closed.
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("eth0", "10.0.0.1"));
    ASSERT_FALSE(socketOpen("eth1", "192.0.2.3"));
    ASSERT_FALSE(socketOpen("eth1", "192.0.2.5"));

    // Reset configuration.
    cfg.reset();

    // Now check that the socket can be bound to a different address on
    // eth1.
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.5"));

    // Open sockets according to the new configuration.
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    EXPECT_FALSE(socketOpen("eth0", "10.0.0.1"));
    EXPECT_FALSE(socketOpen("eth1", "192.0.2.3"));
    EXPECT_TRUE(socketOpen("eth1", "192.0.2.5"));
}

// This test checks that the invalid interface name and/or IPv4 address
// results in error.
TEST_F(CfgIfaceTest, explicitNamesAndAddressesInvalidV4) {
    CfgIface cfg;
    // An address not assigned to the interface.
    EXPECT_THROW(cfg.use(AF_INET, "eth0/10.0.0.2"), NoSuchAddress);
    // IPv6 address.
    EXPECT_THROW(cfg.use(AF_INET, "eth0/2001:db8:1::1"), InvalidIfaceName);
    // Wildcard interface name with an address.
    EXPECT_THROW(cfg.use(AF_INET, "*/10.0.0.1"), InvalidIfaceName);

    // Duplicated interface.
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));
    EXPECT_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"), DuplicateIfaceName);
}

// This test checks that it is possible to explicitly select multiple
// IPv4 addresses on a single interface.
TEST_F(CfgIfaceTest, multipleAddressesSameInterfaceV4) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"));
    // Cannot add the same address twice.
    ASSERT_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"), DuplicateAddress);
    // Can add another address on this interface.
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.5"));
    // Can't select the whole interface.
    ASSERT_THROW(cfg.use(AF_INET, "eth1"), DuplicateIfaceName);

    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    EXPECT_FALSE(socketOpen("eth0", "10.0.0.1"));
    EXPECT_TRUE(socketOpen("eth1", "192.0.2.3"));
    EXPECT_TRUE(socketOpen("eth1", "192.0.2.5"));
}

// This test checks that it is possible to specify the loopback interface.
TEST_F(CfgIfaceTest, explicitLoopbackV4) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET, "lo"));

    // Use UDP sockets
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));

    // Open sockets on specified interfaces and addresses.
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    EXPECT_TRUE(socketOpen("lo", "127.0.0.1"));

    // Close all sockets and make sure they are really closed.
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", "127.0.0.1"));

    // Reset configuration.
    cfg.reset();

    // Retry with wildcard
    ASSERT_NO_THROW(cfg.use(AF_INET, "*"));
    ASSERT_NO_THROW(cfg.use(AF_INET, "lo"));
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
    // It is now allowed to use loopback, even with wildcard.
    EXPECT_TRUE(socketOpen("lo", "127.0.0.1"));
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", "127.0.0.1"));

    // Retry without UDP sockets (lo can be only used with udp sockets)
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET, "lo"));
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
    // No loopback socket
    EXPECT_FALSE(socketOpen("lo", "127.0.0.1"));

    // Retry with a second interface
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth0"));
    ASSERT_NO_THROW(cfg.use(AF_INET, "lo"));
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
    // The logic used to require lo to be the only interface. That constraint
    // was removed.
    EXPECT_TRUE(socketOpen("lo", "127.0.0.1"));
    cfg.closeSockets();
    EXPECT_FALSE(socketOpen("lo", "127.0.0.1"));

    // Finally with interfaces and addresses
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET, "eth0/10.0.0.1"));
    ASSERT_NO_THROW(cfg.use(AF_INET, "lo/127.0.0.1"));
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));
    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
    // Only loopback is no longer a constraint
    EXPECT_TRUE(socketOpen("lo", "127.0.0.1"));
    cfg.closeSockets();
    EXPECT_FALSE(socketOpen("lo", "127.0.0.1"));
}

// This test checks that the interface names can be explicitly selected
// by their names and IPv6 sockets are opened on these interfaces.
TEST_F(CfgIfaceTest, explicitNamesV6) {
    CfgIface cfg;
    // Specify valid interface names. There should be no error.
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0"));
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth1"));

    // Open sockets on specified interfaces.
    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);

    // Sockets should be now open on eth0 and eth1, but not on loopback.
    EXPECT_TRUE(socketOpen("eth0", AF_INET6));
    EXPECT_TRUE(socketOpen("eth1", AF_INET6));
    EXPECT_FALSE(socketOpen("lo", AF_INET6));

    // No IPv4 sockets should be present because we wanted IPv6 sockets.
    EXPECT_FALSE(socketOpen("eth0", AF_INET));
    EXPECT_FALSE(socketOpen("eth1", AF_INET));
    EXPECT_FALSE(socketOpen("lo", AF_INET));

    // Close all sockets and make sure they are really closed.
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("eth0", AF_INET6));
    ASSERT_FALSE(socketOpen("eth1", AF_INET6));
    ASSERT_FALSE(socketOpen("lo", AF_INET6));

    // Reset configuration and select only one interface this time.
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth1"));

    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);

    // Socket should be open on eth1 only.
    EXPECT_FALSE(socketOpen("eth0", AF_INET6));
    EXPECT_TRUE(socketOpen("eth1", AF_INET6));
    EXPECT_FALSE(socketOpen("lo", AF_INET6));
}

// This test checks that the wildcard interface name can be specified to
// select all interfaces to open IPv4 sockets.
TEST_F(CfgIfaceTest, wildcardV4) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET, "*"));

    cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);

    // Sockets should be now open on eth0 and eth1, but not on loopback.
    EXPECT_TRUE(socketOpen("eth0", AF_INET));
    EXPECT_TRUE(socketOpen("eth1", AF_INET));
    EXPECT_FALSE(socketOpen("lo", AF_INET));

    // No IPv6 sockets should be present because we wanted IPv4 sockets.
    EXPECT_FALSE(socketOpen("eth0", AF_INET6));
    EXPECT_FALSE(socketOpen("eth1", AF_INET6));
    EXPECT_FALSE(socketOpen("lo", AF_INET6));
}

// This test checks that the wildcard interface name can be specified to
// select all interfaces to open IPv6 sockets.
TEST_F(CfgIfaceTest, wildcardV6) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET6, "*"));

    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);

    // Sockets should be now open on eth0 and eth1, but not on loopback.
    EXPECT_TRUE(socketOpen("eth0", AF_INET6));
    EXPECT_TRUE(socketOpen("eth1", AF_INET6));
    EXPECT_FALSE(socketOpen("lo", AF_INET6));

    // No IPv6 sockets should be present because we wanted IPv6 sockets.
    EXPECT_FALSE(socketOpen("eth0", AF_INET));
    EXPECT_FALSE(socketOpen("eth1", AF_INET));
    EXPECT_FALSE(socketOpen("lo", AF_INET));
}

// Test that unicast address can be specified for the socket to be opened on
// the interface on which the socket bound to link local address is also
// opened.
TEST_F(CfgIfaceTest, validUnicast) {
    CfgIface cfg;

    // One socket will be opened on link-local address, one on unicast but
    // on the same interface.
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0"));
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0/2001:db8:1::1"));

    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);

    EXPECT_TRUE(socketOpen("eth0", AF_INET6));
    EXPECT_TRUE(unicastOpen("eth0"));
}

// Test that when invalid interface names are specified an exception is thrown.
TEST_F(CfgIfaceTest, invalidValues) {
    CfgIface cfg;
    ASSERT_THROW(cfg.use(AF_INET, ""), InvalidIfaceName);
    ASSERT_THROW(cfg.use(AF_INET, " "), InvalidIfaceName);
    ASSERT_THROW(cfg.use(AF_INET, "bogus"), NoSuchIface);

    ASSERT_NO_THROW(cfg.use(AF_INET, "eth0"));
    ASSERT_THROW(cfg.use(AF_INET, "eth0"), DuplicateIfaceName);

    ASSERT_THROW(cfg.use(AF_INET, "eth0/2001:db8:1::1"), InvalidIfaceName);

    ASSERT_THROW(cfg.use(AF_INET6, "eth0/"), InvalidIfaceName);
    ASSERT_THROW(cfg.use(AF_INET6, "/2001:db8:1::1"), InvalidIfaceName);
    ASSERT_THROW(cfg.use(AF_INET6, "*/2001:db8:1::1"), InvalidIfaceName);
    ASSERT_THROW(cfg.use(AF_INET6, "bogus/2001:db8:1::1"), NoSuchIface);
    ASSERT_THROW(cfg.use(AF_INET6, "eth0/2001:db8:1::2"), NoSuchAddress);
    ASSERT_NO_THROW(cfg.use(AF_INET6, "*"));
    ASSERT_THROW(cfg.use(AF_INET6, "*"), DuplicateIfaceName);
}

// This test checks that it is possible to specify the loopback interface.
// Note that without a link-local address an unicast address is required.
TEST_F(CfgIfaceTest, explicitLoopbackV6) {
    CfgIface cfg;
    ASSERT_NO_THROW(cfg.use(AF_INET6, "lo/::1"));

    // Open sockets on specified interfaces and addresses.
    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);

    EXPECT_TRUE(socketOpen("lo", AF_INET6));

    // Close all sockets and make sure they are really closed.
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", AF_INET6));

    // Reset configuration.
    cfg.reset();

    // Retry with wildcard
    ASSERT_NO_THROW(cfg.use(AF_INET6, "*"));
    ASSERT_NO_THROW(cfg.use(AF_INET6, "lo/::1"));
    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
    // The logic used to require lo to be used only on its own, not with a
    // wildcard. That constraint was removed.
    EXPECT_TRUE(socketOpen("lo", AF_INET6));
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", AF_INET6));

    // Retry with a second interface
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0"));
    ASSERT_NO_THROW(cfg.use(AF_INET6, "lo/::1"));
    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
    // The logic used to require lo to be used only on its own, not with a
    // wildcard. That constraint was removed.
    EXPECT_TRUE(socketOpen("lo", AF_INET6));
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", AF_INET6));

    // Finally with interfaces and addresses
    cfg.reset();
    ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0/2001:db8:1::1"));
    ASSERT_NO_THROW(cfg.use(AF_INET6, "lo/::1"));
    cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
    // The logic used to require lo to be used only on its own, not with a
    // wildcard. That constraint was removed.
    EXPECT_TRUE(socketOpen("lo", AF_INET6));
    cfg.closeSockets();
    ASSERT_FALSE(socketOpen("lo", AF_INET6));
}

// Test that the equality and inequality operators work fine for CfgIface.
TEST_F(CfgIfaceTest, equality) {
    CfgIface cfg1;
    CfgIface cfg2;

    // Initially objects must be equal.
    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);

    // Differ by one interface.
    cfg1.use(AF_INET, "eth0");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // Now interfaces should be equal.
    cfg2.use(AF_INET, "eth0");
    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);

    // Differ by unicast address.
    cfg1.use(AF_INET6, "eth0/2001:db8:1::1");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // Differ by unicast address and one interface.
    cfg2.use(AF_INET6, "eth1");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // Now, the unicast addresses are equal but still differ by one interface.
    cfg2.use(AF_INET6, "eth0/2001:db8:1::1");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // They should be now back to equal.
    cfg1.use(AF_INET6, "eth1");
    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);

    // Even though the wildcard doesn't change anything because all interfaces
    // are already in use, the fact that the wildcard is specified should
    // cause them to be not equal.
    cfg1.use(AF_INET6, "*");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // Finally, both are equal as they use wildcard.
    cfg2.use(AF_INET, "*");
    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);

    // Differ by socket type.
    cfg1.useSocketType(AF_INET, "udp");
    EXPECT_FALSE(cfg1 == cfg2);
    EXPECT_TRUE(cfg1 != cfg2);

    // Now, both should use the same socket type.
    cfg2.useSocketType(AF_INET, "udp");
    EXPECT_TRUE(cfg1 == cfg2);
    EXPECT_FALSE(cfg1 != cfg2);
}

// This test verifies that it is possible to unparse the interface config.
TEST_F(CfgIfaceTest, unparse) {
    CfgIface cfg4;

    // Add things in it
    EXPECT_NO_THROW(cfg4.use(AF_INET, "*"));
    EXPECT_NO_THROW(cfg4.use(AF_INET, "eth0"));
    EXPECT_NO_THROW(cfg4.use(AF_INET, "eth1/192.0.2.3"));
    std::string comment = "{ \"comment\": \"foo\", \"bar\": 1 }";
    EXPECT_NO_THROW(cfg4.setContext(Element::fromJSON(comment)));

    // Check unparse
    std::string expected =
        "{ "
        "\"interfaces\": [ \"*\", \"eth0\", \"eth1/192.0.2.3\" ], "
        "\"re-detect\": false, "
        "\"user-context\": { \"comment\": \"foo\", \"bar\": 1 } }";
    runToElementTest<CfgIface>(expected, cfg4);

    // Now check IPv6
    CfgIface cfg6;
    EXPECT_NO_THROW(cfg6.use(AF_INET6, "*"));
    EXPECT_NO_THROW(cfg6.use(AF_INET6, "eth1"));
    EXPECT_NO_THROW(cfg6.use(AF_INET6, "eth0/2001:db8:1::1"));
    comment = "{ \"comment\": \"bar\", \"foo\": 2 }";
    EXPECT_NO_THROW(cfg6.setContext(Element::fromJSON(comment)));

    expected =
        "{ "
        "\"interfaces\": [ \"*\", \"eth1\", \"eth0/2001:db8:1::1\" ], "
        "\"re-detect\": false, "
        "\"user-context\": { \"comment\": \"bar\", \"foo\": 2 } }";
    runToElementTest<CfgIface>(expected, cfg6);
}

// This test verifies that it is possible to require that all
// service sockets are opened properly. If any socket fails to
// bind then an exception should be thrown.
TEST_F(CfgIfaceTest, requireOpenAllServiceSockets) {
    CfgIface cfg4;
    CfgIface cfg6;

    // Configure a fail callback
    uint16_t fail_calls = 0;
    CfgIface::OpenSocketsFailedCallback on_fail_callback =
        [&fail_calls](ReconnectCtlPtr reconnect_ctl) {
            EXPECT_TRUE(reconnect_ctl);
            EXPECT_TRUE(reconnect_ctl->exitOnFailure());
            fail_calls++;
        };

    CfgIface::open_sockets_failed_callback_ = on_fail_callback;

    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth0"));
    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth1/192.0.2.3"));
    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth0/2001:db8:1::1"));
    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth1"));

    // Require all sockets bind successfully
    cfg4.setServiceSocketsRequireAll(true);
    cfg4.setServiceSocketsMaxRetries(0);
    cfg6.setServiceSocketsRequireAll(true);
    cfg6.setServiceSocketsMaxRetries(0);

    // Open the available ports
    ASSERT_NO_THROW(cfg4.openSockets(AF_INET, DHCP4_SERVER_PORT));
    ASSERT_NO_THROW(cfg6.openSockets(AF_INET6, DHCP6_SERVER_PORT));
    cfg4.closeSockets();
    cfg6.closeSockets();

    // Set the callback to throw an exception on open
    auto open_callback = [](uint16_t) {
        isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
    };
    boost::shared_ptr<isc::dhcp::test::PktFilterTestStub> filter(new isc::dhcp::test::PktFilterTestStub());
    boost::shared_ptr<isc::dhcp::test::PktFilter6TestStub> filter6(new isc::dhcp::test::PktFilter6TestStub());
    filter->setOpenSocketCallback(open_callback);
    filter6->setOpenSocketCallback(open_callback);
    ASSERT_TRUE(filter);
    ASSERT_TRUE(filter6);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter6));

    // Open an unavailable port
    EXPECT_NO_THROW(cfg4.openSockets(AF_INET, DHCP4_SERVER_PORT));
    EXPECT_NO_THROW(cfg6.openSockets(AF_INET6, DHCP6_SERVER_PORT));

    // Both instances should call the fail callback.
    EXPECT_EQ(fail_calls, 2);
}

// This test verifies that if any IPv4 socket fails to bind,
// the opening will retry.
TEST_F(CfgIfaceTest, retryOpenServiceSockets4) {
    CfgIface cfg4;

    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth0"));
    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth1/192.0.2.3"));

    // Parameters
    const uint16_t RETRIES = 5;
    const uint16_t WAIT_TIME = 5; // miliseconds
    // The number of sockets opened in a single retry attempt.
    // iface: eth0 addr: 10.0.0.1 port: 67 rbcast: 0 sbcast: 0
    // iface: eth1 addr: 192.0.2.3 port: 67 rbcast: 0 sbcast: 0
    const uint16_t CALLS_PER_RETRY = 2;

    // Require retry socket binding
    cfg4.setServiceSocketsMaxRetries(RETRIES);
    cfg4.setServiceSocketsRetryWaitTime(WAIT_TIME);

    // For each interface perform 1 init open and a few retries.
    size_t exp_calls = CALLS_PER_RETRY * (RETRIES + 1);

    // Set the callback to count calls and check wait time
    size_t total_calls = 0;
    auto last_call_time = std::chrono::system_clock::time_point::min();
    auto open_callback = [this, &total_calls, &last_call_time, WAIT_TIME, exp_calls](uint16_t) {
        auto now = std::chrono::system_clock::now();

        // Check waiting time only for the first call in a retry attempt.
        if (total_calls % CALLS_PER_RETRY == 0) {
            // Don't check the waiting time for initial call.
            if (total_calls != 0) {
                auto interval = now - last_call_time;
                auto interval_ms =
                    std::chrono::duration_cast<std::chrono::milliseconds>(
                        interval
                    ).count();

                EXPECT_GE(interval_ms, WAIT_TIME);
            }

            last_call_time = now;
        }

        total_calls++;

        if (total_calls == exp_calls) {
            stopWait();
        }

        // Fail to open a socket
        isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
    };

    boost::shared_ptr<isc::dhcp::test::PktFilterTestStub> filter(
        new isc::dhcp::test::PktFilterTestStub()
    );

    filter->setOpenSocketCallback(open_callback);

    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // Open an unavailable port
    ASSERT_NO_THROW(cfg4.openSockets(AF_INET, DHCP4_SERVER_PORT));

    // Wait for a finish sockets binding (with a safe margin).
    doWait(RETRIES * WAIT_TIME * 10);

    EXPECT_EQ(exp_calls, total_calls);
}

// This test verifies that if any IPv4 socket fails to bind, the opening will
// retry, but the opened sockets will not be re-bound.
TEST_F(CfgIfaceTest, retryOpenServiceSockets4OmitBound) {
    CfgIface cfg4;

    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth0"));
    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth1/192.0.2.3"));

    // Parameters
    const uint16_t RETRIES = 5;
    const uint16_t WAIT_TIME = 5; // miliseconds

    // Require retry socket binding
    cfg4.setServiceSocketsMaxRetries(RETRIES);
    cfg4.setServiceSocketsRetryWaitTime(WAIT_TIME);

    // Set the callback to count calls and check wait time
    size_t total_calls = 0;
    auto last_call_time = std::chrono::system_clock::time_point::min();

    // For eth0 interface perform 1 init open and a few retries,
    // for eth1 interface perform only init open.
    size_t exp_calls = (RETRIES + 1) + 1;

    auto open_callback = [this, &total_calls, &last_call_time, WAIT_TIME, exp_calls](uint16_t) {
        auto now = std::chrono::system_clock::now();
        bool is_eth1 = total_calls == 1;

        // Skip the wait time check for the socket when two sockets are
        // binding in a single attempt.

        // Don't check the waiting time for initial calls.
        // iface: eth0 addr: 10.0.0.1 port: 67 rbcast: 0 sbcast: 0
        // iface: eth1 addr: 192.0.2.3 port: 67 rbcast: 0 sbcast: 0 - fails
        if (total_calls > 1) {
            auto interval = now - last_call_time;
            auto interval_ms =
                std::chrono::duration_cast<std::chrono::milliseconds>(
                    interval
                ).count();

            EXPECT_GE(interval_ms, WAIT_TIME);
        }

        last_call_time = now;

        total_calls++;

        if (total_calls == exp_calls) {
            stopWait();
        }

        // Fail to open a socket on eth0, success for eth1
        if (!is_eth1) {
            isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
        }

    };

    boost::shared_ptr<isc::dhcp::test::PktFilterTestStub> filter(
        new isc::dhcp::test::PktFilterTestStub()
    );

    filter->setOpenSocketCallback(open_callback);

    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // Open an unavailable port
    ASSERT_NO_THROW(cfg4.openSockets(AF_INET, DHCP4_SERVER_PORT));

    // Wait for a finish sockets binding (with a safe margin).
    doWait(RETRIES * WAIT_TIME * 10);

    EXPECT_EQ(exp_calls, total_calls);
}

// Test that only one reopen timer is active simultaneously. If a new opening
// starts, then the previous should be interrupted.
TEST_F(CfgIfaceTest, retryDoubleOpenServiceSockets4) {
    CfgIface cfg4;

    ASSERT_NO_THROW(cfg4.use(AF_INET, "eth0"));

    // Initial timer has a high frequency.
    cfg4.setServiceSocketsMaxRetries(10000);
    cfg4.setServiceSocketsRetryWaitTime(1);

    // Set the callback that interrupt the previous execution.
    uint16_t first_port_calls = 0;
    uint16_t second_port_calls = 0;
    auto open_callback = [&first_port_calls, &second_port_calls](uint16_t port) {
        // First timer must be interrupted.
        if (second_port_calls > 0) {
            EXPECT_TRUE(port == 2);
        }

        if (port == 1) {
            first_port_calls++;
        } else {
            second_port_calls++;
        }

        // Fail to open and retry.
        isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
    };

    boost::shared_ptr<isc::dhcp::test::PktFilterTestStub> filter(
        new isc::dhcp::test::PktFilterTestStub()
    );

    filter->setOpenSocketCallback(open_callback);

    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // First opening.
    ASSERT_NO_THROW(cfg4.openSockets(AF_INET, 1));

    // Wait a short time.
    doWait(10);

    // Reconfigure the interface parameters.
    cfg4.setServiceSocketsMaxRetries(1);
    cfg4.setServiceSocketsRetryWaitTime(10);

    // Second opening.
    ASSERT_NO_THROW(cfg4.openSockets(AF_INET, 2));

    doWait(50);

    // The first timer should perform some calls.
    EXPECT_GT(first_port_calls, 0);
    // The secondary timer should make 2 calls: initial and 1 retry.
    EXPECT_EQ(second_port_calls, 2);
}

// This test verifies that if any IPv6 socket fails to bind,
// the opening will retry.
TEST_F(CfgIfaceTest, retryOpenServiceSockets6) {
    CfgIface cfg6;

    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth0/2001:db8:1::1"));
    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth1"));

    // Parameters
    const uint16_t RETRIES = 5;
    const uint16_t WAIT_TIME = 5; // miliseconds
    // The number of sockets opened in a single retry attempt.
    // 1 unicast and 2 multicast sockets.
    // iface: eth0 addr: 2001:db8:1::1 port: 547 multicast: 0
    // iface: eth0 addr: fe80::3a60:77ff:fed5:cdef port: 547 multicast: 1
    // iface: eth1 addr: fe80::3a60:77ff:fed5:abcd port: 547 multicast: 1
    const uint16_t CALLS_PER_RETRY = 3;

    // Require retry socket binding
    cfg6.setServiceSocketsMaxRetries(RETRIES);
    cfg6.setServiceSocketsRetryWaitTime(WAIT_TIME);

    // For each interface perform 1 init open and a few retries.
    size_t exp_calls = CALLS_PER_RETRY * (RETRIES + 1);

    // Set the callback to count calls and check wait time
    size_t total_calls = 0;
    auto last_call_time = std::chrono::system_clock::time_point::min();
    auto open_callback = [this, &total_calls, &last_call_time, WAIT_TIME, exp_calls](uint16_t) {
        auto now = std::chrono::system_clock::now();

        // Check waiting time only for the first call in a retry attempt.
        if (total_calls % CALLS_PER_RETRY == 0) {
            // Don't check the waiting time for initial call.
            if (total_calls != 0) {
                auto interval = now - last_call_time;
                auto interval_ms =
                    std::chrono::duration_cast<std::chrono::milliseconds>(
                        interval
                    ).count();

                EXPECT_GE(interval_ms, WAIT_TIME);
            }

            last_call_time = now;
        }

        total_calls++;

        if (total_calls == exp_calls) {
            stopWait();
        }

        // Fail to open a socket
        isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
    };

    boost::shared_ptr<isc::dhcp::test::PktFilter6TestStub> filter(
        new isc::dhcp::test::PktFilter6TestStub()
    );
    filter->setOpenSocketCallback(open_callback);
    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // Open an unavailable port
    ASSERT_NO_THROW(cfg6.openSockets(AF_INET6, DHCP6_SERVER_PORT));

    // Wait for a finish sockets binding (with a safe margin).
    doWait(RETRIES * WAIT_TIME * 10);

    // For each interface perform 1 init open and a few retries.
    EXPECT_EQ(exp_calls, total_calls);
}

// This test verifies that if any IPv6 socket fails to bind, the opening will
// retry, but the opened sockets will not be re-bound.
TEST_F(CfgIfaceTest, retryOpenServiceSockets6OmitBound) {
    CfgIface cfg6;

    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth0/2001:db8:1::1"));
    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth1"));

    // Parameters
    const uint16_t RETRIES = 5;
    const uint16_t WAIT_TIME = 5; // miliseconds

    // Require retry socket binding
    cfg6.setServiceSocketsMaxRetries(RETRIES);
    cfg6.setServiceSocketsRetryWaitTime(WAIT_TIME);

#if defined OS_LINUX
    const uint32_t opened_by_eth0 = 3;
#else
    const uint32_t opened_by_eth0 = 2;
#endif


    // For eth0 interface perform only 3 (on Linux Systems or 2 otherwise) init open,
    // for eth1 interface perform 1 init open and a few retries.
    size_t exp_calls = RETRIES + 1 + opened_by_eth0;

    // Set the callback to count calls and check wait time
    size_t total_calls = 0;
    auto last_call_time = std::chrono::system_clock::time_point::min();
    auto open_callback = [this, &total_calls, &last_call_time, WAIT_TIME, exp_calls](uint16_t) {
        auto now = std::chrono::system_clock::now();
        bool is_eth0 = total_calls < opened_by_eth0;

        // Skip the wait time check for the socket when two sockets are
        // binding in a single attempt.

        // Don't check the waiting time for initial calls.
        // iface: eth0 addr: 2001:db8:1::1 port: 547 multicast: 0
        // iface: eth0 addr: fe80::3a60:77ff:fed5:cdef port: 547 multicast: 1
        // iface: eth0 addr: ff02::1:2 port: 547 multicast: 0 --- only on Linux systems
        // iface: eth1 addr: fe80::3a60:77ff:fed5:abcd port: 547 multicast: 1 - fails

        if (total_calls > (opened_by_eth0 + 1)) {
            auto interval = now - last_call_time;
            auto interval_ms =
                std::chrono::duration_cast<std::chrono::milliseconds>(
                    interval
                ).count();

            EXPECT_GE(interval_ms, WAIT_TIME);
        }

        last_call_time = now;

        total_calls++;

        if (total_calls == exp_calls) {
            stopWait();
        }

        // Fail to open a socket on eth1, success for eth0
        if (!is_eth0) {
            isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
        }
    };

    boost::shared_ptr<isc::dhcp::test::PktFilter6TestStub> filter(
        new isc::dhcp::test::PktFilter6TestStub()
    );

    filter->setOpenSocketCallback(open_callback);

    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // Open an unavailable port
    ASSERT_NO_THROW(cfg6.openSockets(AF_INET6, DHCP6_SERVER_PORT));

    // Wait for a finish sockets binding (with a safe margin).
    doWait(RETRIES * WAIT_TIME * 10);

    EXPECT_EQ(exp_calls, total_calls);
}

// Test that only one reopen timer is active simultaneously. If a new opening
// starts, then the previous should be interrupted.
TEST_F(CfgIfaceTest, retryDoubleOpenServiceSockets6) {
    CfgIface cfg6;

    ASSERT_NO_THROW(cfg6.use(AF_INET6, "eth0"));

    // Initial timer has a high frequency.
    cfg6.setServiceSocketsMaxRetries(10000);
    cfg6.setServiceSocketsRetryWaitTime(1);

    // Set the callback that interrupt the previous execution.
    uint16_t first_port_calls = 0;
    uint16_t second_port_calls = 0;
    auto open_callback = [&first_port_calls, &second_port_calls](uint16_t port) {
        // First timer must be interrupted.
        if (second_port_calls > 0) {
            EXPECT_TRUE(port == 2);
        }

        if (port == 1) {
            first_port_calls++;
        } else {
            second_port_calls++;
        }

        // Fail to open and retry.
        isc_throw(Unexpected, "CfgIfaceTest: cannot open a port");
    };

    boost::shared_ptr<isc::dhcp::test::PktFilter6TestStub> filter(
        new isc::dhcp::test::PktFilter6TestStub()
    );

    filter->setOpenSocketCallback(open_callback);

    ASSERT_TRUE(filter);
    ASSERT_NO_THROW(IfaceMgr::instance().setPacketFilter(filter));

    // First opening.
    ASSERT_NO_THROW(cfg6.openSockets(AF_INET6, 1));

    // Wait a short time.
    doWait(10);

    // Reconfigure the interface parameters.
    cfg6.setServiceSocketsMaxRetries(1);
    cfg6.setServiceSocketsRetryWaitTime(10);

    // Second opening.
    ASSERT_NO_THROW(cfg6.openSockets(AF_INET6, 2));

    doWait(50);

    // The first timer should perform some calls.
    EXPECT_GT(first_port_calls, 0);
    // The secondary timer should make 2 calls: initial and 1 retry.
    EXPECT_EQ(second_port_calls, 2);
}

// This test verifies that it is possible to specify the socket
// type to be used by the DHCPv4 server.
// This test is enabled on LINUX and BSD only, because the
// direct traffic is only supported on those systems.
#if defined OS_LINUX || defined OS_BSD
TEST(CfgIfaceNoStubTest, useSocketType) {
    CfgIface cfg;
    // Select datagram sockets.
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, "udp"));
    EXPECT_EQ("udp", cfg.socketTypeToText());
    ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
    // For datagram sockets, the direct traffic is not supported.
    ASSERT_TRUE(!IfaceMgr::instance().isDirectResponseSupported());

    // Check unparse
    std::string expected = "{\n"
        " \"interfaces\": [ ],\n"
        " \"dhcp-socket-type\": \"udp\",\n"
        " \"re-detect\": false }";
    runToElementTest<CfgIface>(expected, cfg);

    // Select raw sockets.
    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, "raw"));
    EXPECT_EQ("raw", cfg.socketTypeToText());
    ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
    // For raw sockets, the direct traffic is supported.
    ASSERT_TRUE(IfaceMgr::instance().isDirectResponseSupported());

    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));
    EXPECT_EQ("udp", cfg.socketTypeToText());
    ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
    ASSERT_TRUE(!IfaceMgr::instance().isDirectResponseSupported());

    ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_RAW));
    EXPECT_EQ("raw", cfg.socketTypeToText());
    ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
    ASSERT_TRUE(IfaceMgr::instance().isDirectResponseSupported());

    // Test invalid values.
    EXPECT_THROW(cfg.useSocketType(AF_INET, "default"),
        InvalidSocketType);
    EXPECT_THROW(cfg.useSocketType(AF_INET6, "udp"),
        InvalidSocketType);
}
#endif

} // end of anonymous namespace