]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_autoconnect.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_autoconnect.c
1 /*
2     test_cases_blacklist.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 /* Modify this to change the logging level of Meshlink */
21 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
22
23 #include "execute_tests.h"
24 #include "test_cases_autoconnect.h"
25 #include <pthread.h>
26 #include "../common/test_step.h"
27 #include "../common/common_handlers.h"
28 #include "../../utils.h"
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <setjmp.h>
32 #include <cmocka.h>
33 #include <assert.h>
34 #include <string.h>
35
36 static void test_case_autoconnect(void **state);
37 static bool test_steps_mesh_autoconnect(void);
38 static meshlink_handle_t *mesh1, *mesh2;
39
40 /* State structure for meshlink_blacklist Test Case #1 */
41 static black_box_state_t test_mesh_autoconnect_state = {
42         .test_case_name = "test_case_mesh_autoconnect",
43 };
44 struct sync_flag test_autoconnect_m1n1_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
45 struct sync_flag test_autoconnect_blacklisted = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
46 struct sync_flag test_autoconnect_successful = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
47
48 /* Execute meshlink_blacklist Test Case # 1*/
49 void test_case_autoconnect(void **state) {
50         execute_test(test_steps_mesh_autoconnect, state);
51 }
52
53 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
54                      const char *text) {
55         (void)level;
56
57         fprintf(stderr, "%s: {%s}\n", mesh->name, text);
58
59         if((check_sync_flag(&test_autoconnect_blacklisted) == true) && (strcmp("m1n2", mesh->name) == 0) && (strcmp("* could not find node for initial connect", text) == 0)) {
60                 fprintf(stderr, "Test case successful\n");
61                 set_sync_flag(&test_autoconnect_successful, true);
62         } else if((check_sync_flag(&test_autoconnect_blacklisted) == true) && (strcmp("m1n2", mesh->name) == 0)) {
63                 assert(strcmp(text, "Autoconnect trying to connect to m1n1") != 0);
64         }
65
66 }
67
68 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
69         (void)mesh;
70         (void)src;
71         (void)data;
72         assert(len);
73 }
74
75 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
76         (void)mesh;
77         fprintf(stderr, "Status of node {%s} is %d\n", node->name, reachable);
78
79         if(!strcmp(node->name, "m1n1") && reachable) {
80                 set_sync_flag(&test_autoconnect_m1n1_reachable, true);
81         }
82 }
83
84
85 /* Test Steps for meshlink_blacklist Test Case # 1
86
87     Test Steps:
88     1. Open both the node instances
89     2. Join bar node with foo and Send & Receive data
90     3. Blacklist bar and Send & Receive data
91
92     Expected Result:
93     When default blacklist is disabled, foo node should receive data from bar
94     but when enabled foo node should not receive data
95 */
96 bool test_steps_mesh_autoconnect(void) {
97         char *invite = NULL;
98         meshlink_node_t *node = NULL;
99
100         meshlink_destroy("m1n1");
101         meshlink_destroy("m1n2");
102
103         // Open two new meshlink instance.
104         mesh1 = meshlink_open("m1n1", "m1n1", "autoconnect", DEV_CLASS_BACKBONE);
105         assert(mesh1 != NULL);
106         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, callback_logger);
107
108         mesh2 = meshlink_open("m1n2", "m1n2", "autoconnect", DEV_CLASS_STATIONARY);
109         assert(mesh2 != NULL);
110         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, callback_logger);
111         meshlink_set_receive_cb(mesh1, receive);
112
113         // Start both instances
114         meshlink_set_node_status_cb(mesh1, status_cb);
115         assert(meshlink_start(mesh1));
116
117         invite = meshlink_invite(mesh1, NULL, "m1n2");
118         assert(invite);
119
120         assert(meshlink_join(mesh2, invite));
121
122         meshlink_set_node_status_cb(mesh2, status_cb);
123         assert(meshlink_start(mesh2));
124
125         assert(wait_sync_flag(&test_autoconnect_m1n1_reachable, 30));
126
127         node = meshlink_get_node(mesh2, "m1n1");
128         meshlink_blacklist(mesh2, node);
129         set_sync_flag(&test_autoconnect_blacklisted, true);
130
131         assert(wait_sync_flag(&test_autoconnect_successful, 60));
132
133         // Clean up.
134         meshlink_close(mesh1);
135         fprintf(stderr, "Meshlink node1 closed\n");
136         meshlink_close(mesh2);
137         fprintf(stderr, "Meshlink node2 closed\n");
138
139         meshlink_destroy("m1n1");
140         meshlink_destroy("m1n2");
141         fprintf(stderr, "Meshlink nodes destroyed\n");
142
143         return true;
144 }
145
146 int test_meshlink_autoconnect(void) {
147         const struct CMUnitTest blackbox_blacklist_tests[] = {
148                 cmocka_unit_test_prestate_setup_teardown(test_case_autoconnect, NULL, NULL,
149                                 (void *)&test_mesh_autoconnect_state)
150         };
151
152         total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
153
154         return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);
155 }
156