import: Revamp parse_import_notification()
[nagios-reports-module.git] / test-lparse.c
blob7138c9ac4973801289177ebcec345d55a710673b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <strings.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <unistd.h>
9 #include "logutils.h"
10 #include "lparse.h"
12 static struct lparse_test *lpt;
14 static struct lparse_test {
15 char *path;
16 uint lines, empty;
17 int max_runtime;
18 int byte_delta;
19 } testfiles[] = {
20 { "logs/no-lf-terminator.log", 1, 0, 1, -1 },
21 { "logs/single-read.log", 1, 0, 1, 0 },
22 { "logs/beta.log", 6114, 0, 2, 0 },
23 { "logs/nuls.log", 20002, 8, 2, 0 },
24 { NULL },
27 void sighandler(int signum)
29 if (signum == SIGALRM) {
30 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
31 lpt->path, lpt->max_runtime);
33 exit(1);
36 static int lines, empty;
37 static unsigned long long bytes = 0;
38 static int check_line(char *str, uint len)
40 if (!len)
41 empty++;
42 else
43 lines++;
44 bytes += len + 1;
45 return 0;
48 #define print_expected(a, b, what) \
49 printf("# expected %llu " what ", got %llu. delta %d\n", \
50 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
53 static const char *green, *cyan, *red, *yellow, *reset;
54 static int passed, failed, 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)
64 alarm(0);
66 if (lines == t->lines && t->empty == empty) {
67 if (bytes == st->st_size || st->st_size - bytes == t->byte_delta) {
68 passed++;
69 printf("%sPASS%s %s\n", green, reset, t->path);
70 return;
74 if (lines == t->lines)
75 printf("%spass%s %s\n", yellow, reset, t->path);
76 else {
77 failed++;
78 printf("%sFAIL%s %s\n", red, reset, t->path);
80 if (lines != t->lines)
81 print_expected(t->lines, lines, "lines");
82 if (empty != t->empty)
83 print_expected(t->empty, empty, "empty lines");
84 if (st->st_size != bytes && st->st_size - bytes != t->byte_delta)
85 print_expected(st->st_size, bytes, "bytes");
88 static void test_all(int reverse, const char *msg)
90 int i;
92 printf("%s%s%s\n", cyan, msg, reset);
93 for (i = 0; testfiles[i].path; i++) {
94 struct stat st;
96 lpt = &testfiles[i];
98 if (stat(lpt->path, &st) < 0) {
99 fprintf(stderr, "Failed to stat '%s': %s\n", lpt->path, strerror(errno));
100 exit(1);
103 test_one(reverse, lpt, &st);
107 static struct path_cmp_test {
108 char *path;
109 uint correct;
110 } testpaths[] = {
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) },
117 { "foo.log", 0 },
118 { NULL },
120 static void test_path_cmp(void)
122 int i;
124 printf("%stesting path comparison%s\n", cyan, reset);
125 for (i = 0; testpaths[i].path; i++) {
126 struct path_cmp_test *t;
127 uint cmp;
129 t = &testpaths[i];
130 cmp = path_cmp_number(t->path);
131 if (cmp == t->correct) {
132 printf("%sPASS%s %s parses to %u\n", green, reset, t->path, cmp);
133 passed++;
134 continue;
137 failed++;
138 printf("%sFAIL%s %s should be %u, got %u\n",
139 red, reset, t->path, t->correct, cmp);
143 int main(int argc, char **argv)
145 int i;
147 if (isatty(fileno(stdout))) {
148 green = CLR_GREEN;
149 red = CLR_RED;
150 yellow = CLR_YELLOW;
151 cyan = CLR_CYAN;
152 reset = CLR_RESET;
153 } else {
154 green = red = yellow = cyan = reset = "";
157 signal(SIGALRM, sighandler);
159 for (i = 1; i < argc; i++) {
160 if (!strcmp(argv[i], "--no-alarm"))
161 use_alarm = 0;
164 test_all(0, "testing forward parsing");
165 test_all(1, "testing reverse parsing");
166 test_path_cmp();
168 if (!failed)
169 return 0;
171 return 1;