1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <assert.h>
5 #include "prelude.h"
6
test_criteria(idmef_message_t * idmef,const char * criteria_str,int expect_create,int expect_match)7 static void test_criteria(idmef_message_t *idmef, const char *criteria_str, int expect_create, int expect_match)
8 {
9 idmef_criteria_t *criteria;
10
11 if ( expect_create < 0 ) {
12 assert(idmef_criteria_new_from_string(&criteria, criteria_str) < 0);
13 return;
14 } else
15 assert(idmef_criteria_new_from_string(&criteria, criteria_str) == 0);
16
17 assert(idmef_criteria_match(criteria, idmef) == expect_match);
18 idmef_criteria_destroy(criteria);
19
20 }
21
main(void)22 int main(void)
23 {
24 idmef_time_t *ctime;
25 idmef_alert_t *alert;
26 idmef_message_t *idmef;
27 idmef_classification_t *classification;
28 prelude_string_t *str;
29
30 assert(prelude_string_new_ref(&str, "A") == 0);
31
32 assert(idmef_message_new(&idmef) == 0);
33 assert(idmef_message_new_alert(idmef, &alert) == 0);
34 assert(idmef_alert_new_classification(alert, &classification) == 0);
35 idmef_classification_set_text(classification, str);
36
37 test_criteria(idmef, "alert", 0, 1);
38 test_criteria(idmef, "heartbeat", 0, 0);
39 test_criteria(idmef, "alert || heartbeat", 0, 1);
40 test_criteria(idmef, "alert.classification.txt == A", -1, -1);
41 test_criteria(idmef, "alert.classification.text = (A || B || C || D) || heartbeat", 0, 1);
42 test_criteria(idmef, "(alert.classification.text == A || heartbeat", -1, -1);
43
44 prelude_string_set_ref(str, "My String");
45
46 test_criteria(idmef, "alert.classification.text != 'My String'", 0, 0);
47 test_criteria(idmef, "alert.classification.text != 'random'", 0, 1);
48
49 test_criteria(idmef, "alert.classification.text == 'My String'", 0, 1);
50 test_criteria(idmef, "alert.classification.text <> 'My'", 0, 1);
51 test_criteria(idmef, "alert.classification.text <> 'my'", 0, 0);
52 test_criteria(idmef, "alert.classification.text <>* 'my'", 0, 1);
53
54 test_criteria(idmef, "alert.classification.text ~ 'My String'", 0, 1);
55 test_criteria(idmef, "alert.classification.text ~ 'My (String|Check)'", 0, 1);
56 test_criteria(idmef, "alert.classification.text ~ 'my'", 0, 0);
57 test_criteria(idmef, "alert.classification.text ~* 'my'", 0, 1);
58
59 idmef_alert_new_create_time(alert, &ctime);
60 assert(idmef_time_set_from_string(ctime, "2015-05-03 1:59:08") == 0);
61
62 /*
63 * Regular time operator check
64 */
65 test_criteria(idmef, "alert.create_time == '2015-05-03 1:59:08'", 0, 1);
66 test_criteria(idmef, "alert.create_time != '2015-05-03 1:59:08'", 0, 0);
67 test_criteria(idmef, "alert.create_time < '2015-05-03 1:59:08'", 0, 0);
68 test_criteria(idmef, "alert.create_time > '2015-05-03 1:59:08'", 0, 0);
69 test_criteria(idmef, "alert.create_time <= '2015-05-03 1:59:08'", 0, 1);
70 test_criteria(idmef, "alert.create_time >= '2015-05-03 1:59:08'", 0, 1);
71
72 test_criteria(idmef, "alert.create_time == '2015-05-03 1:59:07'", 0, 0);
73 test_criteria(idmef, "alert.create_time != '2015-05-03 1:59:07'", 0, 1);
74 test_criteria(idmef, "alert.create_time < '2015-05-03 1:59:07'", 0, 0);
75 test_criteria(idmef, "alert.create_time > '2015-05-03 1:59:07'", 0, 1);
76 test_criteria(idmef, "alert.create_time <= '2015-05-03 1:59:07'", 0, 0);
77 test_criteria(idmef, "alert.create_time >= '2015-05-03 1:59:07'", 0, 1);
78
79 test_criteria(idmef, "alert.create_time < '2015-05-03 1:59:09'", 0, 1);
80 test_criteria(idmef, "alert.create_time > '2015-05-03 1:59:09'", 0, 0);
81 test_criteria(idmef, "alert.create_time <= '2015-05-03 1:59:09'", 0, 1);
82 test_criteria(idmef, "alert.create_time >= '2015-05-03 1:59:09'", 0, 0);
83
84 /*
85 * Broken down time check
86 */
87 assert(idmef_time_set_from_string(ctime, "2015-05-04 00:00:00+00:00") == 0);
88 test_criteria(idmef, "alert.create_time == 'month:may mday:3'", 0, 0);
89 test_criteria(idmef, "alert.create_time != 'month:may mday:3'", 0, 1);
90 test_criteria(idmef, "alert.create_time < 'month:may mday:3'", 0, 0);
91 test_criteria(idmef, "alert.create_time > 'month:may mday:3'", 0, 1);
92 test_criteria(idmef, "alert.create_time <= 'month:may mday:3'", 0, 0);
93 test_criteria(idmef, "alert.create_time >= 'month:may mday:3'", 0, 1);
94
95 test_criteria(idmef, "alert.create_time == 'month:may mday:4'", 0, 1);
96 test_criteria(idmef, "alert.create_time != 'month:may mday:4'", 0, 0);
97 test_criteria(idmef, "alert.create_time < 'month:may mday:4'", 0, 0);
98 test_criteria(idmef, "alert.create_time > 'month:may mday:4'", 0, 0);
99 test_criteria(idmef, "alert.create_time <= 'month:may mday:4'", 0, 1);
100 test_criteria(idmef, "alert.create_time >= 'month:may mday:4'", 0, 1);
101
102 test_criteria(idmef, "alert.create_time == 'month:may mday:5'", 0, 0);
103 test_criteria(idmef, "alert.create_time != 'month:may mday:5'", 0, 1);
104 test_criteria(idmef, "alert.create_time < 'month:may mday:5'", 0, 1);
105 test_criteria(idmef, "alert.create_time > 'month:may mday:5'", 0, 0);
106 test_criteria(idmef, "alert.create_time <= 'month:may mday:5'", 0, 1);
107 test_criteria(idmef, "alert.create_time >= 'month:may mday:5'", 0, 0);
108
109 /*
110 * Broken down time special wday/yday fields
111 */
112 test_criteria(idmef, "alert.create_time == 'wday:monday'", 0, 1);
113 test_criteria(idmef, "alert.create_time != 'wday:monday'", 0, 0);
114 test_criteria(idmef, "alert.create_time == 'wday:tuesday'", 0, 0);
115 test_criteria(idmef, "alert.create_time != 'wday:tuesday'", 0, 1);
116
117 test_criteria(idmef, "alert.create_time == 'wday:monday mday:3'", 0, 0);
118 test_criteria(idmef, "alert.create_time != 'wday:monday mday:3'", 0, 1);
119 test_criteria(idmef, "alert.create_time < 'wday:monday mday:3'", 0, 0);
120 test_criteria(idmef, "alert.create_time > 'wday:monday mday:3'", 0, 1);
121 test_criteria(idmef, "alert.create_time <= 'wday:monday mday:3'", 0, 0);
122 test_criteria(idmef, "alert.create_time >= 'wday:monday mday:3'", 0, 1);
123
124 test_criteria(idmef, "alert.create_time == 'wday:monday mday:4'", 0, 1);
125 test_criteria(idmef, "alert.create_time != 'wday:monday mday:4'", 0, 0);
126 test_criteria(idmef, "alert.create_time < 'wday:monday mday:4'", 0, 0);
127 test_criteria(idmef, "alert.create_time > 'wday:monday mday:4'", 0, 0);
128 test_criteria(idmef, "alert.create_time <= 'wday:monday mday:4'", 0, 1);
129 test_criteria(idmef, "alert.create_time >= 'wday:monday mday:4'", 0, 1);
130
131 test_criteria(idmef, "alert.create_time == 'wday:monday mday:5'", 0, 0);
132 test_criteria(idmef, "alert.create_time != 'wday:monday mday:5'", 0, 1);
133 test_criteria(idmef, "alert.create_time < 'wday:monday mday:5'", 0, 1);
134 test_criteria(idmef, "alert.create_time > 'wday:monday mday:5'", 0, 0);
135 test_criteria(idmef, "alert.create_time <= 'wday:monday mday:5'", 0, 1);
136 test_criteria(idmef, "alert.create_time >= 'wday:monday mday:5'", 0, 0);
137
138 exit(0);
139 }
140