From 4a8d9250cf330b37d0bca29067b0ff993ccf3ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Thu, 14 May 2020 09:39:57 +0200 Subject: [PATCH] tests: fix isc/socket_test.c teardown In case of a test failure we weren't tearing down sockets and tasks properly, causing the test to hang instead of failing nicely. --- lib/isc/tests/socket_test.c | 134 ++++++++++++++---------------------- 1 file changed, 52 insertions(+), 82 deletions(-) diff --git a/lib/isc/tests/socket_test.c b/lib/isc/tests/socket_test.c index 24af5b8e55..e2d4b1e194 100644 --- a/lib/isc/tests/socket_test.c +++ b/lib/isc/tests/socket_test.c @@ -36,6 +36,8 @@ static bool recv_dscp; static unsigned int recv_dscp_value; static bool recv_trunc; +isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL; +isc_task_t *test_task = NULL; /* * Helper functions @@ -57,6 +59,19 @@ static int _teardown(void **state) { UNUSED(state); + if (s1 != NULL) { + isc_socket_detach(&s1); + } + if (s2 != NULL) { + isc_socket_detach(&s2); + } + if (s3 != NULL) { + isc_socket_detach(&s3); + } + if (test_task != NULL) { + isc_task_detach(&test_task); + } + isc_test_end(); return (0); @@ -162,8 +177,6 @@ udp_sendto_test(void **state) { isc_result_t result; isc_sockaddr_t addr1, addr2; struct in_addr in; - isc_socket_t *s1 = NULL, *s2 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion; isc_region_t r; @@ -190,7 +203,7 @@ udp_sendto_test(void **state) { assert_int_equal(result, ISC_R_SUCCESS); assert_true(isc_sockaddr_getport(&addr2) != 0); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); snprintf(sendbuf, sizeof(sendbuf), "Hello"); @@ -198,7 +211,7 @@ udp_sendto_test(void **state) { r.length = strlen(sendbuf) + 1; completion_init(&completion); - result = isc_socket_sendto(s1, &r, task, event_done, &completion, + result = isc_socket_sendto(s1, &r, test_task, event_done, &completion, &addr2, NULL); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); @@ -208,17 +221,12 @@ udp_sendto_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); assert_int_equal(completion.result, ISC_R_SUCCESS); assert_string_equal(recvbuf, "Hello"); - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); } /* Test UDP sendto/recv with duplicated socket */ @@ -227,8 +235,6 @@ udp_dup_test(void **state) { isc_result_t result; isc_sockaddr_t addr1, addr2; struct in_addr in; - isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion; isc_region_t r; @@ -258,7 +264,7 @@ udp_dup_test(void **state) { result = isc_socket_dup(s2, &s3); assert_int_equal(result, ISC_R_SUCCESS); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); snprintf(sendbuf, sizeof(sendbuf), "Hello"); @@ -266,7 +272,7 @@ udp_dup_test(void **state) { r.length = strlen(sendbuf) + 1; completion_init(&completion); - result = isc_socket_sendto(s1, &r, task, event_done, &completion, + result = isc_socket_sendto(s1, &r, test_task, event_done, &completion, &addr2, NULL); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); @@ -278,7 +284,7 @@ udp_dup_test(void **state) { r.length = strlen(sendbuf) + 1; completion_init(&completion); - result = isc_socket_sendto(s1, &r, task, event_done, &completion, + result = isc_socket_sendto(s1, &r, test_task, event_done, &completion, &addr2, NULL); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); @@ -288,7 +294,7 @@ udp_dup_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -298,18 +304,12 @@ udp_dup_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s3, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s3, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); assert_int_equal(completion.result, ISC_R_SUCCESS); assert_string_equal(recvbuf, "World"); - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); - isc_socket_detach(&s3); } /* Test UDP sendto/recv (IPv4) */ @@ -318,8 +318,6 @@ udp_dscp_v4_test(void **state) { isc_result_t result; isc_sockaddr_t addr1, addr2; struct in_addr in; - isc_socket_t *s1 = NULL, *s2 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion; isc_region_t r; @@ -347,7 +345,7 @@ udp_dscp_v4_test(void **state) { assert_int_equal(result, ISC_R_SUCCESS); assert_true(isc_sockaddr_getport(&addr2) != 0); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); snprintf(sendbuf, sizeof(sendbuf), "Hello"); @@ -372,7 +370,8 @@ udp_dscp_v4_test(void **state) { recv_dscp = false; recv_dscp_value = 0; - result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0); + result = isc_socket_sendto2(s1, &r, test_task, &addr2, NULL, + socketevent, 0); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -381,7 +380,7 @@ udp_dscp_v4_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -394,10 +393,6 @@ udp_dscp_v4_test(void **state) { } else { assert_false(recv_dscp); } - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); } /* Test UDP sendto/recv (IPv6) */ @@ -406,8 +401,6 @@ udp_dscp_v6_test(void **state) { isc_result_t result; isc_sockaddr_t addr1, addr2; struct in6_addr in6; - isc_socket_t *s1 = NULL, *s2 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion; isc_region_t r; @@ -439,7 +432,7 @@ udp_dscp_v6_test(void **state) { assert_int_equal(result, ISC_R_SUCCESS); assert_true(isc_sockaddr_getport(&addr2) != 0); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); snprintf(sendbuf, sizeof(sendbuf), "Hello"); @@ -462,7 +455,8 @@ udp_dscp_v6_test(void **state) { recv_dscp = false; recv_dscp_value = 0; - result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0); + result = isc_socket_sendto2(s1, &r, test_task, &addr2, NULL, + socketevent, 0); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -471,7 +465,7 @@ udp_dscp_v6_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -483,11 +477,6 @@ udp_dscp_v6_test(void **state) { } else { assert_false(recv_dscp); } - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); } /* Test TCP sendto/recv (IPv4) */ @@ -496,8 +485,6 @@ tcp_dscp_v4_test(void **state) { isc_result_t result; isc_sockaddr_t addr1; struct in_addr in; - isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion, completion2; isc_region_t r; @@ -522,15 +509,16 @@ tcp_dscp_v4_test(void **state) { result = isc_socket_create(socketmgr, PF_INET, isc_sockettype_tcp, &s2); assert_int_equal(result, ISC_R_SUCCESS); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); completion_init(&completion2); - result = isc_socket_accept(s1, task, accept_done, &completion2); + result = isc_socket_accept(s1, test_task, accept_done, &completion2); assert_int_equal(result, ISC_R_SUCCESS); completion_init(&completion); - result = isc_socket_connect(s2, &addr1, task, event_done, &completion); + result = isc_socket_connect(s2, &addr1, test_task, event_done, + &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor2(&completion, &completion2); assert_true(atomic_load(&completion.done)); @@ -549,8 +537,8 @@ tcp_dscp_v4_test(void **state) { recv_dscp_value = 0; completion_init(&completion); - result = isc_socket_sendto(s2, &r, task, event_done, &completion, NULL, - NULL); + result = isc_socket_sendto(s2, &r, test_task, event_done, &completion, + NULL, NULL); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -559,7 +547,7 @@ tcp_dscp_v4_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s3, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s3, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -573,12 +561,6 @@ tcp_dscp_v4_test(void **state) { } else { assert_false(recv_dscp); } - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); - isc_socket_detach(&s3); } /* Test TCP sendto/recv (IPv6) */ @@ -587,8 +569,6 @@ tcp_dscp_v6_test(void **state) { isc_result_t result; isc_sockaddr_t addr1; struct in6_addr in6; - isc_socket_t *s1 = NULL, *s2 = NULL, *s3 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ], recvbuf[BUFSIZ]; completion_t completion, completion2; isc_region_t r; @@ -617,15 +597,16 @@ tcp_dscp_v6_test(void **state) { &s2); assert_int_equal(result, ISC_R_SUCCESS); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); completion_init(&completion2); - result = isc_socket_accept(s1, task, accept_done, &completion2); + result = isc_socket_accept(s1, test_task, accept_done, &completion2); assert_int_equal(result, ISC_R_SUCCESS); completion_init(&completion); - result = isc_socket_connect(s2, &addr1, task, event_done, &completion); + result = isc_socket_connect(s2, &addr1, test_task, event_done, + &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor2(&completion, &completion2); assert_true(atomic_load(&completion.done)); @@ -644,8 +625,8 @@ tcp_dscp_v6_test(void **state) { recv_dscp_value = 0; completion_init(&completion); - result = isc_socket_sendto(s2, &r, task, event_done, &completion, NULL, - NULL); + result = isc_socket_sendto(s2, &r, test_task, event_done, &completion, + NULL, NULL); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -654,7 +635,7 @@ tcp_dscp_v6_test(void **state) { r.base = (void *)recvbuf; r.length = BUFSIZ; completion_init(&completion); - result = isc_socket_recv(s3, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s3, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -672,12 +653,6 @@ tcp_dscp_v6_test(void **state) { } else { assert_false(recv_dscp); } - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); - isc_socket_detach(&s3); } /* probe dscp capabilities */ @@ -719,8 +694,6 @@ udp_trunc_test(void **state) { isc_result_t result; isc_sockaddr_t addr1, addr2; struct in_addr in; - isc_socket_t *s1 = NULL, *s2 = NULL; - isc_task_t *task = NULL; char sendbuf[BUFSIZ * 2], recvbuf[BUFSIZ]; completion_t completion; isc_region_t r; @@ -747,7 +720,7 @@ udp_trunc_test(void **state) { assert_int_equal(result, ISC_R_SUCCESS); assert_true(isc_sockaddr_getport(&addr2) != 0); - result = isc_task_create(taskmgr, 0, &task); + result = isc_task_create(taskmgr, 0, &test_task); assert_int_equal(result, ISC_R_SUCCESS); /* @@ -764,7 +737,8 @@ udp_trunc_test(void **state) { test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); - result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0); + result = isc_socket_sendto2(s1, &r, test_task, &addr2, NULL, + socketevent, 0); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -774,7 +748,7 @@ udp_trunc_test(void **state) { r.length = BUFSIZ; completion_init(&completion); recv_trunc = false; - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -796,7 +770,8 @@ udp_trunc_test(void **state) { test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); - result = isc_socket_sendto2(s1, &r, task, &addr2, NULL, socketevent, 0); + result = isc_socket_sendto2(s1, &r, test_task, &addr2, NULL, + socketevent, 0); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); @@ -806,18 +781,13 @@ udp_trunc_test(void **state) { r.length = BUFSIZ; completion_init(&completion); recv_trunc = false; - result = isc_socket_recv(s2, &r, 1, task, event_done, &completion); + result = isc_socket_recv(s2, &r, 1, test_task, event_done, &completion); assert_int_equal(result, ISC_R_SUCCESS); waitfor(&completion); assert_true(atomic_load(&completion.done)); assert_int_equal(completion.result, ISC_R_SUCCESS); assert_string_equal(recvbuf, "Hello"); assert_true(recv_trunc); - - isc_task_detach(&task); - - isc_socket_detach(&s1); - isc_socket_detach(&s2); } /*