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.c128
1 files changed, 97 insertions, 31 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 79e1110a..f35b7e27 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -21,6 +21,7 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24#include <unistd.h>
24#include <sys/types.h> 25#include <sys/types.h>
25#include <sys/stat.h> 26#include <sys/stat.h>
26 27
@@ -29,31 +30,32 @@
29int 30int
30main (int argc, char **argv) 31main (int argc, char **argv)
31{ 32{
33 char state_path[1024];
32 range *range; 34 range *range;
33 double temp; 35 double temp;
34 thresholds *thresholds = NULL; 36 thresholds *thresholds = NULL;
35 int rc; 37 int i, rc;
36 char *temp_string; 38 char *temp_string;
37 state_key *temp_state_key = NULL; 39 state_key *temp_state_key = NULL;
38 state_data *temp_state_data; 40 state_data *temp_state_data;
39 time_t current_time; 41 time_t current_time;
40 42
41 plan_tests(150); 43 plan_tests(185);
42 44
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); 45 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
44 46
45 np_init( "check_test", argc, argv ); 47 np_init( "check_test", argc, argv );
46 48
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); 49 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 50 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
49 51
50 ok( this_nagios_plugin->argc==argc, "Argc set" ); 52 ok( this_monitoring_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" ); 53 ok( this_monitoring_plugin->argv==argv, "Argv set" );
52 54
53 np_set_args(0,0); 55 np_set_args(0,0);
54 56
55 ok( this_nagios_plugin->argc==0, "argc changed" ); 57 ok( this_monitoring_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" ); 58 ok( this_monitoring_plugin->argv==0, "argv changed" );
57 59
58 np_set_args(argc, argv); 60 np_set_args(argc, argv);
59 61
@@ -182,6 +184,21 @@ main (int argc, char **argv)
182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 184 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 185 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
184 186
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");
201
185 char *test; 202 char *test;
186 test = np_escaped_string("bob\\n"); 203 test = np_escaped_string("bob\\n");
187 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 204 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
@@ -296,23 +313,23 @@ main (int argc, char **argv)
296 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 313 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
297 314
298 315
299 this_nagios_plugin->argc=4; 316 this_monitoring_plugin->argc=4;
300 this_nagios_plugin->argv[0] = "./test_utils"; 317 this_monitoring_plugin->argv[0] = "./test_utils";
301 this_nagios_plugin->argv[1] = "here"; 318 this_monitoring_plugin->argv[1] = "here";
302 this_nagios_plugin->argv[2] = "--and"; 319 this_monitoring_plugin->argv[2] = "--and";
303 this_nagios_plugin->argv[3] = "now"; 320 this_monitoring_plugin->argv[3] = "now";
304 temp_string = (char *) _np_state_generate_key(); 321 temp_string = (char *) _np_state_generate_key();
305 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 322 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
306 323
307 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 324 unsetenv("MP_STATE_PATH");
308 temp_string = (char *) _np_state_calculate_location_prefix(); 325 temp_string = (char *) _np_state_calculate_location_prefix();
309 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
310 327
311 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 328 setenv("MP_STATE_PATH", "", 1);
312 temp_string = (char *) _np_state_calculate_location_prefix(); 329 temp_string = (char *) _np_state_calculate_location_prefix();
313 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 330 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
314 331
315 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 332 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
316 temp_string = (char *) _np_state_calculate_location_prefix(); 333 temp_string = (char *) _np_state_calculate_location_prefix();
317 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 334 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
318 335
@@ -320,36 +337,38 @@ main (int argc, char **argv)
320 337
321 ok(temp_state_key==NULL, "temp_state_key initially empty"); 338 ok(temp_state_key==NULL, "temp_state_key initially empty");
322 339
323 this_nagios_plugin->argc=1; 340 this_monitoring_plugin->argc=1;
324 this_nagios_plugin->argv[0] = "./test_utils"; 341 this_monitoring_plugin->argv[0] = "./test_utils";
325 np_enable_state(NULL, 51); 342 np_enable_state(NULL, 51);
326 temp_state_key = this_nagios_plugin->state; 343 temp_state_key = this_monitoring_plugin->state;
327 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 344 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
328 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 345 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
329 346
330 347
331 np_enable_state("allowedchars_in_keyname", 77); 348 np_enable_state("allowedchars_in_keyname", 77);
332 temp_state_key = this_nagios_plugin->state; 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());
333 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 351 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
334 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); 352 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
335 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); 353 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
336 354
337 355
338 /* Don't do this test just yet. Will die */ 356 /* Don't do this test just yet. Will die */
339 /* 357 /*
340 np_enable_state("bad^chars$in@here", 77); 358 np_enable_state("bad^chars$in@here", 77);
341 temp_state_key = this_nagios_plugin->state; 359 temp_state_key = this_monitoring_plugin->state;
342 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); 360 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
343 */ 361 */
344 362
345 np_enable_state("funnykeyname", 54); 363 np_enable_state("funnykeyname", 54);
346 temp_state_key = this_nagios_plugin->state; 364 temp_state_key = this_monitoring_plugin->state;
365 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid());
347 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 366 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
348 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 367 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
349 368
350 369
351 370
352 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); 371 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
353 ok( temp_state_key->data_version==54, "Version set" ); 372 ok( temp_state_key->data_version==54, "Version set" );
354 373
355 temp_state_data = np_state_read(); 374 temp_state_data = np_state_read();
@@ -367,9 +386,9 @@ main (int argc, char **argv)
367 386
368 temp_state_key->_filename="var/statefile"; 387 temp_state_key->_filename="var/statefile";
369 temp_state_data = np_state_read(); 388 temp_state_data = np_state_read();
370 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); 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");
371 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 390 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" );
372 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 391 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
373 392
374 temp_state_key->data_version=53; 393 temp_state_key->data_version=53;
375 temp_state_data = np_state_read(); 394 temp_state_data = np_state_read();
@@ -379,7 +398,7 @@ main (int argc, char **argv)
379 temp_state_key->_filename="var/nonexistant"; 398 temp_state_key->_filename="var/nonexistant";
380 temp_state_data = np_state_read(); 399 temp_state_data = np_state_read();
381 ok( temp_state_data==NULL, "Missing file gives NULL" ); 400 ok( temp_state_data==NULL, "Missing file gives NULL" );
382 ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); 401 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
383 402
384 temp_state_key->_filename="var/oldformat"; 403 temp_state_key->_filename="var/oldformat";
385 temp_state_data = np_state_read(); 404 temp_state_data = np_state_read();
@@ -426,7 +445,7 @@ main (int argc, char **argv)
426 temp_state_data = np_state_read(); 445 temp_state_data = np_state_read();
427 /* Check time is set to current_time */ 446 /* Check time is set to current_time */
428 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 447 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
429 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 448 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
430 449
431 450
432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 451 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -438,7 +457,54 @@ main (int argc, char **argv)
438 457
439 np_cleanup(); 458 np_cleanup();
440 459
441 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); 460 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
461
462 ok(mp_suid() == FALSE, "Test aren't suid");
463
464 /* base states with random case */
465 char *states[] = {
466 "Ok",
467 "wArnINg",
468 "cRiTIcaL",
469 "UnKNoWN",
470 NULL
471 };
472
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);
481 }
482 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
483 *temp_ptr = toupper(*temp_ptr);
484 }
485 snprintf(statenum, 2, "%i", i);
486
487 /* Base test names, we'll append the state string */
488 char testname[64] = "Translate state string: ";
489 int tlen = strlen(testname);
490
491 strcpy(testname+tlen, states[i]);
492 ok(i==mp_translate_state(states[i]), testname);
493
494 strcpy(testname+tlen, statelower);
495 ok(i==mp_translate_state(statelower), testname);
496
497 strcpy(testname+tlen, stateupper);
498 ok(i==mp_translate_state(stateupper), testname);
499
500 strcpy(testname+tlen, statenum);
501 ok(i==mp_translate_state(statenum), testname);
502 }
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");
442 508
443 return exit_status(); 509 return exit_status();
444} 510}