12 #define CLR_RESET "\033[m"
13 #define CLR_BOLD "\033[1m"
14 #define CLR_RED "\033[31m"
15 #define CLR_GREEN "\033[32m"
16 #define CLR_BROWN "\033[33m"
17 #define CLR_YELLOW "\033[33m\033[1m"
18 #define CLR_BLUE "\033[34m"
19 #define CLR_MAGENTA "\033[35m"
20 #define CLR_CYAN "\033[36m"
21 #define CLR_BG_RED "\033[41m"
23 static struct lparse_test
*lpt
;
25 static struct lparse_test
{
30 { "logs/no-lf-terminator.log", 1, 0, 1 },
31 { "logs/single-read.log", 1, 0, 1 },
32 { "logs/beta.log", 6114, 0, 2 },
33 { "logs/nuls.log", 20002, 8, 2 },
37 void sighandler(int signum
)
39 if (signum
== SIGALRM
) {
40 fprintf(stderr
, "Failed to complete parsing %s in under %d seconds\n",
41 lpt
->path
, lpt
->max_runtime
);
46 static int lines
, empty
;
47 static unsigned long long bytes
= 0;
48 static int check_line(char *str
, uint len
)
58 #define print_expected(a, b, what) \
59 printf("# expected %llu " what ", got %llu. delta %d\n", \
60 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
63 static const char *green
, *cyan
, *red
, *yellow
, *reset
;
64 static int passed
, failed
, use_alarm
= 1;
66 static void test_one(int rev
, struct lparse_test
*t
, struct stat
*st
)
68 if (use_alarm
&& t
->max_runtime
)
69 alarm(t
->max_runtime
);
70 lparse_path_real(rev
, t
->path
, st
->st_size
, check_line
);
71 if (use_alarm
&& t
->max_runtime
)
74 if (lines
== t
->lines
&& t
->empty
== empty
&& bytes
== st
->st_size
) {
76 printf("%sPASS%s %s\n", green
, reset
, t
->path
);
78 if (lines
== t
->lines
)
79 printf("%spass%s %s\n", yellow
, reset
, t
->path
);
82 printf("%sFAIL%s %s\n", red
, reset
, t
->path
);
84 if (lines
!= t
->lines
)
85 print_expected(t
->lines
, lines
, "lines");
86 if (empty
!= t
->empty
)
87 print_expected(t
->empty
, empty
, "empty lines");
88 if (st
->st_size
!= bytes
)
89 print_expected(st
->st_size
, bytes
, "bytes");
92 lines
= bytes
= empty
= 0;
95 static void test_all(int reverse
, const char *msg
)
99 printf("%s%s%s\n", cyan
, msg
, reset
);
100 for (i
= 0; testfiles
[i
].path
; i
++) {
105 if (stat(lpt
->path
, &st
) < 0) {
106 fprintf(stderr
, "Failed to stat '%s': %s\n", lpt
->path
, strerror(errno
));
110 test_one(reverse
, lpt
, &st
);
114 static struct path_cmp_test
{
118 { "nagios-12-01-2002-00.log", 2002120100 },
119 { "nagios-11-30-2006-01.log", 2006113001 },
120 { "nagios-08-01-2009-00.log", 2009080100 },
121 { "nagios-12-30-2147-99.log", 2147123099 },
122 /* nagios.log is a special case */
123 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
126 static void test_path_cmp(void)
130 printf("%stesting path comparison%s\n", cyan
, reset
);
131 for (i
= 0; testpaths
[i
].path
; i
++) {
132 struct path_cmp_test
*t
;
136 cmp
= path_cmp_number(t
->path
);
137 if (cmp
== t
->correct
) {
138 printf("%sPASS%s %s parses to %u\n", green
, reset
, t
->path
, cmp
);
144 printf("%sFAIL%s %s should be %u, got %u\n",
145 red
, reset
, t
->path
, t
->correct
, cmp
);
149 int main(int argc
, char **argv
)
153 if (isatty(fileno(stdout
))) {
160 green
= red
= yellow
= cyan
= reset
= "";
163 signal(SIGALRM
, sighandler
);
165 for (i
= 1; i
< argc
; i
++) {
166 if (!strcmp(argv
[i
], "--no-alarm"))
170 test_all(0, "testing forward parsing");
171 test_all(1, "testing reverse parsing");