]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/network_namespace_framework.h
Add network namespace test framework
[meshlink] / test / blackbox / common / network_namespace_framework.h
1 /*
2     network_namespace_framework.h -- Declarations for Individual Test Case implementation functions
3     Copyright (C) 2019  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 #define _GNU_SOURCE 1
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <setjmp.h>
23 #include <cmocka.h>
24 #include <stdio.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/ioctl.h>
29 #include <net/if.h>
30 #include <arpa/inet.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <ifaddrs.h>
34 #include <ctype.h>
35 #include <assert.h>
36 #include <unistd.h>
37 #include <pthread.h>
38 #include <netinet/in.h>
39 #include <stdbool.h>
40 #include <fcntl.h>
41 #include <time.h>
42 #include <sched.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <spawn.h>
46
47 #define PUB_IF  0
48 #define PRIV_IF 1
49
50 typedef enum {
51         HOST,
52         FULL_CONE,
53         PORT_REST,
54         ADDR_REST,
55         SYMMERTRIC,
56         BRIDGE,
57 } namespace_type_t;
58
59 typedef void *pthread_fun_ptr_t(void *arg);
60
61 typedef struct {
62         char *if_name;
63         int   if_type;
64         char *if_peer;
65         char *if_addr;
66         char *if_route;
67         char *addr_host;
68         char *fetch_ip_netns_name;
69         char *if_default_route_ip;
70         void *priv;
71 } interface_t;
72
73 typedef struct {
74         char *snat_to_source;
75         char *dnat_to_destination;
76 } netns_fullcone_handle_t;
77
78 typedef struct {
79         char *name;
80         namespace_type_t type;
81         void *nat_arg;
82         char static_config_net_addr[INET6_ADDRSTRLEN];   // Buffer should be of length INET_ADDRSTRLEN or INET6_ADDRSTRLEN
83         interface_t *interfaces;
84         int interfaces_no;
85         pid_t *pids;
86         int pid_nos;
87         void *priv;
88 } namespace_t;
89
90 typedef struct {
91         char *test_case_name;
92         namespace_t *namespaces;
93         int num_namespaces;
94         char *public_net_addr;
95         pthread_t **threads;
96         bool test_result;
97 } netns_state_t;
98
99 typedef struct {
100         char *namespace_name;
101         pthread_fun_ptr_t *netns_thread;
102         pthread_t thread_handle;
103         void *arg;
104 } netns_thread_t;
105
106 typedef struct {
107         char *node_name;
108         char *confbase;
109         char *app_name;
110         int dev_class;
111         char *join_invitation;
112 } mesh_arg_t;
113
114 typedef struct {
115         mesh_arg_t *mesh_arg;
116         char *invitee_name;
117         char *invite_str;
118 } mesh_invite_arg_t;
119
120 extern bool netns_create_topology(netns_state_t *state);
121 extern void netns_destroy_topology(netns_state_t *test_state);
122 extern void run_node_in_namespace_thread(netns_thread_t *netns_arg);
123 extern pid_t run_cmd_in_netns(netns_state_t *test_state, char *namespace_name, char *cmd_str);