]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_channel_blacklist_01/node_sim_peer_01.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / test_case_channel_blacklist_01 / node_sim_peer_01.c
1 /*
2     node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
3                     for channel connections with respective to blacklisting their nodes
4     Copyright (C) 2019  Guus Sliepen <guus@meshlink.io>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <assert.h>
25 #include <signal.h>
26 #include "../common/common_handlers.h"
27 #include "../common/test_step.h"
28 #include "../common/network_namespace_framework.h"
29 #include "../../utils.h"
30
31 #define CHANNEL_PORT 1234
32
33 bool test_channel_blacklist_disonnection_peer_01_running;
34 bool test_case_signal_peer_restart_01;
35
36 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
37 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
38
39 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
40         (void)dat;
41         (void)len;
42
43         assert(port == CHANNEL_PORT);
44
45         if(!strcmp(channel->node->name, "nut")) {
46                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
47                 return true;
48         }
49
50         return false;
51 }
52
53 /* channel receive callback */
54 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
55
56         if(len == 0) {
57                 fprintf(stderr, "Channel closure\n");
58         }
59
60         if(!strcmp(channel->node->name, "nut")) {
61                 if(!memcmp(dat, "test", 5)) {
62                         assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
63                 }
64         }
65
66         return;
67 }
68
69 void *test_channel_blacklist_disonnection_peer_01(void *arg) {
70         struct timeval main_loop_wait = { 2, 0 };
71         mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
72         test_channel_blacklist_disonnection_peer_01_running = true;
73
74         // Run relay node instance
75
76         meshlink_handle_t *mesh;
77         mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
78         assert(mesh);
79         meshlink_set_channel_accept_cb(mesh, channel_accept);
80
81         // Join relay node and if fails to join then try few more attempts
82
83         if(mesh_arg->join_invitation) {
84                 assert(meshlink_join(mesh, mesh_arg->join_invitation));
85         }
86
87         assert(meshlink_start(mesh));
88
89         // All test steps executed - wait for signals to stop/start or close the mesh
90
91         while(test_channel_blacklist_disonnection_peer_01_running) {
92                 select(1, NULL, NULL, NULL, &main_loop_wait);
93
94                 if(test_case_signal_peer_restart_01) {
95                         meshlink_stop(mesh);
96                         assert(meshlink_start(mesh));
97                         test_case_signal_peer_restart_01 = false;
98                 }
99         }
100
101         meshlink_close(mesh);
102
103         return NULL;
104 }