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