📄 torture_su_root.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 * *//**@internal * @file torture_su_root.c * * Test su_root_register functionality. * * @author Pekka Pessi <Pekka.Pessi@nokia.com> * * Copyright (c) 2002 Nokia Research Center. All rights reserved. * * @date Created: Wed Jun 12 15:18:11 2002 ppessi */#include "config.h"char const *name = "torture_su_root";#include <stdio.h>#include <string.h>#include <stdlib.h>#include <assert.h>#define TSTFLAGS rt->rt_flags#include <sofia-sip/tstdef.h>typedef struct root_test_s root_test_t;typedef struct test_ep_s test_ep_t;#define SU_ROOT_MAGIC_T root_test_t#define SU_WAKEUP_ARG_T test_ep_t#define SU_MSG_ARG_T root_test_t *#include <sofia-sip/su_wait.h>#include <sofia-sip/su_alloc.h>#include <sofia-sip/su_log.h>#if SU_HAVE_PTHREADS#include <pthread.h>#endifstruct test_ep_s { test_ep_t *next, **prev, **list; int i; int s; su_wait_t wait[1]; int registered; socklen_t addrlen; su_sockaddr_t addr[1];};typedef struct test_ep_s test_ep_at[1];struct root_test_s { su_home_t rt_home[1]; int rt_flags; su_root_t *rt_root; short rt_family; int rt_status; int rt_received; int rt_wakeup; su_clone_r rt_clone; unsigned rt_msg_received; unsigned rt_msg_destroyed; unsigned rt_fail_init:1; unsigned rt_fail_deinit:1; unsigned rt_success_init:1; unsigned rt_success_deinit:1; unsigned rt_sent_reporter:1; unsigned rt_recv_reporter:1; unsigned rt_reported_reporter:1; unsigned rt_executed:1; unsigned :0; test_ep_at rt_ep[5]; int rt_sockets, rt_woken;#if SU_HAVE_PTHREADS struct { /* Used to test su_root_obtain()/su_root_release() */ pthread_mutex_t mutex[1]; pthread_cond_t cond[1]; pthread_t slave; int done; pthread_mutex_t deinit[1]; } rt_sr;#endif};int test_api(root_test_t *rt){ BEGIN(); TEST_1(rt->rt_root = su_root_create(NULL)); TEST_VOID(su_root_destroy(NULL)); TEST_P(su_root_name(NULL), NULL); TEST_1(su_root_name(rt->rt_root) != NULL); TEST(su_root_set_magic(NULL, rt), -1); TEST_P(su_root_magic(rt->rt_root), NULL); TEST(su_root_set_magic(rt->rt_root, rt), 0); TEST_P(su_root_magic(rt->rt_root), rt); TEST_P(su_root_magic(NULL), NULL); TEST(su_root_register(NULL, NULL, NULL, NULL, 0), -1); TEST(su_root_unregister(NULL, NULL, NULL, NULL), -1); TEST(su_root_deregister(NULL, 0), -1); TEST(su_root_deregister(rt->rt_root, 0), -1); TEST(su_root_deregister(rt->rt_root, -1), -1); TEST(su_root_eventmask(NULL, 0, -1, -1), -1); TEST((long int)su_root_step(NULL, 0), -1L); TEST((long int)su_root_sleep(NULL, 0), -1L); TEST(su_root_multishot(NULL, 0), -1); TEST_VOID((void)su_root_run(NULL)); TEST_VOID((void)su_root_break(NULL)); TEST_M(su_root_task(NULL), su_task_null, sizeof su_task_null); TEST_M(su_root_parent(NULL), su_task_null, sizeof su_task_null); TEST(su_root_add_prepoll(NULL, NULL, NULL), -1); TEST(su_root_remove_prepoll(NULL), -1); TEST_P(su_root_gsource(NULL), NULL); TEST_VOID((void)su_root_gsource(rt->rt_root)); TEST(su_root_yield(NULL), -1); TEST(su_root_release(NULL), -1); TEST(su_root_obtain(NULL), -1); TEST(su_root_has_thread(NULL), -1); TEST(su_root_has_thread(rt->rt_root), 2); TEST(su_root_release(rt->rt_root), 0); TEST(su_root_has_thread(rt->rt_root), 0); TEST(su_root_obtain(rt->rt_root), 0); TEST(su_root_has_thread(rt->rt_root), 2); TEST_VOID((void)su_root_destroy(rt->rt_root)); rt->rt_root = NULL; END();}#if SU_HAVE_PTHREADS#include <pthread.h>void *suspend_resume_test_thread(void *_rt) { root_test_t *rt = _rt; su_init(); pthread_mutex_lock(rt->rt_sr.mutex); rt->rt_root = su_root_create(rt); rt->rt_sr.done = 1; pthread_cond_signal(rt->rt_sr.cond); pthread_mutex_unlock(rt->rt_sr.mutex); su_root_release(rt->rt_root); pthread_mutex_lock(rt->rt_sr.deinit); su_root_obtain(rt->rt_root); su_root_destroy(rt->rt_root); rt->rt_root = NULL; pthread_mutex_unlock(rt->rt_sr.deinit); su_deinit(); return NULL;}#endif/** Test root initialization */int init_test(root_test_t *rt, char const *preference, su_port_create_f *create, su_clone_start_f *start){ su_sockaddr_t su[1] = {{ 0 }}; int i; BEGIN(); su_port_prefer(create, start);#if SU_HAVE_PTHREADS pthread_mutex_init(rt->rt_sr.mutex, NULL); pthread_cond_init(rt->rt_sr.cond, NULL); pthread_mutex_init(rt->rt_sr.deinit, NULL); pthread_mutex_lock(rt->rt_sr.deinit); pthread_create(&rt->rt_sr.slave, NULL, suspend_resume_test_thread, rt); pthread_mutex_lock(rt->rt_sr.mutex); while (rt->rt_sr.done == 0) pthread_cond_wait(rt->rt_sr.cond, rt->rt_sr.mutex); pthread_mutex_unlock(rt->rt_sr.mutex); TEST_1(rt->rt_root); TEST(su_root_obtain(rt->rt_root), 0); TEST(su_root_has_thread(rt->rt_root), 2);#else TEST_1(rt->rt_root = su_root_create(rt));#endif printf("%s: testing %s (%s) implementation\n", name, preference, su_root_name(rt->rt_root)); su->su_family = rt->rt_family; for (i = 0; i < 5; i++) { test_ep_t *ep = rt->rt_ep[i]; ep->i = i; ep->addrlen = su_sockaddr_size(su); TEST_1((ep->s = su_socket(su->su_family, SOCK_DGRAM, 0)) != -1); TEST_1(bind(ep->s, &su->su_sa, ep->addrlen) != -1); TEST_1(su_wait_create(ep->wait, ep->s, SU_WAIT_IN|SU_WAIT_ERR) != -1); TEST_1(getsockname(ep->s, &ep->addr->su_sa, &ep->addrlen) != -1); if (SU_HAS_INADDR_ANY(ep->addr)) { su_inet_pton(su->su_family, su->su_family == AF_INET ? "127.0.0.1" : "::1", SU_ADDR(ep->addr)); } } END();}static int deinit_test(root_test_t *rt){ BEGIN();#if SU_HAVE_PTHREADS TEST(su_root_has_thread(rt->rt_root), 2); TEST(su_root_release(rt->rt_root), 0); TEST(su_root_has_thread(rt->rt_root), 0); pthread_mutex_unlock(rt->rt_sr.deinit); pthread_join(rt->rt_sr.slave, NULL); pthread_mutex_destroy(rt->rt_sr.mutex); pthread_cond_destroy(rt->rt_sr.cond); pthread_mutex_destroy(rt->rt_sr.deinit);#else TEST_VOID(su_root_destroy(rt->rt_root)); rt->rt_root = NULL;#endif END();}int wakeup(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ char buffer[64]; int n, error; su_wait_events(w, ep->s); n = recv(ep->s, buffer, sizeof(buffer), 0); error = su_errno(); if (n < 0) fprintf(stderr, "%s: %s\n", "recv", su_strerror(error)); TEST_1(n > 0); rt->rt_received = ep->i; return 0;}static int wakeup0(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ rt->rt_wakeup = 0; return wakeup(rt, w, ep);}static int wakeup1(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ rt->rt_wakeup = 1; return wakeup(rt, w, ep);}static int wakeup2(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ rt->rt_wakeup = 2; return wakeup(rt, w, ep);}static int wakeup3(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ rt->rt_wakeup = 3; return wakeup(rt, w, ep);}static int wakeup4(root_test_t *rt, su_wait_t *w, test_ep_t *ep){ rt->rt_wakeup = 4; return wakeup(rt, w, ep);}staticsu_wakeup_f wakeups[5] = { wakeup0, wakeup1, wakeup2, wakeup3, wakeup4 };staticvoid test_run(root_test_t *rt){ rt->rt_received = -1; while (rt->rt_received == -1) { su_root_step(rt->rt_root, 200); }}static int register_test(root_test_t *rt){ int i; int s; char *msg = "foo"; BEGIN(); TEST_1((s = su_socket(rt->rt_family, SOCK_DGRAM, 0)) != -1); for (i = 0; i < 5; i++) { rt->rt_ep[i]->registered = su_root_register(rt->rt_root, rt->rt_ep[i]->wait, wakeups[i], rt->rt_ep[i], 0); TEST(rt->rt_ep[i]->registered, i + 1 + SU_HAVE_PTHREADS); } for (i = 0; i < 5; i++) { test_ep_t *ep = rt->rt_ep[i]; TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), sizeof(msg)); test_run(rt); TEST(rt->rt_received, i); TEST(rt->rt_wakeup, i); } for (i = 0; i < 5; i++) { TEST(su_root_unregister(rt->rt_root, rt->rt_ep[i]->wait, wakeups[i], rt->rt_ep[i]), rt->rt_ep[i]->registered); } for (i = 0; i < 5; i++) { rt->rt_ep[i]->registered = su_root_register(rt->rt_root, rt->rt_ep[i]->wait, wakeups[i], rt->rt_ep[i], 1); TEST_1(rt->rt_ep[i]->registered > 0); } for (i = 0; i < 5; i++) { test_ep_t *ep = rt->rt_ep[i]; TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), sizeof(msg)); test_run(rt); TEST(rt->rt_received, i); TEST(rt->rt_wakeup, i); } for (i = 0; i < 5; i++) { TEST(su_root_deregister(rt->rt_root, rt->rt_ep[i]->registered), rt->rt_ep[i]->registered); } for (i = 0; i < 5; i++) { test_ep_t *ep = rt->rt_ep[i]; TEST_1(su_wait_create(ep->wait, ep->s, SU_WAIT_IN|SU_WAIT_ERR) != -1); ep->registered = su_root_register(rt->rt_root, ep->wait, wakeups[i], ep, 1); TEST_1(ep->registered > 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -