summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfred E. Heggestad <aeh@db.org>2015-10-04 21:24:31 +0200
committerAlfred E. Heggestad <aeh@db.org>2015-10-04 21:24:31 +0200
commit5cbd1d17177236f7c4515f077c18d328c76af9b1 (patch)
tree287a08249ce9a545318c67985688902e221b4f6d
parent26b1c86e32abc1d2543d667c80b3532e6e178c16 (diff)
test: use new sipsrv from retest
-rw-r--r--Makefile7
-rw-r--r--test/main.c2
-rw-r--r--test/mock/cert.c82
-rw-r--r--test/mock/sipsrv.c145
-rw-r--r--test/sip_server.c113
-rw-r--r--test/srcs.mk18
-rw-r--r--test/test.h32
-rw-r--r--test/ua.c27
8 files changed, 296 insertions, 130 deletions
diff --git a/Makefile b/Makefile
index ebf3547..a6449a4 100644
--- a/Makefile
+++ b/Makefile
@@ -49,6 +49,9 @@ CXXFLAGS += -I$(LIBREM_PATH)/include
CXXFLAGS += -I$(SYSROOT)/local/include/rem -I$(SYSROOT)/include/rem
CXXFLAGS += $(EXTRA_CXXFLAGS)
+# XXX: common for C/C++
+CPPFLAGS += -DHAVE_INTTYPES_H
+
ifneq ($(LIBREM_PATH),)
SPLINT_OPTIONS += -I$(LIBREM_PATH)/include
CLANG_OPTIONS += -I$(LIBREM_PATH)/include
@@ -195,7 +198,7 @@ $(BUILD)/%.o: %.c $(BUILD) Makefile $(APP_MK)
$(BUILD)/%.o: %.cpp $(BUILD) Makefile $(APP_MK)
@echo " CXX $@"
- @$(CXX) $(CXXFLAGS) -c $< -o $@ $(DFLAGS)
+ @$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ $(DFLAGS)
$(BUILD)/%.o: %.m $(BUILD) Makefile $(APP_MK)
@echo " OC $@"
@@ -206,7 +209,7 @@ $(BUILD)/%.o: %.S $(BUILD) Makefile $(APP_MK)
@$(CC) $(CFLAGS) -c $< -o $@ $(DFLAGS)
$(BUILD): Makefile
- @mkdir -p $(BUILD)/src $(MOD_BLD) $(BUILD)/test
+ @mkdir -p $(BUILD)/src $(MOD_BLD) $(BUILD)/test/mock
@touch $@
install: $(BIN) $(MOD_BINS)
diff --git a/test/main.c b/test/main.c
index 11cc1b2..d39adfe 100644
--- a/test/main.c
+++ b/test/main.c
@@ -45,7 +45,7 @@ int main(void)
if (err)
return err;
- re_printf("running test..\n");
+ re_printf("running test version %s\n", BARESIP_VERSION);
/* note: run SIP-traffic on localhost */
config = conf_config();
diff --git a/test/mock/cert.c b/test/mock/cert.c
new file mode 100644
index 0000000..a711082
--- /dev/null
+++ b/test/mock/cert.c
@@ -0,0 +1,82 @@
+/**
+ * @file cert.c TLS Certificate
+ *
+ * Copyright (C) 2010 Creytiv.com
+ */
+#include <re.h>
+#include "../test.h"
+
+
+/**
+ * Dummy certificate for testing.
+ *
+ *
+ * It was generated like this:
+ *
+ * $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem \
+ * -days 36500 -nodes
+ *
+ *
+ * Dumping information:
+ *
+ * $ openssl x509 -subject -dates -fingerprint -in cert.pem
+ * subject= /C=NO/ST=Retest/O=Retest AS/CN=Mr Retest/emailAddress=re@test.org
+ * notBefore=Nov 23 18:40:38 2014 GMT
+ * notAfter=Oct 30 18:40:38 2114 GMT
+ * Fingerprint=49:A4:E9:F4:80:3A:D4:38:84:F1:64:C3:B9:4B:F9:BB:80:F7:07:76
+ */
+
+const char test_certificate[] =
+
+"-----BEGIN CERTIFICATE-----\r\n"
+"MIIDmTCCAoGgAwIBAgIJAIt1/MAlTpB7MA0GCSqGSIb3DQEBCwUAMGIxCzAJBgNV\r\n"
+"BAYTAk5PMQ8wDQYDVQQIDAZSZXRlc3QxEjAQBgNVBAoMCVJldGVzdCBBUzESMBAG\r\n"
+"A1UEAwwJTXIgUmV0ZXN0MRowGAYJKoZIhvcNAQkBFgtyZUB0ZXN0Lm9yZzAgFw0x\r\n"
+"NDExMjMxODQwMzhaGA8yMTE0MTAzMDE4NDAzOFowYjELMAkGA1UEBhMCTk8xDzAN\r\n"
+"B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u7XcFh6f8kDThP+BubfUWpw\r\n"
+"rCRq74gF1JzQISoKqmyW/AXMZQKBgQDNln0jzgh/ySWRjJbuh6GqPQunDQsh3K3I\r\n"
+"4WDHK40NHFgIzPomKO+pqsOu3V/X81NARqfUyoIp/455YRheLHBUJg7maLfxrBq/\r\n"
+"qQPEUwSAI6lheMz1WNWri65GvwBlVENajVuMh/xfmKVL1KVV+LXGO5L1UClWITCM\r\n"
+"VSC0QT8XNwKBgQC7bYUWS+JdAIp0su36MDrCgzPM0HFlbpzGkZMYq9qeG3Z8TGWb\r\n"
+"QQyR9UYSxjDwyqn5xr9BXyABG0SJr2UCiZosps8YMXEHE4d3eumzfdi4ALEx2YlH\r\n"
+"xVwZf9uG9Gy21D9svBW102YX8+Q94diJcZgezTBhZaKqrf4/uMl2cUh1eQKBgQDB\r\n"
+"heZYXOqdN0A5CUlOUbg5YutkHaAcCPpBvP33niRRchvgdOsIHsKzSL6ZDWPaCP+V\r\n"
+"4qy7XsE2PYzk7yQcCeLXI1glRe/Y+3PWdIfKN4dmA6u+yBLO5QeFSqALkmISAEbC\r\n"
+"p4vE9oD3j94RSqM0EUEy0ANfDk1K+UUU5FE7vKth8wKBgQCoKvPNfC3FmyhLK7EK\r\n"
+"zgfIYAcoi52EFu6xQ9PvZHg60ptYvq1L+H6LR8cxYfcrPTt3aDbFf41RahPkokNh\r\n"
+"2HDxiHd4HwWkAGiqZXCTA2znb+rnJ9fheI6g/Wb3p+oGeCFHMcdUcsl1qWBFDtax\r\n"
+"ygS/tEFgy1z2dVMLbLKqEUscsA==\r\n"
+"-----END PRIVATE KEY-----\r\n"
+;
diff --git a/test/mock/sipsrv.c b/test/mock/sipsrv.c
new file mode 100644
index 0000000..0707962
--- /dev/null
+++ b/test/mock/sipsrv.c
@@ -0,0 +1,145 @@
+/**
+ * @file mock/sipsrv.c Mock SIP server
+ *
+ * Copyright (C) 2010 - 2015 Creytiv.com
+ */
+#include <string.h>
+#include <re.h>
+#include "../test.h"
+
+
+#define DEBUG_MODULE "mock/sipsrv"
+#define DEBUG_LEVEL 5
+#include <re_dbg.h>
+
+
+#define LOCAL_PORT 0
+#define LOCAL_SECURE_PORT 0
+
+
+static bool sip_msg_handler(const struct sip_msg *msg, void *arg)
+{
+ struct sip_server *srv = arg;
+ int err;
+
+#if 0
+ DEBUG_NOTICE("recv %r via %s\n", &msg->met, sip_transp_name(msg->tp));
+#endif
+
+ if (0 == pl_strcmp(&msg->met, "REGISTER")) {
+ ++srv->n_register_req;
+ }
+ else {
+ DEBUG_NOTICE("method not handled (%r)\n", &msg->met);
+ return false;
+ }
+
+ srv->tp_last = msg->tp;
+
+ if (srv->terminate)
+ err = sip_reply(srv->sip, msg, 503, "Server Error");
+ else
+ err = sip_reply(srv->sip, msg, 200, "OK");
+
+ if (err) {
+ DEBUG_WARNING("could not reply: %m\n", err);
+ }
+
+#if 1
+ if (srv->terminate)
+ re_cancel();
+#endif
+
+ return true;
+}
+
+
+static void destructor(void *arg)
+{
+ struct sip_server *srv = arg;
+
+ srv->terminate = true;
+
+ sip_close(srv->sip, false);
+ mem_deref(srv->sip);
+}
+
+
+int sip_server_alloc(struct sip_server **srvp)
+{
+ struct sip_server *srv;
+ struct sa laddr, laddrs;
+ struct tls *tls = NULL;
+ int err;
+
+ if (!srvp)
+ return EINVAL;
+
+ srv = mem_zalloc(sizeof *srv, destructor);
+ if (!srv)
+ return ENOMEM;
+
+ err = sa_set_str(&laddr, "127.0.0.1", LOCAL_PORT);
+ err |= sa_set_str(&laddrs, "127.0.0.1", LOCAL_SECURE_PORT);
+ if (err)
+ goto out;
+
+ err = sip_alloc(&srv->sip, NULL, 16, 16, 16,
+ "mock SIP server", NULL, NULL);
+ if (err)
+ goto out;
+
+ err |= sip_transp_add(srv->sip, SIP_TRANSP_UDP, &laddr);
+ err |= sip_transp_add(srv->sip, SIP_TRANSP_TCP, &laddr);
+ if (err)
+ goto out;
+
+#ifdef USE_TLS
+ err = tls_alloc(&tls, TLS_METHOD_SSLV23, NULL, NULL);
+ if (err)
+ goto out;
+
+ err = tls_set_certificate(tls, test_certificate,
+ strlen(test_certificate));
+ if (err)
+ goto out;
+
+ err |= sip_transp_add(srv->sip, SIP_TRANSP_TLS, &laddrs, tls);
+#endif
+ if (err)
+ goto out;
+
+ err = sip_listen(&srv->lsnr, srv->sip, true, sip_msg_handler, srv);
+ if (err)
+ goto out;
+
+ out:
+ mem_deref(tls);
+ if (err)
+ mem_deref(srv);
+ else
+ *srvp = srv;
+
+ return err;
+}
+
+
+int sip_server_uri(struct sip_server *srv, char *uri, size_t sz,
+ enum sip_transp tp)
+{
+ struct sa laddr;
+ int err;
+
+ if (!srv || !uri || !sz)
+ return EINVAL;
+
+ err = sip_transp_laddr(srv->sip, &laddr, tp, NULL);
+ if (err)
+ return err;
+
+ if (re_snprintf(uri, sz, "sip:x:x@%J%s",
+ &laddr, sip_transp_param(tp)) < 0)
+ return ENOMEM;
+
+ return 0;
+}
diff --git a/test/sip_server.c b/test/sip_server.c
deleted file mode 100644
index 0e637de..0000000
--- a/test/sip_server.c
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- * @file sip_server.c Selftest for Baresip core -- SIP Server
- *
- * Copyright (C) 2010 Creytiv.com
- */
-#include <string.h>
-#include <re.h>
-#include <baresip.h>
-#include "test.h"
-
-
-static void udp_recv(const struct sa *src, struct mbuf *mb, void *arg)
-{
- struct sip_server *srv = arg;
- struct sip_msg *msg;
- int err;
-
-#if 0
- re_printf("sip: %zu bytes from %J\n", mbuf_get_left(mb), src);
- re_printf("%b\n", mb->buf, mb->end);
-#endif
-
- err = sip_msg_decode(&msg, mb);
- if (err) {
- warning("selftest: sip_msg_decode: %m\n", err);
- return;
- }
-
- if (0 == pl_strcmp(&msg->met, "REGISTER"))
- srv->got_register_req = true;
-
- msg->sock = mem_ref(srv->us);
- msg->src = *src;
- msg->dst = srv->laddr;
- msg->tp = SIP_TRANSP_UDP;
-
- if (srv->terminate)
- err = sip_reply(srv->sip, msg, 503, "Server Error");
- else
- err = sip_reply(srv->sip, msg, 200, "OK");
- if (err) {
- warning("selftest: could not reply: %m\n", err);
- }
-
- mem_deref(msg);
-
- if (srv->terminate)
- re_cancel();
-}
-
-
-static void destructor(void *arg)
-{
- struct sip_server *srv = arg;
-
- sip_close(srv->sip, false);
- mem_deref(srv->sip);
-
- mem_deref(srv->us);
-}
-
-
-int sip_server_create(struct sip_server **srvp)
-{
- struct sip_server *srv;
- struct sa laddr_tp;
- int err;
-
- srv = mem_zalloc(sizeof *srv, destructor);
- if (!srv)
- return ENOMEM;
-
- err = sa_set_str(&laddr_tp, "127.0.0.1", 0);
- err |= sa_set_str(&srv->laddr, "127.0.0.1", 0);
- if (err)
- goto out;
-
- err = sip_alloc(&srv->sip, NULL, 16, 16, 16,
- "dummy SIP registrar", NULL, NULL);
- if (err)
- goto out;
-
- err = sip_transp_add(srv->sip, SIP_TRANSP_UDP, &laddr_tp);
- if (err) {
- warning("failed to add sip transport (%m)\n", err);
- goto out;
- }
-
- err = udp_listen(&srv->us, &srv->laddr, udp_recv, srv);
- if (err) {
- warning("sip: udp_listen on '%J' failed (%d/%m)\n",
- &srv->laddr, err, err);
- goto out;
- }
-
- err = udp_local_get(srv->us, &srv->laddr);
- if (err) {
- warning("sip: udp_local_get\n");
- goto out;
- }
-
-#if 0
- re_printf("sip: listen on %J\n", &srv->laddr);
-#endif
-
- out:
- if (err)
- mem_deref(srv);
- else
- *srvp = srv;
-
- return err;
-}
diff --git a/test/srcs.mk b/test/srcs.mk
index 4115fdb..1caafbb 100644
--- a/test/srcs.mk
+++ b/test/srcs.mk
@@ -4,12 +4,24 @@
# Copyright (C) 2010 Creytiv.com
#
+
+#
+# Test-cases:
+#
TEST_SRCS += cmd.c
TEST_SRCS += ua.c
+TEST_SRCS += cplusplus.c
-TEST_SRCS += sip_server.c
-TEST_SRCS += test.c
-TEST_SRCS += cplusplus.c
+#
+# Mocks
+#
+TEST_SRCS += mock/sipsrv.c
+ifneq ($(USE_TLS),)
+TEST_SRCS += mock/cert.c
+endif
+
+
+TEST_SRCS += test.c
TEST_SRCS += main.c
diff --git a/test/test.h b/test/test.h
index c2caa6a..61ddd27 100644
--- a/test/test.h
+++ b/test/test.h
@@ -33,21 +33,43 @@
goto out; \
}
+#define TEST_ERR(err) \
+ if ((err)) { \
+ (void)re_fprintf(stderr, "\n"); \
+ warning("TEST_ERR: %s:%u:" \
+ " (%m)\n", \
+ __FILE__, __LINE__, \
+ (err)); \
+ goto out; \
+ }
+
/* helpers */
int re_main_timeout(uint32_t timeout);
+
+#ifdef USE_TLS
+extern const char test_certificate[];
+#endif
+
+
+/*
+ * SIP Server
+ */
+
struct sip_server {
- struct sa laddr;
- struct udp_sock *us;
struct sip *sip;
-
- bool got_register_req;
+ struct sip_lsnr *lsnr;
bool terminate;
+
+ unsigned n_register_req;
+ enum sip_transp tp_last;
};
-int sip_server_create(struct sip_server **srvp);
+int sip_server_alloc(struct sip_server **srvp);
+int sip_server_uri(struct sip_server *srv, char *uri, size_t sz,
+ enum sip_transp tp);
/* test cases */
diff --git a/test/ua.c b/test/ua.c
index ddfa76e..80fd3a4 100644
--- a/test/ua.c
+++ b/test/ua.c
@@ -50,7 +50,7 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
}
-int test_ua_register(void)
+static int reg(enum sip_transp tp)
{
struct test t;
char aor[256];
@@ -58,17 +58,17 @@ int test_ua_register(void)
memset(&t, 0, sizeof t);
- err = sip_server_create(&t.srv);
+ err = sip_server_alloc(&t.srv);
if (err) {
warning("failed to create sip server (%d/%m)\n", err, err);
goto out;
}
- re_snprintf(aor, sizeof(aor), "sip:x:x@%J", &t.srv->laddr);
+ err = sip_server_uri(t.srv, aor, sizeof(aor), tp);
+ TEST_ERR(err);
err = ua_alloc(&t.ua, aor);
- if (err)
- goto out;
+ TEST_ERR(err);
err = uag_event_register(ua_event_handler, &t);
if (err)
@@ -82,7 +82,8 @@ int test_ua_register(void)
if (t.err)
err = t.err;
- ASSERT_TRUE(t.srv->got_register_req);
+ ASSERT_TRUE(t.srv->n_register_req > 0);
+ ASSERT_EQ(tp, t.srv->tp_last);
ASSERT_TRUE(t.got_register_ok);
out:
@@ -97,6 +98,20 @@ int test_ua_register(void)
}
+int test_ua_register(void)
+{
+ int err = 0;
+
+ err = reg(SIP_TRANSP_UDP);
+#if 0
+ err |= reg(SIP_TRANSP_TCP);
+ err |= reg(SIP_TRANSP_TLS);
+#endif
+
+ return err;
+}
+
+
int test_ua_alloc(void)
{
struct ua *ua;