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;
41 plan_tests(141);
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
45 np_init( "check_test", argc, argv );
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
50 ok( this_nagios_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" );
53 np_set_args(0,0);
55 ok( this_nagios_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" );
58 np_set_args(argc, argv);
60 range = parse_range_string("6");
61 ok( range != NULL, "'6' is valid range");
62 ok( range->start == 0, "Start correct");
63 ok( range->start_infinity == FALSE, "Not using negative infinity");
64 ok( range->end == 6, "End correct");
65 ok( range->end_infinity == FALSE, "Not using infinity");
66 free(range);
68 range = parse_range_string("1:12%%");
69 ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
70 ok( range->start == 1, "Start correct");
71 ok( range->start_infinity == FALSE, "Not using negative infinity");
72 ok( range->end == 12, "End correct");
73 ok( range->end_infinity == FALSE, "Not using infinity");
74 free(range);
76 range = parse_range_string("-7:23");
77 ok( range != NULL, "'-7:23' is valid range");
78 ok( range->start == -7, "Start correct");
79 ok( range->start_infinity == FALSE, "Not using negative infinity");
80 ok( range->end == 23, "End correct");
81 ok( range->end_infinity == FALSE, "Not using infinity");
82 free(range);
84 range = parse_range_string(":5.75");
85 ok( range != NULL, "':5.75' is valid range");
86 ok( range->start == 0, "Start correct");
87 ok( range->start_infinity == FALSE, "Not using negative infinity");
88 ok( range->end == 5.75, "End correct");
89 ok( range->end_infinity == FALSE, "Not using infinity");
90 free(range);
92 range = parse_range_string("~:-95.99");
93 ok( range != NULL, "~:-95.99' is valid range");
94 ok( range->start_infinity == TRUE, "Using negative infinity");
95 ok( range->end == -95.99, "End correct (with rounding errors)");
96 ok( range->end_infinity == FALSE, "Not using infinity");
97 free(range);
99 range = parse_range_string("12345678901234567890:");
100 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
101 ok( range != NULL, "'12345678901234567890:' is valid range");
102 ok( range->start == temp, "Start correct");
103 ok( range->start_infinity == FALSE, "Not using negative infinity");
104 ok( range->end_infinity == TRUE, "Using infinity");
105 /* Cannot do a "-1" on temp, as it appears to be same value */
106 ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert");
107 ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert");
108 ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert");
109 free(range);
111 range = parse_range_string("~:0");
112 ok( range != NULL, "'~:0' is valid range");
113 ok( range->start_infinity == TRUE, "Using negative infinity");
114 ok( range->end == 0, "End correct");
115 ok( range->end_infinity == FALSE, "Not using infinity");
116 ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
117 ok( check_range(0.5, range) == TRUE, "0.5 - alert");
118 ok( check_range(-10, range) == FALSE, "-10 - no alert");
119 ok( check_range(0, range) == FALSE, "0 - no alert");
120 free(range);
122 range = parse_range_string("@0:657.8210567");
123 ok( range != 0, "@0:657.8210567' is a valid range");
124 ok( range->start == 0, "Start correct");
125 ok( range->start_infinity == FALSE, "Not using negative infinity");
126 ok( range->end == 657.8210567, "End correct");
127 ok( range->end_infinity == FALSE, "Not using infinity");
128 ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
129 ok( check_range(32.88, range) == TRUE, "32.88 - alert");
130 ok( check_range(-2, range) == FALSE, "-2 - no alert");
131 ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert");
132 ok( check_range(0, range) == TRUE, "0 - alert");
133 free(range);
135 range = parse_range_string("1:1");
136 ok( range != NULL, "'1:1' is a valid range");
137 ok( range->start == 1, "Start correct");
138 ok( range->start_infinity == FALSE, "Not using negative infinity");
139 ok( range->end == 1, "End correct");
140 ok( range->end_infinity == FALSE, "Not using infinity");
141 ok( check_range(0.5, range) == TRUE, "0.5 - alert");
142 ok( check_range(1, range) == FALSE, "1 - no alert");
143 ok( check_range(5.2, range) == TRUE, "5.2 - alert");
144 free(range);
146 range = parse_range_string("2:1");
147 ok( range == NULL, "'2:1' rejected");
149 rc = _set_thresholds(&thresholds, NULL, NULL);
150 ok( rc == 0, "Thresholds (NULL, NULL) set");
151 ok( thresholds->warning == NULL, "Warning not set");
152 ok( thresholds->critical == NULL, "Critical not set");
154 rc = _set_thresholds(&thresholds, NULL, "80");
155 ok( rc == 0, "Thresholds (NULL, '80') set");
156 ok( thresholds->warning == NULL, "Warning not set");
157 ok( thresholds->critical->end == 80, "Critical set correctly");
159 rc = _set_thresholds(&thresholds, "5:33", NULL);
160 ok( rc == 0, "Thresholds ('5:33', NULL) set");
161 ok( thresholds->warning->start == 5, "Warning start set");
162 ok( thresholds->warning->end == 33, "Warning end set");
163 ok( thresholds->critical == NULL, "Critical not set");
165 rc = _set_thresholds(&thresholds, "30", "60");
166 ok( rc == 0, "Thresholds ('30', '60') set");
167 ok( thresholds->warning->end == 30, "Warning set correctly");
168 ok( thresholds->critical->end == 60, "Critical set correctly");
169 ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
170 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
171 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
173 char *test;
174 test = np_escaped_string("bob\\n");
175 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
176 free(test);
178 test = np_escaped_string("rhuba\\rb");
179 ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
180 free(test);
182 test = np_escaped_string("ba\\nge\\r");
183 ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
184 free(test);
186 test = np_escaped_string("\\rabbi\\t");
187 ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
188 free(test);
190 test = np_escaped_string("and\\\\or");
191 ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
192 free(test);
194 test = np_escaped_string("bo\\gus");
195 ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
196 free(test);
198 test = np_escaped_string("everything");
199 ok( strcmp(test, "everything") == 0, "everything okay");
201 /* np_extract_ntpvar tests (23) */
202 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
203 ok(test && !strcmp(test, "bar"), "1st test as expected");
204 free(test);
206 test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
207 ok(test && !strcmp(test, "foo"), "2nd test as expected");
208 free(test);
210 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
211 ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
212 free(test);
214 test=np_extract_ntpvar("foo=bar\n", "foo");
215 ok(test && !strcmp(test, "bar"), "Single test as expected");
216 free(test);
218 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
219 ok(!test, "Key not found 1");
221 test=np_extract_ntpvar("foo=bar\n", "abcd");
222 ok(!test, "Key not found 2");
224 test=np_extract_ntpvar("foo=bar=foobar", "foo");
225 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
226 free(test);
228 test=np_extract_ntpvar("foo", "foo");
229 ok(!test, "Malformed string 1");
231 test=np_extract_ntpvar("foo,", "foo");
232 ok(!test, "Malformed string 2");
234 test=np_extract_ntpvar("foo=", "foo");
235 ok(!test, "Malformed string 3");
237 test=np_extract_ntpvar("foo=,bar=foo", "foo");
238 ok(!test, "Malformed string 4");
240 test=np_extract_ntpvar(",foo", "foo");
241 ok(!test, "Malformed string 5");
243 test=np_extract_ntpvar("=foo", "foo");
244 ok(!test, "Malformed string 6");
246 test=np_extract_ntpvar("=foo,", "foo");
247 ok(!test, "Malformed string 7");
249 test=np_extract_ntpvar(",,,", "foo");
250 ok(!test, "Malformed string 8");
252 test=np_extract_ntpvar("===", "foo");
253 ok(!test, "Malformed string 9");
255 test=np_extract_ntpvar(",=,=,", "foo");
256 ok(!test, "Malformed string 10");
258 test=np_extract_ntpvar("=,=,=", "foo");
259 ok(!test, "Malformed string 11");
261 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
262 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
263 free(test);
265 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
266 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
267 free(test);
269 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
270 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
271 free(test);
273 test=np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
274 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
275 free(test);
277 test=np_extract_ntpvar("", "foo");
278 ok(!test, "Empty string return NULL");
281 /* This is the result of running ./test_utils */
282 temp_string = (char *) _np_state_generate_key();
283 ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ) ||
284 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
287 this_nagios_plugin->argc=4;
288 this_nagios_plugin->argv[0] = "./test_utils";
289 this_nagios_plugin->argv[1] = "here";
290 this_nagios_plugin->argv[2] = "--and";
291 this_nagios_plugin->argv[3] = "now";
292 temp_string = (char *) _np_state_generate_key();
293 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
295 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
296 temp_string = (char *) _np_state_calculate_location_prefix();
297 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
299 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
300 temp_string = (char *) _np_state_calculate_location_prefix();
301 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
303 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
304 temp_string = (char *) _np_state_calculate_location_prefix();
305 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
309 ok(temp_state_key==NULL, "temp_state_key initially empty");
311 this_nagios_plugin->argc=1;
312 this_nagios_plugin->argv[0] = "./test_utils";
313 np_enable_state(NULL, 51);
314 temp_state_key = this_nagios_plugin->state;
315 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
316 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
319 np_enable_state("allowedchars_in_keyname", 77);
320 temp_state_key = this_nagios_plugin->state;
321 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
322 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
323 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
326 /* Don't do this test just yet. Will die */
327 /*
328 np_enable_state("bad^chars$in@here", 77);
329 temp_state_key = this_nagios_plugin->state;
330 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
331 */
333 np_enable_state("funnykeyname", 54);
334 temp_state_key = this_nagios_plugin->state;
335 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
336 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
340 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
341 ok( temp_state_key->data_version==54, "Version set" );
343 temp_state_data = np_state_read();
344 ok( temp_state_data==NULL, "Got no state data as file does not exist" );
347 /*
348 temp_fp = fopen("var/statefile", "r");
349 if (temp_fp==NULL)
350 printf("Error opening. errno=%d\n", errno);
351 printf("temp_fp=%s\n", temp_fp);
352 ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
353 fclose(temp_fp);
354 */
356 temp_state_key->_filename="var/statefile";
357 temp_state_data = np_state_read();
358 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");
359 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" );
360 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
362 temp_state_key->data_version=53;
363 temp_state_data = np_state_read();
364 ok( temp_state_data==NULL, "Older data version gives NULL" );
365 temp_state_key->data_version=54;
367 temp_state_key->_filename="var/nonexistant";
368 temp_state_data = np_state_read();
369 ok( temp_state_data==NULL, "Missing file gives NULL" );
370 ok( this_nagios_plugin->state->state_data==NULL, "No state information" );
372 temp_state_key->_filename="var/oldformat";
373 temp_state_data = np_state_read();
374 ok( temp_state_data==NULL, "Old file format gives NULL" );
376 temp_state_key->_filename="var/baddate";
377 temp_state_data = np_state_read();
378 ok( temp_state_data==NULL, "Bad date gives NULL" );
380 temp_state_key->_filename="var/missingdataline";
381 temp_state_data = np_state_read();
382 ok( temp_state_data==NULL, "Missing data line gives NULL" );
387 unlink("var/generated");
388 temp_state_key->_filename="var/generated";
389 current_time=1234567890;
390 np_state_write_string(current_time, "String to read");
391 ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
396 unlink("var/generated_directory/statefile");
397 unlink("var/generated_directory");
398 temp_state_key->_filename="var/generated_directory/statefile";
399 current_time=1234567890;
400 np_state_write_string(current_time, "String to read");
401 ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
403 /* This test to check cannot write to dir - can't automate yet */
404 /*
405 unlink("var/generated_bad_dir");
406 mkdir("var/generated_bad_dir", S_IRUSR);
407 np_state_write_string(current_time, "String to read");
408 */
411 temp_state_key->_filename="var/generated";
412 time(¤t_time);
413 np_state_write_string(0, "String to read");
414 temp_state_data = np_state_read();
415 /* Check time is set to current_time */
416 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
417 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
420 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
421 /*
422 temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
423 np_state_write_string(0, "Bad file");
424 */
427 np_cleanup();
429 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
431 return exit_status();
432 }