]> git.meshlink.io Git - meshlink/blobdiff - test/blackbox/run_blackbox_tests/test_cases_invite.c
Add missing atomic test cases to the APIs that affects disk writes
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_invite.c
index 929b002ab3241fa663259960b6b00101d1509761..f04cb099dead95811a1181ac0c7ef0461edf3f9a 100644 (file)
 #include "../common/containers.h"
 #include "../common/test_step.h"
 #include "../common/common_handlers.h"
+#include "../../utils.h"
 #include <assert.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include <signal.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_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]);