belle_sip_tester.c 5.58 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
Simon Morlat's avatar
Simon Morlat committed
18 19 20
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Ghislain MARY's avatar
Ghislain MARY committed
21
#include "belle_sip_tester.h"
Simon Morlat's avatar
Simon Morlat committed
22

jehan's avatar
jehan committed
23 24
#include <stdio.h>
#include "CUnit/Basic.h"
25 26 27
#if HAVE_CU_CURSES
#include "CUnit/CUCurses.h"
#endif
Simon Morlat's avatar
Simon Morlat committed
28
#include <belle-sip/belle-sip.h>
jehan's avatar
jehan committed
29

Simon Morlat's avatar
Simon Morlat committed
30
extern const char *test_domain;
31
extern const char *auth_domain;
Ghislain MARY's avatar
Ghislain MARY committed
32 33 34 35 36


static test_suite_t **test_suite = NULL;
static int nb_test_suites = 0;

Simon Morlat's avatar
Simon Morlat committed
37

jehan's avatar
jehan committed
38 39 40
#if HAVE_CU_CURSES
	static unsigned char curses = 0;
#endif
41 42


Ghislain MARY's avatar
Ghislain MARY committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void add_test_suite(test_suite_t *suite) {
	if (test_suite == NULL) {
		test_suite = (test_suite_t **)malloc(10 * sizeof(test_suite_t *));
	}
	test_suite[nb_test_suites] = suite;
	nb_test_suites++;
	if ((nb_test_suites % 10) == 0) {
		test_suite = (test_suite_t **)realloc(test_suite, (nb_test_suites + 10) * sizeof(test_suite_t *));
	}
}

static int run_test_suite(test_suite_t *suite) {
	int i;

	CU_pSuite pSuite = CU_add_suite(suite->name, suite->init_func, suite->cleanup_func);

	for (i = 0; i < suite->nb_tests; i++) {
		if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
			return CU_get_error();
		}
	}

	return 0;
}

static int test_suite_index(const char *suite_name) {
	int i;
70

Ghislain MARY's avatar
Ghislain MARY committed
71 72 73 74 75
	for (i = 0; i < belle_sip_tester_nb_test_suites(); i++) {
		if ((strcmp(suite_name, test_suite[i]->name) == 0) && (strlen(suite_name) == strlen(test_suite[i]->name))) {
			return i;
		}
	}
76

Ghislain MARY's avatar
Ghislain MARY committed
77 78
	return -1;
}
79

Ghislain MARY's avatar
Ghislain MARY committed
80 81 82
int belle_sip_tester_nb_test_suites(void) {
	return nb_test_suites;
}
83

Ghislain MARY's avatar
Ghislain MARY committed
84 85 86 87 88
int belle_sip_tester_nb_tests(const char *suite_name) {
	int i = test_suite_index(suite_name);
	if (i < 0) return 0;
	return test_suite[i]->nb_tests;
}
89

Ghislain MARY's avatar
Ghislain MARY committed
90 91 92 93
const char * belle_sip_tester_test_suite_name(int suite_index) {
	if (suite_index >= belle_sip_tester_nb_test_suites()) return NULL;
	return test_suite[suite_index]->name;
}
94

Ghislain MARY's avatar
Ghislain MARY committed
95 96 97 98 99 100
const char * belle_sip_tester_test_name(const char *suite_name, int test_index) {
	int suite_index = test_suite_index(suite_name);
	if ((suite_index < 0) || (suite_index >= belle_sip_tester_nb_test_suites())) return NULL;
	if (test_index >= test_suite[suite_index]->nb_tests) return NULL;
	return test_suite[suite_index]->tests[test_index].name;
}
101

Ghislain MARY's avatar
Ghislain MARY committed
102 103 104 105 106 107 108 109 110 111 112 113
void belle_sip_tester_init(void) {
	add_test_suite(&cast_test_suite);
	add_test_suite(&uri_test_suite);
	add_test_suite(&headers_test_suite);
	add_test_suite(&sdp_test_suite);
	add_test_suite(&resolver_test_suite);
	add_test_suite(&message_test_suite);
	add_test_suite(&authentication_helper_test_suite);
	add_test_suite(&register_test_suite);
	add_test_suite(&dialog_test_suite);
	add_test_suite(&refresher_test_suite);
}
114

Ghislain MARY's avatar
Ghislain MARY committed
115 116 117 118 119 120 121 122 123
void belle_sip_tester_uninit(void) {
	if (test_suite != NULL) {
		free(test_suite);
		test_suite = NULL;
		nb_test_suites = 0;
	}
}
int belle_sip_tester_run_tests(const char *suite_name, const char *test_name) {
	int i;
Simon Morlat's avatar
Simon Morlat committed
124 125
	belle_sip_object_pool_t *pool;
	
Ghislain MARY's avatar
Ghislain MARY committed
126 127 128
	/* initialize the CUnit test registry */
	if (CUE_SUCCESS != CU_initialize_registry())
		return CU_get_error();
129

Ghislain MARY's avatar
Ghislain MARY committed
130 131 132
	for (i = 0; i < belle_sip_tester_nb_test_suites(); i++) {
		run_test_suite(test_suite[i]);
	}
Simon Morlat's avatar
Simon Morlat committed
133
	pool=belle_sip_object_pool_push();
134
	
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#if HAVE_CU_GET_SUITE
	if (suite_name){
		CU_pSuite suite;
		CU_basic_set_mode(CU_BRM_VERBOSE);
		suite=CU_get_suite(suite_name);
		if (test_name) {
			CU_pTest test=CU_get_test_by_name(test_name, suite);
			CU_basic_run_test(suite, test);
		} else
			CU_basic_run_suite(suite);
	} else
#endif
	{
#if HAVE_CU_CURSES
		if (curses) {
			/* Run tests using the CUnit curses interface */
			CU_curses_run_tests();
		}
		else
#endif
		{
			/* Run all tests using the CUnit Basic interface */
			CU_basic_set_mode(CU_BRM_VERBOSE);
			CU_basic_run_tests();
		}
	}

Simon Morlat's avatar
Simon Morlat committed
162
	belle_sip_object_unref(pool);
163
	
164 165 166 167 168
	CU_cleanup_registry();
	return CU_get_error();
}


169
#ifndef WINAPI_FAMILY_PHONE_APP
jehan's avatar
jehan committed
170
int main (int argc, char *argv[]) {
Simon Morlat's avatar
Simon Morlat committed
171
	int i;
Ghislain MARY's avatar
Ghislain MARY committed
172
	int ret;
173
	char *suite_name=NULL;
174
	char *test_name=NULL;
Simon Morlat's avatar
Simon Morlat committed
175
	const char *env_domain=getenv("TEST_DOMAIN");
jehan's avatar
jehan committed
176

177

Simon Morlat's avatar
Simon Morlat committed
178 179
	if (env_domain)
		test_domain=env_domain;
180

Simon Morlat's avatar
Simon Morlat committed
181 182
	for(i=1;i<argc;++i){
		if (strcmp(argv[i],"--help")==0){
183 184 185 186
				fprintf(stderr,"%s \t--help\n"
						"\t\t\t--verbose\n"
						"\t\t\t--domain <test sip domain>\n"
						"\t\t\t---auth-domain <test auth domain>\n"
187
#if HAVE_CU_GET_SUITE
188
						"\t\t\t--suite <suite name>\n"
189 190 191 192 193 194
						"\t\t\t--test <test name>\n"
#endif
#if HAVE_CU_CURSES
						"\t\t\t--curses\n"
#endif
						, argv[0]);
Simon Morlat's avatar
Simon Morlat committed
195
				return 0;
Simon Morlat's avatar
Simon Morlat committed
196
		}else if (strcmp(argv[i],"--verbose")==0){
Simon Morlat's avatar
Simon Morlat committed
197
			belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
Simon Morlat's avatar
Simon Morlat committed
198 199 200
		}else if (strcmp(argv[i],"--domain")==0){
			i++;
			test_domain=argv[i];
201 202 203 204
		}
		else if (strcmp(argv[i],"--auth-domain")==0){
					i++;
					auth_domain=argv[i];
jehan's avatar
jehan committed
205 206 207
		}
#if HAVE_CU_GET_SUITE
		else if (strcmp(argv[i],"--test")==0){
208 209
			i++;
			test_name=argv[i];
jehan's avatar
jehan committed
210 211 212
		}else if (strcmp(argv[i],"--suite")==0){
			i++;
			suite_name=argv[i];
Simon Morlat's avatar
Simon Morlat committed
213
		}
214 215 216 217 218 219
#endif
#if HAVE_CU_CURSES
		else if (strcmp(argv[i], "--curses") == 0) {
			i++;
			curses = 1;
		}
jehan's avatar
jehan committed
220
#endif
Simon Morlat's avatar
Simon Morlat committed
221
	}
222

Ghislain MARY's avatar
Ghislain MARY committed
223 224 225 226
	belle_sip_tester_init();
	ret = belle_sip_tester_run_tests(suite_name, test_name);
	belle_sip_tester_uninit();
	return ret;
jehan's avatar
jehan committed
227
}
228
#endif