From 9708fcc1417fc26a4328cfacb39652890cab629d Mon Sep 17 00:00:00 2001 From: sairoop-elear Date: Wed, 8 Apr 2020 01:52:26 +0530 Subject: [PATCH] Add missing atomic test cases to the APIs that affects disk writes --- .../run_blackbox_tests/test_cases_invite.c | 155 ++++++++++---- .../run_blackbox_tests/test_cases_open.c | 155 +++++++++++++- .../run_blackbox_tests/test_cases_set_port.c | 194 +++++++++++++++--- 3 files changed, 436 insertions(+), 68 deletions(-) diff --git a/test/blackbox/run_blackbox_tests/test_cases_invite.c b/test/blackbox/run_blackbox_tests/test_cases_invite.c index 929b002a..f04cb099 100644 --- a/test/blackbox/run_blackbox_tests/test_cases_invite.c +++ b/test/blackbox/run_blackbox_tests/test_cases_invite.c @@ -26,16 +26,24 @@ #include "../common/containers.h" #include "../common/test_step.h" #include "../common/common_handlers.h" +#include "../../utils.h" #include #include #include #include #include #include +#include +#include /* Modify this to change the logging level of Meshlink */ #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG +#define NUT "nut" +#define PEER "peer" +#define TEST_MESHLINK_INVITE "test_invite" +#define create_path(confbase, node_name, test_case_no) assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_INVITE "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0) + static void test_case_invite_01(void **state); static bool test_invite_01(void); static void test_case_invite_02(void **state); @@ -44,6 +52,8 @@ static void test_case_invite_03(void **state); static bool test_invite_03(void); static void test_case_invite_04(void **state); static bool test_invite_04(void); +static void test_case_invite_05(void **state); +static bool test_invite_05(void); /* State structure for invite API Test Case #1 */ static black_box_state_t test_case_invite_01_state = { @@ -65,6 +75,11 @@ static black_box_state_t test_case_invite_04_state = { .test_case_name = "test_case_invite_04", }; +/* State structure for invite API Test Case #5 */ +static black_box_state_t test_case_invite_05_state = { + .test_case_name = "test_case_invite_05", +}; + /* Execute invite Test Case # 1 - valid case*/ static void test_case_invite_01(void **state) { execute_test(test_invite_01, state); @@ -78,20 +93,22 @@ static void test_case_invite_01(void **state) { Generates an invitation */ static bool test_invite_01(void) { - assert(meshlink_destroy("inviteconf")); - meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); + char nut_confbase[PATH_MAX]; + char peer_invitation[1000]; + create_path(nut_confbase, NUT, 1); // Create meshlink instance - meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1); - assert(mesh_handle); - meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); - char *invitation = meshlink_invite(mesh_handle, NULL, "new"); - assert_int_equal(invitation, NULL); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + char *invitation = meshlink_invite(mesh, NULL, "new"); + assert_non_null(invitation); free(invitation); - meshlink_close(mesh_handle); - assert(meshlink_destroy("inviteconf")); + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); return true; } @@ -127,20 +144,22 @@ static void test_case_invite_03(void **state) { Reports appropriate error by returning NULL */ static bool test_invite_03(void) { - assert(meshlink_destroy("inviteconf")); - meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); + char nut_confbase[PATH_MAX]; + char peer_invitation[1000]; + create_path(nut_confbase, NUT, 3); // Create meshlink instance - meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1); - assert(mesh_handle); - meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); - // Trying to generate INVITATION by passing NULL as mesh link handle - char *invitation = meshlink_invite(mesh_handle, NULL, NULL); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + char *invitation = meshlink_invite(mesh, NULL, NULL); assert_int_equal(invitation, NULL); - meshlink_close(mesh_handle); - assert(meshlink_destroy("inviteconf")); + free(invitation); + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); return true; } @@ -159,30 +178,92 @@ static void test_case_invite_04(void **state) { Newly added address should be there in the invitation. */ static bool test_invite_04(void) { - assert(meshlink_destroy("inviteconf")); - meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); + char nut_confbase[PATH_MAX]; + char peer_invitation[1000]; + create_path(nut_confbase, NUT, 4); // Create meshlink instance - meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "test", 1); - assert(mesh_handle); - meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); - char *hostname1 = "127.1.1.1"; - assert(meshlink_add_address(mesh_handle, hostname1)); - char *invitation = meshlink_invite(mesh_handle, NULL, "foo"); - assert_int_not_equal(strstr(invitation, hostname1), NULL); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + assert_true(meshlink_add_invitation_address(mesh, "11.11.11.11", "2020")); + char *invitation = meshlink_invite(mesh, NULL, "foo"); + assert_non_null(strstr(invitation, "11.11.11.11:2020")); + free(invitation); + + assert_true(meshlink_add_invitation_address(mesh, "fe80::1548:d713:3899:f645", "3030")); + invitation = meshlink_invite(mesh, NULL, "bar"); + assert_non_null(strstr(invitation, "11.11.11.11:2020")); + assert_non_null(strstr(invitation, "[fe80::1548:d713:3899:f645]:3030")); + free(invitation); + + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); + return true; +} + +/* Execute invite Test Case # 5 - Synchronization testing */ +static void test_case_invite_05(void **state) { + execute_test(test_invite_05, state); +} + +static bool test_invite_05(void) { + bool status; + pid_t pid; + int pid_status; + int pipefd[2]; + char nut_confbase[PATH_MAX]; + char peer_confbase[PATH_MAX]; + char peer_invitation[1000]; + create_path(nut_confbase, NUT, 5); + create_path(peer_confbase, PEER, 5); + + assert_int_not_equal(pipe(pipefd), -1); + + // Fork a new process in which NUT opens it's instance and raises SIGINT to terminate. + + pid = fork(); + assert_int_not_equal(pid, -1); + + if(!pid) { + assert(!close(pipefd[0])); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert(mesh); + + char *invitation = meshlink_invite(mesh, NULL, PEER); + write(pipefd[1], invitation, strlen(invitation) + 1); + + raise(SIGINT); + } + + // Wait for child exit and verify which signal terminated it + + assert_int_not_equal(waitpid(pid, &pid_status, 0), -1); + assert_int_equal(WIFSIGNALED(pid_status), true); + assert_int_equal(WTERMSIG(pid_status), SIGINT); + + assert_int_equal(close(pipefd[1]), 0); + assert_int_not_equal(read(pipefd[0], peer_invitation, sizeof(peer_invitation)), -1); - char *hostname2 = "127.1.2.3"; - assert(meshlink_add_address(mesh_handle, hostname2)); - invitation = meshlink_invite(mesh_handle, NULL, "bar"); + // Reopen the NUT instance in the same test suite - // Verify we have both the added addresses - assert_int_not_equal(strstr(invitation, hostname1), NULL); - assert_int_not_equal(strstr(invitation, hostname2), NULL); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_INVITE, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + assert_true(meshlink_start(mesh)); + assert_true(meshlink_join(mesh_peer, peer_invitation)); - meshlink_close(mesh_handle); - assert(meshlink_destroy("inviteconf")); + // Cleanup + meshlink_close(mesh); + meshlink_close(mesh_peer); + assert_true(meshlink_destroy(nut_confbase)); + assert_true(meshlink_destroy(peer_confbase)); return true; } @@ -195,7 +276,9 @@ int test_meshlink_invite(void) { cmocka_unit_test_prestate_setup_teardown(test_case_invite_03, NULL, NULL, (void *)&test_case_invite_03_state), cmocka_unit_test_prestate_setup_teardown(test_case_invite_04, NULL, NULL, - (void *)&test_case_invite_04_state) + (void *)&test_case_invite_04_state), + cmocka_unit_test_prestate_setup_teardown(test_case_invite_05, NULL, NULL, + (void *)&test_case_invite_05_state) }; total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]); diff --git a/test/blackbox/run_blackbox_tests/test_cases_open.c b/test/blackbox/run_blackbox_tests/test_cases_open.c index 40bd26c7..a1c1b1ea 100644 --- a/test/blackbox/run_blackbox_tests/test_cases_open.c +++ b/test/blackbox/run_blackbox_tests/test_cases_open.c @@ -26,16 +26,24 @@ #include "../common/containers.h" #include "../common/test_step.h" #include "../common/common_handlers.h" +#include "../../utils.h" #include #include #include #include #include #include +#include +#include /* Modify this to change the logging level of Meshlink */ #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG +#define NUT "nut" +#define PEER "peer" +#define TEST_MESHLINK_OPEN "test_open" +#define create_path(confbase, node_name, test_case_no) assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_OPEN "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0) + static void test_case_mesh_open_01(void **state); static bool test_steps_mesh_open_01(void); static void test_case_mesh_open_02(void **state); @@ -46,6 +54,10 @@ static void test_case_mesh_open_04(void **state); static bool test_steps_mesh_open_04(void); static void test_case_mesh_open_05(void **state); static bool test_steps_mesh_open_05(void); +static void test_case_mesh_open_06(void **state); +static bool test_steps_mesh_open_06(void); +static void test_case_mesh_open_07(void **state); +static bool test_steps_mesh_open_07(void); /* State structure for meshlink_open Test Case #1 */ static black_box_state_t test_mesh_open_01_state = { @@ -72,6 +84,16 @@ static black_box_state_t test_mesh_open_05_state = { .test_case_name = "test_case_mesh_open_05", }; +/* State structure for meshlink_open Test Case #6 */ +static black_box_state_t test_mesh_open_06_state = { + .test_case_name = "test_case_mesh_open_06", +}; + +/* State structure for meshlink_open Test Case #7 */ +static black_box_state_t test_mesh_open_07_state = { + .test_case_name = "test_case_mesh_open_07", +}; + /* Execute meshlink_open Test Case # 1*/ static void test_case_mesh_open_01(void **state) { execute_test(test_steps_mesh_open_01, state); @@ -182,6 +204,133 @@ static bool test_steps_mesh_open_05(void) { return true; } +/* Execute meshlink_open Test Case # 7 - Atomicity testing + Validate the meshlink_open behavior opened a new confbase and terminated immediately the open call. +*/ +static void test_case_mesh_open_06(void **state) { + execute_test(test_steps_mesh_open_06, state); +} + +static bool test_steps_mesh_open_06(void) { + bool status; + pid_t pid; + int pid_status; + char nut_confbase[PATH_MAX]; + create_path(nut_confbase, NUT, 6); + + // Fork a new process in which NUT opens it's instance and raises SIGINT to terminate. + + pid = fork(); + assert_int_not_equal(pid, -1); + + if(!pid) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY); + assert(mesh); + raise(SIGINT); + } + + // Wait for child exit and verify which signal terminated it + + assert_int_not_equal(waitpid(pid, &pid_status, 0), -1); + assert_int_equal(WIFSIGNALED(pid_status), true); + assert_int_equal(WTERMSIG(pid_status), SIGINT); + + // Reopen the NUT instance in the same test suite + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + // Validate parameters that were used to open meshlink instance. + + assert_int_equal(strcmp(mesh->name, NUT), 0); + meshlink_node_t *self = meshlink_get_self(mesh); + assert_int_equal(strcmp(self->name, NUT), 0); + assert_int_equal(meshlink_get_node_dev_class(mesh, self), DEV_CLASS_STATIONARY); + + // Cleanup + + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); + return true; +} + +/* Execute meshlink_open Test Case # 7 - Atomicity testing + Validate the meshlink_open behavior opened an existing confbase and terminated immediately the open call. +*/ +static void test_case_mesh_open_07(void **state) { + execute_test(test_steps_mesh_open_07, state); +} + +static bool test_steps_mesh_open_07(void) { + bool status; + pid_t pid; + int pid_status; + char nut_confbase[PATH_MAX]; + char peer_confbase[PATH_MAX]; + create_path(nut_confbase, NUT, 7); + create_path(peer_confbase, PEER, 7); + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_BACKBONE); + assert_non_null(mesh); + meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY); + assert_non_null(mesh_peer); + + // Exporting and Importing mutually + char *export_data = meshlink_export(mesh); + assert_non_null(export_data); + assert_true(meshlink_import(mesh_peer, export_data)); + free(export_data); + export_data = meshlink_export(mesh_peer); + assert_non_null(export_data); + assert_true(meshlink_import(mesh, export_data)); + free(export_data); + + meshlink_close(mesh); + meshlink_close(mesh_peer); + + + // Fork a new process in which NUT reopens it's instance and raises SIGINT to terminate. + + pid = fork(); + assert_int_not_equal(pid, -1); + + if(!pid) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_BACKBONE); + assert(mesh); + raise(SIGINT); + } + + // Wait for child exit and verify which signal terminated it + + assert_int_not_equal(waitpid(pid, &pid_status, 0), -1); + assert_int_equal(WIFSIGNALED(pid_status), true); + assert_int_equal(WTERMSIG(pid_status), SIGINT); + + // Reopen the NUT instance in the same test suite + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_OPEN, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + // Validate parameters that were used to open meshlink instance. + + assert_int_equal(strcmp(mesh->name, NUT), 0); + meshlink_node_t *self = meshlink_get_self(mesh); + assert_int_equal(strcmp(self->name, NUT), 0); + assert_int_equal(meshlink_get_node_dev_class(mesh, self), DEV_CLASS_STATIONARY); + + // Cleanup + + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); + assert_true(meshlink_destroy(peer_confbase)); + return true; +} + int test_meshlink_open(void) { const struct CMUnitTest blackbox_open_tests[] = { cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_01, NULL, NULL, @@ -193,7 +342,11 @@ int test_meshlink_open(void) { cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_04, NULL, NULL, (void *)&test_mesh_open_04_state), cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_05, NULL, NULL, - (void *)&test_mesh_open_05_state) + (void *)&test_mesh_open_05_state), + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_06, NULL, NULL, + (void *)&test_mesh_open_06_state), + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_open_07, NULL, NULL, + (void *)&test_mesh_open_07_state) }; total_tests += sizeof(blackbox_open_tests) / sizeof(blackbox_open_tests[0]); diff --git a/test/blackbox/run_blackbox_tests/test_cases_set_port.c b/test/blackbox/run_blackbox_tests/test_cases_set_port.c index e514e57f..c2153999 100644 --- a/test/blackbox/run_blackbox_tests/test_cases_set_port.c +++ b/test/blackbox/run_blackbox_tests/test_cases_set_port.c @@ -26,6 +26,7 @@ #include "../common/containers.h" #include "../common/test_step.h" #include "../common/common_handlers.h" +#include "../../utils.h" #include "test_cases_set_port.h" #include #include @@ -33,17 +34,26 @@ #include #include #include - +#include +#include +#include /* Modify this to change the logging level of Meshlink */ #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG +#define NUT "nut" +#define PEER "peer" +#define TEST_MESHLINK_SET_PORT "test_set_port" +#define create_path(confbase, node_name, test_case_no) assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_SET_PORT "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0) + static void test_case_set_port_01(void **state); static bool test_set_port_01(void); static void test_case_set_port_02(void **state); static bool test_set_port_02(void); static void test_case_set_port_03(void **state); static bool test_set_port_03(void); +static void test_case_set_port_04(void **state); +static bool test_set_port_04(void); /* State structure for set port API Test Case #1 */ static black_box_state_t test_case_set_port_01_state = { @@ -57,6 +67,75 @@ static black_box_state_t test_case_set_port_02_state = { static black_box_state_t test_case_set_port_03_state = { .test_case_name = "test_case_set_port_03", }; +/* State structure for set port API Test Case #4 */ +static black_box_state_t test_case_set_port_04_state = { + .test_case_name = "test_case_set_port_04", +}; + +static bool try_bind(int portno) { + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + assert_int_not_equal(socket_fd, -1); + + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + bzero(&sin, len); + + assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1); + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = htons(portno); + + errno = 0; + int bind_status = bind(socket_fd, (struct sockaddr *)&sin, len); + + // Exempt EADDRINUSE error only + + if(bind_status) { + assert_int_equal(errno, EADDRINUSE); + } + + assert_int_not_equal(close(socket_fd), -1); + + return !bind_status; +} + +static void wait_for_socket_free(int portno) { + + // Wait upto 20 seconds and poll every second whether the port is freed or not + + for(int i = 0; i < 20; i++) { + if(try_bind(portno)) { + return; + } else { + sleep(1); + } + } + + fail(); +} + +static int get_free_port(void) { + + // Get a free port + + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + assert_int_not_equal(socket_fd, -1); + + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + bzero(&sin, len); + + assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1); + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = 0; + + assert_int_not_equal(bind(socket_fd, (struct sockaddr *)&sin, len), -1); + + assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1); + + assert_int_not_equal(close(socket_fd), -1); + + return (int) sin.sin_port; +} /* Execute meshlink_set_port Test Case # 1 - valid case*/ @@ -99,63 +178,116 @@ static bool test_set_port_01(void) { return true; } -/* Execute meshlink_set_port Test Case # 2 - Invalid case*/ + +/* Execute meshlink_set_port Test Case # 2 - Invalid arguments */ static void test_case_set_port_02(void **state) { execute_test(test_set_port_02, state); } -/* Test Steps for meshlink_set_port Test Case # 2 - Invalid case +/* Test Steps for meshlink_set_port Test Case # 2 - functionality test Test Steps: - 1. Pass NULL as mesh handle argument for meshlink_set_port + 1. Open and start NUT and then pass invalid arguments to the set port API Expected Result: - Report false indicating error. + Meshlink set port API should fail and error out when invalid arguments are passed */ static bool test_set_port_02(void) { + char nut_confbase[PATH_MAX]; + create_path(nut_confbase, NUT, 2); + + // Create meshlink instance + + meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY); + meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb); + // meshlink_set_port called using NULL as mesh handle - bool ret = meshlink_set_port(NULL, 8000); - assert_int_equal(meshlink_errno, 0); - assert_int_equal(ret, false); + meshlink_errno = MESHLINK_OK; + assert_false(meshlink_set_port(NULL, 8000)); + assert_int_equal(meshlink_errno, MESHLINK_EINVAL); - return false; -} + // Setting port after starting NUT + meshlink_errno = MESHLINK_OK; + assert_false(meshlink_set_port(mesh, -1)); + assert_int_equal(meshlink_errno, MESHLINK_EINVAL); + + meshlink_errno = MESHLINK_OK; + assert_false(meshlink_set_port(mesh, 70000)); + assert_int_equal(meshlink_errno, MESHLINK_EINVAL); + + assert_true(meshlink_start(mesh)); + meshlink_errno = MESHLINK_OK; + assert_false(meshlink_set_port(mesh, 8000)); + assert_int_equal(meshlink_errno, MESHLINK_EINVAL); + // Clean up + + meshlink_close(mesh); + assert_true(meshlink_destroy(nut_confbase)); + return true; +} -/* Execute meshlink_set_port Test Case # 3 - Setting port after starting mesh*/ +/* Execute meshlink_set_port Test Case # 3 - Synchronization testing */ static void test_case_set_port_03(void **state) { execute_test(test_set_port_03, state); } -/* Test Steps for meshlink_set_port Test Case # 2 - functionality test +static bool test_set_port_03(void) { + pid_t pid; + int pid_status; + char nut_confbase[PATH_MAX]; + create_path(nut_confbase, NUT, 3); - Test Steps: - 1. Open and start NUT and then try to set new port number + int new_port = get_free_port(); - Expected Result: - New port number cannot be set while mesh is running. -*/ -static bool test_set_port_03(void) { - meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); + // Fork a new process in which NUT opens it's instance, set's the new port and raises SIGINT to terminate. - // Create meshlink instance + pid = fork(); + assert_int_not_equal(pid, -1); - mesh_handle = meshlink_open("getportconf", "nut", "test", 1); - assert(mesh_handle); - meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger); + if(!pid) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY); + assert(mesh); - // Setting port after starting NUT - bool ret = meshlink_set_port(mesh_handle, 50000); - assert_int_equal(meshlink_errno, 0); - assert_int_equal(ret, false); + assert(meshlink_set_port(mesh, new_port)); + raise(SIGINT); + } - // Clean up + // Wait for child exit and verify which signal terminated it - meshlink_close(mesh_handle); - assert(meshlink_destroy("getportconf")); + assert_int_not_equal(waitpid(pid, &pid_status, 0), -1); + assert_int_equal(WIFSIGNALED(pid_status), true); + assert_int_equal(WTERMSIG(pid_status), SIGINT); + + // Wait for the NUT's listening socket to be freed. (i.e, preventing meshlink from binding to a new port + // when NUT instance is reopened and the actual port is not freed due EADDRINUSE) + + wait_for_socket_free(new_port); + + // Reopen the NUT instance in the same test suite - return false; + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY); + assert_non_null(mesh); + + assert_false(try_bind(new_port)); + + // Validate the new port that's being set in the previous instance persists. + + int get_port = meshlink_get_port(mesh); + assert_int_equal(get_port, new_port); + + // Close the mesh instance and verify that the listening port is closed or not + + meshlink_close(mesh); + + wait_for_socket_free(new_port); + + assert_true(meshlink_destroy(nut_confbase)); + return true; } -- 2.39.5