]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/test_step.c
Update invite API in blackbox test cases
[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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <string.h>
24 #include "../../../src/meshlink.h"
25 #include "test_step.h"
26 #include "common_handlers.h"
27
28 /* Modify this to change the logging level of Meshlink */
29 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
30
31 meshlink_handle_t *mesh_handle = NULL;
32 bool mesh_started = false;
33 char *eth_if_name = NULL;
34
35 meshlink_handle_t *execute_open(char *node_name, char *dev_class) {
36         /* Set up logging for Meshlink */
37         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
38
39         /* Create meshlink instance */
40         mesh_handle = meshlink_open("testconf", node_name, "node_sim", atoi(dev_class));
41         fprintf(stderr, "meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
42         meshlink_enable_discovery(mesh_handle, false);
43         PRINT_TEST_CASE_MSG("meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
44         assert(mesh_handle);
45
46         /* Set up logging for Meshlink with the newly acquired Mesh Handle */
47         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
48         /* Set up callback for node status (reachable / unreachable) */
49         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
50
51         return mesh_handle;
52 }
53
54 char *execute_invite(char *invitee) {
55         char *invite_url = meshlink_invite_ex(mesh_handle, NULL, invitee, MESHLINK_INVITE_LOCAL | MESHLINK_INVITE_NUMERIC);
56
57         PRINT_TEST_CASE_MSG("meshlink_invite status: %s\n", meshlink_strerror(meshlink_errno));
58         assert(invite_url);
59
60         return invite_url;
61 }
62
63 void execute_join(char *invite_url) {
64         bool join_status;
65
66         join_status = meshlink_join(mesh_handle, invite_url);
67         assert(join_status);
68 }
69
70 void execute_start(void) {
71         bool start_init_status = meshlink_start(mesh_handle);
72
73         PRINT_TEST_CASE_MSG("meshlink_start status: %s\n", meshlink_strerror(meshlink_errno));
74         assert(start_init_status);
75         mesh_started = true;
76 }
77
78 void execute_stop(void) {
79         assert(mesh_handle);
80         meshlink_stop(mesh_handle);
81         mesh_started = false;
82 }
83
84 void execute_close(void) {
85         assert(mesh_handle);
86         meshlink_close(mesh_handle);
87 }
88
89 void execute_change_ip(void) {
90         char *eth_if_ip;
91         int last_byte;
92         char new_ip[20], gateway_ip[20];
93         char *last_dot_in_ip;
94         char *eth_if_netmask;
95         char route_chg_command[200];
96         int route_chg_status;
97
98         /* Get existing IP Address of Ethernet Bridge Interface */
99         assert(eth_if_ip = get_ip(eth_if_name));
100
101         /* Set new IP Address by replacing the last byte with last byte + 1 */
102         strncpy(new_ip, eth_if_ip, sizeof(new_ip));
103         assert(last_dot_in_ip = strrchr(new_ip, '.'));
104         last_byte = atoi(last_dot_in_ip + 1);
105         assert(snprintf(last_dot_in_ip + 1, 4, "%d", (last_byte > 253) ? 2 : (last_byte + 1)) >= 0);
106
107         /* TO DO: Check for IP conflicts with other interfaces and existing Containers */
108         /* Bring the network interface down before making changes */
109         stop_nw_intf(eth_if_name);
110         /* Save the netmask first, then restore it after setting the new IP Address */
111         assert(eth_if_netmask = get_netmask(eth_if_name));
112         set_ip(eth_if_name, new_ip);
113         set_netmask(eth_if_name, eth_if_netmask);
114         /* Bring the network interface back up again to apply changes */
115         start_nw_intf(eth_if_name);
116
117         /* Get Gateway's IP Address, by replacing the last byte with 1 in the current IP Address */
118         /* TO DO: Obtain the actual Gateway IP Address */
119         strncpy(gateway_ip, eth_if_ip, sizeof(gateway_ip));
120         assert(last_dot_in_ip = strrchr(gateway_ip, '.'));
121         assert(snprintf(last_dot_in_ip + 1, 4, "%d", 1) >= 0);
122
123         /* Add the default route back again, which would have been deleted when the
124             network interface was brought down */
125         /* TO DO: Perform this action using ioctl with SIOCADDRT */
126         /*assert(snprintf(route_chg_command, sizeof(route_chg_command), "route add default gw %s",
127           gateway_ip) >= 0);
128          route_chg_status = system(route_chg_command);
129          PRINT_TEST_CASE_MSG("Default Route Add status = %d\n", route_chg_status);
130          assert(route_chg_status == 0); */
131         // Not necessary for ubuntu versions of 16.04 and 18.04
132
133         PRINT_TEST_CASE_MSG("Node '%s' IP Address changed to %s\n", NUT_NODE_NAME, new_ip);
134
135         free(eth_if_ip);
136         free(eth_if_netmask);
137 }
138