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 | // Copyright (C) 2017-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 verify control command legal file entry
/// generation and callout: command_processed.
/// These tests assume the legal log library is linked in, not loaded.
/// This allows a great deal more flexibility in testing, such as overriding
/// and accessing the BackendStore::instance().
/// The load and unload callouts are exercised in ../libloadtests, which
/// actually uses the HooksManager to load and unload the library.
#include <config.h>
#include <asiolink/io_address.h>
#include <cc/data.h>
#include <cc/command_interpreter.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/cfg_subnets6.h>
#include <dhcpsrv/subnet.h>
#include <exceptions/exceptions.h>
#include <hooks/callout_manager.h>
#include <hooks/hooks.h>
#include <test_utils.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace config;
using namespace data;
using namespace hooks;
using namespace legal_log;
extern "C" {
extern bool getOptionalString(ConstElementPtr& arguments, const string& name,
string& value);
extern bool getOptionalInt(ConstElementPtr& arguments, const string& name,
int64_t& value);
extern bool isPrefix(ConstElementPtr arguments);
extern int command_processed(CalloutHandle& handle);
extern void addDuration(ostringstream& os, ConstElementPtr& arguments);
extern void addContext(ostringstream& os, ConstElementPtr& arguments);
}
/// @brief Describes command name, arguments, and expected log string
struct ValidTest {
const char* name;
const char* arguments;
const char* expected;
};
/// @brief A list of valid command/argument tuples which should produce log entries
ValidTest valid_tests[] =
{
{
"lease4-add",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\" }",
"Administrator added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f"
},
{
"lease4-add",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"client-id\": \"1234567890\" }",
"Administrator added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, client-id: 1234567890"
},
{
"lease4-add",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"valid-lft\": 86400 }",
"Administrator added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease4-add",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"user-context\": { \"foo\": \"bar\" } }",
"Administrator added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, context: { \"foo\": \"bar\" }"
},
{
"lease6-add",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"Administrator added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-add",
"{\"subnet-id\": 66, \"type\": \"IA_PD\", \"ip-address\": \"2001:db8:abcd::\","
" \"prefix-len\": 48, \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"Administrator added a lease of prefix: 2001:db8:abcd::/48 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-add",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"valid-lft\": 86400 }",
"Administrator added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24 for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease6-add",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\"}",
"Administrator added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease6-add",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"user-context\": { \"foo\": \"bar\" } }",
"Administrator added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, context: { \"foo\": \"bar\" }"
},
{
"lease4-del",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\"}",
"Administrator deleted the lease for address: 192.0.2.202"
},
{
"lease4-del",
"{\"subnet-id\": 44, \"identifier-type\": \"hw-address\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f\"}",
"Administrator deleted a lease for a device identified by: hw-address of 1a:1b:1c:1d:1e:1f"
},
{
"lease6-del",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\"}",
"Administrator deleted the lease for address: 2001:db8::3"
},
{
"lease6-del",
"{\"subnet-id\": 66, \"identifier-type\": \"duid\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\"}",
"Administrator deleted a lease for a device identified by:"
" duid of 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease4-update",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\" }",
"Administrator updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease4-update",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"client-id\": \"686f776479\" }",
"Administrator updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, client-id: 686f776479 (howdy)"
},
{
"lease4-update",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"valid-lft\": 86400 }",
"Administrator updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease4-update",
"{\"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"comment\": \"Fred's box\" }",
"Administrator updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address: 1a:1b:1c:1d:1e:1f,"
" context: { \"comment\": \"Fred's box\" }"
},
{
"lease6-update",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"Administrator updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-update",
"{\"subnet-id\": 66, \"type\": \"IA_PD\", \"ip-address\": \"2001:db8:abcd::\","
" \"prefix-len\": 48, \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"Administrator updated information on the lease of prefix:"
" 2001:db8:abcd::/48 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-update",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"68:6f:77:64:79\", \"iaid\": 1234,"
" \"valid-lft\": 86400 }",
"Administrator updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 68:6f:77:64:79 for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease6-update",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\"}",
"Administrator updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease6-update",
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"comment\": \"Fred's box\" }",
"Administrator updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24,"
" context: { \"comment\": \"Fred's box\" }"
},
{
"lease4-del",
"{\"ip-address\": \"192.0.2.202\"}",
"Administrator deleted the lease for address: 192.0.2.202"
},
{
"lease4-del",
"{\"identifier-type\": \"hw-address\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f\"}",
"Administrator deleted a lease for a device identified by: hw-address of 1a:1b:1c:1d:1e:1f"
},
{
"lease6-del",
"{\"ip-address\": \"2001:db8::3\"}",
"Administrator deleted the lease for address: 2001:db8::3"
},
{
"lease6-del",
"{\"identifier-type\": \"duid\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\"}",
"Administrator deleted a lease for a device identified by:"
" duid of 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-bulk-apply",
"{\"leases\": [{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234}]}",
"Administrator updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11"
},
{
"lease6-bulk-apply",
"{\"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"}]}",
"Administrator deleted the lease for address: 2001:db8:1::3"
},
{
"lease6-bulk-apply",
"{\"leases\": [{\"subnet-id\": 66,\"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234},"
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::2\","
" \"duid\": \"22:22:22:22:22:22\", \"iaid\": 1234,"
" \"user-context\": {\"ISC\": {\"relay-info\": [{"
" \"remote-id\": \"010203040506\", \"relay-id\": \"6464646464646464\"}]}}}]}",
"Administrator updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11\n"
"Administrator updated information on the lease of address: 2001:db8:1::2 to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": [ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }"
},
{
"lease6-bulk-apply",
"{\"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"},"
"{\"ip-address\": \"2001:db8:1::4\", \"type\": \"IA_NA\"}]}",
"Administrator deleted the lease for address: 2001:db8:1::3\n"
"Administrator deleted the lease for address: 2001:db8:1::4"
},
{
"lease6-bulk-apply",
"{\"leases\": [{\"subnet-id\": 66,\"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234},"
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::2\","
" \"duid\": \"22:22:22:22:22:22\", \"iaid\": 1234,"
" \"user-context\": {\"ISC\": {\"relay-info\": [{"
" \"remote-id\": \"010203040506\", \"relay-id\": \"6464646464646464\"}]}}}],"
" \"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"},"
"{\"ip-address\": \"2001:db8:1::4\", \"type\": \"IA_NA\"}]}",
"Administrator deleted the lease for address: 2001:db8:1::3\n"
"Administrator deleted the lease for address: 2001:db8:1::4\n"
"Administrator updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11\n"
"Administrator updated information on the lease of address: 2001:db8:1::2 to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": [ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }"
},
{
"lease4-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\" }",
"HA partner added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f"
},
{
"lease4-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"client-id\": \"1234567890\" }",
"HA partner added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, client-id: 1234567890"
},
{
"lease4-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"valid-lft\": 86400 }",
"HA partner added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease4-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"user-context\": { \"foo\": \"bar\" } }",
"HA partner added a lease of address: 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, context: { \"foo\": \"bar\" }"
},
{
"lease6-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"HA partner added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"type\": \"IA_PD\", \"ip-address\": \"2001:db8:abcd::\","
" \"prefix-len\": 48, \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"HA partner added a lease of prefix: 2001:db8:abcd::/48 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"valid-lft\": 86400 }",
"HA partner added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24 for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease6-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\"}",
"HA partner added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease6-add",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"user-context\": { \"foo\": \"bar\" } }",
"HA partner added a lease of address: 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, context: { \"foo\": \"bar\" }"
},
{
"lease4-del",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\"}",
"HA partner deleted the lease for address: 192.0.2.202"
},
{
"lease4-del",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"identifier-type\": \"hw-address\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f\"}",
"HA partner deleted a lease for a device identified by: hw-address of 1a:1b:1c:1d:1e:1f"
},
{
"lease6-del",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\"}",
"HA partner deleted the lease for address: 2001:db8::3"
},
{
"lease6-del",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"identifier-type\": \"duid\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\"}",
"HA partner deleted a lease for a device identified by:"
" duid of 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease4-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\" }",
"HA partner updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease4-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"client-id\": \"686f776479\" }",
"HA partner updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f, client-id: 686f776479 (howdy)"
},
{
"lease4-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"valid-lft\": 86400 }",
"HA partner updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address:"
" 1a:1b:1c:1d:1e:1f for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease4-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 44, \"ip-address\": \"192.0.2.202\","
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\","
" \"comment\": \"Fred's box\" }",
"HA partner updated information on the lease of address:"
" 192.0.2.202 to a device with hardware address: 1a:1b:1c:1d:1e:1f,"
" context: { \"comment\": \"Fred's box\" }"
},
{
"lease6-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"HA partner updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"type\": \"IA_PD\", \"ip-address\": \"2001:db8:abcd::\","
" \"prefix-len\": 48, \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234}",
"HA partner updated information on the lease of prefix:"
" 2001:db8:abcd::/48 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"68:6f:77:64:79\", \"iaid\": 1234,"
" \"valid-lft\": 86400 }",
"HA partner updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 68:6f:77:64:79 for 1 days 0 hrs 0 mins 0 secs"
},
{
"lease6-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"hw-address\": \"1a:1b:1c:1d:1e:1f\"}",
"HA partner updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24, hardware address: 1a:1b:1c:1d:1e:1f"
},
{
"lease6-update",
"{\"origin\": \"ha-partner\", \"subnet-id\": 66, \"ip-address\": \"2001:db8::3\","
" \"duid\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\", \"iaid\": 1234,"
" \"comment\": \"Fred's box\" }",
"HA partner updated information on the lease of address:"
" 2001:db8::3 to a device with DUID:"
" 1a:1b:1c:1d:1e:1f:20:21:22:23:24,"
" context: { \"comment\": \"Fred's box\" }"
},
{
"lease4-del",
"{\"origin\": \"ha-partner\", \"ip-address\": \"192.0.2.202\"}",
"HA partner deleted the lease for address: 192.0.2.202"
},
{
"lease4-del",
"{\"origin\": \"ha-partner\", \"identifier-type\": \"hw-address\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f\"}",
"HA partner deleted a lease for a device identified by: hw-address of 1a:1b:1c:1d:1e:1f"
},
{
"lease6-del",
"{\"origin\": \"ha-partner\", \"ip-address\": \"2001:db8::3\"}",
"HA partner deleted the lease for address: 2001:db8::3"
},
{
"lease6-del",
"{\"origin\": \"ha-partner\", \"identifier-type\": \"duid\","
" \"identifier\": \"1a:1b:1c:1d:1e:1f:20:21:22:23:24\"}",
"HA partner deleted a lease for a device identified by:"
" duid of 1a:1b:1c:1d:1e:1f:20:21:22:23:24"
},
{
"lease6-bulk-apply",
"{\"origin\": \"ha-partner\", \"leases\": [{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234}]}",
"HA partner updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11"
},
{
"lease6-bulk-apply",
"{\"origin\": \"ha-partner\", \"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"}]}",
"HA partner deleted the lease for address: 2001:db8:1::3"
},
{
"lease6-bulk-apply",
"{\"origin\": \"ha-partner\", \"leases\": [{\"subnet-id\": 66,\"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234},"
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::2\","
" \"duid\": \"22:22:22:22:22:22\", \"iaid\": 1234,"
" \"user-context\": {\"ISC\": {\"relay-info\": [{"
" \"remote-id\": \"010203040506\", \"relay-id\": \"6464646464646464\"}]}}}]}",
"HA partner updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11\n"
"HA partner updated information on the lease of address: 2001:db8:1::2 to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": [ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }"
},
{
"lease6-bulk-apply",
"{\"origin\": \"ha-partner\", \"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"},"
"{\"ip-address\": \"2001:db8:1::4\", \"type\": \"IA_NA\"}]}",
"HA partner deleted the lease for address: 2001:db8:1::3\n"
"HA partner deleted the lease for address: 2001:db8:1::4"
},
{
"lease6-bulk-apply",
"{\"origin\": \"ha-partner\", \"leases\": [{\"subnet-id\": 66,\"ip-address\": \"2001:db8:1::1\","
" \"duid\": \"11:11:11:11:11:11\", \"iaid\": 1234},"
"{\"subnet-id\": 66, \"ip-address\": \"2001:db8:1::2\","
" \"duid\": \"22:22:22:22:22:22\", \"iaid\": 1234,"
" \"user-context\": {\"ISC\": {\"relay-info\": [{"
" \"remote-id\": \"010203040506\", \"relay-id\": \"6464646464646464\"}]}}}],"
" \"deleted-leases\": [{\"ip-address\": \"2001:db8:1::3\", \"type\": \"IA_NA\"},"
"{\"ip-address\": \"2001:db8:1::4\", \"type\": \"IA_NA\"}]}",
"HA partner deleted the lease for address: 2001:db8:1::3\n"
"HA partner deleted the lease for address: 2001:db8:1::4\n"
"HA partner updated information on the lease of address: 2001:db8:1::1 to a device with DUID: 11:11:11:11:11:11\n"
"HA partner updated information on the lease of address: 2001:db8:1::2 to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": [ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }"
},
{NULL,NULL,NULL}
};
namespace {
// Exercises the getOptionString() function
TEST(CommandCalloutFuncs, getOptionalStringTest) {
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"foo\": \"bar\", \"num\" : 3 }"));
string value;
bool ret = false;
// Should find the value "bar" for "foo"
ASSERT_NO_THROW(ret = getOptionalString(arguments, "foo", value));
EXPECT_TRUE(ret);
EXPECT_EQ("bar", value);
// Should not find a value for "not-there"
ASSERT_NO_THROW(ret = getOptionalString(arguments, "not-there", value));
EXPECT_FALSE(ret);
EXPECT_EQ("", value);
// Should not find a string when it is an integer"
ASSERT_NO_THROW(ret = getOptionalString(arguments, "num", value));
EXPECT_FALSE(ret);
EXPECT_EQ("", value);
}
// Exercises the getOptionInt() function
TEST(CommandCalloutFuncs, getOptionalIntTest) {<--- syntax error
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"foo\": \"bar\", \"num\" : 3 }"));
int64_t value;
bool ret = false;
// Should find the value 3 for "num"
ASSERT_NO_THROW(ret = getOptionalInt(arguments, "num", value));
EXPECT_TRUE(ret);
EXPECT_EQ(3, value);
// Should not an integer for "foo"
ASSERT_NO_THROW(ret = getOptionalInt(arguments, "foo", value));
EXPECT_FALSE(ret);
EXPECT_EQ(0, value);
// Should not an integer for "not-there"
ASSERT_NO_THROW(ret = getOptionalInt(arguments, "not-there", value));
EXPECT_FALSE(ret);
EXPECT_EQ(0, value);
}
// Exercises the isPrefix() function
TEST(CommandCalloutFuncs, isPrefixTest) {
ConstElementPtr arguments;
bool ret = false;
// Should not be a prefix if type isn't there
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"foo\": \"bar\" }"));
ASSERT_NO_THROW(ret = isPrefix(arguments));
EXPECT_FALSE(ret);
// Should not be a prefix if type is there but isn't "IA_PD"
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"type\": \"monkey\" }"));
ASSERT_NO_THROW(ret = isPrefix(arguments));
EXPECT_FALSE(ret);
// Should be a prefix if type is there and is "IA_PD"
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"type\": \"IA_PD\" }"));
ASSERT_NO_THROW(ret = isPrefix(arguments));
EXPECT_TRUE(ret);
// Should be a prefix if type is there and is "2"
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"type\": \"2\" }"));
ASSERT_NO_THROW(ret = isPrefix(arguments));
EXPECT_TRUE(ret);
}
// Exercises the addDuration() function
TEST_F(CalloutTest, addDurationTest) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Should generate duration text of 1 day based on valid-lft
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"valid-lft\": 86400 }"));
ostringstream os;
ASSERT_NO_THROW(addDuration(os, arguments));
EXPECT_EQ(os.str(), " for 1 days 0 hrs 0 mins 0 secs");
// Clear the stream
os.str("");
// Should not generate duration text.
ASSERT_NO_THROW(arguments = Element::fromJSON("{ \"some-other\": 86400 }"));
ASSERT_NO_THROW(addDuration(os, arguments));
EXPECT_EQ(os.str(), "");
// Should have a duration of 2 days based on expire timestamp.
int64_t expire = BackendStore::instance()->now().tv_sec + 172800;
os.str("");
os << "{ \"expire\":" << expire << "}";
ASSERT_NO_THROW(arguments = Element::fromJSON(os.str()));
os.str("");
ASSERT_NO_THROW(addDuration(os, arguments));
EXPECT_EQ(os.str(), " for 2 days 0 hrs 0 mins 0 secs");
}
// Exercises the addContext() function
TEST_F(CalloutTest, addContext) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Should not generate user context text.
string args = "{ \"some-other\": 86400 }";
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(args));
ostringstream os;
ASSERT_NO_THROW(addContext(os, arguments));
EXPECT_EQ(os.str(), "");
// Should generate user context text.
args = "{ \"user-context\": { \"foobar\": 1234 } }";
ASSERT_NO_THROW(arguments = Element::fromJSON(args));
ASSERT_NO_THROW(addContext(os, arguments));
EXPECT_EQ(os.str(), ", context: { \"foobar\": 1234 }");
// Should generate comment text.
args = "{ \"comment\": \"a comment\" }";
ASSERT_NO_THROW(arguments = Element::fromJSON(args));
os.str("");
ASSERT_NO_THROW(addContext(os, arguments));
EXPECT_EQ(os.str(), ", context: { \"comment\": \"a comment\" }");
// Should generate user context and comment text.
args = "{ \"user-context\": { \"foobar\": 1234 }, "
"\"comment\": \"a comment\" }";
ASSERT_NO_THROW(arguments = Element::fromJSON(args));
os.str("");
ASSERT_NO_THROW(addContext(os, arguments));
EXPECT_EQ(os.str(),
", context: { \"comment\": \"a comment\", \"foobar\": 1234 }");
}
// Iterates over a list of valid command/argument combinations and verifies that
// each produces the expected log in the log file
TEST_F(CalloutTest, validCommandEntries) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Make a callout handle
CalloutHandle handle(getCalloutManager());
ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, "it works!");
vector<string>lines;
ValidTest *test = valid_tests;
int test_cnt = 1;
while (test && test->name) {
// Populate the callout context as the hookpoint should
handle.setArgument("name", string(test->name));
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(test->arguments));
handle.setArgument("arguments", arguments);
handle.setArgument("response", response);
// Callout should succeed and generate an entry
int ret;
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret) << "test#:" << test_cnt << " "
<< test->name << " : " << test->arguments;
// Add the expected lines to the list
istringstream expected_lines(test->expected);
string line;
while (getline(expected_lines, line)) {
lines.push_back(line);
}
++test;
++test_cnt;
}
// Close it to flush any unwritten data
BackendStore::instance()->close();
// Verify that the file content is correct.
string today_now_string = BackendStore::instance()->getNowString();
checkFileLines(genName(today()), today_now_string, lines);
}
// Iterates over a list of valid command/argument combinations and verifies that
// each produces the expected log in the log file
TEST_F(CalloutTest, responseWithErrorsLease6BulkApplyCommandEntries) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Make a callout handle
CalloutHandle handle(getCalloutManager());
vector<string>lines;
vector<string> responses = {
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}],"
" \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}",
"{ \"failed-leases\": [{ \"ip-address\": \"2001:db8:1::1\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}],"
" \"failed-deleted-leases\": [{ \"ip-address\": \"2001:db8:1::3\","
" \"type\": \"IA_NA\", \"result\": 3,"
" \"error-mesage\": \"It's Friday and we don't do Fridays!\"}]}"
};
vector<string> expected = {
"",
"",
"Administrator updated information on the lease of address: 2001:db8:1::2 "
"to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": "
"[ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }",
"Administrator deleted the lease for address: 2001:db8:1::4",
"Administrator deleted the lease for address: 2001:db8:1::4\n"
"Administrator updated information on the lease of address: 2001:db8:1::2 "
"to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": "
"[ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }",
"",
"",
"HA partner updated information on the lease of address: 2001:db8:1::2 "
"to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": "
"[ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }",
"HA partner deleted the lease for address: 2001:db8:1::4",
"HA partner deleted the lease for address: 2001:db8:1::4\n"
"HA partner updated information on the lease of address: 2001:db8:1::2 "
"to a device with DUID: 22:22:22:22:22:22, context: { \"ISC\": { \"relay-info\": "
"[ { \"relay-id\": \"6464646464646464\", \"remote-id\": \"010203040506\" } ] } }"
};
ValidTest *test = valid_tests;
int test_cnt = 1;
while (test && test->name) {
if (string(test->name).find("bulk-apply") != string::npos) {
ElementPtr args = Element::fromJSON(responses[test_cnt - 1]);
ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, "", args);
handle.setArgument("response", response);
// Populate the callout context as the hookpoint should
handle.setArgument("name", string(test->name));
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(test->arguments));
handle.setArgument("arguments", arguments);
// Callout should succeed and generate an entry
int ret;
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret) << "test#:" << test_cnt << " "
<< test->name << " : " << test->arguments;
// Add the expected lines to the list
istringstream expected_lines(expected[test_cnt - 1]);
string line;
while (getline(expected_lines, line)) {
lines.push_back(line);
}
++test_cnt;
}
++test;
}
// Close it to flush any unwritten data
BackendStore::instance()->close();
// Verify that the file content is correct.
string today_now_string = BackendStore::instance()->getNowString();
checkFileLines(genName(today()), today_now_string, lines);
}
// This test verifies that it is possible to disable logging for selected IPv4
// subnets.
TEST_F(CalloutTest, disableLoggingForSubnet4) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Create a subnet with user context disabling legal logging.
Subnet4Ptr subnet4(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 50,
SubnetID(44)));
ElementPtr ctx4 = Element::createMap();
ctx4->set("legal-logging", Element::create(false));
subnet4->setContext(ctx4);
CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet4);
// Explicitly enable legal logging for the other subnet.
Subnet6Ptr subnet6(new Subnet6(IOAddress("2001:db8::"), 48, 30, 40, 50, 60,
SubnetID(66)));
ElementPtr ctx6 = Element::createMap();
ctx6->set("legal-logging", Element::create(true));
subnet6->setContext(ctx6);
CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet6);
CfgMgr::instance().commit();
// Make a callout handle
CalloutHandle handle(getCalloutManager());
ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, "it works!");
vector<string>lines;
ValidTest *test = valid_tests;
int test_cnt = 1;
while (test && test->name) {
if (string(test->name).find("bulk-apply") == string::npos) {
// Populate the callout context as the hookpoint should
handle.setArgument("name", string(test->name));
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(test->arguments));
handle.setArgument("arguments", arguments);
handle.setArgument("response", response);
// Callout should succeed and generate an entry
int ret;
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret) << "test#:" << test_cnt << " "
<< test->name << " : " << test->arguments;
// Only add log entry for non-v4 leases and for those commands which lack
// subnet identifier.
if ((string(test->name).find("lease4-") == string::npos) ||
(string(test->arguments).find("subnet-id") == string::npos)) {
// Add the expected lines to the list
istringstream expected_lines(test->expected);
string line;
while (getline(expected_lines, line)) {
lines.push_back(line);
}
}
++test_cnt;
}
++test;
}
// Close it to flush any unwritten data
BackendStore::instance()->close();
// Verify that the file content is correct.
string today_now_string = BackendStore::instance()->getNowString();
checkFileLines(genName(today()), today_now_string, lines);
}
// This test verifies that it is possible to disable logging for selected IPv6
// subnets.
TEST_F(CalloutTest, disableLoggingForSubnet6) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Create a subnet with user context disabling legal logging.
Subnet6Ptr subnet6(new Subnet6(IOAddress("2001:db8::"), 48, 30, 40, 50, 60,
SubnetID(66)));
ElementPtr ctx6 = Element::createMap();
ctx6->set("legal-logging", Element::create(false));
subnet6->setContext(ctx6);
CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet6);
// Explicitly enable legal logging for the other subnet.
Subnet4Ptr subnet4(new Subnet4(IOAddress("192.0.2.0"), 24, 30, 40, 50,
SubnetID(44)));
ElementPtr ctx4 = Element::createMap();
ctx4->set("legal-logging", Element::create(true));
subnet4->setContext(ctx4);
CfgMgr::instance().getStagingCfg()->getCfgSubnets4()->add(subnet4);
CfgMgr::instance().commit();
// Make a callout handle
CalloutHandle handle(getCalloutManager());
ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, "it works!");
vector<string>lines;
ValidTest *test = valid_tests;
int test_cnt = 1;
while (test && test->name) {
if (string(test->name).find("bulk-apply") == string::npos) {
// Populate the callout context as the hookpoint should
handle.setArgument("name", string(test->name));
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(test->arguments));
handle.setArgument("arguments", arguments);
handle.setArgument("response", response);
// Callout should succeed and generate an entry
int ret;
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret) << "test#:" << test_cnt << " "
<< test->name << " : " << test->arguments;
// Only add log entry for non-v6 leases and for those commands which lack
// subnet identifier.
if ((string(test->name).find("lease6-") == string::npos) ||
(string(test->arguments).find("subnet-id") == string::npos)) {
// Add the expected lines to the list
istringstream expected_lines(test->expected);
string line;
while (getline(expected_lines, line)) {
lines.push_back(line);
}
}
++test_cnt;
}
++test;
}
// Close it to flush any unwritten data
BackendStore::instance()->close();
// Verify that the file content is correct.
string today_now_string = BackendStore::instance()->getNowString();
checkFileLines(genName(today()), today_now_string, lines);
}
// Tests that a command with a failed result code does not generate a log entry
TEST_F(CalloutTest, failedCommand) {
ASSERT_NO_THROW(BackendStore::instance().reset(new TestableRotatingFile(time_)));
// Make a callout handle
CalloutHandle handle(getCalloutManager());
// First we use a known good command and success to generate
// an entry (otherwise rotate file won't get created
handle.setArgument("name", string(valid_tests[0].name));
ConstElementPtr arguments;
ASSERT_NO_THROW(arguments = Element::fromJSON(valid_tests[0].arguments));
handle.setArgument("arguments", arguments);
ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, "command success!");
handle.setArgument("response", response);
// Callout should succeed and generate an entry
int ret;
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret);
vector<string>lines;
lines.push_back(valid_tests[0].expected);
// Now replace the response with a failure result and try again
response = createAnswer(777, "command failed!");
handle.setArgument("response", response);
// Callout should succeed and but not generate an entry
ASSERT_NO_THROW(ret = command_processed(handle));
ASSERT_EQ(0, ret);
// Close it to flush any unwritten data
BackendStore::instance()->close();
// Verify that the file content has only the one expected line.
string today_now_string = BackendStore::instance()->getNowString();
checkFileLines(genName(today()), today_now_string, lines);
}
} // end of anonymous namespace
|