aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authort3sserakt <t3ss@posteo.de>2021-10-14 16:39:24 +0200
committert3sserakt <t3ss@posteo.de>2021-10-14 16:39:24 +0200
commitd64ac269856744b9bab170964e1d6f36896ecc55 (patch)
treeed5aa79b91fe3cf9388433a5611581b7ce3b846c /src/testing
parent003910fc614cd347919707d1bf3c37a939978459 (diff)
downloadgnunet-d64ac269856744b9bab170964e1d6f36896ecc55.tar.gz
gnunet-d64ac269856744b9bab170964e1d6f36896ecc55.zip
removed versioned artefacts with v2 and v3. changes to reflect the changes in testing_api_loop.c
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/Makefile.am4
-rwxr-xr-xsrc/testing/netjail_core.sh190
-rwxr-xr-xsrc/testing/netjail_core_v2.sh260
-rwxr-xr-xsrc/testing/netjail_exec.sh6
-rwxr-xr-xsrc/testing/netjail_exec_v2.sh14
-rwxr-xr-xsrc/testing/netjail_start.sh70
-rwxr-xr-xsrc/testing/netjail_start_v2.sh74
-rwxr-xr-xsrc/testing/netjail_stop.sh49
-rwxr-xr-xsrc/testing/netjail_stop_v2.sh59
-rw-r--r--src/testing/testing.c30
-rw-r--r--src/testing/testing_api_cmd_block_until_all_peers_started.c6
-rw-r--r--src/testing/testing_api_cmd_block_until_external_trigger.c63
-rw-r--r--src/testing/testing_api_cmd_netjail_start.c71
-rw-r--r--src/testing/testing_api_cmd_netjail_start_testsystem.c327
-rw-r--r--src/testing/testing_api_cmd_netjail_start_testsystem_v2.c723
-rw-r--r--src/testing/testing_api_cmd_netjail_start_v2.c209
-rw-r--r--src/testing/testing_api_cmd_netjail_stop.c54
-rw-r--r--src/testing/testing_api_cmd_netjail_stop_testsystem.c79
-rw-r--r--src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c141
-rw-r--r--src/testing/testing_api_cmd_netjail_stop_v2.c185
-rw-r--r--src/testing/testing_api_loop.c8
-rw-r--r--src/testing/testing_cmds.h2
22 files changed, 665 insertions, 1959 deletions
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am
index e9389a876..39e85c4ac 100644
--- a/src/testing/Makefile.am
+++ b/src/testing/Makefile.am
@@ -51,13 +51,9 @@ libgnunettesting_la_SOURCES = \
51 testing_api_cmd_block_until_all_peers_started.c \ 51 testing_api_cmd_block_until_all_peers_started.c \
52 testing_api_cmd_block_until_external_trigger.c \ 52 testing_api_cmd_block_until_external_trigger.c \
53 testing_api_cmd_netjail_start.c \ 53 testing_api_cmd_netjail_start.c \
54 testing_api_cmd_netjail_start_v2.c \
55 testing_api_cmd_netjail_start_testsystem.c \ 54 testing_api_cmd_netjail_start_testsystem.c \
56 testing_api_cmd_netjail_start_testsystem_v2.c \
57 testing_api_cmd_netjail_stop_testsystem.c \ 55 testing_api_cmd_netjail_stop_testsystem.c \
58 testing_api_cmd_netjail_stop_testsystem_v2.c \
59 testing_api_cmd_netjail_stop.c \ 56 testing_api_cmd_netjail_stop.c \
60 testing_api_cmd_netjail_stop_v2.c \
61 testing.c testing.h \ 57 testing.c testing.h \
62 testing_api_cmd_system_create.c \ 58 testing_api_cmd_system_create.c \
63 testing_api_cmd_system_destroy.c \ 59 testing_api_cmd_system_destroy.c \
diff --git a/src/testing/netjail_core.sh b/src/testing/netjail_core.sh
index cf350d3fa..ef0a54a5e 100755
--- a/src/testing/netjail_core.sh
+++ b/src/testing/netjail_core.sh
@@ -2,6 +2,7 @@
2# 2#
3 3
4 4
5PREFIX=${PPID:?must run from a parent process}
5 6
6# running with `sudo` is required to be 7# running with `sudo` is required to be
7# able running the actual commands as the 8# able running the actual commands as the
@@ -9,10 +10,61 @@
9 10
10export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 11export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
11 12
12netjail_check() { 13export RESULT=
13 NODE_COUNT=$1 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}
14 64
15 FD_COUNT=$(($(ls /proc/self/fd | wc -w) - 4)) 65netjail_check() {
66 local NODE_COUNT=$1
67 local FD_COUNT=$(($(ls /proc/self/fd | wc -w) - 4))
16 68
17 # quit if `$FD_COUNT < ($LOCAL_M * $GLOBAL_N * 2)`: 69 # quit if `$FD_COUNT < ($LOCAL_M * $GLOBAL_N * 2)`:
18 # the script also requires `sudo -C ($FD_COUNT + 4)` 70 # the script also requires `sudo -C ($FD_COUNT + 4)`
@@ -25,43 +77,81 @@ netjail_check() {
25 fi 77 fi
26} 78}
27 79
28netjail_print_name() { 80netjail_check_bin() {
29 printf "%s%02x%02x" $1 $2 ${3:-0} 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
30} 91}
31 92
32netjail_bridge() { 93netjail_bridge() {
33 BRIDGE=$1 94 netjail_next_interface
95 local NUM=$RESULT
96 local BRIDGE=$(printf "%06x-%08x" $PREFIX $NUM)
34 97
35 ip link add $BRIDGE type bridge 98 ip link add $BRIDGE type bridge
36 ip link set dev $BRIDGE up 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
37} 110}
38 111
39netjail_bridge_clear() { 112netjail_bridge_clear() {
40 BRIDGE=$1 113 local BRIDGE=$1
41 114
42 ip link delete $BRIDGE 115 ip link delete $BRIDGE
43} 116}
44 117
45netjail_node() { 118netjail_node() {
46 NODE=$1 119 netjail_next_namespace
120 local NUM=$RESULT
121 local NODE=$(printf "%06x-%08x" $PREFIX $NUM)
47 122
48 ip netns add $NODE 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
49} 134}
50 135
51netjail_node_clear() { 136netjail_node_clear() {
52 NODE=$1 137 local NODE=$1
53 138
54 ip netns delete $NODE 139 ip netns delete $NODE
55} 140}
56 141
57netjail_node_link_bridge() { 142netjail_node_link_bridge() {
58 NODE=$1 143 local NODE=$1
59 BRIDGE=$2 144 local BRIDGE=$2
60 ADDRESS=$3 145 local ADDRESS=$3
61 MASK=$4 146 local MASK=$4
62 147
63 LINK_IF="$NODE-$BRIDGE-0" 148 netjail_next_interface
64 LINK_BR="$NODE-$BRIDGE-1" 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)
65 155
66 ip link add $LINK_IF type veth peer name $LINK_BR 156 ip link add $LINK_IF type veth peer name $LINK_BR
67 ip link set $LINK_IF netns $NODE 157 ip link set $LINK_IF netns $NODE
@@ -72,28 +162,47 @@ netjail_node_link_bridge() {
72 ip -n $NODE link set up dev lo 162 ip -n $NODE link set up dev lo
73 163
74 ip link set $LINK_BR up 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
75} 184}
76 185
77netjail_node_add_nat() { 186netjail_node_add_nat() {
78 NODE=$1 187 local NODE=$1
79 ADDRESS=$2 188 local ADDRESS=$2
80 MASK=$3 189 local MASK=$3
81 190
82 ip netns exec $NODE iptables -t nat -A POSTROUTING -s "$ADDRESS/$MASK" -j MASQUERADE 191 ip netns exec $NODE iptables -t nat -A POSTROUTING -s "$ADDRESS/$MASK" -j MASQUERADE
83} 192}
84 193
85netjail_node_add_default() { 194netjail_node_add_default() {
86 NODE=$1 195 local NODE=$1
87 ADDRESS=$2 196 local ADDRESS=$2
88 197
89 ip -n $NODE route add default via $ADDRESS 198 ip -n $NODE route add default via $ADDRESS
90} 199}
91 200
92netjail_node_exec() { 201netjail_node_exec() {
93 JAILOR=${SUDO_USER:?must run in sudo} 202 JAILOR=${SUDO_USER:?must run in sudo}
94 NODE=$1 203 local NODE=$1
95 FD_IN=$2 204 local FD_IN=$2
96 FD_OUT=$3 205 local FD_OUT=$3
97 shift 3 206 shift 3
98 207
99 ip netns exec $NODE sudo -u $JAILOR -- $@ 1>& $FD_OUT 0<& $FD_IN 208 ip netns exec $NODE sudo -u $JAILOR -- $@ 1>& $FD_OUT 0<& $FD_IN
@@ -114,3 +223,38 @@ netjail_node_exec_without_fds_and_sudo() {
114 ip netns exec $NODE $@ 223 ip netns exec $NODE $@
115} 224}
116 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_core_v2.sh b/src/testing/netjail_core_v2.sh
deleted file mode 100755
index ef0a54a5e..000000000
--- a/src/testing/netjail_core_v2.sh
+++ /dev/null
@@ -1,260 +0,0 @@
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.sh b/src/testing/netjail_exec.sh
index 17a7caaac..cd993a39b 100755
--- a/src/testing/netjail_exec.sh
+++ b/src/testing/netjail_exec.sh
@@ -9,8 +9,6 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
9M=$1 9M=$1
10N=$2 10N=$2
11 11
12NODE=$(netjail_print_name "N" $N $M) 12NODE=$6
13 13
14 14netjail_node_exec_without_fds_and_sudo $NODE $3 $4 $5 $1 $2
15
16netjail_node_exec_without_fds_and_sudo $NODE $3 $4 $5 $1 $2
diff --git a/src/testing/netjail_exec_v2.sh b/src/testing/netjail_exec_v2.sh
deleted file mode 100755
index 597baad20..000000000
--- a/src/testing/netjail_exec_v2.sh
+++ /dev/null
@@ -1,14 +0,0 @@
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.sh b/src/testing/netjail_start.sh
index 0984a3c42..1dfe1dfdf 100755
--- a/src/testing/netjail_start.sh
+++ b/src/testing/netjail_start.sh
@@ -1,52 +1,74 @@
1#!/bin/sh 1#!/bin/bash
2. "./../testing/netjail_core.sh" 2. "./../testing/netjail_core.sh"
3. "./../testing/topo.sh"
3 4
4set -eu 5set -eu
5set -x 6set -x
6 7
7export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 8export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
8 9
9LOCAL_M=$1 10filename=$1
10GLOBAL_N=$2 11PREFIX=$2
11 12
12# TODO: stunserver? ..and globally known peer? 13read_topology $filename
13 14
14shift 2 15shift 2
15 16
16LOCAL_GROUP="192.168.15" 17LOCAL_GROUP="192.168.15"
17GLOBAL_GROUP="92.68.150" 18GLOBAL_GROUP="92.68.150"
19KNOWN_GROUP="92.68.151"
18 20
19NETWORK_NET=$(netjail_print_name "n" $GLOBAL_N $LOCAL_M)
20 21
21netjail_bridge $NETWORK_NET 22echo "Start [local: $LOCAL_GROUP.0/24, global: $GLOBAL_GROUP.0/16]"
22 23
23for N in $(seq $GLOBAL_N); do 24netjail_bridge
24 ROUTER=$(netjail_print_name "R" $N) 25NETWORK_NET=$RESULT
25 26
26 netjail_node $ROUTER 27for X in $(seq $KNOWN); do
27 netjail_node_link_bridge $ROUTER $NETWORK_NET "$GLOBAL_GROUP.$N" 24 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
28 33
29 ROUTER_NET=$(netjail_print_name "r" $N) 34declare -A NODES
35declare -A NODE_LINKS
30 36
31 netjail_bridge $ROUTER_NET 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
32 44
33 for M in $(seq $LOCAL_M); do 45 for M in $(seq $LOCAL_M); do
34 NODE=$(netjail_print_name "N" $N $M) 46 netjail_node
35 47 NODES[$N,$M]=$RESULT
36 netjail_node $NODE 48 netjail_node_link_bridge ${NODES[$N,$M]} ${ROUTER_NETS[$N]} "$LOCAL_GROUP.$M" 24
37 netjail_node_link_bridge $NODE $ROUTER_NET "$LOCAL_GROUP.$M" 24 49 NODE_LINKS[$N,$M]=$RESULT
38 done 50 done
39 51
40 ROUTER_ADDR="$LOCAL_GROUP.$(($LOCAL_M+1))" 52 ROUTER_ADDR="$LOCAL_GROUP.$(($LOCAL_M+1))"
41 53 netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24
42 netjail_node_link_bridge $ROUTER $ROUTER_NET $ROUTER_ADDR 24 54 ROUTER_LINKS[$N]=$RESULT
43 netjail_node_add_nat $ROUTER $ROUTER_ADDR 24 55
56 netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24
44 57
45 for M in $(seq $LOCAL_M); do 58 for M in $(seq $LOCAL_M); do
46 NODE=$(netjail_print_name "N" $N $M) 59 netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR
47
48 netjail_node_add_default $NODE $ROUTER_ADDR
49 done 60 done
50done
51
52 61
62 # TODO Topology configuration must be enhanced to configure forwarding to more than one subnet node via different ports.
63
64 if [ "1" == "${R_TCP[$N]}" ]
65 then
66 ip netns exec ${ROUTERS[$N]} iptables -t nat -A PREROUTING -p tcp -d $GLOBAL_GROUP.$N --dport 60002 -j DNAT --to $LOCAL_GROUP.1
67 ip netns exec ${ROUTERS[$N]} iptables -A FORWARD -d $LOCAL_GROUP.1 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
68 fi
69 if [ "1" == "${R_UDP[$N]}" ]
70 then
71 ip netns exec ${ROUTERS[$N]} iptables -t nat -A PREROUTING -p udp -d $GLOBAL_GROUP.$N --dport 60002 -j DNAT --to $LOCAL_GROUP.1
72 ip netns exec ${ROUTERS[$N]} iptables -A FORWARD -d $LOCAL_GROUP.1 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
73 fi
74done
diff --git a/src/testing/netjail_start_v2.sh b/src/testing/netjail_start_v2.sh
deleted file mode 100755
index dd1f2ad93..000000000
--- a/src/testing/netjail_start_v2.sh
+++ /dev/null
@@ -1,74 +0,0 @@
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
61
62 # TODO Topology configuration must be enhanced to configure forwarding to more than one subnet node via different ports.
63
64 if [ "1" == "${R_TCP[$N]}" ]
65 then
66 ip netns exec ${ROUTERS[$N]} iptables -t nat -A PREROUTING -p tcp -d $GLOBAL_GROUP.$N --dport 60002 -j DNAT --to $LOCAL_GROUP.1
67 ip netns exec ${ROUTERS[$N]} iptables -A FORWARD -d $LOCAL_GROUP.1 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
68 fi
69 if [ "1" == "${R_UDP[$N]}" ]
70 then
71 ip netns exec ${ROUTERS[$N]} iptables -t nat -A PREROUTING -p udp -d $GLOBAL_GROUP.$N --dport 60002 -j DNAT --to $LOCAL_GROUP.1
72 ip netns exec ${ROUTERS[$N]} iptables -A FORWARD -d $LOCAL_GROUP.1 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
73 fi
74done
diff --git a/src/testing/netjail_stop.sh b/src/testing/netjail_stop.sh
index 08f68cf7f..abfaf3acf 100755
--- a/src/testing/netjail_stop.sh
+++ b/src/testing/netjail_stop.sh
@@ -1,24 +1,57 @@
1#!/bin/sh 1#!/bin/bash
2. "./../testing/netjail_core.sh" 2. "./../testing/netjail_core.sh"
3. "./../testing/topo.sh"
3 4
4set -eu 5set -eu
5set -x 6set -x
6 7
7export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 8export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
8 9
9LOCAL_M=$1 10filename=$1
10GLOBAL_N=$2 11PREFIX=$2
11NETWORK_NET=$(netjail_print_name "n" $GLOBAL_N $LOCAL_M)
12 12
13shift 2 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
14 29
15for N in $(seq $GLOBAL_N); do 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
16 for M in $(seq $LOCAL_M); do 42 for M in $(seq $LOCAL_M); do
17 netjail_node_clear $(netjail_print_name "N" $N $M) 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]}
18 done 49 done
50
19 51
20 netjail_bridge_clear $(netjail_print_name "r" $N) 52 netjail_bridge_clear ${ROUTER_NETS[$N]}
21 netjail_node_clear $(netjail_print_name "R" $N) 53 netjail_node_unlink_bridge ${NETWORK_LINKS[$N]}
54 netjail_node_clear ${ROUTERS[$N]}
22done 55done
23 56
24netjail_bridge_clear $NETWORK_NET 57netjail_bridge_clear $NETWORK_NET
diff --git a/src/testing/netjail_stop_v2.sh b/src/testing/netjail_stop_v2.sh
deleted file mode 100755
index 6e9fd1ccc..000000000
--- a/src/testing/netjail_stop_v2.sh
+++ /dev/null
@@ -1,59 +0,0 @@
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/testing.c b/src/testing/testing.c
index a11d404a5..766c7b084 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -34,6 +34,7 @@
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#include "gnunet_testing_ng_lib.h"
37#include "testing_cmds.h"
37 38
38#define LOG(kind, ...) GNUNET_log_from (kind, "testing-api", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from (kind, "testing-api", __VA_ARGS__)
39 40
@@ -1961,10 +1962,6 @@ get_connect_value (char *line, struct GNUNET_TESTING_NetjailNode *node)
1961 } 1962 }
1962 1963
1963 GNUNET_free (copy); 1964 GNUNET_free (copy);
1964 LOG (GNUNET_ERROR_TYPE_ERROR,
1965 "address_prefix %s\n",
1966 prefix->address_prefix);
1967
1968 return node_connection; 1965 return node_connection;
1969} 1966}
1970 1967
@@ -2007,6 +2004,31 @@ node_connections (char *line, struct GNUNET_TESTING_NetjailNode *node)
2007 2004
2008 2005
2009/** 2006/**
2007 * Create a GNUNET_CMDS_LOCAL_FINISHED message.
2008 *
2009 * @param rv The result of the local test as GNUNET_GenericReturnValue.
2010 * @return The GNUNET_CMDS_LOCAL_FINISHED message.
2011*/
2012struct GNUNET_MessageHeader *
2013GNUNET_TESTING_send_local_test_finished_msg (enum GNUNET_GenericReturnValue rv)
2014{
2015 struct GNUNET_CMDS_LOCAL_FINISHED *reply;
2016 size_t msg_length;
2017
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "Local test exits with status %d\n",
2020 rv);
2021 msg_length = sizeof(struct GNUNET_CMDS_LOCAL_FINISHED);
2022 reply = GNUNET_new (struct GNUNET_CMDS_LOCAL_FINISHED);
2023 reply->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED);
2024 reply->header.size = htons ((uint16_t) msg_length);
2025 reply->result = htons (rv);
2026
2027 return (struct GNUNET_MessageHeader *) reply;
2028}
2029
2030
2031/**
2010 * Getting the topology from file. 2032 * Getting the topology from file.
2011 * 2033 *
2012 * @param filename The name of the topology file. 2034 * @param filename The name of the topology file.
diff --git a/src/testing/testing_api_cmd_block_until_all_peers_started.c b/src/testing/testing_api_cmd_block_until_all_peers_started.c
index 8a47a7ce0..763713e15 100644
--- a/src/testing/testing_api_cmd_block_until_all_peers_started.c
+++ b/src/testing/testing_api_cmd_block_until_all_peers_started.c
@@ -39,6 +39,11 @@
39struct BlockState 39struct BlockState
40{ 40{
41 /** 41 /**
42 * Context for our asynchronous completion.
43 */
44 struct GNUNET_TESTING_AsyncContext ac;
45
46 /**
42 * Flag to indicate if all peers have started. 47 * Flag to indicate if all peers have started.
43 * 48 *
44 */ 49 */
@@ -93,6 +98,7 @@ GNUNET_TESTING_cmd_block_until_all_peers_started (const char *label,
93 .cls = bs, 98 .cls = bs,
94 .label = label, 99 .label = label,
95 .run = &block_until_all_peers_started_run, 100 .run = &block_until_all_peers_started_run,
101 .ac = &bs->ac,
96 .cleanup = &block_until_all_peers_started_cleanup 102 .cleanup = &block_until_all_peers_started_cleanup
97 }; 103 };
98 104
diff --git a/src/testing/testing_api_cmd_block_until_external_trigger.c b/src/testing/testing_api_cmd_block_until_external_trigger.c
index b416fa595..aeb9ffda3 100644
--- a/src/testing/testing_api_cmd_block_until_external_trigger.c
+++ b/src/testing/testing_api_cmd_block_until_external_trigger.c
@@ -39,9 +39,14 @@
39struct BlockState 39struct BlockState
40{ 40{
41 /** 41 /**
42 * Flag to indicate if all peers have started. 42 * Context for our asynchronous completion.
43 */ 43 */
44 unsigned int *stop_blocking; 44 struct GNUNET_TESTING_AsyncContext ac;
45
46 /**
47 * The label of this command.
48 */
49 const char *label;
45}; 50};
46 51
47 52
@@ -57,6 +62,47 @@ block_until_all_peers_started_cleanup (void *cls)
57 GNUNET_free (bs); 62 GNUNET_free (bs);
58} 63}
59 64
65static int
66block_until_external_trigger_traits (void *cls,
67 const void **ret,
68 const char *trait,
69 unsigned int index)
70{
71 struct BlockState *bs = cls;
72 struct GNUNET_TESTING_AsyncContext *ac = &bs->ac;
73 struct GNUNET_TESTING_Trait traits[] = {
74 {
75 .index = 0,
76 .trait_name = "async_context",
77 .ptr = (const void *) ac,
78 },
79 GNUNET_TESTING_trait_end ()
80 };
81
82 return GNUNET_TESTING_get_trait (traits,
83 ret,
84 trait,
85 index);
86}
87
88
89/**
90 * Function to get the trait with the async context.
91 *
92 * @param[out] ac GNUNET_TESTING_AsyncContext.
93 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
94 */
95int
96GNUNET_TESTING_get_trait_async_context (
97 const struct GNUNET_TESTING_Command *cmd,
98 struct GNUNET_TESTING_AsyncContext **ac)
99{
100 return cmd->traits (cmd->cls,
101 (const void **) ac,
102 "async_context",
103 (unsigned int) 0);
104}
105
60 106
61/** 107/**
62 * This function does nothing but to start the cmd. 108 * This function does nothing but to start the cmd.
@@ -66,8 +112,11 @@ static void
66block_until_all_peers_started_run (void *cls, 112block_until_all_peers_started_run (void *cls,
67 struct GNUNET_TESTING_Interpreter *is) 113 struct GNUNET_TESTING_Interpreter *is)
68{ 114{
115 struct BlockState *bs = cls;
116
69 LOG (GNUNET_ERROR_TYPE_DEBUG, 117 LOG (GNUNET_ERROR_TYPE_DEBUG,
70 "block_until_all_peers_started_run!\n"); 118 "block %s running!\n",
119 bs->label);
71} 120}
72 121
73 122
@@ -79,20 +128,20 @@ block_until_all_peers_started_run (void *cls,
79 * @return command. 128 * @return command.
80 */ 129 */
81struct GNUNET_TESTING_Command 130struct GNUNET_TESTING_Command
82GNUNET_TESTING_cmd_block_until_external_trigger (const char *label, 131GNUNET_TESTING_cmd_block_until_external_trigger (const char *label)
83 unsigned int *
84 stop_blocking)
85{ 132{
86 struct BlockState *bs; 133 struct BlockState *bs;
87 134
88 bs = GNUNET_new (struct BlockState); 135 bs = GNUNET_new (struct BlockState);
89 bs->stop_blocking = stop_blocking; 136 bs->label = label;
90 { 137 {
91 struct GNUNET_TESTING_Command cmd = { 138 struct GNUNET_TESTING_Command cmd = {
92 .cls = bs, 139 .cls = bs,
93 .label = label, 140 .label = label,
94 .run = &block_until_all_peers_started_run, 141 .run = &block_until_all_peers_started_run,
142 .ac = &bs->ac,
95 .cleanup = &block_until_all_peers_started_cleanup, 143 .cleanup = &block_until_all_peers_started_cleanup,
144 .traits = block_until_external_trigger_traits
96 }; 145 };
97 146
98 return cmd; 147 return cmd;
diff --git a/src/testing/testing_api_cmd_netjail_start.c b/src/testing/testing_api_cmd_netjail_start.c
index 9cd84cb4f..35fb90f3c 100644
--- a/src/testing/testing_api_cmd_netjail_start.c
+++ b/src/testing/testing_api_cmd_netjail_start.c
@@ -19,7 +19,7 @@
19 */ 19 */
20 20
21/** 21/**
22 * @file testing/testing_api_cmd_hello_world.c 22 * @file testing/testing_api_cmd_netjail_start.c
23 * @brief Command to start the netjail script. 23 * @brief Command to start the netjail script.
24 * @author t3sserakt 24 * @author t3sserakt
25 */ 25 */
@@ -43,16 +43,16 @@ struct NetJailState
43 // Child Wait handle 43 // Child Wait handle
44 struct GNUNET_ChildWaitHandle *cwh; 44 struct GNUNET_ChildWaitHandle *cwh;
45 45
46 // Number of local nodes in each namespace.
47 char *local_m;
48
49 // The number of namespaces.
50 char *global_n;
51
52 /** 46 /**
53 * The process id of the start script. 47 * The process id of the start script.
54 */ 48 */
55 struct GNUNET_OS_Process *start_proc; 49 struct GNUNET_OS_Process *start_proc;
50
51 /**
52 * Configuration file for the test topology.
53 */
54 char *topology_config;
55
56}; 56};
57 57
58 58
@@ -67,6 +67,7 @@ netjail_start_cleanup (void *cls)
67 67
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 "netjail_start_cleanup!\n"); 69 "netjail_start_cleanup!\n");
70
70 if (NULL != ns->cwh) 71 if (NULL != ns->cwh)
71 { 72 {
72 GNUNET_wait_child_cancel (ns->cwh); 73 GNUNET_wait_child_cancel (ns->cwh);
@@ -105,9 +106,9 @@ child_completed_callback (void *cls,
105 } 106 }
106 else 107 else
107 { 108 {
108 // FIXME: log status code
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 "Child completed with an error!\n"); 110 "Child failed with error %lu!\n",
111 exit_code);
111 GNUNET_TESTING_async_fail (&ns->ac); 112 GNUNET_TESTING_async_fail (&ns->ac);
112 } 113 }
113} 114}
@@ -124,13 +125,13 @@ netjail_start_run (void *cls,
124 struct GNUNET_TESTING_Interpreter *is) 125 struct GNUNET_TESTING_Interpreter *is)
125{ 126{
126 struct NetJailState *ns = cls; 127 struct NetJailState *ns = cls;
127 char *const script_argv[] = { 128 char pid[15];
128 NETJAIL_START_SCRIPT, 129 enum GNUNET_GenericReturnValue helper_check;
129 ns->local_m, 130
130 ns->global_n, 131 // FIXME: NETJAIL_START_SCRIPT like this is bad,
131 NULL 132 // use location from share/gnunet/ of installed
132 }; 133 // binary in case libgnunettesting is used as a lib!
133 unsigned int helper_check = GNUNET_OS_check_helper_binary ( 134 helper_check = GNUNET_OS_check_helper_binary (
134 NETJAIL_START_SCRIPT, 135 NETJAIL_START_SCRIPT,
135 GNUNET_YES, 136 GNUNET_YES,
136 NULL); 137 NULL);
@@ -141,22 +142,37 @@ netjail_start_run (void *cls,
141 "No SUID for %s!\n", 142 "No SUID for %s!\n",
142 NETJAIL_START_SCRIPT); 143 NETJAIL_START_SCRIPT);
143 GNUNET_TESTING_interpreter_fail (is); 144 GNUNET_TESTING_interpreter_fail (is);
145 return;
144 } 146 }
145 else if (GNUNET_NO == helper_check) 147 if (GNUNET_SYSERR == helper_check)
146 { 148 {
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 "%s not found!\n", 150 "%s not found!\n",
149 NETJAIL_START_SCRIPT); 151 NETJAIL_START_SCRIPT);
150 GNUNET_TESTING_interpreter_fail (is); 152 GNUNET_TESTING_interpreter_fail (is);
153 return;
151 } 154 }
152 155
153 ns->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, 156 GNUNET_snprintf (pid,
154 NULL, 157 sizeof (pid),
155 NULL, 158 "%u",
156 NULL, 159 getpid ());
157 NETJAIL_START_SCRIPT, 160 {
158 script_argv); 161 char *const script_argv[] = {
162 NETJAIL_START_SCRIPT,
163 ns->topology_config,
164 pid,
165 NULL
166 };
159 167
168 ns->start_proc
169 = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
170 NULL,
171 NULL,
172 NULL,
173 NETJAIL_START_SCRIPT,
174 script_argv);
175 }
160 ns->cwh = GNUNET_wait_child (ns->start_proc, 176 ns->cwh = GNUNET_wait_child (ns->start_proc,
161 &child_completed_callback, 177 &child_completed_callback,
162 ns); 178 ns);
@@ -168,20 +184,17 @@ netjail_start_run (void *cls,
168 * Create command. 184 * Create command.
169 * 185 *
170 * @param label name for command. 186 * @param label name for command.
171 * @param local_m Number of local nodes in each namespace. 187 * @param topology_config Configuration file for the test topology.
172 * @param global_n The number of namespaces.
173 * @return command. 188 * @return command.
174 */ 189 */
175struct GNUNET_TESTING_Command 190struct GNUNET_TESTING_Command
176GNUNET_TESTING_cmd_netjail_start (const char *label, 191GNUNET_TESTING_cmd_netjail_start (const char *label,
177 char *local_m, 192 char *topology_config)
178 char *global_n)
179{ 193{
180 struct NetJailState *ns; 194 struct NetJailState *ns;
181 195
182 ns = GNUNET_new (struct NetJailState); 196 ns = GNUNET_new (struct NetJailState);
183 ns->local_m = local_m; 197 ns->topology_config = topology_config;
184 ns->global_n = global_n;
185 { 198 {
186 struct GNUNET_TESTING_Command cmd = { 199 struct GNUNET_TESTING_Command cmd = {
187 .cls = ns, 200 .cls = ns,
diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem.c b/src/testing/testing_api_cmd_netjail_start_testsystem.c
index 4224a5736..a1d71c436 100644
--- a/src/testing/testing_api_cmd_netjail_start_testsystem.c
+++ b/src/testing/testing_api_cmd_netjail_start_testsystem.c
@@ -67,6 +67,11 @@ struct NetJailState
67 struct GNUNET_TESTING_AsyncContext ac; 67 struct GNUNET_TESTING_AsyncContext ac;
68 68
69 /** 69 /**
70 * The complete topology information.
71 */
72 struct GNUNET_TESTING_NetjailTopology *topology;
73
74 /**
70 * Pointer to the return value of the test. 75 * Pointer to the return value of the test.
71 * 76 *
72 */ 77 */
@@ -96,16 +101,22 @@ struct NetJailState
96 unsigned int n_helper; 101 unsigned int n_helper;
97 102
98 /** 103 /**
99 * Number of nodes in a network namespace. //TODO make this a unsigned int 104 * Number of nodes in a natted subnet.
105 *
106 */
107 unsigned int local_m;
108
109 /**
110 * Number of natted subnets.
100 * 111 *
101 */ 112 */
102 char *local_m; 113 unsigned int global_n;
103 114
104 /** 115 /**
105 * Number of network namespaces. //TODO make this a unsigned int 116 * Number of global known nodes.
106 * 117 *
107 */ 118 */
108 char *global_n; 119 unsigned int known;
109 120
110 /** 121 /**
111 * The send handle for the helper 122 * The send handle for the helper
@@ -271,7 +282,8 @@ netjail_exec_traits (void *cls,
271int 282int
272GNUNET_TESTING_get_trait_helper_handles (const struct 283GNUNET_TESTING_get_trait_helper_handles (const struct
273 GNUNET_TESTING_Command *cmd, 284 GNUNET_TESTING_Command *cmd,
274 struct GNUNET_HELPER_Handle ***helper) 285 struct GNUNET_HELPER_Handle ***
286 helper)
275{ 287{
276 return cmd->traits (cmd->cls, 288 return cmd->traits (cmd->cls,
277 (const void **) helper, 289 (const void **) helper,
@@ -301,6 +313,43 @@ clear_msg (void *cls, int result)
301} 313}
302 314
303 315
316static void
317send_all_peers_started (unsigned int i, unsigned int j, struct NetJailState *ns)
318{
319 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
320 struct GNUNET_CMDS_ALL_PEERS_STARTED *reply;
321 size_t msg_length;
322 struct GNUNET_HELPER_Handle *helper;
323 struct TestingSystemCount *tbc;
324
325 tbc = GNUNET_new (struct TestingSystemCount);
326 tbc->ns = ns;
327 // 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.
328 if (0 == i)
329 tbc->count = j + total_number;
330 else
331 tbc->count = (i - 1) * ns->local_m + j + total_number + ns->known;
332
333 helper = ns->helper[tbc->count - 1 - total_number];
334 msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED);
335 reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED);
336 reply->header.type = htons (
337 GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED);
338 reply->header.size = htons ((uint16_t) msg_length);
339
340 GNUNET_array_append (ns->msg, ns->n_msg, &reply->header);
341
342 struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send (
343 helper,
344 &reply->header,
345 GNUNET_NO,
346 &clear_msg,
347 tbc);
348
349 GNUNET_array_append (ns->shandle, ns->n_shandle, sh);
350}
351
352
304/** 353/**
305 * Functions with this signature are called whenever a 354 * Functions with this signature are called whenever a
306 * complete message is received by the tokenizer. 355 * complete message is received by the tokenizer.
@@ -319,6 +368,7 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
319 struct TestingSystemCount *tbc = cls; 368 struct TestingSystemCount *tbc = cls;
320 struct NetJailState *ns = tbc->ns; 369 struct NetJailState *ns = tbc->ns;
321 struct HelperMessage *hp_msg; 370 struct HelperMessage *hp_msg;
371 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
322 372
323 if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type)) 373 if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type))
324 { 374 {
@@ -328,11 +378,31 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
328 message->type)) 378 message->type))
329 { 379 {
330 ns->number_of_peers_started++; 380 ns->number_of_peers_started++;
381 if (ns->number_of_peers_started == total_number)
382 {
383 for (int i = 1; i <= ns->known; i++)
384 {
385 send_all_peers_started (0,i, ns);
386 }
387
388 for (int i = 1; i <= ns->global_n; i++)
389 {
390 for (int j = 1; j <= ns->local_m; j++)
391 {
392 send_all_peers_started (i,j, ns);
393 }
394 }
395 ns->number_of_peers_started = 0;
396 }
331 } 397 }
332 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs ( 398 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs (
333 message->type)) 399 message->type))
334 { 400 {
335 ns->number_of_local_test_finished++; 401 ns->number_of_local_test_finished++;
402 if (ns->number_of_local_test_finished == total_number)
403 {
404 GNUNET_TESTING_async_finish (&ns->ac);
405 }
336 } 406 }
337 else 407 else
338 { 408 {
@@ -343,6 +413,12 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
343 hp_msg); 413 hp_msg);
344 } 414 }
345 415
416
417
418
419
420
421
346 return GNUNET_OK; 422 return GNUNET_OK;
347} 423}
348 424
@@ -354,24 +430,21 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
354static void 430static void
355exp_cb (void *cls) 431exp_cb (void *cls)
356{ 432{
357 struct NetJailState *ns = cls; 433 struct TestingSystemCount *tbc = cls;
434
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n"); 435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n");
359 *ns->rv = 1; 436 GNUNET_TESTING_interpreter_fail (tbc->ns->ac.is);
360} 437}
361 438
362 439
363/** 440/**
364 * Function to initialize a init message for the helper. 441 * Function to initialize a init message for the helper.
365 * 442 *
366 * @param m_char The actual node in a namespace. //TODO Change this to unsigned int
367 * @param n_char The actual namespace. //TODO Change this to unsigned int
368 * @param plugin_name Name of the test case plugin the helper will load. 443 * @param plugin_name Name of the test case plugin the helper will load.
369 * 444 *
370 */ 445 */
371static struct GNUNET_CMDS_HelperInit * 446static struct GNUNET_CMDS_HelperInit *
372create_helper_init_msg_ (char *m_char, 447create_helper_init_msg_ (const char *plugin_name)
373 char *n_char,
374 const char *plugin_name)
375{ 448{
376 struct GNUNET_CMDS_HelperInit *msg; 449 struct GNUNET_CMDS_HelperInit *msg;
377 uint16_t plugin_name_len; 450 uint16_t plugin_name_len;
@@ -398,24 +471,49 @@ create_helper_init_msg_ (char *m_char,
398static void 471static void
399start_helper (struct NetJailState *ns, struct 472start_helper (struct NetJailState *ns, struct
400 GNUNET_CONFIGURATION_Handle *config, 473 GNUNET_CONFIGURATION_Handle *config,
401 char *m_char, 474 unsigned int m,
402 char *n_char) 475 unsigned int n)
403{ 476{
404 struct GNUNET_HELPER_Handle *helper; 477 struct GNUNET_HELPER_Handle *helper;
405 struct GNUNET_CMDS_HelperInit *msg; 478 struct GNUNET_CMDS_HelperInit *msg;
406 struct TestingSystemCount *tbc; 479 struct TestingSystemCount *tbc;
407 char *const script_argv[] = { 480 char *m_char, *n_char, *global_n_char, *local_m_char, *known_char, *node_id,
408 NETJAIL_EXEC_SCRIPT, 481 *plugin;
409 m_char, 482 pid_t pid;
410 n_char, 483 unsigned int script_num;
411 GNUNET_OS_get_libexec_binary_path ( 484 struct GNUNET_ShortHashCode *hkey;
412 HELPER_CMDS_BINARY), 485 struct GNUNET_HashCode hc;
413 ns->global_n, 486 struct GNUNET_TESTING_NetjailTopology *topology = ns->topology;
414 ns->local_m, 487 struct GNUNET_TESTING_NetjailNode *node;
415 NULL 488 struct GNUNET_TESTING_NetjailNamespace *namespace;
416 }; 489
417 unsigned int m = atoi (m_char); 490
418 unsigned int n = atoi (n_char); 491 if (0 == n)
492 script_num = m - 1;
493 else
494 script_num = n - 1 + (n - 1) * ns->local_m + m + ns->known;
495 pid = getpid ();
496
497 GNUNET_asprintf (&m_char, "%u", m);
498 GNUNET_asprintf (&n_char, "%u", n);
499 GNUNET_asprintf (&local_m_char, "%u", ns->local_m);
500 GNUNET_asprintf (&global_n_char, "%u",ns->global_n);
501 GNUNET_asprintf (&known_char, "%u",ns->known);
502 GNUNET_asprintf (&node_id, "%06x-%08x\n",
503 pid,
504 script_num);
505
506
507 char *const script_argv[] = {NETJAIL_EXEC_SCRIPT,
508 m_char,
509 n_char,
510 GNUNET_OS_get_libexec_binary_path (
511 HELPER_CMDS_BINARY),
512 global_n_char,
513 local_m_char,
514 node_id,
515 NULL};
516
419 unsigned int helper_check = GNUNET_OS_check_helper_binary ( 517 unsigned int helper_check = GNUNET_OS_check_helper_binary (
420 NETJAIL_EXEC_SCRIPT, 518 NETJAIL_EXEC_SCRIPT,
421 GNUNET_YES, 519 GNUNET_YES,
@@ -423,7 +521,10 @@ start_helper (struct NetJailState *ns, struct
423 521
424 tbc = GNUNET_new (struct TestingSystemCount); 522 tbc = GNUNET_new (struct TestingSystemCount);
425 tbc->ns = ns; 523 tbc->ns = ns;
426 tbc->count = (n - 1) * atoi (ns->local_m) + m; 524 if (0 == n)
525 tbc->count = m;
526 else
527 tbc->count = (n - 1) * ns->local_m + m + ns->known;
427 528
428 GNUNET_CONTAINER_DLL_insert (ns->tbcs_head, ns->tbcs_tail, 529 GNUNET_CONTAINER_DLL_insert (ns->tbcs_head, ns->tbcs_tail,
429 tbc); 530 tbc);
@@ -434,18 +535,17 @@ start_helper (struct NetJailState *ns, struct
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 "No SUID for %s!\n", 536 "No SUID for %s!\n",
436 NETJAIL_EXEC_SCRIPT); 537 NETJAIL_EXEC_SCRIPT);
437 *ns->rv = 1; 538 GNUNET_TESTING_interpreter_fail (ns->ac.is);
438 } 539 }
439 else if (GNUNET_NO == helper_check) 540 else if (GNUNET_NO == helper_check)
440 { 541 {
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 "%s not found!\n", 543 "%s not found!\n",
443 NETJAIL_EXEC_SCRIPT); 544 NETJAIL_EXEC_SCRIPT);
444 *ns->rv = 1; 545 GNUNET_TESTING_interpreter_fail (ns->ac.is);
445 } 546 }
446 GNUNET_array_append (ns->helper, 547
447 ns->n_helper, 548 GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start (
448 GNUNET_HELPER_start (
449 GNUNET_YES, 549 GNUNET_YES,
450 NETJAIL_EXEC_SCRIPT, 550 NETJAIL_EXEC_SCRIPT,
451 script_argv, 551 script_argv,
@@ -455,9 +555,57 @@ start_helper (struct NetJailState *ns, struct
455 555
456 helper = ns->helper[tbc->count - 1]; 556 helper = ns->helper[tbc->count - 1];
457 557
458 msg = create_helper_init_msg_ (m_char, 558 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
459 n_char, 559
460 ns->plugin_name); 560 plugin = topology->plugin;
561
562 if (0 == m)
563 {
564
565 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
566 memcpy (hkey,
567 &hc,
568 sizeof (*hkey));
569 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_globals,
570 hkey))
571 {
572 node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals,
573 hkey);
574 if (NULL != node->plugin)
575 plugin = node->plugin;
576 }
577
578 }
579 else
580 {
581 GNUNET_CRYPTO_hash (&m, sizeof(m), &hc);
582 memcpy (hkey,
583 &hc,
584 sizeof (*hkey));
585 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_namespaces,
586 hkey))
587 {
588 namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces,
589 hkey);
590 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
591 memcpy (hkey,
592 &hc,
593 sizeof (*hkey));
594 if (1 == GNUNET_CONTAINER_multishortmap_contains (namespace->nodes,
595 hkey))
596 {
597 node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes,
598 hkey);
599 if (NULL != node->plugin)
600 plugin = node->plugin;
601 }
602 }
603
604
605 }
606
607 msg = create_helper_init_msg_ (plugin);
608
461 GNUNET_array_append (ns->msg, ns->n_msg, &msg->header); 609 GNUNET_array_append (ns->msg, ns->n_msg, &msg->header);
462 610
463 GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send ( 611 GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send (
@@ -472,7 +620,7 @@ start_helper (struct NetJailState *ns, struct
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "Send handle is NULL!\n"); 621 "Send handle is NULL!\n");
474 GNUNET_free (msg); 622 GNUNET_free (msg);
475 *ns->rv = 1; 623 GNUNET_TESTING_interpreter_fail (ns->ac.is);
476 } 624 }
477} 625}
478 626
@@ -488,93 +636,26 @@ static void
488netjail_exec_run (void *cls, 636netjail_exec_run (void *cls,
489 struct GNUNET_TESTING_Interpreter *is) 637 struct GNUNET_TESTING_Interpreter *is)
490{ 638{
491 char str_m[12];
492 char str_n[12];
493 struct NetJailState *ns = cls; 639 struct NetJailState *ns = cls;
494 struct GNUNET_CONFIGURATION_Handle *config = 640 struct GNUNET_CONFIGURATION_Handle *config =
495 GNUNET_CONFIGURATION_create (); 641 GNUNET_CONFIGURATION_create ();
496 642
497 for (int i = 1; i <= atoi (ns->global_n); i++) 643 for (int i = 1; i <= ns->known; i++)
498 { 644 {
499 for (int j = 1; j <= atoi (ns->local_m); j++) 645 start_helper (ns, config,
500 { 646 i,
501 sprintf (str_n, "%d", i); 647 0);
502 sprintf (str_m, "%d", j);
503 start_helper (ns, config,
504 str_m,
505 str_n);
506 }
507 } 648 }
508}
509
510 649
511/** 650 for (int i = 1; i <= ns->global_n; i++)
512 * This function checks on three different information.
513 *
514 * 1. Did all helpers start. This is only logged.
515 * 2. Did all peer start.
516 * In this case a GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED is send to all peers.
517 * 3. Did all peers finished the test case. In this case interpreter_next will be called.
518 *
519 * => FIXME: must change _completely_.
520 */
521static int
522netjail_start_finish (void *cls,
523 GNUNET_SCHEDULER_TaskCallback cont,
524 void *cont_cls)
525{
526 unsigned int ret = GNUNET_NO;
527 struct NetJailState *ns = cls;
528 unsigned int total_number = atoi (ns->local_m) * atoi (ns->global_n);
529 struct GNUNET_CMDS_ALL_PEERS_STARTED *reply;
530 size_t msg_length;
531 struct GNUNET_HELPER_Handle *helper;
532 struct TestingSystemCount *tbc;
533
534 if (ns->number_of_local_test_finished == total_number)
535 { 651 {
536 ret = GNUNET_YES; 652 for (int j = 1; j <= ns->local_m; j++)
537 cont (cont_cls);
538 }
539
540 if (ns->number_of_testsystems_started == total_number)
541 {
542 ns->number_of_testsystems_started = 0;
543 }
544
545 if (ns->number_of_peers_started == total_number)
546 {
547 for (int i = 1; i <= atoi (ns->global_n); i++)
548 { 653 {
549 for (int j = 1; j <= atoi (ns->local_m); j++) 654 start_helper (ns, config,
550 { 655 j,
551 tbc = GNUNET_new (struct TestingSystemCount); 656 i);
552 tbc->ns = ns;
553 // 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.
554 tbc->count = (i - 1) * atoi (ns->local_m) + j + total_number;
555
556 helper = ns->helper[tbc->count - 1 - total_number];
557 msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED);
558 reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED);
559 reply->header.type = htons (
560 GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED);
561 reply->header.size = htons ((uint16_t) msg_length);
562
563 GNUNET_array_append (ns->msg, ns->n_msg, &reply->header);
564
565 struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send (
566 helper,
567 &reply->header,
568 GNUNET_NO,
569 &clear_msg,
570 tbc);
571
572 GNUNET_array_append (ns->shandle, ns->n_shandle, sh);
573 }
574 } 657 }
575 ns->number_of_peers_started = 0;
576 } 658 }
577 return ret;
578} 659}
579 660
580 661
@@ -582,26 +663,24 @@ netjail_start_finish (void *cls,
582 * Create command. 663 * Create command.
583 * 664 *
584 * @param label Name for the command. 665 * @param label Name for the command.
585 * @param local_m Number of nodes in a network namespace. //TODO make this a unsigned int 666 * @param topology_config Configuration file for the test topology.
586 * @param global_n Number of network namespaces. //TODO make this a unsigned int
587 * @param plugin_name Name of the test case plugin the helper will load.
588 * @param rv Pointer to the return value of the test.
589 * @return command. 667 * @return command.
590 */ 668 */
591struct GNUNET_TESTING_Command 669struct GNUNET_TESTING_Command
592GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label, 670GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label,
593 char *local_m, 671 const char *topology_config)
594 char *global_n,
595 char *plugin_name,
596 unsigned int *rv)
597{ 672{
598 struct NetJailState *ns; 673 struct NetJailState *ns;
599 674
675 struct GNUNET_TESTING_NetjailTopology *topology =
676 GNUNET_TESTING_get_topo_from_file (topology_config);
677
600 ns = GNUNET_new (struct NetJailState); 678 ns = GNUNET_new (struct NetJailState);
601 ns->local_m = local_m; 679 ns->local_m = topology->nodes_m;
602 ns->global_n = global_n; 680 ns->global_n = topology->namespaces_n;
603 ns->plugin_name = plugin_name; 681 ns->known = topology->nodes_x;
604 ns->rv = rv; 682 ns->plugin_name = topology->plugin;
683 ns->topology = topology;
605 684
606 struct GNUNET_TESTING_Command cmd = { 685 struct GNUNET_TESTING_Command cmd = {
607 .cls = ns, 686 .cls = ns,
diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c b/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c
deleted file mode 100644
index d319b73b4..000000000
--- a/src/testing/testing_api_cmd_netjail_start_testsystem_v2.c
+++ /dev/null
@@ -1,723 +0,0 @@
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 * Context for our asynchronous completion.
66 */
67 struct GNUNET_TESTING_AsyncContext ac;
68
69 /**
70 * The complete topology information.
71 */
72 struct GNUNET_TESTING_NetjailTopology *topology;
73
74 /**
75 * Pointer to the return value of the test.
76 *
77 */
78 unsigned int *rv;
79
80 /**
81 * Head of the DLL which stores messages received by the helper.
82 *
83 */
84 struct HelperMessage *hp_messages_head;
85
86 /**
87 * Tail of the DLL which stores messages received by the helper.
88 *
89 */
90 struct HelperMessage *hp_messages_tail;
91
92 /**
93 * Array with handles of helper processes.
94 */
95 struct GNUNET_HELPER_Handle **helper;
96
97 /**
98 * Size of the array NetJailState#helper.
99 *
100 */
101 unsigned int n_helper;
102
103 /**
104 * Number of nodes in a natted subnet.
105 *
106 */
107 unsigned int local_m;
108
109 /**
110 * Number of natted subnets.
111 *
112 */
113 unsigned int global_n;
114
115 /**
116 * Number of global known nodes.
117 *
118 */
119 unsigned int known;
120
121 /**
122 * The send handle for the helper
123 */
124 struct GNUNET_HELPER_SendHandle **shandle;
125
126 /**
127 * Size of the array NetJailState#shandle.
128 *
129 */
130 unsigned int n_shandle;
131
132 /**
133 * The messages send to the helper.
134 */
135 struct GNUNET_MessageHeader **msg;
136
137 /**
138 * Size of the array NetJailState#msg.
139 *
140 */
141 unsigned int n_msg;
142
143 /**
144 * Number of test environments started.
145 *
146 */
147 unsigned int number_of_testsystems_started;
148
149 /**
150 * Number of peers started.
151 *
152 */
153 unsigned int number_of_peers_started;
154
155 /**
156 * Number of local tests finished.
157 *
158 */
159 unsigned int number_of_local_test_finished;
160
161 /**
162 * Name of the test case plugin the helper will load.
163 *
164 */
165 char *plugin_name;
166
167 /**
168 * HEAD of the DLL containing TestingSystemCount.
169 *
170 */
171 struct TestingSystemCount *tbcs_head;
172
173 /**
174 * TAIL of the DLL containing TestingSystemCount.
175 *
176 */
177 struct TestingSystemCount *tbcs_tail;
178};
179
180/**
181 * Struct containing the number of the test environment and the NetJailState which
182 * will be handed to callbacks specific to a test environment.
183 */
184struct TestingSystemCount
185{
186 /**
187 * Kept in a DLL.
188 */
189 struct TestingSystemCount *next;
190
191 /**
192 * Kept in a DLL.
193 */
194 struct TestingSystemCount *prev;
195
196 /**
197 * The number of the test environment.
198 *
199 */
200 unsigned int count;
201
202 /**
203 * Struct to store information handed over to callbacks.
204 *
205 */
206 struct NetJailState *ns;
207};
208
209/**
210* Code to clean up resource this cmd used.
211*
212* @param cls closure
213*/
214static void
215netjail_exec_cleanup (void *cls)
216{
217 struct NetJailState *ns = cls;
218 struct HelperMessage *message_pos;
219 struct TestingSystemCount *tbc_pos;
220
221 while (NULL != (message_pos = ns->hp_messages_head))
222 {
223 GNUNET_CONTAINER_DLL_remove (ns->hp_messages_head,
224 ns->hp_messages_tail,
225 message_pos);
226 GNUNET_free (message_pos);
227 }
228 while (NULL != (tbc_pos = ns->tbcs_head))
229 {
230 GNUNET_CONTAINER_DLL_remove (ns->tbcs_head,
231 ns->tbcs_tail,
232 tbc_pos);
233 GNUNET_free (tbc_pos);
234 }
235 GNUNET_free (ns);
236}
237
238
239/**
240 * This function prepares an array with traits.
241 *
242 */
243static int
244netjail_exec_traits (void *cls,
245 const void **ret,
246 const char *trait,
247 unsigned int index)
248{
249 struct NetJailState *ns = cls;
250 struct GNUNET_HELPER_Handle **helper = ns->helper;
251 struct HelperMessage *hp_messages_head = ns->hp_messages_head;
252
253
254 struct GNUNET_TESTING_Trait traits[] = {
255 {
256 .index = 0,
257 .trait_name = "helper_handles",
258 .ptr = (const void *) helper,
259 },
260 {
261 .index = 1,
262 .trait_name = "hp_msgs_head",
263 .ptr = (const void *) hp_messages_head,
264 },
265 GNUNET_TESTING_trait_end ()
266 };
267
268 return GNUNET_TESTING_get_trait (traits,
269 ret,
270 trait,
271 index);
272}
273
274
275/**
276 * Offer handles to testing cmd helper from trait
277 *
278 * @param cmd command to extract the message from.
279 * @param pt pointer to message.
280 * @return #GNUNET_OK on success.
281 */
282int
283GNUNET_TESTING_get_trait_helper_handles_v2 (const struct
284 GNUNET_TESTING_Command *cmd,
285 struct GNUNET_HELPER_Handle ***
286 helper)
287{
288 return cmd->traits (cmd->cls,
289 (const void **) helper,
290 "helper_handles",
291 (unsigned int) 0);
292}
293
294
295/**
296 * Continuation function from GNUNET_HELPER_send()
297 *
298 * @param cls closure
299 * @param result GNUNET_OK on success,
300 * GNUNET_NO if helper process died
301 * GNUNET_SYSERR during GNUNET_HELPER_stop
302 */
303static void
304clear_msg (void *cls, int result)
305{
306 struct TestingSystemCount *tbc = cls;
307 struct NetJailState *ns = tbc->ns;
308
309 GNUNET_assert (NULL != ns->shandle[tbc->count - 1]);
310 ns->shandle[tbc->count - 1] = NULL;
311 GNUNET_free (ns->msg[tbc->count - 1]);
312 ns->msg[tbc->count - 1] = NULL;
313}
314
315
316/**
317 * Functions with this signature are called whenever a
318 * complete message is received by the tokenizer.
319 *
320 * Do not call GNUNET_SERVER_mst_destroy in callback
321 *
322 * @param cls closure
323 * @param client identification of the client
324 * @param message the actual message
325 *
326 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
327 */
328static int
329helper_mst (void *cls, const struct GNUNET_MessageHeader *message)
330{
331 struct TestingSystemCount *tbc = cls;
332 struct NetJailState *ns = tbc->ns;
333 struct HelperMessage *hp_msg;
334
335 if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type))
336 {
337 ns->number_of_testsystems_started++;
338 }
339 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_PEER_STARTED == ntohs (
340 message->type))
341 {
342 ns->number_of_peers_started++;
343 }
344 else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED == ntohs (
345 message->type))
346 {
347 ns->number_of_local_test_finished++;
348 }
349 else
350 {
351 hp_msg = GNUNET_new (struct HelperMessage);
352 hp_msg->bytes_msg = message->size;
353 memcpy (&hp_msg[1], message, message->size);
354 GNUNET_CONTAINER_DLL_insert (ns->hp_messages_head, ns->hp_messages_tail,
355 hp_msg);
356 }
357
358 return GNUNET_OK;
359}
360
361
362/**
363 * Callback called if there was an exception during execution of the helper.
364 *
365 */
366static void
367exp_cb (void *cls)
368{
369 struct NetJailState *ns = cls;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n");
371 *ns->rv = 1;
372}
373
374
375/**
376 * Function to initialize a init message for the helper.
377 *
378 * @param m_char The actual node in a namespace. //TODO Change this to unsigned int
379 * @param n_char The actual namespace. //TODO Change this to unsigned int
380 * @param plugin_name Name of the test case plugin the helper will load.
381 *
382 */
383static struct GNUNET_CMDS_HelperInit *
384create_helper_init_msg_ (const char *plugin_name)
385{
386 struct GNUNET_CMDS_HelperInit *msg;
387 uint16_t plugin_name_len;
388 uint16_t msg_size;
389
390 GNUNET_assert (NULL != plugin_name);
391 plugin_name_len = strlen (plugin_name);
392 msg_size = sizeof(struct GNUNET_CMDS_HelperInit) + plugin_name_len;
393 msg = GNUNET_malloc (msg_size);
394 msg->header.size = htons (msg_size);
395 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_INIT);
396 msg->plugin_name_size = htons (plugin_name_len);
397 GNUNET_memcpy ((char *) &msg[1],
398 plugin_name,
399 plugin_name_len);
400 return msg;
401}
402
403
404/**
405 * Function which start a single helper process.
406 *
407 */
408static void
409start_helper (struct NetJailState *ns, struct
410 GNUNET_CONFIGURATION_Handle *config,
411 unsigned int m,
412 unsigned int n)
413{
414 struct GNUNET_HELPER_Handle *helper;
415 struct GNUNET_CMDS_HelperInit *msg;
416 struct TestingSystemCount *tbc;
417 char *m_char, *n_char, *global_n_char, *local_m_char, *known_char, *node_id,
418 *plugin;
419 pid_t pid;
420 unsigned int script_num;
421 struct GNUNET_ShortHashCode *hkey;
422 struct GNUNET_HashCode hc;
423 struct GNUNET_TESTING_NetjailTopology *topology = ns->topology;
424 struct GNUNET_TESTING_NetjailNode *node;
425 struct GNUNET_TESTING_NetjailNamespace *namespace;
426
427
428 if (0 == n)
429 script_num = m - 1;
430 else
431 script_num = n - 1 + (n - 1) * ns->local_m + m + ns->known;
432 pid = getpid ();
433
434 GNUNET_asprintf (&m_char, "%u", m);
435 GNUNET_asprintf (&n_char, "%u", n);
436 GNUNET_asprintf (&local_m_char, "%u", ns->local_m);
437 GNUNET_asprintf (&global_n_char, "%u",ns->global_n);
438 GNUNET_asprintf (&known_char, "%u",ns->known);
439 GNUNET_asprintf (&node_id, "%06x-%08x\n",
440 pid,
441 script_num);
442
443
444 char *const script_argv[] = {NETJAIL_EXEC_SCRIPT,
445 m_char,
446 n_char,
447 GNUNET_OS_get_libexec_binary_path (
448 HELPER_CMDS_BINARY),
449 global_n_char,
450 local_m_char,
451 node_id,
452 NULL};
453
454 unsigned int helper_check = GNUNET_OS_check_helper_binary (
455 NETJAIL_EXEC_SCRIPT,
456 GNUNET_YES,
457 NULL);
458
459 tbc = GNUNET_new (struct TestingSystemCount);
460 tbc->ns = ns;
461 if (0 == n)
462 tbc->count = m;
463 else
464 tbc->count = (n - 1) * ns->local_m + m + ns->known;
465
466 GNUNET_CONTAINER_DLL_insert (ns->tbcs_head, ns->tbcs_tail,
467 tbc);
468
469
470 if (GNUNET_NO == helper_check)
471 {
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "No SUID for %s!\n",
474 NETJAIL_EXEC_SCRIPT);
475 *ns->rv = 1;
476 }
477 else if (GNUNET_NO == helper_check)
478 {
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480 "%s not found!\n",
481 NETJAIL_EXEC_SCRIPT);
482 *ns->rv = 1;
483 }
484
485 GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start (
486 GNUNET_YES,
487 NETJAIL_EXEC_SCRIPT,
488 script_argv,
489 &helper_mst,
490 &exp_cb,
491 tbc));
492
493 helper = ns->helper[tbc->count - 1];
494
495 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
496
497 plugin = topology->plugin;
498
499 if (0 == m)
500 {
501
502 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
503 memcpy (hkey,
504 &hc,
505 sizeof (*hkey));
506 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_globals,
507 hkey))
508 {
509 node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals,
510 hkey);
511 if (NULL != node->plugin)
512 plugin = node->plugin;
513 }
514
515 }
516 else
517 {
518 GNUNET_CRYPTO_hash (&m, sizeof(m), &hc);
519 memcpy (hkey,
520 &hc,
521 sizeof (*hkey));
522 if (1 == GNUNET_CONTAINER_multishortmap_contains (topology->map_namespaces,
523 hkey))
524 {
525 namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces,
526 hkey);
527 GNUNET_CRYPTO_hash (&n, sizeof(n), &hc);
528 memcpy (hkey,
529 &hc,
530 sizeof (*hkey));
531 if (1 == GNUNET_CONTAINER_multishortmap_contains (namespace->nodes,
532 hkey))
533 {
534 node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes,
535 hkey);
536 if (NULL != node->plugin)
537 plugin = node->plugin;
538 }
539 }
540
541
542 }
543
544 msg = create_helper_init_msg_ (plugin);
545
546 GNUNET_array_append (ns->msg, ns->n_msg, &msg->header);
547
548 GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send (
549 helper,
550 &msg->header,
551 GNUNET_NO,
552 &clear_msg,
553 tbc));
554
555 if (NULL == ns->shandle[tbc->count - 1])
556 {
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 "Send handle is NULL!\n");
559 GNUNET_free (msg);
560 *ns->rv = 1;
561 }
562}
563
564
565/**
566* This function starts a helper process for each node.
567*
568* @param cls closure.
569* @param cmd CMD being run.
570* @param is interpreter state.
571*/
572static void
573netjail_exec_run (void *cls,
574 struct GNUNET_TESTING_Interpreter *is)
575{
576 struct NetJailState *ns = cls;
577 struct GNUNET_CONFIGURATION_Handle *config =
578 GNUNET_CONFIGURATION_create ();
579
580 for (int i = 1; i <= ns->known; i++)
581 {
582 start_helper (ns, config,
583 i,
584 0);
585 }
586
587 for (int i = 1; i <= ns->global_n; i++)
588 {
589 for (int j = 1; j <= ns->local_m; j++)
590 {
591 start_helper (ns, config,
592 j,
593 i);
594 }
595 }
596}
597
598
599static void
600send_all_peers_started (unsigned int i, unsigned int j, struct NetJailState *ns)
601{
602 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
603 struct GNUNET_CMDS_ALL_PEERS_STARTED *reply;
604 size_t msg_length;
605 struct GNUNET_HELPER_Handle *helper;
606 struct TestingSystemCount *tbc;
607
608 tbc = GNUNET_new (struct TestingSystemCount);
609 tbc->ns = ns;
610 // 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.
611 if (0 == i)
612 tbc->count = j + total_number;
613 else
614 tbc->count = (i - 1) * ns->local_m + j + total_number + ns->known;
615
616 helper = ns->helper[tbc->count - 1 - total_number];
617 msg_length = sizeof(struct GNUNET_CMDS_ALL_PEERS_STARTED);
618 reply = GNUNET_new (struct GNUNET_CMDS_ALL_PEERS_STARTED);
619 reply->header.type = htons (
620 GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED);
621 reply->header.size = htons ((uint16_t) msg_length);
622
623 GNUNET_array_append (ns->msg, ns->n_msg, &reply->header);
624
625 struct GNUNET_HELPER_SendHandle *sh = GNUNET_HELPER_send (
626 helper,
627 &reply->header,
628 GNUNET_NO,
629 &clear_msg,
630 tbc);
631
632 GNUNET_array_append (ns->shandle, ns->n_shandle, sh);
633}
634
635
636/**
637 * This function checks on three different information.
638 *
639 * 1. Did all helpers start. This is only logged.
640 * 2. Did all peer start.
641 * In this case a GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED is send to all peers.
642 * 3. Did all peers finished the test case. In this case interpreter_next will be called.
643 *
644 */
645// FIXME: must change completely!
646static int
647netjail_start_finish (void *cls,
648 GNUNET_SCHEDULER_TaskCallback cont,
649 void *cont_cls)
650{
651 unsigned int ret = GNUNET_NO;
652 struct NetJailState *ns = cls;
653 unsigned int total_number = ns->local_m * ns->global_n + ns->known;
654
655
656 if (ns->number_of_local_test_finished == total_number)
657 {
658 ret = GNUNET_YES;
659 cont (cont_cls);
660 }
661
662 if (ns->number_of_testsystems_started == total_number)
663 {
664 ns->number_of_testsystems_started = 0;
665 }
666
667 if (ns->number_of_peers_started == total_number)
668 {
669 for (int i = 1; i <= ns->known; i++)
670 {
671 send_all_peers_started (0,i, ns);
672 }
673
674 for (int i = 1; i <= ns->global_n; i++)
675 {
676 for (int j = 1; j <= ns->local_m; j++)
677 {
678 send_all_peers_started (i,j, ns);
679 }
680 }
681 ns->number_of_peers_started = 0;
682 }
683 return ret;
684}
685
686
687/**
688 * Create command.
689 *
690 * @param label Name for the command.
691 * @param topology_config Configuration file for the test topology.
692 * @param rv Pointer to the return value of the test.
693 * @return command.
694 */
695struct GNUNET_TESTING_Command
696GNUNET_TESTING_cmd_netjail_start_testing_system_v2 (const char *label,
697 const char *topology_config,
698 unsigned int *rv)
699{
700 struct NetJailState *ns;
701
702 struct GNUNET_TESTING_NetjailTopology *topology =
703 GNUNET_TESTING_get_topo_from_file (topology_config);
704
705 ns = GNUNET_new (struct NetJailState);
706 ns->rv = rv;
707 ns->local_m = topology->nodes_m;
708 ns->global_n = topology->namespaces_n;
709 ns->known = topology->nodes_x;
710 ns->plugin_name = topology->plugin;
711 ns->topology = topology;
712
713 struct GNUNET_TESTING_Command cmd = {
714 .cls = ns,
715 .label = label,
716 .run = &netjail_exec_run,
717 .ac = &ns->ac,
718 .cleanup = &netjail_exec_cleanup,
719 .traits = &netjail_exec_traits
720 };
721
722 return cmd;
723}
diff --git a/src/testing/testing_api_cmd_netjail_start_v2.c b/src/testing/testing_api_cmd_netjail_start_v2.c
deleted file mode 100644
index 7567c2981..000000000
--- a/src/testing/testing_api_cmd_netjail_start_v2.c
+++ /dev/null
@@ -1,209 +0,0 @@
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_netjail_start_v2.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 /**
39 * Context for our asynchronous completion.
40 */
41 struct GNUNET_TESTING_AsyncContext ac;
42
43 // Child Wait handle
44 struct GNUNET_ChildWaitHandle *cwh;
45
46 /**
47 * Configuration file for the test topology.
48 */
49 char *topology_config;
50
51 /**
52 * The process id of the start script.
53 */
54 struct GNUNET_OS_Process *start_proc;
55
56};
57
58
59/**
60 * The cleanup function of this cmd frees resources the cmd allocated.
61 *
62 */
63static void
64netjail_start_cleanup (void *cls)
65{
66 struct NetJailState *ns = cls;
67
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 "netjail_start_cleanup!\n");
70
71 if (NULL != ns->cwh)
72 {
73 GNUNET_wait_child_cancel (ns->cwh);
74 ns->cwh = NULL;
75 }
76 if (NULL != ns->start_proc)
77 {
78 GNUNET_assert (0 ==
79 GNUNET_OS_process_kill (ns->start_proc,
80 SIGKILL));
81 GNUNET_assert (GNUNET_OK ==
82 GNUNET_OS_process_wait (ns->start_proc));
83 GNUNET_OS_process_destroy (ns->start_proc);
84 ns->start_proc = NULL;
85 }
86 GNUNET_free (ns);
87}
88
89
90/**
91 * Callback which will be called if the setup script finished.
92 *
93 */
94static void
95child_completed_callback (void *cls,
96 enum GNUNET_OS_ProcessStatusType type,
97 long unsigned int exit_code)
98{
99 struct NetJailState *ns = cls;
100
101 GNUNET_OS_process_destroy (ns->start_proc);
102 ns->start_proc = NULL;
103 if (0 == exit_code)
104 {
105 GNUNET_TESTING_async_finish (&ns->ac);
106 }
107 else
108 {
109 // FIXME: log status code
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 "Child completed with an error!\n");
112 GNUNET_TESTING_async_fail (&ns->ac);
113 }
114}
115
116
117/**
118* The run method starts the script which setup the network namespaces.
119*
120* @param cls closure.
121* @param is interpreter state.
122*/
123static void
124netjail_start_run (void *cls,
125 struct GNUNET_TESTING_Interpreter *is)
126{
127 struct NetJailState *ns = cls;
128 char pid[15];
129 enum GNUNET_GenericReturnValue helper_check;
130
131 // FIXME: NETJAIL_START_SCRIPT like this is bad,
132 // use location from share/gnunet/ of installed
133 // binary in case libgnunettesting is used as a lib!
134 helper_check = GNUNET_OS_check_helper_binary (
135 NETJAIL_START_SCRIPT,
136 GNUNET_YES,
137 NULL);
138
139 if (GNUNET_NO == helper_check)
140 {
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 "No SUID for %s!\n",
143 NETJAIL_START_SCRIPT);
144 GNUNET_TESTING_interpreter_fail (is);
145 return;
146 }
147 if (GNUNET_SYSERR == helper_check)
148 {
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "%s not found!\n",
151 NETJAIL_START_SCRIPT);
152 GNUNET_TESTING_interpreter_fail (is);
153 return;
154 }
155
156 GNUNET_snprintf (pid,
157 sizeof (pid),
158 "%u",
159 getpid ());
160 {
161 char *const script_argv[] = {
162 NETJAIL_START_SCRIPT,
163 ns->topology_config,
164 pid,
165 NULL
166 };
167
168 ns->start_proc
169 = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
170 NULL,
171 NULL,
172 NULL,
173 NETJAIL_START_SCRIPT,
174 script_argv);
175 }
176 ns->cwh = GNUNET_wait_child (ns->start_proc,
177 &child_completed_callback,
178 ns);
179 GNUNET_break (NULL != ns->cwh);
180}
181
182
183/**
184 * Create command.
185 *
186 * @param label name for command.
187 * @param topology_config Configuration file for the test topology.
188 * @return command.
189 */
190struct GNUNET_TESTING_Command
191GNUNET_TESTING_cmd_netjail_start_v2 (const char *label,
192 char *topology_config)
193{
194 struct NetJailState *ns;
195
196 ns = GNUNET_new (struct NetJailState);
197 ns->topology_config = topology_config;
198 {
199 struct GNUNET_TESTING_Command cmd = {
200 .cls = ns,
201 .label = label,
202 .run = &netjail_start_run,
203 .ac = &ns->ac,
204 .cleanup = &netjail_start_cleanup
205 };
206
207 return cmd;
208 }
209}
diff --git a/src/testing/testing_api_cmd_netjail_stop.c b/src/testing/testing_api_cmd_netjail_stop.c
index 8c78b5e93..5033272a3 100644
--- a/src/testing/testing_api_cmd_netjail_stop.c
+++ b/src/testing/testing_api_cmd_netjail_stop.c
@@ -31,7 +31,7 @@
31#define NETJAIL_STOP_SCRIPT "./../testing/netjail_stop.sh" 31#define NETJAIL_STOP_SCRIPT "./../testing/netjail_stop.sh"
32 32
33// Child Wait handle 33// Child Wait handle
34struct GNUNET_ChildWaitHandle *cwh; 34static struct GNUNET_ChildWaitHandle *cwh;
35 35
36/** 36/**
37 * Struct to hold information for callbacks. 37 * Struct to hold information for callbacks.
@@ -44,11 +44,10 @@ struct NetJailState
44 */ 44 */
45 struct GNUNET_TESTING_AsyncContext ac; 45 struct GNUNET_TESTING_AsyncContext ac;
46 46
47 // Number of local nodes in each namespace. 47 /**
48 char *local_m; 48 * Configuration file for the test topology.
49 49 */
50 // The number of namespaces. 50 char *topology_config;
51 char *global_n;
52 51
53 /** 52 /**
54 * The process id of the start script. 53 * The process id of the start script.
@@ -86,20 +85,6 @@ netjail_stop_cleanup (void *cls)
86 85
87 86
88/** 87/**
89 * Trait function of this cmd does nothing.
90 *
91 */
92static enum GNUNET_GenericReturnValue
93netjail_stop_traits (void *cls,
94 const void **ret,
95 const char *trait,
96 unsigned int index)
97{
98 return GNUNET_NO;
99}
100
101
102/**
103 * Callback which will be called if the setup script finished. 88 * Callback which will be called if the setup script finished.
104 * 89 *
105 */ 90 */
@@ -110,7 +95,7 @@ child_completed_callback (void *cls,
110{ 95{
111 struct NetJailState *ns = cls; 96 struct NetJailState *ns = cls;
112 97
113 cwh = NULL; 98 cwh = NULL; // WTF? globals!?!?!
114 GNUNET_OS_process_destroy (ns->stop_proc); 99 GNUNET_OS_process_destroy (ns->stop_proc);
115 ns->stop_proc = NULL; 100 ns->stop_proc = NULL;
116 if (0 == exit_code) 101 if (0 == exit_code)
@@ -119,7 +104,6 @@ child_completed_callback (void *cls,
119 } 104 }
120 else 105 else
121 { 106 {
122 // FIXME: log exit code!
123 GNUNET_TESTING_async_fail (&ns->ac); 107 GNUNET_TESTING_async_fail (&ns->ac);
124 } 108 }
125} 109}
@@ -129,7 +113,6 @@ child_completed_callback (void *cls,
129* The run method starts the script which deletes the network namespaces. 113* The run method starts the script which deletes the network namespaces.
130* 114*
131* @param cls closure. 115* @param cls closure.
132* @param cmd CMD being run.
133* @param is interpreter state. 116* @param is interpreter state.
134*/ 117*/
135static void 118static void
@@ -137,12 +120,15 @@ netjail_stop_run (void *cls,
137 struct GNUNET_TESTING_Interpreter *is) 120 struct GNUNET_TESTING_Interpreter *is)
138{ 121{
139 struct NetJailState *ns = cls; 122 struct NetJailState *ns = cls;
140 char *const script_argv[] = { 123 char *pid;
141 NETJAIL_STOP_SCRIPT, 124
142 ns->local_m, 125 GNUNET_asprintf (&pid,
143 ns->global_n, 126 "%u",
144 NULL 127 getpid ());
145 }; 128 char *const script_argv[] = {NETJAIL_STOP_SCRIPT,
129 ns->topology_config,
130 pid,
131 NULL};
146 unsigned int helper_check = GNUNET_OS_check_helper_binary ( 132 unsigned int helper_check = GNUNET_OS_check_helper_binary (
147 NETJAIL_STOP_SCRIPT, 133 NETJAIL_STOP_SCRIPT,
148 GNUNET_YES, 134 GNUNET_YES,
@@ -174,28 +160,24 @@ netjail_stop_run (void *cls,
174 &child_completed_callback, 160 &child_completed_callback,
175 ns); 161 ns);
176 GNUNET_break (NULL != cwh); 162 GNUNET_break (NULL != cwh);
177
178} 163}
179 164
180 165
181struct GNUNET_TESTING_Command 166struct GNUNET_TESTING_Command
182GNUNET_TESTING_cmd_netjail_stop (const char *label, 167GNUNET_TESTING_cmd_netjail_stop (const char *label,
183 char *local_m, 168 char *topology_config)
184 char *global_n)
185{ 169{
186 struct NetJailState *ns; 170 struct NetJailState *ns;
187 171
188 ns = GNUNET_new (struct NetJailState); 172 ns = GNUNET_new (struct NetJailState);
189 ns->local_m = local_m; 173 ns->topology_config = topology_config;
190 ns->global_n = global_n;
191 { 174 {
192 struct GNUNET_TESTING_Command cmd = { 175 struct GNUNET_TESTING_Command cmd = {
193 .cls = ns, 176 .cls = ns,
194 .label = label, 177 .label = label,
195 .run = &netjail_stop_run, 178 .run = &netjail_stop_run,
196 .ac = &ns->ac, 179 .ac = &ns->ac,
197 .cleanup = &netjail_stop_cleanup, 180 .cleanup = &netjail_stop_cleanup
198 .traits = &netjail_stop_traits
199 }; 181 };
200 182
201 return cmd; 183 return cmd;
diff --git a/src/testing/testing_api_cmd_netjail_stop_testsystem.c b/src/testing/testing_api_cmd_netjail_stop_testsystem.c
index 04b0e4e94..d3754153d 100644
--- a/src/testing/testing_api_cmd_netjail_stop_testsystem.c
+++ b/src/testing/testing_api_cmd_netjail_stop_testsystem.c
@@ -42,9 +42,15 @@ struct StopHelperState
42 */ 42 */
43 struct GNUNET_HELPER_Handle **helper; 43 struct GNUNET_HELPER_Handle **helper;
44 44
45 char *local_m; 45 unsigned int local_m;
46 46
47 char *global_n; 47 unsigned int global_n;
48
49 /**
50 * Number of global known nodes.
51 *
52 */
53 unsigned int known;
48}; 54};
49 55
50 56
@@ -57,6 +63,7 @@ struct StopHelperState
57static void 63static void
58stop_testing_system_cleanup (void *cls) 64stop_testing_system_cleanup (void *cls)
59{ 65{
66
60} 67}
61 68
62 69
@@ -64,13 +71,13 @@ stop_testing_system_cleanup (void *cls)
64 * Trait function of this cmd does nothing. 71 * Trait function of this cmd does nothing.
65 * 72 *
66 */ 73 */
67static enum GNUNET_GenericReturnValue 74static int
68stop_testing_system_traits (void *cls, 75stop_testing_system_traits (void *cls,
69 const void **ret, 76 const void **ret,
70 const char *trait, 77 const char *trait,
71 unsigned int index) 78 unsigned int index)
72{ 79{
73 return GNUNET_NO; 80 return GNUNET_OK;
74} 81}
75 82
76 83
@@ -88,17 +95,30 @@ stop_testing_system_run (void *cls,
88 struct GNUNET_HELPER_Handle **helper; 95 struct GNUNET_HELPER_Handle **helper;
89 const struct GNUNET_TESTING_Command *start_helper_cmd; 96 const struct GNUNET_TESTING_Command *start_helper_cmd;
90 97
91 start_helper_cmd = GNUNET_TESTING_interpreter_lookup_command ( 98 start_helper_cmd = GNUNET_TESTING_interpreter_lookup_command (NULL,
92 is, 99 shs->
93 shs->helper_start_label); 100 helper_start_label);
94 GNUNET_TESTING_get_trait_helper_handles (start_helper_cmd, 101 GNUNET_TESTING_get_trait_helper_handles (start_helper_cmd,
95 &helper); 102 &helper);
96 103
97 for (int i = 1; i <= atoi (shs->global_n); i++) 104 for (int i = 1; i <= shs->known; i++)
105 {
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "i: %u\n",
108 i);
109 GNUNET_HELPER_stop (helper[i - 1],
110 GNUNET_YES);
111 }
112
113 for (int i = 1; i <= shs->global_n; i++)
98 { 114 {
99 for (int j = 1; j <= atoi (shs->local_m); j++) 115 for (int j = 1; j <= shs->local_m; j++)
100 { 116 {
101 GNUNET_HELPER_stop (helper[(i - 1) * atoi (shs->local_m) + j - 1], 117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "i: %u j: %u\n",
119 i,
120 j);
121 GNUNET_HELPER_stop (helper[(i - 1) * shs->local_m + j + shs->known - 1],
102 GNUNET_YES); 122 GNUNET_YES);
103 } 123 }
104 } 124 }
@@ -107,34 +127,35 @@ stop_testing_system_run (void *cls,
107 127
108/** 128/**
109 * Create command. 129 * Create command.
110 * @param helper_start_label label of the cmd to start the test system. 130 *
111 * @param label name for command. 131 * @param label name for command.
112 * @param . 132 * @param helper_start_label label of the cmd to start the test system.
113 * @param local_m Number of nodes in a network namespace. //TODO make this a unsigned int 133 * @param topology_config Configuration file for the test topology.
114 * @param global_n Number of network namespaces. //TODO make this a unsigned int
115 * @return command. 134 * @return command.
116 */ 135 */
117struct GNUNET_TESTING_Command 136struct GNUNET_TESTING_Command
118GNUNET_TESTING_cmd_stop_testing_system (const char *label, 137GNUNET_TESTING_cmd_stop_testing_system (const char *label,
119 const char *helper_start_label, 138 const char *helper_start_label,
120 char *local_m, 139 const char *topology_config)
121 char *global_n)
122{ 140{
123 struct StopHelperState *shs; 141 struct StopHelperState *shs;
124 142
143 struct GNUNET_TESTING_NetjailTopology *topology =
144 GNUNET_TESTING_get_topo_from_file (topology_config);
145
125 shs = GNUNET_new (struct StopHelperState); 146 shs = GNUNET_new (struct StopHelperState);
126 shs->helper_start_label = helper_start_label; 147 shs->helper_start_label = helper_start_label;
127 shs->local_m = local_m; 148 shs->local_m = topology->nodes_m;
128 shs->global_n = global_n; 149 shs->global_n = topology->namespaces_n;
129 { 150 shs->known = topology->nodes_x;
130 struct GNUNET_TESTING_Command cmd = { 151
131 .cls = shs, 152 struct GNUNET_TESTING_Command cmd = {
132 .label = label, 153 .cls = shs,
133 .run = &stop_testing_system_run, 154 .label = label,
134 .cleanup = &stop_testing_system_cleanup, 155 .run = &stop_testing_system_run,
135 .traits = &stop_testing_system_traits 156 .cleanup = &stop_testing_system_cleanup,
136 }; 157 .traits = &stop_testing_system_traits
137 158 };
138 return cmd; 159
139 } 160 return cmd;
140} 161}
diff --git a/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c b/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c
deleted file mode 100644
index 65eb85d9a..000000000
--- a/src/testing/testing_api_cmd_netjail_stop_testsystem_v2.c
+++ /dev/null
@@ -1,141 +0,0 @@
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*/
56static void
57stop_testing_system_cleanup (void *cls)
58{
59
60}
61
62
63/**
64 * Trait function of this cmd does nothing.
65 *
66 */
67static enum GNUNET_GenericReturnValue
68stop_testing_system_traits (void *cls,
69 const void **ret,
70 const char *trait,
71 unsigned int index)
72{
73 return GNUNET_NO;
74}
75
76
77/**
78* This function stops the helper process for each node.
79*
80* @param cls closure.
81* @param is interpreter state.
82*/
83static void
84stop_testing_system_run (void *cls,
85 struct GNUNET_TESTING_Interpreter *is)
86{
87 struct StopHelperState *shs = cls;
88 struct GNUNET_HELPER_Handle **helper;
89 const struct GNUNET_TESTING_Command *start_helper_cmd;
90
91 start_helper_cmd = GNUNET_TESTING_interpreter_lookup_command (
92 is,
93 shs->helper_start_label);
94 GNUNET_TESTING_get_trait_helper_handles (start_helper_cmd,
95 &helper);
96
97 for (int i = 1; i <= shs->global_n; i++)
98 {
99 for (int j = 1; j <= shs->local_m; j++)
100 {
101 GNUNET_HELPER_stop (helper[(i - 1) * shs->local_m + j - 1],
102 GNUNET_YES);
103 }
104 }
105}
106
107
108/**
109 * Create command.
110 *
111 * @param label name for command.
112 * @param helper_start_label label of the cmd to start the test system.
113 * @param topology_config Configuration file for the test topology.
114 * @return command.
115 */
116struct GNUNET_TESTING_Command
117GNUNET_TESTING_cmd_stop_testing_system_v2 (const char *label,
118 const char *helper_start_label,
119 const char *topology_config)
120{
121 struct StopHelperState *shs;
122
123 struct GNUNET_TESTING_NetjailTopology *topology =
124 GNUNET_TESTING_get_topo_from_file (topology_config);
125
126 shs = GNUNET_new (struct StopHelperState);
127 shs->helper_start_label = helper_start_label;
128 shs->local_m = topology->nodes_m;
129 shs->global_n = topology->namespaces_n;
130 {
131 struct GNUNET_TESTING_Command cmd = {
132 .cls = shs,
133 .label = label,
134 .run = &stop_testing_system_run,
135 .cleanup = &stop_testing_system_cleanup,
136 .traits = &stop_testing_system_traits
137 };
138
139 return cmd;
140 }
141}
diff --git a/src/testing/testing_api_cmd_netjail_stop_v2.c b/src/testing/testing_api_cmd_netjail_stop_v2.c
deleted file mode 100644
index a0e2657cb..000000000
--- a/src/testing/testing_api_cmd_netjail_stop_v2.c
+++ /dev/null
@@ -1,185 +0,0 @@
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_netjail_stop_v2.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
34static struct GNUNET_ChildWaitHandle *cwh;
35
36/**
37 * Struct to hold information for callbacks.
38 *
39 */
40struct NetJailState
41{
42 /**
43 * Context for our asynchronous completion.
44 */
45 struct GNUNET_TESTING_AsyncContext ac;
46
47 /**
48 * Configuration file for the test topology.
49 */
50 char *topology_config;
51
52 /**
53 * The process id of the start script.
54 */
55 struct GNUNET_OS_Process *stop_proc;
56
57};
58
59
60/**
61 * The cleanup function of this cmd frees resources the cmd allocated.
62 *
63 */
64static void
65netjail_stop_cleanup (void *cls)
66{
67 struct NetJailState *ns = cls;
68
69 if (NULL != cwh)
70 {
71 GNUNET_wait_child_cancel (cwh);
72 cwh = NULL;
73 }
74 if (NULL != ns->stop_proc)
75 {
76 GNUNET_assert (0 ==
77 GNUNET_OS_process_kill (ns->stop_proc,
78 SIGKILL));
79 GNUNET_assert (GNUNET_OK ==
80 GNUNET_OS_process_wait (ns->stop_proc));
81 GNUNET_OS_process_destroy (ns->stop_proc);
82 ns->stop_proc = NULL;
83 }
84}
85
86
87/**
88 * Callback which will be called if the setup script finished.
89 *
90 */
91static void
92child_completed_callback (void *cls,
93 enum GNUNET_OS_ProcessStatusType type,
94 long unsigned int exit_code)
95{
96 struct NetJailState *ns = cls;
97
98 cwh = NULL; // WTF? globals!?!?!
99 GNUNET_OS_process_destroy (ns->stop_proc);
100 ns->stop_proc = NULL;
101 if (0 == exit_code)
102 {
103 GNUNET_TESTING_async_finish (&ns->ac);
104 }
105 else
106 {
107 GNUNET_TESTING_async_fail (&ns->ac);
108 }
109}
110
111
112/**
113* The run method starts the script which deletes the network namespaces.
114*
115* @param cls closure.
116* @param is interpreter state.
117*/
118static void
119netjail_stop_run (void *cls,
120 struct GNUNET_TESTING_Interpreter *is)
121{
122 struct NetJailState *ns = cls;
123 char *pid;
124
125 GNUNET_asprintf (&pid,
126 "%u",
127 getpid ());
128 char *const script_argv[] = {NETJAIL_STOP_SCRIPT,
129 ns->topology_config,
130 pid,
131 NULL};
132 unsigned int helper_check = GNUNET_OS_check_helper_binary (
133 NETJAIL_STOP_SCRIPT,
134 GNUNET_YES,
135 NULL);
136
137 if (GNUNET_NO == helper_check)
138 {
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 "No SUID for %s!\n",
141 NETJAIL_STOP_SCRIPT);
142 GNUNET_TESTING_interpreter_fail (is);
143 }
144 else if (GNUNET_NO == helper_check)
145 {
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 "%s not found!\n",
148 NETJAIL_STOP_SCRIPT);
149 GNUNET_TESTING_interpreter_fail (is);
150 }
151
152 ns->stop_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR,
153 NULL,
154 NULL,
155 NULL,
156 NETJAIL_STOP_SCRIPT,
157 script_argv);
158
159 cwh = GNUNET_wait_child (ns->stop_proc,
160 &child_completed_callback,
161 ns);
162 GNUNET_break (NULL != cwh);
163}
164
165
166struct GNUNET_TESTING_Command
167GNUNET_TESTING_cmd_netjail_stop_v2 (const char *label,
168 char *topology_config)
169{
170 struct NetJailState *ns;
171
172 ns = GNUNET_new (struct NetJailState);
173 ns->topology_config = topology_config;
174 {
175 struct GNUNET_TESTING_Command cmd = {
176 .cls = ns,
177 .label = label,
178 .run = &netjail_stop_run,
179 .ac = &ns->ac,
180 .cleanup = &netjail_stop_cleanup
181 };
182
183 return cmd;
184 }
185}
diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c
index e0cb3fcda..3727d2543 100644
--- a/src/testing/testing_api_loop.c
+++ b/src/testing/testing_api_loop.c
@@ -313,8 +313,12 @@ interpreter_run (void *cls)
313 } 313 }
314 cmd->run (cmd->cls, 314 cmd->run (cmd->cls,
315 is); 315 is);
316 if ( (cmd->asynchronous_finish) && 316 if (NULL == cmd->ac)
317 (NULL != cmd->ac->cont) ) 317 {
318 interpreter_next (is);
319 }
320 else if ( (cmd->asynchronous_finish) &&
321 (NULL != cmd->ac->cont) )
318 { 322 {
319 cmd->ac->cont = NULL; 323 cmd->ac->cont = NULL;
320 interpreter_next (is); 324 interpreter_next (is);
diff --git a/src/testing/testing_cmds.h b/src/testing/testing_cmds.h
index 7a5860aea..09e4f2dcb 100644
--- a/src/testing/testing_cmds.h
+++ b/src/testing/testing_cmds.h
@@ -83,6 +83,8 @@ struct GNUNET_CMDS_LOCAL_FINISHED
83 * Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED 83 * Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED
84 */ 84 */
85 struct GNUNET_MessageHeader header; 85 struct GNUNET_MessageHeader header;
86
87 enum GNUNET_GenericReturnValue result;
86}; 88};
87 89
88GNUNET_NETWORK_STRUCT_END 90GNUNET_NETWORK_STRUCT_END