lib: clang-format
[monitoring-plugins.git] / lib / tests / test_utils.c
blobc3150f00dcfee74a5d32be4f44991008f1ad7898
1 /*****************************************************************************
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *****************************************************************************/
19 #include "common.h"
20 #include "utils_base.h"
22 #include "tap.h"
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
28 #include "utils_base.c"
30 int main(int argc, char **argv) {
31 char state_path[1024];
32 range *range;
33 double temp;
34 thresholds *thresholds = NULL;
35 int i, rc;
36 char *temp_string;
37 state_key *temp_state_key = NULL;
38 state_data *temp_state_data;
39 time_t current_time;
41 plan_tests(185);
43 ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised");
45 np_init("check_test", argc, argv);
47 ok(this_monitoring_plugin != NULL, "monitoring_plugin now initialised");
48 ok(!strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised");
50 ok(this_monitoring_plugin->argc == argc, "Argc set");
51 ok(this_monitoring_plugin->argv == argv, "Argv set");
53 np_set_args(0, 0);
55 ok(this_monitoring_plugin->argc == 0, "argc changed");
56 ok(this_monitoring_plugin->argv == 0, "argv changed");
58 np_set_args(argc, argv);
60 range = parse_range_string("6");
61 ok(range != NULL, "'6' is valid range");
62 ok(range->start == 0, "Start correct");
63 ok(range->start_infinity == false, "Not using negative infinity");
64 ok(range->end == 6, "End correct");
65 ok(range->end_infinity == false, "Not using infinity");
66 free(range);
68 range = parse_range_string("1:12%%");
69 ok(range != NULL, "'1:12%%' is valid - percentages are ignored");
70 ok(range->start == 1, "Start correct");
71 ok(range->start_infinity == false, "Not using negative infinity");
72 ok(range->end == 12, "End correct");
73 ok(range->end_infinity == false, "Not using infinity");
74 free(range);
76 range = parse_range_string("-7:23");
77 ok(range != NULL, "'-7:23' is valid range");
78 ok(range->start == -7, "Start correct");
79 ok(range->start_infinity == false, "Not using negative infinity");
80 ok(range->end == 23, "End correct");
81 ok(range->end_infinity == false, "Not using infinity");
82 free(range);
84 range = parse_range_string(":5.75");
85 ok(range != NULL, "':5.75' is valid range");
86 ok(range->start == 0, "Start correct");
87 ok(range->start_infinity == false, "Not using negative infinity");
88 ok(range->end == 5.75, "End correct");
89 ok(range->end_infinity == false, "Not using infinity");
90 free(range);
92 range = parse_range_string("~:-95.99");
93 ok(range != NULL, "~:-95.99' is valid range");
94 ok(range->start_infinity == true, "Using negative infinity");
95 ok(range->end == -95.99, "End correct (with rounding errors)");
96 ok(range->end_infinity == false, "Not using infinity");
97 free(range);
99 range = parse_range_string("12345678901234567890:");
100 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
101 ok(range != NULL, "'12345678901234567890:' is valid range");
102 ok(range->start == temp, "Start correct");
103 ok(range->start_infinity == false, "Not using negative infinity");
104 ok(range->end_infinity == true, "Using infinity");
105 /* Cannot do a "-1" on temp, as it appears to be same value */
106 ok(check_range(temp / 1.1, range) == true, "12345678901234567890/1.1 - alert");
107 ok(check_range(temp, range) == false, "12345678901234567890 - no alert");
108 ok(check_range(temp * 2, range) == false, "12345678901234567890*2 - no alert");
109 free(range);
111 range = parse_range_string("~:0");
112 ok(range != NULL, "'~:0' is valid range");
113 ok(range->start_infinity == true, "Using negative infinity");
114 ok(range->end == 0, "End correct");
115 ok(range->end_infinity == false, "Not using infinity");
116 ok(range->alert_on == OUTSIDE, "Will alert on outside of this range");
117 ok(check_range(0.5, range) == true, "0.5 - alert");
118 ok(check_range(-10, range) == false, "-10 - no alert");
119 ok(check_range(0, range) == false, "0 - no alert");
120 free(range);
122 range = parse_range_string("@0:657.8210567");
123 ok(range != 0, "@0:657.8210567' is a valid range");
124 ok(range->start == 0, "Start correct");
125 ok(range->start_infinity == false, "Not using negative infinity");
126 ok(range->end == 657.8210567, "End correct");
127 ok(range->end_infinity == false, "Not using infinity");
128 ok(range->alert_on == INSIDE, "Will alert on inside of this range");
129 ok(check_range(32.88, range) == true, "32.88 - alert");
130 ok(check_range(-2, range) == false, "-2 - no alert");
131 ok(check_range(657.8210567, range) == true, "657.8210567 - alert");
132 ok(check_range(0, range) == true, "0 - alert");
133 free(range);
135 range = parse_range_string("@1:1");
136 ok(range != NULL, "'@1:1' is a valid range");
137 ok(range->start == 1, "Start correct");
138 ok(range->start_infinity == false, "Not using negative infinity");
139 ok(range->end == 1, "End correct");
140 ok(range->end_infinity == false, "Not using infinity");
141 ok(range->alert_on == INSIDE, "Will alert on inside of this range");
142 ok(check_range(0.5, range) == false, "0.5 - no alert");
143 ok(check_range(1, range) == true, "1 - alert");
144 ok(check_range(5.2, range) == false, "5.2 - no alert");
145 free(range);
147 range = parse_range_string("1:1");
148 ok(range != NULL, "'1:1' is a valid range");
149 ok(range->start == 1, "Start correct");
150 ok(range->start_infinity == false, "Not using negative infinity");
151 ok(range->end == 1, "End correct");
152 ok(range->end_infinity == false, "Not using infinity");
153 ok(check_range(0.5, range) == true, "0.5 - alert");
154 ok(check_range(1, range) == false, "1 - no alert");
155 ok(check_range(5.2, range) == true, "5.2 - alert");
156 free(range);
158 range = parse_range_string("2:1");
159 ok(range == NULL, "'2:1' rejected");
161 rc = _set_thresholds(&thresholds, NULL, NULL);
162 ok(rc == 0, "Thresholds (NULL, NULL) set");
163 ok(thresholds->warning == NULL, "Warning not set");
164 ok(thresholds->critical == NULL, "Critical not set");
166 rc = _set_thresholds(&thresholds, NULL, "80");
167 ok(rc == 0, "Thresholds (NULL, '80') set");
168 ok(thresholds->warning == NULL, "Warning not set");
169 ok(thresholds->critical->end == 80, "Critical set correctly");
171 rc = _set_thresholds(&thresholds, "5:33", NULL);
172 ok(rc == 0, "Thresholds ('5:33', NULL) set");
173 ok(thresholds->warning->start == 5, "Warning start set");
174 ok(thresholds->warning->end == 33, "Warning end set");
175 ok(thresholds->critical == NULL, "Critical not set");
177 rc = _set_thresholds(&thresholds, "30", "60");
178 ok(rc == 0, "Thresholds ('30', '60') set");
179 ok(thresholds->warning->end == 30, "Warning set correctly");
180 ok(thresholds->critical->end == 60, "Critical set correctly");
181 ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
182 ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
185 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
186 ok(rc == 0, "Thresholds ('-30:20', '-10:-2') set");
187 ok(thresholds->warning->start == -10, "Warning start set correctly");
188 ok(thresholds->warning->end == -2, "Warning end set correctly");
189 ok(thresholds->critical->start == -30, "Critical start set correctly");
190 ok(thresholds->critical->end == 20, "Critical end set correctly");
191 ok(get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
192 ok(get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
193 ok(get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
194 ok(get_status(-10, thresholds) == STATE_OK, "-10 - ok");
195 ok(get_status(-2, thresholds) == STATE_OK, "-2 - ok");
196 ok(get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
197 ok(get_status(19, thresholds) == STATE_WARNING, "19 - warning");
198 ok(get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
200 char *test;
201 test = np_escaped_string("bob\\n");
202 ok(strcmp(test, "bob\n") == 0, "bob\\n ok");
203 free(test);
205 test = np_escaped_string("rhuba\\rb");
206 ok(strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
207 free(test);
209 test = np_escaped_string("ba\\nge\\r");
210 ok(strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
211 free(test);
213 test = np_escaped_string("\\rabbi\\t");
214 ok(strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
215 free(test);
217 test = np_escaped_string("and\\\\or");
218 ok(strcmp(test, "and\\or") == 0, "and\\\\or okay");
219 free(test);
221 test = np_escaped_string("bo\\gus");
222 ok(strcmp(test, "bogus") == 0, "bo\\gus okay");
223 free(test);
225 test = np_escaped_string("everything");
226 ok(strcmp(test, "everything") == 0, "everything okay");
228 /* np_extract_ntpvar tests (23) */
229 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
230 ok(test && !strcmp(test, "bar"), "1st test as expected");
231 free(test);
233 test = np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
234 ok(test && !strcmp(test, "foo"), "2nd test as expected");
235 free(test);
237 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
238 ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
239 free(test);
241 test = np_extract_ntpvar("foo=bar\n", "foo");
242 ok(test && !strcmp(test, "bar"), "Single test as expected");
243 free(test);
245 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
246 ok(!test, "Key not found 1");
248 test = np_extract_ntpvar("foo=bar\n", "abcd");
249 ok(!test, "Key not found 2");
251 test = np_extract_ntpvar("foo=bar=foobar", "foo");
252 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
253 free(test);
255 test = np_extract_ntpvar("foo", "foo");
256 ok(!test, "Malformed string 1");
258 test = np_extract_ntpvar("foo,", "foo");
259 ok(!test, "Malformed string 2");
261 test = np_extract_ntpvar("foo=", "foo");
262 ok(!test, "Malformed string 3");
264 test = np_extract_ntpvar("foo=,bar=foo", "foo");
265 ok(!test, "Malformed string 4");
267 test = np_extract_ntpvar(",foo", "foo");
268 ok(!test, "Malformed string 5");
270 test = np_extract_ntpvar("=foo", "foo");
271 ok(!test, "Malformed string 6");
273 test = np_extract_ntpvar("=foo,", "foo");
274 ok(!test, "Malformed string 7");
276 test = np_extract_ntpvar(",,,", "foo");
277 ok(!test, "Malformed string 8");
279 test = np_extract_ntpvar("===", "foo");
280 ok(!test, "Malformed string 9");
282 test = np_extract_ntpvar(",=,=,", "foo");
283 ok(!test, "Malformed string 10");
285 test = np_extract_ntpvar("=,=,=", "foo");
286 ok(!test, "Malformed string 11");
288 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
289 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
290 free(test);
292 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
293 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
294 free(test);
296 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
297 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
298 free(test);
300 test = np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
301 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
302 free(test);
304 test = np_extract_ntpvar("", "foo");
305 ok(!test, "Empty string return NULL");
307 /* This is the result of running ./test_utils */
308 temp_string = (char *)_np_state_generate_key();
309 ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters") ||
310 diag("You are probably running in wrong directory. Must run as ./test_utils");
312 this_monitoring_plugin->argc = 4;
313 this_monitoring_plugin->argv[0] = "./test_utils";
314 this_monitoring_plugin->argv[1] = "here";
315 this_monitoring_plugin->argv[2] = "--and";
316 this_monitoring_plugin->argv[3] = "now";
317 temp_string = (char *)_np_state_generate_key();
318 ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv");
320 unsetenv("MP_STATE_PATH");
321 temp_string = (char *)_np_state_calculate_location_prefix();
322 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory");
324 setenv("MP_STATE_PATH", "", 1);
325 temp_string = (char *)_np_state_calculate_location_prefix();
326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string");
328 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
329 temp_string = (char *)_np_state_calculate_location_prefix();
330 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory");
332 ok(temp_state_key == NULL, "temp_state_key initially empty");
334 this_monitoring_plugin->argc = 1;
335 this_monitoring_plugin->argv[0] = "./test_utils";
336 np_enable_state(NULL, 51);
337 temp_state_key = this_monitoring_plugin->state;
338 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
339 ok(!strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename");
341 np_enable_state("allowedchars_in_keyname", 77);
342 temp_state_key = this_monitoring_plugin->state;
343 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid());
344 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
345 ok(!strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars");
346 ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename");
348 /* Don't do this test just yet. Will die */
350 np_enable_state("bad^chars$in@here", 77);
351 temp_state_key = this_monitoring_plugin->state;
352 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
355 np_enable_state("funnykeyname", 54);
356 temp_state_key = this_monitoring_plugin->state;
357 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid());
358 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
359 ok(!strcmp(temp_state_key->name, "funnykeyname"), "Got key name");
361 ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename");
362 ok(temp_state_key->data_version == 54, "Version set");
364 temp_state_data = np_state_read();
365 ok(temp_state_data == NULL, "Got no state data as file does not exist");
368 temp_fp = fopen("var/statefile", "r");
369 if (temp_fp==NULL)
370 printf("Error opening. errno=%d\n", errno);
371 printf("temp_fp=%s\n", temp_fp);
372 ok( _np_state_read_file(temp_fp) == true, "Can read state file" );
373 fclose(temp_fp);
376 temp_state_key->_filename = "var/statefile";
377 temp_state_data = np_state_read();
378 ok(this_monitoring_plugin->state->state_data != NULL, "Got state data now") ||
379 diag("Are you running in right directory? Will get coredump next if not");
380 ok(this_monitoring_plugin->state->state_data->time == 1234567890, "Got time");
381 ok(!strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected");
383 temp_state_key->data_version = 53;
384 temp_state_data = np_state_read();
385 ok(temp_state_data == NULL, "Older data version gives NULL");
386 temp_state_key->data_version = 54;
388 temp_state_key->_filename = "var/nonexistent";
389 temp_state_data = np_state_read();
390 ok(temp_state_data == NULL, "Missing file gives NULL");
391 ok(this_monitoring_plugin->state->state_data == NULL, "No state information");
393 temp_state_key->_filename = "var/oldformat";
394 temp_state_data = np_state_read();
395 ok(temp_state_data == NULL, "Old file format gives NULL");
397 temp_state_key->_filename = "var/baddate";
398 temp_state_data = np_state_read();
399 ok(temp_state_data == NULL, "Bad date gives NULL");
401 temp_state_key->_filename = "var/missingdataline";
402 temp_state_data = np_state_read();
403 ok(temp_state_data == NULL, "Missing data line gives NULL");
405 unlink("var/generated");
406 temp_state_key->_filename = "var/generated";
407 current_time = 1234567890;
408 np_state_write_string(current_time, "String to read");
409 ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected");
411 unlink("var/generated_directory/statefile");
412 unlink("var/generated_directory");
413 temp_state_key->_filename = "var/generated_directory/statefile";
414 current_time = 1234567890;
415 np_state_write_string(current_time, "String to read");
416 ok(system("cmp var/generated_directory/statefile var/statefile") == 0, "Have created directory");
418 /* This test to check cannot write to dir - can't automate yet */
420 unlink("var/generated_bad_dir");
421 mkdir("var/generated_bad_dir", S_IRUSR);
422 np_state_write_string(current_time, "String to read");
425 temp_state_key->_filename = "var/generated";
426 time(&current_time);
427 np_state_write_string(0, "String to read");
428 temp_state_data = np_state_read();
429 /* Check time is set to current_time */
430 ok(system("cmp var/generated var/statefile > /dev/null") != 0, "Generated file should be different this time");
431 ok(this_monitoring_plugin->state->state_data->time - current_time <= 1, "Has time generated from current time");
433 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
435 temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
436 np_state_write_string(0, "Bad file");
439 np_cleanup();
441 ok(this_monitoring_plugin == NULL, "Free'd this_monitoring_plugin");
443 ok(mp_suid() == false, "Test aren't suid");
445 /* base states with random case */
446 char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL};
448 for (i = 0; states[i] != NULL; i++) {
449 /* out of the random case states, create the lower and upper versions + numeric string one */
450 char *statelower = strdup(states[i]);
451 char *stateupper = strdup(states[i]);
452 char statenum[2];
453 char *temp_ptr;
454 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
455 *temp_ptr = tolower(*temp_ptr);
457 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
458 *temp_ptr = toupper(*temp_ptr);
460 snprintf(statenum, 2, "%i", i);
462 /* Base test names, we'll append the state string */
463 char testname[64] = "Translate state string: ";
464 int tlen = strlen(testname);
466 strcpy(testname + tlen, states[i]);
467 ok(i == mp_translate_state(states[i]), testname);
469 strcpy(testname + tlen, statelower);
470 ok(i == mp_translate_state(statelower), testname);
472 strcpy(testname + tlen, stateupper);
473 ok(i == mp_translate_state(stateupper), testname);
475 strcpy(testname + tlen, statenum);
476 ok(i == mp_translate_state(statenum), testname);
478 ok(ERROR == mp_translate_state("warningfewgw"), "Translate state string with garbage");
479 ok(ERROR == mp_translate_state("00"), "Translate state string: bad numeric string 1");
480 ok(ERROR == mp_translate_state("01"), "Translate state string: bad numeric string 2");
481 ok(ERROR == mp_translate_state("10"), "Translate state string: bad numeric string 3");
482 ok(ERROR == mp_translate_state(""), "Translate state string: empty string");
484 return exit_status();