showlog: Don't show INITIAL/CURRENT state for uninteresting objects
[nagios-reports-module.git] / test-lparse.c
blob9e6f312569935958f80de1f9acde1ad21f253c4b
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 } testfiles[] = {
19 { "logs/no-lf-terminator.log", 1, 0, 1 },
20 { "logs/single-read.log", 1, 0, 1 },
21 { "logs/beta.log", 6114, 0, 2 },
22 { "logs/nuls.log", 20002, 8, 2 },
23 { NULL },
26 void sighandler(int signum)
28 if (signum == SIGALRM) {
29 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
30 lpt->path, lpt->max_runtime);
32 exit(1);
35 static int lines, empty;
36 static unsigned long long bytes = 0;
37 static int check_line(char *str, uint len)
39 if (!len)
40 empty++;
41 else
42 lines++;
43 bytes += len + 1;
44 return 0;
47 #define print_expected(a, b, what) \
48 printf("# expected %llu " what ", got %llu. delta %d\n", \
49 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
52 static const char *green, *cyan, *red, *yellow, *reset;
53 static int passed, failed, use_alarm = 1;
55 static void test_one(int rev, struct lparse_test *t, struct stat *st)
57 if (use_alarm && t->max_runtime)
58 alarm(t->max_runtime);
59 lparse_path_real(rev, t->path, st->st_size, check_line);
60 if (use_alarm && t->max_runtime)
61 alarm(0);
63 if (lines == t->lines && t->empty == empty && bytes == st->st_size) {
64 passed++;
65 printf("%sPASS%s %s\n", green, reset, t->path);
66 } else {
67 if (lines == t->lines)
68 printf("%spass%s %s\n", yellow, reset, t->path);
69 else {
70 failed++;
71 printf("%sFAIL%s %s\n", red, reset, t->path);
73 if (lines != t->lines)
74 print_expected(t->lines, lines, "lines");
75 if (empty != t->empty)
76 print_expected(t->empty, empty, "empty lines");
77 if (st->st_size != bytes)
78 print_expected(st->st_size, bytes, "bytes");
81 lines = bytes = empty = 0;
84 static void test_all(int reverse, const char *msg)
86 int i;
88 printf("%s%s%s\n", cyan, msg, reset);
89 for (i = 0; testfiles[i].path; i++) {
90 struct stat st;
92 lpt = &testfiles[i];
94 if (stat(lpt->path, &st) < 0) {
95 fprintf(stderr, "Failed to stat '%s': %s\n", lpt->path, strerror(errno));
96 exit(1);
99 test_one(reverse, lpt, &st);
103 static struct path_cmp_test {
104 char *path;
105 uint correct;
106 } testpaths[] = {
107 { "nagios-12-01-2002-00.log", 2002120100 },
108 { "nagios-11-30-2006-01.log", 2006113001 },
109 { "nagios-08-01-2009-00.log", 2009080100 },
110 { "nagios-12-30-2147-99.log", 2147123099 },
111 /* nagios.log is a special case, as is all logfiles without a dash */
112 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
113 { "foo.log", 0 },
114 { NULL },
116 static void test_path_cmp(void)
118 int i;
120 printf("%stesting path comparison%s\n", cyan, reset);
121 for (i = 0; testpaths[i].path; i++) {
122 struct path_cmp_test *t;
123 uint cmp;
125 t = &testpaths[i];
126 cmp = path_cmp_number(t->path);
127 if (cmp == t->correct) {
128 printf("%sPASS%s %s parses to %u\n", green, reset, t->path, cmp);
129 passed++;
130 continue;
133 failed++;
134 printf("%sFAIL%s %s should be %u, got %u\n",
135 red, reset, t->path, t->correct, cmp);
139 int main(int argc, char **argv)
141 int i;
143 if (isatty(fileno(stdout))) {
144 green = CLR_GREEN;
145 red = CLR_RED;
146 yellow = CLR_YELLOW;
147 cyan = CLR_CYAN;
148 reset = CLR_RESET;
149 } else {
150 green = red = yellow = cyan = reset = "";
153 signal(SIGALRM, sighandler);
155 for (i = 1; i < argc; i++) {
156 if (!strcmp(argv[i], "--no-alarm"))
157 use_alarm = 0;
160 test_all(0, "testing forward parsing");
161 test_all(1, "testing reverse parsing");
162 test_path_cmp();
164 if (!failed)
165 return 0;
167 return 1;