]> git.meshlink.io Git - meshlink/blobdiff - test/blackbox/run_blackbox_tests/test_cases_set_port.c
Add missing atomic test cases to the APIs that affects disk writes
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_set_port.c
index e514e57f11328d4cf82bc0b96768a47547c40703..c2153999ea755a6cd827fde2f6692bbd4cc63556 100644 (file)
@@ -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 <assert.h>
 #include <string.h>
 #include <stdarg.h>
 #include <setjmp.h>
 #include <cmocka.h>
-
+#include <signal.h>
+#include <wait.h>
+#include <linux/limits.h>
 
 /* 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;
 }