summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tests/test_utils.c')
-rw-r--r--lib/tests/test_utils.c158
1 files changed, 156 insertions, 2 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 278f526..a5189ce 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -30,7 +30,7 @@ main (int argc, char **argv)
30 thresholds *thresholds = NULL; 30 thresholds *thresholds = NULL;
31 int rc; 31 int rc;
32 32
33 plan_tests(82); 33 plan_tests(172);
34 34
35 range = parse_range_string("6"); 35 range = parse_range_string("6");
36 ok( range != NULL, "'6' is valid range"); 36 ok( range != NULL, "'6' is valid range");
@@ -40,6 +40,32 @@ main (int argc, char **argv)
40 ok( range->end_infinity == FALSE, "Not using infinity"); 40 ok( range->end_infinity == FALSE, "Not using infinity");
41 free(range); 41 free(range);
42 42
43 range = _parse_range_string_v2("6");
44 ok( range == NULL, "Missing colon in range" );
45 ok( utils_errno == NP_RANGE_MISSING_COLON, "Right error code" );
46
47 range = _parse_range_string_v2("6:");
48 ok( range != NULL, "'6:' is valid range");
49 ok( range->start == 6, "Start correct");
50 ok( range->start_infinity == FALSE, "Not using negative infinity");
51 ok( range->end_infinity == TRUE, "Using infinity");
52 free(range);
53
54 range = _parse_range_string_v2("6:6");
55 ok( range != NULL, "'6:6' is valid range");
56 ok( range->start == 6, "Start correct");
57 ok( range->start_infinity == FALSE, "Not using negative infinity");
58 ok( range->end == 6, "End correct");
59 ok( range->end_infinity == FALSE, "Not using infinity");
60 free(range);
61
62 range = _parse_range_string_v2(":6");
63 ok( range != NULL, "':6' is valid range");
64 ok( range->start_infinity == TRUE, "Using negative infinity");
65 ok( range->end == 6, "End correct");
66 ok( range->end_infinity == FALSE, "Not using infinity");
67 free(range);
68
43 range = parse_range_string("1:12%%"); 69 range = parse_range_string("1:12%%");
44 ok( range != NULL, "'1:12%%' is valid - percentages are ignored"); 70 ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
45 ok( range->start == 1, "Start correct"); 71 ok( range->start == 1, "Start correct");
@@ -48,6 +74,14 @@ main (int argc, char **argv)
48 ok( range->end_infinity == FALSE, "Not using infinity"); 74 ok( range->end_infinity == FALSE, "Not using infinity");
49 free(range); 75 free(range);
50 76
77 range = _parse_range_string_v2("1:12%%");
78 ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
79 ok( range->start == 1, "Start correct");
80 ok( range->start_infinity == FALSE, "Not using negative infinity");
81 ok( range->end == 12, "End correct");
82 ok( range->end_infinity == FALSE, "Not using infinity");
83 free(range);
84
51 range = parse_range_string("-7:23"); 85 range = parse_range_string("-7:23");
52 ok( range != NULL, "'-7:23' is valid range"); 86 ok( range != NULL, "'-7:23' is valid range");
53 ok( range->start == -7, "Start correct"); 87 ok( range->start == -7, "Start correct");
@@ -56,6 +90,14 @@ main (int argc, char **argv)
56 ok( range->end_infinity == FALSE, "Not using infinity"); 90 ok( range->end_infinity == FALSE, "Not using infinity");
57 free(range); 91 free(range);
58 92
93 range = _parse_range_string_v2("-7:23");
94 ok( range != NULL, "'-7:23' is valid range");
95 ok( range->start == -7, "Start correct");
96 ok( range->start_infinity == FALSE, "Not using negative infinity");
97 ok( range->end == 23, "End correct");
98 ok( range->end_infinity == FALSE, "Not using infinity");
99 free(range);
100
59 range = parse_range_string(":5.75"); 101 range = parse_range_string(":5.75");
60 ok( range != NULL, "':5.75' is valid range"); 102 ok( range != NULL, "':5.75' is valid range");
61 ok( range->start == 0, "Start correct"); 103 ok( range->start == 0, "Start correct");
@@ -64,6 +106,14 @@ main (int argc, char **argv)
64 ok( range->end_infinity == FALSE, "Not using infinity"); 106 ok( range->end_infinity == FALSE, "Not using infinity");
65 free(range); 107 free(range);
66 108
109 range = _parse_range_string_v2(":5.75");
110 ok( range != NULL, "':5.75' is valid range");
111 ok( range->start == 0, "Start correct");
112 ok( range->start_infinity == TRUE, "Using negative infinity");
113 ok( range->end == 5.75, "End correct");
114 ok( range->end_infinity == FALSE, "Not using infinity");
115 free(range);
116
67 range = parse_range_string("~:-95.99"); 117 range = parse_range_string("~:-95.99");
68 ok( range != NULL, "~:-95.99' is valid range"); 118 ok( range != NULL, "~:-95.99' is valid range");
69 ok( range->start_infinity == TRUE, "Using negative infinity"); 119 ok( range->start_infinity == TRUE, "Using negative infinity");
@@ -71,6 +121,26 @@ main (int argc, char **argv)
71 ok( range->end_infinity == FALSE, "Not using infinity"); 121 ok( range->end_infinity == FALSE, "Not using infinity");
72 free(range); 122 free(range);
73 123
124 range = _parse_range_string_v2("~:-95.99");
125 ok( range == NULL, "~:-95.99' is invalid range");
126 ok( utils_errno == NP_RANGE_UNPARSEABLE, "Correct error code" );
127
128 /*
129 * This is currently parseable. This is because ~ is interpreted as a 0
130 * and then 95.99 is the end, so we get 0:95.99. Should validate the characters before
131 * passing to strtod
132 range = _parse_range_string_v2("~:95.99");
133 ok( range == NULL, "~:95.99' is invalid range");
134 ok( utils_errno == NP_RANGE_UNPARSEABLE, "Correct error code" );
135 */
136
137 range = _parse_range_string_v2(":-95.99");
138 ok( range != NULL, ":-95.99' is valid range");
139 ok( range->start_infinity == TRUE, "Using negative infinity");
140 ok( range->end == -95.99, "End correct (with rounding errors)");
141 ok( range->end_infinity == FALSE, "Not using infinity");
142 free(range);
143
74 range = parse_range_string("12345678901234567890:"); 144 range = parse_range_string("12345678901234567890:");
75 temp = atof("12345678901234567890"); /* Can't just use this because number too large */ 145 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
76 ok( range != NULL, "'12345678901234567890:' is valid range"); 146 ok( range != NULL, "'12345678901234567890:' is valid range");
@@ -83,6 +153,14 @@ main (int argc, char **argv)
83 ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert"); 153 ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert");
84 free(range); 154 free(range);
85 155
156 range = _parse_range_string_v2("12345678901234567890:");
157 temp = atof("12345678901234567890");
158 ok( range != NULL, "'12345678901234567890:' is valid range");
159 ok( range->start == temp, "Start correct");
160 ok( range->start_infinity == FALSE, "Not using negative infinity");
161 ok( range->end_infinity == TRUE, "Using infinity");
162 free(range);
163
86 range = parse_range_string("~:0"); 164 range = parse_range_string("~:0");
87 ok( range != NULL, "'~:0' is valid range"); 165 ok( range != NULL, "'~:0' is valid range");
88 ok( range->start_infinity == TRUE, "Using negative infinity"); 166 ok( range->start_infinity == TRUE, "Using negative infinity");
@@ -94,8 +172,16 @@ main (int argc, char **argv)
94 ok( check_range(0, range) == FALSE, "0 - no alert"); 172 ok( check_range(0, range) == FALSE, "0 - no alert");
95 free(range); 173 free(range);
96 174
175 range = _parse_range_string_v2("-4.33:-4.33");
176 ok( range != NULL, "'-4.33:-4.33' is valid range");
177 ok( range->start_infinity == FALSE, "Not using negative infinity");
178 ok( range->start == -4.33, "Start right");
179 ok( range->end == -4.33, "End correct");
180 ok( range->end_infinity == FALSE, "Not using infinity");
181 free(range);
182
97 range = parse_range_string("@0:657.8210567"); 183 range = parse_range_string("@0:657.8210567");
98 ok( range != 0, "@0:657.8210567' is a valid range"); 184 ok( range != NULL, "@0:657.8210567' is a valid range");
99 ok( range->start == 0, "Start correct"); 185 ok( range->start == 0, "Start correct");
100 ok( range->start_infinity == FALSE, "Not using negative infinity"); 186 ok( range->start_infinity == FALSE, "Not using negative infinity");
101 ok( range->end == 657.8210567, "End correct"); 187 ok( range->end == 657.8210567, "End correct");
@@ -107,6 +193,14 @@ main (int argc, char **argv)
107 ok( check_range(0, range) == TRUE, "0 - alert"); 193 ok( check_range(0, range) == TRUE, "0 - alert");
108 free(range); 194 free(range);
109 195
196 range = parse_range_string("^0:657.8210567");
197 ok( range != NULL, "^0:657.8210567' is a valid range");
198 ok( range->start == 0, "Start correct");
199 ok( range->start_infinity == FALSE, "Not using negative infinity");
200 ok( range->end == 657.8210567, "End correct");
201 ok( range->end_infinity == FALSE, "Not using infinity");
202 free(range);
203
110 range = parse_range_string("1:1"); 204 range = parse_range_string("1:1");
111 ok( range != NULL, "'1:1' is a valid range"); 205 ok( range != NULL, "'1:1' is a valid range");
112 ok( range->start == 1, "Start correct"); 206 ok( range->start == 1, "Start correct");
@@ -121,22 +215,71 @@ main (int argc, char **argv)
121 range = parse_range_string("2:1"); 215 range = parse_range_string("2:1");
122 ok( range == NULL, "'2:1' rejected"); 216 ok( range == NULL, "'2:1' rejected");
123 217
218 range = _parse_range_string_v2("2:1");
219 ok( range == NULL, "'2:1' rejected");
220 ok( utils_errno == NP_RANGE_UNPARSEABLE, "Errno correct" );
221
124 rc = _set_thresholds(&thresholds, NULL, NULL); 222 rc = _set_thresholds(&thresholds, NULL, NULL);
125 ok( rc == 0, "Thresholds (NULL, NULL) set"); 223 ok( rc == 0, "Thresholds (NULL, NULL) set");
126 ok( thresholds->warning == NULL, "Warning not set"); 224 ok( thresholds->warning == NULL, "Warning not set");
127 ok( thresholds->critical == NULL, "Critical not set"); 225 ok( thresholds->critical == NULL, "Critical not set");
128 226
227 thresholds = _parse_thresholds_string(NULL);
228 ok( thresholds != NULL, "Threshold still set, even though NULL");
229 ok( thresholds->warning == NULL, "Warning set to NULL");
230 ok( thresholds->critical == NULL, "Critical set to NULL");
231
232 thresholds = _parse_thresholds_string("");
233 ok( thresholds != NULL, "Threshold still set, even though ''");
234 ok( thresholds->warning == NULL, "Warning set to NULL");
235 ok( thresholds->critical == NULL, "Critical set to NULL");
236
237 thresholds = _parse_thresholds_string("/");
238 ok( thresholds != NULL, "Threshold still set, even though '/'");
239 ok( thresholds->warning == NULL, "Warning set to NULL");
240 ok( thresholds->critical == NULL, "Critical set to NULL");
241
129 rc = _set_thresholds(&thresholds, NULL, "80"); 242 rc = _set_thresholds(&thresholds, NULL, "80");
130 ok( rc == 0, "Thresholds (NULL, '80') set"); 243 ok( rc == 0, "Thresholds (NULL, '80') set");
131 ok( thresholds->warning == NULL, "Warning not set"); 244 ok( thresholds->warning == NULL, "Warning not set");
132 ok( thresholds->critical->end == 80, "Critical set correctly"); 245 ok( thresholds->critical->end == 80, "Critical set correctly");
133 246
247 thresholds = _parse_thresholds_string(":80/");
248 ok( thresholds != NULL, "Threshold set for ':80/'");
249 ok( thresholds->warning == NULL, "Warning set to NULL");
250 ok( thresholds->critical->start_infinity == TRUE, "Start is right" );
251 ok( thresholds->critical->end == 80, "Critical set to 80");
252
253 thresholds = _parse_thresholds_string(":80");
254 ok( thresholds != NULL, "Threshold set for ':80'");
255 ok( thresholds->warning == NULL, "Warning set to NULL");
256 ok( thresholds->critical->start_infinity == TRUE, "Start is right" );
257 ok( thresholds->critical->end == 80, "Critical set to 80");
258
259 thresholds = _parse_thresholds_string("80");
260 ok( thresholds == NULL, "Threshold not set because of single value '80'");
261 ok( utils_errno == NP_RANGE_MISSING_COLON, "Correct error message");
262
134 rc = _set_thresholds(&thresholds, "5:33", NULL); 263 rc = _set_thresholds(&thresholds, "5:33", NULL);
135 ok( rc == 0, "Thresholds ('5:33', NULL) set"); 264 ok( rc == 0, "Thresholds ('5:33', NULL) set");
136 ok( thresholds->warning->start == 5, "Warning start set"); 265 ok( thresholds->warning->start == 5, "Warning start set");
137 ok( thresholds->warning->end == 33, "Warning end set"); 266 ok( thresholds->warning->end == 33, "Warning end set");
138 ok( thresholds->critical == NULL, "Critical not set"); 267 ok( thresholds->critical == NULL, "Critical not set");
139 268
269 thresholds = _parse_thresholds_string("5:33");
270 ok( thresholds != NULL, "Threshold set for '5:33'");
271 ok( thresholds->warning == NULL, "Warning set to NULL");
272 ok( thresholds->critical->start_infinity == FALSE, "Start is right" );
273 ok( thresholds->critical->start == 5, "Critical set to 5");
274 ok( thresholds->critical->end == 33, "Critical set to 33");
275
276 thresholds = _parse_thresholds_string("/5:33");
277 ok( thresholds != NULL, "Threshold set for '/5:33'");
278 ok( thresholds->critical == NULL, "Critical set to NULL");
279 ok( thresholds->warning->start_infinity == FALSE, "Start is right" );
280 ok( thresholds->warning->start == 5, "Warning start set to 5");
281 ok( thresholds->warning->end == 33, "Warning end set to 33");
282
140 rc = _set_thresholds(&thresholds, "30", "60"); 283 rc = _set_thresholds(&thresholds, "30", "60");
141 ok( rc == 0, "Thresholds ('30', '60') set"); 284 ok( rc == 0, "Thresholds ('30', '60') set");
142 ok( thresholds->warning->end == 30, "Warning set correctly"); 285 ok( thresholds->warning->end == 30, "Warning set correctly");
@@ -145,6 +288,17 @@ main (int argc, char **argv)
145 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 288 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
146 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 289 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
147 290
291 thresholds = _parse_thresholds_string("-6.7:29 / 235.4:3333.33");
292 ok( thresholds != NULL, "Threshold set for '-6.7:29 / 235.4:3333.33'");
293 ok( thresholds->critical->start_infinity == FALSE, "Critical not starting at infinity");
294 ok( thresholds->critical->start == -6.7, "Critical start right" );
295 ok( thresholds->critical->end_infinity == FALSE, "Critical not ending at infinity");
296 ok( thresholds->critical->end == 29, "Critical end right" );
297 ok( thresholds->warning->start_infinity == FALSE, "Start is right" );
298 ok( thresholds->warning->start == 235.4, "Warning set to 5");
299 ok( thresholds->warning->end_infinity == FALSE, "End is right" );
300 ok( thresholds->warning->end == 3333.33, "Warning set to 33");
301
148 char *test; 302 char *test;
149 test = np_escaped_string("bob\\n"); 303 test = np_escaped_string("bob\\n");
150 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 304 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");