]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_channel_blacklist_01/node_sim_peer_01.c
Add test case for channel blacklist
[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 int client_id = -1;
40
41 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
42         (void)dat;
43         (void)len;
44
45         assert(port == CHANNEL_PORT);
46
47         if(!strcmp(channel->node->name, "nut")) {
48                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
49                 return true;
50         }
51
52         return false;
53 }
54
55 /* channel receive callback */
56 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
57
58         if(len == 0) {
59                 fprintf(stderr, "Channel closure\n");
60         }
61
62         if(!strcmp(channel->node->name, "nut")) {
63                 if(!memcmp(dat, "test", 5)) {
64                         assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
65                 }
66         }
67
68         return;
69 }
70
71 void *test_channel_blacklist_disonnection_peer_01(void *arg) {
72         struct timeval main_loop_wait = { 2, 0 };
73         mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
74         test_channel_blacklist_disonnection_peer_01_running = true;
75
76         // Run relay node instance
77
78         meshlink_handle_t *mesh;
79         mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
80         assert(mesh);
81         meshlink_set_channel_accept_cb(mesh, channel_accept);
82
83         // Join relay node and if fails to join then try few more attempts
84
85         if(mesh_arg->join_invitation) {
86                 assert(meshlink_join(mesh, mesh_arg->join_invitation));
87         }
88
89         assert(meshlink_start(mesh));
90
91         // All test steps executed - wait for signals to stop/start or close the mesh
92
93         while(test_channel_blacklist_disonnection_peer_01_running) {
94                 select(1, NULL, NULL, NULL, &main_loop_wait);
95
96                 if(test_case_signal_peer_restart_01) {
97                         meshlink_stop(mesh);
98                         assert(meshlink_start(mesh));
99                         test_case_signal_peer_restart_01 = false;
100                 }
101         }
102
103         meshlink_close(mesh);
104
105         return NULL;
106 }