⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_tport.c

📁 Sofia SIP is an open-source SIP User-Agent library, compliant with the IETF RFC3261 specification.
💻 C
📖 第 1 页 / 共 3 页
字号:
  msg_t *msg = NULL;  int i, n;  tport_t *tp, *tp0;  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, "cid:sctp-first", 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);  tport_set_params(tp, TPTAG_KEEPALIVE(100), TPTAG_IDLE(500), TAG_END());  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;  tp0 = tport_ref(tp);  pending_server_close = pending_client_close = 0;  /* Ask for notification upon close */  pending_client_close = tport_pend(tp, NULL, client_closed_callback, NULL);  TEST_1(pending_client_close > 0);  tp = tt->tt_rtport;  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);  TEST_1(pending_server_close > 0);  if (0) { /* SCTP does not work reliably. Really. */  tt->tt_received = 0;  /* 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(tp = tport_tsend(tp0, 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(tp = tport_tsend(tp0, msg, tt->tt_sctp_name, TAG_END()));    msg_destroy(msg);  }  /* Try to overflow 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 < n + TPORT_QUEUESIZE - 1;) {    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, 512));  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;  }  tport_unref(tp0);  /* Wait until notifications -      client closes when idle and notifys pending,      then server closes and notifys pending */  while (pending_server_close || pending_client_close)    su_root_step(tt->tt_root, 50);  END();}struct called {  int n, error, pending, released;};staticvoid tls_error_callback(tp_stack_t *tt, tp_client_t *client,			tport_t *tp, msg_t *msg, int error){  struct called *called = (struct called *)client;  tt->tt_status = -1;  called->n++, called->error = error;  if (called->pending) {    called->released = tport_release(tp, called->pending, msg, NULL, client, 0);    called->pending = 0;  }}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];  tport_t *tp, *tp0;  struct called called[1] = {{ 0, 0, 0, 0 }};  TEST_S(dst->tpn_proto, "tls");  /* Send a single message */  TEST_1(!new_test_msg(tt, &msg, "tls-first", 1, 1024));  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));  TEST_1(tp0 = tport_ref(tp));  TEST_1(called->pending = tport_pend(tp, msg, tls_error_callback, (tp_client_t *)called));  i = tport_test_run(tt, 5);  msg_destroy(msg);  if (i < 0) {    if (called->n) {      TEST(called->released, 0);      puts("test_tport: skipping TLS tests");      tport_unref(tp0);      return 0;    }  }  TEST(i, 1);  TEST_1(!check_msg(tt, tt->tt_rmsg, "tls-first"));  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;  tport_set_params(tp, TPTAG_KEEPALIVE(100), TPTAG_IDLE(500), TAG_END());  /* Ask for notification upon close */  pending_client_close = tport_pend(tp0, NULL, client_closed_callback, NULL);  TEST_1(pending_client_close > 0);  tp = tt->tt_rtport;  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);  TEST_1(pending_server_close > 0);  /* Send a largish message */  TEST_1(!new_test_msg(tt, &msg, "tls-0", 16, 16 * 1024));  test_create_md5(tt, msg);  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));  TEST_1(tp == tp0);  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(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));    TEST_1(tp == tp0);    msg_destroy(msg);  }  /* This uses a new connection */  TEST_1(!new_test_msg(tt, &msg, "tls-no-reuse", 1, 1024));  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, 		     TPTAG_REUSE(0), TAG_END()));  TEST_1(tp != tp0);  msg_destroy(msg);  tt->tt_received = 0;  /* 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;  tport_decref(&tp0);  /* Wait until notifications -      client closes when idle and notifys pending,      then server closes and notifys pending */  while (pending_server_close || pending_client_close)    su_root_step(tt->tt_root, 50);  #endif  END();}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_P(result[0].t_tag, tptag_ident);  TEST_P(result[1].t_tag, tptag_ident);  free(lst);  su_home_deinit(home);  END();}#if HAVE_ALARM#include <signal.h>static RETSIGTYPE sig_alarm(int s){  fprintf(stderr, "%s: FAIL! test timeout!\n", name);  exit(1);}char const alarm_option[] = " [--no-alarm]";#elsechar const alarm_option[] = "";#endifvoid usage(int exitcode){  fprintf(stderr, "usage: %s [-v] [-a]%s\n", name, alarm_option);  exit(exitcode);}int main(int argc, char *argv[]){  int flags = 0;	/* XXX */  int retval = 0;  int no_alarm = 0;  int i;  tp_test_t tt[1] = {{{ SU_HOME_INIT(tt) }}};  for (i = 1; argv[i]; i++) {    if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbatim") == 0)      tstflags |= tst_verbatim;    else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--abort") == 0)      tstflags |= tst_abort;    else if (strcmp(argv[i], "--no-alarm") == 0)      no_alarm = 1;    else      usage(1);  }#if HAVE_OPEN_C  tstflags |= tst_verbatim;#endif#if HAVE_ALARM  if (!no_alarm) {    signal(SIGALRM, sig_alarm);    alarm(120);  }#endif  /* 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 |= test_incomplete(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();#if HAVE_OPEN_C  sleep(10);#endif  return retval;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -