diff options
Diffstat (limited to 'lib/tests/test_utils.c')
-rw-r--r-- | lib/tests/test_utils.c | 182 |
1 files changed, 181 insertions, 1 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index 3a45944..5336907 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
@@ -21,6 +21,11 @@ | |||
21 | 21 | ||
22 | #include "tap.h" | 22 | #include "tap.h" |
23 | 23 | ||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | |||
27 | #include "utils_base.c" | ||
28 | |||
24 | int | 29 | int |
25 | main (int argc, char **argv) | 30 | main (int argc, char **argv) |
26 | { | 31 | { |
@@ -28,8 +33,31 @@ main (int argc, char **argv) | |||
28 | double temp; | 33 | double temp; |
29 | thresholds *thresholds = NULL; | 34 | thresholds *thresholds = NULL; |
30 | int rc; | 35 | int rc; |
36 | char *temp_string; | ||
37 | state_key *temp_state_key = NULL; | ||
38 | state_data *temp_state_data; | ||
39 | time_t current_time; | ||
40 | char *temp_filename; | ||
41 | FILE *temp_fp; | ||
42 | |||
43 | plan_tests(141); | ||
44 | |||
45 | ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); | ||
46 | |||
47 | np_init( "check_test", argc, argv ); | ||
48 | |||
49 | ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); | ||
50 | ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); | ||
51 | |||
52 | ok( this_nagios_plugin->argc==argc, "Argc set" ); | ||
53 | ok( this_nagios_plugin->argv==argv, "Argv set" ); | ||
31 | 54 | ||
32 | plan_tests(81+23); | 55 | np_set_args(0,0); |
56 | |||
57 | ok( this_nagios_plugin->argc==0, "argc changed" ); | ||
58 | ok( this_nagios_plugin->argv==0, "argv changed" ); | ||
59 | |||
60 | np_set_args(argc, argv); | ||
33 | 61 | ||
34 | range = parse_range_string("6"); | 62 | range = parse_range_string("6"); |
35 | ok( range != NULL, "'6' is valid range"); | 63 | ok( range != NULL, "'6' is valid range"); |
@@ -251,5 +279,157 @@ main (int argc, char **argv) | |||
251 | test=np_extract_ntpvar("", "foo"); | 279 | test=np_extract_ntpvar("", "foo"); |
252 | ok(!test, "Empty string return NULL"); | 280 | ok(!test, "Empty string return NULL"); |
253 | 281 | ||
282 | |||
283 | /* This is the result of running ./test_utils */ | ||
284 | temp_string = (char *) _np_state_generate_key(); | ||
285 | ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ) || | ||
286 | diag( "You are probably running in wrong directory. Must run as ./test_utils" ); | ||
287 | |||
288 | |||
289 | this_nagios_plugin->argc=4; | ||
290 | this_nagios_plugin->argv[0] = "./test_utils"; | ||
291 | this_nagios_plugin->argv[1] = "here"; | ||
292 | this_nagios_plugin->argv[2] = "--and"; | ||
293 | this_nagios_plugin->argv[3] = "now"; | ||
294 | temp_string = (char *) _np_state_generate_key(); | ||
295 | ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); | ||
296 | |||
297 | unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
298 | temp_string = (char *) _np_state_calculate_location_prefix(); | ||
299 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); | ||
300 | |||
301 | setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); | ||
302 | temp_string = (char *) _np_state_calculate_location_prefix(); | ||
303 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); | ||
304 | |||
305 | setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); | ||
306 | temp_string = (char *) _np_state_calculate_location_prefix(); | ||
307 | ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); | ||
308 | |||
309 | |||
310 | |||
311 | ok(temp_state_key==NULL, "temp_state_key initially empty"); | ||
312 | |||
313 | this_nagios_plugin->argc=1; | ||
314 | this_nagios_plugin->argv[0] = "./test_utils"; | ||
315 | np_enable_state(NULL, 51); | ||
316 | temp_state_key = this_nagios_plugin->state; | ||
317 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | ||
318 | ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); | ||
319 | |||
320 | |||
321 | np_enable_state("allowedchars_in_keyname", 77); | ||
322 | temp_state_key = this_nagios_plugin->state; | ||
323 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | ||
324 | ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); | ||
325 | ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); | ||
326 | |||
327 | |||
328 | /* Don't do this test just yet. Will die */ | ||
329 | /* | ||
330 | np_enable_state("bad^chars$in@here", 77); | ||
331 | temp_state_key = this_nagios_plugin->state; | ||
332 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); | ||
333 | */ | ||
334 | |||
335 | np_enable_state("funnykeyname", 54); | ||
336 | temp_state_key = this_nagios_plugin->state; | ||
337 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | ||
338 | ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); | ||
339 | |||
340 | |||
341 | |||
342 | ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); | ||
343 | ok( temp_state_key->data_version==54, "Version set" ); | ||
344 | |||
345 | temp_state_data = np_state_read(temp_state_key); | ||
346 | ok( temp_state_data==NULL, "Got no state data as file does not exist" ); | ||
347 | |||
348 | |||
349 | /* | ||
350 | temp_fp = fopen("var/statefile", "r"); | ||
351 | if (temp_fp==NULL) | ||
352 | printf("Error opening. errno=%d\n", errno); | ||
353 | printf("temp_fp=%s\n", temp_fp); | ||
354 | ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" ); | ||
355 | fclose(temp_fp); | ||
356 | */ | ||
357 | |||
358 | temp_state_key->_filename="var/statefile"; | ||
359 | temp_state_data = np_state_read(temp_state_key); | ||
360 | 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"); | ||
361 | ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" ); | ||
362 | ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); | ||
363 | |||
364 | temp_state_key->data_version=53; | ||
365 | temp_state_data = np_state_read(temp_state_key); | ||
366 | ok( temp_state_data==NULL, "Older data version gives NULL" ); | ||
367 | temp_state_key->data_version=54; | ||
368 | |||
369 | temp_state_key->_filename="var/nonexistant"; | ||
370 | temp_state_data = np_state_read(temp_state_key); | ||
371 | ok( temp_state_data==NULL, "Missing file gives NULL" ); | ||
372 | ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); | ||
373 | |||
374 | temp_state_key->_filename="var/oldformat"; | ||
375 | temp_state_data = np_state_read(temp_state_key); | ||
376 | ok( temp_state_data==NULL, "Old file format gives NULL" ); | ||
377 | |||
378 | temp_state_key->_filename="var/baddate"; | ||
379 | temp_state_data = np_state_read(temp_state_key); | ||
380 | ok( temp_state_data==NULL, "Bad date gives NULL" ); | ||
381 | |||
382 | temp_state_key->_filename="var/missingdataline"; | ||
383 | temp_state_data = np_state_read(temp_state_key); | ||
384 | ok( temp_state_data==NULL, "Missing data line gives NULL" ); | ||
385 | |||
386 | |||
387 | |||
388 | |||
389 | unlink("var/generated"); | ||
390 | temp_state_key->_filename="var/generated"; | ||
391 | current_time=1234567890; | ||
392 | np_state_write_string(current_time, "String to read"); | ||
393 | ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected"); | ||
394 | |||
395 | |||
396 | |||
397 | |||
398 | unlink("var/generated_directory/statefile"); | ||
399 | unlink("var/generated_directory"); | ||
400 | temp_state_key->_filename="var/generated_directory/statefile"; | ||
401 | current_time=1234567890; | ||
402 | np_state_write_string(current_time, "String to read"); | ||
403 | ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory"); | ||
404 | |||
405 | /* This test to check cannot write to dir - can't automate yet */ | ||
406 | /* | ||
407 | unlink("var/generated_bad_dir"); | ||
408 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
409 | np_state_write_string(current_time, "String to read"); | ||
410 | */ | ||
411 | |||
412 | |||
413 | temp_state_key->_filename="var/generated"; | ||
414 | time(¤t_time); | ||
415 | np_state_write_string(0, "String to read"); | ||
416 | temp_state_data = np_state_read(temp_state_key); | ||
417 | /* Check time is set to current_time */ | ||
418 | ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); | ||
419 | ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); | ||
420 | |||
421 | |||
422 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ | ||
423 | /* | ||
424 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
425 | np_state_write_string(0, "Bad file"); | ||
426 | */ | ||
427 | |||
428 | |||
429 | np_cleanup(); | ||
430 | |||
431 | ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); | ||
432 | |||
254 | return exit_status(); | 433 | return exit_status(); |
255 | } | 434 | } |
435 | |||