4
0

test_utils.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. *****************************************************************************/
  18. #include "common.h"
  19. #include "utils_base.h"
  20. #include "tap.h"
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include "utils_base.c"
  24. int
  25. main (int argc, char **argv)
  26. {
  27. range *range;
  28. double temp;
  29. thresholds *thresholds = NULL;
  30. int rc;
  31. char *temp_string;
  32. state_key *temp_state_key = NULL;
  33. state_data *temp_state_data;
  34. time_t current_time;
  35. plan_tests(150);
  36. ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
  37. np_init( "check_test", argc, argv );
  38. ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
  39. ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
  40. ok( this_nagios_plugin->argc==argc, "Argc set" );
  41. ok( this_nagios_plugin->argv==argv, "Argv set" );
  42. np_set_args(0,0);
  43. ok( this_nagios_plugin->argc==0, "argc changed" );
  44. ok( this_nagios_plugin->argv==0, "argv changed" );
  45. np_set_args(argc, argv);
  46. range = parse_range_string("6");
  47. ok( range != NULL, "'6' is valid range");
  48. ok( range->start == 0, "Start correct");
  49. ok( range->start_infinity == FALSE, "Not using negative infinity");
  50. ok( range->end == 6, "End correct");
  51. ok( range->end_infinity == FALSE, "Not using infinity");
  52. free(range);
  53. range = parse_range_string("1:12%%");
  54. ok( range != NULL, "'1:12%%' is valid - percentages are ignored");
  55. ok( range->start == 1, "Start correct");
  56. ok( range->start_infinity == FALSE, "Not using negative infinity");
  57. ok( range->end == 12, "End correct");
  58. ok( range->end_infinity == FALSE, "Not using infinity");
  59. free(range);
  60. range = parse_range_string("-7:23");
  61. ok( range != NULL, "'-7:23' is valid range");
  62. ok( range->start == -7, "Start correct");
  63. ok( range->start_infinity == FALSE, "Not using negative infinity");
  64. ok( range->end == 23, "End correct");
  65. ok( range->end_infinity == FALSE, "Not using infinity");
  66. free(range);
  67. range = parse_range_string(":5.75");
  68. ok( range != NULL, "':5.75' is valid range");
  69. ok( range->start == 0, "Start correct");
  70. ok( range->start_infinity == FALSE, "Not using negative infinity");
  71. ok( range->end == 5.75, "End correct");
  72. ok( range->end_infinity == FALSE, "Not using infinity");
  73. free(range);
  74. range = parse_range_string("~:-95.99");
  75. ok( range != NULL, "~:-95.99' is valid range");
  76. ok( range->start_infinity == TRUE, "Using negative infinity");
  77. ok( range->end == -95.99, "End correct (with rounding errors)");
  78. ok( range->end_infinity == FALSE, "Not using infinity");
  79. free(range);
  80. range = parse_range_string("12345678901234567890:");
  81. temp = atof("12345678901234567890"); /* Can't just use this because number too large */
  82. ok( range != NULL, "'12345678901234567890:' is valid range");
  83. ok( range->start == temp, "Start correct");
  84. ok( range->start_infinity == FALSE, "Not using negative infinity");
  85. ok( range->end_infinity == TRUE, "Using infinity");
  86. /* Cannot do a "-1" on temp, as it appears to be same value */
  87. ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert");
  88. ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert");
  89. ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert");
  90. free(range);
  91. range = parse_range_string("~:0");
  92. ok( range != NULL, "'~:0' is valid range");
  93. ok( range->start_infinity == TRUE, "Using negative infinity");
  94. ok( range->end == 0, "End correct");
  95. ok( range->end_infinity == FALSE, "Not using infinity");
  96. ok( range->alert_on == OUTSIDE, "Will alert on outside of this range");
  97. ok( check_range(0.5, range) == TRUE, "0.5 - alert");
  98. ok( check_range(-10, range) == FALSE, "-10 - no alert");
  99. ok( check_range(0, range) == FALSE, "0 - no alert");
  100. free(range);
  101. range = parse_range_string("@0:657.8210567");
  102. ok( range != 0, "@0:657.8210567' is a valid range");
  103. ok( range->start == 0, "Start correct");
  104. ok( range->start_infinity == FALSE, "Not using negative infinity");
  105. ok( range->end == 657.8210567, "End correct");
  106. ok( range->end_infinity == FALSE, "Not using infinity");
  107. ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
  108. ok( check_range(32.88, range) == TRUE, "32.88 - alert");
  109. ok( check_range(-2, range) == FALSE, "-2 - no alert");
  110. ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert");
  111. ok( check_range(0, range) == TRUE, "0 - alert");
  112. free(range);
  113. range = parse_range_string("@1:1");
  114. ok( range != NULL, "'@1:1' is a valid range");
  115. ok( range->start == 1, "Start correct");
  116. ok( range->start_infinity == FALSE, "Not using negative infinity");
  117. ok( range->end == 1, "End correct");
  118. ok( range->end_infinity == FALSE, "Not using infinity");
  119. ok( range->alert_on == INSIDE, "Will alert on inside of this range" );
  120. ok( check_range(0.5, range) == FALSE, "0.5 - no alert");
  121. ok( check_range(1, range) == TRUE, "1 - alert");
  122. ok( check_range(5.2, range) == FALSE, "5.2 - no alert");
  123. free(range);
  124. range = parse_range_string("1:1");
  125. ok( range != NULL, "'1:1' is a valid range");
  126. ok( range->start == 1, "Start correct");
  127. ok( range->start_infinity == FALSE, "Not using negative infinity");
  128. ok( range->end == 1, "End correct");
  129. ok( range->end_infinity == FALSE, "Not using infinity");
  130. ok( check_range(0.5, range) == TRUE, "0.5 - alert");
  131. ok( check_range(1, range) == FALSE, "1 - no alert");
  132. ok( check_range(5.2, range) == TRUE, "5.2 - alert");
  133. free(range);
  134. range = parse_range_string("2:1");
  135. ok( range == NULL, "'2:1' rejected");
  136. rc = _set_thresholds(&thresholds, NULL, NULL);
  137. ok( rc == 0, "Thresholds (NULL, NULL) set");
  138. ok( thresholds->warning == NULL, "Warning not set");
  139. ok( thresholds->critical == NULL, "Critical not set");
  140. rc = _set_thresholds(&thresholds, NULL, "80");
  141. ok( rc == 0, "Thresholds (NULL, '80') set");
  142. ok( thresholds->warning == NULL, "Warning not set");
  143. ok( thresholds->critical->end == 80, "Critical set correctly");
  144. rc = _set_thresholds(&thresholds, "5:33", NULL);
  145. ok( rc == 0, "Thresholds ('5:33', NULL) set");
  146. ok( thresholds->warning->start == 5, "Warning start set");
  147. ok( thresholds->warning->end == 33, "Warning end set");
  148. ok( thresholds->critical == NULL, "Critical not set");
  149. rc = _set_thresholds(&thresholds, "30", "60");
  150. ok( rc == 0, "Thresholds ('30', '60') set");
  151. ok( thresholds->warning->end == 30, "Warning set correctly");
  152. ok( thresholds->critical->end == 60, "Critical set correctly");
  153. ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
  154. ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
  155. ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
  156. char *test;
  157. test = np_escaped_string("bob\\n");
  158. ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
  159. free(test);
  160. test = np_escaped_string("rhuba\\rb");
  161. ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
  162. free(test);
  163. test = np_escaped_string("ba\\nge\\r");
  164. ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
  165. free(test);
  166. test = np_escaped_string("\\rabbi\\t");
  167. ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
  168. free(test);
  169. test = np_escaped_string("and\\\\or");
  170. ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
  171. free(test);
  172. test = np_escaped_string("bo\\gus");
  173. ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
  174. free(test);
  175. test = np_escaped_string("everything");
  176. ok( strcmp(test, "everything") == 0, "everything okay");
  177. /* np_extract_ntpvar tests (23) */
  178. test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
  179. ok(test && !strcmp(test, "bar"), "1st test as expected");
  180. free(test);
  181. test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
  182. ok(test && !strcmp(test, "foo"), "2nd test as expected");
  183. free(test);
  184. test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
  185. ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
  186. free(test);
  187. test=np_extract_ntpvar("foo=bar\n", "foo");
  188. ok(test && !strcmp(test, "bar"), "Single test as expected");
  189. free(test);
  190. test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
  191. ok(!test, "Key not found 1");
  192. test=np_extract_ntpvar("foo=bar\n", "abcd");
  193. ok(!test, "Key not found 2");
  194. test=np_extract_ntpvar("foo=bar=foobar", "foo");
  195. ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
  196. free(test);
  197. test=np_extract_ntpvar("foo", "foo");
  198. ok(!test, "Malformed string 1");
  199. test=np_extract_ntpvar("foo,", "foo");
  200. ok(!test, "Malformed string 2");
  201. test=np_extract_ntpvar("foo=", "foo");
  202. ok(!test, "Malformed string 3");
  203. test=np_extract_ntpvar("foo=,bar=foo", "foo");
  204. ok(!test, "Malformed string 4");
  205. test=np_extract_ntpvar(",foo", "foo");
  206. ok(!test, "Malformed string 5");
  207. test=np_extract_ntpvar("=foo", "foo");
  208. ok(!test, "Malformed string 6");
  209. test=np_extract_ntpvar("=foo,", "foo");
  210. ok(!test, "Malformed string 7");
  211. test=np_extract_ntpvar(",,,", "foo");
  212. ok(!test, "Malformed string 8");
  213. test=np_extract_ntpvar("===", "foo");
  214. ok(!test, "Malformed string 9");
  215. test=np_extract_ntpvar(",=,=,", "foo");
  216. ok(!test, "Malformed string 10");
  217. test=np_extract_ntpvar("=,=,=", "foo");
  218. ok(!test, "Malformed string 11");
  219. test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
  220. ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
  221. free(test);
  222. test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
  223. ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
  224. free(test);
  225. test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
  226. ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
  227. free(test);
  228. test=np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
  229. ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
  230. free(test);
  231. test=np_extract_ntpvar("", "foo");
  232. ok(!test, "Empty string return NULL");
  233. /* This is the result of running ./test_utils */
  234. temp_string = (char *) _np_state_generate_key();
  235. ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ) ||
  236. diag( "You are probably running in wrong directory. Must run as ./test_utils" );
  237. this_nagios_plugin->argc=4;
  238. this_nagios_plugin->argv[0] = "./test_utils";
  239. this_nagios_plugin->argv[1] = "here";
  240. this_nagios_plugin->argv[2] = "--and";
  241. this_nagios_plugin->argv[3] = "now";
  242. temp_string = (char *) _np_state_generate_key();
  243. ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
  244. unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
  245. temp_string = (char *) _np_state_calculate_location_prefix();
  246. ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
  247. setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
  248. temp_string = (char *) _np_state_calculate_location_prefix();
  249. ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
  250. setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
  251. temp_string = (char *) _np_state_calculate_location_prefix();
  252. ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
  253. ok(temp_state_key==NULL, "temp_state_key initially empty");
  254. this_nagios_plugin->argc=1;
  255. this_nagios_plugin->argv[0] = "./test_utils";
  256. np_enable_state(NULL, 51);
  257. temp_state_key = this_nagios_plugin->state;
  258. ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
  259. ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
  260. np_enable_state("allowedchars_in_keyname", 77);
  261. temp_state_key = this_nagios_plugin->state;
  262. ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
  263. ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
  264. ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
  265. /* Don't do this test just yet. Will die */
  266. /*
  267. np_enable_state("bad^chars$in@here", 77);
  268. temp_state_key = this_nagios_plugin->state;
  269. ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
  270. */
  271. np_enable_state("funnykeyname", 54);
  272. temp_state_key = this_nagios_plugin->state;
  273. ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
  274. ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
  275. ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
  276. ok( temp_state_key->data_version==54, "Version set" );
  277. temp_state_data = np_state_read();
  278. ok( temp_state_data==NULL, "Got no state data as file does not exist" );
  279. /*
  280. temp_fp = fopen("var/statefile", "r");
  281. if (temp_fp==NULL)
  282. printf("Error opening. errno=%d\n", errno);
  283. printf("temp_fp=%s\n", temp_fp);
  284. ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
  285. fclose(temp_fp);
  286. */
  287. temp_state_key->_filename="var/statefile";
  288. temp_state_data = np_state_read();
  289. 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");
  290. ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" );
  291. ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
  292. temp_state_key->data_version=53;
  293. temp_state_data = np_state_read();
  294. ok( temp_state_data==NULL, "Older data version gives NULL" );
  295. temp_state_key->data_version=54;
  296. temp_state_key->_filename="var/nonexistant";
  297. temp_state_data = np_state_read();
  298. ok( temp_state_data==NULL, "Missing file gives NULL" );
  299. ok( this_nagios_plugin->state->state_data==NULL, "No state information" );
  300. temp_state_key->_filename="var/oldformat";
  301. temp_state_data = np_state_read();
  302. ok( temp_state_data==NULL, "Old file format gives NULL" );
  303. temp_state_key->_filename="var/baddate";
  304. temp_state_data = np_state_read();
  305. ok( temp_state_data==NULL, "Bad date gives NULL" );
  306. temp_state_key->_filename="var/missingdataline";
  307. temp_state_data = np_state_read();
  308. ok( temp_state_data==NULL, "Missing data line gives NULL" );
  309. unlink("var/generated");
  310. temp_state_key->_filename="var/generated";
  311. current_time=1234567890;
  312. np_state_write_string(current_time, "String to read");
  313. ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
  314. unlink("var/generated_directory/statefile");
  315. unlink("var/generated_directory");
  316. temp_state_key->_filename="var/generated_directory/statefile";
  317. current_time=1234567890;
  318. np_state_write_string(current_time, "String to read");
  319. ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
  320. /* This test to check cannot write to dir - can't automate yet */
  321. /*
  322. unlink("var/generated_bad_dir");
  323. mkdir("var/generated_bad_dir", S_IRUSR);
  324. np_state_write_string(current_time, "String to read");
  325. */
  326. temp_state_key->_filename="var/generated";
  327. time(&current_time);
  328. np_state_write_string(0, "String to read");
  329. temp_state_data = np_state_read();
  330. /* Check time is set to current_time */
  331. ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
  332. ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
  333. /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
  334. /*
  335. temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
  336. np_state_write_string(0, "Bad file");
  337. */
  338. np_cleanup();
  339. ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
  340. return exit_status();
  341. }