diff options
author | Alfred E. Heggestad <alfred.heggestad@gmail.com> | 2017-01-21 14:52:53 +0100 |
---|---|---|
committer | Alfred E. Heggestad <alfred.heggestad@gmail.com> | 2017-01-21 14:52:53 +0100 |
commit | a5d4cf8631e140a2224e4b73712c8b2c2251b6e6 (patch) | |
tree | ddd87aa926789add57368b320e511a368d96a6c2 | |
parent | db53294c570840aaecffd6e8cc905f491ed2aa5b (diff) |
account: add tests and accessors
-rw-r--r-- | include/baresip.h | 3 | ||||
-rw-r--r-- | src/account.c | 62 | ||||
-rw-r--r-- | src/core.h | 2 | ||||
-rw-r--r-- | src/ua.c | 8 | ||||
-rw-r--r-- | test/account.c | 64 | ||||
-rw-r--r-- | test/main.c | 1 | ||||
-rw-r--r-- | test/srcs.mk | 1 | ||||
-rw-r--r-- | test/test.h | 22 |
8 files changed, 149 insertions, 14 deletions
diff --git a/include/baresip.h b/include/baresip.h index 9984834..8514f21 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -55,7 +55,10 @@ struct list *account_vidcodecl(const struct account *acc); struct sip_addr *account_laddr(const struct account *acc); uint32_t account_regint(const struct account *acc); uint32_t account_pubint(const struct account *acc); +uint32_t account_ptime(const struct account *acc); enum answermode account_answermode(const struct account *acc); +const char *account_auth_user(const struct account *acc); +const char *account_outbound(const struct account *acc, unsigned ix); /* diff --git a/src/account.c b/src/account.c index 009fcf1..40875f5 100644 --- a/src/account.c +++ b/src/account.c @@ -23,8 +23,8 @@ static void destructor(void *arg) list_clear(&acc->vidcodecl); mem_deref(acc->auth_user); mem_deref(acc->auth_pass); - for (i=0; i<ARRAY_SIZE(acc->outbound); i++) - mem_deref(acc->outbound[i]); + for (i=0; i<ARRAY_SIZE(acc->outboundv); i++) + mem_deref(acc->outboundv[i]); mem_deref(acc->regq); mem_deref(acc->rtpkeep); mem_deref(acc->sipnat); @@ -284,19 +284,20 @@ static int sip_params_decode(struct account *acc, const struct sip_addr *aor) err |= param_dstr(&acc->regq, &aor->params, "regq"); - for (i=0; i<ARRAY_SIZE(acc->outbound); i++) { + for (i=0; i<ARRAY_SIZE(acc->outboundv); i++) { char expr[16] = "outbound"; expr[8] = i + 1 + 0x30; expr[9] = '\0'; - err |= param_dstr(&acc->outbound[i], &aor->params, expr); + err |= param_dstr(&acc->outboundv[i], &aor->params, expr); } /* backwards compat */ - if (!acc->outbound[0]) { - err |= param_dstr(&acc->outbound[0], &aor->params, "outbound"); + if (!acc->outboundv[0]) { + err |= param_dstr(&acc->outboundv[0], &aor->params, + "outbound"); } err |= param_dstr(&acc->sipnat, &aor->params, "sipnat"); @@ -499,6 +500,49 @@ enum answermode account_answermode(const struct account *acc) } +/** + * Get the authentication username of an account + * + * @param acc User-Agent account + * + * @return Authentication username + */ +const char *account_auth_user(const struct account *acc) +{ + return acc ? acc->auth_user : NULL; +} + + +/** + * Get the outbound SIP server of an account + * + * @param acc User-Agent account + * @param ix Index starting at zero + * + * @return Outbound SIP proxy, NULL if not configured + */ +const char *account_outbound(const struct account *acc, unsigned ix) +{ + if (!acc || ix >= ARRAY_SIZE(acc->outboundv)) + return NULL; + + return acc->outboundv[ix]; +} + + +/** + * Get the audio packet-time (ptime) of an account + * + * @param acc User-Agent account + * + * @return Packet-time (ptime) + */ +uint32_t account_ptime(const struct account *acc) +{ + return acc ? acc->ptime : 0; +} + + static const char *answermode_str(enum answermode mode) { switch (mode) { @@ -543,10 +587,10 @@ int account_debug(struct re_printf *pf, const struct account *acc) acc->mencid ? acc->mencid : "none"); err |= re_hprintf(pf, " medianat: %s\n", acc->mnatid ? acc->mnatid : "none"); - for (i=0; i<ARRAY_SIZE(acc->outbound); i++) { - if (acc->outbound[i]) { + for (i=0; i<ARRAY_SIZE(acc->outboundv); i++) { + if (acc->outboundv[i]) { err |= re_hprintf(pf, " outbound%d: %s\n", - i+1, acc->outbound[i]); + i+1, acc->outboundv[i]); } } err |= re_hprintf(pf, " ptime: %u\n", acc->ptime); @@ -61,7 +61,7 @@ struct account { char *mencid; /**< Media encryption type */ const struct mnat *mnat; /**< MNAT module */ const struct menc *menc; /**< MENC module */ - char *outbound[2]; /**< Optional SIP outbound proxies */ + char *outboundv[2]; /**< Optional SIP outbound proxies */ uint32_t ptime; /**< Configured packet time in [ms] */ uint32_t regint; /**< Registration interval in [seconds] */ uint32_t pubint; /**< Publication interval in [seconds] */ @@ -197,7 +197,7 @@ int ua_register(struct ua *ua) struct reg *reg = le->data; err = reg_register(reg, reg_uri, params, - acc->regint, acc->outbound[i]); + acc->regint, acc->outboundv[i]); if (err) { warning("ua: SIP register failed: %m\n", err); goto out; @@ -647,9 +647,9 @@ int ua_alloc(struct ua **uap, const char *aor) goto out; } - for (i=0; i<ARRAY_SIZE(ua->acc->outbound); i++) { + for (i=0; i<ARRAY_SIZE(ua->acc->outboundv); i++) { - if (ua->acc->outbound[i] && ua->acc->regint) { + if (ua->acc->outboundv[i] && ua->acc->regint) { err = reg_add(&ua->regl, ua, (int)i+1); if (err) break; @@ -1001,7 +1001,7 @@ void ua_presence_status_set(struct ua *ua, const enum presence_status status) const char *ua_outbound(const struct ua *ua) { /* NOTE: we pick the first outbound server, should be rotated? */ - return ua ? ua->acc->outbound[0] : NULL; + return ua ? ua->acc->outboundv[0] : NULL; } diff --git a/test/account.c b/test/account.c new file mode 100644 index 0000000..6b37a6c --- /dev/null +++ b/test/account.c @@ -0,0 +1,64 @@ +/** + * @file test/account.c Tests for account + * + * Copyright (C) 2010 - 2017 Creytiv.com + */ +#include <string.h> +#include <re.h> +#include <baresip.h> +#include "test.h" + + +#define DEBUG_MODULE "account" +#define DEBUG_LEVEL 5 +#include <re_dbg.h> + + +static const char str[] = + "\"Mr User\" <sip:user:pass@domain.com>" + ";answermode=auto" + ";auth_user=xuser" + ";outbound=\"sip:edge.domain.com\"" + ";ptime=10" + ";regint=600" + ";pubint=700" + ";sipnat=outbound" + ";stunserver=\"stun:stun_user:stun_pass@stunserver.org\"" + ; + + +int test_account(void) +{ + struct account *acc = NULL; + struct sip_addr *addr; + int err = 0; + + err = account_alloc(&acc, str); + TEST_ERR(err); + ASSERT_TRUE(acc != NULL); + + /* verify the decoded SIP aor */ + addr = account_laddr(acc); + ASSERT_TRUE(addr != NULL); + TEST_STRCMP("Mr User", 7, addr->dname.p, addr->dname.l); + TEST_STRCMP("sip", 3, addr->uri.scheme.p, addr->uri.scheme.l); + TEST_STRCMP("user", 4, addr->uri.user.p, addr->uri.user.l); + TEST_STRCMP("pass", 4, addr->uri.password.p, addr->uri.password.l); + TEST_STRCMP("domain.com", 10, addr->uri.host.p, addr->uri.host.l); + ASSERT_EQ(0, addr->uri.params.l); + ASSERT_TRUE(addr->params.l > 0); + + /* verify all decoded parameters */ + ASSERT_TRUE(ANSWERMODE_AUTO == account_answermode(acc)); + ASSERT_STREQ("xuser", account_auth_user(acc)); + ASSERT_STREQ("sip:edge.domain.com", account_outbound(acc, 0)); + ASSERT_TRUE(NULL == account_outbound(acc, 1)); + ASSERT_TRUE(NULL == account_outbound(acc, 333)); + ASSERT_EQ(10, account_ptime(acc)); + ASSERT_EQ(600, account_regint(acc)); + ASSERT_EQ(700, account_pubint(acc)); + + out: + mem_deref(acc); + return err; +} diff --git a/test/main.c b/test/main.c index cbfe686..ba4deea 100644 --- a/test/main.c +++ b/test/main.c @@ -19,6 +19,7 @@ struct test { #define TEST(a) {a, #a} static const struct test tests[] = { + TEST(test_account), TEST(test_call_af_mismatch), TEST(test_call_answer), TEST(test_call_answer_hangup_a), diff --git a/test/srcs.mk b/test/srcs.mk index 29cd7b3..289607e 100644 --- a/test/srcs.mk +++ b/test/srcs.mk @@ -8,6 +8,7 @@ # # Test-cases: # +TEST_SRCS += account.c TEST_SRCS += cmd.c TEST_SRCS += contact.c TEST_SRCS += ua.c diff --git a/test/test.h b/test/test.h index 0f6d203..1d30eb9 100644 --- a/test/test.h +++ b/test/test.h @@ -53,6 +53,27 @@ goto out; \ } +#define TEST_STRCMP(expected, expn, actual, actn) \ + if (expn != actn || \ + 0 != memcmp((expected), (actual), (expn))) { \ + (void)re_fprintf(stderr, "\n"); \ + DEBUG_WARNING("TEST_STRCMP: %s:%u:" \ + " failed\n", \ + __FILE__, __LINE__); \ + (void)re_fprintf(stderr, \ + "expected string: (%zu bytes)\n" \ + "\"%b\"\n", \ + (size_t)(expn), \ + (expected), (size_t)(expn)); \ + (void)re_fprintf(stderr, \ + "actual string: (%zu bytes)\n" \ + "\"%b\"\n", \ + (size_t)(actn), \ + (actual), (size_t)(actn)); \ + err = EINVAL; \ + goto out; \ + } + /* helpers */ @@ -120,6 +141,7 @@ int mock_vidisp_register(struct vidisp **vidispp); /* test cases */ +int test_account(void); int test_cmd(void); int test_cmd_long(void); int test_contact(void); |