📄 test_tport.c
字号:
TEST_1(tport_incref(tp) != tp1); tport_decref(&tp); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "fresh-1")); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST_1(tport_shutdown(tp0, 2) >= 0); TEST_1(tport_shutdown(tp1, 2) >= 0); TEST_1(tport_shutdown(tp0, 1) >= 0); TEST(tport_shutdown(NULL, 0), -1); tport_decref(&tp0); tport_decref(&tp1); END();}static int sctp_test(tp_test_t *tt){ BEGIN(); msg_t *msg = NULL; int i, n; tport_t *tp; char buffer[32]; if (!tt->tt_sctp_name->tpn_proto) return 0; /* Just a small and nice message first */ TEST_1(!new_test_msg(tt, &msg, "sctp-small", 1, 1024)); test_create_md5(tt, msg); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; if (1) return 0; /* SCTP does not work reliably. Really. */ /* Create large messages, just to force queueing in sending end */ for (n = 0; !tport_queuelen(tp); n++) { snprintf(buffer, sizeof buffer, "cid:sctp-%u", n); TEST_1(!new_test_msg(tt, &msg, buffer, 1, 32000)); test_create_md5(tt, msg); TEST_1(tport_tsend(tp, msg, tt->tt_sctp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); msg_destroy(msg); } /* Fill up the queue */ for (i = 1; i < TPORT_QUEUESIZE; i++) { snprintf(buffer, sizeof buffer, "cid:sctp-%u", n + i); TEST_1(!new_test_msg(tt, &msg, buffer, 1, 1024)); TEST_1(tport_tsend(tp, msg, tt->tt_sctp_name, TAG_END())); msg_destroy(msg); } /* This overflows the queue */ snprintf(buffer, sizeof buffer, "cid:sctp-%u", n + i); TEST_1(!new_test_msg(tt, &msg, buffer, 1, 1024)); TEST_1(!tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END())); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; /* This uses a new connection */ TEST_1(!new_test_msg(tt, &msg, "cid-sctp-new", 1, 1024)); TEST_1(tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TPTAG_REUSE(0), TAG_END())); msg_destroy(msg); /* Receive every message from queue */ for (tt->tt_received = 0; tt->tt_received < TPORT_QUEUESIZE + n;) { TEST(tport_test_run(tt, 10), 1); /* Validate message */ TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; } /* Try to send a single message */ TEST_1(!new_test_msg(tt, &msg, "cid:sctp-final", 1, 1024)); TEST_1(tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END())); msg_destroy(msg); TEST(tport_test_run(tt, 10), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; END();}static int tls_test(tp_test_t *tt){ BEGIN(); #if HAVE_TLS tp_name_t const *dst = tt->tt_tls_name; msg_t *msg = NULL; int i; char ident[16]; TEST_S(dst->tpn_proto, "tls"); tt->tt_received = 0; /* Create a large message, just to force queueing in sending end */ TEST_1(!new_test_msg(tt, &msg, "tls-0", 16, 64 * 1024)); test_create_md5(tt, msg); TEST_1(tport_tsend(tt->tt_tports, msg, dst, TAG_END())); msg_destroy(msg); /* Fill up the queue */ for (i = 1; i < TPORT_QUEUESIZE; i++) { snprintf(ident, sizeof ident, "tls-%u", i); TEST_1(!new_test_msg(tt, &msg, ident, 2, 512)); TEST_1(tport_tsend(tt->tt_tports, msg, dst, TAG_END())); msg_destroy(msg); } /* This overflows the queue */ TEST_1(!new_test_msg(tt, &msg, "tls-overflow", 1, 1024)); TEST_1(!tport_tsend(tt->tt_tports, msg, dst, TAG_END())); msg_destroy(msg); TEST(tport_test_run(tt, 60), 1); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; /* This uses a new connection */ TEST_1(!new_test_msg(tt, &msg, "tls-no-reuse", 1, 1024)); TEST_1(tport_tsend(tt->tt_tports, msg, dst, TPTAG_REUSE(0), TAG_END())); msg_destroy(msg); /* Receive every message from queue */ while (tt->tt_received < TPORT_QUEUESIZE + 1) { TEST(tport_test_run(tt, 5), 1); /* Validate message */ msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; } /* Try to send a single message */ TEST_1(!new_test_msg(tt, &msg, "tls-last", 1, 1024)); TEST_1(tport_tsend(tt->tt_tports, msg, dst, TAG_END())); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tls-last")); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;#endif END(); return 0;}static int sigcomp_test(tp_test_t *tt){ BEGIN();#if HAVE_SIGCOMP su_home_t *home; tp_name_t tpn[1] = {{ NULL }}; struct sigcomp_compartment *cc; if (tt->tt_udp_comp->tpn_comp) { msg_t *msg = NULL; TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tt->tt_udp_comp)); TEST_1(!new_test_msg(tt, &msg, "udp-sigcomp", 1, 1200)); test_create_md5(tt, msg); TEST_1(tport_tsend(tt->tt_tports, msg, tt->tt_udp_comp, TPTAG_COMPARTMENT(cc), TAG_END())); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); test_check_md5(tt, tt->tt_rmsg); TEST_1(msg = tt->tt_rmsg); tt->tt_rmsg = NULL; TEST_1(home = msg_home(msg)); TEST_1(tport_convert_addr(home, tpn, "udp", NULL, msg_addr(msg)) == 0); tpn->tpn_comp = tport_name(tt->tt_rtport)->tpn_comp; /* reply */ TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tpn)); TEST_1(tport_tsend(tt->tt_rtport, msg, tpn, TPTAG_COMPARTMENT(cc), TAG_END()) != NULL); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, NULL)); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; } if (tt->tt_tcp_comp->tpn_comp) { tport_t *tp; msg_t *msg = NULL; *tpn = *tt->tt_tcp_comp; TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp", 1, 1500)); test_create_md5(tt, msg); tport_log->log_level = 9; TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tpn)); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tpn, TPTAG_COMPARTMENT(cc), TAG_END())); TEST_1(tport_incref(tp)); tport_decref(&tp); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp")); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-2", 1, 3000)); test_create_md5(tt, msg); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_comp, TPTAG_COMPARTMENT(cc), TAG_END())); TEST_1(tport_incref(tp)); tport_decref(&tp); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-2")); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-3", 1, 45500)); test_create_md5(tt, msg); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_comp, TPTAG_COMPARTMENT(cc), TAG_END())); TEST_1(tport_incref(tp)); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); tport_decref(&tp); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-3")); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; { tp_name_t tpn[1]; tport_t *ctp, *rtp; *tpn = *tt->tt_tcp_comp; tpn->tpn_comp = NULL; TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-4", 1, 1000)); test_create_md5(tt, msg); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tpn, TPTAG_COMPARTMENT(cc), TPTAG_FRESH(1), TAG_END())); TEST_1(ctp = tport_incref(tp)); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-4")); test_check_md5(tt, tt->tt_rmsg); TEST_1((msg_addrinfo(tt->tt_rmsg)->ai_flags & TP_AI_COMPRESSED) == 0); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST_1(rtp = tport_incref(tt->tt_rtport)); TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-5", 1, 1000)); test_create_md5(tt, msg); { /* Mess with internal data structures in order to force tport to use SigComp on this connection */ tp_name_t *tpn = (tp_name_t *)tport_name(rtp); tpn->tpn_comp = "sigcomp"; } TEST_1(tp = tport_tsend(rtp, msg, tt->tt_tcp_comp, TPTAG_COMPARTMENT(cc), TAG_END())); TEST_1(tport_incref(tp)); msg_destroy(msg); TEST(tp, rtp); TEST(tport_test_run(tt, 5), 1); tport_decref(&tp); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-5")); test_check_md5(tt, tt->tt_rmsg); TEST_1((msg_addrinfo(tt->tt_rmsg)->ai_flags & TP_AI_COMPRESSED) != 0); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST(ctp, tt->tt_rtport); tport_decref(&ctp); } }#endif END();}#if HAVE_SOFIA_STUN#include <sofia-sip/stun_tag.h>static int stun_test(tp_test_t *tt){ BEGIN(); tport_t *mr; tp_name_t tpn[1] = {{ "*", "*", "*", "*", NULL }};#if HAVE_NETINET_SCTP_H char const * transports[] = { "udp", "tcp", "sctp", NULL };#else char const * transports[] = { "udp", "tcp", NULL };#endif TEST_1(mr = tport_tcreate(tt, tp_test_class, tt->tt_root, TAG_END())); TEST(tport_tbind(tt->tt_tports, tpn, transports, TPTAG_SERVER(1), STUNTAG_SERVER("999.999.999.999"), TAG_END()), -1); tport_destroy(mr); END();}#elsestatic int stun_test(tp_test_t *tt){ return 0;}#endifstatic int deinit_test(tp_test_t *tt){ BEGIN(); /* Destroy client transports */ tport_destroy(tt->tt_tports), tt->tt_tports = NULL; /* Destroy server transports */ tport_destroy(tt->tt_srv_tports), tt->tt_srv_tports = NULL;#if HAVE_SIGCOMP sigcomp_state_handler_free(tt->state_handler); tt->state_handler = NULL;#endif END();}/* Test tport_tags filter */static int filter_test(tp_test_t *tt){ tagi_t *lst, *result; su_home_t home[1] = { SU_HOME_INIT(home) }; BEGIN(); lst = tl_list(TSTTAG_HEADER_STR("X: Y"), TAG_SKIP(2), TPTAG_IDENT("foo"), TSTTAG_HEADER_STR("X: Y"), TPTAG_IDENT("bar"), TAG_NULL()); TEST_1(lst); result = tl_afilter(home, tport_tags, lst); TEST_1(result); TEST(result[0].t_tag, tptag_ident); TEST(result[1].t_tag, tptag_ident); free(lst); su_home_deinit(home); END();}int main(int argc, char *argv[]){ int flags = 0; /* XXX */ int retval = 0; int i; tp_test_t tt[1] = {{{ SU_HOME_INIT(tt) }}}; for (i = 1; argv[i]; i++) { if (strcmp(argv[i], "-v") == 0) tstflags |= tst_verbatim; else usage(); } /* Use log */ if (flags & tst_verbatim) tport_log->log_default = 9; else tport_log->log_default = 1; su_init(); retval |= name_test(tt); fflush(stdout); retval |= filter_test(tt); fflush(stdout); retval |= init_test(tt); fflush(stdout); if (retval == 0) { retval |= sigcomp_test(tt); fflush(stdout); retval |= sctp_test(tt); fflush(stdout); retval |= udp_test(tt); fflush(stdout); retval |= tcp_test(tt); fflush(stdout); retval |= reuse_test(tt); fflush(stdout); retval |= tls_test(tt); fflush(stdout); if (0) /* Not yet working... */ retval |= stun_test(tt); fflush(stdout); retval |= deinit_test(tt); fflush(stdout); } su_deinit(); return retval;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -