test_utils.c 18 KB

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