11 #include "test_utils.h"
13 static struct lparse_test
*lpt
;
15 static struct lparse_test
{
21 { "logs/no-lf-terminator.log", 1, 0, 1, -1 },
22 { "logs/single-read.log", 1, 0, 1, 0 },
23 { "logs/beta.log", 6114, 0, 2, 0 },
24 { "logs/nuls.log", 20002, 8, 2, 0 },
28 void sighandler(int signum
)
30 if (signum
== SIGALRM
) {
31 fprintf(stderr
, "Failed to complete parsing %s in under %d seconds\n",
32 lpt
->path
, lpt
->max_runtime
);
37 static int lines
, empty
;
38 static unsigned long long bytes
= 0;
39 static int check_line(char *str
, uint len
)
49 #define print_expected(a, b, what) \
50 printf("# expected %llu " what ", got %llu. delta %d\n", \
51 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
54 static int use_alarm
= 1;
56 static void test_one(int rev
, struct lparse_test
*t
, struct stat
*st
)
58 lines
= bytes
= empty
= 0;
60 if (use_alarm
&& t
->max_runtime
)
61 alarm(t
->max_runtime
);
62 lparse_path_real(rev
, t
->path
, st
->st_size
, check_line
);
63 if (use_alarm
&& t
->max_runtime
)
66 if (lines
== t
->lines
&& t
->empty
== empty
) {
67 if (bytes
== st
->st_size
|| st
->st_size
- bytes
== t
->byte_delta
) {
68 t_pass("%s", t
->path
);
73 if (lines
== t
->lines
)
74 printf("%spass%s %s\n", yellow
, reset
, t
->path
);
77 printf("%sFAIL%s %s\n", red
, reset
, t
->path
);
79 if (lines
!= t
->lines
)
80 print_expected(t
->lines
, lines
, "lines");
81 if (empty
!= t
->empty
)
82 print_expected(t
->empty
, empty
, "empty lines");
83 if (st
->st_size
!= bytes
&& st
->st_size
- bytes
!= t
->byte_delta
)
84 print_expected(st
->st_size
, bytes
, "bytes");
87 static void test_all(int reverse
, const char *msg
)
92 for (i
= 0; testfiles
[i
].path
; i
++) {
97 if (stat(lpt
->path
, &st
) < 0) {
98 fprintf(stderr
, "Failed to stat '%s': %s\n", lpt
->path
, strerror(errno
));
102 test_one(reverse
, lpt
, &st
);
107 static struct path_cmp_test
{
111 { "nagios-12-01-2002-00.log", 2002120100 },
112 { "nagios-11-30-2006-01.log", 2006113001 },
113 { "nagios-08-01-2009-00.log", 2009080100 },
114 { "nagios-12-30-2147-99.log", 2147123099 },
115 /* nagios.log is a special case, as is all logfiles without a dash */
116 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
120 static void test_path_cmp(void)
124 t_start("testing path comparison");
125 for (i
= 0; testpaths
[i
].path
; i
++) {
126 struct path_cmp_test
*t
;
130 cmp
= path_cmp_number(t
->path
);
131 if (cmp
== t
->correct
) {
132 t_pass("%s parses to %u", t
->path
, cmp
);
134 t_fail("%s should be %u, got %u", t
->path
, t
->correct
, cmp
);
140 int main(int argc
, char **argv
)
147 signal(SIGALRM
, sighandler
);
149 for (i
= 1; i
< argc
; i
++) {
150 if (!strcmp(argv
[i
], "--no-alarm"))
154 t_start("testing logfile parsing and sorting");
155 test_all(0, "testing forward parsing");
156 test_all(1, "testing reverse parsing");