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.c64
1 files changed, 58 insertions, 6 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index d7cdc33..6e04bfc 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -21,6 +21,9 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24#include <sys/types.h>
25#include <sys/stat.h>
26
24int 27int
25main (int argc, char **argv) 28main (int argc, char **argv)
26{ 29{
@@ -36,7 +39,7 @@ main (int argc, char **argv)
36 nagios_plugin *temp_nagios_plugin; 39 nagios_plugin *temp_nagios_plugin;
37 FILE *temp_fp; 40 FILE *temp_fp;
38 41
39 plan_tests(81+23); 42 plan_tests(134);
40 43
41 _get_nagios_plugin( &temp_nagios_plugin ); 44 _get_nagios_plugin( &temp_nagios_plugin );
42 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised"); 45 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised");
@@ -295,13 +298,11 @@ main (int argc, char **argv)
295 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); 298 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" );
296 299
297 300
298 printf("Filename=%s\n", temp_state_key->_filename);
299 np_enable_state("funnykeyname", 54); 301 np_enable_state("funnykeyname", 54);
300 temp_state_key = temp_nagios_plugin->state; 302 temp_state_key = temp_nagios_plugin->state;
301 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 303 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
302 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 304 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
303 305
304 printf("Filename=%s\n", temp_state_key->_filename);
305 306
306 307
307 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); 308 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
@@ -325,20 +326,71 @@ main (int argc, char **argv)
325 ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" ); 326 ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" );
326 ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 327 ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" );
327 ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 328 ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
328 printf("state_data=%s|\n", temp_nagios_plugin->state->state_data->data);
329 329
330 temp_state_key->data_version=53;
331 temp_state_data = np_state_read(temp_state_key);
332 ok( temp_state_data==NULL, "Older data version gives NULL" );
333 temp_state_key->data_version=54;
330 334
331 temp_state_key->_filename="var/nonexistant"; 335 temp_state_key->_filename="var/nonexistant";
332 temp_state_data = np_state_read(temp_state_key); 336 temp_state_data = np_state_read(temp_state_key);
333 ok( temp_state_data==NULL, "Missing file gives NULL" ); 337 ok( temp_state_data==NULL, "Missing file gives NULL" );
334 ok( temp_nagios_plugin->state->state_data==NULL, "No state information" ); 338 ok( temp_nagios_plugin->state->state_data==NULL, "No state information" );
335 339
336 time(&current_time); 340 temp_state_key->_filename="var/oldformat";
337 np_state_write_string(NULL, "New data"); 341 temp_state_data = np_state_read(temp_state_key);
342 ok( temp_state_data==NULL, "Old file format gives NULL" );
343
344 temp_state_key->_filename="var/baddate";
345 temp_state_data = np_state_read(temp_state_key);
346 ok( temp_state_data==NULL, "Bad date gives NULL" );
338 347
348 temp_state_key->_filename="var/missingdataline";
349 temp_state_data = np_state_read(temp_state_key);
350 ok( temp_state_data==NULL, "Missing data line gives NULL" );
351
352
353
354
355 unlink("var/generated");
356 temp_state_key->_filename="var/generated";
357 current_time=1234567890;
358 np_state_write_string(&current_time, "String to read");
359 ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
360
361
362
363
364 unlink("var/generated_directory/statefile");
365 unlink("var/generated_directory");
366 temp_state_key->_filename="var/generated_directory/statefile";
367 current_time=1234567890;
368 np_state_write_string(&current_time, "String to read");
369 ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
370
371 /* This test to check cannot write to dir - can't automate yet */
372 /*
373 unlink("var/generated_bad_dir");
374 mkdir("var/generated_bad_dir", S_IRUSR);
375 np_state_write_string(&current_time, "String to read");
376 */
377
378
379 temp_state_key->_filename="var/generated";
380 time(&current_time);
381 np_state_write_string(NULL, "String to read");
339 temp_state_data = np_state_read(temp_state_key); 382 temp_state_data = np_state_read(temp_state_key);
340 /* Check time is set to current_time */ 383 /* Check time is set to current_time */
384 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
385 ok(temp_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
386
341 387
388 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
389 /*
390 temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
391 np_state_write_string(NULL, "Bad file");
392 */
393
342 394
343 np_cleanup(); 395 np_cleanup();
344 ok(temp_state_key==NULL, "temp_state_key cleared"); 396 ok(temp_state_key==NULL, "temp_state_key cleared");