Back to index

nagios-plugins  1.4.16
test_utils.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * This program is free software: you can redistribute it and/or modify
00004 * it under the terms of the GNU General Public License as published by
00005 * the Free Software Foundation, either version 3 of the License, or
00006 * (at your option) any later version.
00007 * 
00008 * This program is distributed in the hope that it will be useful,
00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 * GNU General Public License for more details.
00012 * 
00013 * You should have received a copy of the GNU General Public License
00014 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 * 
00016 * 
00017 *****************************************************************************/
00018 
00019 #include "common.h"
00020 #include "utils_base.h"
00021 
00022 #include "tap.h"
00023 
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 
00027 #include "utils_base.c"
00028 
00029 int
00030 main (int argc, char **argv)
00031 {
00032        range  *range;
00033        double temp;
00034        thresholds *thresholds = NULL;
00035        int    rc;
00036        char   *temp_string;
00037        state_key *temp_state_key = NULL;
00038        state_data *temp_state_data;
00039        time_t current_time;
00040 
00041        plan_tests(141);
00042 
00043        ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
00044 
00045        np_init( "check_test", argc, argv );
00046 
00047        ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
00048        ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
00049 
00050        ok( this_nagios_plugin->argc==argc, "Argc set" );
00051        ok( this_nagios_plugin->argv==argv, "Argv set" );
00052 
00053        np_set_args(0,0);
00054 
00055        ok( this_nagios_plugin->argc==0, "argc changed" );
00056        ok( this_nagios_plugin->argv==0, "argv changed" );
00057 
00058        np_set_args(argc, argv);
00059 
00060        range = parse_range_string("6");
00061        ok( range != NULL, "'6' is valid range");
00062        ok( range->start == 0, "Start correct");
00063        ok( range->start_infinity == FALSE, "Not using negative infinity");
00064        ok( range->end == 6, "End correct");
00065        ok( range->end_infinity == FALSE, "Not using infinity");
00066        free(range);
00067 
00068        range = parse_range_string("1:12%%");
00069        ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
00070        ok( range->start == 1, "Start correct");
00071        ok( range->start_infinity == FALSE, "Not using negative infinity");
00072        ok( range->end == 12, "End correct");
00073        ok( range->end_infinity == FALSE, "Not using infinity");
00074        free(range);
00075 
00076        range = parse_range_string("-7:23");
00077        ok( range != NULL, "'-7:23' is valid range");
00078        ok( range->start == -7, "Start correct");
00079        ok( range->start_infinity == FALSE, "Not using negative infinity");
00080        ok( range->end == 23, "End correct");
00081        ok( range->end_infinity == FALSE, "Not using infinity");
00082        free(range);
00083 
00084        range = parse_range_string(":5.75");
00085        ok( range != NULL, "':5.75' is valid range");
00086        ok( range->start == 0, "Start correct");
00087        ok( range->start_infinity == FALSE, "Not using negative infinity");
00088        ok( range->end == 5.75, "End correct");
00089        ok( range->end_infinity == FALSE, "Not using infinity");
00090        free(range);
00091 
00092        range = parse_range_string("~:-95.99");
00093        ok( range != NULL, "~:-95.99' is valid range");
00094        ok( range->start_infinity == TRUE, "Using negative infinity");
00095        ok( range->end == -95.99, "End correct (with rounding errors)");
00096        ok( range->end_infinity == FALSE, "Not using infinity");
00097        free(range);
00098 
00099        range = parse_range_string("12345678901234567890:");
00100        temp = atof("12345678901234567890");             /* Can't just use this because number too large */
00101        ok( range != NULL, "'12345678901234567890:' is valid range");
00102        ok( range->start == temp, "Start correct");
00103        ok( range->start_infinity == FALSE, "Not using negative infinity");
00104        ok( range->end_infinity == TRUE, "Using infinity");
00105        /* Cannot do a "-1" on temp, as it appears to be same value */
00106        ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert");
00107        ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert");
00108        ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert");
00109        free(range);
00110 
00111        range = parse_range_string("~:0");
00112        ok( range != NULL, "'~:0' is valid range");
00113        ok( range->start_infinity == TRUE, "Using negative infinity");
00114        ok( range->end == 0, "End correct");
00115        ok( range->end_infinity == FALSE, "Not using infinity");
00116        ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
00117        ok( check_range(0.5, range) == TRUE, "0.5 - alert");
00118        ok( check_range(-10, range) == FALSE, "-10 - no alert");
00119        ok( check_range(0, range)   == FALSE, "0 - no alert");
00120        free(range);
00121        
00122        range = parse_range_string("@0:657.8210567");
00123        ok( range != 0, "@0:657.8210567' is a valid range");
00124        ok( range->start == 0, "Start correct");
00125        ok( range->start_infinity == FALSE, "Not using negative infinity");
00126        ok( range->end == 657.8210567, "End correct");
00127        ok( range->end_infinity == FALSE, "Not using infinity");
00128        ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
00129        ok( check_range(32.88, range) == TRUE, "32.88 - alert");
00130        ok( check_range(-2, range)    == FALSE, "-2 - no alert");
00131        ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert");
00132        ok( check_range(0, range)     == TRUE, "0 - alert");
00133        free(range);
00134 
00135        range = parse_range_string("1:1");
00136        ok( range != NULL, "'1:1' is a valid range");
00137        ok( range->start == 1, "Start correct");
00138        ok( range->start_infinity == FALSE, "Not using negative infinity");
00139        ok( range->end == 1, "End correct");
00140        ok( range->end_infinity == FALSE, "Not using infinity");
00141        ok( check_range(0.5, range) == TRUE, "0.5 - alert");
00142        ok( check_range(1, range) == FALSE, "1 - no alert");
00143        ok( check_range(5.2, range) == TRUE, "5.2 - alert");
00144        free(range);
00145 
00146        range = parse_range_string("2:1");
00147        ok( range == NULL, "'2:1' rejected");
00148 
00149        rc = _set_thresholds(&thresholds, NULL, NULL);
00150        ok( rc == 0, "Thresholds (NULL, NULL) set");
00151        ok( thresholds->warning == NULL, "Warning not set");
00152        ok( thresholds->critical == NULL, "Critical not set");
00153 
00154        rc = _set_thresholds(&thresholds, NULL, "80");
00155        ok( rc == 0, "Thresholds (NULL, '80') set");
00156        ok( thresholds->warning == NULL, "Warning not set");
00157        ok( thresholds->critical->end == 80, "Critical set correctly");
00158 
00159        rc = _set_thresholds(&thresholds, "5:33", NULL);
00160        ok( rc == 0, "Thresholds ('5:33', NULL) set");
00161        ok( thresholds->warning->start == 5, "Warning start set");
00162        ok( thresholds->warning->end == 33, "Warning end set");
00163        ok( thresholds->critical == NULL, "Critical not set");
00164 
00165        rc = _set_thresholds(&thresholds, "30", "60");
00166        ok( rc == 0, "Thresholds ('30', '60') set");
00167        ok( thresholds->warning->end == 30, "Warning set correctly");
00168        ok( thresholds->critical->end == 60, "Critical set correctly");
00169        ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
00170        ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
00171        ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
00172 
00173        char *test;
00174        test = np_escaped_string("bob\\n");
00175        ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
00176        free(test);
00177 
00178        test = np_escaped_string("rhuba\\rb");
00179        ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
00180        free(test);
00181 
00182        test = np_escaped_string("ba\\nge\\r");
00183        ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
00184        free(test);
00185 
00186        test = np_escaped_string("\\rabbi\\t");
00187        ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
00188        free(test);
00189 
00190        test = np_escaped_string("and\\\\or");
00191        ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
00192        free(test);
00193 
00194        test = np_escaped_string("bo\\gus");
00195        ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
00196        free(test);
00197 
00198        test = np_escaped_string("everything");
00199        ok( strcmp(test, "everything") == 0, "everything okay");
00200 
00201        /* np_extract_ntpvar tests (23) */
00202        test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
00203        ok(test && !strcmp(test, "bar"), "1st test as expected");
00204        free(test);
00205 
00206        test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
00207        ok(test && !strcmp(test, "foo"), "2nd test as expected");
00208        free(test);
00209 
00210        test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
00211        ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
00212        free(test);
00213 
00214        test=np_extract_ntpvar("foo=bar\n", "foo");
00215        ok(test && !strcmp(test, "bar"), "Single test as expected");
00216        free(test);
00217 
00218        test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
00219        ok(!test, "Key not found 1");
00220 
00221        test=np_extract_ntpvar("foo=bar\n", "abcd");
00222        ok(!test, "Key not found 2");
00223 
00224        test=np_extract_ntpvar("foo=bar=foobar", "foo");
00225        ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
00226        free(test);
00227 
00228        test=np_extract_ntpvar("foo", "foo");
00229        ok(!test, "Malformed string 1");
00230 
00231        test=np_extract_ntpvar("foo,", "foo");
00232        ok(!test, "Malformed string 2");
00233 
00234        test=np_extract_ntpvar("foo=", "foo");
00235        ok(!test, "Malformed string 3");
00236 
00237        test=np_extract_ntpvar("foo=,bar=foo", "foo");
00238        ok(!test, "Malformed string 4");
00239 
00240        test=np_extract_ntpvar(",foo", "foo");
00241        ok(!test, "Malformed string 5");
00242 
00243        test=np_extract_ntpvar("=foo", "foo");
00244        ok(!test, "Malformed string 6");
00245 
00246        test=np_extract_ntpvar("=foo,", "foo");
00247        ok(!test, "Malformed string 7");
00248 
00249        test=np_extract_ntpvar(",,,", "foo");
00250        ok(!test, "Malformed string 8");
00251 
00252        test=np_extract_ntpvar("===", "foo");
00253        ok(!test, "Malformed string 9");
00254 
00255        test=np_extract_ntpvar(",=,=,", "foo");
00256        ok(!test, "Malformed string 10");
00257 
00258        test=np_extract_ntpvar("=,=,=", "foo");
00259        ok(!test, "Malformed string 11");
00260 
00261        test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foo");
00262        ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
00263        free(test);
00264 
00265        test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "bar");
00266        ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
00267        free(test);
00268 
00269        test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foobar");
00270        ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
00271        free(test);
00272 
00273        test=np_extract_ntpvar("  foo=bar  ,\n bar\n \n= \n foo\n , foobar=barfoo  \n  ", "bar");
00274        ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
00275        free(test);
00276 
00277        test=np_extract_ntpvar("", "foo");
00278        ok(!test, "Empty string return NULL");
00279 
00280 
00281        /* This is the result of running ./test_utils */
00282        temp_string = (char *) _np_state_generate_key();
00283        ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ) || 
00284         diag( "You are probably running in wrong directory. Must run as ./test_utils" );
00285 
00286 
00287        this_nagios_plugin->argc=4;
00288        this_nagios_plugin->argv[0] = "./test_utils";
00289        this_nagios_plugin->argv[1] = "here";
00290        this_nagios_plugin->argv[2] = "--and";
00291        this_nagios_plugin->argv[3] = "now";
00292        temp_string = (char *) _np_state_generate_key();
00293        ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
00294 
00295        unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
00296        temp_string = (char *) _np_state_calculate_location_prefix();
00297        ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
00298 
00299        setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
00300        temp_string = (char *) _np_state_calculate_location_prefix();
00301        ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
00302 
00303        setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
00304        temp_string = (char *) _np_state_calculate_location_prefix();
00305        ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
00306 
00307 
00308 
00309        ok(temp_state_key==NULL, "temp_state_key initially empty");
00310 
00311        this_nagios_plugin->argc=1;
00312        this_nagios_plugin->argv[0] = "./test_utils";
00313        np_enable_state(NULL, 51);
00314        temp_state_key = this_nagios_plugin->state;
00315        ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
00316        ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
00317 
00318 
00319        np_enable_state("allowedchars_in_keyname", 77);
00320        temp_state_key = this_nagios_plugin->state;
00321        ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
00322        ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
00323        ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
00324 
00325 
00326        /* Don't do this test just yet. Will die */
00327        /*
00328        np_enable_state("bad^chars$in@here", 77);
00329        temp_state_key = this_nagios_plugin->state;
00330        ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
00331        */
00332 
00333        np_enable_state("funnykeyname", 54);
00334        temp_state_key = this_nagios_plugin->state;
00335        ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
00336        ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
00337 
00338 
00339 
00340        ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
00341        ok( temp_state_key->data_version==54, "Version set" );
00342 
00343        temp_state_data = np_state_read();
00344        ok( temp_state_data==NULL, "Got no state data as file does not exist" );
00345 
00346 
00347 /*
00348        temp_fp = fopen("var/statefile", "r");
00349        if (temp_fp==NULL) 
00350               printf("Error opening. errno=%d\n", errno);
00351        printf("temp_fp=%s\n", temp_fp);
00352        ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
00353        fclose(temp_fp);
00354 */
00355        
00356        temp_state_key->_filename="var/statefile";
00357        temp_state_data = np_state_read();
00358        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");
00359        ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" );
00360        ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
00361 
00362        temp_state_key->data_version=53;
00363        temp_state_data = np_state_read();
00364        ok( temp_state_data==NULL, "Older data version gives NULL" );
00365        temp_state_key->data_version=54;
00366 
00367        temp_state_key->_filename="var/nonexistant";
00368        temp_state_data = np_state_read();
00369        ok( temp_state_data==NULL, "Missing file gives NULL" );
00370        ok( this_nagios_plugin->state->state_data==NULL, "No state information" );
00371 
00372        temp_state_key->_filename="var/oldformat";
00373        temp_state_data = np_state_read();
00374        ok( temp_state_data==NULL, "Old file format gives NULL" );
00375 
00376        temp_state_key->_filename="var/baddate";
00377        temp_state_data = np_state_read();
00378        ok( temp_state_data==NULL, "Bad date gives NULL" );
00379 
00380        temp_state_key->_filename="var/missingdataline";
00381        temp_state_data = np_state_read();
00382        ok( temp_state_data==NULL, "Missing data line gives NULL" );
00383 
00384 
00385 
00386 
00387        unlink("var/generated");
00388        temp_state_key->_filename="var/generated";
00389        current_time=1234567890;
00390        np_state_write_string(current_time, "String to read");
00391        ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
00392 
00393 
00394 
00395 
00396        unlink("var/generated_directory/statefile");
00397        unlink("var/generated_directory");
00398        temp_state_key->_filename="var/generated_directory/statefile";
00399        current_time=1234567890;
00400        np_state_write_string(current_time, "String to read");
00401        ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
00402 
00403        /* This test to check cannot write to dir - can't automate yet */
00404        /*
00405        unlink("var/generated_bad_dir");
00406        mkdir("var/generated_bad_dir", S_IRUSR);
00407        np_state_write_string(current_time, "String to read");
00408        */
00409 
00410 
00411        temp_state_key->_filename="var/generated";
00412        time(&current_time);
00413        np_state_write_string(0, "String to read");
00414        temp_state_data = np_state_read();
00415        /* Check time is set to current_time */
00416        ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
00417        ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
00418        
00419 
00420        /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
00421        /*
00422        temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
00423        np_state_write_string(0, "Bad file");
00424        */
00425        
00426 
00427        np_cleanup();
00428 
00429        ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
00430 
00431        return exit_status();
00432 }
00433