check_curl: fix relative redirects on non-standard port
[monitoring-plugins.git] / lib / tests / test_utils.c
blob01afacdc33a851d1c21b799890b4cc0213ada724
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
31 main (int argc, char **argv)
33 char state_path[1024];
34 range *range;
35 double temp;
36 thresholds *thresholds = NULL;
37 int i, rc;
38 char *temp_string;
39 state_key *temp_state_key = NULL;
40 state_data *temp_state_data;
41 time_t current_time;
43 plan_tests(185);
45 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
47 np_init( "check_test", argc, argv );
49 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
50 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
52 ok( this_monitoring_plugin->argc==argc, "Argc set" );
53 ok( this_monitoring_plugin->argv==argv, "Argv set" );
55 np_set_args(0,0);
57 ok( this_monitoring_plugin->argc==0, "argc changed" );
58 ok( this_monitoring_plugin->argv==0, "argv changed" );
60 np_set_args(argc, argv);
62 range = parse_range_string("6");
63 ok( range != NULL, "'6' is valid range");
64 ok( range->start == 0, "Start correct");
65 ok( range->start_infinity == false, "Not using negative infinity");
66 ok( range->end == 6, "End correct");
67 ok( range->end_infinity == false, "Not using infinity");
68 free(range);
70 range = parse_range_string("1:12%%");
71 ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
72 ok( range->start == 1, "Start correct");
73 ok( range->start_infinity == false, "Not using negative infinity");
74 ok( range->end == 12, "End correct");
75 ok( range->end_infinity == false, "Not using infinity");
76 free(range);
78 range = parse_range_string("-7:23");
79 ok( range != NULL, "'-7:23' is valid range");
80 ok( range->start == -7, "Start correct");
81 ok( range->start_infinity == false, "Not using negative infinity");
82 ok( range->end == 23, "End correct");
83 ok( range->end_infinity == false, "Not using infinity");
84 free(range);
86 range = parse_range_string(":5.75");
87 ok( range != NULL, "':5.75' is valid range");
88 ok( range->start == 0, "Start correct");
89 ok( range->start_infinity == false, "Not using negative infinity");
90 ok( range->end == 5.75, "End correct");
91 ok( range->end_infinity == false, "Not using infinity");
92 free(range);
94 range = parse_range_string("~:-95.99");
95 ok( range != NULL, "~:-95.99' is valid range");
96 ok( range->start_infinity == true, "Using negative infinity");
97 ok( range->end == -95.99, "End correct (with rounding errors)");
98 ok( range->end_infinity == false, "Not using infinity");
99 free(range);
101 range = parse_range_string("12345678901234567890:");
102 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
103 ok( range != NULL, "'12345678901234567890:' is valid range");
104 ok( range->start == temp, "Start correct");
105 ok( range->start_infinity == false, "Not using negative infinity");
106 ok( range->end_infinity == true, "Using infinity");
107 /* Cannot do a "-1" on temp, as it appears to be same value */
108 ok( check_range(temp/1.1, range) == true, "12345678901234567890/1.1 - alert");
109 ok( check_range(temp, range) == false, "12345678901234567890 - no alert");
110 ok( check_range(temp*2, range) == false, "12345678901234567890*2 - no alert");
111 free(range);
113 range = parse_range_string("~:0");
114 ok( range != NULL, "'~:0' is valid range");
115 ok( range->start_infinity == true, "Using negative infinity");
116 ok( range->end == 0, "End correct");
117 ok( range->end_infinity == false, "Not using infinity");
118 ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
119 ok( check_range(0.5, range) == true, "0.5 - alert");
120 ok( check_range(-10, range) == false, "-10 - no alert");
121 ok( check_range(0, range) == false, "0 - no alert");
122 free(range);
124 range = parse_range_string("@0:657.8210567");
125 ok( range != 0, "@0:657.8210567' is a valid range");
126 ok( range->start == 0, "Start correct");
127 ok( range->start_infinity == false, "Not using negative infinity");
128 ok( range->end == 657.8210567, "End correct");
129 ok( range->end_infinity == false, "Not using infinity");
130 ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
131 ok( check_range(32.88, range) == true, "32.88 - alert");
132 ok( check_range(-2, range) == false, "-2 - no alert");
133 ok( check_range(657.8210567, range) == true, "657.8210567 - alert");
134 ok( check_range(0, range) == true, "0 - alert");
135 free(range);
137 range = parse_range_string("@1:1");
138 ok( range != NULL, "'@1:1' is a valid range");
139 ok( range->start == 1, "Start correct");
140 ok( range->start_infinity == false, "Not using negative infinity");
141 ok( range->end == 1, "End correct");
142 ok( range->end_infinity == false, "Not using infinity");
143 ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
144 ok( check_range(0.5, range) == false, "0.5 - no alert");
145 ok( check_range(1, range) == true, "1 - alert");
146 ok( check_range(5.2, range) == false, "5.2 - no alert");
147 free(range);
149 range = parse_range_string("1:1");
150 ok( range != NULL, "'1:1' is a valid range");
151 ok( range->start == 1, "Start correct");
152 ok( range->start_infinity == false, "Not using negative infinity");
153 ok( range->end == 1, "End correct");
154 ok( range->end_infinity == false, "Not using infinity");
155 ok( check_range(0.5, range) == true, "0.5 - alert");
156 ok( check_range(1, range) == false, "1 - no alert");
157 ok( check_range(5.2, range) == true, "5.2 - alert");
158 free(range);
160 range = parse_range_string("2:1");
161 ok( range == NULL, "'2:1' rejected");
163 rc = _set_thresholds(&thresholds, NULL, NULL);
164 ok( rc == 0, "Thresholds (NULL, NULL) set");
165 ok( thresholds->warning == NULL, "Warning not set");
166 ok( thresholds->critical == NULL, "Critical not set");
168 rc = _set_thresholds(&thresholds, NULL, "80");
169 ok( rc == 0, "Thresholds (NULL, '80') set");
170 ok( thresholds->warning == NULL, "Warning not set");
171 ok( thresholds->critical->end == 80, "Critical set correctly");
173 rc = _set_thresholds(&thresholds, "5:33", NULL);
174 ok( rc == 0, "Thresholds ('5:33', NULL) set");
175 ok( thresholds->warning->start == 5, "Warning start set");
176 ok( thresholds->warning->end == 33, "Warning end set");
177 ok( thresholds->critical == NULL, "Critical not set");
179 rc = _set_thresholds(&thresholds, "30", "60");
180 ok( rc == 0, "Thresholds ('30', '60') set");
181 ok( thresholds->warning->end == 30, "Warning set correctly");
182 ok( thresholds->critical->end == 60, "Critical set correctly");
183 ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
184 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
185 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
187 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
188 ok( rc == 0, "Thresholds ('-30:20', '-10:-2') set");
189 ok( thresholds->warning->start == -10, "Warning start set correctly");
190 ok( thresholds->warning->end == -2, "Warning end set correctly");
191 ok( thresholds->critical->start == -30, "Critical start set correctly");
192 ok( thresholds->critical->end == 20, "Critical end set correctly");
193 ok( get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
194 ok( get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
195 ok( get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
196 ok( get_status(-10, thresholds) == STATE_OK, "-10 - ok");
197 ok( get_status(-2, thresholds) == STATE_OK, "-2 - ok");
198 ok( get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
199 ok( get_status(19, thresholds) == STATE_WARNING, "19 - warning");
200 ok( get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
202 char *test;
203 test = np_escaped_string("bob\\n");
204 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
205 free(test);
207 test = np_escaped_string("rhuba\\rb");
208 ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
209 free(test);
211 test = np_escaped_string("ba\\nge\\r");
212 ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
213 free(test);
215 test = np_escaped_string("\\rabbi\\t");
216 ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
217 free(test);
219 test = np_escaped_string("and\\\\or");
220 ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
221 free(test);
223 test = np_escaped_string("bo\\gus");
224 ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
225 free(test);
227 test = np_escaped_string("everything");
228 ok( strcmp(test, "everything") == 0, "everything okay");
230 /* np_extract_ntpvar tests (23) */
231 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
232 ok(test && !strcmp(test, "bar"), "1st test as expected");
233 free(test);
235 test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
236 ok(test && !strcmp(test, "foo"), "2nd test as expected");
237 free(test);
239 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
240 ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
241 free(test);
243 test=np_extract_ntpvar("foo=bar\n", "foo");
244 ok(test && !strcmp(test, "bar"), "Single test as expected");
245 free(test);
247 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
248 ok(!test, "Key not found 1");
250 test=np_extract_ntpvar("foo=bar\n", "abcd");
251 ok(!test, "Key not found 2");
253 test=np_extract_ntpvar("foo=bar=foobar", "foo");
254 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
255 free(test);
257 test=np_extract_ntpvar("foo", "foo");
258 ok(!test, "Malformed string 1");
260 test=np_extract_ntpvar("foo,", "foo");
261 ok(!test, "Malformed string 2");
263 test=np_extract_ntpvar("foo=", "foo");
264 ok(!test, "Malformed string 3");
266 test=np_extract_ntpvar("foo=,bar=foo", "foo");
267 ok(!test, "Malformed string 4");
269 test=np_extract_ntpvar(",foo", "foo");
270 ok(!test, "Malformed string 5");
272 test=np_extract_ntpvar("=foo", "foo");
273 ok(!test, "Malformed string 6");
275 test=np_extract_ntpvar("=foo,", "foo");
276 ok(!test, "Malformed string 7");
278 test=np_extract_ntpvar(",,,", "foo");
279 ok(!test, "Malformed string 8");
281 test=np_extract_ntpvar("===", "foo");
282 ok(!test, "Malformed string 9");
284 test=np_extract_ntpvar(",=,=,", "foo");
285 ok(!test, "Malformed string 10");
287 test=np_extract_ntpvar("=,=,=", "foo");
288 ok(!test, "Malformed string 11");
290 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
291 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
292 free(test);
294 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
295 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
296 free(test);
298 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
299 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
300 free(test);
302 test=np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
303 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
304 free(test);
306 test=np_extract_ntpvar("", "foo");
307 ok(!test, "Empty string return NULL");
310 /* This is the result of running ./test_utils */
311 temp_string = (char *) _np_state_generate_key();
312 ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters" ) ||
313 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
316 this_monitoring_plugin->argc=4;
317 this_monitoring_plugin->argv[0] = "./test_utils";
318 this_monitoring_plugin->argv[1] = "here";
319 this_monitoring_plugin->argv[2] = "--and";
320 this_monitoring_plugin->argv[3] = "now";
321 temp_string = (char *) _np_state_generate_key();
322 ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv" );
324 unsetenv("MP_STATE_PATH");
325 temp_string = (char *) _np_state_calculate_location_prefix();
326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
328 setenv("MP_STATE_PATH", "", 1);
329 temp_string = (char *) _np_state_calculate_location_prefix();
330 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
332 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
333 temp_string = (char *) _np_state_calculate_location_prefix();
334 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
338 ok(temp_state_key==NULL, "temp_state_key initially empty");
340 this_monitoring_plugin->argc=1;
341 this_monitoring_plugin->argv[0] = "./test_utils";
342 np_enable_state(NULL, 51);
343 temp_state_key = this_monitoring_plugin->state;
344 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
345 ok( !strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename" );
348 np_enable_state("allowedchars_in_keyname", 77);
349 temp_state_key = this_monitoring_plugin->state;
350 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid());
351 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
352 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
353 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
356 /* Don't do this test just yet. Will die */
358 np_enable_state("bad^chars$in@here", 77);
359 temp_state_key = this_monitoring_plugin->state;
360 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
363 np_enable_state("funnykeyname", 54);
364 temp_state_key = this_monitoring_plugin->state;
365 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid());
366 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
367 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
371 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
372 ok( temp_state_key->data_version==54, "Version set" );
374 temp_state_data = np_state_read();
375 ok( temp_state_data==NULL, "Got no state data as file does not exist" );
379 temp_fp = fopen("var/statefile", "r");
380 if (temp_fp==NULL)
381 printf("Error opening. errno=%d\n", errno);
382 printf("temp_fp=%s\n", temp_fp);
383 ok( _np_state_read_file(temp_fp) == true, "Can read state file" );
384 fclose(temp_fp);
387 temp_state_key->_filename="var/statefile";
388 temp_state_data = np_state_read();
389 ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not");
390 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" );
391 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
393 temp_state_key->data_version=53;
394 temp_state_data = np_state_read();
395 ok( temp_state_data==NULL, "Older data version gives NULL" );
396 temp_state_key->data_version=54;
398 temp_state_key->_filename="var/nonexistent";
399 temp_state_data = np_state_read();
400 ok( temp_state_data==NULL, "Missing file gives NULL" );
401 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
403 temp_state_key->_filename="var/oldformat";
404 temp_state_data = np_state_read();
405 ok( temp_state_data==NULL, "Old file format gives NULL" );
407 temp_state_key->_filename="var/baddate";
408 temp_state_data = np_state_read();
409 ok( temp_state_data==NULL, "Bad date gives NULL" );
411 temp_state_key->_filename="var/missingdataline";
412 temp_state_data = np_state_read();
413 ok( temp_state_data==NULL, "Missing data line gives NULL" );
418 unlink("var/generated");
419 temp_state_key->_filename="var/generated";
420 current_time=1234567890;
421 np_state_write_string(current_time, "String to read");
422 ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
427 unlink("var/generated_directory/statefile");
428 unlink("var/generated_directory");
429 temp_state_key->_filename="var/generated_directory/statefile";
430 current_time=1234567890;
431 np_state_write_string(current_time, "String to read");
432 ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
434 /* This test to check cannot write to dir - can't automate yet */
436 unlink("var/generated_bad_dir");
437 mkdir("var/generated_bad_dir", S_IRUSR);
438 np_state_write_string(current_time, "String to read");
442 temp_state_key->_filename="var/generated";
443 time(&current_time);
444 np_state_write_string(0, "String to read");
445 temp_state_data = np_state_read();
446 /* Check time is set to current_time */
447 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
448 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
451 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
453 temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
454 np_state_write_string(0, "Bad file");
458 np_cleanup();
460 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
462 ok(mp_suid() == false, "Test aren't suid");
464 /* base states with random case */
465 char *states[] = {
466 "Ok",
467 "wArnINg",
468 "cRiTIcaL",
469 "UnKNoWN",
470 NULL
473 for (i=0; states[i]!=NULL; i++) {
474 /* out of the random case states, create the lower and upper versions + numeric string one */
475 char *statelower = strdup(states[i]);
476 char *stateupper = strdup(states[i]);
477 char statenum[2];
478 char *temp_ptr;
479 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
480 *temp_ptr = tolower(*temp_ptr);
482 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
483 *temp_ptr = toupper(*temp_ptr);
485 snprintf(statenum, 2, "%i", i);
487 /* Base test names, we'll append the state string */
488 char testname[64] = "Translate state string: ";
489 int tlen = strlen(testname);
491 strcpy(testname+tlen, states[i]);
492 ok(i==mp_translate_state(states[i]), testname);
494 strcpy(testname+tlen, statelower);
495 ok(i==mp_translate_state(statelower), testname);
497 strcpy(testname+tlen, stateupper);
498 ok(i==mp_translate_state(stateupper), testname);
500 strcpy(testname+tlen, statenum);
501 ok(i==mp_translate_state(statenum), testname);
503 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage");
504 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1");
505 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2");
506 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3");
507 ok(ERROR==mp_translate_state(""), "Translate state string: empty string");
509 return exit_status();