Commit d7a39a0b authored by Pekka Pessi's avatar Pekka Pessi

check_nua: updated

darcs-hash:20080512230026-db55f-85f2a0674c7ee982d796df7f40a3ab85bd52e1b2.gz
parent a2ab682a
......@@ -43,8 +43,9 @@ libnua_la_SOURCES = nua.c nua_stack.h nua_common.c nua_stack.c \
COVERAGE_INPUT = $(libnua_la_SOURCES) $(include_sofia_HEADERS)
check_nua_SOURCES = check_nua.c check_nua.h check_session.c \
s2tester.h s2tester.c
check_nua_SOURCES = check_nua.c check_nua.h \
check_session.c check_register.c \
test_s2.h test_s2.c
check_nua_LDADD = $(nua_libs) @CHECK_LIBS@
......
......@@ -36,6 +36,43 @@
#include "check_nua.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if HAVE_FNMATCH_H
#include <fnmatch.h>
#endif
static char const * const default_patterns[] = { "*", NULL };
static char const * const *test_patterns = default_patterns;
void check_nua_tcase_add_test(TCase *tc, TFun tf, char const *name)
{
char const * const *patterns;
#if HAVE_FNMATCH_H
for (patterns = test_patterns; *patterns; patterns++) {
if (!fnmatch(*patterns, name, 0)) {
if (strcmp(*patterns, "*")) {
printf("%s: match with %s\n", name, *patterns);
}
_tcase_add_test(tc, tf, name, 0, 0, 1);
return;
}
}
#else
for (patterns = test_patterns; *patterns; patterns++) {
if (!strcmp(*patterns, name) || !strcmp(*patterns, "*")) {
if (strcmp(*patterns, "*")) {
printf("%s: match with %s\n", name, *patterns);
}
_tcase_add_test(tc, tf, name, 0, 0, 1);
return;
}
}
#endif
printf("%s: no match\n", name);
}
int main(int argc, char *argv[])
{
......@@ -44,6 +81,28 @@ int main(int argc, char *argv[])
Suite *suite = suite_create("Unit tests for Sofia-SIP UA Engine");
SRunner *runner;
if (getenv("CHECK_NUA_CASES")) {
size_t i;
char *s, **patterns;
char *cases = strdup(getenv("CHECK_NUA_CASES"));
/* Count commas */
for (i = 2, s = cases; (s = strchr(s, ',')); s++, i++);
patterns = calloc(i, sizeof *patterns);
for (i = 0, s = cases;; i++) {
patterns[i] = s;
if (s == NULL)
break;
s = strchr(s, ',');
if (s)
*s++ = '\0';
}
test_patterns = (char const * const *)patterns;
}
check_register_cases(suite);
check_session_cases(suite);
......
......@@ -2,6 +2,12 @@
#include <check.h>
#undef tcase_add_test
#define tcase_add_test(tc, tf) \
check_nua_tcase_add_test(tc, tf, "" #tf "")
void check_nua_tcase_add_test(TCase *, TFun, char const *name);
void check_session_cases(Suite *suite);
void check_register_cases(Suite *suite);
......
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2008 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@CFILE check_register.c
*
* @brief Check-driven tester for Sofia SIP User Agent library
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
*
* @copyright (C) 2008 Nokia Corporation.
*/
#include "config.h"
#include "check_nua.h"
#include "test_s2.h"
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/soa.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_tag_io.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static nua_t *nua;
static void register_setup(void)
{
nua = s2_nua_setup(TAG_END());
}
static void register_teardown(void)
{
nua_shutdown(nua);
fail_unless(s2_check_event(nua_r_shutdown, 200));
s2_nua_teardown();
}
/* ---------------------------------------------------------------------- */
START_TEST(register_1_0)
{
nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
struct message *m;
s2_case("1.3", "Failed Register", "REGISTER returned 403 response");
nua_register(nh, TAG_END());
fail_unless((m = s2_wait_for_request(SIP_METHOD_REGISTER)) != NULL, NULL);
s2_respond_to(m, NULL,
SIP_403_FORBIDDEN,
TAG_END());
s2_free_message(m);
nua_handle_destroy(nh);
} END_TEST
START_TEST(register_1_1_1)
{
s2_case("1.1.1", "Basic Register", "REGISTER returning 200 OK");
s2_register_setup();
s2_register_teardown();
} END_TEST
START_TEST(register_1_1_2)
{
nua_handle_t *nh;
struct message *m;
s2_case("1.1.2", "Register with dual authentication",
"Register, authenticate");
nh = nua_handle(nua, NULL, TAG_END());
nua_register(nh, TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
s2_respond_to(m, NULL,
SIP_407_PROXY_AUTH_REQUIRED,
SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 407);
nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
s2_respond_to(m, NULL,
SIP_401_UNAUTHORIZED,
SIPTAG_WWW_AUTHENTICATE_STR(s2_auth2_digest_str),
SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 401);
nua_authenticate(nh, NUTAG_AUTH(s2_auth2_credentials), TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_authorization);
fail_if(!m->sip->sip_proxy_authorization);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
TAG_END());
s2_free_message(m);
assert(s2->registration->contact != NULL);
s2_check_event(nua_r_register, 200);
s2->registration->nh = nh;
s2_register_teardown();
} END_TEST
/* ---------------------------------------------------------------------- */
static char const *receive_natted = "received=4.255.255.9";
/* Return Via that looks very natted */
static sip_via_t *natted_via(struct message *m)
{
su_home_t *h;
sip_via_t *via;
h = msg_home(m->msg);
via = sip_via_dup(h, m->sip->sip_via);
msg_header_replace_param(h, via->v_common, receive_natted);
if (via->v_protocol == sip_transport_udp)
msg_header_replace_param(h, via->v_common, "rport=9");
if (via->v_protocol == sip_transport_tcp && via->v_rport) {
tp_name_t const *tpn = tport_name(m->tport);
char *rport = su_sprintf(h, "rport=%s", tpn->tpn_port);
msg_header_replace_param(h, via->v_common, rport);
}
return via;
}
/* ---------------------------------------------------------------------- */
START_TEST(register_1_2_1) {
nua_handle_t *nh;
struct message *m;
s2_case("1.2.1", "Register behind NAT",
"Register through NAT, detect NAT, re-REGISTER");
nh = nua_handle(nua, NULL, TAG_END());
nua_register(nh, TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
assert(s2->registration->contact != NULL);
s2_check_event(nua_r_register, 100);
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
fail_unless(s2->registration->contact != NULL);
fail_if(s2->registration->contact->m_next != NULL);
s2_check_event(nua_r_register, 200);
s2->registration->nh = nh;
s2_register_teardown();
} END_TEST
static nua_handle_t *make_auth_natted_register(
nua_handle_t *nh,
tag_type_t tag, tag_value_t value, ...)
{
struct message *m;
ta_list ta;
ta_start(ta, tag, value);
nua_register(nh, ta_tags(ta));
ta_end(ta);
m = s2_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
s2_respond_to(m, NULL,
SIP_401_UNAUTHORIZED,
SIPTAG_WWW_AUTHENTICATE_STR(s2_auth_digest_str),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 401);
nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_authorization);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
assert(s2->registration->contact != NULL);
s2_check_event(nua_r_register, 200);
return nh;
}
START_TEST(register_1_2_2_1)
{
nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
s2_case("1.2.2.1", "Register behind NAT",
"Authenticate, outbound activated");
mark_point();
make_auth_natted_register(nh, TAG_END());
s2->registration->nh = nh;
s2_register_teardown();
}
END_TEST
START_TEST(register_1_2_2_2)
{
nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
struct message *m;
s2_case("1.2.2.2", "Register behind NAT",
"Authenticate, outbound activated, "
"authenticate OPTIONS probe, "
"NAT binding change");
mark_point();
make_auth_natted_register(nh, TAG_END());
s2->registration->nh = nh;
mark_point();
m = s2_wait_for_request(SIP_METHOD_OPTIONS);
fail_if(!m);
s2_respond_to(m, NULL,
SIP_407_PROXY_AUTH_REQUIRED,
SIPTAG_VIA(natted_via(m)),
SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
TAG_END());
s2_free_message(m);
mark_point();
m = s2_wait_for_request(SIP_METHOD_OPTIONS);
fail_if(!m); fail_if(!m->sip->sip_proxy_authorization);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
su_root_step(s2->root, 20); su_root_step(s2->root, 20);
s2_fast_forward(120); /* Default keepalive interval */
mark_point();
m = s2_wait_for_request(SIP_METHOD_OPTIONS);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
su_root_step(s2->root, 20); su_root_step(s2->root, 20);
s2_fast_forward(120); /* Default keepalive interval */
mark_point();
receive_natted = "received=4.255.255.10";
m = s2_wait_for_request(SIP_METHOD_OPTIONS);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
s2_check_event(nua_i_outbound, 0);
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m); fail_if(!m->sip->sip_authorization);
fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 200);
fail_unless(s2->registration->contact != NULL);
fail_if(s2->registration->contact->m_next != NULL);
s2_register_teardown();
} END_TEST
START_TEST(register_1_2_2_3)
{
nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
struct message *m;
s2_case("1.2.2.3", "Register behind NAT",
"Authenticate, outbound activated, "
"detect NAT binding change when re-REGISTERing");
mark_point();
make_auth_natted_register(nh,
NUTAG_OUTBOUND("no-options-keepalive"),
TAG_END());
s2->registration->nh = nh;
receive_natted = "received=4.255.255.10";
s2_fast_forward(3600);
mark_point();
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m); fail_if(!m->sip->sip_authorization);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 100);
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m); fail_if(!m->sip->sip_authorization);
fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
fail_unless(s2->registration->contact != NULL);
fail_if(s2->registration->contact->m_next != NULL);
s2_check_event(nua_r_register, 200);
s2_register_teardown();
} END_TEST
START_TEST(register_1_2_3) {
nua_handle_t *nh;
struct message *m;
s2_case("1.2.3", "Register behind NAT",
"Outbound activated by error response");
nh = nua_handle(nua, NULL, TAG_END());
nua_register(nh, TAG_END());
mark_point();
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
s2_respond_to(m, NULL,
400, "Bad Contact",
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
s2_check_event(nua_r_register, 100);
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
fail_unless(s2->registration->contact != NULL);
fail_if(s2->registration->contact->m_next != NULL);
s2_check_event(nua_r_register, 200);
s2->registration->nh = nh;
s2_register_teardown();
} END_TEST
/* ---------------------------------------------------------------------- */
START_TEST(register_1_3_1)
{
nua_handle_t *nh;
struct message *m;
s2_case("1.3.1", "Register over TCP via NAT",
"REGISTER via TCP, detect NTA, re-REGISTER");
nh = nua_handle(nua, NULL, TAG_END());
nua_register(nh, NUTAG_PROXY(s2->tcp.contact->m_url), TAG_END());
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m); fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
fail_if(!tport_is_tcp(m->tport));
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
assert(s2->registration->contact != NULL);
s2_check_event(nua_r_register, 100);
m = s2_wait_for_request(SIP_METHOD_REGISTER);
fail_if(!m);
fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
s2_save_register(m);
s2_respond_to(m, NULL,
SIP_200_OK,
SIPTAG_CONTACT(s2->registration->contact),
SIPTAG_VIA(natted_via(m)),
TAG_END());
s2_free_message(m);
fail_unless(s2->registration->contact != NULL);
fail_if(s2->registration->contact->m_next != NULL);
fail_unless(
url_has_param(s2->registration->contact->m_url, "transport=tcp"));
s2_check_event(nua_r_register, 200);
s2->registration->nh = nh;