+static void nop_stage(bool stage) {
+ (void)stage;
+ return;
+}
+
+static void debug_probe(bool stage) {
+ (void)stage;
+ raise(SIGINT);
+ return;
+}
+
+/* Test Steps for meshlink_get_node_reachability Test Case # 5 - Test the invitee committing first scenario
+
+ Test steps and scenarios:
+ 1. Open peer node instance, invite NUT and start peer node. Enable the debug probe, Spawn a new process in
+ which it opens and joins the NUT with peer node which terminates the NUT while joining.
+ Reopen NUT instance in the test suite process and verify peer is joined.
+ Expected Result:
+ NUT(invitee) commits the config file(s) first but peer is unaware of NUT.
+
+*/
+static void test_case_meshlink_join_05(void **state) {
+ (void) state;
+ pid_t pid;
+ int pid_status;
+ char nut_confbase[PATH_MAX];
+ char peer_confbase[PATH_MAX];
+ create_path(nut_confbase, NUT, 5);
+ create_path(peer_confbase, PEER, 5);
+ meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
+
+ assert(signal(SIGINT, SIG_DFL) != SIG_ERR);
+ assert(signal(SIGABRT, SIG_DFL) != SIG_ERR);
+
+ // Set debug_probe callback
+
+ devtool_set_inviter_commits_first = debug_probe;
+
+ // Open peer node instance and invite NUT.
+
+ meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_JOIN,
+ DEV_CLASS_STATIONARY);
+ assert_int_not_equal(mesh_peer, NULL);
+ meshlink_set_inviter_commits_first(mesh_peer, false);
+ char *invitation = meshlink_invite(mesh_peer, NULL, NUT);
+ assert_non_null(invitation);
+
+ assert_true(meshlink_start(mesh_peer));
+
+ // Fork a new process in which NUT is joins with the peer node and raises SIGINT to terminate.
+
+ pid = fork();
+ assert_int_not_equal(pid, -1);
+
+ if(!pid) {
+ meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_JOIN, DEV_CLASS_STATIONARY);
+ assert(mesh);
+ meshlink_set_inviter_commits_first(mesh_peer, false);
+ meshlink_set_log_cb(mesh, MESHLINK_DEBUG, log_cb);
+
+ assert_true(meshlink_join(mesh, invitation));
+
+ raise(SIGABRT);
+ }
+
+ // Wait for child exit and verify which signal terminated it
+ printf("\n");
+ 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_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_JOIN, DEV_CLASS_STATIONARY);
+ assert_non_null(mesh);
+
+ // Invitee committed host config file but invitee should not
+
+ assert_non_null(meshlink_get_node(mesh, PEER));
+ assert_null(meshlink_get_node(mesh_peer, NUT));
+
+ // Cleanup
+
+ free(invitation);
+ meshlink_close(mesh);
+ meshlink_close(mesh_peer);
+ assert_true(meshlink_destroy(nut_confbase));
+ assert_true(meshlink_destroy(peer_confbase));
+
+ devtool_set_inviter_commits_first = nop_stage;
+ return;
+}
+
+/* Test Steps for meshlink_get_node_reachability Test Case # 6 - Test the inviter committing first scenario
+
+ Test steps and scenarios:
+ 1. Open NUT node instance, invite peer and close the instance. Enable the debug probe, Spawn a new process in
+ which it starts the NUT instance. At the parents/test vector thread wait for the signal that NUT raises after starting
+ and join peer with NUT. NUT terminates in debug probe after committing into the disk
+ Reopen NUT instance in the test suite process and verify peer is joined.
+ Expected Result:
+ NUT(inviter) commits the config file(s) first but peer is unaware of NUT.
+
+*/
+static void test_case_meshlink_join_06(void **state) {
+ (void) state;
+ pid_t pid;
+ int pid_status;
+ char nut_confbase[PATH_MAX];
+ char peer_confbase[PATH_MAX];
+ create_path(nut_confbase, NUT, 6);
+ create_path(peer_confbase, PEER, 6);
+ meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
+
+ assert(signal(SIGINT, SIG_DFL) != SIG_ERR);
+ assert(signal(SIGABRT, SIG_DFL) != SIG_ERR);
+
+ // Set debug_probe callback
+
+ devtool_set_inviter_commits_first = debug_probe;
+
+ // Open NUT node instance and invite peer node. Close NUT node instance.
+
+ meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_JOIN, DEV_CLASS_STATIONARY);
+ assert_non_null(mesh);
+ meshlink_set_inviter_commits_first(mesh, true);
+ char *invitation = meshlink_invite(mesh, NULL, PEER);
+ meshlink_close(mesh);
+
+ // Set the SIGUSR2 signal handler with handler that signal the condition to the test suite
+
+ sighandler_t usr2sighandler = signal(SIGUSR2, nut_started_user_signal_handler);
+ assert_int_not_equal(usr2sighandler, SIG_ERR);
+ set_sync_flag(&peer_reachable_status_cond, false);
+
+ // Fork a new process and run NUT in it which just waits for the peer node reachable status callback
+ // and terminates the process immediately.
+
+ pid = fork();
+ assert_int_not_equal(pid, -1);
+
+ if(!pid) {
+ assert(signal(SIGUSR2, SIG_DFL) != SIG_ERR);
+
+ mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_JOIN, DEV_CLASS_STATIONARY);
+ assert(mesh);
+ meshlink_set_inviter_commits_first(mesh, true);
+ meshlink_set_log_cb(mesh, MESHLINK_DEBUG, log_cb);
+ meshlink_set_node_status_cb(mesh, meshlink_node_reachable_status_cb);
+
+ assert(meshlink_start(mesh));
+
+ assert(kill(getppid(), SIGUSR2) != -1);
+
+ sleep(10);
+
+ raise(SIGABRT);
+ }
+
+ // Open peer node instance and join with the invitation obtained.
+
+ meshlink_handle_t *mesh_peer = meshlink_open(peer_confbase, PEER, TEST_MESHLINK_JOIN,
+ DEV_CLASS_STATIONARY);
+ assert_non_null(mesh_peer);
+ meshlink_set_inviter_commits_first(mesh_peer, true);
+
+ // Wait for the started signal from NUT and reset the previous SIGUSR2 signal handler
+
+ assert_true(wait_sync_flag(&nut_started_status_cond, 60));
+ assert_int_not_equal(signal(SIGUSR2, usr2sighandler), SIG_ERR);
+
+ assert_false(meshlink_join(mesh_peer, invitation));
+
+ // 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
+
+ mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_JOIN, DEV_CLASS_STATIONARY);
+ assert_non_null(mesh);
+
+ // Inviter should first commit config file(s) into the disk
+
+ assert_null(meshlink_get_node(mesh_peer, NUT));
+ assert_non_null(meshlink_get_node(mesh, PEER));
+
+ // Cleanup
+
+ free(invitation);
+ meshlink_close(mesh);
+ meshlink_close(mesh_peer);
+ assert_true(meshlink_destroy(nut_confbase));
+ assert_true(meshlink_destroy(peer_confbase));
+ devtool_set_inviter_commits_first = nop_stage;
+ return;
+}
+
+/* Test Steps for meshlink_join Test Case # 7 - Inviter sets that invitee should commit first,
+ even invitee sets that inviter should commit first.