aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authort3sserakt <t3ss@posteo.de>2021-09-13 18:25:21 +0200
committert3sserakt <t3ss@posteo.de>2021-09-13 18:25:21 +0200
commit72d77af5270ba1dabe6fa8c45009601b44d23b7b (patch)
tree3ea58e04fdaf79befff8925086b2dbf391586d64 /src/testing
parent46e07ed8eb64ff5a7473ac51de2e39c5a8a6ab3d (diff)
downloadgnunet-72d77af5270ba1dabe6fa8c45009601b44d23b7b.tar.gz
gnunet-72d77af5270ba1dabe6fa8c45009601b44d23b7b.zip
- added tng milestone 2 versions with improvements onto version 1 files , fixed smaller issues in milestone 1 versions, added version 1 to buildbot, added new testcase for testing udp backchannel
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/Makefile.am12
-rw-r--r--src/testing/gnunet-cmds-helper.c20
-rwxr-xr-xsrc/testing/netjail_core_v2.sh260
-rwxr-xr-xsrc/testing/netjail_exec_v2.sh14
-rwxr-xr-xsrc/testing/netjail_start_v2.sh61
-rwxr-xr-xsrc/testing/netjail_stop_v2.sh59
-rw-r--r--src/testing/test_testing_api_cmd_netjail.c10
-rw-r--r--src/testing/testing.c381
-rw-r--r--src/testing/testing_api_cmd_netjail_start_testsystem.c20
-rw-r--r--src/testing/testing_api_cmd_netjail_start_testsystem_v2.c718
-rw-r--r--src/testing/testing_api_cmd_netjail_start_v2.c229
-rw-r--r--src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c143
-rw-r--r--src/testing/testing_api_cmd_netjail_stop_v2.c225
-rwxr-xr-xsrc/testing/topo.sh95
14 files changed, 2217 insertions, 30 deletions
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am
index 038f0cb08..3e9d15986 100644
--- a/src/testing/Makefile.am
+++ b/src/testing/Makefile.am
@@ -45,9 +45,13 @@ libgnunettesting_la_SOURCES = \
45 testing_api_cmd_send_peer_ready.c \ 45 testing_api_cmd_send_peer_ready.c \
46 testing_api_cmd_block_until_all_peers_started.c \ 46 testing_api_cmd_block_until_all_peers_started.c \
47 testing_api_cmd_netjail_start.c \ 47 testing_api_cmd_netjail_start.c \
48 testing_api_cmd_netjail_start_v2.c \
48 testing_api_cmd_netjail_start_testsystem.c \ 49 testing_api_cmd_netjail_start_testsystem.c \
50 testing_api_cmd_netjail_start_testsystem_v2.c \
49 testing_api_cmd_netjail_stop_testsystem.c \ 51 testing_api_cmd_netjail_stop_testsystem.c \
52 testing_api_cmd_netjail_stop_testsystem_v2.c \
50 testing_api_cmd_netjail_stop.c \ 53 testing_api_cmd_netjail_stop.c \
54 testing_api_cmd_netjail_stop_v2.c \
51 testing.c testing.h \ 55 testing.c testing.h \
52 testing_api_cmd_system_create.c \ 56 testing_api_cmd_system_create.c \
53 testing_api_cmd_system_destroy.c \ 57 testing_api_cmd_system_destroy.c \
@@ -87,6 +91,7 @@ list_keys_LDADD = \
87 91
88 92
89check_PROGRAMS = \ 93check_PROGRAMS = \
94 test_testing_topology \
90 test_testing_api_cmd_netjail \ 95 test_testing_api_cmd_netjail \
91 test_testing_hello_world \ 96 test_testing_hello_world \
92 test_testing_portreservation \ 97 test_testing_portreservation \
@@ -98,6 +103,7 @@ check_PROGRAMS = \
98if ENABLE_TEST_RUN 103if ENABLE_TEST_RUN
99AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; 104AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
100TESTS = \ 105TESTS = \
106 test_testing_topology \
101 test_testing_api_cmd_netjail \ 107 test_testing_api_cmd_netjail \
102 test_testing_hello_world \ 108 test_testing_hello_world \
103 test_testing_portreservation \ 109 test_testing_portreservation \
@@ -106,6 +112,12 @@ TESTS = \
106 test_testing_servicestartup 112 test_testing_servicestartup
107endif 113endif
108 114
115test_testing_topology_SOURCES = \
116 test_testing_topology.c
117test_testing_topology_LDADD = \
118 libgnunettesting.la \
119 $(top_builddir)/src/util/libgnunetutil.la
120
109test_testing_api_cmd_netjail_SOURCES = \ 121test_testing_api_cmd_netjail_SOURCES = \
110 test_testing_api_cmd_netjail.c 122 test_testing_api_cmd_netjail.c
111test_testing_api_cmd_netjail_LDADD = \ 123test_testing_api_cmd_netjail_LDADD = \
diff --git a/src/testing/gnunet-cmds-helper.c b/src/testing/gnunet-cmds-helper.c
index 21ea33888..8f9e77709 100644
--- a/src/testing/gnunet-cmds-helper.c
+++ b/src/testing/gnunet-cmds-helper.c
@@ -163,14 +163,17 @@ struct WriteContext
163 size_t pos; 163 size_t pos;
164}; 164};
165 165
166struct Plugin *plugin;
167
168/** 166/**
169 * The process handle to the testbed service 167 * The process handle to the testbed service
170 168
171static struct GNUNET_OS_Process *cmd_binary_process;*/ 169static struct GNUNET_OS_Process *cmd_binary_process;*/
172 170
173/** 171/**
172 * Plugin to dynamically load a test case.
173 */
174struct Plugin *plugin;
175
176/**
174 * Handle to the testing system 177 * Handle to the testing system
175 */ 178 */
176static struct GNUNET_TESTING_System *test_system; 179static struct GNUNET_TESTING_System *test_system;
@@ -329,6 +332,7 @@ write_message (struct GNUNET_MessageHeader *message, size_t msg_length)
329static int 332static int
330tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message) 333tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message)
331{ 334{
335
332 struct NodeIdentifier *ni = cls; 336 struct NodeIdentifier *ni = cls;
333 const struct GNUNET_CMDS_HelperInit *msg; 337 const struct GNUNET_CMDS_HelperInit *msg;
334 struct GNUNET_CMDS_HelperReply *reply; 338 struct GNUNET_CMDS_HelperReply *reply;
@@ -369,14 +373,14 @@ tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message)
369 plugin->n = ni->n; 373 plugin->n = ni->n;
370 plugin->m = ni->m; 374 plugin->m = ni->m;
371 375
372 router_ip = GNUNET_malloc (strlen (ROUTER_BASE_IP) + strlen (plugin->m) 376 router_ip = GNUNET_malloc (strlen (ROUTER_BASE_IP) + strlen (plugin->n)
373 + 1); 377 + 1);
374 strcpy (router_ip, ROUTER_BASE_IP); 378 strcpy (router_ip, ROUTER_BASE_IP);
375 strcat (router_ip, plugin->m); 379 strcat (router_ip, plugin->n);
376 380
377 node_ip = GNUNET_malloc (strlen (NODE_BASE_IP) + strlen (plugin->n) + 1); 381 node_ip = GNUNET_malloc (strlen (NODE_BASE_IP) + strlen (plugin->m) + 1);
378 strcat (node_ip, NODE_BASE_IP); 382 strcat (node_ip, NODE_BASE_IP);
379 strcat (node_ip, plugin->n); 383 strcat (node_ip, plugin->m);
380 384
381 plugin->api->start_testcase (&write_message, router_ip, node_ip, plugin->m, 385 plugin->api->start_testcase (&write_message, router_ip, node_ip, plugin->m,
382 plugin->n, plugin->local_m); 386 plugin->n, plugin->local_m);
@@ -533,8 +537,8 @@ main (int argc, char **argv)
533 ni = GNUNET_new (struct NodeIdentifier); 537 ni = GNUNET_new (struct NodeIdentifier);
534 ni->global_n = argv[1]; 538 ni->global_n = argv[1];
535 ni->local_m = argv[2]; 539 ni->local_m = argv[2];
536 ni->n = argv[3]; 540 ni->m = argv[3];
537 ni->m = argv[4]; 541 ni->n = argv[4];
538 542
539 status = GNUNET_OK; 543 status = GNUNET_OK;
540 if (NULL == 544 if (NULL ==
diff --git a/src/testing/netjail_core_v2.sh b/src/testing/netjail_core_v2.sh
new file mode 100755
index 000000000..ef0a54a5e
--- /dev/null
+++ b/src/testing/netjail_core_v2.sh
@@ -0,0 +1,260 @@
1#!/bin/sh
2#
3
4
5PREFIX=${PPID:?must run from a parent process}
6
7# running with `sudo` is required to be
8# able running the actual commands as the
9# original user.
10
11export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
12
13export RESULT=
14export NAMESPACE_NUM=0
15export INTERFACE_NUM=0
16
17netjail_next_namespace() {
18 local NUM=$NAMESPACE_NUM
19 NAMESPACE_NUM=$(($NAMESPACE_NUM + 1))
20 RESULT=$NUM
21}
22
23netjail_next_interface() {
24 local NUM=$INTERFACE_NUM
25 INTERFACE_NUM=$(($INTERFACE_NUM + 1))
26 RESULT=$NUM
27}
28
29netjail_opt() {
30 local OPT=$1
31 shift 1
32
33 INDEX=1
34
35 while [ $# -gt 0 ]; do
36 if [ "$1" = "$OPT" ]; then
37 RESULT=$INDEX
38 return
39 fi
40
41 INDEX=$(($INDEX + 1))
42 shift 1
43 done
44
45 RESULT=0
46}
47
48netjail_opts() {
49 local OPT=$1
50 local DEF=$2
51 shift 2
52
53 while [ $# -gt 0 ]; do
54 if [ "$1" = "$OPT" ]; then
55 printf "$2"
56 return
57 fi
58
59 shift 1
60 done
61
62 RESULT="$DEF"
63}
64
65netjail_check() {
66 local NODE_COUNT=$1
67 local FD_COUNT=$(($(ls /proc/self/fd | wc -w) - 4))
68
69 # quit if `$FD_COUNT < ($LOCAL_M * $GLOBAL_N * 2)`:
70 # the script also requires `sudo -C ($FD_COUNT + 4)`
71 # so you need 'Defaults closefrom_override' in the
72 # sudoers file.
73
74 if [ $FD_COUNT -lt $(($NODE_COUNT * 2)) ]; then
75 echo "File descriptors do not match requirements!" >&2
76 exit 1
77 fi
78}
79
80netjail_check_bin() {
81 local PROGRAM=$1
82 local MATCH=$(ls $(echo $PATH | tr ":" "\n") | grep "^$PROGRAM\$" | tr "\n" " " | awk '{ print $1 }')
83
84 # quit if the required binary $PROGRAM can not be
85 # found in the used $PATH.
86
87 if [ "$MATCH" != "$PROGRAM" ]; then
88 echo "Required binary not found: $PROGRAM" >&2
89 exit 1
90 fi
91}
92
93netjail_bridge() {
94 netjail_next_interface
95 local NUM=$RESULT
96 local BRIDGE=$(printf "%06x-%08x" $PREFIX $NUM)
97
98 ip link add $BRIDGE type bridge
99 ip link set dev $BRIDGE up
100
101 RESULT=$BRIDGE
102}
103
104netjail_bridge_name() {
105 netjail_next_interface
106 local NUM=$RESULT
107 local BRIDGE=$(printf "%06x-%08x" $PREFIX $NUM)
108
109 RESULT=$BRIDGE
110}
111
112netjail_bridge_clear() {
113 local BRIDGE=$1
114
115 ip link delete $BRIDGE
116}
117
118netjail_node() {
119 netjail_next_namespace
120 local NUM=$RESULT
121 local NODE=$(printf "%06x-%08x" $PREFIX $NUM)
122
123 ip netns add $NODE
124
125 RESULT=$NODE
126}
127
128netjail_node_name() {
129 netjail_next_namespace
130 local NUM=$RESULT
131 local NODE=$(printf "%06x-%08x" $PREFIX $NUM)
132
133 RESULT=$NODE
134}
135
136netjail_node_clear() {
137 local NODE=$1
138
139 ip netns delete $NODE
140}
141
142netjail_node_link_bridge() {
143 local NODE=$1
144 local BRIDGE=$2
145 local ADDRESS=$3
146 local MASK=$4
147
148 netjail_next_interface
149 local NUM_IF=$RESULT
150 netjail_next_interface
151 local NUM_BR=$RESULT
152
153 local LINK_IF=$(printf "%06x-%08x" $PREFIX $NUM_IF)
154 local LINK_BR=$(printf "%06x-%08x" $PREFIX $NUM_BR)
155
156 ip link add $LINK_IF type veth peer name $LINK_BR
157 ip link set $LINK_IF netns $NODE
158 ip link set $LINK_BR master $BRIDGE
159
160 ip -n $NODE addr add "$ADDRESS/$MASK" dev $LINK_IF
161 ip -n $NODE link set $LINK_IF up
162 ip -n $NODE link set up dev lo
163
164 ip link set $LINK_BR up
165
166 RESULT=$LINK_BR
167}
168
169netjail_node_link_bridge_name() {
170
171 netjail_next_interface
172 netjail_next_interface
173 local NUM_BR=$RESULT
174
175 local LINK_BR=$(printf "%06x-%08x" $PREFIX $NUM_BR)
176
177 RESULT=$LINK_BR
178}
179
180netjail_node_unlink_bridge() {
181 local LINK_BR=$1
182
183 ip link delete $LINK_BR
184}
185
186netjail_node_add_nat() {
187 local NODE=$1
188 local ADDRESS=$2
189 local MASK=$3
190
191 ip netns exec $NODE iptables -t nat -A POSTROUTING -s "$ADDRESS/$MASK" -j MASQUERADE
192}
193
194netjail_node_add_default() {
195 local NODE=$1
196 local ADDRESS=$2
197
198 ip -n $NODE route add default via $ADDRESS
199}
200
201netjail_node_exec() {
202 JAILOR=${SUDO_USER:?must run in sudo}
203 local NODE=$1
204 local FD_IN=$2
205 local FD_OUT=$3
206 shift 3
207
208 ip netns exec $NODE sudo -u $JAILOR -- $@ 1>& $FD_OUT 0<& $FD_IN
209}
210
211netjail_node_exec_without_fds() {
212 JAILOR=${SUDO_USER:?must run in sudo}
213 NODE=$1
214 shift 1
215
216 ip netns exec $NODE sudo -u $JAILOR -- $@
217}
218
219netjail_node_exec_without_fds_and_sudo() {
220 NODE=$1
221 shift 1
222
223 ip netns exec $NODE $@
224}
225
226netjail_kill() {
227 local PID=$1
228 local MATCH=$(ps --pid $PID | awk "{ if ( \$1 == $PID ) { print \$1 } }" | wc -l)
229
230 if [ $MATCH -gt 0 ]; then
231 kill -n 19 $PID
232
233 for CHILD in $(ps -o pid,ppid -ax | awk "{ if ( \$2 == $PID ) { print \$1 } }"); do
234 netjail_kill $CHILD
235 done
236
237 kill $PID
238 fi
239}
240
241netjail_killall() {
242 if [ $# -gt 0 ]; then
243 local PIDS=$1
244
245 for PID in $PIDS; do
246 netjail_kill $PID
247 done
248 fi
249}
250
251netjail_waitall() {
252 if [ $# -gt 0 ]; then
253 local PIDS=$1
254
255 for PID in $PIDS; do
256 wait $PID
257 done
258 fi
259}
260
diff --git a/src/testing/netjail_exec_v2.sh b/src/testing/netjail_exec_v2.sh
new file mode 100755
index 000000000..597baad20
--- /dev/null
+++ b/src/testing/netjail_exec_v2.sh
@@ -0,0 +1,14 @@
1#!/bin/sh
2. "./../testing/netjail_core_v2.sh"
3
4set -eu
5set -x
6
7export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
8
9M=$1
10N=$2
11
12NODE=$6
13
14netjail_node_exec_without_fds_and_sudo $NODE $3 $4 $5 $1 $2
diff --git a/src/testing/netjail_start_v2.sh b/src/testing/netjail_start_v2.sh
new file mode 100755
index 000000000..cf1fe113a
--- /dev/null
+++ b/src/testing/netjail_start_v2.sh
@@ -0,0 +1,61 @@
1#!/bin/bash
2. "./../testing/netjail_core_v2.sh"
3. "./../testing/topo.sh"
4
5set -eu
6set -x
7
8export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
9
10filename=$1
11PREFIX=$2
12
13read_topology $filename
14
15shift 2
16
17LOCAL_GROUP="192.168.15"
18GLOBAL_GROUP="92.68.150"
19KNOWN_GROUP="92.68.151"
20
21
22echo "Start [local: $LOCAL_GROUP.0/24, global: $GLOBAL_GROUP.0/16]"
23
24netjail_bridge
25NETWORK_NET=$RESULT
26
27for X in $(seq $KNOWN); do
28 netjail_node
29 KNOWN_NODES[$X]=$RESULT
30 netjail_node_link_bridge ${KNOWN_NODES[$X]} $NETWORK_NET "$KNOWN_GROUP.$X" 16
31 KNOWN_LINKS[$X]=$RESULT
32done
33
34declare -A NODES
35declare -A NODE_LINKS
36
37for N in $(seq $GLOBAL_N); do
38 netjail_node
39 ROUTERS[$N]=$RESULT
40 netjail_node_link_bridge ${ROUTERS[$N]} $NETWORK_NET "$GLOBAL_GROUP.$N" 16
41 NETWORK_LINKS[$N]=$RESULT
42 netjail_bridge
43 ROUTER_NETS[$N]=$RESULT
44
45 for M in $(seq $LOCAL_M); do
46 netjail_node
47 NODES[$N,$M]=$RESULT
48 netjail_node_link_bridge ${NODES[$N,$M]} ${ROUTER_NETS[$N]} "$LOCAL_GROUP.$M" 24
49 NODE_LINKS[$N,$M]=$RESULT
50 done
51
52 ROUTER_ADDR="$LOCAL_GROUP.$(($LOCAL_M+1))"
53 netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24
54 ROUTER_LINKS[$N]=$RESULT
55
56 netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24
57
58 for M in $(seq $LOCAL_M); do
59 netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR
60 done
61done
diff --git a/src/testing/netjail_stop_v2.sh b/src/testing/netjail_stop_v2.sh
new file mode 100755
index 000000000..6e9fd1ccc
--- /dev/null
+++ b/src/testing/netjail_stop_v2.sh
@@ -0,0 +1,59 @@
1#!/bin/bash
2. "./../testing/netjail_core_v2.sh"
3. "./../testing/topo.sh"
4
5set -eu
6set -x
7
8export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
9
10filename=$1
11PREFIX=$2
12
13read_topology $filename
14
15declare -A NODES
16declare -A NODE_LINKS
17
18netjail_bridge_name
19NETWORK_NET=$RESULT
20
21for X in $(seq $KNOWN); do
22 netjail_node_name
23 KNOWN_NODES[$X]=$RESULT
24 netjail_node_link_bridge_name
25 KNOWN_LINKS[$X]=$RESULT
26 netjail_node_unlink_bridge ${KNOWN_LINKS[$X]}
27 netjail_node_clear ${KNOWN_NODES[$X]}
28done
29
30for N in $(seq $GLOBAL_N); do
31 netjail_node_name
32 ROUTERS[$N]=$RESULT
33 netjail_node_link_bridge_name
34 NETWORK_LINKS[$N]=$RESULT
35 netjail_bridge_name
36 ROUTER_NETS[$N]=$RESULT
37 netjail_node_link_bridge_name
38 ROUTER_LINKS[$N]=$RESULT
39
40 netjail_node_unlink_bridge ${ROUTER_LINKS[$N]}
41
42 for M in $(seq $LOCAL_M); do
43 netjail_node_name
44 NODES[$N,$M]=$RESULT
45 netjail_node_link_bridge_name
46 NODE_LINKS[$N,$M]=$RESULT
47 netjail_node_unlink_bridge ${NODE_LINKS[$N,$M]}
48 netjail_node_clear ${NODES[$N,$M]}
49 done
50
51
52 netjail_bridge_clear ${ROUTER_NETS[$N]}
53 netjail_node_unlink_bridge ${NETWORK_LINKS[$N]}
54 netjail_node_clear ${ROUTERS[$N]}
55done
56
57netjail_bridge_clear $NETWORK_NET
58
59echo "Done"
diff --git a/src/testing/test_testing_api_cmd_netjail.c b/src/testing/test_testing_api_cmd_netjail.c
index 543642109..aeddfb7c9 100644
--- a/src/testing/test_testing_api_cmd_netjail.c
+++ b/src/testing/test_testing_api_cmd_netjail.c
@@ -29,6 +29,13 @@
29 29
30 30
31/** 31/**
32 * Return value of the test.
33 *
34 */
35static unsigned int rv = 0;
36
37
38/**
32 * Main function to run the test cases. 39 * Main function to run the test cases.
33 * 40 *
34 * @param cls not used. 41 * @param cls not used.
@@ -44,7 +51,8 @@ run (void *cls)
44 GNUNET_TESTING_cmd_netjail_start_testing_system ("netjail-start-testbed-1", 51 GNUNET_TESTING_cmd_netjail_start_testing_system ("netjail-start-testbed-1",
45 "2", 52 "2",
46 "2", 53 "2",
47 "libgnunet_plugin_testcmd"), 54 "libgnunet_plugin_testcmd",
55 &rv),
48 GNUNET_TESTING_cmd_stop_testing_system ("stop-testbed", 56 GNUNET_TESTING_cmd_stop_testing_system ("stop-testbed",
49 "netjail-start-testbed-1", 57 "netjail-start-testbed-1",
50 "2", 58 "2",
diff --git a/src/testing/testing.c b/src/testing/testing.c
index 991c11a6c..fcde39901 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -33,6 +33,7 @@
33#include "gnunet_util_lib.h" 33#include "gnunet_util_lib.h"
34#include "gnunet_arm_service.h" 34#include "gnunet_arm_service.h"
35#include "gnunet_testing_lib.h" 35#include "gnunet_testing_lib.h"
36#include "gnunet_testing_ng_lib.h"
36 37
37#define LOG(kind, ...) GNUNET_log_from (kind, "testing-api", __VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from (kind, "testing-api", __VA_ARGS__)
38 39
@@ -1308,7 +1309,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
1308 peer->nports = nports; 1309 peer->nports = nports;
1309 return peer; 1310 return peer;
1310 1311
1311err_ret: 1312 err_ret:
1312 GNUNET_free (ss_instances); 1313 GNUNET_free (ss_instances);
1313 GNUNET_free (ports); 1314 GNUNET_free (ports);
1314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_); 1315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
@@ -1773,4 +1774,382 @@ GNUNET_TESTING_get_testname_from_underscore (const char *argv0)
1773} 1774}
1774 1775
1775 1776
1777static unsigned int
1778get_first_value (char *line)
1779{
1780 char *copy;
1781 size_t slen;
1782 char *token;
1783 unsigned int ret;
1784 char *rest = NULL;
1785
1786 slen = strlen (line) + 1;
1787 copy = malloc (slen);
1788 memcpy (copy, line, slen);
1789 token = strtok_r (copy, ":", &rest);
1790 token = strtok_r (NULL, ":", &rest);
1791 sscanf (token, "%u", &ret);
1792 free (copy);
1793 return ret;
1794}
1795
1796
1797static char *
1798get_key (char *line)
1799{
1800 char *copy;
1801 size_t slen;
1802 char *token;
1803 char *ret;
1804 char *rest = NULL;
1805
1806 slen = strlen (line) + 1;
1807 copy = malloc (slen);
1808 memcpy (copy, line, slen);
1809 token = strtok_r (copy, ":", &rest);
1810 ret = malloc (2);
1811 memcpy (ret, token, 2);
1812 free (copy);
1813 return ret;
1814}
1815
1816
1817static char *
1818get_first_string_value (char *line)
1819{
1820 char *copy;
1821 size_t slen, slen_token;
1822 char *token;
1823 char *ret;
1824 char *rest = NULL;
1825
1826 slen = strlen (line) + 1;
1827 copy = malloc (slen);
1828 memcpy (copy, line, slen);
1829 token = strtok_r (copy, ":", &rest);
1830 token = strtok_r (NULL, ":", &rest);
1831 LOG (GNUNET_ERROR_TYPE_DEBUG,
1832 "first token %s\n",
1833 token);
1834 slen_token = strlen (token);
1835 ret = malloc (slen_token + 1);
1836 memcpy (ret, token, slen_token + 1);
1837 free (copy);
1838 return ret;
1839}
1840
1841
1842static unsigned int
1843get_second_value (char *line)
1844{
1845 char *copy;
1846 size_t slen;
1847 char *token;
1848 unsigned int ret;
1849 char *rest = NULL;
1850
1851 slen = strlen (line) + 1;
1852 copy = malloc (slen);
1853 memcpy (copy, line, slen);
1854 token = strtok_r (copy, ":", &rest);
1855 token = strtok_r (NULL, ":", &rest);
1856 token = strtok_r (NULL, ":", &rest);
1857 sscanf (token, "%u", &ret);
1858 free (copy);
1859 return ret;
1860}
1861
1862
1863static char *
1864get_value (char *key, char *line)
1865{
1866 char *copy;
1867 size_t slen, slen_token;
1868 char *token;
1869 char *token2;
1870 char *temp;
1871 char *rest = NULL;
1872 char *ret;
1873
1874 slen = strlen (line) + 1;
1875 copy = malloc (slen);
1876 memcpy (copy, line, slen);
1877 temp = strstr (copy, key);
1878 if (NULL == temp)
1879 return NULL;
1880 token = strtok_r (temp, ":", &rest);
1881 token = strtok_r (NULL, ":", &rest);
1882 token2 = strtok_r (token, "}", &rest);
1883 slen_token = strlen (token2);
1884 ret = malloc (slen_token + 1);
1885 memcpy (ret, token2, slen_token + 1);
1886 free (copy);
1887 return ret;
1888}
1889
1890
1891/**
1892 * Getting the topology from file.
1893 *
1894 * @param filename The name of the topology file.
1895 * @return The GNUNET_TESTING_NetjailTopology
1896 */
1897struct GNUNET_TESTING_NetjailTopology *
1898GNUNET_TESTING_get_topo_from_file (const char *filename)
1899{
1900 uint64_t fs;
1901 char *data;
1902 char *token;
1903 char *key;
1904 unsigned int out;
1905 char *rest = NULL;
1906 char *value;
1907 int ret;
1908 struct GNUNET_TESTING_NetjailTopology *topo = GNUNET_new (struct
1909 GNUNET_TESTING_NetjailTopology);
1910 struct GNUNET_TESTING_NetjailNode *node;
1911 struct GNUNET_TESTING_NetjailRouter *router;
1912 struct GNUNET_TESTING_NetjailNamespace *namespace;
1913 struct GNUNET_ShortHashCode *hkey;
1914 struct GNUNET_HashCode hc;
1915 topo->map_namespaces =
1916 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
1917 topo->map_globals =
1918 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
1919
1920 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1921 {
1922 LOG (GNUNET_ERROR_TYPE_ERROR,
1923 _ ("Topology file %s not found\n"),
1924 filename);
1925 return NULL;
1926 }
1927 if (GNUNET_OK !=
1928 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
1929 {
1930 LOG (GNUNET_ERROR_TYPE_ERROR,
1931 _ ("Topology file %s has no data\n"),
1932 filename);
1933 return NULL;
1934 }
1935 data = GNUNET_malloc (fs);
1936 if (fs != GNUNET_DISK_fn_read (filename, data, fs))
1937 {
1938 LOG (GNUNET_ERROR_TYPE_ERROR,
1939 _ ("Topology file %s cannot be read\n"),
1940 filename);
1941 GNUNET_free (data);
1942 return NULL;
1943 }
1944
1945 LOG (GNUNET_ERROR_TYPE_DEBUG,
1946 "data: %s\n",
1947 data);
1948
1949 token = strtok_r (data, "\n", &rest);
1950
1951 while (NULL != token)
1952 {
1953 key = get_key (token);
1954 LOG (GNUNET_ERROR_TYPE_DEBUG,
1955 "In the loop with token: %s beginning with %s\n",
1956 token,
1957 key);
1958 if (0 == strcmp (key, "M"))
1959 {
1960 LOG (GNUNET_ERROR_TYPE_DEBUG,
1961 "Get first Value for M.\n");
1962 out = get_first_value (token);
1963 LOG (GNUNET_ERROR_TYPE_DEBUG,
1964 "M: %u\n",
1965 out);
1966 topo->nodes_m = out;
1967 }
1968 else if (0 == strcmp (key, "N"))
1969 {
1970 LOG (GNUNET_ERROR_TYPE_DEBUG,
1971 "Get first Value for N.\n");
1972 out = get_first_value (token);
1973 LOG (GNUNET_ERROR_TYPE_DEBUG,
1974 "N: %u\n",
1975 out);
1976 topo->namespaces_n = out;
1977 }
1978 else if (0 == strcmp (key, "X"))
1979 {
1980 LOG (GNUNET_ERROR_TYPE_DEBUG,
1981 "Get first Value for X.\n");
1982 out = get_first_value (token);
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 "X: %u\n",
1985 out);
1986 topo->nodes_x = out;
1987 }
1988 else if (0 == strcmp (key, "T"))
1989 {
1990 LOG (GNUNET_ERROR_TYPE_DEBUG,
1991 "Get first string value for T.\n");
1992 value = get_first_string_value (token);
1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "value: %s\n",
1995 value);
1996 topo->plugin = value;
1997 }
1998 else if (0 == strcmp (key, "K"))
1999 {
2000 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
2001 node = GNUNET_new (struct GNUNET_TESTING_NetjailNode);
2002
2003 LOG (GNUNET_ERROR_TYPE_DEBUG,
2004 "Get first Value for K.\n");
2005 out = get_first_value (token);
2006 LOG (GNUNET_ERROR_TYPE_DEBUG,
2007 "K: %u\n",
2008 out);
2009 node->node_n = out;
2010 GNUNET_CRYPTO_hash (&out, sizeof(out), &hc);
2011 memcpy (hkey,
2012 &hc,
2013 sizeof (*hkey));
2014 node->is_global = GNUNET_YES;
2015 LOG (GNUNET_ERROR_TYPE_DEBUG,
2016 "Get value for key value on K.\n");
2017 value = get_value ("plugin", token);
2018 LOG (GNUNET_ERROR_TYPE_DEBUG,
2019 "value: %s\n",
2020 value);
2021 if (0 == GNUNET_CONTAINER_multishortmap_contains (topo->map_globals,
2022 hkey))
2023 GNUNET_break (0);
2024 else
2025 GNUNET_CONTAINER_multishortmap_put (topo->map_globals,
2026 hkey,
2027 node,
2028 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2029 }
2030 else if (0 == strcmp (key, "R"))
2031 {
2032 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
2033 router = GNUNET_new (struct GNUNET_TESTING_NetjailRouter);
2034 node = GNUNET_new (struct GNUNET_TESTING_NetjailNode);
2035
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "Get first Value for R.\n");
2038 out = get_first_value (token);
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "R: %u\n",
2041 out);
2042 node->node_n = out;
2043 GNUNET_CRYPTO_hash (&out, sizeof(out), &hc);
2044 memcpy (hkey,
2045 &hc,
2046 sizeof (*hkey));
2047 LOG (GNUNET_ERROR_TYPE_DEBUG,
2048 "Get value for key tcp_port on R.\n");
2049 value = get_value ("tcp_port", token);
2050 LOG (GNUNET_ERROR_TYPE_DEBUG,
2051 "tcp_port: %s\n",
2052 value);
2053 ret = sscanf (value, "%u", &(router->tcp_port));
2054
2055 GNUNET_break (0 == ret || 1 < router->tcp_port);
2056
2057 LOG (GNUNET_ERROR_TYPE_DEBUG,
2058 "Get value for key udp_port on R.\n");
2059 value = get_value ("udp_port", token);
2060 ret = sscanf (value, "%u", &(router->udp_port));
2061 GNUNET_break (0 == ret || 1 < router->udp_port);
2062 LOG (GNUNET_ERROR_TYPE_DEBUG,
2063 "udp_port: %s\n",
2064 value);
2065
2066 if (0 == GNUNET_CONTAINER_multishortmap_contains (topo->map_namespaces,
2067 hkey))
2068 {
2069 namespace = GNUNET_CONTAINER_multishortmap_get (topo->map_namespaces,
2070 hkey);
2071 }
2072 else
2073 {
2074 namespace = GNUNET_new (struct GNUNET_TESTING_NetjailNamespace);
2075 namespace->namespace_n = out;
2076 GNUNET_CONTAINER_multishortmap_put (topo->map_namespaces,
2077 hkey,
2078 namespace,
2079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2080 }
2081 namespace->router = router;
2082
2083 }
2084 else if (0 == strcmp (key, "P"))
2085 {
2086 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
2087 node = GNUNET_new (struct GNUNET_TESTING_NetjailNode);
2088
2089 LOG (GNUNET_ERROR_TYPE_DEBUG,
2090 "Get first Value for P.\n");
2091 out = get_first_value (token);
2092 LOG (GNUNET_ERROR_TYPE_DEBUG,
2093 "P: %u\n",
2094 out);
2095 GNUNET_CRYPTO_hash (&out, sizeof(out), &hc);
2096 memcpy (hkey,
2097 &hc,
2098 sizeof (*hkey));
2099
2100 LOG (GNUNET_ERROR_TYPE_DEBUG,
2101 "Get value for key plugin on P.\n");
2102 value = get_value ("plugin", token);
2103 LOG (GNUNET_ERROR_TYPE_DEBUG,
2104 "plugin: %s\n",
2105 value);
2106 if (0 == GNUNET_CONTAINER_multishortmap_contains (topo->map_namespaces,
2107 hkey))
2108 {
2109 namespace = GNUNET_CONTAINER_multishortmap_get (topo->map_namespaces,
2110 hkey);
2111 }
2112 else
2113 {
2114 namespace = GNUNET_new (struct GNUNET_TESTING_NetjailNamespace);
2115 namespace->namespace_n = out;
2116 GNUNET_CONTAINER_multishortmap_put (topo->map_namespaces,
2117 hkey,
2118 namespace,
2119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2120 }
2121 LOG (GNUNET_ERROR_TYPE_DEBUG,
2122 "Get second Value for P.\n");
2123 out = get_second_value (token);
2124 LOG (GNUNET_ERROR_TYPE_DEBUG,
2125 "P: %u\n",
2126 out);
2127 GNUNET_CRYPTO_hash (&out, sizeof(out), &hc);
2128 memcpy (hkey,
2129 &hc,
2130 sizeof (*hkey));
2131 if (0 == GNUNET_CONTAINER_multishortmap_contains (namespace->nodes,
2132 hkey))
2133 {
2134 GNUNET_break (0);
2135 }
2136 else
2137 {
2138 node = GNUNET_new (struct GNUNET_TESTING_NetjailNode);
2139 GNUNET_CONTAINER_multishortmap_put (namespace->nodes,
2140 hkey,
2141 node,
2142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2143 node->plugin = value;
2144 node->node_n = out;
2145 node->namespace_n = namespace->namespace_n;
2146 }
2147 }
2148 token = strtok_r (NULL, "\n", &rest);
2149 }
2150
2151 return topo;
2152}
2153
2154
1776/* end of testing.c */ 2155/* end of testing.c */
diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem.c b/src/testing/testing_api_cmd_netjail_start_testsystem.c
index 0fe0541b0..531621eb5 100644
--- a/src/testing/testing_api_cmd_netjail_start_testsystem.c
+++ b/src/testing/testing_api_cmd_netjail_start_testsystem.c
@@ -278,26 +278,6 @@ GNUNET_TESTING_get_trait_helper_handles (const struct
278 278
279 279
280/** 280/**
281 * Offer messages received via testing cmd helper from trait
282 *
283 * @param cmd command to extract the message from.
284 * @param pt pointer to message.
285 * @return #GNUNET_OK on success.
286 */
287int
288GNUNET_TESTING_get_trait_helper_messages (const struct
289 GNUNET_TESTING_Command *cmd,
290 struct HelperMessage ***
291 hp_messages_head)
292{
293 return cmd->traits (cmd->cls,
294 (const void **) hp_messages_head,
295 "hp_msgs_head",
296 (unsigned int) 1);
297}
298
299
300/**
301 * Continuation function from GNUNET_HELPER_send() 281 * Continuation function from GNUNET_HELPER_send()
302 * 282 *
303 * @param cls closure 283 * @param cls closure
diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c b/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c
new file mode 100644
index 000000000..64ce549a4
--- /dev/null
+++ b/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c
@@ -0,0 +1,718 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testing/testing_api_cmd_hello_world.c
23 * @brief Command to start the netjail peers.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_ng_lib.h"
28#include "testing_cmds.h"
29
30#define NETJAIL_EXEC_SCRIPT "./../testing/netjail_exec_v2.sh"
31
32/**
33 * Struct to store messages send/received by the helper into a DLL
34 *
35 */
36struct HelperMessage
37{
38
39 /**
40 * Kept in a DLL.
41 */
42 struct HelperMessage *next;
43
44 /**
45 * Kept in a DLL.
46 */
47 struct HelperMessage *prev;
48
49 /**
50 * Size of the original message.
51 */
52 uint16_t bytes_msg;
53
54 /* Followed by @e bytes_msg of msg.*/
55};
56
57
58/**
59 * Struct to store information handed over to callbacks.
60 *
61 */
62struct NetJailState
63{
64 /**
65 * The complete topology infomation.
66 */
67 struct GNUNET_TESTING_NetjailTopology *topology;
68
69 /**
70 * Pointer to the return value of the test.
71 *
72 */
73 unsigned int *rv;
74
75 /**
76 * Head of the DLL which stores messages received by the helper.
77 *
78 */
79 struct HelperMessage *hp_messages_head;
80
81 /**
82 * Tail of the DLL which stores messages received by the helper.
83 *
84 */
85 struct HelperMessage *hp_messages_tail;
86
87 /**
88 * Array with handles of helper processes.
89 */
90 struct GNUNET_HELPER_Handle **helper;
91
92 /**
93 * Size of the array NetJailState#helper.
94 *
95 */
96 unsigned int n_helper;
97
98 /**
99 * Number of nodes in a natted subnet.
100 *
101 */
102 unsigned int local_m;
103
104 /**
105 * Number of natted subnets.
106 *
107 */
108 unsigned int global_n;
109
110 /**
111 * Number of global known nodes.
112 *
113 */
114 unsigned int known;
115
116 /**
117 * The send handle for the helper
118 */
119 struct GNUNET_HELPER_SendHandle **shandle;
120
121 /**
122 * Size of the array NetJailState#shandle.
123 *
124 */
125 unsigned int n_shandle;
126
127 /**
128 * The messages send to the helper.
129 */
130 struct GNUNET_MessageHeader **msg;
131
132 /**
133 * Size of the array NetJailState#msg.
134 *
135 */
136 unsigned int n_msg;
137
138 /**
139 * Number of test environments started.
140 *
141 */
142 unsigned int number_of_testsystems_started;
143
144 /**
145 * Number of peers started.
146 *
147 */
148 unsigned int number_of_peers_started;
149
150 /**
151 * Number of local tests finished.
152 *
153 */
154 unsigned int number_of_local_test_finished;
155
156 /**
157 * Name of the test case plugin the helper will load.
158 *
159 */
160 char *plugin_name;
161
162 /**
163 * HEAD of the DLL containing TestingSystemCount.
164 *
165 */
166 struct TestingSystemCount *tbcs_head;
167
168 /**
169 * TAIL of the DLL containing TestingSystemCount.
170 *
171 */
172 struct TestingSystemCount *tbcs_tail;
173};
174
175/**
176 * Struct containing the number of the test environment and the NetJailState which
177 * will be handed to callbacks specific to a test environment.
178 */
179struct TestingSystemCount
180{
181 /**
182 * Kept in a DLL.
183 */
184 struct TestingSystemCount *next;
185
186 /**
187 * Kept in a DLL.
188 */
189 struct TestingSystemCount *prev;
190
191 /**
192 * The number of the test environment.
193 *
194 */
195 unsigned int count;
196
197 /**
198 * Struct to store information handed over to callbacks.
199 *
200 */
201 struct NetJailState *ns;
202};
203
204/**
205* Code to clean up resource this cmd used.
206*
207* @param cls closure
208* @param cmd current CMD being cleaned up.
209*/
210static void
211netjail_exec_cleanup (void *cls,
212 const struct GNUNET_TESTING_Command *cmd)
213{
214 struct NetJailState *ns = cls;
215 struct HelperMessage *message_pos;
216 struct TestingSystemCount *tbc_pos;
217
218 while (NULL != (message_pos = ns->hp_messages_head))
219 {
220 GNUNET_CONTAINER_DLL_remove (ns->hp_messages_head,
221 ns->hp_messages_tail,
222 message_pos);
223 GNUNET_free (message_pos);
224 }
225 while (NULL != (tbc_pos = ns->tbcs_head))
226 {
227 GNUNET_CONTAINER_DLL_remove (ns->tbcs_head,
228 ns->tbcs_tail,
229 tbc_pos);
230 GNUNET_free (tbc_pos);
231 }
232 GNUNET_free (ns);
233}
234
235
236/**
237 * This function prepares an array with traits.
238 *
239 */
240static int
241netjail_exec_traits (void *cls,
242 const void **ret,
243 const char *trait,
244 unsigned int index)
245{
246 struct NetJailState *ns = cls;
247 struct GNUNET_HELPER_Handle **helper = ns->helper;
248 struct HelperMessage *hp_messages_head = ns->hp_messages_head;
249
250
251 struct GNUNET_TESTING_Trait traits[] = {
252 {
253 .index = 0,
254 .trait_name = "helper_handles",
255 .ptr = (const void *) helper,
256 },
257 {
258 .index = 1,
259 .trait_name = "hp_msgs_head",
260 .ptr = (const void *) hp_messages_head,
261 },
262 GNUNET_TESTING_trait_end ()
263 };
264
265 return GNUNET_TESTING_get_trait (traits,
266 ret,
267 trait,
268 index);
269}
270
271
272/**
273 * Offer handles to testing cmd helper from trait
274 *
275 * @param cmd command to extract the message from.
276 * @param pt pointer to message.
277 * @return #GNUNET_OK on success.
278 */
279int
280GNUNET_TESTING_get_trait_helper_handles_v2 (const struct
281 GNUNET_TESTING_Command *cmd,
282 struct GNUNET_HELPER_Handle ***
283 helper)
284{
285 return cmd->traits (cmd->cls,
286 (const void **) helper,
287 "helper_handles",
288 (unsigned int) 0);
289}
290
291
292/**
293 * Continuation function from GNUNET_HELPER_send()
294 *
295 * @param cls closure
296 * @param result GNUNET_OK on success,
297 * GNUNET_NO if helper process died
298 * GNUNET_SYSERR during GNUNET_HELPER_stop
299 */
300static void
301clear_msg (void *cls, int result)
302{
303 struct TestingSystemCount *tbc = cls;
304 struct NetJailState *ns = tbc->ns;
305
306 GNUNET_assert (NULL != ns->shandle[tbc->count - 1]);
307 ns->shandle[tbc->count - 1] = NULL;
308 GNUNET_free (ns->msg[tbc->count - 1]);
309 ns->msg[tbc->count - 1] = NULL;
310}
311
312
313/**
314 * Functions with this signature are called whenever a
315 * complete message is received by the tokenizer.
316 *
317 * Do not call GNUNET_SERVER_mst_destroy in callback
318 *
319 * @param cls closure
320 * @param client identification of the client
321 * @param message the actual message
322 *
323 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
324 */
325static int
326helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
327{
328 struct TestingSystemCount *tbc = cls;
329 struct NetJailState *ns = tbc->ns;
330 struct HelperMessage *hp_msg;
331
332 if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type))
333 {
334 ns->number_of_testsystems_started++;
335 }
336 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_PEER_STARTED == ntohs (
337 message->type))
338 {
339 ns->number_of_peers_started++;
340 }
341 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs (
342 message->type))
343 {
344 ns->number_of_local_test_finished++;
345 }
346 else
347 {
348 hp_msg = GNUNET_new (struct HelperMessage);
349 hp_msg->bytes_msg = message->size;
350 memcpy (&hp_msg[1], message, message->size);
351 GNUNET_CONTAINER_DLL_insert (ns->hp_messages_head, ns->hp_messages_tail,
352 hp_msg);
353 }
354
355 return GNUNET_OK;
356}
357
358
359/**
360 * Callback called if there was an exception during execution of the helper.
361 *
362 */
363static void
364exp_cb (void *cls)
365{
366 struct NetJailState *ns = cls;
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n");
368 *ns->rv = 1;
369}
370
371
372/**
373 * Function to initialize a init message for the helper.
374 *
375 * @param m_char The actual node in a namespace. //TODO Change this to unsigned int
376 * @param n_char The actual namespace. //TODO Change this to unsigned int
377 * @param plugin_name Name of the test case plugin the helper will load.
378 *
379 */
380static struct GNUNET_CMDS_HelperInit *
381create_helper_init_msg_ (const char *plugin_name)
382{
383 struct GNUNET_CMDS_HelperInit *msg;
384 uint16_t plugin_name_len;
385 uint16_t msg_size;
386
387 GNUNET_assert (NULL != plugin_name);
388 plugin_name_len = strlen (plugin_name);
389 msg_size = sizeof(struct GNUNET_CMDS_HelperInit) + plugin_name_len;
390 msg = GNUNET_malloc (msg_size);
391 msg->header.size = htons (msg_size);
392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT);
393 msg->plugin_name_size = htons (plugin_name_len);
394 GNUNET_memcpy ((char *) &msg[1],
395 plugin_name,
396 plugin_name_len);
397 return msg;
398}
399
400
401/**
402 * Function which start a single helper process.
403 *
404 */
405static void
406start_helper (struct NetJailState *ns, struct
407 GNUNET_CONFIGURATION_Handle *config,
408 unsigned int m,
409 unsigned int n)
410{
411 struct GNUNET_HELPER_Handle *helper;
412 struct GNUNET_CMDS_HelperInit *msg;
413 struct TestingSystemCount *tbc;
414 char *m_char, *n_char, *global_n_char, *local_m_char, *known_char, *node_id,
415 *plugin;
416 pid_t pid;
417 unsigned int script_num;
418 struct GNUNET_ShortHashCode *hkey;
419 struct GNUNET_HashCode hc;
420 struct GNUNET_TESTING_NetjailTopology *topology = ns->topology;
421 struct GNUNET_TESTING_NetjailNode *node;
422 struct GNUNET_TESTING_NetjailNamespace *namespace;
423
424
425 if (0 == m)
426 script_num = n - 1;
427 else
428 script_num = n - 1 + (n - 1) * ns->local_m + m + ns->known;
429 pid = getpid ();
430
431 GNUNET_asprintf (&m_char, "%u", m);
432 GNUNET_asprintf (&n_char, "%u", n);
433 GNUNET_asprintf (&local_m_char, "%u", ns->local_m);
434 GNUNET_asprintf (&global_n_char, "%u",ns->global_n);
435 GNUNET_asprintf (&known_char, "%u",ns->known);
436 GNUNET_asprintf (&node_id, "%06x-%08x\n",
437 pid,
438 script_num);
439
440
441 char *const script_argv[] = {NETJAIL_EXEC_SCRIPT,
442 m_char,
443 n_char,
444 GNUNET_OS_get_libexec_binary_path (
445 HELPER_CMDS_BINARY),
446 global_n_char,
447 local_m_char,
448 node_id,
449 NULL};
450
451 unsigned int helper_check = GNUNET_OS_check_helper_binary (
452 NETJAIL_EXEC_SCRIPT,
453 GNUNET_YES,
454 NULL);
455
456 tbc = GNUNET_new (struct TestingSystemCount);
457 tbc->ns = ns;
458 if (0 == m)
459 tbc->count = n;
460 else
461 tbc->count = (n - 1) * ns->local_m + m + ns->known;
462
463 GNUNET_CONTAINER_DLL_insert (ns->tbcs_head, ns->tbcs_tail,
464 tbc);
465
466
467 if (GNUNET_NO == helper_check)
468 {
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 "No SUID for %s!\n",
471 NETJAIL_EXEC_SCRIPT);
472 *ns->rv = 1;
473 }
474 else if (GNUNET_NO == helper_check)
475 {
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 "%s not found!\n",
478 NETJAIL_EXEC_SCRIPT);
479 *ns->rv = 1;
480 }
481
482 GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start (
483 GNUNET_YES,
484 NETJAIL_EXEC_SCRIPT,
485 script_argv,
486 &helper_mst,
487 &exp_cb,
488 tbc));
489
490 helper = ns->helper[tbc->count - 1];
491
492 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
493
494 plugin = ns->plugin_name;
495
496 if (0 == m)
497 {
498
499 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
500 memcpy (hkey,
501 &hc,
502 sizeof (*hkey));
503 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_globals,
504 hkey))
505 {
506 node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals,
507 hkey);
508 plugin = node->plugin;
509 }
510
511 }
512 else
513 {
514 GNUNET_CRYPTO_hash (&m, sizeof(m), &hc);
515 memcpy (hkey,
516 &hc,
517 sizeof (*hkey));
518 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_namespaces,
519 hkey))
520 {
521 namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces,
522 hkey);
523 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
524 memcpy (hkey,
525 &hc,
526 sizeof (*hkey));
527 if (1 == GNUNET_CONTAINER_multishortmap_contains (namespace->nodes,
528 hkey))
529 {
530 node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes,
531 hkey);
532 plugin = node->plugin;
533 }
534 }
535
536
537 }
538
539 msg = create_helper_init_msg_ (plugin);
540
541 GNUNET_array_append (ns->msg, ns->n_msg, &msg->header);
542
543 GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send (
544 helper,
545 &msg->header,
546 GNUNET_NO,
547 &clear_msg,
548 tbc));
549
550 if (NULL == ns->shandle[tbc->count - 1])
551 {
552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
553 "Send handle is NULL!\n");
554 GNUNET_free (msg);
555 *ns->rv = 1;
556 }
557}
558
559
560/**
561* This function starts a helper process for each node.
562*
563* @param cls closure.
564* @param cmd CMD being run.
565* @param is interpreter state.
566*/
567static void
568netjail_exec_run (void *cls,
569 const struct GNUNET_TESTING_Command *cmd,
570 struct GNUNET_TESTING_Interpreter *is)
571{
572 struct NetJailState *ns = cls;
573 struct GNUNET_CONFIGURATION_Handle *config =
574 GNUNET_CONFIGURATION_create ();
575
576 for (int i = 1; i <= ns->known; i++)
577 {
578 start_helper (ns, config,
579 i,
580 0);
581 }
582
583 for (int i = 1; i <= ns->global_n; i++)
584 {
585 for (int j = 1; j <= ns->local_m; j++)
586 {
587 start_helper (ns, config,
588 j,
589 i);
590 }
591 }
592}
593
594
595static void
596send_all_peers_started (unsigned int i, unsigned int j, struct NetJailState *ns)
597{
598 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
599 struct GNUNET_CMDS_ALL_PEERS_STARTED *reply;
600 size_t msg_length;
601 struct GNUNET_HELPER_Handle *helper;
602 struct TestingSystemCount *tbc;
603
604 tbc = GNUNET_new (struct TestingSystemCount);
605 tbc->ns = ns;
606 // TODO This needs to be more generic. As we send more messages back and forth, we can not grow the arrays again and again, because this is to error prone.
607 if (0 == i)
608 tbc->count = j + total_number;
609 else
610 tbc->count = (i - 1) * ns->local_m + j + total_number + ns->known;
611
612 helper = ns->helper[tbc->count - 1 - total_number];
613 msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED);
614 reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED);
615 reply->header.type = htons (
616 GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED);
617 reply->header.size = htons ((uint16_t) msg_length);
618
619 GNUNET_array_append (ns->msg, ns->n_msg, &reply->header);
620
621 struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send (
622 helper,
623 &reply->header,
624 GNUNET_NO,
625 &clear_msg,
626 tbc);
627
628 GNUNET_array_append (ns->shandle, ns->n_shandle, sh);
629}
630
631
632/**
633 * This function checks on three different information.
634 *
635 * 1. Did all helpers start. This is only logged.
636 * 2. Did all peer start.
637 * In this case a GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED is send to all peers.
638 * 3. Did all peers finished the test case. In this case interpreter_next will be called.
639 *
640 */
641static int
642netjail_start_finish (void *cls,
643 GNUNET_SCHEDULER_TaskCallback cont,
644 void *cont_cls)
645{
646 unsigned int ret = GNUNET_NO;
647 struct NetJailState *ns = cls;
648 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
649
650
651 if (ns->number_of_local_test_finished == total_number)
652 {
653 ret = GNUNET_YES;
654 cont (cont_cls);
655 }
656
657 if (ns->number_of_testsystems_started == total_number)
658 {
659 ns->number_of_testsystems_started = 0;
660 }
661
662 if (ns->number_of_peers_started == total_number)
663 {
664 for (int i = 1; i <= ns->known; i++)
665 {
666 send_all_peers_started (0,i, ns);
667 }
668
669 for (int i = 1; i <= ns->global_n; i++)
670 {
671 for (int j = 1; j <= ns->local_m; j++)
672 {
673 send_all_peers_started (i,j, ns);
674 }
675 }
676 ns->number_of_peers_started = 0;
677 }
678 return ret;
679}
680
681
682/**
683 * Create command.
684 *
685 * @param label Name for the command.
686 * @param topology_config Configuration file for the test topology.
687 * @param rv Pointer to the return value of the test.
688 * @return command.
689 */
690struct GNUNET_TESTING_Command
691GNUNET_TESTING_cmd_netjail_start_testing_system_v2 (const char *label,
692 const char *topology_config,
693 unsigned int *rv)
694{
695 struct NetJailState *ns;
696
697 struct GNUNET_TESTING_NetjailTopology *topology =
698 GNUNET_TESTING_get_topo_from_file (topology_config);
699
700 ns = GNUNET_new (struct NetJailState);
701 ns->rv = rv;
702 ns->local_m = topology->nodes_m;
703 ns->global_n = topology->namespaces_n;
704 ns->known = topology->nodes_x;
705 ns->plugin_name = topology->plugin;
706 ns->topology = topology;
707
708 struct GNUNET_TESTING_Command cmd = {
709 .cls = ns,
710 .label = label,
711 .run = &netjail_exec_run,
712 .finish = &netjail_start_finish,
713 .cleanup = &netjail_exec_cleanup,
714 .traits = &netjail_exec_traits
715 };
716
717 return cmd;
718}
diff --git a/src/testing/testing_api_cmd_netjail_start_v2.c b/src/testing/testing_api_cmd_netjail_start_v2.c
new file mode 100644
index 000000000..36fbb0e10
--- /dev/null
+++ b/src/testing/testing_api_cmd_netjail_start_v2.c
@@ -0,0 +1,229 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testing/testing_api_cmd_hello_world.c
23 * @brief Command to start the netjail script.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29
30#define NETJAIL_START_SCRIPT "./../testing/netjail_start_v2.sh"
31
32/**
33 * Struct to hold information for callbacks.
34 *
35 */
36struct NetJailState
37{
38 // Child Wait handle
39 struct GNUNET_ChildWaitHandle *cwh;
40
41 /**
42 * Configuration file for the test topology.
43 */
44 char *topology_config;
45
46 /**
47 * The process id of the start script.
48 */
49 struct GNUNET_OS_Process *start_proc;
50
51 // Flag indication if the script finished.
52 unsigned int finished;
53};
54
55
56/**
57 * The cleanup function of this cmd frees resources the cmd allocated.
58 *
59 */
60static void
61netjail_start_cleanup (void *cls,
62 const struct GNUNET_TESTING_Command *cmd)
63{
64 struct NetJailState *ns = cls;
65
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "netjail_start_cleanup!\n");
68
69 if (NULL != ns->cwh)
70 {
71 GNUNET_wait_child_cancel (ns->cwh);
72 ns->cwh = NULL;
73 }
74 if (NULL != ns->start_proc)
75 {
76 GNUNET_assert (0 ==
77 GNUNET_OS_process_kill (ns->start_proc,
78 SIGKILL));
79 GNUNET_assert (GNUNET_OK ==
80 GNUNET_OS_process_wait (ns->start_proc));
81 GNUNET_OS_process_destroy (ns->start_proc);
82 ns->start_proc = NULL;
83 }
84 GNUNET_free (ns);
85}
86
87
88/**
89 * Trait function of this cmd does nothing.
90 *
91 */
92static int
93netjail_start_traits (void *cls,
94 const void **ret,
95 const char *trait,
96 unsigned int index)
97{
98 return GNUNET_OK;
99}
100
101
102/**
103 * Callback which will be called if the setup script finished.
104 *
105 */
106static void
107child_completed_callback (void *cls,
108 enum GNUNET_OS_ProcessStatusType type,
109 long unsigned int exit_code)
110{
111 struct NetJailState *ns = cls;
112
113 if (0 == exit_code)
114 {
115 ns->finished = GNUNET_YES;
116 }
117 else
118 {
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 "Child completed with an error!\n");
121 ns->finished = GNUNET_SYSERR;
122 }
123 GNUNET_OS_process_destroy (ns->start_proc);
124 ns->start_proc = NULL;
125}
126
127
128
129/**
130* The run method starts the script which setup the network namespaces.
131*
132* @param cls closure.
133* @param cmd CMD being run.
134* @param is interpreter state.
135*/
136static void
137netjail_start_run (void *cls,
138 const struct GNUNET_TESTING_Command *cmd,
139 struct GNUNET_TESTING_Interpreter *is)
140{
141 struct NetJailState *ns = cls;
142 char *pid;
143 GNUNET_asprintf (&pid,
144 "%u",
145 getpid ());
146 char *const script_argv[] = {NETJAIL_START_SCRIPT,
147 ns->topology_config,
148 pid,
149 NULL};
150 unsigned int helper_check = GNUNET_OS_check_helper_binary (
151 NETJAIL_START_SCRIPT,
152 GNUNET_YES,
153 NULL);
154
155 if (GNUNET_NO == helper_check)
156 {
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158 "No SUID for %s!\n",
159 NETJAIL_START_SCRIPT);
160 GNUNET_TESTING_interpreter_fail ();
161 }
162 else if (GNUNET_NO == helper_check)
163 {
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "%s not found!\n",
166 NETJAIL_START_SCRIPT);
167 GNUNET_TESTING_interpreter_fail ();
168 }
169
170 ns->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
171 NULL,
172 NULL,
173 NULL,
174 NETJAIL_START_SCRIPT,
175 script_argv);
176
177 ns->cwh = GNUNET_wait_child (ns->start_proc,
178 &child_completed_callback,
179 ns);
180 GNUNET_break (NULL != ns->cwh);
181}
182
183
184/**
185 * This function checks the flag NetJailState#finished, if this cmd finished.
186 *
187 */
188static int
189netjail_start_finish (void *cls,
190 GNUNET_SCHEDULER_TaskCallback cont,
191 void *cont_cls)
192{
193 struct NetJailState *ns = cls;
194
195 if (ns->finished)
196 {
197 cont (cont_cls);
198 }
199 return ns->finished;
200}
201
202/**
203 * Create command.
204 *
205 * @param label name for command.
206 * @param topology_config Configuration file for the test topology.
207 * @return command.
208 */
209struct GNUNET_TESTING_Command
210GNUNET_TESTING_cmd_netjail_start_v2 (const char *label,
211 char *topology_config)
212{
213 struct NetJailState *ns;
214
215 ns = GNUNET_new (struct NetJailState);
216 ns->finished = GNUNET_NO;
217 ns->topology_config = topology_config;
218
219 struct GNUNET_TESTING_Command cmd = {
220 .cls = ns,
221 .label = label,
222 .run = &netjail_start_run,
223 .finish = &netjail_start_finish,
224 .cleanup = &netjail_start_cleanup,
225 .traits = &netjail_start_traits
226 };
227
228 return cmd;
229}
diff --git a/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c b/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c
new file mode 100644
index 000000000..8eccc5764
--- /dev/null
+++ b/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c
@@ -0,0 +1,143 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testing/testing_api_cmd_hello_world.c
23 * @brief Command to start the netjail peers.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_ng_lib.h"
28#include "testing_cmds.h"
29
30
31/**
32 * Struct to store information handed over to callbacks.
33 *
34 */
35struct StopHelperState
36{
37
38 const char *helper_start_label;
39
40 /**
41 * The process handle
42 */
43 struct GNUNET_HELPER_Handle **helper;
44
45 unsigned int local_m;
46
47 unsigned int global_n;
48};
49
50
51/**
52* Code to clean up resource this cmd used.
53*
54* @param cls closure
55* @param cmd current CMD being cleaned up.
56*/
57static void
58stop_testing_system_cleanup (void *cls,
59 const struct GNUNET_TESTING_Command *cmd)
60{
61
62}
63
64
65/**
66 * Trait function of this cmd does nothing.
67 *
68 */
69static int
70stop_testing_system_traits (void *cls,
71 const void **ret,
72 const char *trait,
73 unsigned int index)
74{
75 return GNUNET_OK;
76}
77
78
79/**
80* This function stops the helper process for each node.
81*
82* @param cls closure.
83* @param cmd CMD being run.
84* @param is interpreter state.
85*/
86static void
87stop_testing_system_run (void *cls,
88 const struct GNUNET_TESTING_Command *cmd,
89 struct GNUNET_TESTING_Interpreter *is)
90{
91 struct StopHelperState *shs = cls;
92 struct GNUNET_HELPER_Handle **helper;
93 const struct GNUNET_TESTING_Command *start_helper_cmd;
94
95 start_helper_cmd = GNUNET_TESTING_interpreter_lookup_command (
96 shs->helper_start_label);
97 GNUNET_TESTING_get_trait_helper_handles (start_helper_cmd,
98 &helper);
99
100 for (int i = 1; i <= shs->global_n; i++)
101 {
102 for (int j = 1; j <= shs->local_m; j++)
103 {
104 GNUNET_HELPER_stop (helper[(i - 1) * shs->local_m + j - 1],
105 GNUNET_YES);
106 }
107 }
108}
109
110
111/**
112 * Create command.
113 *
114 * @param label name for command.
115 * @param helper_start_label label of the cmd to start the test system.
116 * @param topology_config Configuration file for the test topology.
117 * @return command.
118 */
119struct GNUNET_TESTING_Command
120GNUNET_TESTING_cmd_stop_testing_system_v2 (const char *label,
121 const char *helper_start_label,
122 const char *topology_config)
123{
124 struct StopHelperState *shs;
125
126 struct GNUNET_TESTING_NetjailTopology *topology =
127 GNUNET_TESTING_get_topo_from_file (topology_config);
128
129 shs = GNUNET_new (struct StopHelperState);
130 shs->helper_start_label = helper_start_label;
131 shs->local_m = topology->nodes_m;
132 shs->global_n = topology->namespaces_n;
133
134 struct GNUNET_TESTING_Command cmd = {
135 .cls = shs,
136 .label = label,
137 .run = &stop_testing_system_run,
138 .cleanup = &stop_testing_system_cleanup,
139 .traits = &stop_testing_system_traits
140 };
141
142 return cmd;
143}
diff --git a/src/testing/testing_api_cmd_netjail_stop_v2.c b/src/testing/testing_api_cmd_netjail_stop_v2.c
new file mode 100644
index 000000000..1d13407d7
--- /dev/null
+++ b/src/testing/testing_api_cmd_netjail_stop_v2.c
@@ -0,0 +1,225 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file testing/testing_api_cmd_hello_world.c
23 * @brief Command to stop the netjail script.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29
30
31#define NETJAIL_STOP_SCRIPT "./../testing/netjail_stop_v2.sh"
32
33// Child Wait handle
34struct GNUNET_ChildWaitHandle *cwh;
35
36/**
37 * Struct to hold information for callbacks.
38 *
39 */
40struct NetJailState
41{
42 /**
43 * Configuration file for the test topology.
44 */
45 char *topology_config;
46
47 /**
48 * The process id of the start script.
49 */
50 struct GNUNET_OS_Process *stop_proc;
51
52 // Flag indication if the script finished.
53 unsigned int finished;
54};
55
56
57/**
58 * The cleanup function of this cmd frees resources the cmd allocated.
59 *
60 */
61static void
62netjail_stop_cleanup (void *cls,
63 const struct GNUNET_TESTING_Command *cmd)
64{
65 struct NetJailState *ns = cls;
66
67 if (NULL != cwh)
68 {
69 GNUNET_wait_child_cancel (cwh);
70 cwh = NULL;
71 }
72 if (NULL != ns->stop_proc)
73 {
74 GNUNET_assert (0 ==
75 GNUNET_OS_process_kill (ns->stop_proc,
76 SIGKILL));
77 GNUNET_assert (GNUNET_OK ==
78 GNUNET_OS_process_wait (ns->stop_proc));
79 GNUNET_OS_process_destroy (ns->stop_proc);
80 ns->stop_proc = NULL;
81 }
82}
83
84
85/**
86 * Trait function of this cmd does nothing.
87 *
88 */
89static int
90netjail_stop_traits (void *cls,
91 const void **ret,
92 const char *trait,
93 unsigned int index)
94{
95 return GNUNET_OK;
96}
97
98
99/**
100 * Callback which will be called if the setup script finished.
101 *
102 */
103static void
104child_completed_callback (void *cls,
105 enum GNUNET_OS_ProcessStatusType type,
106 long unsigned int exit_code)
107{
108 struct NetJailState *ns = cls;
109
110 cwh = NULL;
111 if (0 == exit_code)
112 {
113 ns->finished = GNUNET_YES;
114 }
115 else
116 {
117 ns->finished = GNUNET_SYSERR;
118 }
119 GNUNET_OS_process_destroy (ns->stop_proc);
120 ns->stop_proc = NULL;
121}
122
123
124/**
125* The run method starts the script which deletes the network namespaces.
126*
127* @param cls closure.
128* @param cmd CMD being run.
129* @param is interpreter state.
130*/
131static void
132netjail_stop_run (void *cls,
133 const struct GNUNET_TESTING_Command *cmd,
134 struct GNUNET_TESTING_Interpreter *is)
135{
136 struct NetJailState *ns = cls;
137 char *pid;
138 GNUNET_asprintf (&pid,
139 "%u",
140 getpid ());
141 char *const script_argv[] = {NETJAIL_STOP_SCRIPT,
142 ns->topology_config,
143 pid,
144 NULL};
145 unsigned int helper_check = GNUNET_OS_check_helper_binary (
146 NETJAIL_STOP_SCRIPT,
147 GNUNET_YES,
148 NULL);
149
150 if (GNUNET_NO == helper_check)
151 {
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 "No SUID for %s!\n",
154 NETJAIL_STOP_SCRIPT);
155 GNUNET_TESTING_interpreter_fail ();
156 }
157 else if (GNUNET_NO == helper_check)
158 {
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "%s not found!\n",
161 NETJAIL_STOP_SCRIPT);
162 GNUNET_TESTING_interpreter_fail ();
163 }
164
165 ns->stop_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
166 NULL,
167 NULL,
168 NULL,
169 NETJAIL_STOP_SCRIPT,
170 script_argv);
171
172 cwh = GNUNET_wait_child (ns->stop_proc,
173 &child_completed_callback,
174 ns);
175 GNUNET_break (NULL != cwh);
176
177}
178
179
180/**
181 * This function checks the flag NetJailState#finished, if this cmd finished.
182 *
183 */
184static int
185netjail_stop_finish (void *cls,
186 GNUNET_SCHEDULER_TaskCallback cont,
187 void *cont_cls)
188{
189 struct NetJailState *ns = cls;
190
191 if (ns->finished)
192 {
193 cont (cont_cls);
194 }
195 return ns->finished;
196}
197
198
199/**
200 * Create command.
201 *
202 * @param label name for command.
203 * @param topology_config Configuration file for the test topology.
204 * @return command.
205 */
206struct GNUNET_TESTING_Command
207GNUNET_TESTING_cmd_netjail_stop_v2 (const char *label,
208 char *topology_config)
209{
210 struct NetJailState *ns;
211
212 ns = GNUNET_new (struct NetJailState);
213 ns->topology_config = topology_config;
214
215 struct GNUNET_TESTING_Command cmd = {
216 .cls = ns,
217 .label = label,
218 .run = &netjail_stop_run,
219 .finish = &netjail_stop_finish,
220 .cleanup = &netjail_stop_cleanup,
221 .traits = &netjail_stop_traits
222 };
223
224 return cmd;
225}
diff --git a/src/testing/topo.sh b/src/testing/topo.sh
new file mode 100755
index 000000000..090c3053f
--- /dev/null
+++ b/src/testing/topo.sh
@@ -0,0 +1,95 @@
1#!/bin/bash
2
3declare -A K_PLUGIN
4declare -A R_TCP
5declare -A R_UDP
6declare -A P_PLUGIN
7
8extract_attributes()
9{
10 line_key=$1
11 line=$2
12
13 if [ "$line_key" = "P" ]
14 then
15 n=$(echo $line|cut -d \| -f 1|awk -F: '{print $2}')
16 echo $n
17 m=$(echo $line|cut -d \| -f 1|awk -F: '{print $3}')
18 echo $m
19 else
20 number=$(echo $line|cut -d \| -f 1| cut -c 2-|cut -d : -f 2 )
21 echo $number
22 fi
23
24
25 nf=$(echo $line|awk -F: '{print NF}')
26 for ((i=2;i<=$nf;i++))
27 do
28 entry=$(echo $line |awk -v i=$i -F\| '{print $i}')
29 key=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 1)
30 value=$(echo $entry|cut -d { -f 2|cut -d } -f 1|cut -d : -f 2)
31 if [ "$key" = "tcp_port" ]
32 then
33 echo tcp port: $value
34 R_TCP[$number]=$value
35 elif [ "$key" = "udp_port" ]
36 then
37 echo udp port: $value
38 R_UDP[$number]=$value
39 elif [ "$key" = "plugin" ]
40 then
41 echo plugin: $value
42 echo $line_key
43 if [ "$line_key" = "P" ]
44 then
45 P_PLUGIN[$n,$m]=$value
46 echo $n $m ${P_PLUGIN[$n,$m]}
47 elif [ "$line_key" = "K" ]
48 then
49 K_PLUGIN[$number]=$value
50 fi
51 fi
52 done
53}
54
55read_topology(){
56
57local filename=$1
58while read line; do
59# reading each line
60 echo $line
61 key=$(cut -c -1 <<< $line)
62 if [ "$key" = "M" ]
63 then
64 LOCAL_M=$(cut -d : -f 2 <<< $line)
65 echo $LOCAL_M
66 elif [ "$key" = "N" ]
67 then
68 GLOBAL_N=$(cut -d : -f 2 <<< $line)
69 echo $GLOBAL_N
70 elif [ "$key" = "X" ]
71 then
72 KNOWN=$(cut -d : -f 2 <<< $line)
73 echo $KNOWN
74 elif [ "$key" = "T" ]
75 then
76 PLUGIN=$(cut -d : -f 2 <<< $line)
77 echo $PLUGIN
78 elif [ "$key" = "K" ]
79 then
80 echo know node
81 extract_attributes $key $line
82 elif [ "$key" = "R" ]
83 then
84 echo router
85 extract_attributes $key $line
86 elif [ "$key" = "P" ]
87 then
88 echo node
89 extract_attributes $key $line
90 fi
91done < $filename
92}
93
94
95