⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nth_test.c

📁 this is simple sip stack.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 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 nth_test.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>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[] = "nth_test";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;  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;  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 site_create(tester_t *t, int status, char const *phrase,		       tag_type_t tag, tag_value_t value, ...){  site_t *s;  ta_list ta;  BEGIN();  s = su_zalloc(t->t_home, sizeof *s); TEST_1(s);  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));   ta_end(ta);    TEST_1(s->s_tags);  t->t_sites = s;  END();}static int init_test(tester_t *t){  int s;  char const *agent_pem;  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 = socket(t->t_addr->su_family = AF_INET, SOCK_STREAM, 0); TEST_1(s != -1);  TEST_1(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);  agent_pem = su_sprintf(t->t_home, "%s/agent.pem", t->t_srcdir);  TEST_1(site_create(t, 		     HTTP_200_OK, 		     HTTPTAG_CONTENT_TYPE_STR("text/html"),		     HTTPTAG_PAYLOAD_STR("<html><body>Hello</body></html>\n"),		     TPTAG_CERTIFICATE(agent_pem),		     TAG_END()) == 0);  TEST_1(t->t_master = t->t_sites);  t->t_master->s_url = (url_string_t *)    su_sprintf(t->t_home, "HTTP://127.0.0.1:%u", htons(t->t_addr->su_port));  TEST_1(t->t_master->s_url);  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);  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(nth_client_url(NULL), NULL);  TEST(nth_client_request(NULL), NULL);  TEST(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(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(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 nth_test_site(site_t *t, 			  nth_site_t *server,			  nth_request_t *req, 			  http_t const *http,			  char const *path);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 = t->t_master;    BEGIN();  v = nth_site_server_version();   TEST_1(v); TEST_1(strlen(v)); TEST_S(v, "nth/" NTH_SERVER_VERSION);  s = t->t_sites;  /* Fails because no parent site, no root */  TEST_1(!nth_site_create(NULL, nth_test_site, s, s->s_url, 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(nth_site_magic(NULL), NULL);  TEST_VOID(nth_site_bind(NULL, nth_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(nth_request_message(NULL), NULL);  TEST_1(nth_request_treply(NULL, HTTP_200_OK, TAG_END()) == -1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -