]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/test_step.c
Ensure NDEBUG is not set in the test suite.
[meshlink] / test / blackbox / common / test_step.c
1 /*
2     test_step.c -- Handlers for executing test steps during node simulation
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 ŝ
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <string.h>
28 #include "../../../src/meshlink.h"
29 #include "test_step.h"
30 #include "common_handlers.h"
31
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34
35 meshlink_handle_t *mesh_handle = NULL;
36 bool mesh_started = false;
37 char *eth_if_name = NULL;
38
39 meshlink_handle_t *execute_open(char *node_name, char *dev_class) {
40         /* Set up logging for Meshlink */
41         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
42
43         /* Create meshlink instance */
44         mesh_handle = meshlink_open("testconf", node_name, "node_sim", atoi(dev_class));
45         fprintf(stderr, "meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
46         meshlink_enable_discovery(mesh_handle, false);
47         PRINT_TEST_CASE_MSG("meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
48         assert(mesh_handle);
49
50         /* Set up logging for Meshlink with the newly acquired Mesh Handle */
51         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
52         /* Set up callback for node status (reachable / unreachable) */
53         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
54
55         return mesh_handle;
56 }
57
58 char *execute_invite(char *invitee, meshlink_submesh_t *submesh) {
59         char *invite_url = meshlink_invite_ex(mesh_handle, submesh, invitee, MESHLINK_INVITE_LOCAL | MESHLINK_INVITE_NUMERIC);
60
61         PRINT_TEST_CASE_MSG("meshlink_invite status: %s\n", meshlink_strerror(meshlink_errno));
62         assert(invite_url);
63
64         return invite_url;
65 }
66
67 void execute_join(char *invite_url) {
68         bool join_status;
69
70         join_status = meshlink_join(mesh_handle, invite_url);
71         assert(join_status);
72 }
73
74 void execute_start(void) {
75         bool start_init_status = meshlink_start(mesh_handle);
76
77         PRINT_TEST_CASE_MSG("meshlink_start status: %s\n", meshlink_strerror(meshlink_errno));
78         assert(start_init_status);
79         mesh_started = true;
80 }
81
82 void execute_stop(void) {
83         assert(mesh_handle);
84         meshlink_stop(mesh_handle);
85         mesh_started = false;
86 }
87
88 void execute_close(void) {
89         assert(mesh_handle);
90         meshlink_close(mesh_handle);
91 }
92
93 void execute_change_ip(void) {
94         char *eth_if_ip;
95         int last_byte;
96         char new_ip[20] = "", gateway_ip[20] = "";
97         char *last_dot_in_ip;
98         char *eth_if_netmask;
99
100         /* Get existing IP Address of Ethernet Bridge Interface */
101         assert((eth_if_ip = get_ip(eth_if_name)));
102
103         /* Set new IP Address by replacing the last byte with last byte + 1 */
104         strncpy(new_ip, eth_if_ip, sizeof(new_ip) - 1);
105         assert((last_dot_in_ip = strrchr(new_ip, '.')));
106         last_byte = atoi(last_dot_in_ip + 1);
107         assert(snprintf(last_dot_in_ip + 1, 4, "%d", (last_byte > 253) ? 2 : (last_byte + 1)) >= 0);
108
109         /* TO DO: Check for IP conflicts with other interfaces and existing Containers */
110         /* Bring the network interface down before making changes */
111         stop_nw_intf(eth_if_name);
112         /* Save the netmask first, then restore it after setting the new IP Address */
113         assert((eth_if_netmask = get_netmask(eth_if_name)));
114         set_ip(eth_if_name, new_ip);
115         set_netmask(eth_if_name, eth_if_netmask);
116         /* Bring the network interface back up again to apply changes */
117         start_nw_intf(eth_if_name);
118
119         /* Get Gateway's IP Address, by replacing the last byte with 1 in the current IP Address */
120         /* TO DO: Obtain the actual Gateway IP Address */
121         strncpy(gateway_ip, eth_if_ip, sizeof(gateway_ip) - 1);
122         assert((last_dot_in_ip = strrchr(gateway_ip, '.')));
123         assert(snprintf(last_dot_in_ip + 1, 4, "%d", 1) >= 0);
124
125         /* Add the default route back again, which would have been deleted when the
126             network interface was brought down */
127         /* TO DO: Perform this action using ioctl with SIOCADDRT */
128         /*assert(snprintf(route_chg_command, sizeof(route_chg_command), "route add default gw %s",
129           gateway_ip) >= 0);
130          route_chg_status = system(route_chg_command);
131          PRINT_TEST_CASE_MSG("Default Route Add status = %d\n", route_chg_status);
132          assert(route_chg_status == 0); */
133         // Not necessary for ubuntu versions of 16.04 and 18.04
134
135         PRINT_TEST_CASE_MSG("Node '%s' IP Address changed to %s\n", NUT_NODE_NAME, new_ip);
136
137         free(eth_if_ip);
138         free(eth_if_netmask);
139 }
140