diff --git a/test/regress.c b/test/regress.c index 087ac370..9e924f2a 100644 --- a/test/regress.c +++ b/test/regress.c @@ -1439,7 +1439,6 @@ end: event_config_free(cfg); } - struct testcase_t main_testcases[] = { /* Some converted-over tests */ { "methods", test_methods, TT_FORK, NULL, NULL }, diff --git a/test/regress.h b/test/regress.h index 8cb69a14..fa1f2660 100644 --- a/test/regress.h +++ b/test/regress.h @@ -56,6 +56,15 @@ extern int in_legacy_test_wrapper; evutil_socket_t regress_make_tmpfile(const void *data, size_t datalen); +struct basic_test_data { + struct event_base *base; + int pair[2]; + + void (*legacy_test_fn)(void); +}; +extern const struct testcase_setup_t basic_setup; + + extern const struct testcase_setup_t legacy_setup; void run_legacy_test_fn(void *ptr); @@ -63,12 +72,13 @@ void run_legacy_test_fn(void *ptr); #define TT_NEED_SOCKETPAIR TT_FIRST_USER_FLAG #define TT_NEED_BASE (TT_FIRST_USER_FLAG<<1) #define TT_NEED_DNS (TT_FIRST_USER_FLAG<<2) +#define TT_LEGACY (TT_FIRST_USER_FLAG<<3) /* All the flags that a legacy test needs. */ #define TT_ISOLATED TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE #define LEGACY(name,flags) \ - { #name, run_legacy_test_fn, flags, &legacy_setup, \ + { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \ test_## name } diff --git a/test/regress_dns.c b/test/regress_dns.c index 09d9bcd6..75cf5e4b 100644 --- a/test/regress_dns.c +++ b/test/regress_dns.c @@ -400,7 +400,7 @@ end: #define DNS_LEGACY(name, flags) \ - { #name, run_legacy_test_fn, flags, &legacy_setup, \ + { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \ dns_##name } struct testcase_t dns_testcases[] = { diff --git a/test/regress_http.c b/test/regress_http.c index 21b465fa..1531820e 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -2191,7 +2191,7 @@ http_negative_content_length_test(void) } #define HTTP_LEGACY(name) \ - { #name, run_legacy_test_fn, TT_ISOLATED, &legacy_setup, \ + { #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \ http_##name##_test } struct testcase_t http_testcases[] = { diff --git a/test/regress_main.c b/test/regress_main.c index 50a46a88..624ef71b 100644 --- a/test/regress_main.c +++ b/test/regress_main.c @@ -115,32 +115,36 @@ regress_make_tmpfile(const void *data, size_t datalen) #endif } -/* The "data" for a legacy test is just a pointer to the void fn(void) - function implementing the test case. We need to set up some globals, - though, since that's where legacy tests expect to find a socketpair - (sometimes) and a global event_base (sometimes). - */ static void * -legacy_test_setup(const struct testcase_t *testcase) +basic_test_setup(const struct testcase_t *testcase) { + struct event_base *base = NULL; + int spair[2] = { -1, -1 }; + struct basic_test_data *data = NULL; + if (testcase->flags & TT_NEED_SOCKETPAIR) { - if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) { fprintf(stderr, "%s: socketpair\n", __func__); exit(1); } - if (evutil_make_socket_nonblocking(pair[0]) == -1) { + if (evutil_make_socket_nonblocking(spair[0]) == -1) { fprintf(stderr, "fcntl(O_NONBLOCK)"); exit(1); } - if (evutil_make_socket_nonblocking(pair[1]) == -1) { + if (evutil_make_socket_nonblocking(spair[1]) == -1) { fprintf(stderr, "fcntl(O_NONBLOCK)"); exit(1); } } if (testcase->flags & TT_NEED_BASE) { - global_base = event_init(); + if (testcase->flags & TT_LEGACY) + base = event_init(); + else + base = event_base_new(); + if (!base) + exit(1); } if (testcase->flags & TT_NEED_DNS) { @@ -149,7 +153,57 @@ legacy_test_setup(const struct testcase_t *testcase) return NULL; /* fast failure *//*XXX asserts. */ } - return testcase->setup_data; + data = calloc(1, sizeof(*data)); + if (!data) + exit(1); + data->base = base; + data->pair[0] = spair[0]; + data->pair[1] = spair[1]; + return data; +} + +static int +basic_test_cleanup(const struct testcase_t *testcase, void *ptr) +{ + struct basic_test_data *data = ptr; + if (testcase->flags & TT_NEED_SOCKETPAIR) { + if (data->pair[0] != -1) + EVUTIL_CLOSESOCKET(data->pair[0]); + if (data->pair[1] != -1) + EVUTIL_CLOSESOCKET(data->pair[1]); + } + + if (testcase->flags & TT_NEED_BASE) { + event_base_free(data->base); + } + + if (testcase->flags & TT_NEED_DNS) { + evdns_shutdown(0); + } + + free(data); + + return 1; +} + +const struct testcase_setup_t basic_setup = { + basic_test_setup, basic_test_cleanup +}; + +/* The "data" for a legacy test is just a pointer to the void fn(void) + function implementing the test case. We need to set up some globals, + though, since that's where legacy tests expect to find a socketpair + (sometimes) and a global event_base (sometimes). + */ +static void * +legacy_test_setup(const struct testcase_t *testcase) +{ + struct basic_test_data *data = basic_test_setup(testcase); + global_base = data->base; + pair[0] = data->pair[0]; + pair[1] = data->pair[1]; + data->legacy_test_fn = testcase->setup_data; + return data; } /* This function is the implementation of every legacy test case. It @@ -159,12 +213,11 @@ legacy_test_setup(const struct testcase_t *testcase) void run_legacy_test_fn(void *ptr) { - void (*fn)(void); + struct basic_test_data *data = ptr; test_ok = called = 0; - fn = ptr; in_legacy_test_wrapper = 1; - fn(); /* This part actually calls the test */ + data->legacy_test_fn(); /* This part actually calls the test */ in_legacy_test_wrapper = 0; if (!test_ok) @@ -181,25 +234,10 @@ end: static int legacy_test_cleanup(const struct testcase_t *testcase, void *ptr) { - (void)ptr; - if (testcase->flags & TT_NEED_SOCKETPAIR) { - if (pair[0] != -1) - EVUTIL_CLOSESOCKET(pair[0]); - if (pair[1] != -1) - EVUTIL_CLOSESOCKET(pair[1]); - pair[0] = pair[1] = -1; - } - - if (testcase->flags & TT_NEED_BASE) { - event_base_free(global_base); - global_base = NULL; - } - - if (testcase->flags & TT_NEED_DNS) { - evdns_shutdown(0); - } - - return 1; + int r = basic_test_cleanup(testcase, ptr); + pair[0] = pair[1] = -1; + global_base = NULL; + return r; } const struct testcase_setup_t legacy_setup = { diff --git a/test/regress_rpc.c b/test/regress_rpc.c index f973c6c9..bc68b1b8 100644 --- a/test/regress_rpc.c +++ b/test/regress_rpc.c @@ -856,7 +856,8 @@ end: } #define RPC_LEGACY(name) \ - { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE, &legacy_setup, \ + { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \ + &legacy_setup, \ rpc_##name } struct testcase_t rpc_testcases[] = {