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
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710 | // Copyright (C) 2019-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.
#include <config.h>
#include <cb_cmds_test.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <database/backend_selector.h>
#include <dhcp/option_string.h>
#include <dhcp/option6_addrlst.h>
#include <dhcpsrv/config_backend_dhcp6_mgr.h>
#include <set><--- 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.
#include <sstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::asiolink;
using namespace isc::cb;
using namespace isc::cb::test;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::db;
namespace {
/// @brief Test fixture class for callouts pertaining to subnets.
class Subnet6CmdsTest : public ConfigCmdsDhcp6Test {
public:
/// @brief Constructor.
Subnet6CmdsTest() {
initTestSharedNetwork6();
}
/// @brief Inserts shared network used in tests into the database.
void initTestSharedNetwork6() {
SharedNetwork6Ptr shared_network = SharedNetwork6::create("test6");
ConfigBackendDHCPv6Mgr::instance().getPool()->
createUpdateSharedNetwork6(BackendSelector(), ServerSelector::ALL(),
shared_network);
}
/// @brief Adds unassigned subnet to the configuration backend.
///
/// @param subnet Pointer to the subnet to be stored.
void setSubnet(const Subnet6Ptr& subnet) {
ConfigBackendDHCPv6Mgr::instance().getPool()->
createUpdateSubnet6(BackendSelector(), ServerSelector::UNASSIGNED(),
subnet);
}
/// @brief Returns remote-subnet6-set command as text.
///
/// @param shared_network_name Name to be associated with the subnet.
/// @param subnet Pointer to the subnet to be used in the command.
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
/// @param server_tags Server tags to be included in the command.
///
/// @return Command in the textual format.
std::string makeSubnet6SetConfig(const std::string& shared_network_name,
const Subnet6Ptr& subnet,
const std::string& remote_map,
const std::set<std::string>& server_tags) {
auto subnet_element = subnet->toElement();
// Always include shared-network-name.
if (shared_network_name == "null") {
subnet_element->set("shared-network-name", Element::create());
} else {
subnet_element->set("shared-network-name",
Element::create(shared_network_name));
}
std::string config = "{"
"\"command\": \"remote-subnet6-set\","
"\"arguments\": {"
" \"subnets\": [";
config += subnet_element->str() + "]";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += ", \"remote\": " + remote_map;
}
// Only include server tags if specified.
if (!server_tags.empty()) {
config += ", \"server-tags\": [";
for (auto const& tag : server_tags) {
config += "\"" + tag + "\",";
}
// Remove extraneous comma.
config.pop_back();
config += "]";
}
config += "} }";
return (config);
}
/// @brief Positive test scenario for setting or updating a subnet
/// belonging to a shared network.
///
/// @param shared_network_name Name to be associated with the subnet.
/// @param subnet Pointer to the subnet to be created or updated.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
/// @param server_tags Server tags to be included in the command.
void testSubnet6Set(const std::string& shared_network_name,
const Subnet6Ptr& subnet,
const std::string& remote_map = "",
const std::set<std::string>& server_tags = { ServerTag::ALL }) {
// Generate the command as text.
std::string command_text = makeSubnet6SetConfig(shared_network_name,
subnet, remote_map,
server_tags);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::setSubnet6,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_SUCCESS, rcode);
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ("IPv6 subnet successfully set.", msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
// Validate returned list of subnets.
auto subnet_list = args->get("subnets");
ASSERT_TRUE(subnet_list);
EXPECT_EQ(Element::list, subnet_list->getType());
ASSERT_EQ(1, subnet_list->size());
auto subnet_0 = subnet_list->get(0);
ASSERT_TRUE(subnet_0);
ASSERT_EQ(Element::map, subnet_0->getType());
EXPECT_EQ(2, subnet_0->size());
auto subnet_0_id = subnet_0->get("id");
ASSERT_TRUE(subnet_0_id);
ASSERT_EQ(Element::integer, subnet_0_id->getType());
EXPECT_EQ(subnet->getID(), static_cast<uint32_t>(subnet_0_id->intValue()));
auto subnet_0_subnet = subnet_0->get("subnet");
ASSERT_TRUE(subnet_0_subnet);
ASSERT_EQ(Element::string, subnet_0_subnet->getType());
EXPECT_EQ(subnet->toText(), subnet_0_subnet->stringValue());
// Make sure that the created/updated subnet is in the database
// and has correct values and metadata.
auto fetched_subnet = ConfigBackendDHCPv6Mgr::instance().getPool()->
getSubnet6(BackendSelector(),
ServerSelector::ONE(*server_tags.begin()),
subnet->getID());
ASSERT_TRUE(fetched_subnet);
EXPECT_EQ(subnet->toElement()->str(), fetched_subnet->toElement()->str());
// Make sure that the subnet options are not encapsulated.
EXPECT_FALSE(fetched_subnet->getCfgOption()->isEncapsulated());
if (shared_network_name != "null") {
EXPECT_EQ(shared_network_name, fetched_subnet->getSharedNetworkName());
} else {
EXPECT_TRUE(fetched_subnet->getSharedNetworkName().empty());
}
auto metadata = fetched_subnet->getMetadata();
ASSERT_TRUE(metadata);
auto expected_metadata = createMetadata(server_tags);
EXPECT_EQ(expected_metadata->str(), metadata->str());
}
/// @brief Positive test scenario for setting or updating a subnet.
///
/// @param subnet Pointer to the subnet to be created or updated.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
///
/// @param server_tags Server tags to be included in the command.
void testSubnet6Set(const Subnet6Ptr& subnet,
const std::string& remote_map = "",
const std::set<std::string>& server_tags = { ServerTag::ALL }) {
testSubnet6Set("", subnet, remote_map, server_tags);
}
/// @brief Negative test scenario for setting or updating a subnet.
///
/// @param args Arguments for the command in the textual format.
/// @param answer_text Expected error message.
void testSubnet6SetFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-set\","
"\"arguments\": {" + args + "}"
"}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::setSubnet6,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
ASSERT_EQ(CONTROL_RESULT_ERROR, rcode);
/// @todo Skipping line number possibly appended to the error message by
/// the subnet parser. We should modify the parser to not output the
/// position when used for control commands parsing.
EXPECT_EQ(0, answer->stringValue().find(answer_text))
<< "expected: " << answer_text
<< ", found: " << answer->stringValue();
}
/// @brief Returns remote-subnet6-del-by-id command as text.
///
/// @param subnet Pointer to the subnet to be used in the command.
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
///
/// @return Command in the textual format.
std::string makeSubnet6DelIdConfig(const Subnet6Ptr& subnet,
const std::string& remote_map) {
std::string config = "{"
"\"command\": \"remote-subnet6-del-by-id\","
"\"arguments\": {"
" \"subnets\": [ { ";
std::ostringstream id;
id << subnet->getID();
config += "\"id\": " + id.str() + " } ]";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += ", \"remote\": " + remote_map;
}
// No server tags.
config += "} }";
return (config);
}
/// @brief Positive test scenario for deleting a subnet by id.
///
/// @param subnet Pointer to the subnet to be deleted.
/// @param expected_count Expected number of deleted subnets.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
void testSubnet6DelId(const Subnet6Ptr& subnet,
unsigned expected_count,
const std::string& remote_map = "") {
// Generate the command as text.
std::string command_text = makeSubnet6DelIdConfig(subnet, remote_map);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delSubnet6Id,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
int expected_rcode = (expected_count == 0 ?
CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
EXPECT_EQ(expected_rcode, rcode);
std::ostringstream expected_msg;
expected_msg << expected_count << " IPv6 subnet(s) deleted.";
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ(expected_msg.str(), msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
// Validate count.
auto count = args->get("count");
ASSERT_TRUE(count);
ASSERT_EQ(Element::integer, count->getType());
EXPECT_EQ(expected_count, count->intValue());
// Make sure that the deleted subnet is not in the database.
auto fetched_subnet = ConfigBackendDHCPv6Mgr::instance().getPool()->
getSubnet6(BackendSelector(),
ServerSelector::ANY(),
subnet->getID());
ASSERT_FALSE(fetched_subnet);
}
/// @brief Negative test scenario for deleting a subnet by id.
///
/// @param args Arguments for the command in the textual format.
/// @param answer_text Expected error message.
void testSubnet6DelIdFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-del-by-id\","
"\"arguments\": {" + args + "}"
"}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::delSubnet6Id,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);
EXPECT_EQ(answer_text, answer->stringValue());
}
/// @brief Returns remote-subnet6-del-by-prefix command as text.
///
/// @param subnet Pointer to the subnet to be used in the command.
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
///
/// @return Command in the textual format.
std::string makeSubnet6DelPrefixConfig(const Subnet6Ptr& subnet,
const std::string& remote_map) {
std::string config = "{"
"\"command\": \"remote-subnet6-del-by-prefix\","
"\"arguments\": {"
" \"subnets\": [ { ";
config += "\"subnet\": \"" + subnet->toText() + "\" } ]";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += ", \"remote\": " + remote_map;
}
// No server tags.
config += "} }";
return (config);
}
/// @brief Positive test scenario for deleting a subnet by prefix.
///
/// @param subnet Pointer to the subnet to be deleted.
/// @param expected_count Expected number of deleted subnets.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
void testSubnet6DelPrefix(const Subnet6Ptr& subnet,
unsigned expected_count,
const std::string& remote_map = "") {
// Generate the command as text.
std::string command_text =
makeSubnet6DelPrefixConfig(subnet, remote_map);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::delSubnet6Prefix, command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
int expected_rcode = (expected_count == 0 ?
CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
EXPECT_EQ(expected_rcode, rcode);
std::ostringstream expected_msg;
expected_msg << expected_count << " IPv6 subnet(s) deleted.";
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ(expected_msg.str(), msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
// Validate count.
auto count = args->get("count");
ASSERT_TRUE(count);
ASSERT_EQ(Element::integer, count->getType());
EXPECT_EQ(expected_count, count->intValue());
// Make sure that the deleted subnet is not in the database.
auto fetched_subnet = ConfigBackendDHCPv6Mgr::instance().getPool()->
getSubnet6(BackendSelector(),
ServerSelector::ANY(),
subnet->getID());
ASSERT_FALSE(fetched_subnet);
}
/// @brief Negative test scenario for deleting a subnet by prefix.
///
/// @param args Arguments for the command in the textual format.
/// @param answer_text Expected error message.
void testSubnet6DelPrefixFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-del-by-prefix\","
"\"arguments\": {" + args + "}"
"}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::delSubnet6Prefix, command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);
EXPECT_EQ(answer_text, answer->stringValue());
}
/// @brief Returns remote-subnet6-get-by-id command as text.
///
/// @param subnet Pointer to the subnet to be used in the command.
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
///
/// @return Command in the textual format.
std::string makeSubnet6GetIdConfig(const Subnet6Ptr& subnet,
const std::string& remote_map) {
std::string config = "{"
"\"command\": \"remote-subnet6-get-by-id\","
"\"arguments\": {"
" \"subnets\": [ { ";
std::ostringstream id;
id << subnet->getID();
config += "\"id\": " + id.str() + " } ]";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += ", \"remote\": " + remote_map;
}
// No server tags.
config += "} }";
return (config);
}
/// @brief Positive test scenario for getting a subnet by id.
///
/// @param subnet Pointer to the subnet to be got.
/// @param expected Pointer to the expected subnet.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
void testSubnet6GetId(const Subnet6Ptr& subnet,
const Subnet6Ptr& expected,
const std::string& remote_map = "") {
// Generate the command as text.
std::string command_text = makeSubnet6GetIdConfig(subnet, remote_map);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::getSubnet6Id,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
int expected_rcode = (!expected ?
CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
EXPECT_EQ(expected_rcode, rcode);
std::ostringstream expected_msg;
expected_msg << "IPv6 subnet " << subnet->getID() << " "
<< (!expected ? "not " : "") << "found.";
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ(expected_msg.str(), msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
// Validate returned subnet.
ConstElementPtr subnets = args->get("subnets");
ASSERT_TRUE(subnets);
ASSERT_EQ(Element::list, subnets->getType());
ConstElementPtr count = args->get("count");
ASSERT_TRUE(count);
ASSERT_EQ(Element::integer, count->getType());
if (!expected) {
EXPECT_EQ(0, subnets->size());
EXPECT_EQ(0, count->intValue());
} else {
ASSERT_EQ(1, subnets->size());
ConstElementPtr got = subnets->get(0);
ElementPtr expected_element = expected->toElement();
// Shared network name must always be returned.
std::string sn_name = subnet->getSharedNetworkName();
if (!sn_name.empty()) {
expected_element->set("shared-network-name",
Element::create(sn_name));
} else {
// Shared network name is not specified. Output null value.
expected_element->set("shared-network-name",
Element::create());
}
// Make sure that expected element includes metadata.
auto tags = expected->getServerTags();
ASSERT_FALSE(tags.empty());
expected_element->set("metadata",
createMetadata(tags.begin()->get()));
EXPECT_TRUE(isEquivalent(got, expected_element))
<< "Actual: " << got->str()
<< "\nExpected: " << expected_element->str();
EXPECT_EQ(1, count->intValue());
}
}
/// @brief Negative test scenario for getting a subnet by id.
///
/// @param args Arguments for the command in the textual format.
/// @param answer_text Expected error message.
void testSubnet6GetIdFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-get-by-id\","
"\"arguments\": {" + args + "}"
"}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response = impl_.run(&TestConfigCmdsDhcp6Impl::getSubnet6Id,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);
EXPECT_EQ(answer_text, answer->stringValue());
}
/// @brief Returns remote-subnet6-get-by-prefix command as text.
///
/// @param subnet Pointer to the subnet to be used in the command.
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
///
/// @return Command in the textual format.
std::string makeSubnet6GetPrefixConfig(const Subnet6Ptr& subnet,
const std::string& remote_map) {
std::string config = "{"
"\"command\": \"remote-subnet6-get-by-prefix\","
"\"arguments\": {"
" \"subnets\": [ { ";
config += "\"subnet\": \"" + subnet->toText() + "\" } ]";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += ", \"remote\": " + remote_map;
}
// No server tags.
config += "} }";
return (config);
}
/// @brief Positive test scenario for getting a subnet by prefix.
///
/// @param subnet Pointer to the subnet to be got.
/// @param expected Pointer to the expected subnet.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
void testSubnet6GetPrefix(const Subnet6Ptr& subnet,
const Subnet6Ptr& expected,
const std::string& remote_map = "") {
// Generate the command as text.
std::string command_text =
makeSubnet6GetPrefixConfig(subnet, remote_map);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::getSubnet6Prefix, command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
int expected_rcode = (!expected ?
CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
EXPECT_EQ(expected_rcode, rcode);
std::ostringstream expected_msg;
expected_msg << "IPv6 subnet " << subnet->toText()
<< (!expected ? " not " : " ") << "found.";
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ(expected_msg.str(), msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
// Validate returned subnet.
ConstElementPtr subnets = args->get("subnets");
ASSERT_TRUE(subnets);
ASSERT_EQ(Element::list, subnets->getType());
ConstElementPtr count = args->get("count");
ASSERT_TRUE(count);
ASSERT_EQ(Element::integer, count->getType());
if (!expected) {
EXPECT_EQ(0, subnets->size());
EXPECT_EQ(0, count->intValue());
} else {
ASSERT_EQ(1, subnets->size());
ConstElementPtr got = subnets->get(0);
ElementPtr expected_element = expected->toElement();
// Shared network name must always be returned.
std::string sn_name = subnet->getSharedNetworkName();
if (!sn_name.empty()) {
expected_element->set("shared-network-name",
Element::create(sn_name));
} else {
// Shared network name is not specified. Output null value.
expected_element->set("shared-network-name",
Element::create());
}
// Make sure that expected element includes metadata.
auto tags = expected->getServerTags();
ASSERT_FALSE(tags.empty());
expected_element->set("metadata",
createMetadata(tags.begin()->get()));
EXPECT_TRUE(isEquivalent(got, expected_element))
<< "Actual: " << got->str()
<< "\nExpected: " << expected_element->str();
EXPECT_EQ(1, count->intValue());
}
}
/// @brief Negative test scenario for getting a subnet by prefix.
///
/// @param args Arguments for the command in the textual format.
/// @param answer_text Expected error message.
void testSubnet6GetPrefixFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-get-by-prefix\","
"\"arguments\": {" + args + "}"
"}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::getSubnet6Prefix, command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);
EXPECT_EQ(answer_text, answer->stringValue());
}
/// @brief Returns remote-subnet6-list command as text.
///
/// @param remote_map A map to be assigned to the "remote" argument
/// of the command. If the string is empty the "remote" argument is
/// not included.
/// @param server_tags Server tags to be included in the command.
///
/// @return Command in the textual format.
std::string makeSubnet6ListConfig(const std::string& remote_map,
const std::set<std::string>& server_tags) {
std::string config = "{"
"\"command\": \"remote-subnet6-list\""
", \"arguments\": {";
// Only include "remote" parameter if non empty.
if (!remote_map.empty()) {
config += "\"remote\": " + remote_map + ",";
}
// Always include server tags,
config += "\"server-tags\": [";
if (!server_tags.empty()) {
for (auto const& tag : server_tags) {
config += "\"" + tag + "\",";
}
// Remove extraneous comma.
config.pop_back();
} else {
// Empty means unassigned.
config += " null ";
}
config += "] } }";
return (config);
}
/// @brief Positive test scenario for getting all subnets.
///
/// @param expected Expected result.
/// @param remote_map A map to be assigned to the "remote" argument. The
/// default value is the empty string in which case the "remote" argument
/// is not included in the command.
/// @param server_tags Server tags to be included in the command,
/// empty means UNASSIGNED.
void testSubnet6List(ConstElementPtr expected,
const std::string& remote_map = "",
const std::set<std::string>& server_tags = { ServerTag::ALL }) {
// Sanity.
ASSERT_TRUE(expected) << " bad test";
ASSERT_EQ(Element::list, expected->getType()) << " bad test";
// Generate the command as text.
std::string command_text = makeSubnet6ListConfig(remote_map, server_tags);
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::listSubnets6,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates success.
int rcode = 0;
auto args = parseAnswer(rcode, response);
int expected_rcode = (expected->empty() ?
CONTROL_RESULT_EMPTY : CONTROL_RESULT_SUCCESS);
EXPECT_EQ(expected_rcode, rcode);
std::ostringstream expected_msg;
expected_msg << expected->size() << " IPv6 subnet(s) found.";
auto msg = response->get(CONTROL_TEXT);
ASSERT_TRUE(msg);
EXPECT_EQ(expected_msg.str(), msg->stringValue());
ASSERT_TRUE(args);
ASSERT_EQ(Element::map, args->getType());
auto count = args->get("count");
ASSERT_TRUE(count);
ASSERT_EQ(Element::integer, count->getType());
EXPECT_EQ(expected->size(), count->intValue());
// Validate returned subnets.
// Note this includes the metadata.
ConstElementPtr subnets = args->get("subnets");
ASSERT_TRUE(subnets);
EXPECT_TRUE(isEquivalent(expected, subnets))
<< "Actual: " << subnets->str()
<< "\nExpected: " << expected->str();
}
/// @brief Negative test scenario for getting all subnets.
///
/// @param args Arguments for the command in the textual format not
/// including "arguments".
/// @param answer_text Expected error message.
void testSubnet6ListFail(const std::string& args,
const std::string& answer_text) {
// Generate the command as text.
std::string command_text = "{"
"\"command\": \"remote-subnet6-list\" " + args + "}";
// Invoke the callout and get the response.
ConstElementPtr response;
ASSERT_NO_THROW(response =
impl_.run(&TestConfigCmdsDhcp6Impl::listSubnets6,
command_text));
ASSERT_TRUE(response);
// Make sure that the response indicates error.
int rcode = 0;
auto answer = parseAnswer(rcode, response);
EXPECT_EQ(CONTROL_RESULT_ERROR, rcode);
EXPECT_EQ(answer_text, answer->stringValue());
}
};
// This test verifies that it is possible to add and update subnet.
TEST_F(Subnet6CmdsTest, subnet6Set) {
{
SCOPED_TRACE("add new subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
testSubnet6Set(subnet, "", { "server1" });
}
{
SCOPED_TRACE("add another subnet within shared network");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
testSubnet6Set("test6", subnet);
}
{
SCOPED_TRACE("update existing subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 40, 50, 60, 70, 1));
testSubnet6Set(subnet, "", { "server1" });
}
{
SCOPED_TRACE("update another subnet with specifying remote");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 40, 50, 60, 70, 2));
testSubnet6Set("test6", subnet, "{ \"type\": \"mysql\" }");
}
{
SCOPED_TRACE("nullify subnet's shared network");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 40, 50, 60, 70, 2));
testSubnet6Set("null", subnet);
}
{
SCOPED_TRACE("add a subnet for multiple servers");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:3::"), 64, 40, 50, 60, 70, 3));
testSubnet6Set(subnet, "", { "server1", "server2" });
}
{
SCOPED_TRACE("add a subnet with unknown interface");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:4::"), 64, 40, 50, 60, 70, 4));
subnet->setIface("foobar");
testSubnet6Set(subnet, "", { "server1" });
}
}
// This test verifies that it is possible to add a subnet with
// DHCP options at the subnet and pool levels.
TEST_F(Subnet6CmdsTest, subnet6SetWithOptions) {
{
SCOPED_TRACE("add a subnet with standard option");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 50, 60, 4));
OptionStringPtr option(new OptionString(Option::V6, D6O_BOOTFILE_URL,
"http://myurl"));
OptionDescriptor option_desc(option, false, false, "http://myurl");
option_desc.space_name_ = DHCP6_OPTION_SPACE;
subnet->getCfgOption()->add(option_desc, DHCP6_OPTION_SPACE);
Option6AddrLstPtr option2(new Option6AddrLst(D6O_NIS_SERVERS, IOAddress("2001:db8:1::1")));
OptionDescriptor option_desc2(option2, false, false);
option_desc2.space_name_ = DHCP6_OPTION_SPACE;
Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"),
IOAddress("2001:db8:1::100")));
pool->getCfgOption()->add(option_desc2, DHCP6_OPTION_SPACE);
subnet->addPool(pool);
testSubnet6Set(subnet, "", { "server1" });
}
{
SCOPED_TRACE("add a subnet with a custom option definition");
OptionDefinitionPtr option_def(new OptionDefinition("foo",
1001,
DHCP6_OPTION_SPACE,
"string"));
ASSERT_NO_THROW(<--- There is an unknown macro here somewhere. Configuration is required. If ASSERT_NO_THROW is a macro then please configure it.
ConfigBackendDHCPv6Mgr::instance().getPool()->
createUpdateOptionDef6(BackendSelector(),
ServerSelector::ALL(),
option_def);
);
OptionDefinitionPtr option_def2(new OptionDefinition("baz", 1, "myspace",
"ipv6-address", true));
ASSERT_NO_THROW(
ConfigBackendDHCPv6Mgr::instance().getPool()->
createUpdateOptionDef6(BackendSelector(),
ServerSelector::ALL(),
option_def2);
);
OptionDefinitionPtr option_def3(new OptionDefinition("azy", 2, "myspace",
"ipv6-address", true));
ASSERT_NO_THROW(
ConfigBackendDHCPv6Mgr::instance().getPool()->
createUpdateOptionDef6(BackendSelector(),
ServerSelector::ALL(),
option_def3);
);
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 50, 60, 4));
OptionStringPtr option(new OptionString(Option::V6, 1001, "xyz"));
OptionDescriptor option_desc(option, false, false, "xyz");
option_desc.space_name_ = DHCP6_OPTION_SPACE;
subnet->getCfgOption()->add(option_desc, DHCP6_OPTION_SPACE);
Option6AddrLstPtr option2(new Option6AddrLst(1, IOAddress("2001:db8:1::1")));
OptionDescriptor option_desc2(option2, false, false, "2001:db8:1::1");
option_desc2.space_name_ = "myspace";
Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"),
IOAddress("2001:db8:1::100")));
pool->getCfgOption()->add(option_desc2, "myspace");
subnet->addPool(pool);
Option6AddrLstPtr option3(new Option6AddrLst(2, IOAddress("2001:db8:1::2")));
OptionDescriptor option_desc3(option3, false, false, "2001:db8:1::2");
option_desc3.space_name_ = "myspace";
Pool6Ptr pd_pool(new Pool6(Lease::TYPE_PD, IOAddress("3000::"), 64, 96));
pd_pool->getCfgOption()->add(option_desc3, "myspace");
subnet->addPool(pd_pool);
testSubnet6Set(subnet, "", { "server1" });
}
}
// This test verifies that malformed requests to add and update subnets are
// rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6SetFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6SetFail("", "invalid command 'remote-subnet6-set': 'arguments' is empty");
}
{
SCOPED_TRACE("empty subnet6 list");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": [ ]", "'subnets' list "
"must include exactly one element");
}
{
SCOPED_TRACE("empty subnet map");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": [ { } ]",
"subnet 'id' is mandatory for the remote-subnet6-set command");
}
{
SCOPED_TRACE("malformed subnet");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": [ { \"interface\": \"eth1\" } ]",
"subnet 'id' is mandatory for the remote-subnet6-set command");
}
{
SCOPED_TRACE("spurious keyword");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\", \"id\": 1,"
"\"shared-network-name\": null, "
"\"next-server\": \"\" } ]",
"spurious 'next-server' parameter");
}
{
SCOPED_TRACE("keyword with bad type");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\", \"id\": 1,"
"\"shared-network-name\": null, "
"\"interface-id\": 0 } ]",
"'interface-id' parameter is not a string");
}
{
SCOPED_TRACE("multiple subnets");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8::/64\", \"shared-network-name\": null },"
"{ \"subnet\": \"2001:db8:1::/64\", \"shared-network-name\": null }"
"]",
"'subnets' list must include exactly one element");
}
{
SCOPED_TRACE("invalid shared network name type");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\", \"id\": 1,"
" \"shared-network-name\": 1 }"
"]",
"'shared-network-name' must be a string or null");
}
{
SCOPED_TRACE("no subnet id");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\" }"
"]",
"subnet 'id' is mandatory for the remote-subnet6-set command");
}
{
SCOPED_TRACE("subnet id not int");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\","
" \"id\": \"foo\" }"
"]",
"'id' is not an integer");
}
{
SCOPED_TRACE("zero subnet id");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\","
" \"id\": 0 }"
"]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("too large subnet id");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\","
" \"id\": 4294967295 }"
"]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("no shared network name");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
"{ \"subnet\": \"2001:db8:1::/64\", \"id\": 1 }"
"]",
"'shared-network-name' is mandatory (but can be null or empty)");
}
{
SCOPED_TRACE("no server tags");
testSubnet6SetFail("\"subnets\": [ ]",
"'server-tags' parameter is mandatory");
}
{
SCOPED_TRACE("unassigned server tag");
testSubnet6SetFail("\"server-tags\": [ null ], "
"\"subnets\": ["
" { \"subnet\": \"2001:db8::/64\", \"id\": 1,"
" \"shared-network-name\": null }"
"]",
"'server-tags' list must contain one or"
" more server tags for the "
"'remote-subnet6-set' command");
}
{
SCOPED_TRACE("empty server tags list");
testSubnet6SetFail("\"server-tags\": [ ]",
"'server-tags' list must not be empty");
}
{
SCOPED_TRACE("empty server tag");
testSubnet6SetFail("\"server-tags\": [ \" \" ]",
"server-tag must not be empty");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
" { \"subnet\": \"2001:db8::/64\", \"id\": 1,"
" \"shared-network-name\": null }"
"],"
"\"remote\": {"
" \"type\": \"postgresql\""
"}",
"no such database found for selector: type=postgresql");
}
{
SCOPED_TRACE("lacking option definition");
testSubnet6SetFail("\"server-tags\": [ \"all\"], "
"\"subnets\": ["
" {"
" \"subnet\": \"2001:db8::/64\","
" \"id\": 1,"
" \"option-data\": ["
" {"
" \"name\": \"bootfile-url\","
" \"data\": \"my-boot-file\","
" \"space\": \"myspace\""
" }"
" ]"
" }"
"]",
"definition for the option 'myspace.bootfile-url' does not exist");
}
}
// This test verifies that requests with reservations are refused.
TEST_F(Subnet6CmdsTest, subnet6SetHostsFail) {
{
SCOPED_TRACE("not list hosts");
std::string config =
"\"server-tags\": [ \"all\"], "
"\"subnets\": [ {"
" \"id\": 1,"
" \"subnet\": \"2001:db8::/64\","
" \"reservations\": 0"
"} ]";
testSubnet6SetFail(config, "'reservations' entry is not a list");
}
{
SCOPED_TRACE("not empty hosts");
std::string config =
"\"server-tags\": [ \"all\"], "
"\"subnets\": [ {"
" \"id\": 1,"
" \"subnet\": \"2001:db8::/64\","
" \"reservations\": [ { } ]"
"} ]";
testSubnet6SetFail(config, "'reservations' entry is not empty. "
"This is not supported by the "
"remote-subnet6-set command");
}
}
// This test verifies that it is possible to delete subnet by id.
TEST_F(Subnet6CmdsTest, subnet6DelById) {
{
SCOPED_TRACE("add and delete subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
testSubnet6Set(subnet);
testSubnet6DelId(subnet, 1);
}
{
SCOPED_TRACE("add and delete another subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
testSubnet6Set(subnet);
testSubnet6DelId(subnet, 1);
}
{
SCOPED_TRACE("delete subnet");
// Deleting not existing is not an error.
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
testSubnet6DelId(subnet, 0);
}
{
SCOPED_TRACE("delete another subnet with specifying remote");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 40, 50, 60, 70, 2));
testSubnet6Set(subnet, "{ \"type\": \"mysql\" }");
testSubnet6DelId(subnet, 1, "{ \"type\": \"mysql\" }");
}
}
// This test verifies that malformed requests to delete subnets by id are
// rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6DelByIdFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6DelIdFail("", "invalid command 'remote-subnet6-del-by-id': 'arguments' is empty");
}
{
SCOPED_TRACE("empty subnet6 list");
testSubnet6DelIdFail("\"subnets\": [ ]", "'subnets' list "
"must include exactly one element");
}
{
SCOPED_TRACE("empty subnet map");
testSubnet6DelIdFail("\"subnets\": [ { } ]", "missing 'id' parameter");
}
{
SCOPED_TRACE("malformed id");
testSubnet6DelIdFail("\"subnets\": [ { \"id\": true } ]",
"'id' parameter is not an integer");
}
{
SCOPED_TRACE("zero subnet id");
testSubnet6DelIdFail("\"subnets\": [ { \"id\": 0 } ]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("invalid subnet id");
testSubnet6DelIdFail("\"subnets\": [ { \"id\": 4294967295 } ]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("malformed subnet");
testSubnet6DelIdFail("\"subnets\": [ { \"interface\": \"eth1\" } ]",
"missing 'id' parameter");
}
{
SCOPED_TRACE("spurious prefix");
testSubnet6DelIdFail("\"subnets\": [ { "
"\"id\": 1, \"subnet\": \"2001:db8::/64\" } ]",
"spurious 'subnet' parameter");
}
{
SCOPED_TRACE("multiple subnets");
testSubnet6DelIdFail("\"subnets\": ["
"{ \"id\": 1 },"
"{ \"id\": 2 }"
"]",
"'subnets' list must include exactly one element");
}
{
SCOPED_TRACE("server tags");
testSubnet6DelIdFail("\"server-tags\": [ ]",
"'server-tags' parameter is forbidden");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6DelIdFail("\"subnets\": ["
" { \"id\": 1 }"
"],"
"\"remote\": {"
" \"type\": \"postgresql\""
"}",
"no such database found for selector: type=postgresql");
}
}
// This test verifies that it is possible to delete subnet by prefix.
TEST_F(Subnet6CmdsTest, subnet6DelByPrefix) {
{
SCOPED_TRACE("add and delete subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
testSubnet6Set(subnet);
testSubnet6DelPrefix(subnet, 1);
}
{
SCOPED_TRACE("add and delete another subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
testSubnet6Set(subnet);
testSubnet6DelPrefix(subnet, 1);
}
{
SCOPED_TRACE("delete subnet");
// Deleting not existing is not an error.
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
testSubnet6DelPrefix(subnet, 0);
}
{
SCOPED_TRACE("delete another subnet with specifying remote");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 40, 50, 60, 70, 2));
testSubnet6Set(subnet, "{ \"type\": \"mysql\" }");
testSubnet6DelPrefix(subnet, 1, "{ \"type\": \"mysql\" }");
}
}
// This test verifies that malformed requests to delete subnets by prefix
// are rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6DelByPrefixFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6DelPrefixFail("", "invalid command 'remote-subnet6-del-by-prefix': 'arguments' is empty");
}
{
SCOPED_TRACE("empty subnets list");
testSubnet6DelPrefixFail("\"subnets\": [ ]", "'subnets' list "
"must include exactly one element");
}
{
SCOPED_TRACE("empty subnet map");
testSubnet6DelPrefixFail("\"subnets\": [ { } ]",
"missing 'subnet' parameter");
}
{
SCOPED_TRACE("malformed subnet");
testSubnet6DelPrefixFail("\"subnets\": [ { \"interface\": \"eth1\" } ]",
"missing 'subnet' parameter");
}
{
SCOPED_TRACE("empty subnet");
testSubnet6DelPrefixFail("\"subnets\": [ { \"subnet\": \"\" } ]",
"'subnet' parameter must not be empty");
}
{
SCOPED_TRACE("invalid subnet");
testSubnet6DelPrefixFail("\"subnets\": [ { \"subnet\": \"foo\" } ]",
"unable to parse invalid prefix foo");
}
{
SCOPED_TRACE("spurious id");
testSubnet6DelPrefixFail("\"subnets\": [ { "
"\"subnet\": \"2001:db8::/64\", \"id\": 1 } ]",
"spurious 'id' parameter");
}
{
SCOPED_TRACE("multiple subnets");
testSubnet6DelPrefixFail("\"subnets\": ["
"{ \"subnet\": \"2001:db8::/64\" },"
"{ \"subnet\": \"2001:db8:1::/64\" }"
"]",
"'subnets' list must include exactly one element");
}
{
SCOPED_TRACE("server tags");
testSubnet6DelPrefixFail("\"server-tags\": [ ]",
"'server-tags' parameter is forbidden");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6DelPrefixFail("\"subnets\": ["
" { \"subnet\": \"2001:db8::/64\" }"
"],"
"\"remote\": {"
" \"type\": \"postgresql\""
"}",
"no such database found for selector: type=postgresql");
}
}
// This test verifies that it is possible to get subnet by id.
TEST_F(Subnet6CmdsTest, subnet6GetById) {
{
SCOPED_TRACE("add and get subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
subnet->setServerTag("server1");
testSubnet6Set(subnet, "", { "server1" });
testSubnet6GetId(subnet, subnet);
}
{
SCOPED_TRACE("add and get another subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet);
testSubnet6GetId(subnet, subnet);
}
{
SCOPED_TRACE("get no subnet");
// Getting not existing is not an error.
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:2::"), 64, 30, 40, 60, 70, 3));
testSubnet6GetId(subnet, Subnet6Ptr());
}
{
SCOPED_TRACE("get another subnet with specifying remote");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:3::"), 64, 40, 50, 60, 70, 4));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet, "{ \"type\": \"mysql\" }");
testSubnet6GetId(subnet, subnet, "{ \"type\": \"mysql\" }");
}
}
// This test verifies that malformed requests to get subnets by id are
// rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6GetByIdFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6GetIdFail("", "invalid command 'remote-subnet6-get-by-id': 'arguments' is empty");
}
{
SCOPED_TRACE("empty subnets list");
testSubnet6GetIdFail("\"subnets\": [ ]", "'subnets' list "
"must include exactly one element");
}
{
SCOPED_TRACE("empty subnet map");
testSubnet6GetIdFail("\"subnets\": [ { } ]", "missing 'id' parameter");
}
{
SCOPED_TRACE("malformed id");
testSubnet6GetIdFail("\"subnets\": [ { \"id\": true } ]",
"'id' parameter is not an integer");
}
{
SCOPED_TRACE("zero subnet id");
testSubnet6GetIdFail("\"subnets\": [ { \"id\": 0 } ]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("invalid subnet id");
testSubnet6GetIdFail("\"subnets\": [ { \"id\": 4294967295 } ]",
"'id' parameter must be in [1..4294967294] range");
}
{
SCOPED_TRACE("malformed subnet");
testSubnet6GetIdFail("\"subnets\": [ { \"interface\": \"eth1\" } ]",
"missing 'id' parameter");
}
{
SCOPED_TRACE("spurious prefix");
testSubnet6GetIdFail("\"subnets\": [ { "
"\"id\": 1, \"subnet\": \"2001:db8::/64\" } ]",
"spurious 'subnet' parameter");
}
{
SCOPED_TRACE("multiple subnets");
testSubnet6GetIdFail("\"subnets\": ["
"{ \"id\": 1 },"
"{ \"id\": 2 }"
"]",
"'subnets' list must include exactly one element");
}
{
SCOPED_TRACE("server tags");
testSubnet6GetIdFail("\"server-tags\": [ ]",
"'server-tags' parameter is forbidden");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6GetIdFail("\"subnets\": ["
" { \"id\": 1 }"
"],"
"\"remote\": {"
" \"type\": \"postgresql\""
"}",
"no such database found for selector: type=postgresql");
}
}
// This test verifies that it is possible to get subnet by prefix.
TEST_F(Subnet6CmdsTest, subnet6GetByPrefix) {
{
SCOPED_TRACE("add and get subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
subnet->setServerTag("server1");
testSubnet6Set(subnet, "", { "server1" });
testSubnet6GetPrefix(subnet, subnet);
}
{
SCOPED_TRACE("add and get another subnet");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet);
testSubnet6GetPrefix(subnet, subnet);
}
{
SCOPED_TRACE("get no subnet");
// Getting not existing is not an error.
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:2::"), 64, 30, 40, 60, 70, 3));
testSubnet6GetPrefix(subnet, Subnet6Ptr());
}
{
SCOPED_TRACE("get another subnet with specifying remote");
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:3::"), 64, 40, 50, 60, 70, 4));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet, "{ \"type\": \"mysql\" }");
testSubnet6GetPrefix(subnet, subnet, "{ \"type\": \"mysql\" }");
}
}
// This test verifies that malformed requests to get subnets by prefix
// are rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6GetByPrefixFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6GetPrefixFail("", "invalid command 'remote-subnet6-get-by-prefix': 'arguments' is empty");
}
{
SCOPED_TRACE("empty subnets list");
testSubnet6GetPrefixFail("\"subnets\": [ ]", "'subnets' list "
"must include exactly one element");
}
{
SCOPED_TRACE("empty subnet map");
testSubnet6GetPrefixFail("\"subnets\": [ { } ]",
"missing 'subnet' parameter");
}
{
SCOPED_TRACE("malformed subnet");
testSubnet6GetPrefixFail("\"subnets\": [ { \"interface\": \"eth1\" } ]",
"missing 'subnet' parameter");
}
{
SCOPED_TRACE("empty subnet");
testSubnet6GetPrefixFail("\"subnets\": [ { \"subnet\": \"\" } ]",
"'subnet' parameter must not be empty");
}
{
SCOPED_TRACE("invalid subnet");
testSubnet6GetPrefixFail("\"subnets\": [ { \"subnet\": \"foo\" } ]",
"unable to parse invalid prefix foo");
}
{
SCOPED_TRACE("spurious id");
testSubnet6GetPrefixFail("\"subnets\": [ { "
"\"subnet\": \"2001:db8::/64\", \"id\": 1 } ]",
"spurious 'id' parameter");
}
{
SCOPED_TRACE("multiple subnets");
testSubnet6GetPrefixFail("\"subnets\": ["
"{ \"subnet\": \"2001:db8::/64\" },"
"{ \"subnet\": \"2001:db8:1::/64\" }"
"]",
"'subnets' list must include exactly one element");
}
{
SCOPED_TRACE("server tags");
testSubnet6GetPrefixFail("\"server-tags\": [ ]",
"'server-tags' parameter is forbidden");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6GetPrefixFail("\"subnets\": ["
" { \"subnet\": \"2001:db8::/64\" }"
"],"
"\"remote\": {"
" \"type\": \"postgresql\""
"}",
"no such database found for selector: type=postgresql");
}
}
// This test verifies that it is possible to get all subnets.
TEST_F(Subnet6CmdsTest, subnet6List) {
// Empty.
ElementPtr expected = Element::createList();
ElementPtr expected_all = Element::createList();
ElementPtr expected_unassigned = Element::createList();
testSubnet6List(expected);
testSubnet6List(expected_all);
testSubnet6List(expected_unassigned);
// Add a subnet.
Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 64, 30, 40, 60, 70, 1));
subnet->setServerTag("server1");
testSubnet6Set("test6", subnet, "", { "server1" });
ElementPtr sub = Element::createMap();
sub->set("id", Element::create(1));
sub->set("subnet", Element::create("2001:db8::/64"));
sub->set("shared-network-name", Element::create("test6"));
sub->set("metadata", createMetadata("server1"));
expected->add(sub);
testSubnet6List(expected, "", { "server1" });
// Add a second subnet.
subnet.reset(new Subnet6(IOAddress("2001:db8:1::"), 64, 30, 40, 60, 70, 2));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet);
sub = Element::createMap();
sub->set("id", Element::create(2));
sub->set("subnet", Element::create("2001:db8:1::/64"));
sub->set("shared-network-name", Element::create());
sub->set("metadata", createMetadata(ServerTag::ALL));
expected->add(sub);
expected_all->add(sub);
testSubnet6List(expected, "", { "server1" });
testSubnet6List(expected_all);
// With another server only subnets belonging to all are returned.
testSubnet6List(expected_all, "", { "server2" });
// Try with a remote.
subnet.reset(new Subnet6(IOAddress("2001:db8:3::"), 64, 40, 50, 60, 70, 4));
subnet->setServerTag(ServerTag::ALL);
testSubnet6Set(subnet, "{ \"type\": \"mysql\" }");
sub = Element::createMap();
sub->set("id", Element::create(4));
sub->set("subnet", Element::create("2001:db8:3::/64"));
sub->set("shared-network-name", Element::create());
sub->set("metadata", createMetadata(ServerTag::ALL));
expected->add(sub);
expected_all->add(sub);
testSubnet6List(expected, "{ \"type\": \"mysql\" }", { "server1" });
testSubnet6List(expected_all, "{ \"type\": \"mysql\" }");
// Add a third subnet in another server.
subnet.reset(new Subnet6(IOAddress("2001:db8:4::"), 64, 40, 50, 60, 70, 5));
subnet->setServerTag("server2");
testSubnet6Set(subnet, "", { "server2" });
// Not in server1 so not yet expected.
testSubnet6List(expected, "", { "server1" });
sub = Element::createMap();
sub->set("id", Element::create(5));
sub->set("subnet", Element::create("2001:db8:4::/64"));
sub->set("shared-network-name", Element::create());
sub->set("metadata", createMetadata("server2"));
expected->add(sub);
testSubnet6List(expected, "", { "server1", "server2" });
// Now try unassigned.
testSubnet6List(expected_unassigned, "", { });
subnet.reset(new Subnet6(IOAddress("2001:db8:5::"), 64, 40, 50, 60, 70, 6));
// Adds it directly to the backend (no command yet to do this).
setSubnet(subnet);
// Not visible by a server.
testSubnet6List(expected, "", { "server1", "server2" });
sub = Element::createMap();
sub->set("id", Element::create(6));
sub->set("subnet", Element::create("2001:db8:5::/64"));
sub->set("shared-network-name", Element::create());
sub->set("metadata", createMetadata(std::set<std::string>()));
expected_unassigned->add(sub);
testSubnet6List(expected_unassigned, "", { });
}
// This test verifies that malformed requests to get subnets by id are
// rejected and proper error messages are returned.
TEST_F(Subnet6CmdsTest, subnet6ListFail) {
{
SCOPED_TRACE("empty arguments");
testSubnet6ListFail(", \"arguments\": { }",
"invalid command 'remote-subnet6-list': 'arguments' is empty");
}
{
SCOPED_TRACE("no server tags");
testSubnet6ListFail(", \"arguments\": { \"remote\": { } }",
"'server-tags' parameter is mandatory");
}
{
SCOPED_TRACE("empty server tags list");
testSubnet6ListFail(", \"arguments\": { \"server-tags\": [ ] }",
"'server-tags' list must not be empty");
}
{
SCOPED_TRACE("bad formed unassigned");
testSubnet6ListFail(", \"arguments\": { "
"\"server-tags\": [ null, \"all\" ] }",
"when the 'server-tags' list contains "
"multiple elements all these elements must "
"have the string type");
}
{
SCOPED_TRACE("unsupported backend type");
testSubnet6ListFail(", \"arguments\": { "
"\"server-tags\": [ \"all\"], "
"\"remote\": {"
" \"type\": \"postgresql\""
"} }",
"no such database found for selector: type=postgresql");
}
}
}
|