-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_logging.c
802 lines (714 loc) · 34 KB
/
test_logging.c
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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "nr_banner.h"
#include "util_logging.h"
#include "util_logging_private.h"
#include "util_memory.h"
#include "util_strings.h"
#include "util_syscalls.h"
#include "tlib_main.h"
/*
* throw away any strings involving the uid or gid. This filter is pretty
* coarse.
*/
const char* cleanup_string
= "sed "
"-e 's/ os=[^]]*//g' "
"-e 's/id=[0-9]*/id=-1/g' "
"-e 's/New Relic [0-9.]*/New Relic X.Y.Z.W/' "
"-e 's/(\"[-.a-zA-Z0-9]*\" - \"[0-9a-fA-F]*\")/(\"NAME\" - \"GITSHA\")/' "
"| sed -e 's/^[^a-fA-F]*[0-9]*) //'";
static void test_set_log_level(void);
static void test_logging(void) {
int i;
nr_status_t rv;
/*
* Need to ensure we don't start out with existing log files from multiple
* test runs.
*/
nr_unlink("logtest1.tmp");
nr_unlink("logtest2.tmp");
/*
* Try sending a log message before anything has been initialized. Should
* fail.
*/
rv = nrl_send_log_message(NRL_ALWAYS, "test should fail");
tlib_pass_if_true("log write before initialization failed", NR_FAILURE == rv,
"rv=%d", (int)rv);
/*
* Try to initialize to an impossible to write to file. Should fail.
*/
rv = nrl_set_log_file("/should/not/exist");
tlib_pass_if_true("initialization to bad path fails", NR_FAILURE == rv,
"rv=%d", (int)rv);
/*
* Attempts to log should still fail.
*/
rv = nrl_send_log_message(NRL_ALWAYS, "test should fail");
tlib_pass_if_true("log write after failed init fails", NR_FAILURE == rv,
"rv=%d", (int)rv);
/*
* Open to a valid file should succeed.
*/
rv = nrl_set_log_file("./logtest1.tmp");
tlib_pass_if_true("log initialization succeeds", NR_SUCCESS == rv, "rv=%d",
(int)rv);
tlib_pass_if_exists("./logtest1.tmp");
/*
* Log message should succeed now.
*/
rv = nrl_send_log_message(NRL_ALWAYS, "expect PASS 1");
tlib_pass_if_true("NRL_ALWAYS succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
/*
* Test the logging macros at the default level. Whether or not these tests
* succeed will be determined when the log file is compared to the reference
* version at the end of these tests.
*/
nrl_always("NRL_ALWAYS should be present (1)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (1)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (1)");
nrl_info(NRL_TEST, "NRL_INFO should be present (1)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (1)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (1)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (1)");
/*
* Test mechanisms to thwart log injection.
*/
{
const char* short_appname = "01234";
const char* long_appname
= "01234567890123456789012345678901234567890123456789";
nrl_error(NRL_TEST, "A short appname " NRP_FMT, NRP_APPNAME(short_appname));
nrl_error(NRL_TEST, "A 50 char appname truncated " NRP_FMT,
NRP_APPNAME(long_appname)); /* 50 chars */
}
/*
* Test long log messages, which is potentially a bit of a stress for
* the vasprintf. We have seen cases where asan complains about the
* free of the vasprintf allocated memory from within the logging
* engine.
*/
for (i = 1; i <= (1 << 9); i <<= 1) {
nrl_error(NRL_TEST, "Variable width int %*d", i, i);
}
/*
* Close the log file.
*/
nrl_close_log_file();
tlib_pass_if_int_equal("close log file", -1, nrl_get_log_fd());
/*
* A second close attempt should not blow up.
*/
nrl_close_log_file();
tlib_pass_if_int_equal("still closed", -1, nrl_get_log_fd());
/*
* Writing a log message should fail (file is closed).
*/
rv = nrl_send_log_message(NRL_ALWAYS, "test should fail");
tlib_pass_if_true("log write after close failed", NR_FAILURE == rv, "rv=%d",
(int)rv);
/*
* Reopen the same file. Data should be appended. Logging should succeed.
*/
rv = nrl_set_log_file("./logtest1.tmp");
tlib_pass_if_true("log reopen succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_exists("./logtest1.tmp");
rv = nrl_send_log_message(NRL_ALWAYS, "expect PASS 2");
tlib_pass_if_true("NRL_ALWAYS succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
/*
* Put in a banner, with various configurations.
*/
nr_banner("daemon_location", NR_DAEMON_STARTUP_UNKOWN, "Axiom Tests");
nr_banner("daemon_location", NR_DAEMON_STARTUP_INIT, "Axiom Tests");
nr_banner("daemon_location", NR_DAEMON_STARTUP_AGENT, "Axiom Tests");
nr_banner(0, NR_DAEMON_STARTUP_AGENT, "Axiom Tests");
/*
* Retest the macros with newly reopened log file.
*/
nrl_always("NRL_ALWAYS should be present (2)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (2)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (2)");
nrl_info(NRL_TEST, "NRL_INFO should be present (2)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (2)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (2)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (2)");
/*
* Change the log file name without first closing the old file. Should
* succeed and messages should be written just fine.
*/
rv = nrl_set_log_file("./logtest2.tmp");
tlib_pass_if_true("log change succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_exists("./logtest2.tmp");
rv = nrl_send_log_message(NRL_ALWAYS, "expect PASS 3");
tlib_pass_if_true("NRL_ALWAYS succeeds", NR_SUCCESS == rv, "rv=%d", (int)rv);
nrl_always("NRL_ALWAYS should be present (3)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (3)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (3)");
nrl_info(NRL_TEST, "NRL_INFO should be present (3)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (3)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (3)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (3)");
test_set_log_level();
nrl_close_log_file();
}
/*
* Now do the tests for setting the log level. We do this by manually
* inspecting the nrl_level_mask_ptr array after doing the various settings.
* We will also actually call the macro functions at key points and
* verify that only the correct messages make it through to the log file
* when we compare the generated log file to the reference version. We
* check each and every valid log level first.
*/
static void test_set_log_level_simple(void) {
nr_status_t rv;
/*
* Simple settings of valid levels.
*/
rv = nrl_set_log_level("error");
tlib_pass_if_true("set level (error)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = 0", 0 == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = 0", 0 == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (4)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (4)");
nrl_warning(NRL_TEST, "NRL_WARNING should not be present (4)");
nrl_info(NRL_TEST, "NRL_INFO should not be present (4)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (4)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (4)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (4)");
}
static void test_set_log_level_null(void) {
nr_status_t rv;
/*
* Passing NULL as the level should default to info.
*/
rv = nrl_set_log_level(0);
tlib_pass_if_true("set level (0)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (5)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (5)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (5)");
nrl_info(NRL_TEST, "NRL_INFO should be present (5)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (5)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (5)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (5)");
}
static void test_set_log_level_warning(void) {
nr_status_t rv;
rv = nrl_set_log_level("warning");
tlib_pass_if_true("set level (warning)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = 0", 0 == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (6)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (6)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (6)");
nrl_info(NRL_TEST, "NRL_INFO should not be present (6)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (6)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (6)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (6)");
}
static void test_set_log_level_info(void) {
nr_status_t rv;
rv = nrl_set_log_level("info");
tlib_pass_if_true("set level (info)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (7)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (7)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (7)");
nrl_info(NRL_TEST, "NRL_INFO should be present (7)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (7)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (7)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (7)");
}
static void test_set_log_level_verbose(void) {
nr_status_t rv;
rv = nrl_set_log_level("verbose");
tlib_pass_if_true("set level (verbose)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (8)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (8)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (8)");
nrl_info(NRL_TEST, "NRL_INFO should be present (8)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should be present (8)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (8)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (8)");
}
static void test_set_log_level_debug(void) {
nr_status_t rv;
rv = nrl_set_log_level("debug");
tlib_pass_if_true("set level (debug)", NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (9)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (9)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (9)");
nrl_info(NRL_TEST, "NRL_INFO should be present (9)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should be present (9)");
nrl_debug(NRL_TEST, "NRL_DEBUG should be present (9)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (9)");
}
static void test_set_log_level_verbosedebug(void) {
nr_status_t rv;
rv = nrl_set_log_level("verbosedebug");
tlib_pass_if_true("set level (verbosedebug)", NR_SUCCESS == rv, "rv=%d",
(int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_VERBOSEDEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (9)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (9)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (9)");
nrl_info(NRL_TEST, "NRL_INFO should be present (9)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should be present (9)");
nrl_debug(NRL_TEST, "NRL_DEBUG should be present (9)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should be present (9)");
}
static void test_set_log_level_bogus(void) {
nr_status_t rv;
/*
* Now test a simple invalid level. Should have the effect of falling back
* to the default (info).
*/
rv = nrl_set_log_level("bogus");
tlib_pass_if_true("set level (bogus)", NR_FAILURE == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (10)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (10)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (10)");
nrl_info(NRL_TEST, "NRL_INFO should be present (10)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (10)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (10)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (10)");
}
static void test_set_log_level_overall(void) {
nr_status_t rv;
/*
* Set an overall level, and turn on extra for some subsystems.
*/
rv = nrl_set_log_level("warning,autorum=verbose,framework=verbosedebug");
tlib_pass_if_true(
"set level (warning,autorum=verbose,framework=verbosedebug)",
NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true(
"mask[info] = ALL",
(NRL_AUTORUM | NRL_FRAMEWORK) == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true(
"mask[verbose] = 0",
(NRL_AUTORUM | NRL_FRAMEWORK) == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0",
NRL_FRAMEWORK == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
NRL_FRAMEWORK == nrl_level_mask_ptr[NRL_VERBOSEDEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (11)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (11)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (11)");
nrl_info(NRL_TEST, "NRL_INFO should not be present (11)");
nrl_info(NRL_AUTORUM, "NRL_INFO(AUTORUM) should be present (11)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (11)");
nrl_verbose(NRL_FRAMEWORK, "NRL_VERBOSE(FRAMEWORK) should be present (11)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (11)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (11)");
nrl_verbosedebug(NRL_FRAMEWORK,
"NRL_VERBOSEDEBUG(FRAMEWORK) should be present (11)");
}
static void test_set_log_level_invalid_subsystem(void) {
nr_status_t rv;
/*
* Set an invalid sub-system. Should reset everything back to defaults.
*/
rv = nrl_set_log_level("verbosedebug,bogus=debug");
tlib_pass_if_true("set level (verbosedebug,bogus=debug)", NR_FAILURE == rv,
"rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true("mask[info] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true("mask[verbose] = 0", 0 == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = 0", 0 == nrl_level_mask_ptr[NRL_DEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = 0",
0 == nrl_level_mask_ptr[NRL_VERBOSEDEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (12)");
nrl_error(NRL_TEST, "NRL_ERROR should be present (12)");
nrl_warning(NRL_TEST, "NRL_WARNING should be present (12)");
nrl_info(NRL_TEST, "NRL_INFO should be present (12)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE should not be present (12)");
nrl_debug(NRL_TEST, "NRL_DEBUG should not be present (12)");
nrl_verbosedebug(NRL_TEST, "NRL_VERBOSEDEBUG should not be present (12)");
}
static void test_set_log_level_3_subsystems(void) {
nr_status_t rv;
/*
* Don't set an overall level, only set 3 subsystems to different values.
*/
rv = nrl_set_log_level("metrics=info,listener=verbose,daemon=verbosedebug");
tlib_pass_if_true(
"set level (metrics=info,listener=verbose,daemon=verbosedebug)",
NR_SUCCESS == rv, "rv=%d", (int)rv);
tlib_pass_if_true("mask[always] = ALL",
NRL_ALL_FLAGS == nrl_level_mask_ptr[NRL_ALWAYS],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ALWAYS]);
tlib_pass_if_true("mask[error] = METRICS|LISTENER|DAEMON",
(NRL_METRICS | NRL_LISTENER | NRL_DAEMON)
== nrl_level_mask_ptr[NRL_ERROR],
"mask=0x%08x", nrl_level_mask_ptr[NRL_ERROR]);
tlib_pass_if_true("mask[warning] = METRICS|LISTENER|DAEMON",
(NRL_METRICS | NRL_LISTENER | NRL_DAEMON)
== nrl_level_mask_ptr[NRL_WARNING],
"mask=0x%08x", nrl_level_mask_ptr[NRL_WARNING]);
tlib_pass_if_true(
"mask[info] = METRICS|LISTENER|DAEMON",
(NRL_METRICS | NRL_LISTENER | NRL_DAEMON) == nrl_level_mask_ptr[NRL_INFO],
"mask=0x%08x", nrl_level_mask_ptr[NRL_INFO]);
tlib_pass_if_true(
"mask[verbose] = LISTENER|DAEMON",
(NRL_LISTENER | NRL_DAEMON) == nrl_level_mask_ptr[NRL_VERBOSE],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSE]);
tlib_pass_if_true("mask[debug] = DAEMON",
NRL_DAEMON == nrl_level_mask_ptr[NRL_DEBUG], "mask=0x%08x",
nrl_level_mask_ptr[NRL_DEBUG]);
tlib_pass_if_true("mask[verbosedebug] = DAEMON",
NRL_DAEMON == nrl_level_mask_ptr[NRL_VERBOSEDEBUG],
"mask=0x%08x", nrl_level_mask_ptr[NRL_VERBOSEDEBUG]);
nrl_always("NRL_ALWAYS should be present (13)");
nrl_error(NRL_TEST, "NRL_ERROR(TEST) should not be present (13)");
nrl_error(NRL_LISTENER, "NRL_ERROR(LISTENER) should be present (13)");
nrl_error(NRL_DAEMON, "NRL_ERROR(DAEMON) should be present (13)");
nrl_error(NRL_METRICS, "NRL_ERROR(METRICS) should be present (13)");
nrl_warning(NRL_TEST, "NRL_WARNING(TEST) should not be present (13)");
nrl_warning(NRL_LISTENER, "NRL_WARNING(LISTENER) should be present (13)");
nrl_warning(NRL_DAEMON, "NRL_WARNING(DAEMON) should be present (13)");
nrl_warning(NRL_METRICS, "NRL_WARNING(METRICS) should be present (13)");
nrl_info(NRL_TEST, "NRL_INFO(TEST) should not be present (13)");
nrl_info(NRL_LISTENER, "NRL_INFO(LISTENER) should be present (13)");
nrl_info(NRL_METRICS, "NRL_INFO(METRICS) should be present (13)");
nrl_info(NRL_DAEMON, "NRL_INFO(DAEMON) should be present (13)");
nrl_verbose(NRL_TEST, "NRL_VERBOSE(TEST) should not be present (13)");
nrl_verbose(NRL_LISTENER, "NRL_VERBOSE(LISTENER) should be present (13)");
nrl_verbose(NRL_METRICS, "NRL_VERBOSE(METRICS) should not be present (13)");
nrl_verbose(NRL_DAEMON, "NRL_VERBOSE(DAEMON) should be present (13)");
nrl_debug(NRL_TEST, "NRL_DEBUG(TEST) should not be present (13)");
nrl_debug(NRL_LISTENER, "NRL_DEBUG(LISTENER) should not be present (13)");
nrl_debug(NRL_METRICS, "NRL_DEBUG(METRICS) should not be present (13)");
nrl_debug(NRL_DAEMON, "NRL_DEBUG(DAEMON) should be present (13)");
nrl_verbosedebug(NRL_TEST,
"NRL_VERBOSEDEBUG(TEST) should not be present (13)");
nrl_verbosedebug(NRL_LISTENER,
"NRL_VERBOSEDEBUG(LISTENER) should not be present (13)");
nrl_verbosedebug(NRL_METRICS,
"NRL_VERBOSEDEBUG(METRICS) should not be present (13)");
nrl_verbosedebug(NRL_DAEMON,
"NRL_VERBOSEDEBUG(DAEMON) should be present (13)");
}
static void test_set_log_level(void) {
test_set_log_level_simple();
test_set_log_level_null();
test_set_log_level_warning();
test_set_log_level_info();
test_set_log_level_verbose();
test_set_log_level_debug();
test_set_log_level_verbosedebug();
test_set_log_level_bogus();
test_set_log_level_overall();
test_set_log_level_invalid_subsystem();
test_set_log_level_3_subsystems();
}
static void test_vlog_helper(nrloglev_t level,
uint32_t subsystem,
const char* fmt,
...) {
va_list ap;
va_start(ap, fmt);
nrl_vlog(level, subsystem, fmt, ap);
va_end(ap);
}
static void test_vlog(void) {
nr_unlink("vlogtest.tmp");
nrl_set_log_file("./vlogtest.tmp");
nrl_set_log_level("warning");
test_vlog_helper(NRL_ALWAYS, NRL_TEST, "%s", "NRL_ALWAYS");
test_vlog_helper(NRL_ERROR, NRL_TEST, "%s", "NRL_ERROR");
test_vlog_helper(NRL_WARNING, NRL_TEST, "%s", "NRL_WARNING");
test_vlog_helper(NRL_INFO, NRL_TEST, "%s", "NRL_INFO");
test_vlog_helper(NRL_VERBOSE, NRL_TEST, "%s", "NRL_VERBOSE");
test_vlog_helper(NRL_DEBUG, NRL_TEST, "%s", "NRL_DEBUG");
test_vlog_helper(NRL_VERBOSEDEBUG, NRL_TEST, "%s", "NRL_VERBOSEDEBUG");
nrl_close_log_file();
tlib_pass_if_not_diff("vlogtest.tmp", REFERENCE_DIR "/test_vlog.cmp",
cleanup_string, 0, 0);
}
static void test_format_timestamp(const char* msg,
time_t utc_time,
const char* expected_timestamp) {
char buf[64];
struct timeval tv;
tv.tv_sec = utc_time;
tv.tv_usec = 0;
nr_memset(buf, 0, sizeof(buf));
nrl_format_timestamp(buf, sizeof(buf), &tv);
tlib_pass_if_str_equal(msg, expected_timestamp, buf);
}
static void test_timezones(void) {
struct tm tm;
time_t jan1_midnight_utc;
time_t jul1_midnight_utc;
char* saved_tz = getenv("TZ");
if (saved_tz) {
saved_tz = nr_strdup(saved_tz);
}
/*
* The only portable way to create UTC timestamps is to force
* the timezone to UTC, invoke mktime(3), and then restore
* the local timezone.
*/
setenv("TZ", "UTC", 1 /* overwrite */);
tzset();
nr_memset(&tm, 0, sizeof(tm));
tm.tm_year = 2015 - 1900;
tm.tm_mon = 0;
tm.tm_mday = 1;
jan1_midnight_utc = mktime(&tm);
nr_memset(&tm, 0, sizeof(tm));
tm.tm_year = 2015 - 1900;
tm.tm_mon = 6;
tm.tm_mday = 1;
jul1_midnight_utc = mktime(&tm);
/*
* See http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for
* more test cases.
*/
setenv("TZ", "America/Los_Angeles", 1 /* overwrite */);
tzset();
test_format_timestamp("integral timezone with negative offset",
jan1_midnight_utc, "2014-12-31 16:00:00.000 -0800");
test_format_timestamp(
"integral timezone with negative offset during daylight savings",
jul1_midnight_utc, "2015-06-30 17:00:00.000 -0700");
setenv("TZ", "America/St_Johns", 1 /* overwrite */);
tzset();
test_format_timestamp("half hour timezone with negative offset",
jan1_midnight_utc, "2014-12-31 20:30:00.000 -0330");
test_format_timestamp(
"half hour timezone with negative offset during daylight savings",
jul1_midnight_utc, "2015-06-30 21:30:00.000 -0230");
setenv("TZ", "Europe/Dublin", 1 /* overwrite */);
tzset();
test_format_timestamp("integral timezone with a positive offset",
jan1_midnight_utc, "2015-01-01 00:00:00.000 +0000");
test_format_timestamp(
"integral timezone with a positive offset during daylight savings",
jul1_midnight_utc, "2015-07-01 01:00:00.000 +0100");
setenv("TZ", "Pacific/Chatham", 1 /* overwrite */);
tzset();
test_format_timestamp("quarter hour timezone with a positive offset",
jan1_midnight_utc, "2015-01-01 13:45:00.000 +1345");
test_format_timestamp(
"quarter hour timezone with a positive offset during daylight savings",
jul1_midnight_utc, "2015-07-01 12:45:00.000 +1245");
setenv("TZ", "Pacific/Honolulu", 1 /* overwrite */);
tzset();
test_format_timestamp("timezone without daylight savings", jan1_midnight_utc,
"2014-12-31 14:00:00.000 -1000");
test_format_timestamp("timezone without daylight savings", jul1_midnight_utc,
"2015-06-30 14:00:00.000 -1000");
if (saved_tz) {
setenv("TZ", saved_tz, 1 /* overwrite */);
nr_free(saved_tz);
} else {
unsetenv("TZ");
}
}
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};
void test_main(void* p NRUNUSED) {
test_logging();
/*
* We're done with the tests. Last few tests are to compare the two log files
* generated during the tests to ensure that they have the correct contents.
* These are tested after both the main logging tests and the audit log have
* run in order to check that there was no bleed-through from one file to the
* other.
*/
#if HAVE_BACKTRACE
tlib_pass_if_not_diff("logtest1.tmp", REFERENCE_DIR "/test_logging_1.cmp",
cleanup_string, 0, 0);
#else
tlib_pass_if_not_diff("logtest1.tmp",
REFERENCE_DIR "/test_logging_1_no_backtrace.cmp",
cleanup_string, 0, 0);
#endif
tlib_pass_if_not_diff("logtest2.tmp", REFERENCE_DIR "/test_logging_2.cmp",
cleanup_string, 0, 0);
test_vlog();
test_timezones();
}