tester: add before_each and after_each functions to allow init/cleanup of tests individually

parent bad7bc03
......@@ -197,10 +197,5 @@ test_t authentication_helper_tests[] = {
};
test_suite_t authentication_helper_test_suite = {
"Authentication helper",
NULL,
NULL,
sizeof(authentication_helper_tests) / sizeof(authentication_helper_tests[0]),
authentication_helper_tests
};
"Authentication helper", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(authentication_helper_tests) / sizeof(authentication_helper_tests[0]), authentication_helper_tests};
......@@ -138,10 +138,5 @@ static test_t tests[] = {
{ "Absolute uri", test_absolute_uri }
};
test_suite_t generic_uri_test_suite = {
"Generic uri",
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
test_suite_t generic_uri_test_suite = {"Generic uri", NULL, NULL, belle_sip_tester_before_each,
belle_sip_tester_after_each, sizeof(tests) / sizeof(tests[0]), tests};
......@@ -76,7 +76,7 @@ static void process_auth_requested(void *data, belle_sip_auth_event_t *event){
static belle_sip_stack_t *stack=NULL;
static belle_http_provider_t *prov=NULL;
static int http_init(void){
static int http_before_all(void) {
stack=belle_sip_stack_new(NULL);
prov=belle_sip_stack_create_http_provider(stack,"0.0.0.0");
if (belle_sip_tester_get_root_ca_path() != NULL) {
......@@ -87,7 +87,7 @@ static int http_init(void){
return 0;
}
static int http_cleanup(void){
static int http_after_all(void) {
belle_sip_object_unref(prov);
belle_sip_object_unref(stack);
return 0;
......@@ -336,11 +336,5 @@ test_t http_tests[] = {
{ "http GET with long user body", http_get_long_user_body}
};
test_suite_t http_test_suite = {
"http",
http_init,
http_cleanup,
sizeof(http_tests) / sizeof(http_tests[0]),
http_tests
};
test_suite_t http_test_suite = {"http", http_before_all, http_after_all, belle_sip_tester_before_each,
belle_sip_tester_after_each, sizeof(http_tests) / sizeof(http_tests[0]), http_tests};
......@@ -703,11 +703,5 @@ test_t sdp_tests[] = {
{ "Marshal buffer overflow", test_overflow }
};
test_suite_t sdp_test_suite = {
"SDP",
NULL,
NULL,
sizeof(sdp_tests) / sizeof(sdp_tests[0]),
sdp_tests
};
test_suite_t sdp_test_suite = {"SDP", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(sdp_tests) / sizeof(sdp_tests[0]), sdp_tests};
......@@ -192,11 +192,5 @@ test_t core_tests[] = {
{ "Dictionary", test_dictionary }
};
test_suite_t core_test_suite = {
"Core",
NULL,
NULL,
sizeof(core_tests) / sizeof(core_tests[0]),
core_tests
};
test_suite_t core_test_suite = {"Core", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(core_tests) / sizeof(core_tests[0]), core_tests};
......@@ -365,11 +365,6 @@ test_t dialog_tests[] = {
{ "Simple call with delay", simple_call_with_delay }
};
test_suite_t dialog_test_suite = {
"Dialog",
register_init,
register_uninit,
sizeof(dialog_tests) / sizeof(dialog_tests[0]),
dialog_tests
};
test_suite_t dialog_test_suite = {"Dialog", register_before_all, register_after_all, belle_sip_tester_before_each,
belle_sip_tester_after_each, sizeof(dialog_tests) / sizeof(dialog_tests[0]),
dialog_tests};
......@@ -1041,11 +1041,5 @@ test_t headers_tests[] = {
};
test_suite_t headers_test_suite = {
"Headers",
NULL,
NULL,
sizeof(headers_tests) / sizeof(headers_tests[0]),
headers_tests
};
test_suite_t headers_test_suite = {"Headers", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(headers_tests) / sizeof(headers_tests[0]), headers_tests};
......@@ -1034,11 +1034,5 @@ test_t message_tests[] = {
{ "Get body size",testGetBody}
};
test_suite_t message_test_suite = {
"Message",
NULL,
NULL,
sizeof(message_tests) / sizeof(message_tests[0]),
message_tests
};
test_suite_t message_test_suite = {"Message", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(message_tests) / sizeof(message_tests[0]), message_tests};
......@@ -792,11 +792,5 @@ test_t refresher_tests[] = {
{ "REGISTER TCP from random port using AF_INET6", register_tcp_test_ipv6_random_port },
};
test_suite_t refresher_test_suite = {
"Refresher",
NULL,
NULL,
sizeof(refresher_tests) / sizeof(refresher_tests[0]),
refresher_tests
};
test_suite_t refresher_test_suite = {"Refresher", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(refresher_tests) / sizeof(refresher_tests[0]), refresher_tests};
......@@ -186,7 +186,7 @@ static void process_auth_requested(void *user_ctx, belle_sip_auth_event_t *event
}
}
int register_init(void) {
int register_before_all(void) {
belle_sip_listening_point_t *lp;
stack=belle_sip_stack_new(NULL);
lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
......@@ -216,7 +216,7 @@ int register_init(void) {
return 0;
}
int register_uninit(void) {
int register_after_all(void) {
belle_sip_object_unref(prov);
belle_sip_object_unref(stack);
belle_sip_object_unref(listener);
......@@ -728,12 +728,6 @@ test_t register_tests[] = {
{ "Nonce reutilization", reuse_nonce }
};
test_suite_t register_test_suite = {
"REGISTER",
register_init,
register_uninit,
sizeof(register_tests) / sizeof(register_tests[0]),
register_tests
};
test_suite_t register_test_suite = {"REGISTER", register_before_all, register_after_all, belle_sip_tester_before_each,
belle_sip_tester_after_each, sizeof(register_tests) / sizeof(register_tests[0]),
register_tests};
......@@ -563,11 +563,5 @@ test_t resolver_tests[] = {
{ "IPv4 and v6 DNS servers", ipv4_and_ipv6_dns_server }
};
test_suite_t resolver_test_suite = {
"Resolver",
NULL,
NULL,
sizeof(resolver_tests) / sizeof(resolver_tests[0]),
resolver_tests
};
test_suite_t resolver_test_suite = {"Resolver", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(resolver_tests) / sizeof(resolver_tests[0]), resolver_tests};
......@@ -33,6 +33,8 @@ static FILE * log_file = NULL;
static belle_sip_object_pool_t *pool;
static int leaked_objects_count;
static int _belle_sip_tester_ipv6_available(void){
struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_INET6,"2a01:e00::2",53);
if (ai){
......@@ -134,7 +136,7 @@ int main (int argc, char *argv[]) {
if (strstr(argv[0], ".libs")) {
int prefix_length = strstr(argv[0], ".libs") - argv[0] + 1;
char *prefix = belle_sip_strdup_printf("%s%.*s", argv[0][0] == '/' ? "" : "./", prefix_length, argv[0]);
belle_sip_warning("Resource prefix set to %s", prefix);
// printf("Resource prefix set to %s\n", prefix);
bc_tester_set_resource_dir_prefix(prefix);
bc_tester_set_writable_dir_prefix(prefix);
belle_sip_free(prefix);
......@@ -187,4 +189,19 @@ int main (int argc, char *argv[]) {
belle_sip_tester_uninit();
return ret;
}
void belle_sip_tester_before_each() {
belle_sip_object_enable_leak_detector(TRUE);
leaked_objects_count = belle_sip_object_get_object_count();
}
void belle_sip_tester_after_each() {
int leaked_objects = belle_sip_object_get_object_count() - leaked_objects_count;
BC_ASSERT_EQUAL(leaked_objects, 0, int, "%d");
if (leaked_objects > 0) {
belle_sip_object_dump_active_objects();
belle_sip_error("%d objects were leaked, aborting!\n", leaked_objects);
}
}
#endif
......@@ -50,6 +50,9 @@ extern const char* belle_sip_tester_client_cert_fingerprint;
extern const char* belle_sip_tester_private_key;
extern const char* belle_sip_tester_private_key_passwd;
void belle_sip_tester_before_each();
void belle_sip_tester_after_each();
#ifdef __cplusplus
};
#endif
......
......@@ -492,11 +492,5 @@ static test_t uri_tests[] = {
{ "Empty password", test_empty_password },
};
test_suite_t sip_uri_test_suite = {
"SIP URI",
NULL,
NULL,
sizeof(uri_tests) / sizeof(uri_tests[0]),
uri_tests
};
test_suite_t sip_uri_test_suite = {"SIP URI", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
sizeof(uri_tests) / sizeof(uri_tests[0]), uri_tests};
......@@ -57,11 +57,5 @@ test_t cast_tests[] = {
{ "Casting requests and responses", cast_test }
};
test_suite_t cast_test_suite = {
"Object inheritence",
NULL,
NULL,
sizeof(cast_tests) / sizeof(cast_tests[0]),
cast_tests
};
test_suite_t cast_test_suite = {"Object inheritance", NULL, NULL, NULL, NULL,
sizeof(cast_tests) / sizeof(cast_tests[0]), cast_tests};
......@@ -74,7 +74,7 @@ void bc_tester_printf(int level, const char *fmt, ...) {
int bc_tester_run_suite(test_suite_t *suite) {
int i;
CU_pSuite pSuite = CU_add_suite(suite->name, suite->init_func, suite->cleanup_func);
CU_pSuite pSuite = CU_add_suite(suite->name, suite->before_all, suite->after_all);
for (i = 0; i < suite->nb_tests; i++) {
if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
......@@ -94,7 +94,7 @@ int bc_tester_suite_index(const char *suite_name) {
int i;
for (i = 0; i < nb_test_suites; i++) {
if ((strcmp(suite_name, test_suite[i]->name) == 0) && (strlen(suite_name) == strlen(test_suite[i]->name))) {
if (strcmp(suite_name, test_suite[i]->name) == 0) {
return i;
}
}
......@@ -159,17 +159,24 @@ static void suite_complete_message_handler(const CU_pSuite pSuite, const CU_pFai
}
static void test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) {
int suite_index = bc_tester_suite_index(pSuite->pName);
bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] Test [%s] started", pSuite->pName,pTest->pName);
if (test_suite[suite_index]->before_each) {
test_suite[suite_index]->before_each();
}
}
/*derivated from cunit*/
static void test_complete_message_handler(const CU_pTest pTest,
const CU_pSuite pSuite,
const CU_pFailureRecord pFailureList) {
static void test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite,
const CU_pFailureRecord pFailureList) {
int suite_index = bc_tester_suite_index(pSuite->pName);
int i;
char result[2048]={0};
char buffer[2048]={0};
CU_pFailureRecord pFailure = pFailureList;
if (test_suite[suite_index]->after_each) {
test_suite[suite_index]->after_each();
}
snprintf(result, sizeof(result), "Suite [%s] Test [%s]", pSuite->pName, pTest->pName);
if (pFailure) {
strncat(result, " failed:", strlen(" failed:"));
......
......@@ -35,9 +35,11 @@ extern int bc_printf_verbosity_info;
extern int bc_printf_verbosity_error;
typedef void (*test_function_t)(void);
typedef int (*init_function_t)(void);
typedef int (*cleanup_function_t)(void);
typedef int (*test_suite_function_t)(const char *name);
/** Function used in all suites - it is invoked before all and each tests and also after each and all tests
* @return 0 means success, otherwise it's an error
**/
typedef int (*pre_post_function_t)(void);
// typedef int (*test_suite_function_t)(const char *name);
typedef struct {
const char *name;
......@@ -45,11 +47,14 @@ typedef struct {
} test_t;
typedef struct {
const char *name;
init_function_t init_func;
cleanup_function_t cleanup_func;
int nb_tests;
test_t *tests;
const char *name; /*suite name*/
pre_post_function_t
before_all; /*function invoked before running the suite. If not returning 0, suite is not launched. */
pre_post_function_t after_all; /*function invoked at the end of the suite, even if some tests failed. */
test_function_t before_each; /*function invoked before each test within this suite. */
test_function_t after_each; /*function invoked after each test within this suite, even if it failed. */
int nb_tests; /* number of tests */
test_t *tests; /* tests within this suite */
} test_suite_t;
#ifdef __cplusplus
......
......@@ -21,8 +21,8 @@ extern belle_sip_stack_t * stack;
extern belle_sip_provider_t *prov;
extern const char *test_domain;
int call_endeed;
extern int register_init(void);
extern int register_uninit(void);
extern int register_before_all(void);
extern int register_after_all(void);
extern belle_sip_request_t* register_user(belle_sip_stack_t * stack
,belle_sip_provider_t *prov
,const char *transport
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment