Code

Added state retention APIs. Implemented for check_snmp with --rate option.
[nagiosplug.git] / lib / tests / test_utils.c
1 /*****************************************************************************
2
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16
17 *****************************************************************************/
19 #include "common.h"
20 #include "utils_base.h"
22 #include "tap.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include "utils_base.c"
29 int
30 main (int argc, char **argv)
31 {
32         range   *range;
33         double  temp;
34         thresholds *thresholds = NULL;
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;
43         plan_tests(141);
45         ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
47         np_init( "check_test", argc, argv );
49         ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
50         ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
52         ok( this_nagios_plugin->argc==argc, "Argc set" );
53         ok( this_nagios_plugin->argv==argv, "Argv set" );
55         np_set_args(0,0);
57         ok( this_nagios_plugin->argc==0, "argc changed" );
58         ok( this_nagios_plugin->argv==0, "argv changed" );
60         np_set_args(argc, argv);
62         range = parse_range_string("6");
63         ok( range != NULL, "'6' is valid range");
64         ok( range->start == 0, "Start correct");
65         ok( range->start_infinity == FALSE, "Not using negative infinity");
66         ok( range->end == 6, "End correct");
67         ok( range->end_infinity == FALSE, "Not using infinity");
68         free(range);
70         range = parse_range_string("1:12%%");
71         ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
72         ok( range->start == 1, "Start correct");
73         ok( range->start_infinity == FALSE, "Not using negative infinity");
74         ok( range->end == 12, "End correct");
75         ok( range->end_infinity == FALSE, "Not using infinity");
76         free(range);
78         range = parse_range_string("-7:23");
79         ok( range != NULL, "'-7:23' is valid range");
80         ok( range->start == -7, "Start correct");
81         ok( range->start_infinity == FALSE, "Not using negative infinity");
82         ok( range->end == 23, "End correct");
83         ok( range->end_infinity == FALSE, "Not using infinity");
84         free(range);
86         range = parse_range_string(":5.75");
87         ok( range != NULL, "':5.75' is valid range");
88         ok( range->start == 0, "Start correct");
89         ok( range->start_infinity == FALSE, "Not using negative infinity");
90         ok( range->end == 5.75, "End correct");
91         ok( range->end_infinity == FALSE, "Not using infinity");
92         free(range);
94         range = parse_range_string("~:-95.99");
95         ok( range != NULL, "~:-95.99' is valid range");
96         ok( range->start_infinity == TRUE, "Using negative infinity");
97         ok( range->end == -95.99, "End correct (with rounding errors)");
98         ok( range->end_infinity == FALSE, "Not using infinity");
99         free(range);
101         range = parse_range_string("12345678901234567890:");
102         temp = atof("12345678901234567890");            /* Can't just use this because number too large */
103         ok( range != NULL, "'12345678901234567890:' is valid range");
104         ok( range->start == temp, "Start correct");
105         ok( range->start_infinity == FALSE, "Not using negative infinity");
106         ok( range->end_infinity == TRUE, "Using infinity");
107         /* Cannot do a "-1" on temp, as it appears to be same value */
108         ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert");
109         ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert");
110         ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert");
111         free(range);
113         range = parse_range_string("~:0");
114         ok( range != NULL, "'~:0' is valid range");
115         ok( range->start_infinity == TRUE, "Using negative infinity");
116         ok( range->end == 0, "End correct");
117         ok( range->end_infinity == FALSE, "Not using infinity");
118         ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
119         ok( check_range(0.5, range) == TRUE, "0.5 - alert");
120         ok( check_range(-10, range) == FALSE, "-10 - no alert");
121         ok( check_range(0, range)   == FALSE, "0 - no alert");
122         free(range);
123         
124         range = parse_range_string("@0:657.8210567");
125         ok( range != 0, "@0:657.8210567' is a valid range");
126         ok( range->start == 0, "Start correct");
127         ok( range->start_infinity == FALSE, "Not using negative infinity");
128         ok( range->end == 657.8210567, "End correct");
129         ok( range->end_infinity == FALSE, "Not using infinity");
130         ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
131         ok( check_range(32.88, range) == TRUE, "32.88 - alert");
132         ok( check_range(-2, range)    == FALSE, "-2 - no alert");
133         ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert");
134         ok( check_range(0, range)     == TRUE, "0 - alert");
135         free(range);
137         range = parse_range_string("1:1");
138         ok( range != NULL, "'1:1' is a valid range");
139         ok( range->start == 1, "Start correct");
140         ok( range->start_infinity == FALSE, "Not using negative infinity");
141         ok( range->end == 1, "End correct");
142         ok( range->end_infinity == FALSE, "Not using infinity");
143         ok( check_range(0.5, range) == TRUE, "0.5 - alert");
144         ok( check_range(1, range) == FALSE, "1 - no alert");
145         ok( check_range(5.2, range) == TRUE, "5.2 - alert");
146         free(range);
148         range = parse_range_string("2:1");
149         ok( range == NULL, "'2:1' rejected");
151         rc = _set_thresholds(&thresholds, NULL, NULL);
152         ok( rc == 0, "Thresholds (NULL, NULL) set");
153         ok( thresholds->warning == NULL, "Warning not set");
154         ok( thresholds->critical == NULL, "Critical not set");
156         rc = _set_thresholds(&thresholds, NULL, "80");
157         ok( rc == 0, "Thresholds (NULL, '80') set");
158         ok( thresholds->warning == NULL, "Warning not set");
159         ok( thresholds->critical->end == 80, "Critical set correctly");
161         rc = _set_thresholds(&thresholds, "5:33", NULL);
162         ok( rc == 0, "Thresholds ('5:33', NULL) set");
163         ok( thresholds->warning->start == 5, "Warning start set");
164         ok( thresholds->warning->end == 33, "Warning end set");
165         ok( thresholds->critical == NULL, "Critical not set");
167         rc = _set_thresholds(&thresholds, "30", "60");
168         ok( rc == 0, "Thresholds ('30', '60') set");
169         ok( thresholds->warning->end == 30, "Warning set correctly");
170         ok( thresholds->critical->end == 60, "Critical set correctly");
171         ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
172         ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
173         ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
175         char *test;
176         test = np_escaped_string("bob\\n");
177         ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
178         free(test);
180         test = np_escaped_string("rhuba\\rb");
181         ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
182         free(test);
184         test = np_escaped_string("ba\\nge\\r");
185         ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
186         free(test);
188         test = np_escaped_string("\\rabbi\\t");
189         ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
190         free(test);
192         test = np_escaped_string("and\\\\or");
193         ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
194         free(test);
196         test = np_escaped_string("bo\\gus");
197         ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
198         free(test);
200         test = np_escaped_string("everything");
201         ok( strcmp(test, "everything") == 0, "everything okay");
203         /* np_extract_ntpvar tests (23) */
204         test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
205         ok(test && !strcmp(test, "bar"), "1st test as expected");
206         free(test);
208         test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
209         ok(test && !strcmp(test, "foo"), "2nd test as expected");
210         free(test);
212         test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
213         ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
214         free(test);
216         test=np_extract_ntpvar("foo=bar\n", "foo");
217         ok(test && !strcmp(test, "bar"), "Single test as expected");
218         free(test);
220         test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
221         ok(!test, "Key not found 1");
223         test=np_extract_ntpvar("foo=bar\n", "abcd");
224         ok(!test, "Key not found 2");
226         test=np_extract_ntpvar("foo=bar=foobar", "foo");
227         ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
228         free(test);
230         test=np_extract_ntpvar("foo", "foo");
231         ok(!test, "Malformed string 1");
233         test=np_extract_ntpvar("foo,", "foo");
234         ok(!test, "Malformed string 2");
236         test=np_extract_ntpvar("foo=", "foo");
237         ok(!test, "Malformed string 3");
239         test=np_extract_ntpvar("foo=,bar=foo", "foo");
240         ok(!test, "Malformed string 4");
242         test=np_extract_ntpvar(",foo", "foo");
243         ok(!test, "Malformed string 5");
245         test=np_extract_ntpvar("=foo", "foo");
246         ok(!test, "Malformed string 6");
248         test=np_extract_ntpvar("=foo,", "foo");
249         ok(!test, "Malformed string 7");
251         test=np_extract_ntpvar(",,,", "foo");
252         ok(!test, "Malformed string 8");
254         test=np_extract_ntpvar("===", "foo");
255         ok(!test, "Malformed string 9");
257         test=np_extract_ntpvar(",=,=,", "foo");
258         ok(!test, "Malformed string 10");
260         test=np_extract_ntpvar("=,=,=", "foo");
261         ok(!test, "Malformed string 11");
263         test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foo");
264         ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
265         free(test);
267         test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "bar");
268         ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
269         free(test);
271         test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foobar");
272         ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
273         free(test);
275         test=np_extract_ntpvar("  foo=bar  ,\n bar\n \n= \n foo\n , foobar=barfoo  \n  ", "bar");
276         ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
277         free(test);
279         test=np_extract_ntpvar("", "foo");
280         ok(!test, "Empty string return NULL");
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" );
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" );
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" );
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" );
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" );
311         ok(temp_state_key==NULL, "temp_state_key initially empty");
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" );
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" );
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         */
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" );
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" );
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" );
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" );
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;
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" );
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" );
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" );
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" );
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");
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");
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         */
413         temp_state_key->_filename="var/generated";
414         time(&current_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         
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         
429         np_cleanup();
431         ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
433         return exit_status();