summaryrefslogtreecommitdiff
path: root/test/ua.c
diff options
context:
space:
mode:
authorAlfred E. Heggestad <aeh@db.org>2016-04-02 14:44:27 +0200
committerAlfred E. Heggestad <aeh@db.org>2016-04-02 14:44:27 +0200
commit059b6a9f597419adc331ca7e1e2b40b090d907a1 (patch)
tree441eae974d945e1d3bcb89009ca9929e4d42d312 /test/ua.c
parenta80081cc08f65e3cd99d7b94cb1f849ee94dc47a (diff)
add test for SIP with DNS
Diffstat (limited to 'test/ua.c')
-rw-r--r--test/ua.c177
1 files changed, 169 insertions, 8 deletions
diff --git a/test/ua.c b/test/ua.c
index c039705..90bfe98 100644
--- a/test/ua.c
+++ b/test/ua.c
@@ -10,17 +10,31 @@
struct test {
- struct sip_server *srv;
+ struct sip_server *srvv[16];
+ size_t srvc;
struct ua *ua;
int err;
unsigned got_register_ok;
};
+static void test_reset(struct test *t)
+{
+ size_t i;
+
+ for (i=0; i<ARRAY_SIZE(t->srvv); i++)
+ mem_deref(t->srvv[i]);
+ mem_deref(t->ua);
+
+ memset(t, 0, sizeof(*t));
+}
+
+
static void ua_event_handler(struct ua *ua, enum ua_event ev,
struct call *call, const char *prm, void *arg)
{
struct test *t = arg;
+ size_t i;
int err = 0;
(void)call;
(void)prm;
@@ -38,7 +52,9 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
ASSERT_TRUE(ua_isregistered(t->ua));
/* Terminate SIP Server, then De-REGISTER */
- t->srv->terminate = true;
+ for (i=0; i<t->srvc; i++)
+ t->srvv[i]->terminate = true;
+
t->ua = mem_deref(t->ua);
}
@@ -58,13 +74,13 @@ static int reg(enum sip_transp tp)
memset(&t, 0, sizeof t);
- err = sip_server_alloc(&t.srv);
+ err = sip_server_alloc(&t.srvv[0]);
if (err) {
warning("failed to create sip server (%d/%m)\n", err, err);
goto out;
}
- err = sip_server_uri(t.srv, aor, sizeof(aor), tp);
+ err = sip_server_uri(t.srvv[0], aor, sizeof(aor), tp);
TEST_ERR(err);
err = ua_alloc(&t.ua, aor);
@@ -82,8 +98,8 @@ static int reg(enum sip_transp tp)
if (t.err)
err = t.err;
- ASSERT_TRUE(t.srv->n_register_req > 0);
- ASSERT_EQ(tp, t.srv->tp_last);
+ ASSERT_TRUE(t.srvv[0]->n_register_req > 0);
+ ASSERT_EQ(tp, t.srvv[0]->tp_last);
ASSERT_TRUE(t.got_register_ok > 0);
out:
@@ -91,8 +107,7 @@ static int reg(enum sip_transp tp)
warning("selftest: ua_register test failed (%m)\n", err);
}
uag_event_unregister(ua_event_handler);
- mem_deref(t.srv);
- mem_deref(t.ua);
+ test_reset(&t);
return err;
}
@@ -102,12 +117,18 @@ int test_ua_register(void)
{
int err = 0;
+ err = ua_init("test", true, true, true, false);
+ TEST_ERR(err);
+
err |= reg(SIP_TRANSP_UDP);
err |= reg(SIP_TRANSP_TCP);
#ifdef USE_TLS
err |= reg(SIP_TRANSP_TLS);
#endif
+ ua_close();
+
+ out:
return err;
}
@@ -169,3 +190,143 @@ int test_uag_find_param(void)
return err;
}
+
+
+#define SERVER_COUNT 1
+
+
+static const char *_sip_transp_srvid(enum sip_transp tp)
+{
+ switch (tp) {
+
+ case SIP_TRANSP_UDP: return "_sip._udp";
+ case SIP_TRANSP_TCP: return "_sip._tcp";
+ case SIP_TRANSP_TLS: return "_sips._tcp";
+ default: return "???";
+ }
+}
+
+
+static int reg_dns(enum sip_transp tp)
+{
+ struct dns_server *dnssrv = NULL;
+ struct test t;
+ const char *domain = "test.invalid";
+ char aor[256];
+ char srv[256];
+ size_t i;
+ int err;
+
+ memset(&t, 0, sizeof t);
+
+ /*
+ * Setup server-side mocks:
+ */
+
+ err = dns_server_alloc(&dnssrv, true);
+ TEST_ERR(err);
+
+ info("| DNS-server on %J\n", &dnssrv->addr);
+
+ /* NOTE: must be done before ua_init() */
+ err = net_dnssrv_add(&dnssrv->addr);
+ TEST_ERR(err);
+
+ for (i=0; i<SERVER_COUNT; i++) {
+ struct sa sip_addr;
+ char arec[256];
+
+ err = sip_server_alloc(&t.srvv[i]);
+ if (err) {
+ warning("failed to create sip server (%d/%m)\n",
+ err, err);
+ goto out;
+ }
+
+ err = sip_transp_laddr(t.srvv[i]->sip, &sip_addr, tp, NULL);
+ TEST_ERR(err);
+
+ info("| SIP-server on %J\n", &sip_addr);
+
+ re_snprintf(arec, sizeof(arec),
+ "alpha%u.%s", i+1, domain);
+
+ re_snprintf(srv, sizeof(srv),
+ "%s.%s", _sip_transp_srvid(tp), domain);
+ err = dns_server_add_srv(dnssrv, srv,
+ 20, 0, sa_port(&sip_addr),
+ arec);
+ TEST_ERR(err);
+
+ err = dns_server_add_a(dnssrv, arec, sa_in(&sip_addr));
+ TEST_ERR(err);
+ }
+ t.srvc = SERVER_COUNT;
+
+ /* NOTE: angel brackets needed to parse ;transport parameter */
+ if (re_snprintf(aor, sizeof(aor), "<sip:x:x@%s;transport=%s>",
+ domain, sip_transp_name(tp)) < 0)
+ return ENOMEM;
+
+ /*
+ * Start SIP client:
+ */
+
+ err = ua_init("test", true, true, true, false);
+ TEST_ERR(err);
+
+ err = ua_alloc(&t.ua, aor);
+ TEST_ERR(err);
+
+ err = uag_event_register(ua_event_handler, &t);
+ if (err)
+ goto out;
+
+ /* run main-loop with timeout, wait for events */
+ err = re_main_timeout(5000);
+ if (err)
+ goto out;
+
+ if (t.err)
+ err = t.err;
+
+ /* verify that all SIP requests was sent to the first
+ * SIP-server.
+ */
+ ASSERT_TRUE(t.srvv[0]->n_register_req > 0);
+ ASSERT_EQ(tp, t.srvv[0]->tp_last);
+ ASSERT_TRUE(t.got_register_ok > 0);
+
+ out:
+ if (err) {
+ warning("selftest: ua_register test failed (%m)\n", err);
+ }
+ uag_event_unregister(ua_event_handler);
+
+ test_reset(&t);
+
+ ua_stop_all(true);
+ ua_close();
+
+ mem_deref(dnssrv);
+
+ return err;
+}
+
+
+int test_ua_register_dns(void)
+{
+ int err = 0;
+
+ err |= reg_dns(SIP_TRANSP_UDP);
+ TEST_ERR(err);
+ err |= reg_dns(SIP_TRANSP_TCP);
+ TEST_ERR(err);
+#ifdef USE_TLS
+ err |= reg_dns(SIP_TRANSP_TLS);
+ TEST_ERR(err);
+#endif
+
+ out:
+ return err;
+}