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.c120
1 files changed, 91 insertions, 29 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 79e1110..356887d 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -32,28 +32,28 @@ main (int argc, char **argv)
32 range *range; 32 range *range;
33 double temp; 33 double temp;
34 thresholds *thresholds = NULL; 34 thresholds *thresholds = NULL;
35 int rc; 35 int i, rc;
36 char *temp_string; 36 char *temp_string;
37 state_key *temp_state_key = NULL; 37 state_key *temp_state_key = NULL;
38 state_data *temp_state_data; 38 state_data *temp_state_data;
39 time_t current_time; 39 time_t current_time;
40 40
41 plan_tests(150); 41 plan_tests(185);
42 42
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); 43 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
44 44
45 np_init( "check_test", argc, argv ); 45 np_init( "check_test", argc, argv );
46 46
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); 47 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 48 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
49 49
50 ok( this_nagios_plugin->argc==argc, "Argc set" ); 50 ok( this_monitoring_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" ); 51 ok( this_monitoring_plugin->argv==argv, "Argv set" );
52 52
53 np_set_args(0,0); 53 np_set_args(0,0);
54 54
55 ok( this_nagios_plugin->argc==0, "argc changed" ); 55 ok( this_monitoring_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" ); 56 ok( this_monitoring_plugin->argv==0, "argv changed" );
57 57
58 np_set_args(argc, argv); 58 np_set_args(argc, argv);
59 59
@@ -182,6 +182,21 @@ main (int argc, char **argv)
182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
184 184
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");
199
185 char *test; 200 char *test;
186 test = np_escaped_string("bob\\n"); 201 test = np_escaped_string("bob\\n");
187 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 202 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
@@ -296,23 +311,23 @@ main (int argc, char **argv)
296 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 311 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
297 312
298 313
299 this_nagios_plugin->argc=4; 314 this_monitoring_plugin->argc=4;
300 this_nagios_plugin->argv[0] = "./test_utils"; 315 this_monitoring_plugin->argv[0] = "./test_utils";
301 this_nagios_plugin->argv[1] = "here"; 316 this_monitoring_plugin->argv[1] = "here";
302 this_nagios_plugin->argv[2] = "--and"; 317 this_monitoring_plugin->argv[2] = "--and";
303 this_nagios_plugin->argv[3] = "now"; 318 this_monitoring_plugin->argv[3] = "now";
304 temp_string = (char *) _np_state_generate_key(); 319 temp_string = (char *) _np_state_generate_key();
305 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 320 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
306 321
307 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 322 unsetenv("MP_STATE_PATH");
308 temp_string = (char *) _np_state_calculate_location_prefix(); 323 temp_string = (char *) _np_state_calculate_location_prefix();
309 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 324 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
310 325
311 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 326 setenv("MP_STATE_PATH", "", 1);
312 temp_string = (char *) _np_state_calculate_location_prefix(); 327 temp_string = (char *) _np_state_calculate_location_prefix();
313 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 328 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
314 329
315 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 330 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
316 temp_string = (char *) _np_state_calculate_location_prefix(); 331 temp_string = (char *) _np_state_calculate_location_prefix();
317 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 332 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
318 333
@@ -320,16 +335,16 @@ main (int argc, char **argv)
320 335
321 ok(temp_state_key==NULL, "temp_state_key initially empty"); 336 ok(temp_state_key==NULL, "temp_state_key initially empty");
322 337
323 this_nagios_plugin->argc=1; 338 this_monitoring_plugin->argc=1;
324 this_nagios_plugin->argv[0] = "./test_utils"; 339 this_monitoring_plugin->argv[0] = "./test_utils";
325 np_enable_state(NULL, 51); 340 np_enable_state(NULL, 51);
326 temp_state_key = this_nagios_plugin->state; 341 temp_state_key = this_monitoring_plugin->state;
327 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 342 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
328 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 343 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
329 344
330 345
331 np_enable_state("allowedchars_in_keyname", 77); 346 np_enable_state("allowedchars_in_keyname", 77);
332 temp_state_key = this_nagios_plugin->state; 347 temp_state_key = this_monitoring_plugin->state;
333 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 348 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" ); 349 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" ); 350 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
@@ -338,12 +353,12 @@ main (int argc, char **argv)
338 /* Don't do this test just yet. Will die */ 353 /* Don't do this test just yet. Will die */
339 /* 354 /*
340 np_enable_state("bad^chars$in@here", 77); 355 np_enable_state("bad^chars$in@here", 77);
341 temp_state_key = this_nagios_plugin->state; 356 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" ); 357 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
343 */ 358 */
344 359
345 np_enable_state("funnykeyname", 54); 360 np_enable_state("funnykeyname", 54);
346 temp_state_key = this_nagios_plugin->state; 361 temp_state_key = this_monitoring_plugin->state;
347 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 362 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
348 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 363 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
349 364
@@ -367,9 +382,9 @@ main (int argc, char **argv)
367 382
368 temp_state_key->_filename="var/statefile"; 383 temp_state_key->_filename="var/statefile";
369 temp_state_data = np_state_read(); 384 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"); 385 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" ); 386 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" ); 387 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
373 388
374 temp_state_key->data_version=53; 389 temp_state_key->data_version=53;
375 temp_state_data = np_state_read(); 390 temp_state_data = np_state_read();
@@ -379,7 +394,7 @@ main (int argc, char **argv)
379 temp_state_key->_filename="var/nonexistant"; 394 temp_state_key->_filename="var/nonexistant";
380 temp_state_data = np_state_read(); 395 temp_state_data = np_state_read();
381 ok( temp_state_data==NULL, "Missing file gives NULL" ); 396 ok( temp_state_data==NULL, "Missing file gives NULL" );
382 ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); 397 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
383 398
384 temp_state_key->_filename="var/oldformat"; 399 temp_state_key->_filename="var/oldformat";
385 temp_state_data = np_state_read(); 400 temp_state_data = np_state_read();
@@ -426,7 +441,7 @@ main (int argc, char **argv)
426 temp_state_data = np_state_read(); 441 temp_state_data = np_state_read();
427 /* Check time is set to current_time */ 442 /* 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"); 443 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"); 444 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
430 445
431 446
432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 447 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -438,7 +453,54 @@ main (int argc, char **argv)
438 453
439 np_cleanup(); 454 np_cleanup();
440 455
441 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); 456 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
457
458 ok(mp_suid() == FALSE, "Test aren't suid");
459
460 /* base states with random case */
461 char *states[] = {
462 "Ok",
463 "wArnINg",
464 "cRiTIcaL",
465 "UnKNoWN",
466 NULL
467 };
468
469 for (i=0; states[i]!=NULL; i++) {
470 /* out of the random case states, create the lower and upper versions + numeric string one */
471 char *statelower = strdup(states[i]);
472 char *stateupper = strdup(states[i]);
473 char statenum[2];
474 char *temp_ptr;
475 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
476 *temp_ptr = tolower(*temp_ptr);
477 }
478 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
479 *temp_ptr = toupper(*temp_ptr);
480 }
481 snprintf(statenum, 2, "%i", i);
482
483 /* Base test names, we'll append the state string */
484 char testname[64] = "Translate state string: ";
485 int tlen = strlen(testname);
486
487 strcpy(testname+tlen, states[i]);
488 ok(i==mp_translate_state(states[i]), testname);
489
490 strcpy(testname+tlen, statelower);
491 ok(i==mp_translate_state(statelower), testname);
492
493 strcpy(testname+tlen, stateupper);
494 ok(i==mp_translate_state(stateupper), testname);
495
496 strcpy(testname+tlen, statenum);
497 ok(i==mp_translate_state(statenum), testname);
498 }
499 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage");
500 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1");
501 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2");
502 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3");
503 ok(ERROR==mp_translate_state(""), "Translate state string: empty string");
442 504
443 return exit_status(); 505 return exit_status();
444} 506}