]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_autoconnect.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[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 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 /* Modify this to change the logging level of Meshlink */
25 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
26
27 #include "execute_tests.h"
28 #include "test_cases_autoconnect.h"
29 #include <pthread.h>
30 #include "../common/test_step.h"
31 #include "../common/common_handlers.h"
32 #include "../../utils.h"
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <setjmp.h>
36 #include <cmocka.h>
37 #include <assert.h>
38 #include <string.h>
39
40 static void test_case_autoconnect(void **state);
41 static bool test_steps_mesh_autoconnect(void);
42 static meshlink_handle_t *mesh1, *mesh2;
43
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",
47 };
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};
51
52 /* Execute meshlink_blacklist Test Case # 1*/
53 void test_case_autoconnect(void **state) {
54         execute_test(test_steps_mesh_autoconnect, state);
55 }
56
57 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
58                      const char *text) {
59         (void)level;
60
61         fprintf(stderr, "%s: {%s}\n", mesh->name, text);
62
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);
68         }
69
70 }
71
72 static void receive(meshlink_handle_t *mesh, meshlink_node_t *src, const void *data, size_t len) {
73         (void)mesh;
74         (void)src;
75         (void)data;
76         assert(len);
77 }
78
79 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
80         (void)mesh;
81         fprintf(stderr, "Status of node {%s} is %d\n", node->name, reachable);
82
83         if(!strcmp(node->name, "m1n1") && reachable) {
84                 set_sync_flag(&test_autoconnect_m1n1_reachable, true);
85         }
86 }
87
88
89 /* Test Steps for meshlink_blacklist Test Case # 1
90
91     Test Steps:
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
95
96     Expected Result:
97     When default blacklist is disabled, foo node should receive data from bar
98     but when enabled foo node should not receive data
99 */
100 bool test_steps_mesh_autoconnect(void) {
101         char *invite = NULL;
102         meshlink_node_t *node = NULL;
103
104         assert(meshlink_destroy("m1n1"));
105         assert(meshlink_destroy("m1n2"));
106
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);
111
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);
116
117         // Start both instances
118         meshlink_set_node_status_cb(mesh1, status_cb);
119         assert(meshlink_start(mesh1));
120
121         invite = meshlink_invite(mesh1, NULL, "m1n2");
122         assert(invite);
123
124         assert(meshlink_join(mesh2, invite));
125
126         meshlink_set_node_status_cb(mesh2, status_cb);
127         assert(meshlink_start(mesh2));
128
129         assert(wait_sync_flag(&test_autoconnect_m1n1_reachable, 30));
130
131         node = meshlink_get_node(mesh2, "m1n1");
132         assert(meshlink_blacklist(mesh2, node));
133         set_sync_flag(&test_autoconnect_blacklisted, true);
134
135         assert(wait_sync_flag(&test_autoconnect_successful, 60));
136
137         // Clean up.
138         meshlink_close(mesh1);
139         fprintf(stderr, "Meshlink node1 closed\n");
140         meshlink_close(mesh2);
141         fprintf(stderr, "Meshlink node2 closed\n");
142
143         assert(meshlink_destroy("m1n1"));
144         assert(meshlink_destroy("m1n2"));
145         fprintf(stderr, "Meshlink nodes destroyed\n");
146
147         return true;
148 }
149
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)
154         };
155
156         total_tests += sizeof(blackbox_blacklist_tests) / sizeof(blackbox_blacklist_tests[0]);
157
158         return cmocka_run_group_tests(blackbox_blacklist_tests, NULL, NULL);
159 }
160