📄 test_nth.c
字号:
/* * This file is part of the Sofia-SIP package * * Copyright (C) 2005 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 * *//**@file test_nth.c * @brief Tests for nth module * * @author Pekka Pessi <Pekka.Pessi@nokia.com> * * @date Created: Tue Oct 22 20:52:37 2002 ppessi */#include "config.h"#include <stddef.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <assert.h>#if HAVE_ALARM#include <unistd.h>#include <signal.h>#endiftypedef struct tester tester_t;typedef struct site site_t;typedef struct client client_t;#define SU_ROOT_MAGIC_T tester_t#include <sofia-sip/su_tagarg.h>#include <sofia-sip/su_wait.h>#define NTH_CLIENT_MAGIC_T client_t#define NTH_SITE_MAGIC_T site_t#include "sofia-sip/nth.h"#include <sofia-sip/http_header.h>#include <sofia-sip/msg_mclass.h>#include <sofia-sip/tport_tag.h>#include <sofia-sip/auth_module.h>int tstflags = 0;#define TSTFLAGS tstflags#include <sofia-sip/tstdef.h>#if HAVE_FUNC#elif HAVE_FUNCTION#define __func__ __FUNCTION__#else#define __func__ name#endifchar const name[] = "test_nth";static int init_test(tester_t *t);static int deinit_test(tester_t *t);static int test_nth_client_api(tester_t *t);static int test_nth_server_api(tester_t *t);static int init_server(tester_t *t);static int test_requests(tester_t *t);static int init_engine(tester_t *t);struct site { site_t *s_next, *s_parent; tester_t *s_tester; url_string_t *s_url; nth_site_t *s_ns; int s_called; int s_status; char const *s_phrase; tagi_t *s_tags;};struct client{ unsigned c_status;};struct tester{ su_home_t t_home[1]; su_root_t *t_root; msg_mclass_t *t_mclass; url_string_t *t_proxy; nth_engine_t *t_engine; char const *t_srcdir; char const *t_pem; su_sockaddr_t t_addr[1]; socklen_t t_addrlen; su_socket_t t_sink; url_string_t *t_sinkuri; su_sockaddr_t t_sinkaddr[1]; socklen_t t_sinkaddrlen; site_t *t_sites; site_t *t_master;};static int test_site(site_t *t, nth_site_t *server, nth_request_t *req, http_t const *http, char const *path);static site_t *site_create(tester_t *t, site_t *parent, char const *url, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...){ nth_site_t *pns = parent ? parent->s_ns : NULL; site_t *s; ta_list ta; if (url == NULL) return NULL; s = su_zalloc(t->t_home, sizeof *s); if (s == NULL) return NULL; s->s_url = URL_STRING_MAKE(url); s->s_tester = t; s->s_next = t->t_sites; s->s_status = status; s->s_phrase = phrase; ta_start(ta, tag, value); s->s_tags = tl_adup(t->t_home, ta_args(ta)); if (s->s_tags) s->s_ns = nth_site_create(pns, test_site, s, (url_string_t *)s->s_url, NTHTAG_ROOT(t->t_root), ta_tags(ta)); ta_end(ta); if (s->s_ns == NULL) return NULL; t->t_sites = s; return s;}static int init_test(tester_t *t){ su_socket_t s; BEGIN(); t->t_root = su_root_create(t); TEST_1(t->t_root); t->t_mclass = msg_mclass_clone(http_default_mclass(), 0, 0); TEST_1(t->t_mclass); t->t_addr->su_len = (sizeof t->t_addr->su_sin); s = su_socket(t->t_addr->su_family = AF_INET, SOCK_STREAM, 0); TEST_1(s != INVALID_SOCKET); TEST_1(su_inet_pton(AF_INET, "127.0.0.1", &t->t_addr->su_sin.sin_addr) >= 0); TEST_1(bind(s, &t->t_addr->su_sa, t->t_addrlen = (sizeof t->t_addr->su_sin)) != -1); TEST_1(getsockname(s, &t->t_addr->su_sa, &t->t_addrlen) != -1); TEST_1(t->t_addr->su_port != 0); TEST_1(su_close(s) != -1); t->t_pem = su_sprintf(t->t_home, "%s/agent.pem", t->t_srcdir); END();}static int deinit_test(tester_t *t){ site_t *s, *s_next; BEGIN(); nth_engine_destroy(t->t_engine); for (s = t->t_sites; s; s = s_next) { s_next = s->s_next; nth_site_destroy(s->s_ns), s->s_ns = NULL; su_free(t->t_home, s); } su_root_destroy(t->t_root); su_home_deinit(t->t_home); memset(t, 0, sizeof t); END();}static int test_nth_client_api(tester_t *t){ char const *s; BEGIN(); s = nth_engine_version(); TEST_1(s); TEST_1(strlen(s)); TEST_S(s, "sofia-http-client/" NTH_CLIENT_VERSION); TEST_1(nth_engine_create(NULL, TAG_END()) == NULL); TEST(errno, EINVAL); TEST_VOID(nth_engine_destroy(NULL)); TEST_1(nth_engine_get_params(NULL, TAG_END()) == -1); TEST_1(nth_engine_set_params(NULL, TAG_END()) == -1); TEST_1(!nth_client_tcreate(NULL, NULL, NULL, HTTP_METHOD_OPTIONS, URL_STRING_MAKE("*"), TAG_END())); TEST(nth_client_status(NULL), 400); TEST(nth_client_method(NULL), http_method_invalid); TEST(nth_client_is_streaming(NULL), 0); TEST_P(nth_client_url(NULL), NULL); TEST_P(nth_client_request(NULL), NULL); TEST_P(nth_client_response(NULL), NULL); TEST_VOID(nth_client_destroy(NULL)); t->t_engine = nth_engine_create(t->t_root, NTHTAG_ERROR_MSG(2), NTHTAG_MCLASS(t->t_mclass), NTHTAG_MFLAGS(MSG_DO_CANONIC|MSG_DO_COMPACT), NTHTAG_STREAMING(0), NTHTAG_PROXY("http://localhost:8888"), TAG_END()); TEST_1(t->t_engine); { int error_msg = -1; msg_mclass_t const *mclass = (void *)-1; int mflags = -1; unsigned expires = -1; int streaming = -1; url_string_t const *proxy = (void *)-1; char *proxy_str; TEST(nth_engine_get_params(t->t_engine, NTHTAG_ERROR_MSG_REF(error_msg), NTHTAG_MCLASS_REF(mclass), NTHTAG_MFLAGS_REF(mflags), NTHTAG_EXPIRES_REF(expires), NTHTAG_STREAMING_REF(streaming), NTHTAG_PROXY_REF(proxy), TAG_END()), 6); TEST(error_msg, 1); TEST_P(mclass, t->t_mclass); TEST(mflags, MSG_DO_CANONIC|MSG_DO_COMPACT); TEST(expires, 32000); TEST(streaming, 0); TEST_1(proxy != NULL); TEST_1(proxy_str = url_as_string(t->t_home, proxy->us_url)); TEST_S(proxy_str, "http://localhost:8888"); proxy = URL_STRING_MAKE("http://127.0.0.1:80"); TEST(nth_engine_set_params(t->t_engine, NTHTAG_ERROR_MSG(0), NTHTAG_MCLASS(http_default_mclass()), NTHTAG_MFLAGS(0), NTHTAG_EXPIRES(10000), NTHTAG_STREAMING(2), NTHTAG_PROXY(proxy), TAG_END()), 6); error_msg = -1; mclass = (void *)-1; mflags = -1; expires = (unsigned)-1; streaming = -1; proxy = (void *)-1; TEST(nth_engine_get_params(t->t_engine, NTHTAG_ERROR_MSG_REF(error_msg), NTHTAG_MCLASS_REF(mclass), NTHTAG_MFLAGS_REF(mflags), NTHTAG_EXPIRES_REF(expires), NTHTAG_STREAMING_REF(streaming), NTHTAG_PROXY_REF(proxy), TAG_END()), 6); TEST(error_msg, 0); TEST_P(mclass, NULL); TEST(mflags, 0); TEST(expires, 10000); TEST(streaming, 1); TEST_1(proxy != NULL); TEST_1(proxy_str = url_as_string(t->t_home, proxy->us_url)); TEST_S(proxy_str, "http://127.0.0.1:80"); } TEST_1(nth_engine_get_stats(NULL, TAG_END()) == -1); { msg_t *msg; http_t *http; TEST_1(nth_engine_msg_create(NULL, -1) == NULL); TEST_1(msg = nth_engine_msg_create(t->t_engine, -1)); TEST_1(http = http_object(msg)); TEST(http->http_flags, MSG_FLG_USERMASK); msg_destroy(msg); /* Use mflags set by set_params (+ streaming flag) */ TEST_1(msg = nth_engine_msg_create(t->t_engine, 0)); TEST_1(http = http_object(msg)); TEST(http->http_flags, MSG_FLG_STREAMING | t->t_mclass->mc_flags); msg_destroy(msg); } TEST_VOID(nth_engine_destroy(t->t_engine)); t->t_engine = NULL; END();}static int site_check_all(site_t *t, nth_site_t *server, nth_request_t *req, http_t const *http, char const *path);static int test_nth_server_api(tester_t *t){ char const *v; site_t s[1]; BEGIN(); memset(s, 0, sizeof s); v = nth_site_server_version(); TEST_1(v); TEST_1(strlen(v)); TEST_S(v, "nth/" NTH_SERVER_VERSION); /* Fails because no parent site, no root */ TEST_1(!nth_site_create(NULL, test_site, s, URL_STRING_MAKE("http://127.0.0.1:8888"), TAG_END())); /* Fails because url specifies both host and path */ TEST_1(!nth_site_create(NULL, site_check_all, s, URL_STRING_MAKE("http://127.0.0.1:8888/foo/"), NTHTAG_ROOT(t->t_root), TAG_END())); TEST_VOID(nth_site_destroy(NULL)); TEST_P(nth_site_magic(NULL), NULL); TEST_VOID(nth_site_bind(NULL, test_site, s)); TEST_1(nth_site_set_params(NULL, TAG_END()) == -1); TEST_1(nth_site_get_params(NULL, TAG_END()) == -1); TEST_1(nth_site_get_stats(NULL, TAG_END()) == -1); TEST(nth_request_status(NULL), 400); TEST(nth_request_method(NULL), http_method_invalid); TEST_P(nth_request_message(NULL), NULL); TEST_1(nth_request_treply(NULL, HTTP_200_OK, TAG_END()) == -1); TEST_VOID(nth_request_destroy(NULL)); END();}static int test_site(site_t *s, nth_site_t *ns, nth_request_t *req, http_t const *http, char const *path){ if (s == NULL || ns == NULL || req == NULL) return 500; TEST_1(nth_request_treply(req, s->s_status, s->s_phrase, TAG_NEXT(s->s_tags)) != -1); TEST_VOID(nth_request_destroy(req)); return s->s_status;}static int site_check_all(site_t *s, nth_site_t *ns, nth_request_t *req, http_t const *http, char const *path){ msg_t *msg; auth_status_t *as; TEST_1(s); TEST_1(ns); TEST_1(req); TEST_1(http); TEST_1(path); if (s == NULL || ns == NULL || req == NULL) return 500; TEST(nth_request_status(req), 0); TEST(nth_request_method(req), http_method_get); TEST_1(msg = nth_request_message(req)); msg_destroy(msg); as = nth_request_auth(req); TEST_1(nth_request_treply(req, s->s_status, s->s_phrase, TAG_NEXT(s->s_tags)) != -1); TEST_VOID(nth_request_destroy(req)); return s->s_status;}static char passwd_name[] = "tmp_sippasswd.XXXXXX";static void remove_tmp(void){ if (passwd_name[0]) unlink(passwd_name);}static char const passwd[] = "alice:secret:\n" "bob:secret:\n" "charlie:secret:\n";static int init_server(tester_t *t){ BEGIN(); site_t *m = t->t_master, *sub2; auth_mod_t *am; int temp; TEST_1(t->t_master = m = site_create(t, NULL, su_sprintf(t->t_home, "HTTP://127.0.0.1:%u", htons(t->t_addr->su_port)), HTTP_200_OK, HTTPTAG_CONTENT_TYPE_STR("text/html"), HTTPTAG_PAYLOAD_STR("<html><body>Hello</body></html>\n"), TPTAG_CERTIFICATE(t->t_pem), TAG_END())); TEST_1(site_create(t, m, "/sub/sub", HTTP_200_OK, HTTPTAG_CONTENT_TYPE_STR("text/html"), HTTPTAG_PAYLOAD_STR ("<html><body>sub/sub</body></html>\n"), TAG_END())); TEST_1(site_create(t, m, "/sub/",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -