]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_channel_blacklist_01/node_sim_peer_01.c
Ensure NDEBUG is not set in the test suite.
[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
21 #ifdef NDEBUG
22 #undef NDEBUG
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include <assert.h>
30 #include <signal.h>
31 #include "../common/common_handlers.h"
32 #include "../common/test_step.h"
33 #include "../common/network_namespace_framework.h"
34 #include "../../utils.h"
35
36 #define CHANNEL_PORT 1234
37
38 bool test_channel_blacklist_disonnection_peer_01_running;
39 bool test_case_signal_peer_restart_01;
40
41 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
43
44 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
45         (void)dat;
46         (void)len;
47
48         assert(port == CHANNEL_PORT);
49
50         if(!strcmp(channel->node->name, "nut")) {
51                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
52                 return true;
53         }
54
55         return false;
56 }
57
58 /* channel receive callback */
59 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
60
61         if(len == 0) {
62                 fprintf(stderr, "Channel closure\n");
63         }
64
65         if(!strcmp(channel->node->name, "nut")) {
66                 if(!memcmp(dat, "test", 5)) {
67                         assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
68                 }
69         }
70
71         return;
72 }
73
74 void *test_channel_blacklist_disonnection_peer_01(void *arg) {
75         struct timeval main_loop_wait = { 2, 0 };
76         mesh_arg_t *mesh_arg = (mesh_arg_t *)arg;
77         test_channel_blacklist_disonnection_peer_01_running = true;
78
79         // Run relay node instance
80
81         meshlink_handle_t *mesh;
82         mesh = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class);
83         assert(mesh);
84         meshlink_set_channel_accept_cb(mesh, channel_accept);
85
86         // Join relay node and if fails to join then try few more attempts
87
88         if(mesh_arg->join_invitation) {
89                 assert(meshlink_join(mesh, mesh_arg->join_invitation));
90         }
91
92         assert(meshlink_start(mesh));
93
94         // All test steps executed - wait for signals to stop/start or close the mesh
95
96         while(test_channel_blacklist_disonnection_peer_01_running) {
97                 select(1, NULL, NULL, NULL, &main_loop_wait);
98
99                 if(test_case_signal_peer_restart_01) {
100                         meshlink_stop(mesh);
101                         assert(meshlink_start(mesh));
102                         test_case_signal_peer_restart_01 = false;
103                 }
104         }
105
106         meshlink_close(mesh);
107
108         return NULL;
109 }