2 test_cases_blacklist.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* Modify this to change the logging level of Meshlink */
25 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
27 #include "execute_tests.h"
28 #include "test_cases_autoconnect.h"
30 #include "../common/test_step.h"
31 #include "../common/common_handlers.h"
32 #include "../../utils.h"
40 static void test_case_autoconnect(void **state);
41 static bool test_steps_mesh_autoconnect(void);
42 static meshlink_handle_t *mesh1, *mesh2;
44 /* State structure for meshlink_blacklist Test Case #1 */
45 static black_box_state_t test_mesh_autoconnect_state = {
46 .test_case_name = "test_case_mesh_autoconnect",
48 struct sync_flag test_autoconnect_m1n1_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
49 struct sync_flag test_autoconnect_blacklisted = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
50 struct sync_flag test_autoconnect_successful = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
52 /* Execute meshlink_blacklist Test Case # 1*/
53 void test_case_autoconnect(void **state) {
54 execute_test(test_steps_mesh_autoconnect, state);
57 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
61 fprintf(stderr, "%s: {%s}\n", mesh->name, text);
63 if((check_sync_flag(&test_autoconnect_blacklisted) == true) && (strcmp("m1n2", mesh->name) == 0) && (strcmp("* could not find node for initial connect", text) == 0)) {
64 fprintf(stderr, "Test case successful\n");
65 set_sync_flag(&test_autoconnect_successful, true);
66 } else if((check_sync_flag(&test_autoconnect_blacklisted) == true) && (strcmp("m1n2", mesh->name) == 0)) {
67 assert(strcmp(text, "Autoconnect trying to connect to m1n1") != 0);
72 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
79 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
81 fprintf(stderr, "Status of node {%s} is %d\n", node->name, reachable);
83 if(!strcmp(node->name, "m1n1") && reachable) {
84 set_sync_flag(&test_autoconnect_m1n1_reachable, true);
89 /* Test Steps for meshlink_blacklist Test Case # 1
92 1. Open both the node instances
93 2. Join bar node with foo and Send & Receive data
94 3. Blacklist bar and Send & Receive data
97 When default blacklist is disabled, foo node should receive data from bar
98 but when enabled foo node should not receive data
100 bool test_steps_mesh_autoconnect(void) {
102 meshlink_node_t *node = NULL;
104 assert(meshlink_destroy("m1n1"));
105 assert(meshlink_destroy("m1n2"));
107 // Open two new meshlink instance.
108 mesh1 = meshlink_open("m1n1", "m1n1", "autoconnect", DEV_CLASS_BACKBONE);
109 assert(mesh1 != NULL);
110 meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, callback_logger);
112 mesh2 = meshlink_open("m1n2", "m1n2", "autoconnect", DEV_CLASS_STATIONARY);
113 assert(mesh2 != NULL);
114 meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, callback_logger);
115 meshlink_set_receive_cb(mesh1, receive);
117 // Start both instances
118 meshlink_set_node_status_cb(mesh1, status_cb);
119 assert(meshlink_start(mesh1));
121 invite = meshlink_invite(mesh1, NULL, "m1n2");
124 assert(meshlink_join(mesh2, invite));
126 meshlink_set_node_status_cb(mesh2, status_cb);
127 assert(meshlink_start(mesh2));
129 assert(wait_sync_flag(&test_autoconnect_m1n1_reachable, 30));
131 node = meshlink_get_node(mesh2, "m1n1");
132 assert(meshlink_blacklist(mesh2, node));
133 set_sync_flag(&test_autoconnect_blacklisted, true);
135 assert(wait_sync_flag(&test_autoconnect_successful, 60));
138 meshlink_close(mesh1);
139 fprintf(stderr, "Meshlink node1 closed\n");
140 meshlink_close(mesh2);
141 fprintf(stderr, "Meshlink node2 closed\n");
143 assert(meshlink_destroy("m1n1"));
144 assert(meshlink_destroy("m1n2"));
145 fprintf(stderr, "Meshlink nodes destroyed\n");
150 int test_meshlink_autoconnect(void) {
151 const struct CMUnitTest blackbox_blacklist_tests[] = {
152 cmocka_unit_test_prestate_setup_teardown(test_case_autoconnect, NULL, NULL,
153 (void *)&test_mesh_autoconnect_state)
156 total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
158 return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);