check_check_pack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "../lib/libcompat.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "check.h"
  25. #include "check_pack.h"
  26. #include "check_error.h"
  27. #include "check_check.h"
  28. #include "check_msg.h"
  29. static char errm[512];
  30. START_TEST(test_pack_fmsg)
  31. {
  32. FailMsg *fmsg;
  33. char *buf;
  34. enum ck_msg_type type;
  35. fmsg = (FailMsg *)emalloc (sizeof (FailMsg));
  36. fmsg->msg = (char *) "Hello, world!";
  37. pack (CK_MSG_FAIL, &buf, (CheckMsg *) fmsg);
  38. fmsg->msg = NULL;
  39. upack (buf, (CheckMsg *) fmsg, &type);
  40. ck_assert_msg (type == CK_MSG_FAIL,
  41. "Bad type unpacked for FailMsg");
  42. ck_assert_msg (fmsg->msg != NULL,
  43. "Unpacked string is NULL, should be Hello, World!");
  44. if (strcmp (fmsg->msg, "Hello, world!") != 0) {
  45. snprintf (errm, sizeof(errm),
  46. "Unpacked string is %s, should be Hello, World!",
  47. fmsg->msg);
  48. fail("%s", errm);
  49. }
  50. free (fmsg->msg);
  51. free (fmsg);
  52. free (buf);
  53. }
  54. END_TEST
  55. START_TEST(test_pack_loc)
  56. {
  57. LocMsg *lmsg;
  58. char *buf;
  59. enum ck_msg_type type;
  60. lmsg =(LocMsg *) emalloc (sizeof (LocMsg));
  61. lmsg->file = (char *) "abc123.c";
  62. lmsg->line = 125;
  63. pack (CK_MSG_LOC, &buf, (CheckMsg *) lmsg);
  64. lmsg->file = NULL;
  65. lmsg->line = 0;
  66. upack (buf, (CheckMsg *) lmsg, &type);
  67. ck_assert_msg (type == CK_MSG_LOC,
  68. "Bad type unpacked for LocMsg");
  69. if (lmsg->line != 125) {
  70. snprintf (errm, sizeof (errm),
  71. "LocMsg line was %d, should be %d",
  72. lmsg->line, 125);
  73. fail("%s", errm);
  74. }
  75. if (strcmp (lmsg->file, "abc123.c") != 0) {
  76. snprintf (errm, sizeof (errm),
  77. "LocMsg file was %s, should be abc123.c",
  78. lmsg->file);
  79. fail("%s", errm);
  80. }
  81. free (lmsg->file);
  82. free (lmsg);
  83. free (buf);
  84. }
  85. END_TEST
  86. START_TEST(test_pack_ctx)
  87. {
  88. CtxMsg cmsg;
  89. char *buf;
  90. enum ck_msg_type type;
  91. cmsg.ctx = CK_CTX_SETUP;
  92. pack (CK_MSG_CTX, &buf, (CheckMsg *) &cmsg);
  93. cmsg.ctx = CK_CTX_TEARDOWN;
  94. upack (buf, (CheckMsg *) &cmsg, &type);
  95. ck_assert_msg (type == CK_MSG_CTX,
  96. "Bad type unpacked for CtxMsg");
  97. if (cmsg.ctx != CK_CTX_SETUP) {
  98. snprintf (errm, sizeof (errm),
  99. "CtxMsg ctx got %d, expected %d",
  100. cmsg.ctx, CK_CTX_SETUP);
  101. fail("%s", errm);
  102. }
  103. free (buf);
  104. }
  105. END_TEST
  106. START_TEST(test_pack_len)
  107. {
  108. CtxMsg cmsg;
  109. char *buf;
  110. int n = 0;
  111. enum ck_msg_type type;
  112. cmsg.ctx = CK_CTX_TEST;
  113. n = pack (CK_MSG_CTX, &buf, (CheckMsg *) &cmsg);
  114. ck_assert_msg (n > 0, "Return val from pack not set correctly");
  115. /* Value below may change with different implementations of pack */
  116. ck_assert_msg (n == 8, "Return val from pack not correct");
  117. n = upack (buf, (CheckMsg *) &cmsg, &type);
  118. if (n != 8) {
  119. snprintf (errm, sizeof (errm), "%d bytes read from upack, should be 8", n);
  120. fail("%s", errm);
  121. }
  122. free (buf);
  123. }
  124. END_TEST
  125. START_TEST(test_pack_abuse)
  126. {
  127. char *buf;
  128. CtxMsg cmsg;
  129. enum ck_msg_type type;
  130. /* Should report -1 (e.g. invalid) if no buffer is passed */
  131. ck_assert_int_eq(pack(CK_MSG_CTX, NULL, (CheckMsg *) &cmsg), -1);
  132. /* Should report 0 (e.g. nothing packed) if no message is passed */
  133. ck_assert_int_eq(pack(CK_MSG_CTX, &buf, NULL), 0);
  134. /* Should report -1 (e.g. invalid) if no buffer is passed */
  135. ck_assert_int_eq(upack(NULL, (CheckMsg *) &cmsg, &type), -1);
  136. }
  137. END_TEST
  138. START_TEST(test_pack_ctx_limit)
  139. {
  140. CtxMsg cmsg;
  141. CtxMsg *cmsgp = NULL;
  142. char *buf;
  143. cmsg.ctx = (enum ck_result_ctx)-1;
  144. pack (CK_MSG_CTX, &buf, (CheckMsg *) &cmsg);
  145. pack (CK_MSG_CTX, &buf, (CheckMsg *) cmsgp);
  146. }
  147. END_TEST
  148. START_TEST(test_pack_fail_limit)
  149. {
  150. FailMsg fmsg;
  151. FailMsg *fmsgp = NULL;
  152. char *buf;
  153. enum ck_msg_type type;
  154. fmsg.msg = (char *) "";
  155. pack (CK_MSG_FAIL, &buf, (CheckMsg *) &fmsg);
  156. fmsg.msg = NULL;
  157. upack (buf, (CheckMsg *) &fmsg, &type);
  158. free (buf);
  159. ck_assert_msg (fmsg.msg != NULL,
  160. "Empty string not handled properly");
  161. ck_assert_msg (strcmp (fmsg.msg, "") == 0,
  162. "Empty string not handled properly");
  163. free (fmsg.msg);
  164. fmsg.msg = NULL;
  165. pack (CK_MSG_FAIL, &buf, (CheckMsg *) &fmsg);
  166. pack (CK_MSG_FAIL, &buf, (CheckMsg *) fmsgp);
  167. }
  168. END_TEST
  169. START_TEST(test_pack_loc_limit)
  170. {
  171. LocMsg lmsg;
  172. LocMsg *lmsgp = NULL;
  173. char *buf;
  174. enum ck_msg_type type;
  175. lmsg.file = (char *) "";
  176. lmsg.line = 0;
  177. pack (CK_MSG_LOC, &buf, (CheckMsg *) &lmsg);
  178. lmsg.file = NULL;
  179. upack (buf, (CheckMsg *) &lmsg, &type);
  180. ck_assert_msg (lmsg.file != NULL,
  181. "Empty string not handled properly");
  182. ck_assert_msg (strcmp (lmsg.file, "") == 0,
  183. "Empty string not handled properly");
  184. free (lmsg.file);
  185. lmsg.file = NULL;
  186. pack (CK_MSG_LOC, &buf, (CheckMsg *) &lmsg);
  187. pack (CK_MSG_LOC, &buf, (CheckMsg *) lmsgp);
  188. }
  189. END_TEST
  190. /* the ppack probably means 'pipe' pack */
  191. #if defined(HAVE_FORK) && HAVE_FORK==1
  192. START_TEST(test_ppack)
  193. {
  194. FILE * result_file;
  195. char * result_file_name = NULL;
  196. CtxMsg cmsg;
  197. LocMsg lmsg;
  198. FailMsg fmsg;
  199. RcvMsg *rmsg;
  200. cmsg.ctx = CK_CTX_TEST;
  201. lmsg.file = (char *) "abc123.c";
  202. lmsg.line = 10;
  203. fmsg.msg = (char *) "oops";
  204. result_file = open_tmp_file(&result_file_name);
  205. free(result_file_name);
  206. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  207. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  208. ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg);
  209. rewind(result_file);
  210. rmsg = punpack (result_file);
  211. ck_assert_msg (rmsg != NULL,
  212. "Return value from ppack should always be malloc'ed");
  213. ck_assert_msg (rmsg->lastctx == CK_CTX_TEST,
  214. "CTX not set correctly in ppack");
  215. ck_assert_msg (rmsg->fixture_line == -1,
  216. "Default fixture loc not correct");
  217. ck_assert_msg (rmsg->fixture_file == NULL,
  218. "Default fixture loc not correct");
  219. ck_assert_msg (rmsg->test_line == 10,
  220. "Test line not received correctly");
  221. ck_assert_msg (strcmp(rmsg->test_file,"abc123.c") == 0,
  222. "Test file not received correctly");
  223. ck_assert_msg (strcmp(rmsg->msg, "oops") == 0,
  224. "Failure message not received correctly");
  225. free(rmsg);
  226. fclose(result_file);
  227. }
  228. END_TEST
  229. START_TEST(test_ppack_noctx)
  230. {
  231. FILE * result_file;
  232. char * result_file_name = NULL;
  233. LocMsg lmsg;
  234. FailMsg fmsg;
  235. RcvMsg *rmsg;
  236. lmsg.file = (char *) "abc123.c";
  237. lmsg.line = 10;
  238. fmsg.msg = (char *) "oops";
  239. result_file = open_tmp_file(&result_file_name);
  240. free(result_file_name);
  241. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  242. ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg);
  243. rewind(result_file);
  244. rmsg = punpack (result_file);
  245. ck_assert_msg (rmsg == NULL,
  246. "Result should be NULL with no CTX");
  247. if (rmsg != NULL)
  248. free (rmsg);
  249. fclose(result_file);
  250. }
  251. END_TEST
  252. START_TEST(test_ppack_onlyctx)
  253. {
  254. FILE * result_file;
  255. char * result_file_name = NULL;
  256. CtxMsg cmsg;
  257. RcvMsg *rmsg;
  258. cmsg.ctx = CK_CTX_SETUP;
  259. result_file = open_tmp_file(&result_file_name);
  260. free(result_file_name);
  261. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  262. rewind(result_file);
  263. rmsg = punpack (result_file);
  264. ck_assert_msg (rmsg != NULL && rmsg->msg == NULL,
  265. "Result message should be NULL with only CTX");
  266. ck_assert_msg (rmsg->fixture_line == -1,
  267. "Result loc line should be -1 with only CTX");
  268. ck_assert_msg (rmsg->test_line == -1,
  269. "Result loc line should be -1 with only CTX");
  270. if (rmsg != NULL)
  271. free (rmsg);
  272. fclose(result_file);
  273. }
  274. END_TEST
  275. START_TEST(test_ppack_multictx)
  276. {
  277. FILE * result_file;
  278. char * result_file_name = NULL;
  279. CtxMsg cmsg;
  280. LocMsg lmsg;
  281. RcvMsg *rmsg;
  282. cmsg.ctx = CK_CTX_SETUP;
  283. lmsg.line = 5;
  284. lmsg.file = (char *) "abc123.c";
  285. result_file = open_tmp_file(&result_file_name);
  286. free(result_file_name);
  287. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  288. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  289. cmsg.ctx = CK_CTX_TEST;
  290. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  291. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  292. cmsg.ctx = CK_CTX_TEARDOWN;
  293. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  294. rewind(result_file);
  295. rmsg = punpack (result_file);
  296. ck_assert_msg (rmsg != NULL && rmsg->test_line == 5,
  297. "Test loc not being preserved on CTX change");
  298. ck_assert_msg (rmsg->fixture_line == -1,
  299. "Fixture not reset on CTX change");
  300. free (rmsg);
  301. fclose(result_file);
  302. }
  303. END_TEST
  304. START_TEST(test_ppack_nofail)
  305. {
  306. FILE * result_file;
  307. char * result_file_name = NULL;
  308. CtxMsg cmsg;
  309. LocMsg lmsg;
  310. RcvMsg *rmsg;
  311. lmsg.file = (char *) "abc123.c";
  312. lmsg.line = 10;
  313. cmsg.ctx = CK_CTX_SETUP;
  314. result_file = open_tmp_file(&result_file_name);
  315. free(result_file_name);
  316. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  317. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  318. rewind (result_file);
  319. rmsg = punpack (result_file);
  320. ck_assert_msg (rmsg != NULL && rmsg->msg == NULL,
  321. "Failure result should be NULL with no failure message");
  322. free (rmsg);
  323. fclose(result_file);
  324. }
  325. END_TEST
  326. #define BIG_MSG_LEN 1037
  327. START_TEST(test_ppack_big)
  328. {
  329. FILE * result_file;
  330. char * result_file_name = NULL;
  331. CtxMsg cmsg;
  332. LocMsg lmsg;
  333. FailMsg fmsg;
  334. RcvMsg *rmsg;
  335. cmsg.ctx = CK_CTX_TEST;
  336. lmsg.file = (char *)emalloc (BIG_MSG_LEN);
  337. memset (lmsg.file,'a',BIG_MSG_LEN - 1);
  338. lmsg.file[BIG_MSG_LEN - 1] = '\0';
  339. lmsg.line = 10;
  340. fmsg.msg = (char *)emalloc (BIG_MSG_LEN);
  341. memset (fmsg.msg, 'a', BIG_MSG_LEN - 1);
  342. fmsg.msg[BIG_MSG_LEN - 1] = '\0';
  343. result_file = open_tmp_file(&result_file_name);
  344. free(result_file_name);
  345. ppack (result_file, CK_MSG_CTX, (CheckMsg *) &cmsg);
  346. ppack (result_file, CK_MSG_LOC, (CheckMsg *) &lmsg);
  347. ppack (result_file, CK_MSG_FAIL, (CheckMsg *) &fmsg);
  348. rewind (result_file);
  349. rmsg = punpack (result_file);
  350. ck_assert_msg (rmsg != NULL,
  351. "Return value from ppack should always be malloc'ed");
  352. ck_assert_msg (rmsg->lastctx == CK_CTX_TEST,
  353. "CTX not set correctly in ppack");
  354. ck_assert_msg (rmsg->test_line == 10,
  355. "Test line not received correctly");
  356. ck_assert_msg (strcmp (rmsg->test_file, lmsg.file) == 0,
  357. "Test file not received correctly");
  358. ck_assert_msg (strcmp (rmsg->msg, fmsg.msg) == 0,
  359. "Failure message not received correctly");
  360. free (rmsg);
  361. free (lmsg.file);
  362. free (fmsg.msg);
  363. fclose(result_file);
  364. }
  365. END_TEST
  366. #endif /* HAVE_FORK */
  367. Suite *make_pack_suite(void)
  368. {
  369. Suite *s;
  370. TCase *tc_core;
  371. TCase *tc_limit;
  372. s = suite_create ("Pack");
  373. tc_core = tcase_create ("Core");
  374. tc_limit = tcase_create ("Limit");
  375. suite_add_tcase (s, tc_core);
  376. tcase_add_test (tc_core, test_pack_fmsg);
  377. tcase_add_test (tc_core, test_pack_loc);
  378. tcase_add_test (tc_core, test_pack_ctx);
  379. tcase_add_test (tc_core, test_pack_len);
  380. tcase_add_test (tc_core, test_pack_abuse);
  381. #if defined(HAVE_FORK) && HAVE_FORK==1
  382. tcase_add_test (tc_core, test_ppack);
  383. tcase_add_test (tc_core, test_ppack_noctx);
  384. tcase_add_test (tc_core, test_ppack_onlyctx);
  385. tcase_add_test (tc_core, test_ppack_multictx);
  386. tcase_add_test (tc_core, test_ppack_nofail);
  387. #endif /* HAVE_FORK */
  388. suite_add_tcase (s, tc_limit);
  389. tcase_add_test (tc_limit, test_pack_ctx_limit);
  390. tcase_add_test (tc_limit, test_pack_fail_limit);
  391. tcase_add_test (tc_limit, test_pack_loc_limit);
  392. #if defined(HAVE_FORK) && HAVE_FORK==1
  393. tcase_add_test (tc_limit, test_ppack_big);
  394. #endif /* HAVE_FORK */
  395. return s;
  396. }