📄 test_msg.c
字号:
}/* Test error handling */int test_msg_error(void){ msg_t *msg; su_home_t *home; msg_test_t *tst; BEGIN(); msg = read_msg("GET a-life HTTP/1.1" CRLF "Content-Length: 6" CRLF "Content-Language: fi" CRLF "Content-Language: <se>" CRLF "Accept-Language: en;q=0.8, fi, \"\", se ; q = 0.6" CRLF "Foo bar baf: bar" CRLF CRLF "test" CRLF); home = msg_home(msg); tst = msg_test_public(msg); TEST_1(msg); TEST_1(home); TEST_1(tst); TEST_1(tst->msg_error); msg_destroy(msg); END();}int test_mclass(void){ msg_t *msg; su_home_t *home; msg_test_t *tst; msg_request_t *request; msg_status_t *status; msg_separator_t *separator; msg_payload_t *payload; msg_content_length_t *l; msg_content_language_t *la; msg_content_encoding_t *k0, *k; msg_unknown_t *foo; BEGIN(); /* Test that critical errors are signaled */ msg = read_msg("GET a-life HTTP/1.1" CRLF "Content-Length: 6bytes" CRLF "Content-Type: *" CRLF "Foo: bar" CRLF "Content-Encoding: identity" CRLF "Content-Language: en" CRLF "Content-Language: en-us" CRLF CRLF "test" CRLF); tst = msg_test_public(msg); TEST_1(msg); TEST_1(tst); TEST_1(MSG_HAS_ERROR(tst->msg_flags)); /* Content-Length is critical */ msg_destroy(msg); msg = read_msg("GET a-life HTTP/1.1" CRLF "Content-Length: 6" CRLF "Content-Type: *" CRLF "Foo: bar" CRLF "Content-Encoding: " CRLF /* XXX */ "Content-Language: en" CRLF "Content-Language: en-us" CRLF CRLF "test" CRLF); home = msg_home(msg); tst = msg_test_public(msg); TEST_1(msg); TEST_1(home); TEST_1(tst); TEST_SIZE(msg_iovec(msg, NULL, 0), 1); TEST_1(tst->msg_unknown); /* Foo */ TEST_1(tst->msg_content_type == NULL); TEST_1(tst->msg_error); /* Content-Type */ TEST_1(tst->msg_error->er_next == NULL); TEST_1(!MSG_HAS_ERROR(tst->msg_flags)); /* Content-type is not critical */ TEST_1(la = tst->msg_content_language); TEST_1(la->k_common->h_data); TEST_1(la->k_items); TEST_S(la->k_items[0], "en"); TEST_S(la->k_items[1], "en-us"); TEST_P(la->k_items[2], NULL); TEST_1(la->k_next); TEST_1(la->k_next->k_common->h_data); TEST_1(la->k_next->k_items == NULL); TEST(msg_header_add_make(msg, (msg_pub_t *)tst, msg_content_language_class, "en-gb"), 0); TEST_P(la, tst->msg_content_language); TEST_P(la->k_common->h_data, NULL); TEST_S(la->k_items[2], "en-gb"); TEST_P(la->k_next, NULL); TEST_1(status = msg_status_make(home, "HTTP/1.1 200 Ok")); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)status), 0); TEST_P(tst->msg_status, status); TEST_P(tst->msg_request, NULL); TEST_1(request = msg_request_make(home, "GET a-wife HTTP/1.0")); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)request), 0); TEST_P(tst->msg_request, request); TEST_P(tst->msg_status, NULL); TEST_1(separator = msg_separator_make(home, "\r\n")); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0); TEST_P(tst->msg_separator, separator); TEST_P(separator->sep_common->h_succ, tst->msg_payload); /* Try to add a new payload */ TEST_1(payload = msg_payload_make(home, "foofaa\r\n")); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0); /* The new payload should be appended */ TEST_P(tst->msg_payload->pl_next, payload); TEST_P(tst->msg_payload->pl_common->h_succ, payload); /* Try to add a new header */ TEST_1(l = msg_content_length_create(home, tst->msg_payload->pl_len + payload->pl_len)); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)l), 0); /* The new header should be last before separator */ TEST_P(l->l_common->h_succ, separator); TEST_1(foo = tst->msg_unknown); TEST_S(foo->un_name, "Foo"); TEST_S(foo->un_value, "bar"); foo->un_value = "baz"; TEST_1(foo = msg_unknown_dup(home, foo)); TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)foo), 0); TEST_P(tst->msg_unknown->un_next, foo); TEST_1(k = msg_content_encoding_make(home, "gzip, compress")); k0 = tst->msg_content_encoding; TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0); TEST_P(k0, tst->msg_content_encoding); TEST_1(k0->k_items); TEST_S(k0->k_items[0], "gzip"); TEST_S(k0->k_items[1], "compress"); TEST_P(k0->k_items[2], NULL); TEST_1(k = msg_content_encoding_make(home, "gzip, deflate, compress")); TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0); TEST_P(k0, tst->msg_content_encoding); TEST_1(k0->k_items); TEST_S(k0->k_items[0], "gzip"); TEST_S(k0->k_items[1], "compress"); TEST_S(k0->k_items[2], "deflate"); TEST_P(k0->k_items[3], NULL); msg_destroy(msg); END();}int test_copy(void){ msg_t *msg0, *msg, *msg1, *msg2; su_home_t *home; msg_test_t *tst0, *tst, *copy, *dup; msg_request_t *request; msg_common_t *h, *h_succ; msg_iovec_t iovec[8]; char const s[] = "GET /a-life HTTP/1.1" CRLF "Content-Length: 6" CRLF "Content-Type: *" CRLF "Foo: bar" CRLF "Content-Language: " CRLF CRLF "test" CRLF; BEGIN(); msg0 = read_msg(s); TEST_1(msg0); TEST_1(tst0 = msg_test_public(msg0)); TEST_SIZE(msg_iovec(msg0, iovec, 8), 1); TEST_1(msg = msg_copy(msg0)); TEST_1(copy = msg_test_public(msg)); TEST_1(copy->msg_request); TEST_1(tst0->msg_request); TEST_S(copy->msg_request->rq_url->url_path, tst0->msg_request->rq_url->url_path); TEST_S(copy->msg_request->rq_url->url_path, "a-life"); TEST_P(copy->msg_request->rq_url->url_path, tst0->msg_request->rq_url->url_path); msg_destroy(msg); TEST_1(msg = msg_dup(msg0)); TEST_1(dup = msg_test_public(msg)); TEST_1(dup->msg_request); TEST_1(tst0->msg_request); TEST_S(dup->msg_request->rq_url->url_path, tst0->msg_request->rq_url->url_path); TEST_S(dup->msg_request->rq_url->url_path, "a-life"); TEST_1(dup->msg_request->rq_url->url_path != tst0->msg_request->rq_url->url_path); msg_destroy(msg); TEST_1(msg = msg_copy(msg0)); msg_destroy(msg0); TEST_1(home = msg_home(msg)); TEST_1(tst = msg_test_public(msg)); TEST_1(tst->msg_unknown); /* Foo */ TEST_1(tst->msg_content_type == NULL); TEST_1(tst->msg_error); /* Content-Type */ TEST_1(!MSG_HAS_ERROR(tst->msg_flags)); /* Flags are not copied */ TEST_1(tst0->msg_request); TEST_1(request = tst->msg_request); TEST_P(tst0->msg_request->rq_url->url_path, request->rq_url->url_path); TEST_SIZE(msg_iovec(msg, iovec, 8), 1); TEST_S(iovec->siv_base, s); TEST_1(msg1 = msg_dup(msg)); TEST_1(tst = msg_test_public(msg1)); TEST_1(tst->msg_request); for (h = tst->msg_request->rq_common; h; h = h_succ) { if (h->h_prev) *h->h_prev = NULL; h_succ = (msg_common_t*)h->h_succ; h->h_succ = NULL; } TEST_1(msg2 = msg_copy(msg1)); msg_destroy(msg2); TEST_1(msg2 = msg_dup(msg1)); msg_destroy(msg2); msg_destroy(msg1); msg_destroy(msg); END();}int test_mime(void){ msg_t *msg; su_home_t *home; int n; msg_test_t *tst; msg_header_t *h, *h_succ, *head; void *removed; msg_accept_t *ac, *ac0; msg_accept_charset_t *aa; msg_multipart_t *mp, *mp0, *mpX, *mpnew; msg_payload_t *pl; msg_content_type_t *c; msg_content_id_t *cid; msg_content_transfer_encoding_t *cte; 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-Length: 305" 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; 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); TEST_1(ac = tst->msg_accept); TEST_1(ac = ac->ac_next); TEST_S(ac->ac_type, "text/plain"); TEST_S(ac->ac_q, "0.9"); TEST_1(ac = msg_accept_dup(home, ac)); TEST_S(ac->ac_type, "text/plain"); TEST_S(ac->ac_q, "0.9"); TEST_S(tst->msg_accept->ac_next->ac_q, "0.9"); TEST_1(ac->ac_q != tst->msg_accept->ac_next->ac_q); TEST_1(ac = msg_accept_make(home, "text / plain")); ac->ac_q = "1.0"; TEST_1(ac = msg_accept_dup(home, ac0 = ac)); TEST_1(ac->ac_q != ac0->ac_q); 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"); mp = msg_multipart_parse(home, tst->msg_content_type, tst->msg_payload); TEST_1(mp0 = mp); TEST_1(mp->mp_data); TEST(memcmp(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CRLF, mp->mp_len), 0); TEST_1(mp->mp_common->h_data); TEST_M(mp->mp_common->h_data, CRLF "--" "LaGqGt4BI6Ho" " " CRLF, mp->mp_common->h_len); TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data); TEST_SIZE(strlen("part 1" CRLF), pl->pl_len); TEST(memcmp(pl->pl_data, "part 1" CRLF, pl->pl_len), 0); TEST_1(mp = mp->mp_next); TEST_1(mp->mp_data); TEST(memcmp(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CR LF, mp->mp_len), 0); TEST_1(c = mp->mp_content_type); TEST_S(c->c_type, "text/plain"); TEST_S(c->c_subtype, "plain"); TEST_1(c->c_params); TEST_1(c->c_params[0]); TEST_S(c->c_params[0], "charset=iso-8859-1"); TEST_1(cid = mp->mp_content_id); TEST_1(cte = mp->mp_content_transfer_encoding); TEST_S(cte->g_string, "quoted-unreadable"); TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data); TEST_SIZE(strlen("part 2"), pl->pl_len); TEST(memcmp(pl->pl_data, "part 2", pl->pl_len), 0); TEST_1(mp = mp->mp_next); TEST_1(mp->mp_data); TEST_M(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CRLF, mp->mp_len); TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data); TEST_SIZE(strlen(BODY3), pl->pl_len); TEST(memcmp(pl->pl_data, BODY3, pl->pl_len), 0); mpX = mp; TEST_1(!(mp = mp->mp_next)); /* Test serialization */ head = NULL; 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)); /* Remove chain */ for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) { h_succ = h->sh_succ; if (h->sh_prev) *h->sh_prev = NULL; h->sh_prev = NULL; h->sh_succ = NULL; } TEST(n, 15); 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, 15); /* Add a new part to multipart */ mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew); removed = mpX->mp_close_delim; mpX->mp_next = mpnew; mpX = mpnew; mpnew->mp_content_type = msg_content_type_make(home, "multipart/mixed"); TEST_1(mpnew->mp_content_type); TEST(msg_multipart_complete(msg_home(msg), tst->msg_content_type, mp0), 0); head = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -