+/* 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;
+}
+