📄 test_tport.c
字号:
#else char const * transports[] = { "udp", "tcp", NULL };#endif tp_name_t const *tpn; tport_t *tp; BEGIN(); int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST;#ifdef AI_ALL mask |= AI_ALL;#endif#ifdef AI_V4MAPPED_CFG mask |= AI_V4MAPPED_CFG;#endif#ifdef AI_ADDRCONFIG mask |= AI_ADDRCONFIG;#endif#ifdef AI_V4MAPPED mask |= AI_V4MAPPED;#endif /* Test that we have no common flags with underlying getaddrinfo() */ TEST(mask & TP_AI_MASK, 0); TEST_1(tt->tt_root = su_root_create(NULL)); myname->tpn_host = "127.0.0.1"; myname->tpn_ident = "client"; /* Create message class */ TEST_1(tt->tt_mclass = msg_mclass_clone(msg_test_mclass, 0, 0)); /* Try to insert Content-Length header (expecting failure) */ TEST(msg_mclass_insert(tt->tt_mclass, msg_content_length_href), -1);#if HAVE_SIGCOMP TEST_1(tt->state_handler = sigcomp_state_handler_create()); TEST_1(tt->algorithm = sigcomp_algorithm_by_name(getenv("SIGCOMP_ALGORITHM"))); TEST_1(tt->master_cc = sigcomp_compartment_create(tt->algorithm, tt->state_handler, 0, "", 0, NULL, 0)); TEST(sigcomp_compartment_option(tt->master_cc, "stateless"), 1);#endif /* Create client transport */ TEST_1(tt->tt_tports = tport_tcreate(tt, tp_test_class, tt->tt_root, IF_SIGCOMP_TPTAG_COMPARTMENT(tt->master_cc) TAG_END())); /* Bind client transports */ TEST(tport_tbind(tt->tt_tports, myname, transports, TPTAG_SERVER(0), TAG_END()), 0); if (getenv("TPORT_TEST_HOST")) myname->tpn_host = getenv("TPORT_TEST_HOST"); else myname->tpn_host = "*"; if (getenv("TPORT_TEST_PORT")) myname->tpn_port = getenv("TPORT_TEST_PORT"); myname->tpn_ident = "server"; /* Create server transport */ TEST_1(tt->tt_srv_tports = tport_tcreate(tt, tp_test_class, tt->tt_root, IF_SIGCOMP_TPTAG_COMPARTMENT(tt->master_cc) TAG_END())); /* Bind server transports */ TEST(tport_tbind(tt->tt_srv_tports, myname, transports, TPTAG_SERVER(1), TAG_END()), 0); for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp)) TEST_S(tport_name(tp)->tpn_ident, "server"); { su_sockaddr_t su[1]; socklen_t sulen; int s; int i, before, after; char port[8]; tp_name_t rname[1]; *rname = *myname; memset(su, 0, sulen = sizeof(su->su_sin)); s = socket(su->su_family = AF_INET, SOCK_STREAM, 0); TEST_1(s != -1); TEST_1(bind(s, &su->su_sa, sulen) != -1); TEST_1(listen(s, 5) != -1); TEST_1(getsockname(s, &su->su_sa, &sulen) != -1); sprintf(port, "%u", ntohs(su->su_port)); rname->tpn_port = port; rname->tpn_ident = "failure"; before = count_tports(tt->tt_srv_tports); /* Bind server transports to an reserved port */ TEST(tport_tbind(tt->tt_srv_tports, rname, transports, TPTAG_SERVER(1), TAG_END()), -1); after = count_tports(tt->tt_srv_tports); TEST(before, after); for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp)) TEST_S(tport_name(tp)->tpn_ident, "server"); rname->tpn_port = "*"; rname->tpn_ident = "server2"; /* Bind server transports to another port */ TEST(tport_tbind(tt->tt_srv_tports, rname, transports, TPTAG_SERVER(1), TAG_END()), 0); tp = tport_primaries(tt->tt_srv_tports); for (i = 0; i++ < before; tp = tport_next(tp)) TEST_S(tport_name(tp)->tpn_ident, "server"); for (; tp; tp = tport_next(tp)) TEST_S(tport_name(tp)->tpn_ident, "server2"); }#if HAVE_TLS { tp_name_t tlsname[1] = {{ "tls", "*", "*", "*", NULL }}; char const * transports[] = { "tls", NULL }; char const *srcdir = getenv("srcdir"); if (srcdir == NULL) srcdir = "."; tlsname->tpn_host = myname->tpn_host; tlsname->tpn_ident = "server"; /* Bind client transports */ TEST(tport_tbind(tt->tt_tports, tlsname, transports, TPTAG_SERVER(0), TPTAG_CERTIFICATE(srcdir), TAG_END()), 0); /* Bind tls server transport */ TEST(tport_tbind(tt->tt_srv_tports, tlsname, transports, TPTAG_SERVER(1), TPTAG_CERTIFICATE(srcdir), TAG_END()), 0); }#endif for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp)) { TEST_1(tpn = tport_name(tp)); if (1 || tt->tt_flags & tst_verbatim) printf("bound transport to %s/%s:%s;maddr=%s%s%s\n", tpn->tpn_proto, tpn->tpn_canon, tpn->tpn_port, tpn->tpn_host, tpn->tpn_comp ? ";comp=" : "", tpn->tpn_comp ? tpn->tpn_comp : ""); /* Ignore server2 tports for now */ if (strcmp(tpn->tpn_ident, "server")) continue; if (strcmp(tpn->tpn_proto, "udp") == 0) { *tt->tt_udp_name = *tpn; tt->tt_udp_name->tpn_comp = NULL; tt->tt_udp_name->tpn_ident = NULL; *tt->tt_udp_comp = *tpn; tt->tt_udp_comp->tpn_ident = NULL; } else if (strcmp(tpn->tpn_proto, "tcp") == 0) { *tt->tt_tcp_name = *tpn; tt->tt_tcp_name->tpn_comp = NULL; tt->tt_tcp_name->tpn_ident = NULL; *tt->tt_tcp_comp = *tpn; tt->tt_tcp_comp->tpn_ident = NULL; } else if (strcmp(tpn->tpn_proto, "sctp") == 0) { *tt->tt_sctp_name = *tpn; tt->tt_sctp_name->tpn_ident = NULL; } else if (strcmp(tpn->tpn_proto, "tls") == 0) { *tt->tt_tls_name = *tpn; tt->tt_tls_name->tpn_ident = NULL; } } END();}char const payload[] = "Some data\n""More data\n";#include <time.h>int tport_test_run(tp_test_t *tt, unsigned timeout){ time_t now = time(NULL); tt->tt_status = 0; msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; tt->tt_rtport = NULL; while (!tt->tt_status) { if (tt->tt_flags & tst_verbatim) { fputs(".", stdout); fflush(stdout); } su_root_step(tt->tt_root, 500L); if (!getenv("TPORT_TEST_DEBUG") && time(NULL) > now + timeout) return 0; } return tt->tt_status;}static int udp_test(tp_test_t *tt){ tport_t *tp; msg_t *msg; msg_test_t *tst; su_home_t *home; msg_request_t *rq; msg_unknown_t *u; msg_content_length_t *l; msg_content_md5_t *md5; msg_separator_t *sep; msg_payload_t *pl; BEGIN(); TEST_1(msg = msg_create(tt->tt_mclass, 0)); TEST_1(tst = msg_test_public(msg)); TEST_1(home = msg_home(msg)); TEST_1(rq = msg_request_make(home, "DO im:foo@faa MSG/1.0")); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)rq), 0); TEST_1(u = msg_unknown_make(home, "Foo: faa")); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)u), 0); TEST_1(pl = msg_payload_make(home, payload)); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)pl), 0); TEST_1(l = msg_content_length_format(home, "%u", pl->pl_len)); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)l), 0); TEST_1(md5 = msg_content_md5_make(home, "R6nitdrtJFpxYzrPaSXfrA==")); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)md5), 0); TEST_1(sep = msg_separator_create(home)); TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)sep), 0); TEST(msg_serialize(msg, (void *)tst), 0); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_udp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST(tport_test_run(tt, 5), 1); msg_destroy(msg);#if 0 tp_name_t tpn[1] = {{ NULL }}; 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(tport_tsend(tt->tt_rtport, msg, tpn, TAG_END()) != NULL); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); msg_destroy(msg);#endif msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; END();}static int tcp_test(tp_test_t *tt){ msg_t *msg = NULL; int i; tport_t *tp, *tp0; char ident[16]; BEGIN(); /* Create a large message, just to force queueing in sending end */ TEST(new_test_msg(tt, &msg, "tcp-0", 1, 16 * 64 * 1024), 0); test_create_md5(tt, msg); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); tp0 = tport_incref(tp); msg_destroy(msg); /* Fill up the queue */ for (i = 1; i < TPORT_QUEUESIZE; i++) { snprintf(ident, sizeof ident, "tcp-%u", i); TEST(new_test_msg(tt, &msg, ident, 1, 1024), 0); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST(tport_incref(tp), tp0); tport_decref(&tp); msg_destroy(msg); } /* This overflows the queue */ TEST(new_test_msg(tt, &msg, "tcp-overflow", 1, 1024), 0); TEST_1(!tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); msg_destroy(msg); tt->tt_received = 0; TEST(tport_test_run(tt, 60), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-0")); test_check_md5(tt, tt->tt_rmsg); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; snprintf(ident, sizeof ident, "tcp-%u", tt->tt_received); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, ident)); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; /* This uses a new connection */ TEST_1(!new_test_msg(tt, &msg, "tcp-no-reuse", 1, 1024)); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TPTAG_REUSE(0), TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST_1(tport_incref(tp) != tp0); tport_decref(&tp); msg_destroy(msg); /* This uses the old connection */ TEST_1(!new_test_msg(tt, &msg, "tcp-reuse", 1, 1024)); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TPTAG_REUSE(1), TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST_1(tport_incref(tp) == tp0); tport_decref(&tp); msg_destroy(msg); /* Receive every message from queue */ while (tt->tt_received < TPORT_QUEUESIZE + 2) { TEST(tport_test_run(tt, 5), 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, "tcp-last", 1, 1024)); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST(tport_incref(tp), tp0); tport_decref(&tp); msg_destroy(msg); TEST(tport_test_run(tt, 5), 1); TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-last")); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; tport_decref(&tp0); END();}static int reuse_test(tp_test_t *tt){ msg_t *msg = NULL; int i, reuse = -1; tport_t *tp, *tp0, *tp1; tp_name_t tpn[1]; BEGIN(); /* Flush existing connections */ *tpn = *tt->tt_tcp_name; tpn->tpn_port = "*"; TEST_1(tp = tport_by_name(tt->tt_tports, tpn)); TEST_1(tport_is_primary(tp)); TEST(tport_flush(tp), 0); for (i = 0; i < 10; i++) su_root_step(tt->tt_root, 10L); TEST(tport_set_params(tp, TPTAG_REUSE(0), TAG_END()), 1); /* Send two messages */ TEST(new_test_msg(tt, &msg, "reuse-1", 1, 1024), 0); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST_1(tp0 = tport_incref(tp)); TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1); TEST(reuse, 0); msg_destroy(msg), msg = NULL; TEST(new_test_msg(tt, &msg, "reuse-2", 1, 1024), 0); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST_1(tp1 = tport_incref(tp)); TEST_1(tp0 != tp1); TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1); TEST(reuse, 0); msg_destroy(msg), msg = NULL; /* Receive every message from queue */ for (tt->tt_received = 0; tt->tt_received < 2;) { TEST(tport_test_run(tt, 5), 1); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; } /* Enable reuse on single connection */ TEST(tport_set_params(tp1, TPTAG_REUSE(1), TAG_END()), 1); TEST(new_test_msg(tt, &msg, "reuse-3", 1, 1024), 0); TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TPTAG_REUSE(1), TAG_END())); TEST_S(tport_name(tp)->tpn_ident, "client"); TEST_1(tp1 == tp); TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1); TEST(reuse, 1); msg_destroy(msg), msg = NULL; TEST(tport_test_run(tt, 5), 1); msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL; TEST_1(tp = tport_by_name(tt->tt_tports, tpn)); TEST_1(tport_is_primary(tp)); TEST(tport_set_params(tp, TPTAG_REUSE(1), TAG_END()), 1); /* Send a single message with different connection */ TEST_1(!new_test_msg(tt, &msg, "fresh-1", 1, 1024));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -