]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_cases_submesh04/node_sim_app1node2.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / test_cases_submesh04 / node_sim_app1node2.c
1 /*
2     node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
3                     for meta connection test case 01 - re-connection of
4                     two nodes when relay node goes down
5     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #ifdef NDEBUG
23 #undef NDEBUG
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <time.h>
33 #include "../common/common_handlers.h"
34 #include "../common/test_step.h"
35 #include "../common/mesh_event_handler.h"
36 #include "../../utils.h"
37
38 #define CMD_LINE_ARG_NODENAME   1
39 #define CMD_LINE_ARG_DEVCLASS   2
40 #define CMD_LINE_ARG_CLIENTID   3
41 #define CMD_LINE_ARG_IMPORTSTR  4
42 #define CMD_LINE_ARG_INVITEURL  5
43 #define CHANNEL_PORT 1234
44
45 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
46 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
47
48 static int client_id = -1;
49 static meshlink_handle_t *mesh = NULL;
50
51 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
52 static struct sync_flag start_test = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
53 static struct sync_flag app_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
54 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
55 static struct sync_flag channel_data_recieved = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
56 static meshlink_channel_t *ch_app1node1 = NULL;
57
58 static void send_event(mesh_event_t event) {
59         int attempts;
60
61         for(attempts = 0; attempts < 5; attempts += 1) {
62                 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
63                         break;
64                 }
65         }
66
67         assert(attempts < 5);
68
69         return;
70 }
71
72 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
73         (void)dat;
74         (void)len;
75
76         assert(port == CHANNEL_PORT);
77
78         fprintf(stderr, "\tapp1node2 got channel request from %s\n", channel->node->name);
79
80         if(!strcmp(channel->node->name, "corenode1")) {
81                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
82                 mesh->priv = channel;
83
84                 return true;
85         }
86
87         return false;
88 }
89
90 /* channel receive callback */
91 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
92         (void)mesh;
93
94         char data[100] = {0};
95
96         if(len == 0) {
97                 fprintf(stderr, "\tapp1node2 got error from %s at %lu\n", channel->node->name, time(NULL));
98                 send_event(ERR_NETWORK);
99                 return;
100         }
101
102         memcpy(data, dat, len);
103
104         fprintf(stderr, "\tapp1node2 got message from %s as %s\n", channel->node->name, data);
105
106         if(!strcmp(channel->node->name, "corenode1")) {
107                 if(!memcmp(dat, "Channel Message", len)) {
108                         set_sync_flag(&channel_data_recieved, true);
109                 } else if(!memcmp(dat, "failure", 7)) {
110                         assert(false);
111                 }
112         } else if(!strcmp(channel->node->name, "app1node1")) {
113                 if(!memcmp(dat, "Channel Message", len)) {
114                         ch_app1node1 = channel;
115                         set_sync_flag(&channel_data_recieved, true);
116                 } else if(!memcmp(dat, "failure", 7)) {
117                         assert(false);
118                 }
119         } else {
120                 assert(false);
121         }
122
123         return;
124 }
125
126 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
127         char *message = "Channel Message";
128         char *node = (char *)channel->node->name;
129         (void)len;
130         meshlink_set_channel_poll_cb(mesh, channel, NULL);
131         fprintf(stderr, "\tapp1node2's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
132         set_sync_flag(&channel_opened, true);
133         assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
134         return;
135 }
136
137
138 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
139         (void)mesh;
140
141         if(!strcasecmp(node->name, "corenode1")) {
142                 if(reachable) {
143                         fprintf(stderr, "\tNode corenode1 became reachable\n");
144                         set_sync_flag(&peer_reachable, true);
145                 }
146         } else if(!strcasecmp(node->name, "app1node1")) {
147                 if(reachable) {
148                         fprintf(stderr, "\tNode app1node1 became reachable\n");
149                         set_sync_flag(&app_reachable, true);
150                 }
151         }
152
153         return;
154 }
155
156 void mesh_start_test_handler(int signum) {
157         (void)signum;
158
159         fprintf(stderr, "Starting test in app1node2\n");
160         set_sync_flag(&start_test, true);
161 }
162
163 int main(int argc, char *argv[]) {
164         (void)argc;
165
166         size_t num_nodes, i;
167         struct timeval main_loop_wait = { 2, 0 };
168         meshlink_channel_t *channel = NULL;
169         meshlink_node_t *core_node = NULL;
170         meshlink_node_t **node_handles = NULL;
171
172         fprintf(stderr, "\tMesh node 'app1node2' starting up........\n");
173
174         // Import mesh event handler
175
176         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
177                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
178                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
179         }
180
181         // Setup required signals
182
183         setup_signals();
184         signal(SIGIO, mesh_start_test_handler);
185
186         // Run peer node instance
187
188         mesh = meshlink_open("app1node2conf", argv[CMD_LINE_ARG_NODENAME],
189                              "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
190         assert(mesh);
191         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
192         meshlink_set_channel_accept_cb(mesh, channel_accept);
193         meshlink_set_node_status_cb(mesh, node_status_cb);
194
195         if(argv[CMD_LINE_ARG_INVITEURL]) {
196                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
197         }
198
199         assert(meshlink_start(mesh));
200
201         send_event(NODE_STARTED);
202
203         // Wait for peer node to join
204
205         assert(wait_sync_flag(&peer_reachable, 15));
206         send_event(NODE_JOINED);
207
208         while(false == wait_sync_flag(&start_test, 10));
209
210         // Open a channel to peer node
211         core_node = meshlink_get_node(mesh, "corenode1");
212         assert(core_node);
213         fprintf(stderr, "\tapp1node2 Sending Channel request to corenode1 at : %lu\n", time(NULL));
214         channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
215                                         channel_receive_cb, NULL, 0);
216         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
217         assert(wait_sync_flag(&channel_opened, 15));
218         send_event(CHANNEL_OPENED);
219
220         assert(wait_sync_flag(&channel_data_recieved, 30));
221         send_event(CHANNEL_DATA_RECIEVED);
222
223         // Open a channel to peer node
224         channel_opened.flag = false;
225         channel_data_recieved.flag = false;
226
227         assert(wait_sync_flag(&app_reachable, 60));
228
229         core_node = meshlink_get_node(mesh, "app1node1");
230         assert(core_node);
231         fprintf(stderr, "\tapp1node2 Sending Channel request to app1node1 at : %lu\n", time(NULL));
232         channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
233                                         channel_receive_cb, NULL, 0);
234         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
235         assert(wait_sync_flag(&channel_opened, 15));
236         send_event(CHANNEL_OPENED);
237
238         assert(wait_sync_flag(&channel_data_recieved, 30));
239         send_event(CHANNEL_DATA_RECIEVED);
240
241         num_nodes = 0;
242         node_handles = meshlink_get_all_nodes(mesh, NULL, &num_nodes);
243         fprintf(stderr, "\tGot %lu nodes in list with error : %s\n", num_nodes, meshlink_strerror(meshlink_errno));
244         assert(node_handles);
245
246         for(i = 0; i < num_nodes; i++) {
247                 fprintf(stderr, "\tChecking the node : %s\n", node_handles[i]->name);
248
249                 if(0 == strcmp(node_handles[i]->name, "app2node1")) {
250                         send_event(SIG_ABORT);
251                         assert(false);
252                 } else if(0 == strcmp(node_handles[i]->name, "app2node2")) {
253                         send_event(SIG_ABORT);
254                         assert(false);
255                 }
256         }
257
258         meshlink_node_t *app1_node1 = meshlink_get_node(mesh, "app1node1");
259
260         if(!app1_node1) {
261                 send_event(SIG_ABORT);
262                 assert(app1_node1);
263         }
264
265         channel_data_recieved.flag = false;
266         assert(meshlink_blacklist(mesh, app1_node1));
267
268         sleep(2);
269
270         assert(meshlink_channel_send(mesh, ch_app1node1, "test", 5) == 5);
271
272         wait_sync_flag(&channel_data_recieved, 30);
273
274         if(true == channel_data_recieved.flag) {
275                 send_event(SIG_ABORT);
276                 assert(false);
277         }
278
279         channel_data_recieved.flag = false;
280         assert(meshlink_whitelist(mesh, app1_node1));
281
282         sleep(2);
283
284         assert(meshlink_channel_send(mesh, ch_app1node1, "Channel Message", strlen("Channel Message")) == strlen("Channel Message"));
285
286         assert(wait_sync_flag(&channel_data_recieved, 60));
287
288         send_event(MESH_EVENT_COMPLETED);
289
290         // All test steps executed - wait for signals to stop/start or close the mesh
291
292         while(test_running) {
293                 select(1, NULL, NULL, NULL, &main_loop_wait);
294         }
295
296         meshlink_close(mesh);
297
298         return EXIT_SUCCESS;
299 }