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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file This file contains tests which exercise the load and unload
/// functions in the Radius hooks library. In order to test the load
/// function, one must be able to pass it hook library parameters. The
/// the only way to populate these parameters is by actually loading the
/// library via HooksManager::loadLibraries().

#include <config.h>

#include <radius_parsers.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/host_mgr.h>
#include <testutils/gtest_utils.h>
#include <testutils/test_to_element.h>
#include <attribute_test.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::radius;
using namespace isc::test;

namespace {

/// @brief Test fixture for testing configurations for the radius library.
class ConfigTest : public radius::test::AttributeTest {
public:
    /// @brief Constructor
    ConfigTest() : AttributeTest(), impl_(RadiusImpl::instance()) {
        impl_.reset();
        CfgMgr::instance().clear();
        HostMgr::instance().setDisableSingleQuery(false);
    }

    /// @brief Destructor
    virtual ~ConfigTest() {
        impl_.reset();
    }

    /// @brief Radius implementation.
    RadiusImpl& impl_;
};

// Verify that a configuration must be a map.
TEST_F(ConfigTest, notMap) {<--- syntax error
    ElementPtr config = Element::createList();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "get(string) called on a non-map Element");
}

// Verify that an empty configuration works.
TEST_F(ConfigTest, empty) {
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify that global defaults are set as expected.
TEST_F(ConfigTest, defaults) {
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));
    string expected = "{ "
        "\"access\": {"
        "   \"attributes\": [ ]"
        "}, "
        "\"accounting\": {"
        "   \"attributes\": [ ]"
        "}, "
        "\"bindaddr\": \"*\", "
        "\"canonical-mac-address\": false, "
        "\"client-id-pop0\": false, "
        "\"client-id-printable\": false, "
        "\"deadtime\": 0, "
        "\"dictionary\": \"" + string(DICTIONARY) + "\", "
        "\"extract-duid\": true, "
        "\"identifier-type4\": \"client-id\", "
        "\"identifier-type6\": \"duid\", "
        "\"reselect-subnet-address\": false, "
        "\"reselect-subnet-pool\": false, "
        "\"retries\": 3, "
        "\"session-history\": \"\", "
        "\"thread-pool-size\": 0, "
        "\"timeout\": 10"
        " }";
    runToElementTest<RadiusImpl>(expected, impl_);
}

// Verify that global can be set.
TEST_F(ConfigTest, global) {
    ElementPtr config = Element::createMap();
    config->set("bindaddr", Element::create("127.0.0.1"));
    config->set("canonical-mac-address", Element::create(true));
    config->set("client-id-pop0", Element::create(true));
    config->set("client-id-printable", Element::create(true));
    config->set("deadtime", Element::create(10));
    config->set("dictionary", Element::create("/dev/null"));
    config->set("extract-duid", Element::create(false));
    config->set("identifier-type4", Element::create("hw-address"));
    config->set("identifier-type6", Element::create("flex-id"));
    config->set("reselect-subnet-address", Element::create(true));
    config->set("reselect-subnet-pool", Element::create(true));
    config->set("retries", Element::create(5));
    config->set("session-history", Element::create("/tmp/foobar"));
    config->set("thread-pool-size", Element::create(8));
    config->set("timeout", Element::create(12));
    EXPECT_NO_THROW(impl_.init(config));
    string expected = "{ "
        "\"access\": {"
        "   \"attributes\": [ ]"
        "}, "
        "\"accounting\": {"
        "   \"attributes\": [ ]"
        "}, "
        "\"bindaddr\": \"127.0.0.1\", "
        "\"canonical-mac-address\": true, "
        "\"client-id-pop0\": true, "
        "\"client-id-printable\": true, "
        "\"deadtime\": 10, "
        "\"dictionary\": \"/dev/null\", "
        "\"extract-duid\": false, "
        "\"identifier-type4\": \"hw-address\", "
        "\"identifier-type6\": \"flex-id\", "
        "\"reselect-subnet-address\": true, "
        "\"reselect-subnet-pool\": true, "
        "\"retries\": 5, "
        "\"session-history\": \"/tmp/foobar\", "
        "\"thread-pool-size\": 8, "
        "\"timeout\": 12"
        " }";
    runToElementTest<RadiusImpl>(expected, impl_);

    // Check state.
    EXPECT_EQ("127.0.0.1", impl_.bindaddr_);
    EXPECT_TRUE(impl_.canonical_mac_address_);
    EXPECT_TRUE(impl_.clientid_pop0_);
    EXPECT_TRUE(impl_.clientid_printable_);
    EXPECT_EQ(10, impl_.deadtime_);
    EXPECT_EQ("/dev/null", impl_.dictionary_);
    EXPECT_FALSE(impl_.extract_duid_);
    EXPECT_EQ(Host::IDENT_HWADDR, impl_.id_type4_);
    EXPECT_EQ(Host::IDENT_FLEX, impl_.id_type6_);
    EXPECT_EQ(5, impl_.retries_);
    EXPECT_TRUE(impl_.reselect_subnet_address_);
    EXPECT_TRUE(impl_.reselect_subnet_pool_);
    EXPECT_EQ("/tmp/foobar", impl_.session_history_filename_);
    EXPECT_EQ(8, impl_.thread_pool_size_);
    EXPECT_EQ(12, impl_.timeout_);
    EXPECT_TRUE(impl_.remap_.empty());
}

// Verify that bad dictionary throws.
TEST_F(ConfigTest, badDictionary) {
    // Unload dictionary.
    AttrDefs::instance().clear();

    ElementPtr config = Element::createMap();
    config->set("dictionary", Element::create("/do-not-exist"));
    string expected = "can't read radius dictionary: ";
    expected += "can't open dictionary '/do-not-exist': ";
    expected += "No such file or directory";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);

    impl_.reset();
    config = Element::createMap();
    config->set("dictionary", Element::create(string(TEST_DICTIONARY)));
    EXPECT_NO_THROW_LOG(impl_.init(config));
}

// Verify that bad global throws.
TEST_F(ConfigTest, badGlobal) {
    ElementPtr config = Element::createMap();
    config->set("bindaddr", Element::create(0));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "stringValue() called on non-string Element");

    impl_.reset();
    config = Element::createMap();
    config->set("canonical-mac-address", Element::create("no"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("client-id-pop0", Element::create(2));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("client-id-printable", Element::create("yes"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("deadtime", Element::create("now"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "intValue() called on non-integer Element");

    impl_.reset();
    config = Element::createMap();
    config->set("deadtime", Element::create(-1));
    ostringstream expected;
    expected << "bad deadtime -1 not in [0.."
             << numeric_limits<unsigned>::max() << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("deadtime", Element::create(123456789000));
    expected.str("");
    expected << "bad deadtime 123456789000 not in [0.."
             << numeric_limits<unsigned>::max() << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("dictionary", Element::create(0));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "stringValue() called on non-string Element");

    impl_.reset();
    config = Element::createMap();
    config->set("extract-duid", Element::create(1));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("identifier-type4", Element::create(1));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "stringValue() called on non-string Element");

    impl_.reset();
    config = Element::createMap();
    config->set("identifier-type4", Element::create("hardware-address"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "invalid client identifier type 'hardware-address'");

    impl_.reset();
    config = Element::createMap();
    config->set("identifier-type6", Element::create(2));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "stringValue() called on non-string Element");

    impl_.reset();
    config = Element::createMap();
    config->set("identifier-type6", Element::create("flex"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "invalid client identifier type 'flex'");

    impl_.reset();
    config = Element::createMap();
    config->set("nas-ports", Element::create(0));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "listValue() called on non-list Element");

    impl_.reset();
    config = Element::createMap();
    config->set("reselect-subnet-address", Element::create(0));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("reselect-subnet-pool", Element::create("false"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "boolValue() called on non-Bool Element");

    impl_.reset();
    config = Element::createMap();
    config->set("retries", Element::create("no"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "intValue() called on non-integer Element");

    impl_.reset();
    config = Element::createMap();
    config->set("retries", Element::create(-1));
    expected.str("");
    expected << "bad retries -1 not in [0.."
             << numeric_limits<unsigned>::max() << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("retries", Element::create(123456789000));
    expected.str("");
    expected << "bad retries 123456789000 not in [0.."
             << numeric_limits<unsigned>::max() << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("session-history", Element::create(-1));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "stringValue() called on non-string Element");

    config = Element::createMap();
    config->set("thread-pool-size", Element::create("large"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "intValue() called on non-integer Element");

    impl_.reset();
    config = Element::createMap();
    config->set("timeout", Element::create("no"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "intValue() called on non-integer Element");

    impl_.reset();
    config = Element::createMap();
    config->set("timeout", Element::create(-1));
    expected.str("");
    expected << "bad timeout -1 not in [0.."
             << (numeric_limits<long>::max() / 1000) << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("timeout", Element::create(10000000000000000));
    expected.str("");
    expected << "bad timeout 10000000000000000 not in [0.."
             << (numeric_limits<long>::max() / 1000) << "]";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("access", Element::create(false));
    expected.str("");
    expected << "expected service to be map, but got boolean instead"
             << " (parsing access)";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());

    impl_.reset();
    config = Element::createMap();
    config->set("accounting", Element::create(true));
    expected.str("");
    expected << "expected service to be map, but got boolean instead"
             << " (parsing accounting)";
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected.str());
}

// Verify syntax of nas-ports.
TEST_F(ConfigTest, nasPorts) {
    EXPECT_TRUE(impl_.remap_.empty());

    // Nas-ports can be empty.
    ElementPtr config = Element::createMap();
    config->set("nas-ports", Element::createList());
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_TRUE(impl_.remap_.empty());

    // Default.
    impl_.reset();
    config = Element::createMap();
    ElementPtr ports = Element::createList();
    ElementPtr entry = Element::createMap();
    entry->set("port", Element::create(10));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(1, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[SUBNET_ID_DEFAULT]);

    // Subnet-id.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    entry->set("subnet-id", Element::create(1));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(1, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[1]);

    // Subnet-prefix - IPv4.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    CfgMgr::instance().setFamily(AF_INET);
    Subnet4Ptr subnet4(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3, 1));
    CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet4);
    entry->set("subnet-prefix", Element::create("192.0.2.0/24"));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(1, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[1]);

    // Subnet-prefix - IPv6.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    CfgMgr::instance().setFamily(AF_INET6);
    Subnet6Ptr subnet6(new Subnet6(IOAddress("2001:db8::"),
                                   64, 1, 2, 3, 4, 1));
    CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet6);
    entry->set("subnet-prefix", Element::create("2001:db8::/64"));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(1, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[1]);

    // Shared-network - IPv4.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    CfgMgr::instance().setFamily(AF_INET);
    auto networks4 =
        CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks4();
    SharedNetwork4Ptr network4(new SharedNetwork4("frog"));
    networks4->add(network4);
    subnet4.reset(new Subnet4(IOAddress("192.0.2.0"), 25, 1, 2, 3, 1));
    subnet4->setReservationsGlobal(true);
    subnet4->setReservationsInSubnet(false);
    network4->add(subnet4);
    subnet4.reset(new Subnet4(IOAddress("192.0.2.128"), 25, 1, 2, 3, 2));
    subnet4->setReservationsGlobal(true);
    subnet4->setReservationsInSubnet(false);
    network4->add(subnet4);
    entry->set("shared-network-name", Element::create("frog"));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(2, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[1]);
    EXPECT_EQ(10, impl_.remap_[2]);

    // Shared-network - IPv6.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    CfgMgr::instance().setFamily(AF_INET6);
    auto networks6 =
        CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks6();
    SharedNetwork6Ptr network6(new SharedNetwork6("frog"));
    networks6->add(network6);
    subnet6.reset(new Subnet6(IOAddress("2001:db8::"), 64, 1, 2, 3, 4, 1));
    subnet6->setReservationsGlobal(true);
    subnet6->setReservationsInSubnet(false);
    network6->add(subnet6);
    subnet6.reset(new Subnet6(IOAddress("2001:db8:1::"), 64, 1, 2, 3, 4, 2));
    subnet6->setReservationsGlobal(true);
    subnet6->setReservationsInSubnet(false);
    network6->add(subnet6);
    entry->set("shared-network-name", Element::create("frog"));
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_EQ(2, impl_.remap_.size());
    EXPECT_EQ(10, impl_.remap_[1]);
    EXPECT_EQ(10, impl_.remap_[2]);

    // Error cases.
    CfgMgr::instance().clear();

    // Not a list.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createMap();
    config->set("nas-ports", ports);
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "listValue() called on non-list Element");
    EXPECT_TRUE(impl_.remap_.empty());

    // Not a map.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createList();
    ports->add(entry);
    config->set("nas-ports", ports);
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "get(string) called on a non-map Element");
    EXPECT_TRUE(impl_.remap_.empty());

    // Empty map.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    ports->add(entry);
    config->set("nas-ports", ports);
    string expected = "missing port in nas-ports entry: ";
    expected += entry->str();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);
    EXPECT_TRUE(impl_.remap_.empty());

    // No value.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("subnet-id", Element::create(1));
    ports->add(entry);
    config->set("nas-ports", ports);
    expected = "missing port in nas-ports entry: ";
    expected += entry->str();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);
    EXPECT_TRUE(impl_.remap_.empty());

    // Unknown selector.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    entry->set("id", Element::create(1));
    ports->add(entry);
    config->set("nas-ports", ports);
    expected = "unknown selector in ";
    expected += entry->str();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);
    EXPECT_TRUE(impl_.remap_.empty());

    // Can't find prefix.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    entry->set("subnet-prefix", Element::create("192.0.2.0/24"));
    ports->add(entry);
    config->set("nas-ports", ports);
    expected = "can't find subnet for ";
    expected += entry->str();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);
    EXPECT_TRUE(impl_.remap_.empty());

    // Can't find name.
    impl_.reset();
    config = Element::createMap();
    ports = Element::createList();
    entry = Element::createMap();
    entry->set("port", Element::create(10));
    entry->set("shared-network-name", Element::create("frog"));
    ports->add(entry);
    config->set("nas-ports", ports);
    expected = "can't find shared network for ";
    expected += entry->str();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError, expected);
    EXPECT_TRUE(impl_.remap_.empty());
}

// Verify syntax of services.
TEST_F(ConfigTest, services) {
    // Services can be empty.
    ElementPtr config = Element::createMap();
    config->set("access", Element::createMap());
    config->set("accounting", Element::createMap());
    EXPECT_NO_THROW(impl_.init(config));

    // Simple attribute.
    impl_.reset();
    config = Element::createMap();
    ElementPtr access = Element::createMap();
    config->set("access", access);
    ElementPtr attrs = Element::createList();
    access->set("attributes", attrs);
    ElementPtr attr = Element::createMap();
    attrs->add(attr);
    attr->set("name", Element::create("User-Name"));
    attr->set("type", Element::create(1));
    attr->set("data", Element::create("foobar"));
    EXPECT_NO_THROW(impl_.init(config));

    string expected = "{ "
        "\"attributes\": [ {"
        "   \"name\": \"User-Name\", "
        "   \"type\": 1, "
        "   \"data\": \"foobar\" "
        "} ] }";
    runToElementTest<RadiusService>(expected, *impl_.auth_);

    // Needs a server to be enabled.
    EXPECT_FALSE(impl_.auth_->enabled_);
    EXPECT_FALSE(impl_.acct_->enabled_);
    config = Element::createMap();
    ElementPtr acct = Element::createMap();
    config->set("accounting", acct);
    ElementPtr servers = Element::createList();
    acct->set("servers", servers);
    ElementPtr server1 = Element::createMap();
    servers->add(server1);
    impl_.reset();
    // Name and secret are required.
    server1->set("port", Element::create(16460));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'name' (:0:0) (parsing accounting)");
    impl_.reset();
    server1->set("name", Element::create("127.0.0.1"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'secret' (:0:0) (parsing accounting)");
    impl_.reset();
    server1->set("secret", Element::create("1111"));
    ElementPtr server2 = Element::createMap();
    servers->add(server2);
    server2->set("secret", Element::create("22222"));
    impl_.reset();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'name' (:0:0) (parsing accounting)");
    impl_.reset();
    server2->set("name", Element::create("::1"));
    EXPECT_NO_THROW(impl_.init(config));

    expected = "{ "
        "\"attributes\": [ ], "
        "\"servers\": [ {"
        "   \"deadtime\": 0, "
        "   \"local-address\": \"127.0.0.1\", "
        "   \"peer-address\": \"127.0.0.1\", "
        "   \"peer-port\": 16460, "
        "   \"secret\": \"1111\", "
        "   \"timeout\": 10 "
        "},{"
        "   \"deadtime\": 0, "
        "   \"local-address\": \"::1\", "
        "   \"peer-address\": \"::1\", "
        "   \"peer-port\": 1813, "
        "   \"secret\": \"22222\", "
        "   \"timeout\": 10 "
        "} ] }";
    runToElementTest<RadiusService>(expected, *impl_.acct_);

    // A server means enabled.
    EXPECT_TRUE(impl_.acct_->enabled_);

    // Check state.
    Servers srvs = impl_.acct_->servers_;
    ASSERT_EQ(2, srvs.size());
    ServerPtr srv = srvs[0];
    ASSERT_TRUE(srv);
    EXPECT_EQ("127.0.0.1", srv->getPeerAddress().toText());
    EXPECT_EQ(16460, srv->getPeerPort());

    // Add auth too.
    ElementPtr auth = Element::createMap();
    config->set("access", auth);
    ElementPtr auth_servers = Element::createList();
    auth->set("servers", auth_servers);
    ElementPtr auth_server1 = Element::createMap();
    auth_servers->add(auth_server1);
    impl_.reset();
    // Name and secret are required.
    auth_server1->set("port", Element::create(16450));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'name' (:0:0) (parsing access)");
    impl_.reset();
    auth_server1->set("name", Element::create("127.0.0.1"));
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'secret' (:0:0) (parsing access)");
    impl_.reset();
    auth_server1->set("secret", Element::create("1111"));
    ElementPtr auth_server2 = Element::createMap();
    auth_servers->add(auth_server2);
    auth_server2->set("secret", Element::create("22222"));
    impl_.reset();
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "missing parameter 'name' (:0:0) (parsing access)");
    impl_.reset();
    auth_server2->set("name", Element::create("::1"));
    EXPECT_NO_THROW(impl_.init(config));

    expected = "{ "
        "\"attributes\": [ ], "
        "\"servers\": [ {"
        "   \"deadtime\": 0, "
        "   \"local-address\": \"127.0.0.1\", "
        "   \"peer-address\": \"127.0.0.1\", "
        "   \"peer-port\": 16450, "
        "   \"secret\": \"1111\", "
        "   \"timeout\": 10 "
        "},{"
        "   \"deadtime\": 0, "
        "   \"local-address\": \"::1\", "
        "   \"peer-address\": \"::1\", "
        "   \"peer-port\": 1812, "
        "   \"secret\": \"22222\", "
        "   \"timeout\": 10 "
        "} ] }";
    runToElementTest<RadiusService>(expected, *impl_.auth_);

    // A server means enabled.
    EXPECT_TRUE(impl_.auth_->enabled_);

    // Check state.
    srvs = impl_.auth_->servers_;
    ASSERT_EQ(2, srvs.size());
    srv = srvs[0];
    ASSERT_TRUE(srv);
    EXPECT_EQ("127.0.0.1", srv->getPeerAddress().toText());
    EXPECT_EQ(16450, srv->getPeerPort());
}

// Verify parsing of attributes.
TEST_F(ConfigTest, attribute) {
    // Need the dictionary.
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));

    RadiusServicePtr srv(new RadiusService("test"));
    ASSERT_TRUE(srv);
    RadiusAttributeParser parser;

    // Attribute must be a map.
    ElementPtr attr = Element::createList();
    EXPECT_THROW_MSG(parser.parse(srv, attr), TypeError,
                     "get(string) called on a non-map Element");
    EXPECT_TRUE(srv->attributes_.empty());

    // Name or type is required.
    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "name or type are required");
    EXPECT_TRUE(srv->attributes_.empty());

    // Name must be known.
    attr->set("name", Element::create("No-Such-Attribute"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "attribute 'No-Such-Attribute' is unknown");

    // Type and name must match.
    attr->set("name", Element::create("User-Name"));
    attr->set("type", Element::create(123));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "User-Name attribute has type 1, not 123");

    // Type must be between 0 and 255.
    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    attr->set("type", Element::create(-1));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "out of range attribute type -1");

    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    attr->set("type", Element::create(256));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "out of range attribute type 256");

    // Type must be known too.
    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    attr->set("type", Element::create(234));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "attribute type 234 is unknown");

    // Set a type.
    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    attr->set("type", Element::create(1));
    EXPECT_NO_THROW(parser.parse(srv, attr));

    // Get the option to look at into.
    EXPECT_FALSE(srv->attributes_.empty());
    EXPECT_FALSE(srv->attributes_.getExpr(1));
    EXPECT_EQ("", srv->attributes_.getTest(1));
    const Attributes& attrs = srv->attributes_.getAll();
    ASSERT_EQ(1, attrs.size());
    const ConstAttributePtr& first = *attrs.cbegin();
    ASSERT_TRUE(first);
    EXPECT_EQ("User-Name=foobar", first->toText());

    // Another way to check.
    string expected = "[ { "
        " \"name\": \"User-Name\", "
        " \"type\": 1, "
        " \"data\": \"foobar\" } ]";
    runToElementTest<CfgAttributes>(expected, srv->attributes_);

    // One of expr, data, raw
    srv->attributes_.clear();
    attr = Element::createMap();
    attr->set("data", Element::create("foobar"));
    attr->set("type", Element::create(1));
    attr->set("expr", Element::create("option[1].hex"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "data, raw and expr are exclusive");

    attr = Element::createMap();
    attr->set("type", Element::create(1));
    attr->set("expr", Element::create("option[1].hex"));
    attr->set("raw", Element::create("01ef"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "data, raw and expr are exclusive");

    attr = Element::createMap();
    attr->set("type", Element::create(1));
    attr->set("data", Element::create("foobar"));
    attr->set("raw", Element::create("01ef"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "data and raw are exclusive");

    // Check with raw.
    srv->attributes_.clear();
    attr = Element::createMap();
    attr->set("type", Element::create(1));
    // Raw is in hexadecimal.
    attr->set("raw", Element::create("gaga"));
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError,
                     "can't decode raw: [gaga] for User-Name attribute");
    // Odd number of hexadecimal digits works.
    attr->set("raw", Element::create("1ac"));
    EXPECT_NO_THROW(parser.parse(srv, attr));

    // Try the raw version of User-Name f\001\0002bar.
    srv->attributes_.clear();
    attr->set("raw", Element::create("660102626172"));
    EXPECT_NO_THROW(parser.parse(srv, attr));
    EXPECT_EQ(1, srv->attributes_.getAll().size());
    EXPECT_FALSE(srv->attributes_.getExpr(1));
    EXPECT_EQ("", srv->attributes_.getTest(1));
    const ConstAttributePtr& firstr = srv->attributes_.get(1);
    ASSERT_TRUE(firstr);
    expected = "User-Name=f\x01\x02";
    expected += "bar";
    EXPECT_EQ(expected, firstr->toText());
    expected = "[ { "
        " \"name\": \"User-Name\", "
        " \"type\": 1, "
        " \"raw\": \"660102626172\" } ]";
    runToElementTest<CfgAttributes>(expected, srv->attributes_);

    // Check with expr.
    srv->attributes_.clear();
    attr = Element::createMap();
    attr->set("type", Element::create(1));
    // The expression must follow the eval syntax for strings.
    attr->set("expr", Element::create("'foobar"));
    expected = "expression: ['foobar] error: ";
    expected += "<string>:1.1: Invalid character: '";
    expected += " for User-Name attribute";
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError, expected);

    attr = Element::createMap();
    attr->set("type", Element::create(1));
    attr->set("expr", Element::create("option[1].exists"));
    expected = "expression: [option[1].exists] error: ";
    expected += "<string>:1.11-16: syntax error, unexpected exists, ";
    expected += "expecting option or text or hex";
    expected += " for User-Name attribute";
    EXPECT_THROW_MSG(parser.parse(srv, attr), ConfigError, expected);

    // Try the 'foobar' expression.
    attr->set("expr", Element::create("'foobar'"));
    EXPECT_NO_THROW(parser.parse(srv, attr));
    EXPECT_EQ(1, srv->attributes_.size());
    EXPECT_TRUE(srv->attributes_.getAll().empty());
    const ExpressionPtr& expr = srv->attributes_.getExpr(1);
    ASSERT_TRUE(expr);
    ASSERT_EQ(1, expr->size());
    TokenPtr token = (*expr)[0];
    boost::shared_ptr<TokenString> tokstr =
        boost::dynamic_pointer_cast<TokenString>(token);
    EXPECT_TRUE(tokstr);
    // No way to get the string from the token without evaluating it.

    EXPECT_EQ("'foobar'", srv->attributes_.getTest(1));
    expected = "[ { "
        " \"name\": \"User-Name\", "
        " \"type\": 1, "
        " \"expr\": \"'foobar'\" } ]";
    runToElementTest<CfgAttributes>(expected, srv->attributes_);
}

// Verify checkAttributes sanity check.
TEST_F(ConfigTest, checkAttributes) {
    // Need the dictionary.
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));

    // Enable services.
    impl_.auth_->enabled_ = true;
    impl_.acct_->enabled_ = true;

    // Build an attribute using an expression.
    ElementPtr srv_cfg = Element::createMap();
    RadiusServiceParser parser;
    ElementPtr attrs = Element::createList();
    srv_cfg->set("attributes", attrs);
    ElementPtr attr = Element::createMap();
    attrs->add(attr);
    attr->set("name", Element::create("User-Name"));
    attr->set("type", Element::create(1));
    attr->set("expr", Element::create("option[1].hex"));

    // Works with access.
    ASSERT_NO_THROW(parser.parse(impl_.auth_, srv_cfg));
    ASSERT_NO_THROW(parser.checkAttributes(impl_.auth_));

    // Throws with accounting,
    ASSERT_NO_THROW(parser.parse(impl_.acct_, srv_cfg));
    EXPECT_THROW(parser.checkAttributes(impl_.acct_), ConfigError);
}

// Verify checkEarlyGlobalResvLookup (vs. hook configuration).
TEST_F(ConfigTest, checkEarlyGlobalResvLookup) {
    CfgMgr::instance().setFamily(AF_INET);
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_FALSE(HostMgr::instance().getDisableSingleQuery());
    impl_.reset();

    // Set the flag in the staging config.
    CfgMgr::instance().getStagingCfg()->
        addConfiguredGlobal("early-global-reservations-lookup", Element::create(true));
    EXPECT_THROW(impl_.init(config), ConfigError);
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv4.
TEST_F(ConfigTest, checkSharedNetworks4) {
    CfgMgr::instance().setFamily(AF_INET);
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_FALSE(HostMgr::instance().getDisableSingleQuery());
    impl_.reset();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks4();
    SharedNetwork4Ptr network(new SharedNetwork4("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Retry: the disable single query should change.
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_TRUE(HostMgr::instance().getDisableSingleQuery());
    impl_.reset();

    // Add a subnet: a shared network with at most one subnet is ignored.
    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 25, 1, 2, 3, 1));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(impl_.init(config));
    impl_.reset();

    // Add a second subnet.
    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.128"), 25, 1, 2, 3, 2));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode.
    subnet1->setReservationsGlobal(true);
    subnet1->setReservationsInSubnet(false);
    subnet2->setReservationsGlobal(true);
    subnet2->setReservationsInSubnet(false);
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv4 using parent inheritance.
TEST_F(ConfigTest, parentCheckSharedNetworks4) {
    CfgMgr::instance().setFamily(AF_INET);
    ElementPtr config = Element::createMap();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks4();
    SharedNetwork4Ptr network(new SharedNetwork4("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Add two subnets.
    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 25, 1, 2, 3, 1));
    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.128"), 25, 1, 2, 3, 2));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode on the parent.
    network->setReservationsGlobal(true);
    network->setReservationsInSubnet(false);
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv4 using *staging* global inheritance.
TEST_F(ConfigTest, globalCheckSharedNetworks4) {
    CfgMgr::instance().setFamily(AF_INET);
    ElementPtr config = Element::createMap();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks4();
    SharedNetwork4Ptr network(new SharedNetwork4("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Add two subnets.
    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 25, 1, 2, 3, 1));
    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.128"), 25, 1, 2, 3, 2));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode at global scope in current config.
    CfgMgr::instance().getCurrentCfg()->
        addConfiguredGlobal("reservations-global", Element::create(true));
    CfgMgr::instance().getCurrentCfg()->
        addConfiguredGlobal("reservations-in-subnet", Element::create(false));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode at global scope in staging config.
    CfgMgr::instance().getStagingCfg()->
        addConfiguredGlobal("reservations-global", Element::create(true));
    CfgMgr::instance().getStagingCfg()->
        addConfiguredGlobal("reservations-in-subnet", Element::create(false));
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv6.
TEST_F(ConfigTest, checkSharedNetworks6) {
    CfgMgr::instance().setFamily(AF_INET6);
    ElementPtr config = Element::createMap();
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_FALSE(HostMgr::instance().getDisableSingleQuery());
    impl_.reset();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks6();
    SharedNetwork6Ptr network(new SharedNetwork6("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Retry: the disable single query should change.
    EXPECT_NO_THROW(impl_.init(config));
    EXPECT_TRUE(HostMgr::instance().getDisableSingleQuery());
    impl_.reset();

    // Add a subnet: a shared network with at most one subnet is ignored.
    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:0:1::"),
                                   64, 1, 2, 3, 4, 1));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(impl_.init(config));
    impl_.reset();

    // Add a second subnet.
    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:0:2::"),
                                   64, 1, 2, 3, 4, 2));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode.
    subnet1->setReservationsGlobal(true);
    subnet1->setReservationsInSubnet(false);
    subnet2->setReservationsGlobal(true);
    subnet2->setReservationsInSubnet(false);
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv6 using parent inheritance.
TEST_F(ConfigTest, parentCheckSharedNetworks6) {
    CfgMgr::instance().setFamily(AF_INET6);
    ElementPtr config = Element::createMap();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks6();
    SharedNetwork6Ptr network(new SharedNetwork6("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Add two subnets.
    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:0:1::"),
                                   64, 1, 2, 3, 4, 1));
    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:0:2::"),
                                   64, 1, 2, 3, 4, 2));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode on the parent.
    network->setReservationsGlobal(true);
    network->setReservationsInSubnet(false);
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify checkSharedNetworks server configuration (vs. hook configuration)
// checks for IPv6 using *staging* global inheritance.
TEST_F(ConfigTest, globalCheckSharedNetworks6) {
    CfgMgr::instance().setFamily(AF_INET6);
    ElementPtr config = Element::createMap();

    // Create a shared network.
    auto networks = CfgMgr::instance().getStagingCfg()->getCfgSharedNetworks6();
    SharedNetwork6Ptr network(new SharedNetwork6("frog"));
    ASSERT_NO_THROW(networks->add(network));

    // Add two subnets.
    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:0:1::"),
                                   64, 1, 2, 3, 4, 1));
    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:0:2::"),
                                   64, 1, 2, 3, 4, 2));
    EXPECT_NO_THROW(network->add(subnet1));
    EXPECT_NO_THROW(network->add(subnet2));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode at global scope in current config.
    CfgMgr::instance().getCurrentCfg()->
        addConfiguredGlobal("reservations-global", Element::create(true));
    CfgMgr::instance().getCurrentCfg()->
        addConfiguredGlobal("reservations-in-subnet", Element::create(false));
    EXPECT_THROW(impl_.init(config), ConfigError);
    impl_.reset();

    // Set global HR mode at global scope in staging config.
    CfgMgr::instance().getStagingCfg()->
        addConfiguredGlobal("reservations-global", Element::create(true));
    CfgMgr::instance().getStagingCfg()->
        addConfiguredGlobal("reservations-in-subnet", Element::create(false));
    EXPECT_NO_THROW(impl_.init(config));
}

// Verify that peer-updates can be configured and that proper errors are
// reported in negative cases.
TEST_F(ConfigTest, peerUpdates) {
    ElementPtr config;

    config = Element::fromJSON(R"({
        "access": {
            "peer-updates": false
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "peer-updates configured for the access service, but "
                     "it is only supported for the accounting service "
                     "(parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "peer-updates": true
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "peer-updates configured for the access service, but "
                     "it is only supported for the accounting service "
                     "(parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "peer-updates": 0
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "peer-updates configured for the access service, but "
                     "it is only supported for the accounting service "
                     "(parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "accounting": {
            "peer-updates": false
        }
    })");
    EXPECT_NO_THROW_LOG(impl_.init(config));
    EXPECT_FALSE(impl_.acct_->peer_updates_);
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "accounting": {
            "peer-updates": true
        }
    })");
    EXPECT_NO_THROW_LOG(impl_.init(config));
    EXPECT_TRUE(impl_.acct_->peer_updates_);
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "accounting": {
            "peer-updates": 0
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "expected peer-updates to be boolean, but got integer "
                     "instead (parsing accounting)");
    EXPECT_NO_THROW_LOG(impl_.reset());
}

// Verify that max-pending-requests can be configured and that proper errors
// are reported in negative cases.
TEST_F(ConfigTest, maxPendingRequests) {
    ElementPtr config;

    config = Element::fromJSON(R"({
        "accounting": {
            "max-pending-requests": 10
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "max-pending-requests configured for the accounting "
                     "service, but it is only supported for the access "
                     "service (parsing accounting)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "max-pending-requests": -1
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "expected max-pending-requests to be positive, but got "
                     "-1 instead (parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "max-pending-requests": false
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "expected max-pending-requests to be integer, but got "
                     "boolean instead (parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "max-pending-requests": 10
        }
    })");
    EXPECT_NO_THROW_LOG(impl_.init(config));
    EXPECT_EQ(10, impl_.auth_->max_pending_requests_);
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::createMap();
    EXPECT_NO_THROW_LOG(impl_.init(config));
    EXPECT_EQ(0, impl_.auth_->max_pending_requests_);
    EXPECT_NO_THROW_LOG(impl_.reset());

    config = Element::fromJSON(R"({
        "access": {
            "max-pending-request": 10
        }
    })");
    EXPECT_THROW_MSG(impl_.init(config), ConfigError,
                     "unknown service parameter: max-pending-request "
                     "(parsing access)");
    EXPECT_NO_THROW_LOG(impl_.reset());
}

} // end of anonymous namespace