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 | // Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
/// @file This file contains tests which exercise the command entry points.
#include <config.h>
#include <host_cache.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <host_cache_parsers.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <hooks/hooks_manager.h>
#include <testutils/multi_threading_utils.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <functional><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- 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::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace isc::host_cache;
using namespace isc::test;
using namespace isc::util;
using namespace std;
namespace ph = std::placeholders;
namespace {
/// @brief Test fixture for testing commands for the host-cache library
class CommandTest : public ::testing::Test {
public:
/// @brief Constructor
CommandTest() {
hcptr_.reset(new HostCache());
MultiThreadingMgr::instance().setMode(false);
}
/// @brief Destructor
virtual ~CommandTest() {
::remove("dump.json");
::remove("source.json");
}
/// @brief Creates a sample IPv4 host
HostPtr createHost4() {
HostPtr h(new Host("01:02:03:04:05:06", "hw-address",
SubnetID(4), SubnetID(SUBNET_ID_UNUSED),
IOAddress("192.0.2.100"),
"somehost.example.org",
std::string(), std::string(),
IOAddress("192.0.0.2"),
"server-hostname.example.org",
"bootfile.efi"));
return (h);
}
/// @brief Creates another sample IPv4 host
HostPtr createHost4bis() {
HostPtr h(new Host("07:08:09:0a:0b:0c", "hw-address",
SubnetID(4), SubnetID(SUBNET_ID_UNUSED),
IOAddress("192.0.2.200"),
"otherhost.example.org",
std::string(), std::string(),
IOAddress("192.0.0.2"),
"server-hostname.example.org",
"bootfile.efi"));
return (h);
}
/// @brief Creates a sample IPv6 host
HostPtr createHost6() {
HostPtr host(new Host("01:02:03:04:05:06", "hw-address",
SubnetID(SUBNET_ID_UNUSED), SubnetID(6),
IOAddress("0.0.0.0")));
// Add 2 reservations: 1 for NA, 1 for PD.
host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
IOAddress("2001:db8::cafe:babe")));
host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD,
IOAddress("2001:db8:dead:beef::"), 64));
return (host);
};
/// @brief Creates another sample IPv6 host
HostPtr createHost6bis() {
HostPtr host(new Host("07:08:09:0a:0b:0c", "hw-address",
SubnetID(SUBNET_ID_UNUSED), SubnetID(6),
IOAddress("0.0.0.0")));
// Add a reservation for NA.
host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
IOAddress("2001:db8::babe:cafe")));
return (host);
};
string host4_txt = "{\n"
"\"hw-address\": \"01:02:03:04:05:06\",\n"
"\"subnet-id4\": 4,\n"
"\"subnet-id6\": 4294967295,\n" // SUBNET_ID_UNUSED
"\"ip-address\": \"192.0.2.100\",\n"
"\"hostname\": \"somehost.example.org\",\n"
"\"client-classes4\": [ ],\n"
"\"client-classes6\": [ ],\n"
"\"option-data4\": [ ],\n"
"\"option-data6\": [ ],\n"
"\"next-server\": \"192.0.0.2\",\n"
"\"server-hostname\": \"server-hostname.example.org\",\n"
"\"boot-file-name\": \"bootfile.efi\",\n"
"\"host-id\": 0 }\n";
string host6_txt = "{\n"
"\"hw-address\": \"01:02:03:04:05:06\",\n"
"\"subnet-id4\": 4294967295,\n" // SUBNET_ID_UNUSED
"\"subnet-id6\": 6,\n"
"\"ip-addresses\": [ \"2001:db8::cafe:babe\" ],\n"
"\"prefixes\": [ \"2001:db8:dead:beef::/64\" ],\n"
"\"hostname\": \"\",\n"
"\"client-classes4\": [ ],\n"
"\"client-classes6\": [ ],\n"
"\"option-data4\": [ ],\n"
"\"option-data6\": [ ],\n"
"\"next-server\": \"0.0.0.0\",\n"
"\"server-hostname\": \"\",\n"
"\"boot-file-name\": \"\",\n"
"\"host-id\": 0 }\n";
/// @brief Handler type
typedef std::function<int (CalloutHandle& handle)> handlerType;
/// @brief Check handler
///
/// @param handler handler to call
/// @param command JSON command
/// @param result expected value returned by the handler
/// @param rcode expected rcode from answer
/// @param answer expected answer
void checkCommand(handlerType handler, ConstElementPtr command,
int result, int rcode, ConstElementPtr answer) {
// Get a callout handle
CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
ASSERT_TRUE(callout_handle);
callout_handle->setArgument("command", command);
ConstElementPtr response;
callout_handle->setArgument("response", response);
// Call the handler
EXPECT_EQ(result, handler(*callout_handle));
// Check the response
callout_handle->getArgument("response", response);
int got = 0;
ConstElementPtr comment = parseAnswer(got, response);
EXPECT_EQ(rcode, got);
ASSERT_TRUE(answer);
ASSERT_TRUE(comment);
EXPECT_EQ(*answer, *comment);
}
/// @brief Check handler
///
/// @param handler handler to call
/// @param command command string
/// @param result expected value returned by the handler
/// @param rcode expected rcode from answer
/// @param answer expected answer
void checkCommand(handlerType handler, string command, int result,
int rcode, ConstElementPtr answer) {
ConstElementPtr cmd = Element::fromJSON(command);
checkCommand(handler, cmd, result, rcode, answer);
}
/// @brief Check handler
///
/// @param handler handler to call
/// @param command JSON command
/// @param result expected value returned by the handler
/// @param rcode expected rcode from answer
/// @param answer expected answer
void checkCommand(handlerType handler, ConstElementPtr command,
int result, int rcode, string answer) {
checkCommand(handler, command, result, rcode, Element::create(answer));
}
/// @brief Check handler
///
/// @param handler handler to call
/// @param command command string
/// @param result expected value returned by the handler
/// @param rcode expected rcode from answer
/// @param answer expected answer
void checkCommand(handlerType handler, string command, int result,
int rcode, string answer) {
ConstElementPtr cmd = Element::fromJSON(command);
checkCommand(handler, cmd, result, rcode, Element::create(answer));
}
/// Fill cache with 4 entries.
void fill() {
ASSERT_EQ(4294967295U, SUBNET_ID_UNUSED);
HostPtr host = createHost4();
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost4bis();
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost6();
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost6bis();
EXPECT_EQ(0, hcptr_->insert(host, false));
EXPECT_EQ(4, hcptr_->size());
}
/// @brief Check host ID handling.
///
/// The issue here is that host ID type is uint64_t and
/// JSON integer type is int64_t, i.e. unsigned vs signed.
///
/// @param hid host ID
/// @param jint JSON integer
void checkHostID(HostID hid, int64_t jint) {
// Build host
HostPtr host = createHost4();
host->setHostId(hid);
ASSERT_EQ(hid, host->getHostId());
// Check host -> json
ElementPtr json;
ASSERT_NO_THROW(json = isc::host_cache::toElement(host));
ASSERT_TRUE(json);
ConstElementPtr host_id = json->get("host-id");
ASSERT_TRUE(host_id);
EXPECT_EQ(jint, host_id->intValue());
// Build json
json = Element::fromJSON(host4_txt);
json->set("host-id", Element::create(static_cast<long long>(jint)));
host_id = json->get("host-id");
ASSERT_TRUE(host_id);
ASSERT_EQ(jint, host_id->intValue());
// Check json -> host
HCEntryParser parser;
ASSERT_NO_THROW(host = parser.parse(json));
ASSERT_TRUE(host);
EXPECT_EQ(hid, host->getHostId());
}
/// @brief Test cache-size command.
void testSizeCommand();
/// @brief Test cache-flush command.
void testFlushCommand();
/// @brief Test cache-get (aka dump) command.
void testDumpCommand();
/// @brief Test cache-get-by-id command using hw-address.
void testByHwAddressCommand();
/// @brief Test cache-get-by-id command using client-id.
void testByClientIdCommand();
/// @brief Test cache-get-by-id command using flex-id.
void testByFlexIdCommand();
/// @brief Test cache-get-by-id command using multiple identifiers.
void testByIdsCommand();
/// @brief Test cache-insert command.
void testInsertCommand();
/// @brief Test cache-write command.
void testWriteCommand();
/// @brief Test cache-load command.
void testLoadCommand();
/// @brief Test cache-remove command.
void testRemoveCommand();
/// @brief Test global reservations.
void testGlobal();
/// @brief Test host-id.
void testHostID();
/// @brief Test negative.
void testNegative();
/// @brief Host Cache
HostCachePtr hcptr_;
};
/// @brief Size handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int sizeHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheSizeHandler(handle));
}
/// @brief Clear handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int clearHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheClearHandler(handle));
}
/// @brief Flush handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int flushHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheFlushHandler(handle));
}
/// @brief Get handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int getHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheGetHandler(handle));
}
/// @brief Get by identifier handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int getByIdHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheGetByIdHandler(handle));
}
/// @brief Insert handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int insertHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheInsertHandler(handle));
}
/// @brief Write handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int writeHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheWriteHandler(handle));
}
/// @brief Load handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int loadHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheLoadHandler(handle));
}
/// @brief Remove handler function
///
/// @param hcptr Pointer to host cache
/// @param handle Handle
/// @return int 0 if successful, 1 if not
int removeHandler(HostCachePtr hcptr, CalloutHandle& handle) {
return (hcptr->cacheRemoveHandler(handle));
}
// Verifies different valid and invalid configurations.
TEST_F(CommandTest, config) {<--- syntax error
ConstElementPtr config;
// Empty config is fine.
EXPECT_NO_THROW(hcptr_->configure(config));
// Check defaults.
EXPECT_EQ(0, hcptr_->getMaximum());
// Config must be a map.
config = Element::createList();
EXPECT_THROW(hcptr_->configure(config), ConfigError);
// Fill config.
config = Element::fromJSON("{ \"maximum\": 19 }");
EXPECT_NO_THROW(hcptr_->configure(config));
EXPECT_EQ(19, hcptr_->getMaximum());
// Check maximum.
config = Element::fromJSON("{ \"maximum\": 0 }");
EXPECT_NO_THROW(hcptr_->configure(config));
EXPECT_EQ(0, hcptr_->getMaximum());
config = Element::fromJSON("{ \"maximum\": -1 }");
EXPECT_THROW(hcptr_->configure(config), ConfigError);
config = Element::fromJSON("{ \"maximum\": 1000000001 }");
EXPECT_THROW(hcptr_->configure(config), ConfigError);
config = Element::fromJSON("{ \"maximum\": \"infinite\" }");
EXPECT_THROW(hcptr_->configure(config), ConfigError);
config = Element::fromJSON("{ \"foo\": \"bar\" }");
EXPECT_NO_THROW(hcptr_->configure(config));
}
// Verifies that cache-size works as expected.
void
CommandTest::testSizeCommand() {
// Prepare
handlerType size_handler = std::bind(&sizeHandler, hcptr_, ph::_1);
string size_cmd = "{ \"command\": \"cache-size\" }";
fill();
ElementPtr result = Element::createMap();
result->set("size", Element::create(4));
// Get size (expect 4).
checkCommand(size_handler, size_cmd, 0, 0, result);
}
// Verifies that cache-flush works as expected.
void
CommandTest::testFlushCommand() {
// Prepare
handlerType flush_handler = std::bind(&flushHandler, hcptr_, ph::_1);
string flush1_cmd = "{ \"command\": \"cache-flush\", \"arguments\": 1 }";
handlerType clear_handler = std::bind(&clearHandler, hcptr_, ph::_1);
string clear_cmd = "{ \"command\": \"cache-clear\" }";
fill();
// Flush an entry
checkCommand(flush_handler, flush1_cmd, 0, 0,
"Cache flushed (1 entries removed).");
EXPECT_EQ(3, hcptr_->size());
// Flush another
checkCommand(flush_handler, flush1_cmd, 0, 0,
"Cache flushed (1 entries removed).");
EXPECT_EQ(2, hcptr_->size());
// Flush all aka clear
checkCommand(clear_handler, clear_cmd, 0, 0, "Cache cleared.");
EXPECT_EQ(0, hcptr_->size());
// No issue to flush an empty cache
checkCommand(flush_handler, flush1_cmd, 0, 0,
"Cache flushed (0 entries removed).");
// Check errors
string noarg_cmd = "{ \"command\": \"cache-flush\" }";
checkCommand(flush_handler, noarg_cmd, 1, 1,
"no parameters specified for the command");
string badarg_cmd =
"{ \"command\": \"cache-flush\", \"arguments\": true }";
checkCommand(flush_handler, badarg_cmd, 1, 1,
"intValue() called on non-integer Element in (<string>:1:42)");
string negarg_cmd =
"{ \"command\": \"cache-flush\", \"arguments\": -1 }";
checkCommand(flush_handler, negarg_cmd, 1, 1, "invalid (<0) parameter");
string zeroarg_cmd =
"{ \"command\": \"cache-flush\", \"arguments\": 0 }";
checkCommand(flush_handler, zeroarg_cmd, 1, 1,
"invalid (0) parameter: please use cache-clear command");
string notall_cmd =
"{ \"command\": \"cache-flush\", \"arguments\": \"everything\" }";
}
// Verifies that cache-get dumps the cache content.
void
CommandTest::testDumpCommand() {
// Prepare
handlerType get_handler = std::bind(&getHandler, hcptr_, ph::_1);
string get_cmd = "{ \"command\": \"cache-get\" }";
// Dump empty cache
ElementPtr entries = Element::createList();
checkCommand(get_handler, get_cmd, 0, 3, entries);
// Dump cache with host4
EXPECT_EQ(0, hcptr_->insert(createHost4(), false));
entries->add(Element::fromJSON(host4_txt));
checkCommand(get_handler, get_cmd, 0, 0, entries);
// Dump cache with host4 and host6
EXPECT_EQ(0, hcptr_->insert(createHost6(), false));
entries->add(Element::fromJSON(host6_txt));
checkCommand(get_handler, get_cmd, 0, 0, entries);
}
// Verifies that cache-get-by-id returns expected entries using hw-address.
void
CommandTest::testByHwAddressCommand() {
// Prepare
handlerType get_by_id_handler = std::bind(&getByIdHandler, hcptr_, ph::_1);
string get_by_id_cmd = "{ "
"\"command\": \"cache-get-by-id\","
"\"arguments\": { \"hw-address\": \"01:02:03:04:05:06\" } }";
// From empty cache
ElementPtr entries = Element::createList();
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 3, entries);
// From cache with host4
EXPECT_EQ(0, hcptr_->insert(createHost4(), false));
entries->add(Element::fromJSON(host4_txt));
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 0, entries);
// From cache with host4 and host6
EXPECT_EQ(0, hcptr_->insert(createHost6(), false));
entries->add(Element::fromJSON(host6_txt));
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 0, entries);
}
// Verifies that cache-get-by-id returns expected entries using client-id.
void
CommandTest::testByClientIdCommand() {
// Prepare
handlerType get_by_id_handler = std::bind(&getByIdHandler, hcptr_, ph::_1);
string get_by_id_cmd = "{ "
"\"command\": \"cache-get-by-id\","
"\"arguments\": { \"client-id\": \"01:02:03:04:05:06\" } }";
// From empty cache
ElementPtr entries = Element::createList();
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 3, entries);
// From cache with host4
HostPtr h(new Host("010203040506", "client-id",
SubnetID(4), SubnetID(SUBNET_ID_UNUSED),
IOAddress("192.0.2.100"),
"somehost.example.org",
std::string(), std::string(),
IOAddress("192.0.0.2"),
"server-hostname.example.org",
"bootfile.efi"));
string txt = "{\n"
"\"client-id\": \"010203040506\",\n"
"\"subnet-id4\": 4,\n"
"\"subnet-id6\": 4294967295,\n" // SUBNET_ID_UNUSED
"\"ip-address\": \"192.0.2.100\",\n"
"\"hostname\": \"somehost.example.org\",\n"
"\"client-classes4\": [ ],\n"
"\"client-classes6\": [ ],\n"
"\"option-data4\": [ ],\n"
"\"option-data6\": [ ],\n"
"\"next-server\": \"192.0.0.2\",\n"
"\"server-hostname\": \"server-hostname.example.org\",\n"
"\"boot-file-name\": \"bootfile.efi\",\n"
"\"host-id\": 0 }\n";
EXPECT_EQ(0, hcptr_->insert(h, false));
entries->add(Element::fromJSON(txt));
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 0, entries);
}
// Verifies that cache-get-by-id returns expected entries using flex-id.
void
CommandTest::testByFlexIdCommand() {
// Prepare
handlerType get_by_id_handler = std::bind(&getByIdHandler, hcptr_, ph::_1);
string get_by_id_cmd = "{ "
"\"command\": \"cache-get-by-id\","
"\"arguments\": { \"flex-id\": \"01:02:03:04:05:06\" } }";
// From empty cache
ElementPtr entries = Element::createList();
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 3, entries);
// From cache with host4
HostPtr h(new Host("010203040506", "flex-id",
SubnetID(4), SubnetID(SUBNET_ID_UNUSED),
IOAddress("192.0.2.100"),
"somehost.example.org",
std::string(), std::string(),
IOAddress("192.0.0.2"),
"server-hostname.example.org",
"bootfile.efi"));
string txt = "{\n"
"\"flex-id\": \"010203040506\",\n"
"\"subnet-id4\": 4,\n"
"\"subnet-id6\": 4294967295,\n" // SUBNET_ID_UNUSED
"\"ip-address\": \"192.0.2.100\",\n"
"\"hostname\": \"somehost.example.org\",\n"
"\"client-classes4\": [ ],\n"
"\"client-classes6\": [ ],\n"
"\"option-data4\": [ ],\n"
"\"option-data6\": [ ],\n"
"\"next-server\": \"192.0.0.2\",\n"
"\"server-hostname\": \"server-hostname.example.org\",\n"
"\"boot-file-name\": \"bootfile.efi\",\n"
"\"host-id\": 0 }\n";
EXPECT_EQ(0, hcptr_->insert(h, false));
entries->add(Element::fromJSON(txt));
checkCommand(get_by_id_handler, get_by_id_cmd, 0, 0, entries);
}
// Verifies that cache-get-by-id rejects multiple identifiers.
void
CommandTest::testByIdsCommand() {
// Prepare
handlerType get_by_id_handler = std::bind(&getByIdHandler, hcptr_, ph::_1);
string get_by_id_cmd = "{ "
"\"command\": \"cache-get-by-id\","
"\"arguments\": { \"hw-address\": \"01:02:03:04:05:06\","
" \"client-id\": \"010203040506\" } }";
checkCommand(get_by_id_handler, get_by_id_cmd, 1, 1,
"only one identifier can be specified");
}
// Verifies that cache-insert inserts entries into the cache.
void
CommandTest::testInsertCommand() {
// Prepare
handlerType add_handler = std::bind(&insertHandler, hcptr_, ph::_1);
handlerType get_handler = std::bind(&getHandler, hcptr_, ph::_1);
string get_cmd = "{ \"command\": \"cache-get\" }";
ElementPtr add = Element::createMap();
add->set("command", Element::create(string("cache-insert")));
// Requires arguments
checkCommand(add_handler, add, 1, 1,
"no parameters specified for the command");
// Empty arguments does nothing
ElementPtr args = Element::createList();
add->set("arguments", args);
checkCommand(add_handler, add, 0, 0,
"0 entries inserted (0 overwritten by more recent entries).");
EXPECT_EQ(0, hcptr_->size());
// Insert one entry
args = Element::fromJSON(host4_txt);
add->set("arguments", args);
checkCommand(add_handler, add, 0, 0,
"1 entries inserted (0 overwritten by more recent entries).");
EXPECT_EQ(1, hcptr_->size());
// Insert a list
args = Element::createList();
args->add(Element::fromJSON(host6_txt));
add->set("arguments", args);
checkCommand(add_handler, add, 0, 0,
"1 entries inserted (0 overwritten by more recent entries).");
EXPECT_EQ(2, hcptr_->size());
// Overwrite an entry
args = Element::createList();
args->add(Element::fromJSON(host4_txt));
add->set("arguments", args);
checkCommand(add_handler, add, 0, 0,
"1 entries inserted (1 overwritten by more recent entries).");
EXPECT_EQ(2, hcptr_->size());
// Check the order
ElementPtr entries = Element::createList();
entries->add(Element::fromJSON(host6_txt));
entries->add(Element::fromJSON(host4_txt));
add->set("arguments", args);
checkCommand(get_handler, get_cmd, 0, 0, entries);
// JSON list (aka array) is ordered.
args->add(Element::fromJSON(host6_txt));
// [6, 4] != [4, 6]
EXPECT_NE(*entries, *args);
// Make the list parser to fail
add->set("arguments", Element::create(true));
checkCommand(add_handler, add, 1, 1,
"listValue() called on non-list Element");
// Make the parser to fail
args = Element::fromJSON(host4_txt);
args->remove("hw-address");
add->set("arguments", args);
checkCommand(add_handler, add, 1, 1, "no identifier");
}
// Verifies that cache-write really writes the expected file.
void
CommandTest::testWriteCommand() {
// Prepare
handlerType write_handler = std::bind(&writeHandler, hcptr_, ph::_1);
string file_txt = "dump.json";
string write_cmd =
"{ \"command\": \"cache-write\", \"arguments\": \"" + file_txt + "\" }";
// Insert two entries
EXPECT_EQ(0, hcptr_->insert(createHost4(), false));
EXPECT_EQ(0, hcptr_->insert(createHost6(), false));
EXPECT_EQ(2, hcptr_->size());
// Write file
checkCommand(write_handler, write_cmd, 0, 0,
"2 entries dumped to '" + file_txt + "'.");
// Check file
ifstream f(file_txt, ios::binary | ios::ate);
ASSERT_TRUE(f.good());
EXPECT_LE(host4_txt.size(), static_cast<size_t>(f.tellg()));
ElementPtr from_file = Element::fromJSONFile(file_txt);
ASSERT_TRUE(from_file);
ElementPtr expected = Element::createList();
expected->add(Element::fromJSON(host4_txt));
expected->add(Element::fromJSON(host6_txt));
EXPECT_EQ(*from_file, *expected);
// Check errors
string noarg_cmd = "{ \"command\": \"cache-write\" }";
checkCommand(write_handler, noarg_cmd, 1, 1,
"no parameters specified for the command");
string badarg_cmd =
"{ \"command\": \"cache-write\", \"arguments\": true }";
checkCommand(write_handler, badarg_cmd, 1, 1,
"invalid (not a string) parameter");
string emptyarg_cmd =
"{ \"command\": \"cache-write\", \"arguments\": \"\" }";
checkCommand(write_handler, emptyarg_cmd, 1, 1,
"invalid (empty string) parameter");
string notexists = "/this/does/not/exit";
string notexists_cmd =
"{ \"command\": \"cache-write\", \"arguments\": \"" + notexists + "\" }";
checkCommand(write_handler, notexists_cmd, 1, 1,
"Unable to open file '" + notexists + "' for writing.");
}
// Verifies that cache-load can load a dump file.
void
CommandTest::testLoadCommand() {
// Prepare
handlerType load_handler = std::bind(&loadHandler, hcptr_, ph::_1);
string file_txt = "source.json";
string load_cmd =
"{ \"command\": \"cache-load\", \"arguments\": \"" + file_txt + "\" }";
// Fill file
ofstream f(file_txt, ios::trunc);
ASSERT_TRUE(f.is_open());
f << "[\n"
<< host4_txt << "\n,\n"
<< host6_txt << "\n,\n"
<< host4_txt << "\n]\n";
f.close();
// Load file
checkCommand(load_handler, load_cmd, 0, 0,
"3 entries loaded from '" + file_txt + "' (1 overwritten "
"by more recent entries).");
EXPECT_EQ(2, hcptr_->size());
// Check errors
string noarg_cmd = "{ \"command\": \"cache-load\" }";
checkCommand(load_handler, noarg_cmd, 1, 1,
"no parameters specified for the command");
string badarg_cmd =
"{ \"command\": \"cache-load\", \"arguments\": true }";
checkCommand(load_handler, badarg_cmd, 1, 1,
"invalid (not a string) parameter");
string emptyarg_cmd =
"{ \"command\": \"cache-load\", \"arguments\": \"\" }";
checkCommand(load_handler, emptyarg_cmd, 1, 1,
"invalid (empty string) parameter");
string nullarg_cmd =
"{ \"command\": \"cache-load\", \"arguments\": \"/dev/null\" }";
checkCommand(load_handler, nullarg_cmd, 1, 1, "nothing read");
string notexists = "/this/does/not/exit";
string notexists_cmd =
"{ \"command\": \"cache-load\", \"arguments\": \"" + notexists + "\" }";
checkCommand(load_handler, notexists_cmd, 1, 1,
"failed to read file '" + notexists + "': "
"No such file or directory");
}
// Verifies that cache-remove behaves as expected.
void
CommandTest::testRemoveCommand() {
// Prepare
handlerType del_handler = std::bind(&removeHandler, hcptr_, ph::_1);
ElementPtr del = Element::createMap();
del->set("command", Element::create(string("cache-remove")));
fill();
ASSERT_EQ(4, hcptr_->size());
// Common remove by address.
ElementPtr args = Element::createMap();
args->set("subnet-id", Element::create(4));
args->set("ip-address", Element::create(string("192.0.2.200")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(3, hcptr_->size());
// Remove it a second time won't find it.
checkCommand(del_handler, del, 0, 3, "Host not removed (not found).");
EXPECT_EQ(3, hcptr_->size());
// Common remove by identifier.
args = Element::createMap();
args->set("subnet-id6", Element::create(6));
args->set("hw-address", Element::create(string("07:08:09:0a:0b:0c")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(2, hcptr_->size());
// Subnet-id specification is flexible for by address.
args = Element::createMap();
args->set("subnet-id6", Element::create(6));
args->set("ip-address", Element::create(string("2001:db8:dead:beef::")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(1, hcptr_->size());
// Check errors.
// No parameters
del->remove("arguments");
checkCommand(del_handler, del, 1, 1,
"no parameters specified for the command");
// Not a map
args = Element::createList();
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "invalid (not a map) parameter");
// Unknown parameter
args = Element::createMap();
args->set("subnetID", Element::create(4));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "unknown parameter 'subnetID'");
// No subnet-id
args = Element::createMap();
args->set("ip-address", Element::create(string("192.0.2.200")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "subnet-id is required");
// Bad address
args = Element::createMap();
args->set("subnet-id", Element::create(4));
args->set("ip-address", Element::create(string("192.0.2.2.0")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1,
"Failed to convert string to address '192.0.2.2.0': "
"Invalid argument");
// Undefined IPv4 address
args = Element::createMap();
args->set("subnet-id", Element::create(4));
args->set("ip-address", Element::create(string("0.0.0.0")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "invalid ip-address '0.0.0.0'");
// Undefined IPv6 address
args = Element::createMap();
args->set("subnet-id", Element::create(6));
args->set("ip-address", Element::create(string("::")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "invalid ip-address '::'");
// No address nor identifier
args = Element::createMap();
args->set("subnet-id4", Element::create(4));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1,
"either ip-address or an identifier is required");
// Identifier in unknown family
args = Element::createMap();
args->set("subnet-id", Element::create(4));
args->set("hw-address", Element::create(string("07:08:09:0a:0b:0c")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1,
"either subnet-id4 or subnet-id6 is required");
// Bad identifier
args = Element::createMap();
args->set("subnet-id4", Element::create(4));
args->set("hw-address", Element::create(string("foobar")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "invalid identifier 'foobar'");
// Multiple subnet-id's
args = Element::createMap();
args->set("subnet-id", Element::create(4));
args->set("subnet-id4", Element::create(4));
args->set("hw-address", Element::create(string("07:08:09:0a:0b:0c")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "inconsistent parameters");
// Multiple identifiers
args = Element::createMap();
args->set("subnet-id4", Element::create(4));
args->set("hw-address", Element::create(string("07:08:09:0a:0b:0c")));
args->set("duid", Element::create(string("07:08:09:0a:0b:0c")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "inconsistent parameters");
// Address and subnet-id in different families.
args = Element::createMap();
args->set("subnet-id6", Element::create(4));
args->set("ip-address", Element::create(string("192.0.2.200")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "inconsistent parameters");
// The other combination.
args = Element::createMap();
args->set("subnet-id4", Element::create(6));
args->set("ip-address", Element::create(string("2001:db8::cafe:babe")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "inconsistent parameters");
// Empty identifier
args = Element::createMap();
args->set("subnet-id4", Element::create(4));
args->set("hw-address", Element::create(string("")));
del->set("arguments", args);
checkCommand(del_handler, del, 1, 1, "invalid (empty) identifier");
// All of these failed.
EXPECT_EQ(1, hcptr_->size());
}
// Verifies that global reservations are handled.
void
CommandTest::testGlobal() {
// Prepare remove.
handlerType del_handler = std::bind(&removeHandler, hcptr_, ph::_1);
ElementPtr del = Element::createMap();
del->set("command", Element::create(string("cache-remove")));
// Like fill() but replacing the subnet ID by global (0).
HostPtr host = createHost4();
host->setIPv4SubnetID(SUBNET_ID_GLOBAL);
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost4bis();
host->setIPv4SubnetID(SUBNET_ID_GLOBAL);
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost6();
host->setIPv6SubnetID(SUBNET_ID_GLOBAL);
EXPECT_EQ(0, hcptr_->insert(host, false));
host = createHost6bis();
host->setIPv6SubnetID(SUBNET_ID_GLOBAL);
EXPECT_EQ(0, hcptr_->insert(host, false));
EXPECT_EQ(4, hcptr_->size());
// Copy of remove positive tests.
// Common remove by address.
ElementPtr args = Element::createMap();
args->set("subnet-id",
Element::create(static_cast<int64_t>(SUBNET_ID_GLOBAL)));
args->set("ip-address", Element::create(string("192.0.2.200")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(3, hcptr_->size());
// Remove it a second time won't find it.
checkCommand(del_handler, del, 0, 3, "Host not removed (not found).");
EXPECT_EQ(3, hcptr_->size());
// Common remove by identifier.
args = Element::createMap();
args->set("subnet-id6",
Element::create(static_cast<int64_t>(SUBNET_ID_GLOBAL)));
args->set("hw-address", Element::create(string("07:08:09:0a:0b:0c")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(2, hcptr_->size());
// Subnet-id specification is flexible for by address.
args = Element::createMap();
args->set("subnet-id6",
Element::create(static_cast<int64_t>(SUBNET_ID_GLOBAL)));
args->set("ip-address", Element::create(string("2001:db8:dead:beef::")));
del->set("arguments", args);
checkCommand(del_handler, del, 0, 0, "Host removed.");
EXPECT_EQ(1, hcptr_->size());
}
// Verifies that host-id is correctly handled.
void
CommandTest::testHostID() {
checkHostID(0, 0);
checkHostID(1, 1);
checkHostID(123456ULL, 123456LL);
// 2^63 - 1
checkHostID(9223372036854775807ULL, 9223372036854775807LL);
// 2^63
checkHostID(9223372036854775808ULL, -9223372036854775807LL - 1);
// 2^63 + 1
checkHostID(9223372036854775809ULL, -9223372036854775807LL);
// 2^63 + 10
checkHostID(9223372036854775818ULL, -9223372036854775798LL);
// 2^64 - 10
checkHostID(18446744073709551606ULL, -10LL);
// 2^64 - 1
checkHostID(18446744073709551615ULL, -1LL);
}
// Verifies that the negative cache is correctly handled.
void
CommandTest::testNegative() {
HostPtr host = createHost4();
// Check false case.
bool negative = false;
host->setNegative(negative);
ASSERT_EQ(negative, host->getNegative());
// Check host -> json
ElementPtr json;
ASSERT_NO_THROW(json = isc::host_cache::toElement(host));
ASSERT_TRUE(json);
ConstElementPtr neg = json->get("negative");
ASSERT_FALSE(neg);
// Check json -> host
HCEntryParser parser;
ConstHostPtr hostf;
ASSERT_NO_THROW(hostf = parser.parse(json));
ASSERT_TRUE(hostf);
EXPECT_EQ(negative, hostf->getNegative());
// Again with a negative: false entry
json->set("negative", Element::create(negative));
ASSERT_NO_THROW(hostf = parser.parse(json));
ASSERT_TRUE(hostf);
EXPECT_EQ(negative,hostf->getNegative());
// Check true case.
negative = true;
host->setNegative(negative);
ASSERT_EQ(negative, host->getNegative());
// host -> json
ASSERT_NO_THROW(json = isc::host_cache::toElement(host));
ASSERT_TRUE(json);
neg = json->get("negative");
ASSERT_TRUE(neg);
EXPECT_EQ(negative, neg->boolValue());
// json -> host
ConstHostPtr hostt;
ASSERT_NO_THROW(hostt = parser.parse(json));
ASSERT_TRUE(hostt);
ASSERT_EQ(negative, hostt->getNegative());
}
TEST_F(CommandTest, size) {
testSizeCommand();
}
TEST_F(CommandTest, sizeMultiThreading) {
MultiThreadingTest mt(true);
testSizeCommand();
}
TEST_F(CommandTest, flush) {
testFlushCommand();
}
TEST_F(CommandTest, flushMultiThreading) {
MultiThreadingTest mt(true);
testFlushCommand();
}
TEST_F(CommandTest, dump) {
testDumpCommand();
}
TEST_F(CommandTest, dumpMultiThreading) {
MultiThreadingTest mt(true);
testDumpCommand();
}
TEST_F(CommandTest, byHwAddress) {
testByHwAddressCommand();
}
TEST_F(CommandTest, byHwAddressMultiThreading) {
MultiThreadingTest mt(true);
testByHwAddressCommand();
}
TEST_F(CommandTest, byClientId) {
testByClientIdCommand();
}
TEST_F(CommandTest, byClientIdMultiThreading) {
MultiThreadingTest mt(true);
testByClientIdCommand();
}
TEST_F(CommandTest, byFlexId) {
testByFlexIdCommand();
}
TEST_F(CommandTest, byFlexIdMultiThreading) {
MultiThreadingTest mt(true);
testByFlexIdCommand();
}
TEST_F(CommandTest, byIds) {
testByIdsCommand();
}
TEST_F(CommandTest, byIdsMultiThreading) {
MultiThreadingTest mt(true);
testByIdsCommand();
}
TEST_F(CommandTest, insert) {
testInsertCommand();
}
TEST_F(CommandTest, insertMultiThreading) {
MultiThreadingTest mt(true);
testInsertCommand();
}
TEST_F(CommandTest, write) {
testWriteCommand();
}
TEST_F(CommandTest, writeMultiThreading) {
MultiThreadingTest mt(true);
testWriteCommand();
}
TEST_F(CommandTest, load) {
testLoadCommand();
}
TEST_F(CommandTest, loadMultiThreading) {
MultiThreadingTest mt(true);
testLoadCommand();
}
TEST_F(CommandTest, remove) {
testRemoveCommand();
}
TEST_F(CommandTest, removeMultiThreading) {
MultiThreadingTest mt(true);
testRemoveCommand();
}
TEST_F(CommandTest, global) {
testGlobal();
}
TEST_F(CommandTest, globalMultiThreading) {
MultiThreadingTest mt(true);
testGlobal();
}
TEST_F(CommandTest, hostID) {
testHostID();
}
TEST_F(CommandTest, hostIDMultiThreading) {
MultiThreadingTest mt(true);
testHostID();
}
TEST_F(CommandTest, negative) {
testNegative();
}
TEST_F(CommandTest, negativeMultiThreading) {
MultiThreadingTest mt(true);
testNegative();
}
} // end of anonymous namespace
|