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 | // Copyright (C) 2017-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 <asiolink/io_address.h>
#include <cc/data.h>
#include <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option.h>
#include <dhcp/option4_addrlst.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/parsers/shared_network_parser.h>
#include <testutils/gtest_utils.h>
#include <testutils/log_utils.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
namespace {
class SharedNetworkParserTest : public LogContentTest {
public:
/// @brief Structure for describing a single relay test scenario
struct RelayTest {
/// @brief Description of the test scenario, used for logging
std::string description_;
/// @brief JSON configuration body of the "relay" element
std::string json_content_;
/// @brief indicates if parsing should pass or fail
bool should_parse_;
/// @brief list of addresses expected after parsing
std::vector<IOAddress> addresses_;
};
/// @brief virtual destructor
virtual ~SharedNetworkParserTest() = default;
/// @brief Fetch valid shared network configuration JSON text
virtual std::string getWorkingConfig() const = 0;
ElementPtr makeTestConfig(const std::string& name, const std::string& json_content) {
// Create working config element tree
ElementPtr config = Element::fromJSON(getWorkingConfig());
// Create test element contents
ElementPtr content = Element::fromJSON(json_content);
// Add the test element to working config
config->set(name, content);
return (config);
}
/// @brief Executes a single "relay" parsing scenario
///
/// Each test pass consists of the following steps:
/// -# Attempt to parse the given JSON text
/// -# If parsing is expected to fail and it does return otherwise fatal fail
/// -# If parsing is expected to succeed, fatal fail if it does not
/// -# Verify the network's relay address list matches the expected list
/// in size and content.
///
/// @param test RelayTest which describes the test to conduct
void relayTest(const RelayTest& test) {
ElementPtr test_config;
ASSERT_NO_THROW(test_config =
makeTestConfig("relay", test.json_content_));
// Init our ref to a place holder
Network4 dummy;
Network& network = dummy;
// If parsing should fail, call parse expecting a throw.
if (!test.should_parse_) {
ASSERT_THROW(network = parseIntoNetwork(test_config), DhcpConfigError);
// No throw so test outcome is correct, nothing else to do.
return;
}
// Should parse without error, let's see if it does.
ASSERT_NO_THROW(network = parseIntoNetwork(test_config));
// It parsed, are the number of entries correct?
ASSERT_EQ(test.addresses_.size(), network.getRelayAddresses().size());
// Are the expected addresses in the list?
for (auto const& exp_address : test.addresses_) {
EXPECT_TRUE(network.hasRelayAddress(exp_address))
<< " expected address: " << exp_address.toText() << " not found" ;
}
}
/// @brief Attempts to parse the given configuration into a shared network
///
/// Virtual function used by relayTest() to parse a test configuration.
/// Implementation should not catch parsing exceptions.
///
/// @param test_config JSON configuration text to parse
/// @return A reference to the Network created if parsing is successful
virtual Network& parseIntoNetwork(ConstElementPtr test_config) = 0;
};
/// @brief Test fixture class for SharedNetwork4Parser class.
class SharedNetwork4ParserTest : public SharedNetworkParserTest {
public:
/// @brief Creates valid shared network configuration.
///
/// @return Valid shared network configuration.
virtual std::string getWorkingConfig() const {
std::string config = "{"
" \"allocator\": \"iterative\","
" \"authoritative\": true,"
" \"boot-file-name\": \"/dev/null\","
" \"client-class\": \"srv1\","
" \"interface\": \"eth1961\","
" \"match-client-id\": true,"
" \"name\": \"bird\","
" \"next-server\": \"10.0.0.1\","
" \"rebind-timer\": 199,"
" \"relay\": { \"ip-addresses\": [ \"10.1.1.1\" ] },"
" \"renew-timer\": 99,"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": true,"
" \"server-hostname\": \"example.org\","
" \"evaluate-additional-classes\": [ \"runner\" ],"
" \"user-context\": { \"comment\": \"example\" },"
" \"valid-lifetime\": 399,"
" \"min-valid-lifetime\": 299,"
" \"max-valid-lifetime\": 499,"
" \"calculate-tee-times\": true,"
" \"t1-percent\": 0.345,"
" \"t2-percent\": 0.721,"
" \"ddns-send-updates\": true,"
" \"ddns-override-no-update\": true,"
" \"ddns-override-client-update\": true,"
" \"ddns-replace-client-name\": \"always\","
" \"ddns-generated-prefix\": \"prefix\","
" \"ddns-qualifying-suffix\": \"example.com.\","
" \"hostname-char-set\": \"[^A-Z]\","
" \"hostname-char-replacement\": \"x\","
" \"store-extended-info\": true,"
" \"cache-threshold\": 0.123,"
" \"cache-max-age\": 123,"
" \"offer-lifetime\": 777,"
" \"ddns-update-on-renew\": true,"
" \"option-data\": ["
" {"
" \"name\": \"domain-name-servers\","
" \"data\": \"192.0.2.3\""
" }"
" ],"
" \"subnet4\": ["
" {"
" \"id\": 1,"
" \"subnet\": \"10.1.2.0/24\","
" \"interface\": \"\","
" \"renew-timer\": 100,"
" \"rebind-timer\": 200,"
" \"valid-lifetime\": 300,"
" \"min-valid-lifetime\": 200,"
" \"max-valid-lifetime\": 400,"
" \"match-client-id\": false,"
" \"authoritative\": false,"
" \"next-server\": \"\","
" \"server-hostname\": \"\","
" \"boot-file-name\": \"\","
" \"client-class\": \"\","
" \"evaluate-additional-classes\": []\n,"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": false,"
" \"4o6-interface\": \"\","
" \"4o6-interface-id\": \"\","
" \"4o6-subnet\": \"\","
" \"calculate-tee-times\": true,"
" \"t1-percent\": .45,"
" \"t2-percent\": .65,"
" \"hostname-char-set\": \"\","
" \"cache-threshold\": .20,"
" \"cache-max-age\": 50,"
" \"allocator\": \"random\""
" },"
" {"
" \"id\": 2,"
" \"subnet\": \"192.0.2.0/24\","
" \"interface\": \"\","
" \"renew-timer\": 10,"
" \"rebind-timer\": 20,"
" \"valid-lifetime\": 30,"
" \"match-client-id\": false,"
" \"authoritative\": false,"
" \"next-server\": \"\","
" \"server-hostname\": \"\","
" \"boot-file-name\": \"\","
" \"client-class\": \"\","
" \"evaluate-additional-classes\": []\n,"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": false,"
" \"4o6-interface\": \"\","
" \"4o6-interface-id\": \"\","
" \"4o6-subnet\": \"\","
" \"calculate-tee-times\": false,"
" \"t1-percent\": .40,"
" \"t2-percent\": .80,"
" \"cache-threshold\": .15,"
" \"cache-max-age\": 5"
" }"
" ]"
"}";
return (config);
}
virtual Network& parseIntoNetwork(ConstElementPtr test_config) {
// Parse configuration.
SharedNetwork4Parser parser;
network_ = parser.parse(test_config);
return (*network_);
}
private:
SharedNetwork4Ptr network_;
};
// This test verifies that shared network parser for IPv4 works properly
// in a positive test scenario.
TEST_F(SharedNetwork4ParserTest, parse) {<--- syntax error
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network. A bunch of parameters
// have to be specified for subnets because subnet parsers expect
// that default and global values are set.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
ASSERT_NO_THROW_LOG(network = parser.parse(config_element));
ASSERT_TRUE(network);
// Check basic parameters.
EXPECT_TRUE(network->getAuthoritative());
EXPECT_EQ("srv1", network->getClientClass().get());
EXPECT_EQ("bird", network->getName());
EXPECT_EQ("eth1961", network->getIface().get());
EXPECT_EQ(99, network->getT1().get());
EXPECT_EQ(199, network->getT2().get());
EXPECT_EQ(399, network->getValid().get());
EXPECT_EQ(299, network->getValid().getMin());
EXPECT_EQ(499, network->getValid().getMax());
EXPECT_TRUE(network->getCalculateTeeTimes());
EXPECT_EQ(0.345, network->getT1Percent());
EXPECT_EQ(0.721, network->getT2Percent());
EXPECT_EQ("/dev/null", network->getFilename().get());
EXPECT_EQ("10.0.0.1", network->getSiaddr().get().toText());
EXPECT_EQ("example.org", network->getSname().get());
EXPECT_FALSE(network->getReservationsGlobal());
EXPECT_TRUE(network->getReservationsInSubnet());
EXPECT_TRUE(network->getReservationsOutOfPool());
EXPECT_TRUE(network->getDdnsSendUpdates().get());
EXPECT_TRUE(network->getDdnsOverrideNoUpdate().get());
EXPECT_TRUE(network->getDdnsOverrideClientUpdate().get());
EXPECT_EQ(D2ClientConfig::RCM_ALWAYS, network->getDdnsReplaceClientNameMode().get());
EXPECT_EQ("prefix", network->getDdnsGeneratedPrefix().get());
EXPECT_EQ("example.com.", network->getDdnsQualifyingSuffix().get());
EXPECT_EQ("[^A-Z]", network->getHostnameCharSet().get());
EXPECT_EQ("x", network->getHostnameCharReplacement().get());
EXPECT_TRUE(network->getStoreExtendedInfo().get());
EXPECT_EQ(0.123, network->getCacheThreshold());
EXPECT_EQ(123, network->getCacheMaxAge().get());
EXPECT_EQ(777, network->getOfferLft().get());
EXPECT_TRUE(network->getDdnsUpdateOnRenew().get());
EXPECT_EQ("iterative", network->getAllocatorType().get());
// Relay information.
auto relay_info = network->getRelayInfo();
EXPECT_EQ(1, relay_info.getAddresses().size());
EXPECT_TRUE(relay_info.containsAddress(IOAddress("10.1.1.1")));
// Additional client classes.
auto additional = network->getAdditionalClasses();
ASSERT_EQ(1, additional.size());
EXPECT_EQ("runner", *additional.cbegin());
// Check user context.
ConstElementPtr context = network->getContext();
ASSERT_TRUE(context);
EXPECT_TRUE(context->get("comment"));
// Subnet with id 1
Subnet4Ptr subnet = network->getSubnet(SubnetID(1));
ASSERT_TRUE(subnet);
EXPECT_EQ("10.1.2.0", subnet->get().first.toText());
EXPECT_EQ(300, subnet->getValid().get());
EXPECT_EQ(200, subnet->getValid().getMin());
EXPECT_EQ(400, subnet->getValid().getMax());
EXPECT_FALSE(subnet->getHostnameCharSet().unspecified());
EXPECT_EQ("", subnet->getHostnameCharSet().get());
EXPECT_EQ("random", subnet->getAllocatorType().get());
// Subnet with id 2
subnet = network->getSubnet(SubnetID(2));
ASSERT_TRUE(subnet);
EXPECT_EQ("192.0.2.0", subnet->get().first.toText());
EXPECT_EQ(30, subnet->getValid().get());
EXPECT_EQ(30, subnet->getValid().getMin());
EXPECT_EQ(30, subnet->getValid().getMax());
EXPECT_EQ("[^A-Z]", subnet->getHostnameCharSet().get());
EXPECT_EQ("x", subnet->getHostnameCharReplacement().get());
EXPECT_EQ("iterative", subnet->getAllocatorType().get());
// DHCP options
ConstCfgOptionPtr cfg_option = network->getCfgOption();
ASSERT_TRUE(cfg_option);
OptionDescriptor opt_dns_servers = cfg_option->get("dhcp4",
DHO_DOMAIN_NAME_SERVERS);
ASSERT_TRUE(opt_dns_servers.option_);
Option4AddrLstPtr dns_servers = boost::dynamic_pointer_cast<
Option4AddrLst>(opt_dns_servers.option_);
ASSERT_TRUE(dns_servers);
Option4AddrLst::AddressContainer addresses = dns_servers->getAddresses();
ASSERT_EQ(1, addresses.size());
EXPECT_EQ("192.0.2.3", addresses[0].toText());
}
// This test verifies that parser throws an exception when mandatory parameter
// "name" is not specified.
TEST_F(SharedNetwork4ParserTest, missingName) {
// Remove a name parameter from the valid configuration.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ASSERT_NO_THROW(config_element->remove("name"));
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
ASSERT_THROW(network = parser.parse(config_element), DhcpConfigError);
}
// This test verifies that it's possible to specify client-class,
// match-client-id, and authoritative on shared-network level.
TEST_F(SharedNetwork4ParserTest, clientClassMatchClientIdAuthoritative) {
IfaceMgrTestConfig ifmgr(true);
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
config_element->set("authoritative", Element::create(true));
config_element->set("match-client-id", Element::create(false));
config_element->set("client-class", Element::create("alpha"));
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
network = parser.parse(config_element);
ASSERT_TRUE(network);
EXPECT_EQ("alpha", network->getClientClass().get());
EXPECT_FALSE(network->getMatchClientId());
EXPECT_TRUE(network->getAuthoritative());
}
// This test verifies that parsing of the "relay" element.
// It checks both valid and invalid scenarios.
TEST_F(SharedNetwork4ParserTest, relayInfoTests) {
IfaceMgrTestConfig ifmgr(true);
// Create the vector of test scenarios.
std::vector<RelayTest> tests = {
{
"valid ip-addresses #1",
"{ \"ip-addresses\": [ ] }",
true,
{}
},
{
"valid ip-addresses #2",
"{ \"ip-addresses\": [ \"192.168.2.1\" ] }",
true,
{ asiolink::IOAddress("192.168.2.1") }
},
{
"valid ip-addresses #3",
"{ \"ip-addresses\": [ \"192.168.2.1\", \"192.168.2.2\" ] }",
true,
{ asiolink::IOAddress("192.168.2.1"), asiolink::IOAddress("192.168.2.2") }
},
{
"invalid ip-addresses #1",
"{ \"ip-addresses\": [ \"not an address\" ] }",
false,
{ }
},
{
"invalid ip-addresses #2",
"{ \"ip-addresses\": [ \"2001:db8::1\" ] }",
false,
{ }
},
{
"invalid no ip-addresses",
"{}",
false,
{ }
}
};
// Iterate over the test scenarios, verifying each prescribed
// outcome.
for (auto const& test : tests) {
SCOPED_TRACE(test.description_);
relayTest(test);
}
}
// This test verifies that the optional interface check works as expected.
TEST_F(SharedNetwork4ParserTest, iface) {
// Basic configuration for shared network.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
// The interface check can be disabled.
SharedNetwork4Parser parser_no_check(false);
SharedNetwork4Ptr network;
EXPECT_NO_THROW(network = parser_no_check.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
// Retry with the interface check enabled.
SharedNetwork4Parser parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError);
// Configure default test interfaces.
IfaceMgrTestConfig ifmgr(true);
EXPECT_NO_THROW(network = parser_no_check.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
EXPECT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
}
// This test verifies that shared network parser for IPv4 works properly
// when using invalid renew and rebind timers.
TEST_F(SharedNetwork4ParserTest, parseWithInvalidRenewRebind) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network but with a renew-timer value
// larger than rebind-timer.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ConstElementPtr valid_element = config_element->get("rebind-timer");
int64_t value = valid_element->intValue();
valid_element = config_element->get("renew-timer");
ElementPtr mutable_element = boost::const_pointer_cast<Element>(valid_element);
mutable_element->setValue(value + 1);
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
// Parser should not throw.
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
// Veriy we emitted the proper log message.
addString("DHCPSRV_CFGMGR_RENEW_GTR_REBIND in shared-network bird,"
" the value of renew-timer 200 is greater than the value"
" of rebind-timer 199, ignoring renew-timer");
EXPECT_TRUE(checkFile());
}
// This test verifies that shared network parser for IPv4 works properly
// when renew and rebind timers are equal.
TEST_F(SharedNetwork4ParserTest, parseValidWithEqualRenewRebind) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ConstElementPtr valid_element = config_element->get("rebind-timer");
int64_t value = valid_element->intValue();
valid_element = config_element->get("renew-timer");
ElementPtr mutable_element = boost::const_pointer_cast<Element>(valid_element);
mutable_element->setValue(value);
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
}
// Test that FLQ allocator can be used for a shared network.
TEST_F(SharedNetwork4ParserTest, parseFLQAllocator) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network.
std::string config = "{ \"name\": \"lion\", \"allocator\": \"flq\" }";
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
EXPECT_EQ("flq", network->getAllocatorType().get());
}
/// @brief Test fixture class for SharedNetwork6Parser class.
class SharedNetwork6ParserTest : public SharedNetworkParserTest {
public:
/// @brief Constructor.
SharedNetwork6ParserTest()
: SharedNetworkParserTest(), network_(), use_iface_id_(false) {
}
/// @brief Creates valid shared network configuration.
///
/// @return Valid shared network configuration.
virtual std::string getWorkingConfig() const {
std::string config = "{"
" \"client-class\": \"srv1\","
+ std::string(use_iface_id_ ? "\"interface-id\": " : "\"interface\": ") +
"\"eth1961\","
" \"name\": \"bird\","
" \"preferred-lifetime\": 211,"
" \"min-preferred-lifetime\": 111,"
" \"max-preferred-lifetime\": 311,"
" \"rapid-commit\": true,"
" \"rebind-timer\": 199,"
" \"relay\": { \"ip-addresses\": [ \"2001:db8:1::1\" ] },"
" \"renew-timer\": 99,"
" \"evaluate-additional-classes\": [ \"runner\" ],"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": true,"
" \"user-context\": { },"
" \"valid-lifetime\": 399,"
" \"min-valid-lifetime\": 299,"
" \"max-valid-lifetime\": 499,"
" \"calculate-tee-times\": true,"
" \"t1-percent\": 0.345,"
" \"t2-percent\": 0.721,"
" \"ddns-send-updates\": true,"
" \"ddns-override-no-update\": true,"
" \"ddns-override-client-update\": true,"
" \"ddns-replace-client-name\": \"always\","
" \"ddns-generated-prefix\": \"prefix\","
" \"ddns-qualifying-suffix\": \"example.com.\","
" \"hostname-char-set\": \"[^A-Z]\","
" \"hostname-char-replacement\": \"x\","
" \"store-extended-info\": true,"
" \"cache-threshold\": 0.123,"
" \"cache-max-age\": 123,"
" \"ddns-update-on-renew\": true,"
" \"allocator\": \"random\","
" \"pd-allocator\": \"iterative\","
" \"option-data\": ["
" {"
" \"name\": \"dns-servers\","
" \"data\": \"2001:db8:1::cafe\""
" }"
" ],"
" \"subnet6\": ["
" {"
" \"id\": 1,"
" \"subnet\": \"3000::/16\","
" \"interface\": \"\","
" \"interface-id\": \"\","
" \"renew-timer\": 100,"
" \"rebind-timer\": 200,"
" \"preferred-lifetime\": 300,"
" \"min-preferred-lifetime\": 200,"
" \"max-preferred-lifetime\": 400,"
" \"valid-lifetime\": 400,"
" \"min-valid-lifetime\": 300,"
" \"max-valid-lifetime\": 500,"
" \"client-class\": \"\","
" \"evaluate-additional-classes\": []\n,"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": false,"
" \"rapid-commit\": false,"
" \"hostname-char-set\": \"\","
" \"allocator\": \"iterative\","
" \"pd-allocator\": \"random\""
" },"
" {"
" \"id\": 2,"
" \"subnet\": \"2001:db8:1::/64\","
" \"interface\": \"\","
" \"interface-id\": \"\","
" \"renew-timer\": 10,"
" \"rebind-timer\": 20,"
" \"preferred-lifetime\": 30,"
" \"valid-lifetime\": 40,"
" \"client-class\": \"\","
" \"evaluate-additional-classes\": []\n,"
" \"reservations-global\": false,"
" \"reservations-in-subnet\": true,"
" \"reservations-out-of-pool\": false,"
" \"rapid-commit\": false"
" }"
" ]"
"}";
return (config);
}
virtual Network& parseIntoNetwork(ConstElementPtr test_config) {
// Parse configuration.
SharedNetwork6Parser parser;
network_ = parser.parse(test_config);
return (*network_);
}
public:
SharedNetwork6Ptr network_;
/// Boolean flag indicating if the interface-id should be used instead
/// of interface.
bool use_iface_id_;
};
// This test verifies that shared network parser for IPv6 works properly
// in a positive test scenario.
TEST_F(SharedNetwork6ParserTest, parse) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network. A bunch of parameters
// have to be specified for subnets because subnet parsers expect
// that default and global values are set.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
// Check basic parameters.
EXPECT_EQ("srv1", network->getClientClass().get());
EXPECT_EQ("bird", network->getName());
EXPECT_EQ("eth1961", network->getIface().get());
EXPECT_EQ(211, network->getPreferred().get());
EXPECT_EQ(111, network->getPreferred().getMin());
EXPECT_EQ(311, network->getPreferred().getMax());
EXPECT_TRUE(network->getRapidCommit());
EXPECT_EQ(99, network->getT1().get());
EXPECT_EQ(199, network->getT2().get());
EXPECT_EQ(399, network->getValid().get());
EXPECT_EQ(299, network->getValid().getMin());
EXPECT_EQ(499, network->getValid().getMax());
EXPECT_TRUE(network->getCalculateTeeTimes());
EXPECT_EQ(0.345, network->getT1Percent());
EXPECT_EQ(0.721, network->getT2Percent());
EXPECT_TRUE(network->getDdnsSendUpdates().get());
EXPECT_TRUE(network->getDdnsOverrideNoUpdate().get());
EXPECT_TRUE(network->getDdnsOverrideClientUpdate().get());
EXPECT_EQ(D2ClientConfig::RCM_ALWAYS, network->getDdnsReplaceClientNameMode().get());
EXPECT_EQ("prefix", network->getDdnsGeneratedPrefix().get());
EXPECT_EQ("example.com.", network->getDdnsQualifyingSuffix().get());
EXPECT_EQ("[^A-Z]", network->getHostnameCharSet().get());
EXPECT_EQ("x", network->getHostnameCharReplacement().get());
EXPECT_TRUE(network->getStoreExtendedInfo().get());
EXPECT_EQ(0.123, network->getCacheThreshold());
EXPECT_EQ(123, network->getCacheMaxAge().get());
EXPECT_TRUE(network->getDdnsUpdateOnRenew().get());
EXPECT_EQ("random", network->getAllocatorType().get());
EXPECT_EQ("iterative", network->getPdAllocatorType().get());
// Relay information.
auto relay_info = network->getRelayInfo();
EXPECT_EQ(1, relay_info.getAddresses().size());
EXPECT_TRUE(relay_info.containsAddress(IOAddress("2001:db8:1::1")));
// Additional client classes.
auto additional = network->getAdditionalClasses();
ASSERT_EQ(1, additional.size());
EXPECT_EQ("runner", *additional.cbegin());
// Check user context.
ConstElementPtr context = network->getContext();
ASSERT_TRUE(context);
EXPECT_EQ(0, context->size());
// Subnet with id 1
Subnet6Ptr subnet = network->getSubnet(SubnetID(1));
ASSERT_TRUE(subnet);
EXPECT_EQ("3000::", subnet->get().first.toText());
EXPECT_EQ(300, subnet->getPreferred().get());
EXPECT_EQ(200, subnet->getPreferred().getMin());
EXPECT_EQ(400, subnet->getPreferred().getMax());
EXPECT_EQ(400, subnet->getValid().get());
EXPECT_EQ(300, subnet->getValid().getMin());
EXPECT_EQ(500, subnet->getValid().getMax());
EXPECT_FALSE(subnet->getHostnameCharSet().unspecified());
EXPECT_EQ("", subnet->getHostnameCharSet().get());
EXPECT_EQ("iterative", subnet->getAllocatorType().get());
EXPECT_EQ("random", subnet->getPdAllocatorType().get());
// Subnet with id 2
subnet = network->getSubnet(SubnetID(2));
ASSERT_TRUE(subnet);
EXPECT_EQ("2001:db8:1::", subnet->get().first.toText());
EXPECT_EQ(30, subnet->getPreferred().get());
EXPECT_EQ(30, subnet->getPreferred().getMin());
EXPECT_EQ(30, subnet->getPreferred().getMax());
EXPECT_EQ(40, subnet->getValid().get());
EXPECT_EQ(40, subnet->getValid().getMin());
EXPECT_EQ(40, subnet->getValid().getMax());
EXPECT_EQ("[^A-Z]", subnet->getHostnameCharSet().get());
EXPECT_EQ("x", subnet->getHostnameCharReplacement().get());
EXPECT_EQ("random", subnet->getAllocatorType().get());
EXPECT_EQ("iterative", subnet->getPdAllocatorType().get());
// DHCP options
ConstCfgOptionPtr cfg_option = network->getCfgOption();
ASSERT_TRUE(cfg_option);
OptionDescriptor opt_dns_servers = cfg_option->get("dhcp6",
D6O_NAME_SERVERS);
ASSERT_TRUE(opt_dns_servers.option_);
Option6AddrLstPtr dns_servers = boost::dynamic_pointer_cast<
Option6AddrLst>(opt_dns_servers.option_);
ASSERT_TRUE(dns_servers);
Option6AddrLst::AddressContainer addresses = dns_servers->getAddresses();
ASSERT_EQ(1, addresses.size());
EXPECT_EQ("2001:db8:1::cafe", addresses[0].toText());
}
// This test verifies that shared network parser for IPv6 works properly
// in a positive test scenario.
TEST_F(SharedNetwork6ParserTest, parseWithInterfaceId) {
IfaceMgrTestConfig ifmgr(true);
// Use the configuration with interface-id instead of interface parameter.
use_iface_id_ = true;
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
// Check that interface-id has been parsed.
auto opt_iface_id = network->getInterfaceId();
ASSERT_TRUE(opt_iface_id);
}
// This test verifies that shared network parser for IPv6 works properly
// when using invalid renew and rebind timers.
TEST_F(SharedNetwork6ParserTest, parseWithInvalidRenewRebind) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network but with a renew-timer value
// larger than rebind-timer.
use_iface_id_ = true;
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ConstElementPtr valid_element = config_element->get("rebind-timer");
int64_t value = valid_element->intValue();
valid_element = config_element->get("renew-timer");
ElementPtr mutable_element = boost::const_pointer_cast<Element>(valid_element);
mutable_element->setValue(value + 1);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
// Parser should not throw.
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
// Veriy we emitted the proper log message.
addString("DHCPSRV_CFGMGR_RENEW_GTR_REBIND in shared-network bird,"
" the value of renew-timer 200 is greater than the value"
" of rebind-timer 199, ignoring renew-timer");
EXPECT_TRUE(checkFile());
}
// This test verifies that shared network parser for IPv6 works properly
// when renew and rebind timers are equal.
TEST_F(SharedNetwork6ParserTest, parseValidWithEqualRenewRebind) {
IfaceMgrTestConfig ifmgr(true);
// Use the configuration with interface-id instead of interface parameter.
use_iface_id_ = true;
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ConstElementPtr valid_element = config_element->get("rebind-timer");
int64_t value = valid_element->intValue();
valid_element = config_element->get("renew-timer");
ElementPtr mutable_element = boost::const_pointer_cast<Element>(valid_element);
mutable_element->setValue(value);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
}
// This test verifies that error is returned when trying to configure a
// shared network with both interface and interface id.
TEST_F(SharedNetwork6ParserTest, mutuallyExclusiveInterfaceId) {
IfaceMgrTestConfig ifmgr(true);
// Use the configuration with interface-id instead of interface parameter.
use_iface_id_ = true;
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Add interface which is mutually exclusive with interface-id
config_element->set("interface", Element::create("eth1"));
// Parse configuration specified above.
SharedNetwork6Parser parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError);
}
// This test verifies that it's possible to specify client-class
// on shared-network level.
TEST_F(SharedNetwork6ParserTest, clientClass) {
IfaceMgrTestConfig ifmgr(true);
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
config_element->set("client-class", Element::create("alpha"));
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
network = parser.parse(config_element);
ASSERT_TRUE(network);
EXPECT_EQ("alpha", network->getClientClass().get());
}
// This test verifies that it's possible to specify evaluate-additional-classes
// on shared-network level.
TEST_F(SharedNetwork6ParserTest, evalClientClasses) {
IfaceMgrTestConfig ifmgr(true);
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
ElementPtr class_list = Element::createList();
class_list->add(Element::create("alpha"));
class_list->add(Element::create("beta"));
config_element->set("evaluate-additional-classes", class_list);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
network = parser.parse(config_element);
ASSERT_TRUE(network);
const ClientClasses& classes = network->getAdditionalClasses();
EXPECT_EQ(2, classes.size());
EXPECT_EQ("alpha, beta", classes.toText());
}
// This test verifies that bad evaluate-additional-classes configs raise
// expected errors.
TEST_F(SharedNetwork6ParserTest, badEvalClientClasses) {
IfaceMgrTestConfig ifmgr(true);
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Element of the list must be strings.
ElementPtr class_list = Element::createList();
class_list->add(Element::create("alpha"));
class_list->add(Element::create(1234));
config_element->set("evaluate-additional-classes", class_list);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
EXPECT_THROW(network = parser.parse(config_element), DhcpConfigError);
// Empty class name is forbidden.
class_list = Element::createList();
class_list->add(Element::create("alpha"));
class_list->add(Element::create(""));
EXPECT_THROW(network = parser.parse(config_element), DhcpConfigError);
// And of course the list must be a list even the parser can only
// trigger the previous error case...
class_list = Element::createMap();
EXPECT_THROW(network = parser.parse(config_element), DhcpConfigError);
}
// This test verifies that v6 parsing of the "relay" element.
// It checks both valid and invalid scenarios.
TEST_F(SharedNetwork6ParserTest, relayInfoTests) {
IfaceMgrTestConfig ifmgr(true);
// Create the vector of test scenarios.
std::vector<RelayTest> tests = {
{
"valid ip-addresses #1",
"{ \"ip-addresses\": [ ] }",
true,
{}
},
{
"valid ip-addresses #2",
"{ \"ip-addresses\": [ \"2001:db8::1\" ] }",
true,
{ asiolink::IOAddress("2001:db8::1") }
},
{
"valid ip-addresses #3",
"{ \"ip-addresses\": [ \"2001:db8::1\", \"2001:db8::2\" ] }",
true,
{ asiolink::IOAddress("2001:db8::1"), asiolink::IOAddress("2001:db8::2") }
},
{
"invalid ip-addresses #1",
"{ \"ip-addresses\": [ \"not an address\" ] }",
false,
{ }
},
{
"invalid ip-addresses #2",
"{ \"ip-addresses\": [ \"192.168.1.1\" ] }",
false,
{ }
},
{
"invalid no ip-addresses",
"{}",
false,
{ }
}
};
// Iterate over the test scenarios, verifying each prescribed
// outcome.
for (auto const& test : tests) {
SCOPED_TRACE(test.description_);
relayTest(test);
}
}
// This test verifies that the optional interface check works as expected.
TEST_F(SharedNetwork6ParserTest, iface) {
// Basic configuration for shared network.
std::string config = getWorkingConfig();
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
// The interface check can be disabled.
SharedNetwork6Parser parser_no_check(false);
SharedNetwork6Ptr network;
EXPECT_NO_THROW(network = parser_no_check.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
// Retry with the interface check enabled.
SharedNetwork6Parser parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError);
// Configure default test interfaces.
IfaceMgrTestConfig ifmgr(true);
EXPECT_NO_THROW(network = parser_no_check.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
EXPECT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
EXPECT_FALSE(network->getIface().unspecified());
EXPECT_EQ("eth1961", network->getIface().get());
}
// Test that the FLQ allocator can't be used for DHCPv6 address assignment.
TEST_F(SharedNetwork6ParserTest, parseFLQAllocatorNA) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network.
std::string config = "{ \"name\": \"lion\", \"allocator\": \"flq\" }";
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork6Parser parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError);
}
// Test that FLQ allocator can be used for prefix delegation.
TEST_F(SharedNetwork6ParserTest, parseFLQAllocatorPD) {
IfaceMgrTestConfig ifmgr(true);
// Basic configuration for shared network.
std::string config = "{ \"name\": \"lion\", \"pd-allocator\": \"flq\" }";
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
EXPECT_EQ("flq", network->getPdAllocatorType().get());
}
// Verify that deprecated require-client-classes is handled properly
// by v4 parser.
TEST_F(SharedNetwork4ParserTest, deprecatedRequireClientClasses) {
// Valid entry.
std::string config =
R"^({
"name": "foo",
"require-client-classes": [ "one", "two" ]
})^";
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork4Parser parser;
SharedNetwork4Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
const auto cclasses = network->getAdditionalClasses();
EXPECT_EQ(cclasses.size(), 2);
auto cclass = cclasses.begin();
EXPECT_EQ(*cclass, "one");
++cclass;
EXPECT_EQ(*cclass, "two");
// Invalid entry specifies both parameters.
config =
R"^({
"name": "foo",
"require-client-classes": [ "one", "two" ],
"evaluate-additional-classes": [ "one", "two" ]
})^";
config_element = Element::fromJSON(config);
// Should throw a complaint.
ASSERT_THROW_MSG(parser.parse(config_element),
DhcpConfigError,
"cannot specify both 'require-client-classes' and"
" 'evaluate-additional-classes'. Use only the latter."
" (<string>:1:2)");
}
// Verify that deprecated require-client-classes is handled properly
// by v6 parser.
TEST_F(SharedNetwork6ParserTest, deprecatedRequireClientClasses) {
// Valid entry.
std::string config =
R"^({
"name": "foo",
"require-client-classes": [ "one", "two" ]
})^";
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
SharedNetwork6Parser parser;
SharedNetwork6Ptr network;
ASSERT_NO_THROW(network = parser.parse(config_element));
ASSERT_TRUE(network);
const auto cclasses = network->getAdditionalClasses();
EXPECT_EQ(cclasses.size(), 2);
auto cclass = cclasses.begin();
EXPECT_EQ(*cclass, "one");
++cclass;
EXPECT_EQ(*cclass, "two");
// Invalid entry specifies both parameters.
config =
R"^({
"name": "foo",
"require-client-classes": [ "one", "two" ],
"evaluate-additional-classes": [ "one", "two" ]
})^";
config_element = Element::fromJSON(config);
// Should throw a complaint.
ASSERT_THROW_MSG(parser.parse(config_element),
DhcpConfigError,
"cannot specify both 'require-client-classes' and"
" 'evaluate-additional-classes'. Use only the latter."
" (<string>:1:2)");
}
} // end of anonymous namespace
|