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