END_TEST START_TEST(test_pack_ctx) { CtxMsg cmsg; char *buf; enum ck_msg_type type; cmsg.ctx = CK_CTX_SETUP; pack (CK_MSG_CTX, &buf, (CheckMsg *) &cmsg); cmsg.ctx = CK_CTX_TEARDOWN; upack (buf, (CheckMsg *) &cmsg, &type); ck_assert_msg (type == CK_MSG_CTX, "Bad type unpacked for CtxMsg"); if (cmsg.ctx != CK_CTX_SETUP) { snprintf (errm, sizeof (errm), "CtxMsg ctx got %d, expected %d", cmsg.ctx, CK_CTX_SETUP); fail (errm); } free (buf); }
END_TEST START_TEST(test_pack_fail_limit) { FailMsg fmsg; FailMsg *fmsgp = NULL; char *buf; enum ck_msg_type type; fmsg.msg = (char *) ""; pack (CK_MSG_FAIL, &buf, (CheckMsg *) &fmsg); fmsg.msg = NULL; upack (buf, (CheckMsg *) &fmsg, &type); free (buf); ck_assert_msg (fmsg.msg != NULL, "Empty string not handled properly"); ck_assert_msg (strcmp (fmsg.msg, "") == 0, "Empty string not handled properly"); free (fmsg.msg); fmsg.msg = NULL; pack (CK_MSG_FAIL, &buf, (CheckMsg *) &fmsg); pack (CK_MSG_FAIL, &buf, (CheckMsg *) fmsgp); }
END_TEST START_TEST(test_pack_len) { CtxMsg cmsg; char *buf; int n = 0; enum ck_msg_type type; cmsg.ctx = CK_CTX_TEST; n = pack (CK_MSG_CTX, &buf, (CheckMsg *) &cmsg); fail_unless (n > 0, "Return val from pack not set correctly"); /* Value below may change with different implementations of pack */ fail_unless (n == 8, "Return val from pack not correct"); n = 0; n = upack (buf, (CheckMsg *) &cmsg, &type); if (n != 8) { snprintf (errm, sizeof (errm), "%d bytes read from upack, should be 8", n); fail (errm); } free (buf); }
END_TEST START_TEST(test_pack_abuse) { char *buf; CtxMsg cmsg; /* Should report -1 (e.g. invalid) if no buffer is passed */ ck_assert_int_eq(pack(CK_MSG_CTX, NULL, (CheckMsg *) &cmsg), -1); /* Should report 0 (e.g. nothing packed) if no message is passed */ ck_assert_int_eq(pack(CK_MSG_CTX, &buf, NULL), 0); /* Should report -1 (e.g. invalid) if no buffer is passed */ ck_assert_int_eq(upack(NULL, (CheckMsg *) &cmsg, CK_MSG_CTX), -1); }
END_TEST START_TEST(test_pack_loc_limit) { LocMsg lmsg; LocMsg *lmsgp = NULL; char *buf; enum ck_msg_type type; lmsg.file = (char *) ""; lmsg.line = 0; pack (CK_MSG_LOC, &buf, (CheckMsg *) &lmsg); lmsg.file = (char *) "abc"; upack (buf, (CheckMsg *) &lmsg, &type); fail_unless (strcmp (lmsg.file, "") == 0, "Empty string not handled properly"); free (lmsg.file); lmsg.file = NULL; pack (CK_MSG_LOC, &buf, (CheckMsg *) &lmsg); pack (CK_MSG_LOC, &buf, (CheckMsg *) lmsgp); }
END_TEST START_TEST(test_pack_loc) { LocMsg *lmsg; char *buf; enum ck_msg_type type; lmsg =(LocMsg *) emalloc (sizeof (LocMsg)); lmsg->file = (char *) "abc123.c"; lmsg->line = 125; pack (CK_MSG_LOC, &buf, (CheckMsg *) lmsg); lmsg->file = NULL; lmsg->line = 0; upack (buf, (CheckMsg *) lmsg, &type); ck_assert_msg (type == CK_MSG_LOC, "Bad type unpacked for LocMsg"); if (lmsg->line != 125) { snprintf (errm, sizeof (errm), "LocMsg line was %d, should be %d", lmsg->line, 125); fail (errm); } if (strcmp (lmsg->file, "abc123.c") != 0) { snprintf (errm, sizeof (errm), "LocMsg file was %s, should be abc123.c", lmsg->file); fail (errm); } free (lmsg->file); free (lmsg); free (buf); }