📄 test_msg.c
字号:
TEST_1(h = msg_multipart_serialize(&head, mp0)); TEST_P((void *)h, mpX->mp_close_delim); TEST_1(!msg_chain_errors((msg_header_t *)mp0)); for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) { h_succ = h->sh_succ; TEST_1(h != removed); } TEST(n, 19);#define remove(h) \ (((*((msg_header_t*)(h))->sh_prev = ((msg_header_t*)(h))->sh_succ) ? \ (((msg_header_t*)(h))->sh_succ->sh_prev = ((msg_header_t*)(h))->sh_prev) \ : NULL), \ ((msg_header_t*)(h))->sh_succ = NULL, \ ((msg_header_t*)(h))->sh_prev = NULL) remove(mp0->mp_separator); remove(mp0->mp_next->mp_payload); remove(mp0->mp_next->mp_next->mp_content_type); remove(mp0->mp_next->mp_next->mp_next->mp_close_delim); TEST_1(!msg_chain_errors((msg_header_t *)mp0)); head = NULL; TEST_1(h = msg_multipart_serialize(&head, mp0)); TEST_P(h, mpX->mp_close_delim); TEST_1(!msg_chain_errors((msg_header_t *)mp0)); for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) { h_succ = h->sh_succ; if (h_succ == NULL) TEST_P(h, mpX->mp_close_delim); TEST_1(h != removed); } TEST(n, 19); /* Add an recursive multipart */ mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew); mpX->mp_multipart = mpnew; mpnew->mp_content_type = msg_content_type_make(home, "text/plain"); TEST_1(mpnew->mp_content_type); TEST(msg_multipart_complete(msg_home(msg), tst->msg_content_type, mp0), 0); TEST_1(mpnew->mp_close_delim); head = NULL; TEST_1(h = msg_multipart_serialize(&head, mp0)); TEST_P(h, mpX->mp_close_delim); TEST_1(!msg_chain_errors((msg_header_t *)mp0)); for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) h_succ = h->sh_succ; TEST(n, 24); su_home_check(home); su_home_zap(home); END();}/** Test MIME encoding */int test_mime2(void){ msg_t *msg; su_home_t *home; int n, m, len; msg_test_t *tst; msg_header_t *h; msg_accept_charset_t *aa; msg_multipart_t *mp; msg_content_type_t *c; msg_payload_t *pl; char const *end; char const s[] = "GET /a-life HTTP/1.1" CRLF "Accept: text/html;level=4;q=1" CRLF "Accept: text / plain;q=0.9" CRLF "Accept-Charset: *;q=0.1, iso-latin-1, utf-8;q=0.9" CRLF "Accept-Encoding: gzip;q=0.9, deflate" CRLF "Accept-Encoding: , identity ," CRLF "Accept: */*;q=0.2" CRLF "Accept-Language: en;q=0.5, es;q=0.2, fr;q=0.9, fi, x-pig-latin" CRLF "Content-Language: fi, se" CRLF "Content-Language: en, de" CRLF "Content-Disposition: render; required" CRLF "Content-Encoding: gzip, deflate" CRLF "Content-Base: http://localhost/foo" CRLF "MIME-Version: 1.0" CRLF "Content-Type: multipart/alternative ; boundary=\"LaGqGt4BI6Ho\"" CRLF "Content-MD5: LLO7gLaGqGt4BI6HouiWng==" CRLF CRLF "test" CRLF CRLF /* 1 */ "--LaGqGt4BI6Ho" " " CRLF CRLF /* 2 */ "part 1" CRLF /* 3 */ CRLF /* 4 */ "--LaGqGt4BI6Ho" CRLF "Content-Type: text/plain;charset=iso-8859-1" CRLF /* 5 */ "Content-ID: <m7ZvEEm49xdTT0WCDUgnww@localhost>" CRLF /* 6 */ "Content-Transfer-Encoding: quoted-unreadable" CRLF /* 7 */ CRLF /* 8 */ "part 2" /* 9 */ CRLF "--LaGqGt4BI6Ho" /* 10 */ "Content-Type: text/html" CRLF /* 11 */ "Content-ID: <4SP77aQZ9z6Top2dvLqKPQ@localhost>" CRLF /* 12 */ CRLF /* 13 */#define BODY3 "<html><body>part 3</body></html>" CRLF BODY3 /* 14 */ CRLF /* 15 */ "--LaGqGt4BI6Ho--" CRLF; char const part1[] = "This is text\n"; char const part2[] = "<html><body>This is html</body></html>"; char const part3[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; BEGIN(); msg = read_msg(s); home = msg_home(msg); tst = msg_test_public(msg); TEST_1(msg); TEST_1(home); TEST_1(tst); TEST_1(tst->msg_error == NULL); TEST_1((tst->msg_flags & MSG_FLG_ERROR) == 0); for (h = (msg_header_t *)tst->msg_request; h; h = h->sh_succ) { TEST_1(h->sh_data); if (h->sh_succ) TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data); } TEST_1(aa = tst->msg_accept_charset); TEST_S(aa->aa_value, "*"); TEST_S(aa->aa_q, "0.1"); TEST_1(c = tst->msg_content_type); TEST_1(tst->msg_payload); { su_home_t h0[1] = { SU_HOME_INIT(h0) }; pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl); mp = msg_multipart_parse(home, c, pl); TEST_1(mp); for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) h->sh_data = NULL, h->sh_len = 0; TEST(n, 15); n = msg_multipart_prepare(msg, mp, 0); TEST_1(end = strstr(s, "--LaGqGt4BI6Ho ")); len = strlen(end); TEST(len, n); TEST_1(mp = msg_multipart_dup(h0, mp)); su_home_check(h0); su_home_deinit(h0); } /* Test parsing without explicit boundary */ { su_home_t h0[1] = { SU_HOME_INIT(h0) }; pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl); mp = msg_multipart_parse(h0, NULL, pl); TEST_1(mp); for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) h->sh_data = NULL, h->sh_len = 0; TEST(n, 15); n = msg_multipart_prepare(msg, mp, 0); TEST_1(end = strstr(s, "--LaGqGt4BI6Ho ")); len = strlen(end); TEST(len, n); TEST_1(mp = msg_multipart_dup(h0, mp)); su_home_check(h0); su_home_deinit(h0); } /* Test parsing without preamble and explicit boundary */ { su_home_t h0[1] = { SU_HOME_INIT(h0) }; pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl); n = strstr(pl->pl_data, "--LaGqGt4BI6Ho") - (char *)pl->pl_data; pl->pl_data = n + (char *)pl->pl_data; pl->pl_len -= n; len = pl->pl_len; mp = msg_multipart_parse(h0, NULL, pl); TEST_1(mp); for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) h->sh_data = NULL, h->sh_len = 0; TEST(n, 15); n = msg_multipart_prepare(msg, mp, 0); TEST(len, n); TEST_1(mp = msg_multipart_dup(h0, mp)); su_home_check(h0); su_home_deinit(h0); } /* Test parsing without CR's */ { su_home_t h0[1] = { SU_HOME_INIT(h0) }; char *b; pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl); /* Remove CRs */ b = pl->pl_data, len = pl->pl_len; for (n = m = 0; n < len; n++) { if ((b[m] = b[n]) != '\r') m++; } pl->pl_len = m; mp = msg_multipart_parse(h0, NULL, pl); TEST_1(mp); for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) h->sh_data = NULL, h->sh_len = 0; TEST(n, 15); n = msg_multipart_prepare(msg, mp, 0); TEST_1(n > 0); TEST_1(mp = msg_multipart_dup(h0, mp)); su_home_check(h0); su_home_deinit(h0); } /* Create a new multipart from three parts */ TEST_1(c = msg_content_type_make(home, "multipart/related")); TEST_1(mp = msg_multipart_create(home, "text/plain", part1, strlen(part1))); TEST_1(mp->mp_next = msg_multipart_create(home, "text/html", part2, strlen(part2))); TEST_1(mp->mp_next->mp_next = msg_multipart_create(home, "application/octet-stream", part3, sizeof part3)); TEST(msg_multipart_complete(home, c, mp), 0); h = NULL; TEST_P(msg_multipart_serialize(&h, mp), mp->mp_next->mp_next->mp_close_delim); TEST_1(msg_multipart_prepare(msg, mp, 0)); TEST_1(mp = msg_multipart_dup(home, mp)); su_home_check(home); su_home_zap(home); END();}/* Test serialization */int test_serialize(void){ msg_t *msg; su_home_t *home; msg_test_t *tst; msg_mime_version_t *mime; msg_separator_t *sep; msg_payload_t *pl; msg_accept_encoding_t *aen; msg_accept_language_t *ala; char const s[] = "GET /a-life HTTP/1.1" CRLF "Accept-Language: fi" CRLF "Accept-Encoding: z0" CRLF "Accept-Language: se, de" CRLF "Accept-Encoding: z1, z2" CRLF "Accept-Language: en, sv" CRLF "Accept-Encoding: z3, z4" CRLF "Content-Length: 6" CRLF CRLF "test" CRLF; BEGIN(); msg = read_msg(s); TEST_1(msg); home = msg_home(msg); TEST_1(tst = msg_test_public(msg)); TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0); TEST_1(ala = tst->msg_accept_language->aa_next->aa_next); TEST(msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)ala), 0); TEST_S(ala->aa_value, "de"); TEST_1(ala = tst->msg_accept_language); TEST_1(ala = ala->aa_next); TEST_S(ala->aa_value, "se"); /* Make sure that cached encoding of se is reset */ TEST_1(ala->aa_common->h_data == NULL); TEST_1(ala->aa_common->h_len == 0); TEST_1(ala = ala->aa_next); TEST_S(ala->aa_value, "en"); /* Make sure that cached encoding of en is kept intact */ TEST_1(ala->aa_common->h_data != NULL); TEST_1(ala->aa_common->h_len != 0); TEST_1(aen = tst->msg_accept_encoding->aa_next->aa_next); TEST(msg_header_remove_all(msg, (msg_pub_t *)tst, (msg_header_t *)aen), 0); TEST_1(aen = tst->msg_accept_encoding); TEST_1(aen = aen->aa_next); TEST_S(aen->aa_value, "z1"); /* Make sure that cached encoding of z1 is reset */ TEST_1(aen->aa_common->h_data == NULL); TEST_1(aen->aa_common->h_len == 0); TEST_1(aen->aa_next == NULL); TEST_1(aen->aa_common->h_succ == (void *)ala); TEST_1(ala->aa_next->aa_common); TEST_1(ala->aa_next->aa_common->h_succ == (void *)tst->msg_content_length); TEST_1(mime = msg_mime_version_make(home, "1.0")); tst->msg_mime_version = mime; TEST(msg_serialize(msg, (msg_pub_t *)tst), 0); TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0); TEST_P(tst->msg_content_length->l_common->h_succ, mime); TEST_P(mime->g_common->h_succ, tst->msg_separator); msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)tst->msg_separator); TEST_1(sep = msg_separator_make(home, CRLF)); tst->msg_separator = sep; TEST(msg_serialize(msg, (msg_pub_t *)tst), 0); TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0); TEST_P(mime->g_common->h_succ, sep); TEST_P(sep->sep_common->h_succ, tst->msg_payload); msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)tst->msg_payload); TEST_1(pl = msg_payload_make(home, "foobar" CRLF)); pl->pl_next = tst->msg_payload; tst->msg_payload = pl; TEST(msg_serialize(msg, (msg_pub_t *)tst), 0); TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0); TEST_P(mime->g_common->h_succ, sep); TEST_P(sep->sep_common->h_succ, pl); TEST_P(pl->pl_common->h_succ, pl->pl_next); msg_destroy(msg); END();}static int random_test(void){ struct { uint64_t low, mid, hi; } seed = { 0, 0, 0 }; uint8_t zeros[24] = { 0 }; uint8_t ones[24]; char token[33]; BEGIN(); memset(ones, 255, sizeof ones); TEST_SIZE(msg_random_token(token, 32, (void *)&seed, sizeof(seed)), 32); TEST_S(token, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); TEST_SIZE(msg_random_token(token, 32, zeros, 4), 7); TEST_S(token, "aaaaaaa"); TEST_SIZE(msg_random_token(token, 32, ones, 4), 7); /* Last char may vary.. */ token[6] = 0; TEST_S(token, "999999"); TEST_SIZE(msg_random_token(token, 32, zeros, 8), 13); TEST_S(token, "aaaaaaaaaaaaa"); TEST_SIZE(msg_random_token(token, 32, zeros, 12), 20); TEST_S(token, "aaaaaaaaaaaaaaaaaaaa"); END();}void usage(int exitcode){ fprintf(stderr, "usage: %s [-v] [-a]\n", name); exit(exitcode);}int main(int argc, char *argv[]){ int retval = 0; int i; for (i = 1; argv[i]; i++) { if (strcmp(argv[i], "-v") == 0) test_flags |= tst_verbatim; else if (strcmp(argv[i], "-a") == 0) test_flags |= tst_abort; else usage(1); }#if HAVE_OPEN_C test_flags |= tst_verbatim;#endif retval |= msg_time_test(); fflush(stdout); retval |= addr_test(); fflush(stdout); retval |= hash_test(); fflush(stdout); retval |= random_test(); fflush(stdout); retval |= test_header_parsing(); fflush(stdout); retval |= test_msg_parsing(); fflush(stdout); retval |= test_warning(); fflush(stdout); retval |= test_msg_error(); fflush(stdout); retval |= test_mclass(); fflush(stdout); retval |= test_copy(); fflush(stdout); retval |= test_mime(); fflush(stdout); retval |= test_mime2(); fflush(stdout); retval |= test_serialize(); fflush(stdout); return retval;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -