aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/dhtu/test_dhtu_ip.c8
-rw-r--r--src/include/gnunet_testing_ng_lib.h86
-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
-rw-r--r--src/transport/Makefile.am48
-rw-r--r--src/transport/test_transport_plugin_cmd_simple_send.c192
-rw-r--r--src/transport/test_transport_plugin_cmd_simple_send_v2.c235
-rw-r--r--src/transport/test_transport_plugin_cmd_udp_backchannel.c175
-rw-r--r--src/transport/test_transport_port_forward.c85
-rw-r--r--src/transport/test_transport_simple_send.c86
-rwxr-xr-xsrc/transport/test_transport_simple_send.sh2
-rw-r--r--src/transport/test_transport_simple_send_v2.c83
-rwxr-xr-xsrc/transport/test_transport_simple_send_v2.sh2
-rw-r--r--src/transport/test_transport_start_with_config.c59
-rw-r--r--src/transport/transport-testing-cmds.h201
-rw-r--r--src/transport/transport_api_cmd_connecting_peers.c482
-rw-r--r--src/transport/transport_api_cmd_connecting_peers_v2.c237
-rw-r--r--src/transport/transport_api_cmd_connecting_peers_v3.c496
-rw-r--r--src/transport/transport_api_cmd_send_simple.c20
-rw-r--r--src/transport/transport_api_cmd_send_simple_v2.c156
-rw-r--r--src/transport/transport_api_cmd_start_peer.c78
-rw-r--r--src/transport/transport_api_cmd_start_peer_v2.c605
-rw-r--r--src/transport/transport_api_cmd_start_peer_v3.c617
-rw-r--r--src/util/child_management.c19
44 files changed, 1441 insertions, 5155 deletions
diff --git a/src/dhtu/test_dhtu_ip.c b/src/dhtu/test_dhtu_ip.c
index eb886e994..c9528053f 100644
--- a/src/dhtu/test_dhtu_ip.c
+++ b/src/dhtu/test_dhtu_ip.c
@@ -37,10 +37,10 @@ main (int argc,
37 char *const *argv) 37 char *const *argv)
38{ 38{
39 struct GNUNET_TESTING_Command commands[] = { 39 struct GNUNET_TESTING_Command commands[] = {
40 GNUNET_TESTING_cmd_netjail_start_v2 ("netjail-start", 40 GNUNET_TESTING_cmd_netjail_start ("netjail-start",
41 CONFIG_FILE), 41 CONFIG_FILE),
42 GNUNET_TESTING_cmd_netjail_stop_v2 ("netjail-stop", 42 GNUNET_TESTING_cmd_netjail_stop ("netjail-stop",
43 CONFIG_FILE), 43 CONFIG_FILE),
44 GNUNET_TESTING_cmd_end () 44 GNUNET_TESTING_cmd_end ()
45 }; 45 };
46 46
diff --git a/src/include/gnunet_testing_ng_lib.h b/src/include/gnunet_testing_ng_lib.h
index 0cab64504..adf5453e1 100644
--- a/src/include/gnunet_testing_ng_lib.h
+++ b/src/include/gnunet_testing_ng_lib.h
@@ -1054,40 +1054,12 @@ GNUNET_TESTING_cmd_system_destroy (const char *label,
1054 * Create command. 1054 * Create command.
1055 * 1055 *
1056 * @param label name for command. 1056 * @param label name for command.
1057 * @param local_m Number of local nodes in each namespace.
1058 * @param global_n The number of namespaces.
1059 * @return command.
1060 */
1061struct GNUNET_TESTING_Command
1062GNUNET_TESTING_cmd_netjail_start (const char *label,
1063 char *local_m,
1064 char *global_n);
1065
1066/**
1067 * Create command.
1068 *
1069 * @param label name for command.
1070 * @param topology_config Configuration file for the test topology. 1057 * @param topology_config Configuration file for the test topology.
1071 * @return command. 1058 * @return command.
1072 */ 1059 */
1073struct GNUNET_TESTING_Command 1060struct GNUNET_TESTING_Command
1074GNUNET_TESTING_cmd_netjail_start_v2 (const char *label, 1061GNUNET_TESTING_cmd_netjail_start (const char *label,
1075 char *topology_config); 1062 char *topology_config);
1076
1077
1078/**
1079 * Create command.
1080 *
1081 * @param label name for command.
1082 * @param binaryname to exec.
1083 * @return command.
1084 */
1085struct GNUNET_TESTING_Command
1086GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label,
1087 char *local_m,
1088 char *global_n,
1089 char *plugin_name,
1090 unsigned int *rv);
1091 1063
1092 1064
1093/** 1065/**
@@ -1099,22 +1071,20 @@ GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label,
1099 * @return command. 1071 * @return command.
1100 */ 1072 */
1101struct GNUNET_TESTING_Command 1073struct GNUNET_TESTING_Command
1102GNUNET_TESTING_cmd_netjail_start_testing_system_v2 (const char *label, 1074GNUNET_TESTING_cmd_netjail_start_testing_system (const char *label,
1103 const char *topology_config, 1075 const char *topology_config);
1104 unsigned int *rv);
1105 1076
1106 1077
1107/** 1078/**
1108 * Create command. 1079 * Create command.
1109 * 1080 *
1110 * @param label name for command. 1081 * @param label name for command.
1111 * @param binaryname to stop. 1082 * @param topology_config Configuration file for the test topology.
1112 * @return command. 1083 * @return command.
1113 */ 1084 */
1114struct GNUNET_TESTING_Command 1085struct GNUNET_TESTING_Command
1115GNUNET_TESTING_cmd_netjail_stop (const char *label, 1086GNUNET_TESTING_cmd_netjail_stop (const char *label,
1116 char *local_m, 1087 char *topology_config);
1117 char *global_n);
1118 1088
1119 1089
1120/** 1090/**
@@ -1125,34 +1095,31 @@ GNUNET_TESTING_cmd_netjail_stop (const char *label,
1125 * @return command. 1095 * @return command.
1126 */ 1096 */
1127struct GNUNET_TESTING_Command 1097struct GNUNET_TESTING_Command
1128GNUNET_TESTING_cmd_netjail_stop_v2 (const char *label,
1129 char *topology_config);
1130
1131
1132struct GNUNET_TESTING_Command
1133GNUNET_TESTING_cmd_stop_testing_system (const char *label, 1098GNUNET_TESTING_cmd_stop_testing_system (const char *label,
1134 const char *helper_start_label, 1099 const char *helper_start_label,
1135 char *local_m, 1100 const char *topology_config);
1136 char *global_n); 1101
1137 1102
1138/** 1103/**
1139 * Create command. 1104 * Create a GNUNET_CMDS_LOCAL_FINISHED message.
1140 * 1105 *
1141 * @param label name for command. 1106 * @param rv The result of the local test as GNUNET_GenericReturnValue.
1142 * @param topology_config Configuration file for the test topology. 1107 * @return The GNUNET_CMDS_LOCAL_FINISHED message.
1143 * @return command. 1108*/
1144 */ 1109struct GNUNET_MessageHeader *
1145struct GNUNET_TESTING_Command 1110GNUNET_TESTING_send_local_test_finished_msg (enum GNUNET_GenericReturnValue rv);
1146GNUNET_TESTING_cmd_stop_testing_system_v2 (const char *label,
1147 const char *helper_start_label,
1148 const char *topology_config);
1149 1111
1150 1112
1151// FIXME: document! 1113/**
1152enum GNUNET_GenericReturnValue 1114 * Function to get the trait with the async context.
1153GNUNET_TESTING_get_trait_helper_handles (const struct 1115 *
1154 GNUNET_TESTING_Command *cmd, 1116 * @param[out] ac GNUNET_TESTING_AsyncContext.
1155 struct GNUNET_HELPER_Handle ***helper); 1117 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
1118 */
1119int
1120GNUNET_TESTING_get_trait_async_context (
1121 const struct GNUNET_TESTING_Command *cmd,
1122 struct GNUNET_TESTING_AsyncContext **ac);
1156 1123
1157 1124
1158/** 1125/**
@@ -1163,7 +1130,7 @@ GNUNET_TESTING_get_trait_helper_handles (const struct
1163 * @return #GNUNET_OK on success. 1130 * @return #GNUNET_OK on success.
1164 */ 1131 */
1165enum GNUNET_GenericReturnValue 1132enum GNUNET_GenericReturnValue
1166GNUNET_TESTING_get_trait_helper_handles_v2 ( 1133GNUNET_TESTING_get_trait_helper_handles (
1167 const struct GNUNET_TESTING_Command *cmd, 1134 const struct GNUNET_TESTING_Command *cmd,
1168 struct GNUNET_HELPER_Handle ***helper); 1135 struct GNUNET_HELPER_Handle ***helper);
1169 1136
@@ -1176,8 +1143,7 @@ GNUNET_TESTING_cmd_block_until_all_peers_started (
1176 1143
1177struct GNUNET_TESTING_Command 1144struct GNUNET_TESTING_Command
1178GNUNET_TESTING_cmd_block_until_external_trigger ( 1145GNUNET_TESTING_cmd_block_until_external_trigger (
1179 const char *label, 1146 const char *label);
1180 unsigned int *stop_blocking);
1181 1147
1182struct GNUNET_TESTING_Command 1148struct GNUNET_TESTING_Command
1183GNUNET_TESTING_cmd_send_peer_ready (const char *label, 1149GNUNET_TESTING_cmd_send_peer_ready (const char *label,
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
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am
index a539f3f25..0df422976 100644
--- a/src/transport/Makefile.am
+++ b/src/transport/Makefile.am
@@ -159,14 +159,9 @@ libgnunettransporttesting_la_LDFLAGS = \
159 159
160libgnunettransporttesting2_la_SOURCES = \ 160libgnunettransporttesting2_la_SOURCES = \
161 transport_api_cmd_connecting_peers.c \ 161 transport_api_cmd_connecting_peers.c \
162 transport_api_cmd_connecting_peers_v2.c \
163 transport_api_cmd_connecting_peers_v3.c \
164 transport_api_cmd_start_peer.c \ 162 transport_api_cmd_start_peer.c \
165 transport_api_cmd_start_peer_v2.c \
166 transport_api_cmd_start_peer_v3.c \
167 transport_api_cmd_stop_peer.c \ 163 transport_api_cmd_stop_peer.c \
168 transport_api_cmd_send_simple.c \ 164 transport_api_cmd_send_simple.c \
169 transport_api_cmd_send_simple_v2.c \
170 transport-testing2.c transport-testing2.h \ 165 transport-testing2.c transport-testing2.h \
171 transport-testing-cmds.h \ 166 transport-testing-cmds.h \
172 transport-testing-filenames2.c \ 167 transport-testing-filenames2.c \
@@ -394,7 +389,6 @@ plugin_LTLIBRARIES = \
394 $(WLAN_PLUGIN_LA) \ 389 $(WLAN_PLUGIN_LA) \
395 $(BT_PLUGIN_LA) \ 390 $(BT_PLUGIN_LA) \
396 libgnunet_test_transport_plugin_cmd_simple_send.la \ 391 libgnunet_test_transport_plugin_cmd_simple_send.la \
397 libgnunet_test_transport_plugin_cmd_simple_send_v2.la \
398 libgnunet_test_transport_plugin_cmd_udp_backchannel.la 392 libgnunet_test_transport_plugin_cmd_udp_backchannel.la
399 393
400libgnunet_test_transport_plugin_cmd_udp_backchannel_la_SOURCES = \ 394libgnunet_test_transport_plugin_cmd_udp_backchannel_la_SOURCES = \
@@ -431,23 +425,6 @@ libgnunet_test_transport_plugin_cmd_simple_send_la_LIBADD = \
431libgnunet_test_transport_plugin_cmd_simple_send_la_LDFLAGS = \ 425libgnunet_test_transport_plugin_cmd_simple_send_la_LDFLAGS = \
432 $(GN_PLUGIN_LDFLAGS) 426 $(GN_PLUGIN_LDFLAGS)
433 427
434libgnunet_test_transport_plugin_cmd_simple_send_v2_la_SOURCES = \
435 test_transport_plugin_cmd_simple_send_v2.c
436libgnunet_test_transport_plugin_cmd_simple_send_v2_la_LIBADD = \
437 libgnunettransporttesting2.la \
438 libgnunettransportapplication.la \
439 libgnunettransportcore.la \
440 $(top_builddir)/src/testing/libgnunettesting.la \
441 $(top_builddir)/src/peerstore/libgnunetpeerstore.la \
442 $(top_builddir)/src/statistics/libgnunetstatistics.la \
443 $(top_builddir)/src/hello/libgnunethello.la \
444 $(top_builddir)/src/ats/libgnunetats.la \
445 $(top_builddir)/src/arm/libgnunetarm.la \
446 $(top_builddir)/src/util/libgnunetutil.la \
447 $(LTLIBINTL)
448libgnunet_test_transport_plugin_cmd_simple_send_v2_la_LDFLAGS = \
449 $(GN_PLUGIN_LDFLAGS)
450
451if HAVE_EXPERIMENTAL 428if HAVE_EXPERIMENTAL
452plugin_LTLIBRARIES += \ 429plugin_LTLIBRARIES += \
453 libgnunet_plugin_transport_udp.la 430 libgnunet_plugin_transport_udp.la
@@ -593,8 +570,6 @@ libgnunet_plugin_transport_https_server_la_CFLAGS = \
593 $(MHD_CFLAGS) $(AM_CFLAGS) -DBUILD_HTTPS 570 $(MHD_CFLAGS) $(AM_CFLAGS) -DBUILD_HTTPS
594 571
595check_PROGRAMS = \ 572check_PROGRAMS = \
596 test_transport_simple_send \
597 test_transport_simple_send_v2 \
598 test_transport_start_with_config \ 573 test_transport_start_with_config \
599 test_transport_address_switch_tcp \ 574 test_transport_address_switch_tcp \
600 test_transport_testing_startstop \ 575 test_transport_testing_startstop \
@@ -743,8 +718,6 @@ test_transport_api_slow_ats
743endif 718endif
744if HAVE_EXPERIMENTAL 719if HAVE_EXPERIMENTAL
745TESTS += \ 720TESTS += \
746 test_transport_simple_send \
747 test_transport_simple_send_v2 \
748 test_transport_start_with_config \ 721 test_transport_start_with_config \
749 test_transport_address_switch_udp \ 722 test_transport_address_switch_udp \
750 test_plugin_udp \ 723 test_plugin_udp \
@@ -765,7 +738,6 @@ endif
765 738
766check_SCRIPTS= \ 739check_SCRIPTS= \
767 test_transport_simple_send.sh \ 740 test_transport_simple_send.sh \
768 test_transport_simple_send_v2.sh \
769 test_transport_udp_backchannel.sh 741 test_transport_udp_backchannel.sh
770 742
771test_transport_start_with_config_SOURCES = \ 743test_transport_start_with_config_SOURCES = \
@@ -778,26 +750,6 @@ test_transport_start_with_config_LDADD = \
778 libgnunettransportcore.la \ 750 libgnunettransportcore.la \
779 libgnunettransporttesting2.la 751 libgnunettransporttesting2.la
780 752
781test_transport_simple_send_SOURCES = \
782 test_transport_simple_send.c
783test_transport_simple_send_LDADD = \
784 $(top_builddir)/src/testing/libgnunettesting.la \
785 $(top_builddir)/src/util/libgnunetutil.la \
786 $(top_builddir)/src/testbed/libgnunettestbed.la \
787 $(top_builddir)/src/hello/libgnunethello.la \
788 libgnunettransportcore.la \
789 libgnunettransporttesting2.la
790
791test_transport_simple_send_v2_SOURCES = \
792 test_transport_simple_send_v2.c
793test_transport_simple_send_v2_LDADD = \
794 $(top_builddir)/src/testing/libgnunettesting.la \
795 $(top_builddir)/src/util/libgnunetutil.la \
796 $(top_builddir)/src/testbed/libgnunettestbed.la \
797 $(top_builddir)/src/hello/libgnunethello.la \
798 libgnunettransportcore.la \
799 libgnunettransporttesting2.la
800
801test_transport_testing_startstop_SOURCES = \ 753test_transport_testing_startstop_SOURCES = \
802 test_transport_testing_startstop.c 754 test_transport_testing_startstop.c
803test_transport_testing_startstop_LDADD = \ 755test_transport_testing_startstop_LDADD = \
diff --git a/src/transport/test_transport_plugin_cmd_simple_send.c b/src/transport/test_transport_plugin_cmd_simple_send.c
index 21167415d..5384bf24d 100644
--- a/src/transport/test_transport_plugin_cmd_simple_send.c
+++ b/src/transport/test_transport_plugin_cmd_simple_send.c
@@ -37,28 +37,34 @@
37 37
38#define BASE_DIR "testdir" 38#define BASE_DIR "testdir"
39 39
40/** 40#define TOPOLOGY_CONFIG "test_transport_simple_send_topo.conf"
41 * The name for a specific test environment directory.
42 *
43 */
44char *testdir;
45 41
46/** 42struct TestState
47 * The name for the configuration file of the specific node. 43{
48 * 44 /**
49 */ 45 * Callback to write messages to the master loop.
50char *cfgname; 46 *
47 */
48 TESTING_CMD_HELPER_write_cb write_message;
51 49
52/** 50 /**
53 * Flag indicating if all peers have been started. 51 * The name for a specific test environment directory.
54 * 52 *
55 */ 53 */
56unsigned int are_all_peers_started; 54 char *testdir;
57 55
58/** 56 /**
59 * Flag indicating a received message. 57 * The name for the configuration file of the specific node.
60 */ 58 *
61unsigned int message_received; 59 */
60 char *cfgname;
61};
62
63static struct GNUNET_TESTING_Command block_send;
64
65static struct GNUNET_TESTING_Command block_receive;
66
67static struct GNUNET_TESTING_Command connect_peers;
62 68
63 69
64/** 70/**
@@ -83,7 +89,14 @@ static void
83handle_test (void *cls, 89handle_test (void *cls,
84 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) 90 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
85{ 91{
86 message_received = GNUNET_YES; 92 struct GNUNET_TESTING_AsyncContext *ac;
93
94 GNUNET_TESTING_get_trait_async_context (&block_receive,
95 &ac);
96 if ((NULL == ac) || (NULL == ac->cont))
97 GNUNET_TESTING_async_fail (ac);
98 else
99 GNUNET_TESTING_async_finish (ac);
87} 100}
88 101
89 102
@@ -94,7 +107,62 @@ handle_test (void *cls,
94static void 107static void
95all_peers_started () 108all_peers_started ()
96{ 109{
97 are_all_peers_started = GNUNET_YES; 110 struct GNUNET_TESTING_AsyncContext *ac;
111
112 GNUNET_TESTING_get_trait_async_context (&block_send,
113 &ac);
114 if ((NULL == ac) || (NULL == ac->cont))
115 GNUNET_TESTING_async_fail (ac);
116 else
117 GNUNET_TESTING_async_finish (ac);
118}
119
120
121/**
122 * Function called with the final result of the test.
123 *
124 * @param cls the `struct MainParams`
125 * @param rv #GNUNET_OK if the test passed
126 */
127static void
128handle_result (void *cls,
129 enum GNUNET_GenericReturnValue rv)
130{
131 struct TestState *ts = cls;
132 struct GNUNET_MessageHeader *reply;
133
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Local test exits with status %d\n",
136 rv);
137 reply = GNUNET_TESTING_send_local_test_finished_msg (rv);
138
139 ts->write_message (reply,
140 ntohs (reply->size));
141 GNUNET_free (ts->testdir);
142 GNUNET_free (ts->cfgname);
143 GNUNET_free (ts);
144}
145
146
147/**
148 * Callback from start peer cmd for signaling a peer got connected.
149 *
150 */
151static void *
152notify_connect (void *cls,
153 const struct GNUNET_PeerIdentity *peer,
154 struct GNUNET_MQ_Handle *mq)
155{
156 struct ConnectPeersState *cps;
157
158 GNUNET_TESTING_get_trait_connect_peer_state (&connect_peers,
159 &cps);
160 void *ret = NULL;
161
162 cps->notify_connect (cps,
163 peer,
164 mq);
165 return ret;
98} 166}
99 167
100 168
@@ -116,24 +184,46 @@ start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
116 char *local_m) 184 char *local_m)
117{ 185{
118 186
119 GNUNET_asprintf (&cfgname, 187 unsigned int n_int;
120 "test_transport_api2_tcp_node%s.conf", 188 unsigned int m_int;
121 "1"); 189 unsigned int local_m_int;
190 unsigned int num;
191 struct TestState *ts = GNUNET_new (struct TestState);
192 struct GNUNET_TESTING_NetjailTopology *topology =
193 GNUNET_TESTING_get_topo_from_file (TOPOLOGY_CONFIG);
194
195 if (0 == m_int)
196 num = n_int;
197 else
198 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
199
200 block_send = GNUNET_TESTING_cmd_block_until_external_trigger ("block");
201 block_receive = GNUNET_TESTING_cmd_block_until_external_trigger (
202 "block-receive");
203 connect_peers = GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers",
204 "start-peer",
205 "system-create",
206 num,
207 NULL);
208
209
210
211 sscanf (m, "%u", &m_int);
212 sscanf (n, "%u", &n_int);
213 sscanf (local_m, "%u", &local_m_int);
214
215 GNUNET_asprintf (&ts->cfgname,
216 "test_transport_api2_tcp_node1.conf");
122 217
123 LOG (GNUNET_ERROR_TYPE_ERROR, 218 LOG (GNUNET_ERROR_TYPE_ERROR,
124 "plugin cfgname: %s\n", 219 "plugin cfgname: %s\n",
125 cfgname); 220 ts->cfgname);
126 221
127 LOG (GNUNET_ERROR_TYPE_ERROR, 222 LOG (GNUNET_ERROR_TYPE_ERROR,
128 "node ip: %s\n", 223 "node ip: %s\n",
129 node_ip); 224 node_ip);
130 225
131 LOG (GNUNET_ERROR_TYPE_ERROR, 226 GNUNET_asprintf (&ts->testdir,
132 "m: %s n: %s\n",
133 m,
134 n);
135
136 GNUNET_asprintf (&testdir,
137 "%s%s%s", 227 "%s%s%s",
138 BASE_DIR, 228 BASE_DIR,
139 m, 229 m,
@@ -143,50 +233,38 @@ start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
143 GNUNET_MQ_hd_var_size (test, 233 GNUNET_MQ_hd_var_size (test,
144 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, 234 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
145 struct GNUNET_TRANSPORT_TESTING_TestMessage, 235 struct GNUNET_TRANSPORT_TESTING_TestMessage,
146 NULL), 236 ts),
147 GNUNET_MQ_handler_end () 237 GNUNET_MQ_handler_end ()
148 }; 238 };
149 239
150 struct GNUNET_TESTING_Command commands[] = { 240 struct GNUNET_TESTING_Command commands[] = {
151 GNUNET_TESTING_cmd_system_create ("system-create", 241 GNUNET_TESTING_cmd_system_create ("system-create",
152 testdir), 242 ts->testdir),
153 GNUNET_TRANSPORT_cmd_start_peer ("start-peer", 243 GNUNET_TRANSPORT_cmd_start_peer ("start-peer",
154 "system-create", 244 "system-create",
155 m, 245 num,
156 n,
157 local_m,
158 node_ip, 246 node_ip,
159 handlers, 247 handlers,
160 cfgname), 248 ts->cfgname,
249 notify_connect),
161 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready", 250 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready",
162 write_message), 251 write_message),
163 GNUNET_TESTING_cmd_block_until_all_peers_started ("block", 252 block_send,
164 &are_all_peers_started), 253 connect_peers,
165 GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers",
166 "start-peer",
167 "system-create",
168 (atoi (n) - 1) * atoi (local_m) + atoi (
169 m)),
170 GNUNET_TRANSPORT_cmd_send_simple ("send-simple", 254 GNUNET_TRANSPORT_cmd_send_simple ("send-simple",
171 m, 255 "start-peer",
172 n, 256 num),
173 (atoi (n) - 1) * atoi (local_m) + atoi ( 257 block_receive,
174 m),
175 "start-peer"),
176 GNUNET_TESTING_cmd_block_until_external_trigger ("block-receive",
177 &message_received),
178 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer", 258 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
179 "start-peer"), 259 "start-peer"),
180 GNUNET_TESTING_cmd_system_destroy ("system-destroy", 260 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
181 "system-create"), 261 "system-create")
182 GNUNET_TESTING_cmd_local_test_finished ("local-test-finished",
183 write_message)
184 }; 262 };
185 263
186 GNUNET_TESTING_run (commands, 264 GNUNET_TESTING_run (commands,
187 GNUNET_TIME_UNIT_FOREVER_REL, 265 GNUNET_TIME_UNIT_FOREVER_REL,
188 NULL, /* FIXME: will crash */ 266 &handle_result,
189 NULL); 267 ts);
190 268
191} 269}
192 270
@@ -221,8 +299,6 @@ libgnunet_test_transport_plugin_cmd_simple_send_done (void *cls)
221 struct GNUNET_TESTING_PluginFunctions *api = cls; 299 struct GNUNET_TESTING_PluginFunctions *api = cls;
222 300
223 GNUNET_free (api); 301 GNUNET_free (api);
224 GNUNET_free (testdir);
225 GNUNET_free (cfgname);
226 return NULL; 302 return NULL;
227} 303}
228 304
diff --git a/src/transport/test_transport_plugin_cmd_simple_send_v2.c b/src/transport/test_transport_plugin_cmd_simple_send_v2.c
deleted file mode 100644
index 98e9d5fca..000000000
--- a/src/transport/test_transport_plugin_cmd_simple_send_v2.c
+++ /dev/null
@@ -1,235 +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 testbed/plugin_cmd_simple_send.c
23 * @brief a plugin to provide the API for running test cases.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_ng_lib.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_transport_application_service.h"
30#include "transport-testing2.h"
31#include "transport-testing-cmds.h"
32
33/**
34 * Generic logging shortcut
35 */
36#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
37
38#define BASE_DIR "testdir"
39
40#define TOPOLOGY_CONFIG "test_transport_simple_send_topo.conf"
41
42/**
43 * The name for a specific test environment directory.
44 *
45 */
46char *testdir;
47
48/**
49 * The name for the configuration file of the specific node.
50 *
51 */
52char *cfgname;
53
54/**
55 * Flag indicating if all peers have been started.
56 *
57 */
58unsigned int are_all_peers_started;
59
60/**
61 * Flag indicating a received message.
62 */
63unsigned int message_received;
64
65
66/**
67 * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being
68 * received.
69 *
70 */
71static int
72check_test (void *cls,
73 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
74{
75 return GNUNET_OK;
76}
77
78
79/**
80 * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE
81 * being received.
82 *
83 */
84static void
85handle_test (void *cls,
86 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
87{
88 message_received = GNUNET_YES;
89}
90
91
92/**
93 * Callback to set the flag indicating all peers started. Will be called via the plugin api.
94 *
95 */
96static void
97all_peers_started ()
98{
99 are_all_peers_started = GNUNET_YES;
100}
101
102
103/**
104 * Function to start a local test case.
105 *
106 * @param write_message Callback to send a message to the master loop.
107 * @param router_ip Global address of the network namespace.
108 * @param node_ip Local address of a node i a network namespace.
109 * @param m The number of the node in a network namespace.
110 * @param n The number of the network namespace.
111 * @param local_m The number of nodes in a network namespace.
112 */
113static void
114start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
115 char *node_ip,
116 char *m,
117 char *n,
118 char *local_m)
119{
120
121 unsigned int n_int, m_int, local_m_int, num;
122
123 struct GNUNET_TESTING_NetjailTopology *topology =
124 GNUNET_TESTING_get_topo_from_file (TOPOLOGY_CONFIG);
125
126 sscanf (m, "%u", &m_int);
127 sscanf (n, "%u", &n_int);
128 sscanf (local_m, "%u", &local_m_int);
129
130
131 if (0 == m_int)
132 num = n_int;
133 else
134 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
135
136 GNUNET_asprintf (&cfgname,
137 "test_transport_api2_tcp_node1.conf");
138
139 LOG (GNUNET_ERROR_TYPE_ERROR,
140 "plugin cfgname: %s\n",
141 cfgname);
142
143 LOG (GNUNET_ERROR_TYPE_ERROR,
144 "node ip: %s\n",
145 node_ip);
146
147 GNUNET_asprintf (&testdir,
148 "%s%s%s",
149 BASE_DIR,
150 m,
151 n);
152
153 struct GNUNET_MQ_MessageHandler handlers[] = {
154 GNUNET_MQ_hd_var_size (test,
155 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
156 struct GNUNET_TRANSPORT_TESTING_TestMessage,
157 NULL),
158 GNUNET_MQ_handler_end ()
159 };
160
161 struct GNUNET_TESTING_Command commands[] = {
162 GNUNET_TESTING_cmd_system_create ("system-create",
163 testdir),
164 GNUNET_TRANSPORT_cmd_start_peer_v2 ("start-peer",
165 "system-create",
166 num,
167 node_ip,
168 handlers,
169 cfgname),
170 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready",
171 write_message),
172 GNUNET_TESTING_cmd_block_until_all_peers_started ("block",
173 &are_all_peers_started),
174 GNUNET_TRANSPORT_cmd_connect_peers_v2 ("connect-peers",
175 "start-peer",
176 "system-create",
177 num),
178 GNUNET_TRANSPORT_cmd_send_simple_v2 ("send-simple",
179 "start-peer",
180 num),
181 GNUNET_TESTING_cmd_block_until_external_trigger ("block-receive",
182 &message_received),
183 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
184 "start-peer"),
185 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
186 "system-create"),
187 GNUNET_TESTING_cmd_local_test_finished ("local-test-finished",
188 write_message)
189 };
190
191 GNUNET_TESTING_run (commands,
192 GNUNET_TIME_UNIT_FOREVER_REL,
193 NULL, /* FIXME: will crash */
194 NULL);
195
196}
197
198
199/**
200 * Entry point for the plugin.
201 *
202 * @param cls NULL
203 * @return the exported block API
204 */
205void *
206libgnunet_test_transport_plugin_cmd_simple_send_init (void *cls)
207{
208 struct GNUNET_TESTING_PluginFunctions *api;
209
210 api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions);
211 api->start_testcase = &start_testcase;
212 api->all_peers_started = &all_peers_started;
213 return api;
214}
215
216
217/**
218 * Exit point from the plugin.
219 *
220 * @param cls the return value from #libgnunet_test_transport_plugin_block_test_init
221 * @return NULL
222 */
223void *
224libgnunet_test_transport_plugin_cmd_simple_send_done (void *cls)
225{
226 struct GNUNET_TESTING_PluginFunctions *api = cls;
227
228 GNUNET_free (api);
229 GNUNET_free (testdir);
230 GNUNET_free (cfgname);
231 return NULL;
232}
233
234
235/* end of plugin_cmd_simple_send.c */
diff --git a/src/transport/test_transport_plugin_cmd_udp_backchannel.c b/src/transport/test_transport_plugin_cmd_udp_backchannel.c
index 6b0342e51..108e1ac09 100644
--- a/src/transport/test_transport_plugin_cmd_udp_backchannel.c
+++ b/src/transport/test_transport_plugin_cmd_udp_backchannel.c
@@ -39,28 +39,32 @@
39 39
40#define TOPOLOGY_CONFIG "test_transport_udp_backchannel_topo.conf" 40#define TOPOLOGY_CONFIG "test_transport_udp_backchannel_topo.conf"
41 41
42/** 42struct TestState
43 * The name for a specific test environment directory. 43{
44 * 44 /**
45 */ 45 * Callback to write messages to the master loop.
46char *testdir; 46 *
47 */
48 TESTING_CMD_HELPER_write_cb write_message;
47 49
48/** 50 /**
49 * The name for the configuration file of the specific node. 51 * The name for a specific test environment directory.
50 * 52 *
51 */ 53 */
52char *cfgname; 54 char *testdir;
53 55
54/** 56 /**
55 * Flag indicating if all peers have been started. 57 * The name for the configuration file of the specific node.
56 * 58 *
57 */ 59 */
58unsigned int are_all_peers_started; 60 char *cfgname;
61};
59 62
60/** 63static struct GNUNET_TESTING_Command block_send;
61 * Flag indicating a received message. 64
62 */ 65static struct GNUNET_TESTING_Command block_receive;
63unsigned int message_received; 66
67static struct GNUNET_TESTING_Command connect_peers;
64 68
65 69
66/** 70/**
@@ -85,7 +89,14 @@ static void
85handle_test (void *cls, 89handle_test (void *cls,
86 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) 90 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
87{ 91{
88 message_received = GNUNET_YES; 92 struct GNUNET_TESTING_AsyncContext *ac;
93
94 GNUNET_TESTING_get_trait_async_context (&block_receive,
95 &ac);
96 if ((NULL == ac) || (NULL == ac->cont))
97 GNUNET_TESTING_async_fail (ac);
98 else
99 GNUNET_TESTING_async_finish (ac);
89} 100}
90 101
91 102
@@ -96,10 +107,62 @@ handle_test (void *cls,
96static void 107static void
97all_peers_started () 108all_peers_started ()
98{ 109{
99 are_all_peers_started = GNUNET_YES; 110 struct GNUNET_TESTING_AsyncContext *ac;
100 LOG (GNUNET_ERROR_TYPE_ERROR, 111
101 "setting are_all_peers_started: %d\n", 112 GNUNET_TESTING_get_trait_async_context (&block_send,
102 are_all_peers_started); 113 &ac);
114 if ((NULL == ac) || (NULL == ac->cont))
115 GNUNET_TESTING_async_fail (ac);
116 else
117 GNUNET_TESTING_async_finish (ac);
118}
119
120
121/**
122* Function called with the final result of the test.
123*
124* @param cls the `struct MainParams`
125* @param rv #GNUNET_OK if the test passed
126*/
127static void
128handle_result (void *cls,
129 enum GNUNET_GenericReturnValue rv)
130{
131 struct TestState *ts = cls;
132 struct GNUNET_MessageHeader *reply;
133
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Local test exits with status %d\n",
136 rv);
137 reply = GNUNET_TESTING_send_local_test_finished_msg (rv);
138
139 ts->write_message (reply,
140 ntohs (reply->size));
141 GNUNET_free (ts->testdir);
142 GNUNET_free (ts->cfgname);
143 GNUNET_free (ts);
144}
145
146
147/**
148 * Callback from start peer cmd for signaling a peer got connected.
149 *
150 */
151static void *
152notify_connect (void *cls,
153 const struct GNUNET_PeerIdentity *peer,
154 struct GNUNET_MQ_Handle *mq)
155{
156 struct ConnectPeersState *cps;
157
158 GNUNET_TESTING_get_trait_connect_peer_state (&connect_peers,
159 &cps);
160 void *ret = NULL;
161
162 cps->notify_connect (cps,
163 peer,
164 mq);
165 return ret;
103} 166}
104 167
105 168
@@ -121,7 +184,11 @@ start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
121 char *local_m) 184 char *local_m)
122{ 185{
123 186
124 unsigned int n_int, m_int, local_m_int, num; 187 unsigned int n_int;
188 unsigned int m_int;
189 unsigned int local_m_int;
190 unsigned int num;
191 struct TestState *ts = GNUNET_new (struct TestState);
125 192
126 struct GNUNET_TESTING_NetjailTopology *topology = 193 struct GNUNET_TESTING_NetjailTopology *topology =
127 GNUNET_TESTING_get_topo_from_file (TOPOLOGY_CONFIG); 194 GNUNET_TESTING_get_topo_from_file (TOPOLOGY_CONFIG);
@@ -136,18 +203,27 @@ start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
136 else 203 else
137 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x; 204 num = (n_int - 1) * local_m_int + m_int + topology->nodes_x;
138 205
139 GNUNET_asprintf (&cfgname, 206 block_send = GNUNET_TESTING_cmd_block_until_external_trigger ("block");
207 block_receive = GNUNET_TESTING_cmd_block_until_external_trigger (
208 "block-receive");
209 connect_peers = GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers",
210 "start-peer",
211 "system-create",
212 num,
213 NULL);
214
215 GNUNET_asprintf (&ts->cfgname,
140 "test_transport_api2_tcp_node1.conf"); 216 "test_transport_api2_tcp_node1.conf");
141 217
142 LOG (GNUNET_ERROR_TYPE_ERROR, 218 LOG (GNUNET_ERROR_TYPE_ERROR,
143 "plugin cfgname: %s\n", 219 "plugin cfgname: %s\n",
144 cfgname); 220 ts->cfgname);
145 221
146 LOG (GNUNET_ERROR_TYPE_ERROR, 222 LOG (GNUNET_ERROR_TYPE_ERROR,
147 "node ip: %s\n", 223 "node ip: %s\n",
148 node_ip); 224 node_ip);
149 225
150 GNUNET_asprintf (&testdir, 226 GNUNET_asprintf (&ts->testdir,
151 "%s%s%s", 227 "%s%s%s",
152 BASE_DIR, 228 BASE_DIR,
153 m, 229 m,
@@ -163,39 +239,32 @@ start_testcase (TESTING_CMD_HELPER_write_cb write_message, char *router_ip,
163 239
164 struct GNUNET_TESTING_Command commands[] = { 240 struct GNUNET_TESTING_Command commands[] = {
165 GNUNET_TESTING_cmd_system_create ("system-create", 241 GNUNET_TESTING_cmd_system_create ("system-create",
166 testdir), 242 ts->testdir),
167 GNUNET_TRANSPORT_cmd_start_peer_v3 ("start-peer", 243 GNUNET_TRANSPORT_cmd_start_peer ("start-peer",
168 "system-create", 244 "system-create",
169 num, 245 num,
170 node_ip, 246 node_ip,
171 handlers, 247 handlers,
172 cfgname), 248 ts->cfgname,
249 notify_connect),
173 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready", 250 GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready",
174 write_message), 251 write_message),
175 GNUNET_TESTING_cmd_block_until_all_peers_started ("block", 252 block_send,
176 &are_all_peers_started), 253 connect_peers,
177 GNUNET_TRANSPORT_cmd_connect_peers_v3 ("connect-peers", 254 GNUNET_TRANSPORT_cmd_send_simple ("send-simple",
178 "start-peer", 255 "start-peer",
179 "system-create", 256 num),
180 num, 257 block_receive,
181 topology),
182 GNUNET_TRANSPORT_cmd_send_simple_v2 ("send-simple",
183 "start-peer",
184 num),
185 GNUNET_TESTING_cmd_block_until_external_trigger ("block-receive",
186 &message_received),
187 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer", 258 GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer",
188 "start-peer"), 259 "start-peer"),
189 GNUNET_TESTING_cmd_system_destroy ("system-destroy", 260 GNUNET_TESTING_cmd_system_destroy ("system-destroy",
190 "system-create"), 261 "system-create")
191 GNUNET_TESTING_cmd_local_test_finished ("local-test-finished",
192 write_message)
193 }; 262 };
194 263
195 GNUNET_TESTING_run (commands, 264 GNUNET_TESTING_run (commands,
196 GNUNET_TIME_UNIT_FOREVER_REL, 265 GNUNET_TIME_UNIT_FOREVER_REL,
197 NULL, /* FIMXE: will crash */ 266 &handle_result,
198 NULL); 267 ts);
199 268
200} 269}
201 270
@@ -234,8 +303,6 @@ libgnunet_test_transport_plugin_cmd_udp_backchannel_done (void *cls)
234 struct GNUNET_TESTING_PluginFunctions *api = cls; 303 struct GNUNET_TESTING_PluginFunctions *api = cls;
235 304
236 GNUNET_free (api); 305 GNUNET_free (api);
237 GNUNET_free (testdir);
238 GNUNET_free (cfgname);
239 return NULL; 306 return NULL;
240} 307}
241 308
diff --git a/src/transport/test_transport_port_forward.c b/src/transport/test_transport_port_forward.c
deleted file mode 100644
index d3233c2da..000000000
--- a/src/transport/test_transport_port_forward.c
+++ /dev/null
@@ -1,85 +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 transport/test_transport_port_forward.c
23 * @brief Test case executing a script which sends a test UDP message from a nated peer
24 * to a global known peer. There is a tcp port forwarding in place towards the
25 * natted peer to test the backchannel functionality of the TNG service.
26 * @author t3sserakt
27 */
28#include "platform.h"
29#include "gnunet_testing_ng_lib.h"
30#include "gnunet_util_lib.h"
31
32#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
33
34/**
35 * Return value of the test.
36 *
37 */
38static unsigned int rv = 0;
39
40
41/**
42 * Main function to run the test cases.
43 *
44 * @param cls not used.
45 *
46 */
47static void
48run (void *cls)
49{
50 const char *topology_config = "test_topology_port_forward.conf";
51
52 struct GNUNET_TESTING_Command commands[] = {
53 GNUNET_TESTING_cmd_netjail_start_v2 ("netjail-start",
54 topology_config),
55 GNUNET_TESTING_cmd_netjail_start_testing_system_v2 ("netjail-start-testbed",
56 topology_config,
57 &rv),
58 GNUNET_TESTING_cmd_stop_testing_system_v2 ("stop-testbed",
59 "netjail-start-testbed",
60 topology_config),
61 GNUNET_TESTING_cmd_netjail_stop_v2 ("netjail-stop",
62 topology_config),
63 GNUNET_TESTING_cmd_end ()
64 };
65
66 GNUNET_TESTING_run (NULL,
67 commands,
68 TIMEOUT);
69}
70
71
72int
73main (int argc,
74 char *const *argv)
75{
76 GNUNET_log_setup ("test-netjail",
77 "DEBUG",
78 NULL);
79 GNUNET_SCHEDULER_run (&run,
80 NULL);
81
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 "Test finished!\n");
84 return rv;
85}
diff --git a/src/transport/test_transport_simple_send.c b/src/transport/test_transport_simple_send.c
deleted file mode 100644
index 924990d0a..000000000
--- a/src/transport/test_transport_simple_send.c
+++ /dev/null
@@ -1,86 +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 transport/test_transport_api_cmd_simple_send.c
23 * @brief Test case executing a script which sends a test message between two peers.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_ng_lib.h"
28#include "gnunet_util_lib.h"
29
30#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
31
32/**
33 * Return value of the test.
34 *
35 */
36static unsigned int rv = 0;
37
38
39/**
40 * Main function to run the test cases.
41 *
42 * @param cls not used.
43 *
44 */
45static void
46run (void *cls)
47{
48 struct GNUNET_TESTING_Command commands[] = {
49 GNUNET_TESTING_cmd_netjail_start ("netjail-start",
50 "2",
51 "1"),
52 GNUNET_TESTING_cmd_netjail_start_testing_system ("netjail-start-testbed",
53 "2",
54 "1",
55 "libgnunet_test_transport_plugin_cmd_simple_send",
56 &rv),
57 GNUNET_TESTING_cmd_stop_testing_system ("stop-testbed",
58 "netjail-start-testbed",
59 "2",
60 "1"),
61 GNUNET_TESTING_cmd_netjail_stop ("netjail-stop",
62 "2",
63 "1"),
64 GNUNET_TESTING_cmd_end ()
65 };
66
67 GNUNET_TESTING_run (NULL,
68 commands,
69 TIMEOUT);
70}
71
72
73int
74main (int argc,
75 char *const *argv)
76{
77 GNUNET_log_setup ("test-netjail",
78 "DEBUG",
79 NULL);
80 GNUNET_SCHEDULER_run (&run,
81 NULL);
82
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Test finished!\n");
85 return rv;
86}
diff --git a/src/transport/test_transport_simple_send.sh b/src/transport/test_transport_simple_send.sh
index 25d5b6797..2dd269fcc 100755
--- a/src/transport/test_transport_simple_send.sh
+++ b/src/transport/test_transport_simple_send.sh
@@ -1,2 +1,2 @@
1#!/bin/bash 1#!/bin/bash
2exec unshare -r -nmU bash -c "mount -t tmpfs --make-rshared tmpfs /run/netns; ./test_transport_simple_send" 2exec unshare -r -nmU bash -c "mount -t tmpfs --make-rshared tmpfs /run/netns; ./test_transport_start_with_config test_transport_simple_send_topo.conf"
diff --git a/src/transport/test_transport_simple_send_v2.c b/src/transport/test_transport_simple_send_v2.c
deleted file mode 100644
index aeac1bfb6..000000000
--- a/src/transport/test_transport_simple_send_v2.c
+++ /dev/null
@@ -1,83 +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 transport/test_transport_api_cmd_simple_send.c
23 * @brief Test case executing a script which sends a test message between two peers.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_testing_ng_lib.h"
28#include "gnunet_util_lib.h"
29
30#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
31
32#define TOPOLOGY_CONFIG "test_transport_simple_send_topo.conf"
33
34/**
35 * Return value of the test.
36 *
37 */
38static unsigned int rv = 0;
39
40
41/**
42 * Main function to run the test cases.
43 *
44 * @param cls not used.
45 *
46 */
47static void
48run (void *cls)
49{
50 struct GNUNET_TESTING_Command commands[] = {
51 GNUNET_TESTING_cmd_netjail_start_v2 ("netjail-start",
52 TOPOLOGY_CONFIG),
53 GNUNET_TESTING_cmd_netjail_start_testing_system_v2 ("netjail-start-testbed",
54 TOPOLOGY_CONFIG,
55 &rv),
56 GNUNET_TESTING_cmd_stop_testing_system_v2 ("stop-testbed",
57 "netjail-start-testbed",
58 TOPOLOGY_CONFIG),
59 GNUNET_TESTING_cmd_netjail_stop_v2 ("netjail-stop",
60 TOPOLOGY_CONFIG),
61 GNUNET_TESTING_cmd_end ()
62 };
63
64 GNUNET_TESTING_run (NULL,
65 commands,
66 TIMEOUT);
67}
68
69
70int
71main (int argc,
72 char *const *argv)
73{
74 GNUNET_log_setup ("test-netjail",
75 "DEBUG",
76 NULL);
77 GNUNET_SCHEDULER_run (&run,
78 NULL);
79
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Test finished!\n");
82 return rv;
83}
diff --git a/src/transport/test_transport_simple_send_v2.sh b/src/transport/test_transport_simple_send_v2.sh
deleted file mode 100755
index 8462d1849..000000000
--- a/src/transport/test_transport_simple_send_v2.sh
+++ /dev/null
@@ -1,2 +0,0 @@
1#!/bin/bash
2exec unshare -r -nmU bash -c "mount -t tmpfs --make-rshared tmpfs /run/netns; ./test_transport_simple_send_v2"
diff --git a/src/transport/test_transport_start_with_config.c b/src/transport/test_transport_start_with_config.c
index 795c157d7..932b0e583 100644
--- a/src/transport/test_transport_start_with_config.c
+++ b/src/transport/test_transport_start_with_config.c
@@ -20,7 +20,7 @@
20 20
21/** 21/**
22 * @file transport/test_transport_start_with_config.c 22 * @file transport/test_transport_start_with_config.c
23 * @brief Test case executing a script which sends a test message between two peers. 23 * @brief Generic program to start testcases in an configurable topology.
24 * @author t3sserakt 24 * @author t3sserakt
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
@@ -29,57 +29,32 @@
29 29
30#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) 30#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
31 31
32/**
33 * Return value of the test.
34 *
35 */
36static unsigned int rv = 0;
37
38static char *topology_config;
39
40/**
41 * Main function to run the test cases.
42 *
43 * @param cls not used.
44 *
45 */
46static void
47run (void *cls)
48{
49 struct GNUNET_TESTING_Command commands[] = {
50 GNUNET_TESTING_cmd_netjail_start_v2 ("netjail-start",
51 topology_config),
52 GNUNET_TESTING_cmd_netjail_start_testing_system_v2 ("netjail-start-testbed",
53 topology_config,
54 &rv),
55 GNUNET_TESTING_cmd_stop_testing_system_v2 ("stop-testbed",
56 "netjail-start-testbed",
57 topology_config),
58 GNUNET_TESTING_cmd_netjail_stop_v2 ("netjail-stop",
59 topology_config),
60 GNUNET_TESTING_cmd_end ()
61 };
62
63 GNUNET_TESTING_run (NULL,
64 commands,
65 TIMEOUT);
66}
67
68 32
69int 33int
70main (int argc, 34main (int argc,
71 char *const *argv) 35 char *const *argv)
72{ 36{
37 char *topology_config;
38
73 GNUNET_log_setup ("test-netjail", 39 GNUNET_log_setup ("test-netjail",
74 "DEBUG", 40 "DEBUG",
75 NULL); 41 NULL);
76 42
77 topology_config = argv[1]; 43 topology_config = argv[1];
78 44
79 GNUNET_SCHEDULER_run (&run, 45 struct GNUNET_TESTING_Command commands[] = {
80 NULL); 46 GNUNET_TESTING_cmd_netjail_start ("netjail-start",
47 topology_config),
48 GNUNET_TESTING_cmd_netjail_start_testing_system ("netjail-start-testbed",
49 topology_config),
50 GNUNET_TESTING_cmd_stop_testing_system ("stop-testbed",
51 "netjail-start-testbed",
52 topology_config),
53 GNUNET_TESTING_cmd_netjail_stop ("netjail-stop",
54 topology_config),
55 GNUNET_TESTING_cmd_end ()
56 };
81 57
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 58 return GNUNET_TESTING_main (commands,
83 "Test finished!\n"); 59 TIMEOUT);
84 return rv;
85} 60}
diff --git a/src/transport/transport-testing-cmds.h b/src/transport/transport-testing-cmds.h
index ecdabd35f..f6e34df62 100644
--- a/src/transport/transport-testing-cmds.h
+++ b/src/transport/transport-testing-cmds.h
@@ -29,92 +29,71 @@
29#include "gnunet_testing_lib.h" 29#include "gnunet_testing_lib.h"
30 30
31 31
32struct StartPeerState_v2 32/**
33 * Struct to store information needed in callbacks.
34 *
35 */
36struct ConnectPeersState
33{ 37{
34 /** 38 /**
35 * The ip of a node. 39 * Context for our asynchronous completion.
36 */
37 char *node_ip;
38
39 /**
40 * Receive callback
41 */ 40 */
42 struct GNUNET_MQ_MessageHandler *handlers; 41 struct GNUNET_TESTING_AsyncContext ac;
43 42
44 const char *cfgname; 43 GNUNET_TRANSPORT_NotifyConnect notify_connect;
45 44
46 /** 45 /**
47 * Peer's configuration 46 * The testing system of this node.
48 */ 47 */
49 struct GNUNET_CONFIGURATION_Handle *cfg; 48 struct GNUNET_TESTING_System *tl_system;
50
51 struct GNUNET_TESTING_Peer *peer;
52 49
53 /** 50 // Label of the cmd which started the test system.
54 * Peer identity 51 const char *create_label;
55 */
56 struct GNUNET_PeerIdentity id;
57 52
58 /** 53 /**
59 * Peer's transport service handle 54 * Number globally identifying the node.
55 *
60 */ 56 */
61 struct GNUNET_TRANSPORT_CoreHandle *th; 57 uint32_t num;
62 58
63 /** 59 /**
64 * Application handle 60 * Label of the cmd to start a peer.
61 *
65 */ 62 */
66 struct GNUNET_TRANSPORT_ApplicationHandle *ah; 63 const char *start_peer_label;
67 64
68 /** 65 /**
69 * Peer's PEERSTORE Handle 66 * The peer identity of this peer.
67 *
70 */ 68 */
71 struct GNUNET_PEERSTORE_Handle *ph; 69 struct GNUNET_PeerIdentity *id;
72 70
73 /** 71 /**
74 * Hello get task 72 * The topology of the test setup.
75 */ 73 */
76 struct GNUNET_SCHEDULER_Task *rh_task; 74 struct GNUNET_TESTING_NetjailTopology *topology;
77 75
78 /** 76 /**
79 * Peer's transport get hello handle to retrieve peer's HELLO message 77 * Connections to other peers.
80 */ 78 */
81 struct GNUNET_PEERSTORE_IterateContext *pic; 79 struct GNUNET_TESTING_NodeConnection *node_connections_head;
82 80
83 /** 81 struct GNUNET_TESTING_Interpreter *is;
84 * Hello
85 */
86 char *hello;
87 82
88 /** 83 /**
89 * Hello size 84 * Number of connections.
90 */ 85 */
91 size_t hello_size; 86 unsigned int con_num;
92
93 char *m;
94
95 char *n;
96
97 char *local_m;
98
99 unsigned int finished;
100
101 const char *system_label;
102
103 /**
104 * An unique number to identify the peer
105 */
106 unsigned int no;
107
108 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
109
110 struct GNUNET_TESTING_System *tl_system;
111
112}; 87};
113 88
114
115struct StartPeerState 89struct StartPeerState
116{ 90{
117 /** 91 /**
92 * Context for our asynchronous completion.
93 */
94 struct GNUNET_TESTING_AsyncContext ac;
95
96 /**
118 * The ip of a node. 97 * The ip of a node.
119 */ 98 */
120 char *node_ip; 99 char *node_ip;
@@ -179,8 +158,6 @@ struct StartPeerState
179 158
180 char *local_m; 159 char *local_m;
181 160
182 unsigned int finished;
183
184 const char *system_label; 161 const char *system_label;
185 162
186 /** 163 /**
@@ -192,9 +169,24 @@ struct StartPeerState
192 169
193 struct GNUNET_TESTING_System *tl_system; 170 struct GNUNET_TESTING_System *tl_system;
194 171
172 GNUNET_TRANSPORT_NotifyConnect notify_connect;
173
195}; 174};
196 175
197 176
177/**
178 * Function to get the trait with the struct ConnectPeersState.
179 *
180 * @param[out] sps struct ConnectPeersState.
181 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
182 *
183 */
184int
185GNUNET_TESTING_get_trait_connect_peer_state (
186 const struct GNUNET_TESTING_Command *cmd,
187 struct ConnectPeersState **cps);
188
189
198int 190int
199GNUNET_TRANSPORT_get_trait_state (const struct 191GNUNET_TRANSPORT_get_trait_state (const struct
200 GNUNET_TESTING_Command 192 GNUNET_TESTING_Command
@@ -203,61 +195,28 @@ GNUNET_TRANSPORT_get_trait_state (const struct
203 195
204 196
205struct GNUNET_TESTING_Command 197struct GNUNET_TESTING_Command
206GNUNET_TRANSPORT_cmd_start_peer_v2 (const char *label,
207 const char *system_label,
208 uint32_t no,
209 char *node_ip,
210 struct GNUNET_MQ_MessageHandler *handlers,
211 const char *cfgname);
212
213struct GNUNET_TESTING_Command
214GNUNET_TRANSPORT_cmd_start_peer_v3 (const char *label,
215 const char *system_label,
216 uint32_t no,
217 char *node_ip,
218 struct GNUNET_MQ_MessageHandler *handlers,
219 const char *cfgname);
220
221struct GNUNET_TESTING_Command
222GNUNET_TRANSPORT_cmd_start_peer (const char *label, 198GNUNET_TRANSPORT_cmd_start_peer (const char *label,
223 const char *system_label, 199 const char *system_label,
224 char *m, 200 uint32_t no,
225 char *n,
226 char *local_m,
227 char *node_ip, 201 char *node_ip,
228 struct GNUNET_MQ_MessageHandler *handlers, 202 struct GNUNET_MQ_MessageHandler *handlers,
229 const char *cfgname); 203 const char *cfgname,
204 GNUNET_TRANSPORT_NotifyConnect notify_connect);
205
230 206
231struct GNUNET_TESTING_Command 207struct GNUNET_TESTING_Command
232GNUNET_TRANSPORT_cmd_stop_peer (const char *label, 208GNUNET_TRANSPORT_cmd_stop_peer (const char *label,
233 const char *start_label); 209 const char *start_label);
234 210
211
235struct GNUNET_TESTING_Command 212struct GNUNET_TESTING_Command
236GNUNET_TRANSPORT_cmd_connect_peers (const char *label, 213GNUNET_TRANSPORT_cmd_connect_peers (const char *label,
237 const char *start_peer_label, 214 const char *start_peer_label,
238 const char *create_label, 215 const char *create_label,
239 uint32_t num); 216 uint32_t num,
240 217 struct GNUNET_TESTING_NetjailTopology *
241struct GNUNET_TESTING_Command 218 topology);
242GNUNET_TRANSPORT_cmd_connect_peers_v2 (const char *label,
243 const char *start_peer_label,
244 const char *create_label,
245 uint32_t num);
246
247struct GNUNET_TESTING_Command
248GNUNET_TRANSPORT_cmd_connect_peers_v3 (const char *label,
249 const char *start_peer_label,
250 const char *create_label,
251 uint32_t num,
252 struct GNUNET_TESTING_NetjailTopology *
253 topology);
254 219
255struct GNUNET_TESTING_Command
256GNUNET_TRANSPORT_cmd_send_simple (const char *label,
257 char *m,
258 char *n,
259 uint32_t num,
260 const char *start_peer_label);
261 220
262/** 221/**
263 * Create command. 222 * Create command.
@@ -270,43 +229,29 @@ GNUNET_TRANSPORT_cmd_send_simple (const char *label,
270 * @return command. 229 * @return command.
271 */ 230 */
272struct GNUNET_TESTING_Command 231struct GNUNET_TESTING_Command
273GNUNET_TRANSPORT_cmd_send_simple_v2 (const char *label, 232GNUNET_TRANSPORT_cmd_send_simple (const char *label,
274 const char *start_peer_label, 233 const char *start_peer_label,
275 uint32_t num); 234 uint32_t num);
235
236
237
238
276 239
277int 240int
278GNUNET_TRANSPORT_get_trait_peer_id (const struct 241GNUNET_TRANSPORT_get_trait_peer_id (const struct
279 GNUNET_TESTING_Command *cmd, 242 GNUNET_TESTING_Command *cmd,
280 struct GNUNET_PeerIdentity **id); 243 struct GNUNET_PeerIdentity **id);
281 244
245
282int 246int
283GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct 247GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
284 GNUNET_TESTING_Command 248 GNUNET_TESTING_Command
285 *cmd, 249 *cmd,
286 struct 250 struct
287 GNUNET_CONTAINER_MultiShortmap * 251 GNUNET_CONTAINER_MultiShortmap
252 *
288 * 253 *
289 connected_peers_map); 254 connected_peers_map);
290
291int
292GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (const struct
293 GNUNET_TESTING_Command
294 *cmd,
295 struct
296 GNUNET_CONTAINER_MultiShortmap
297 *
298 *
299 connected_peers_map);
300
301int
302GNUNET_TRANSPORT_get_trait_connected_peers_map_v3 (const struct
303 GNUNET_TESTING_Command
304 *cmd,
305 struct
306 GNUNET_CONTAINER_MultiShortmap
307 *
308 *
309 connected_peers_map);
310int 255int
311GNUNET_TRANSPORT_get_trait_hello_size (const struct 256GNUNET_TRANSPORT_get_trait_hello_size (const struct
312 GNUNET_TESTING_Command 257 GNUNET_TESTING_Command
@@ -327,19 +272,5 @@ GNUNET_TRANSPORT_get_trait_application_handle (const struct
327 GNUNET_TRANSPORT_ApplicationHandle 272 GNUNET_TRANSPORT_ApplicationHandle
328 **ah); 273 **ah);
329 274
330int
331GNUNET_TRANSPORT_get_trait_application_handle_v2 (const struct
332 GNUNET_TESTING_Command *cmd,
333 struct
334 GNUNET_TRANSPORT_ApplicationHandle
335 **ah);
336
337int
338GNUNET_TRANSPORT_get_trait_application_handle_v3 (const struct
339 GNUNET_TESTING_Command *cmd,
340 struct
341 GNUNET_TRANSPORT_ApplicationHandle
342 **ah);
343
344#endif 275#endif
345/* end of transport_testing.h */ 276/* end of transport_testing.h */
diff --git a/src/transport/transport_api_cmd_connecting_peers.c b/src/transport/transport_api_cmd_connecting_peers.c
index 82194ff15..2e51363f6 100644
--- a/src/transport/transport_api_cmd_connecting_peers.c
+++ b/src/transport/transport_api_cmd_connecting_peers.c
@@ -36,40 +36,240 @@
36 */ 36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38 38
39/** 39#define CONNECT_ADDRESS_TEMPLATE_TCP "tcp-192.168.15.%u:60002"
40 * Struct to store information needed in callbacks. 40
41 * 41#define CONNECT_ADDRESS_TEMPLATE_UDP "udp-192.168.15.%u:60002"
42 */ 42
43struct ConnectPeersState 43#define ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.150.%u:60002"
44
45#define ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.150.%u:60002"
46
47#define GLOBAL_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.151.%u:60002"
48
49#define GLOBAL_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.151.%u:60002"
50
51#define PREFIX_TCP "tcp"
52
53#define PREFIX_UDP "udp"
54
55
56
57
58static struct GNUNET_PeerIdentity *
59get_pub_key (unsigned int num, struct GNUNET_TESTING_System *tl_system)
44{ 60{
45 /** 61 struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity);
46 * Context for our asynchronous completion. 62 struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
47 */ 63 GNUNET_CRYPTO_EddsaPublicKey);
48 struct GNUNET_TESTING_AsyncContext ac; 64 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct
49 65 GNUNET_CRYPTO_EddsaPrivateKey);
50 // Label of the cmd which started the test system. 66
51 const char *create_label; 67 priv_key = GNUNET_TESTING_hostkey_get (tl_system,
52 68 num,
53 /** 69 peer);
54 * Number globally identifying the node. 70
55 * 71 GNUNET_CRYPTO_eddsa_key_get_public (priv_key,
56 */ 72 pub_key);
57 uint32_t num; 73 peer->public_key = *pub_key;
74 return peer;
75}
76
77
78static int
79log_nodes (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
80{
81 struct GNUNET_TESTING_NetjailNode *node = value;
82 struct GNUNET_TESTING_NodeConnection *pos_connection;
83 struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix;
84
85 LOG (GNUNET_ERROR_TYPE_ERROR,
86 "plugin: %s space: %u node: %u global: %u\n",
87 node->plugin,
88 node->namespace_n,
89 node->node_n,
90 node->is_global);
91
92 for (pos_connection = node->node_connections_head; NULL != pos_connection;
93 pos_connection = pos_connection->next)
94 {
95
96 LOG (GNUNET_ERROR_TYPE_ERROR,
97 "namespace_n: %u node_n: %u node_type: %u\n",
98 pos_connection->namespace_n,
99 pos_connection->node_n,
100 pos_connection->node_type);
101
102 for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix;
103 pos_prefix =
104 pos_prefix->next)
105 {
106 LOG (GNUNET_ERROR_TYPE_ERROR,
107 "prefix: %s\n",
108 pos_prefix->address_prefix);
109 }
110 }
111 return GNUNET_YES;
112}
113
114
115static int
116log_namespaces (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
117{
118 struct GNUNET_TESTING_NetjailNamespace *namespace = value;
119 struct GNUNET_TESTING_NetjailRouter *router = namespace->router;
120
121 LOG (GNUNET_ERROR_TYPE_ERROR,
122 "router_tcp: %u router_udp: %u spaces: %u\n",
123 router->tcp_port,
124 router->udp_port,
125 namespace->namespace_n);
126 GNUNET_CONTAINER_multishortmap_iterate (namespace->nodes, &log_nodes, NULL);
127 return GNUNET_YES;
128}
129
130
131static int
132log_topo (struct GNUNET_TESTING_NetjailTopology *topology)
133{
134 LOG (GNUNET_ERROR_TYPE_ERROR,
135 "plugin: %s spaces: %u nodes: %u known: %u\n",
136 topology->plugin,
137 topology->namespaces_n,
138 topology->nodes_m,
139 topology->nodes_x);
140
141 GNUNET_CONTAINER_multishortmap_iterate (topology->map_namespaces,
142 log_namespaces, NULL);
143 GNUNET_CONTAINER_multishortmap_iterate (topology->map_globals, &log_nodes,
144 NULL);
145 return GNUNET_YES;
146}
147
148
149static struct GNUNET_TESTING_NodeConnection *
150get_connections (unsigned int num, struct
151 GNUNET_TESTING_NetjailTopology *topology)
152{
153 struct GNUNET_TESTING_NetjailNode *node;
154 struct GNUNET_ShortHashCode *hkey;
155 struct GNUNET_HashCode hc;
156 struct GNUNET_TESTING_NetjailNamespace *namespace;
157 unsigned int namespace_n, node_m;
158
159 log_topo (topology);
160
161 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
162 if (topology->nodes_x >= num)
163 {
164
165 GNUNET_CRYPTO_hash (&num, sizeof(num), &hc);
166 memcpy (hkey,
167 &hc,
168 sizeof (*hkey));
169 node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals,
170 hkey);
171 }
172 else
173 {
174 namespace_n = (unsigned int) floor ((num - topology->nodes_x)
175 / topology->nodes_m);
176 LOG (GNUNET_ERROR_TYPE_ERROR,
177 "num: %u nodes_x: %u nodes_m: %u namespace_n: %u\n",
178 num,
179 topology->nodes_x,
180 topology->nodes_m,
181 namespace_n);
182 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
183 GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc);
184 memcpy (hkey,
185 &hc,
186 sizeof (*hkey));
187 namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces,
188 hkey);
189 node_m = num - topology->nodes_x - topology->nodes_m * (namespace_n - 1);
190 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
191 GNUNET_CRYPTO_hash (&node_m, sizeof(node_m), &hc);
192 memcpy (hkey,
193 &hc,
194 sizeof (*hkey));
195 node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes,
196 hkey);
197 }
198
199
200 return node->node_connections_head;
201}
58 202
59 /**
60 * Label of the cmd to start a peer.
61 *
62 */
63 const char *start_peer_label;
64 203
65 /** 204static unsigned int
66 * The peer identity of this peer. 205calculate_num (struct GNUNET_TESTING_NodeConnection *node_connection,
67 * 206 struct GNUNET_TESTING_NetjailTopology *topology)
68 */ 207{
69 struct GNUNET_PeerIdentity *id; 208 unsigned int n, m, num;
209
210 n = node_connection->namespace_n;
211 m = node_connection->node_n;
70 212
71 struct GNUNET_TESTING_Interpreter *is; 213 if (0 == n)
72}; 214 num = m;
215 else
216 num = (n - 1) * topology->nodes_m + m + topology->nodes_x;
217
218 return num;
219}
220
221
222static char *
223get_address (struct GNUNET_TESTING_NodeConnection *connection,
224 char *prefix)
225{
226 struct GNUNET_TESTING_NetjailNode *node;
227 char *addr;
228
229 node = connection->node;
230 if (connection->namespace_n == node->namespace_n)
231 {
232 if (0 == strcmp (PREFIX_TCP, prefix))
233 {
234
235 GNUNET_asprintf (&addr,
236 CONNECT_ADDRESS_TEMPLATE_TCP,
237 connection->node_n);
238 }
239 else if (0 == strcmp (PREFIX_UDP, prefix))
240 {
241 GNUNET_asprintf (&addr,
242 CONNECT_ADDRESS_TEMPLATE_UDP,
243 connection->node_n);
244 }
245 else
246 {
247 GNUNET_break (0);
248 }
249 }
250 else
251 {
252 if (0 == strcmp (PREFIX_TCP, prefix))
253 {
254
255 GNUNET_asprintf (&addr,
256 ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP,
257 connection->namespace_n);
258 }
259 else if (0 == strcmp (PREFIX_UDP, prefix))
260 {
261 GNUNET_asprintf (&addr,
262 ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP,
263 connection->namespace_n);
264 }
265 else
266 {
267 GNUNET_break (0);
268 }
269 }
270
271 return addr;
272}
73 273
74 274
75/** 275/**
@@ -83,24 +283,17 @@ connect_peers_run (void *cls,
83 struct ConnectPeersState *cps = cls; 283 struct ConnectPeersState *cps = cls;
84 const struct GNUNET_TESTING_Command *system_cmd; 284 const struct GNUNET_TESTING_Command *system_cmd;
85 struct GNUNET_TESTING_System *tl_system; 285 struct GNUNET_TESTING_System *tl_system;
86 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct 286
87 GNUNET_CRYPTO_EddsaPrivateKey); 287
88 struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
89 GNUNET_CRYPTO_EddsaPublicKey);
90 ;
91 const struct GNUNET_TESTING_Command *peer1_cmd; 288 const struct GNUNET_TESTING_Command *peer1_cmd;
92 // const struct GNUNET_TESTING_Command *peer2_cmd;
93 struct GNUNET_TRANSPORT_ApplicationHandle *ah; 289 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
94 struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity); 290 struct GNUNET_PeerIdentity *peer;
95 char *addr; 291 char *addr;
96 // struct GNUNET_TIME_Absolute t;
97 char *hello;
98 // size_t *hello_size;
99 enum GNUNET_NetworkType nt = 0; 292 enum GNUNET_NetworkType nt = 0;
100 char *peer_id;
101 struct GNUNET_PeerIdentity *id;
102 struct GNUNET_PeerIdentity *other = GNUNET_new (struct GNUNET_PeerIdentity);
103 uint32_t num; 293 uint32_t num;
294 struct GNUNET_TESTING_NodeConnection *pos_connection;
295 struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix;
296 unsigned int con_num = 0;
104 297
105 cps->is = is; 298 cps->is = is;
106 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is, 299 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
@@ -108,121 +301,82 @@ connect_peers_run (void *cls,
108 GNUNET_TRANSPORT_get_trait_application_handle (peer1_cmd, 301 GNUNET_TRANSPORT_get_trait_application_handle (peer1_cmd,
109 &ah); 302 &ah);
110 303
111 GNUNET_TRANSPORT_get_trait_hello (peer1_cmd,
112 &hello);
113
114 GNUNET_TRANSPORT_get_trait_peer_id (peer1_cmd,
115 &id);
116
117 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is, 304 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
118 cps->create_label); 305 cps->create_label);
119 GNUNET_TESTING_get_trait_test_system (system_cmd, 306 GNUNET_TESTING_get_trait_test_system (system_cmd,
120 &tl_system); 307 &tl_system);
121 308
122 if (2 == cps->num) 309 cps->tl_system = tl_system;
123 num = 1;
124 else
125 num = 2;
126 310
311 cps->node_connections_head = get_connections (cps->num, cps->topology);
127 312
128 // if (strstr (hello, "60002") != NULL) 313 for (pos_connection = cps->node_connections_head; NULL != pos_connection;
129 if (2 == num) 314 pos_connection = pos_connection->next)
130 {
131 addr = "tcp-192.168.15.2:60002";
132 peer_id = "F2F3X9G1YNCTXKK7A4J6M4ZM4BBSKC9DEXZVHCWQ475M0C7PNWCG";
133 }
134 else
135 { 315 {
136 addr = "tcp-192.168.15.1:60002"; 316 con_num++;
137 peer_id = "4TTC9WBSVP9RJT6DVEZ7E0TDW7TQXC11NR1EMR2F8ARS87WZ2730"; 317 num = calculate_num (pos_connection, cps->topology);
318 for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix;
319 pos_prefix =
320 pos_prefix->next)
321 {
322
323 LOG (GNUNET_ERROR_TYPE_ERROR,
324 "prefix: %s\n",
325 pos_prefix->address_prefix);
326
327 addr = get_address (pos_connection, pos_prefix->address_prefix);
328
329 peer = get_pub_key (num, tl_system);
330
331 LOG (GNUNET_ERROR_TYPE_ERROR,
332 "num: %u pub_key %s addr: %s\n",
333 num,
334 GNUNET_CRYPTO_eddsa_public_key_to_string (&(peer->public_key)),
335 addr);
336
337 cps->id = peer;
338
339 GNUNET_TRANSPORT_application_validate (ah,
340 peer,
341 nt,
342 addr);
343 }
138 } 344 }
139 345 cps->con_num = con_num;
140 priv_key = GNUNET_TESTING_hostkey_get (tl_system,
141 num,
142 other);
143
144 GNUNET_CRYPTO_eddsa_key_get_public (priv_key,
145 pub_key);
146
147 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
148 strlen (peer_id),
149 &peer->public_key);
150
151 peer->public_key = *pub_key;
152
153 LOG (GNUNET_ERROR_TYPE_ERROR,
154 "\nnum: %u\n peer_id: %s\n pub_key %s\n",
155 num,
156 peer_id,
157 GNUNET_CRYPTO_eddsa_public_key_to_string (pub_key));
158
159 cps->id = peer;
160
161 // TODO This does not work, because the other peer is running in another local loop. We need to message between different local loops. For now we will create the hello manually with the known information about the other local peers.
162 // ---------------------------------------------
163 /*peer2_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->peer2_label);
164 GNUNET_TRANSPORT_get_trait_peer_id (peer2_cmd,
165 &id);
166 GNUNET_TRANSPORT_get_trait_hello (peer2_cmd,
167 &hello);
168 GNUNET_TRANSPORT_get_trait_hello_size (peer2_cmd,
169 &hello_size);
170
171 addr = GNUNET_HELLO_extract_address (hello,
172 *hello_size,
173 id,
174 &nt,
175 &t);*/
176
177 // ----------------------------------------------
178
179
180 GNUNET_TRANSPORT_application_validate (ah,
181 peer,
182 nt,
183 addr);
184} 346}
185 347
186 348
187/** 349/**
188 * The finish function of this cmd will check if the peer we are trying to connect to is in the connected peers map of the start peer cmd for this peer. 350 * Callback from start peer cmd for signaling a peer got connected.
189 * 351 *
190 */ 352 */
191// FIXME: this needs a complete rewrite! 353static void *
192static int 354notify_connect (void *cls,
193connect_peers_finish (void *cls, 355 const struct GNUNET_PeerIdentity *peer,
194 GNUNET_SCHEDULER_TaskCallback cont, 356 struct GNUNET_MQ_Handle *mq)
195 void *cont_cls)
196{ 357{
197 struct ConnectPeersState *cps = cls; 358 struct ConnectPeersState *cps = cls;
198 const struct GNUNET_TESTING_Command *peer1_cmd; 359 struct GNUNET_PeerIdentity *peer_connection;
199 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map; 360 unsigned int con_num = 0;
200 unsigned int ret; 361 struct GNUNET_TESTING_NodeConnection *pos_connection;
201 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode); 362 unsigned int num;
202 struct GNUNET_HashCode hc; 363 void *ret = NULL;
203 int node_number; 364
204 365 for (pos_connection = cps->node_connections_head; NULL != pos_connection;
205 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->is, 366 pos_connection = pos_connection->next)
206 cps->start_peer_label); 367 {
207 GNUNET_TRANSPORT_get_trait_connected_peers_map (peer1_cmd, 368 num = calculate_num (pos_connection, cps->topology);
208 &connected_peers_map); 369 peer_connection = get_pub_key (num, cps->tl_system);
209 370 if (0 == GNUNET_memcmp (peer,
210 node_number = 1; 371 peer_connection))
211 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc); 372 con_num++;
373 }
212 374
213 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
214 memcpy (key,
215 &hc,
216 sizeof (*key));
217 ret = GNUNET_CONTAINER_multishortmap_contains (connected_peers_map,
218 key);
219 375
220 if (GNUNET_YES == ret) 376 if (cps->con_num == con_num)
221 { 377 {
222 cont (cont_cls); 378 GNUNET_TESTING_async_finish (&cps->ac);
223 } 379 }
224
225 GNUNET_free (key);
226 return ret; 380 return ret;
227} 381}
228 382
@@ -241,11 +395,58 @@ connect_peers_cleanup (void *cls)
241} 395}
242 396
243 397
398/**
399 * This function prepares an array with traits.
400 *
401 */
402static int
403connect_peers_traits (void *cls,
404 const void **ret,
405 const char *trait,
406 unsigned int index)
407{
408 struct StartPeerState *cps = cls;
409 struct GNUNET_TESTING_Trait traits[] = {
410 {
411 .index = 0,
412 .trait_name = "state",
413 .ptr = (const void *) cps,
414 },
415 GNUNET_TESTING_trait_end ()
416 };
417 return GNUNET_TESTING_get_trait (traits,
418 ret,
419 trait,
420 index);
421}
422
423
424/**
425 * Function to get the trait with the struct ConnectPeersState.
426 *
427 * @param[out] sps struct ConnectPeersState.
428 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
429 *
430 */
431int
432GNUNET_TESTING_get_trait_connect_peer_state (
433 const struct GNUNET_TESTING_Command *cmd,
434 struct ConnectPeersState **cps)
435{
436 return cmd->traits (cmd->cls,
437 (const void **) cps,
438 "state",
439 (unsigned int) 0);
440}
441
442
244struct GNUNET_TESTING_Command 443struct GNUNET_TESTING_Command
245GNUNET_TRANSPORT_cmd_connect_peers (const char *label, 444GNUNET_TRANSPORT_cmd_connect_peers (const char *label,
246 const char *start_peer_label, 445 const char *start_peer_label,
247 const char *create_label, 446 const char *create_label,
248 uint32_t num) 447 uint32_t num,
448 struct GNUNET_TESTING_NetjailTopology *
449 topology)
249{ 450{
250 struct ConnectPeersState *cps; 451 struct ConnectPeersState *cps;
251 452
@@ -253,6 +454,8 @@ GNUNET_TRANSPORT_cmd_connect_peers (const char *label,
253 cps->start_peer_label = start_peer_label; 454 cps->start_peer_label = start_peer_label;
254 cps->num = num; 455 cps->num = num;
255 cps->create_label = create_label; 456 cps->create_label = create_label;
457 cps->topology = topology;
458 cps->notify_connect = &notify_connect;
256 459
257 { 460 {
258 struct GNUNET_TESTING_Command cmd = { 461 struct GNUNET_TESTING_Command cmd = {
@@ -260,7 +463,8 @@ GNUNET_TRANSPORT_cmd_connect_peers (const char *label,
260 .label = label, 463 .label = label,
261 .run = &connect_peers_run, 464 .run = &connect_peers_run,
262 .ac = &cps->ac, 465 .ac = &cps->ac,
263 .cleanup = &connect_peers_cleanup 466 .cleanup = &connect_peers_cleanup,
467 .traits = &connect_peers_traits
264 }; 468 };
265 469
266 return cmd; 470 return cmd;
diff --git a/src/transport/transport_api_cmd_connecting_peers_v2.c b/src/transport/transport_api_cmd_connecting_peers_v2.c
deleted file mode 100644
index 8d0f74916..000000000
--- a/src/transport/transport_api_cmd_connecting_peers_v2.c
+++ /dev/null
@@ -1,237 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_transport_application_service.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_transport_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39#define CONNECT_ADDRESS_TEMPLATE "tcp-192.168.15.%u:60002"
40
41/**
42 * Struct to store information needed in callbacks.
43 *
44 */
45struct ConnectPeersState
46{
47 /**
48 * Context for our asynchronous completion.
49 */
50 struct GNUNET_TESTING_AsyncContext ac;
51
52 // Label of the cmd which started the test system.
53 const char *create_label;
54
55 /**
56 * Number globally identifying the node.
57 *
58 */
59 uint32_t num;
60
61 /**
62 * Label of the cmd to start a peer.
63 *
64 */
65 const char *start_peer_label;
66
67 /**
68 * The peer identity of this peer.
69 *
70 */
71 struct GNUNET_PeerIdentity *id;
72
73 struct GNUNET_TESTING_Interpreter *is;
74};
75
76
77/**
78 * The run method of this cmd will connect to peers.
79 *
80 */
81static void
82connect_peers_run (void *cls,
83 struct GNUNET_TESTING_Interpreter *is)
84{
85 struct ConnectPeersState *cps = cls;
86 const struct GNUNET_TESTING_Command *system_cmd;
87 struct GNUNET_TESTING_System *tl_system;
88 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct
89 GNUNET_CRYPTO_EddsaPrivateKey);
90 struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
91 GNUNET_CRYPTO_EddsaPublicKey);
92 const struct GNUNET_TESTING_Command *peer1_cmd;
93 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
94 struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity);
95 char *addr;
96 enum GNUNET_NetworkType nt = 0;
97 struct GNUNET_PeerIdentity *other = GNUNET_new (struct GNUNET_PeerIdentity);
98 uint32_t num;
99
100 cps->is = is;
101 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
102 cps->start_peer_label);
103 GNUNET_TRANSPORT_get_trait_application_handle_v2 (peer1_cmd,
104 &ah);
105
106 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
107 cps->create_label);
108 GNUNET_TESTING_get_trait_test_system (system_cmd,
109 &tl_system);
110
111 if (1 == cps->num)
112 {
113 num = 2;
114 // addr = "tcp-192.168.15.2:60002";
115 }
116 else
117 {
118 num = 1;
119 // addr = "tcp-192.168.15.1:60002";
120 }
121
122 GNUNET_asprintf (&addr,
123 CONNECT_ADDRESS_TEMPLATE,
124 num);
125
126 priv_key = GNUNET_TESTING_hostkey_get (tl_system,
127 num,
128 other);
129
130 GNUNET_CRYPTO_eddsa_key_get_public (priv_key,
131 pub_key);
132
133
134 peer->public_key = *pub_key;
135
136 LOG (GNUNET_ERROR_TYPE_ERROR,
137 "num: %u pub_key %s\n",
138 num,
139 GNUNET_CRYPTO_eddsa_public_key_to_string (pub_key));
140
141 cps->id = peer;
142
143 GNUNET_TRANSPORT_application_validate (ah,
144 peer,
145 nt,
146 addr);
147}
148
149
150/**
151 * The finish function of this cmd will check if the peer we are trying to connect to is in the connected peers map of the start peer cmd for this peer.
152 *
153 */
154static int
155connect_peers_finish (void *cls,
156 GNUNET_SCHEDULER_TaskCallback cont,
157 void *cont_cls)
158{
159 struct ConnectPeersState *cps = cls;
160 const struct GNUNET_TESTING_Command *peer1_cmd;
161 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
162 unsigned int ret;
163 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
164 struct GNUNET_HashCode hc;
165 int node_number;
166
167 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->is,
168 cps->start_peer_label);
169 GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (peer1_cmd,
170 &connected_peers_map);
171
172 node_number = 1;
173 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
174
175 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
176 memcpy (key,
177 &hc,
178 sizeof (*key));
179 ret = GNUNET_CONTAINER_multishortmap_contains (connected_peers_map,
180 key);
181
182 if (GNUNET_YES == ret)
183 {
184 cont (cont_cls);
185 }
186
187 GNUNET_free (key);
188 return ret;
189}
190
191
192/**
193 * The cleanup function of this cmd frees resources the cmd allocated.
194 *
195 */
196static void
197connect_peers_cleanup (void *cls)
198{
199 struct ConnectPeersState *cps = cls;
200
201 GNUNET_free (cps->id);
202 GNUNET_free (cps);
203}
204
205
206/**
207 * Create command.
208 *
209 * @param label name for command.
210 * @param start_peer_label Label of the cmd to start a peer.
211 * @return command.
212 */
213struct GNUNET_TESTING_Command
214GNUNET_TRANSPORT_cmd_connect_peers_v2 (const char *label,
215 const char *start_peer_label,
216 const char *create_label,
217 uint32_t num)
218{
219 struct ConnectPeersState *cps;
220
221 cps = GNUNET_new (struct ConnectPeersState);
222 cps->start_peer_label = start_peer_label;
223 cps->num = num;
224 cps->create_label = create_label;
225
226 {
227 struct GNUNET_TESTING_Command cmd = {
228 .cls = cps,
229 .label = label,
230 .run = &connect_peers_run,
231 .ac = &cps->ac,
232 .cleanup = &connect_peers_cleanup
233 };
234
235 return cmd;
236 }
237}
diff --git a/src/transport/transport_api_cmd_connecting_peers_v3.c b/src/transport/transport_api_cmd_connecting_peers_v3.c
deleted file mode 100644
index ae32a087a..000000000
--- a/src/transport/transport_api_cmd_connecting_peers_v3.c
+++ /dev/null
@@ -1,496 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_transport_application_service.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_transport_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39#define CONNECT_ADDRESS_TEMPLATE_TCP "tcp-192.168.15.%u:60002"
40
41#define CONNECT_ADDRESS_TEMPLATE_UDP "udp-192.168.15.%u:60002"
42
43#define ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.150.%u:60002"
44
45#define ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.150.%u:60002"
46
47#define GLOBAL_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.151.%u:60002"
48
49#define GLOBAL_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.151.%u:60002"
50
51#define PREFIX_TCP "tcp"
52
53#define PREFIX_UDP "udp"
54
55/**
56 * Struct to store information needed in callbacks.
57 *
58 */
59struct ConnectPeersState
60{
61 /**
62 * Context for our asynchronous completion.
63 */
64 struct GNUNET_TESTING_AsyncContext ac;
65
66 /**
67 * The testing system of this node.
68 */
69 struct GNUNET_TESTING_System *tl_system;
70
71 // Label of the cmd which started the test system.
72 const char *create_label;
73
74 /**
75 * Number globally identifying the node.
76 *
77 */
78 uint32_t num;
79
80 /**
81 * Label of the cmd to start a peer.
82 *
83 */
84 const char *start_peer_label;
85
86 /**
87 * The peer identity of this peer.
88 *
89 */
90 struct GNUNET_PeerIdentity *id;
91
92 /**
93 * The topology of the test setup.
94 */
95 struct GNUNET_TESTING_NetjailTopology *topology;
96
97 /**
98 * Connections to other peers.
99 */
100 struct GNUNET_TESTING_NodeConnection *node_connections_head;
101
102 struct GNUNET_TESTING_Interpreter *is;
103
104 /**
105 * Number of connections.
106 */
107 unsigned int con_num;
108};
109
110
111static struct GNUNET_PeerIdentity *
112get_pub_key (unsigned int num, struct GNUNET_TESTING_System *tl_system)
113{
114 struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity);
115 struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
116 GNUNET_CRYPTO_EddsaPublicKey);
117 struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct
118 GNUNET_CRYPTO_EddsaPrivateKey);
119
120 priv_key = GNUNET_TESTING_hostkey_get (tl_system,
121 num,
122 peer);
123
124 GNUNET_CRYPTO_eddsa_key_get_public (priv_key,
125 pub_key);
126 peer->public_key = *pub_key;
127 return peer;
128}
129
130
131static int
132log_nodes (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
133{
134 struct GNUNET_TESTING_NetjailNode *node = value;
135 struct GNUNET_TESTING_NodeConnection *pos_connection;
136 struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix;
137
138 LOG (GNUNET_ERROR_TYPE_ERROR,
139 "plugin: %s space: %u node: %u global: %u\n",
140 node->plugin,
141 node->namespace_n,
142 node->node_n,
143 node->is_global);
144
145 for (pos_connection = node->node_connections_head; NULL != pos_connection;
146 pos_connection = pos_connection->next)
147 {
148
149 LOG (GNUNET_ERROR_TYPE_ERROR,
150 "namespace_n: %u node_n: %u node_type: %u\n",
151 pos_connection->namespace_n,
152 pos_connection->node_n,
153 pos_connection->node_type);
154
155 for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix;
156 pos_prefix =
157 pos_prefix->next)
158 {
159 LOG (GNUNET_ERROR_TYPE_ERROR,
160 "prefix: %s\n",
161 pos_prefix->address_prefix);
162 }
163 }
164 return GNUNET_YES;
165}
166
167
168static int
169log_namespaces (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
170{
171 struct GNUNET_TESTING_NetjailNamespace *namespace = value;
172 struct GNUNET_TESTING_NetjailRouter *router = namespace->router;
173
174 LOG (GNUNET_ERROR_TYPE_ERROR,
175 "router_tcp: %u router_udp: %u spaces: %u\n",
176 router->tcp_port,
177 router->udp_port,
178 namespace->namespace_n);
179 GNUNET_CONTAINER_multishortmap_iterate (namespace->nodes, &log_nodes, NULL);
180 return GNUNET_YES;
181}
182
183
184static int
185log_topo (struct GNUNET_TESTING_NetjailTopology *topology)
186{
187 LOG (GNUNET_ERROR_TYPE_ERROR,
188 "plugin: %s spaces: %u nodes: %u known: %u\n",
189 topology->plugin,
190 topology->namespaces_n,
191 topology->nodes_m,
192 topology->nodes_x);
193
194 GNUNET_CONTAINER_multishortmap_iterate (topology->map_namespaces,
195 log_namespaces, NULL);
196 GNUNET_CONTAINER_multishortmap_iterate (topology->map_globals, &log_nodes,
197 NULL);
198 return GNUNET_YES;
199}
200
201
202static struct GNUNET_TESTING_NodeConnection *
203get_connections (unsigned int num, struct
204 GNUNET_TESTING_NetjailTopology *topology)
205{
206 struct GNUNET_TESTING_NetjailNode *node;
207 struct GNUNET_ShortHashCode *hkey;
208 struct GNUNET_HashCode hc;
209 struct GNUNET_TESTING_NetjailNamespace *namespace;
210 unsigned int namespace_n, node_m;
211
212 log_topo (topology);
213
214 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
215 if (topology->nodes_x >= num)
216 {
217
218 GNUNET_CRYPTO_hash (&num, sizeof(num), &hc);
219 memcpy (hkey,
220 &hc,
221 sizeof (*hkey));
222 node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals,
223 hkey);
224 }
225 else
226 {
227 namespace_n = (unsigned int) floor ((num - topology->nodes_x)
228 / topology->nodes_m);
229 LOG (GNUNET_ERROR_TYPE_ERROR,
230 "num: %u nodes_x: %u nodes_m: %u namespace_n: %u\n",
231 num,
232 topology->nodes_x,
233 topology->nodes_m,
234 namespace_n);
235 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
236 GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc);
237 memcpy (hkey,
238 &hc,
239 sizeof (*hkey));
240 namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces,
241 hkey);
242 node_m = num - topology->nodes_x - topology->nodes_m * (namespace_n - 1);
243 hkey = GNUNET_new (struct GNUNET_ShortHashCode);
244 GNUNET_CRYPTO_hash (&node_m, sizeof(node_m), &hc);
245 memcpy (hkey,
246 &hc,
247 sizeof (*hkey));
248 node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes,
249 hkey);
250 }
251
252
253 return node->node_connections_head;
254}
255
256
257static unsigned int
258calculate_num (struct GNUNET_TESTING_NodeConnection *node_connection,
259 struct GNUNET_TESTING_NetjailTopology *topology)
260{
261 unsigned int n, m, num;
262
263 n = node_connection->namespace_n;
264 m = node_connection->node_n;
265
266 if (0 == n)
267 num = m;
268 else
269 num = (n - 1) * topology->nodes_m + m + topology->nodes_x;
270
271 return num;
272}
273
274
275static char *
276get_address (struct GNUNET_TESTING_NodeConnection *connection,
277 char *prefix)
278{
279 struct GNUNET_TESTING_NetjailNode *node;
280 char *addr;
281
282 node = connection->node;
283 if (connection->namespace_n == node->namespace_n)
284 {
285 if (0 == strcmp (PREFIX_TCP, prefix))
286 {
287
288 GNUNET_asprintf (&addr,
289 CONNECT_ADDRESS_TEMPLATE_TCP,
290 connection->node_n);
291 }
292 else if (0 == strcmp (PREFIX_UDP, prefix))
293 {
294 GNUNET_asprintf (&addr,
295 CONNECT_ADDRESS_TEMPLATE_UDP,
296 connection->node_n);
297 }
298 else
299 {
300 GNUNET_break (0);
301 }
302 }
303 else
304 {
305 if (0 == strcmp (PREFIX_TCP, prefix))
306 {
307
308 GNUNET_asprintf (&addr,
309 ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP,
310 connection->namespace_n);
311 }
312 else if (0 == strcmp (PREFIX_UDP, prefix))
313 {
314 GNUNET_asprintf (&addr,
315 ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP,
316 connection->namespace_n);
317 }
318 else
319 {
320 GNUNET_break (0);
321 }
322 }
323
324 return addr;
325}
326
327
328/**
329 * The run method of this cmd will connect to peers.
330 *
331 */
332static void
333connect_peers_run (void *cls,
334 struct GNUNET_TESTING_Interpreter *is)
335{
336 struct ConnectPeersState *cps = cls;
337 const struct GNUNET_TESTING_Command *system_cmd;
338 struct GNUNET_TESTING_System *tl_system;
339
340
341 const struct GNUNET_TESTING_Command *peer1_cmd;
342 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
343 struct GNUNET_PeerIdentity *peer;
344 char *addr;
345 enum GNUNET_NetworkType nt = 0;
346 uint32_t num;
347 struct GNUNET_TESTING_NodeConnection *pos_connection;
348 struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix;
349 unsigned int con_num = 0;
350
351 cps->is = is;
352 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
353 cps->start_peer_label);
354 GNUNET_TRANSPORT_get_trait_application_handle_v2 (peer1_cmd,
355 &ah);
356
357 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
358 cps->create_label);
359 GNUNET_TESTING_get_trait_test_system (system_cmd,
360 &tl_system);
361
362 cps->tl_system = tl_system;
363
364 cps->node_connections_head = get_connections (cps->num, cps->topology);
365
366 for (pos_connection = cps->node_connections_head; NULL != pos_connection;
367 pos_connection = pos_connection->next)
368 {
369 con_num++;
370 num = calculate_num (pos_connection, cps->topology);
371 for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix;
372 pos_prefix =
373 pos_prefix->next)
374 {
375
376 LOG (GNUNET_ERROR_TYPE_ERROR,
377 "prefix: %s\n",
378 pos_prefix->address_prefix);
379
380 addr = get_address (pos_connection, pos_prefix->address_prefix);
381
382 peer = get_pub_key (num, tl_system);
383
384 LOG (GNUNET_ERROR_TYPE_ERROR,
385 "num: %u pub_key %s addr: %s\n",
386 num,
387 GNUNET_CRYPTO_eddsa_public_key_to_string (&(peer->public_key)),
388 addr);
389
390 cps->id = peer;
391
392 GNUNET_TRANSPORT_application_validate (ah,
393 peer,
394 nt,
395 addr);
396 }
397 }
398 cps->con_num = con_num;
399}
400
401
402/**
403 * The finish function of this cmd will check if the peers we are trying to
404 * connect to are in the connected peers map of the start peer cmd for this peer.
405 *
406 */
407static int
408connect_peers_finish (void *cls,
409 GNUNET_SCHEDULER_TaskCallback cont,
410 void *cont_cls)
411{
412 struct ConnectPeersState *cps = cls;
413 const struct GNUNET_TESTING_Command *peer1_cmd;
414 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
415 unsigned int ret;
416 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
417 struct GNUNET_HashCode hc;
418 struct GNUNET_PeerIdentity *peer;
419 unsigned int con_num = 0;
420 struct GNUNET_TESTING_NodeConnection *pos_connection;
421 unsigned int num;
422
423 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->is,
424 cps->start_peer_label);
425 GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (peer1_cmd,
426 &connected_peers_map);
427
428 for (pos_connection = cps->node_connections_head; NULL != pos_connection;
429 pos_connection = pos_connection->next)
430 {
431 num = calculate_num (pos_connection, cps->topology);
432 peer = get_pub_key (num, cps->tl_system);
433 GNUNET_CRYPTO_hash (&(peer->public_key), sizeof(peer->public_key), &hc);
434 memcpy (key,
435 &hc,
436 sizeof (*key));
437 if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains (
438 connected_peers_map,
439 key))
440 con_num++;
441 }
442
443
444 if (cps->con_num == con_num)
445 {
446 cont (cont_cls);
447 ret = GNUNET_YES;
448 }
449
450 GNUNET_free (key);
451 return ret;
452}
453
454
455/**
456 * The cleanup function of this cmd frees resources the cmd allocated.
457 *
458 */
459static void
460connect_peers_cleanup (void *cls)
461{
462 struct ConnectPeersState *cps = cls;
463
464 GNUNET_free (cps->id);
465 GNUNET_free (cps);
466}
467
468
469struct GNUNET_TESTING_Command
470GNUNET_TRANSPORT_cmd_connect_peers_v3 (const char *label,
471 const char *start_peer_label,
472 const char *create_label,
473 uint32_t num,
474 struct GNUNET_TESTING_NetjailTopology *
475 topology)
476{
477 struct ConnectPeersState *cps;
478
479 cps = GNUNET_new (struct ConnectPeersState);
480 cps->start_peer_label = start_peer_label;
481 cps->num = num;
482 cps->create_label = create_label;
483 cps->topology = topology;
484
485 {
486 struct GNUNET_TESTING_Command cmd = {
487 .cls = cps,
488 .label = label,
489 .run = &connect_peers_run,
490 .ac = &cps->ac,
491 .cleanup = &connect_peers_cleanup
492 };
493
494 return cmd;
495 }
496}
diff --git a/src/transport/transport_api_cmd_send_simple.c b/src/transport/transport_api_cmd_send_simple.c
index 1599dfe97..0631f7051 100644
--- a/src/transport/transport_api_cmd_send_simple.c
+++ b/src/transport/transport_api_cmd_send_simple.c
@@ -36,18 +36,6 @@
36struct SendSimpleState 36struct SendSimpleState
37{ 37{
38 /** 38 /**
39 * The number of the local node of the actual network namespace.
40 *
41 */
42 char *m;
43
44 /**
45 * The number of the actual namespace.
46 *
47 */
48 char *n;
49
50 /**
51 * Number globally identifying the node. 39 * Number globally identifying the node.
52 * 40 *
53 */ 41 */
@@ -146,16 +134,12 @@ send_simple_run (void *cls,
146 */ 134 */
147struct GNUNET_TESTING_Command 135struct GNUNET_TESTING_Command
148GNUNET_TRANSPORT_cmd_send_simple (const char *label, 136GNUNET_TRANSPORT_cmd_send_simple (const char *label,
149 char *m, 137 const char *start_peer_label,
150 char *n, 138 uint32_t num)
151 uint32_t num,
152 const char *start_peer_label)
153{ 139{
154 struct SendSimpleState *sss; 140 struct SendSimpleState *sss;
155 141
156 sss = GNUNET_new (struct SendSimpleState); 142 sss = GNUNET_new (struct SendSimpleState);
157 sss->m = m;
158 sss->n = n;
159 sss->num = num; 143 sss->num = num;
160 sss->start_peer_label = start_peer_label; 144 sss->start_peer_label = start_peer_label;
161 { 145 {
diff --git a/src/transport/transport_api_cmd_send_simple_v2.c b/src/transport/transport_api_cmd_send_simple_v2.c
deleted file mode 100644
index 8d65eb684..000000000
--- a/src/transport/transport_api_cmd_send_simple_v2.c
+++ /dev/null
@@ -1,156 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "transport-testing2.h"
30#include "transport-testing-cmds.h"
31
32/**
33 * Struct to hold information for callbacks.
34 *
35 */
36struct SendSimpleState
37{
38 /**
39 * Number globally identifying the node.
40 *
41 */
42 uint32_t num;
43
44 /**
45 * Label of the cmd to start a peer.
46 *
47 */
48 const char *start_peer_label;
49};
50
51
52/**
53 * Trait function of this cmd does nothing.
54 *
55 */
56static int
57send_simple_traits (void *cls,
58 const void **ret,
59 const char *trait,
60 unsigned int index)
61{
62 return GNUNET_OK;
63}
64
65
66/**
67 * The cleanup function of this cmd frees resources the cmd allocated.
68 *
69 */
70static void
71send_simple_cleanup (void *cls)
72{
73 struct SendSimpleState *sss = cls;
74
75 GNUNET_free (sss);
76}
77
78
79/**
80 * The run method of this cmd will send a simple message to the connected peer.
81 *
82 */
83static void
84send_simple_run (void *cls,
85 struct GNUNET_TESTING_Interpreter *is)
86{
87 struct SendSimpleState *sss = cls;
88 struct GNUNET_MQ_Envelope *env;
89 struct GNUNET_TRANSPORT_TESTING_TestMessage *test;
90 struct GNUNET_MQ_Handle *mq;
91 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
92 const struct GNUNET_TESTING_Command *peer1_cmd;
93 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
94 struct GNUNET_HashCode hc;
95 int node_number;
96
97 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
98 sss->start_peer_label);
99 GNUNET_TRANSPORT_get_trait_connected_peers_map (peer1_cmd,
100 &connected_peers_map);
101
102 node_number = 1;
103 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
104 memcpy (key,
105 &hc,
106 sizeof (*key));
107
108 mq = GNUNET_CONTAINER_multishortmap_get (connected_peers_map,
109 key);
110
111 env = GNUNET_MQ_msg_extra (test,
112 2600 - sizeof(*test),
113 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE);
114 test->num = htonl (sss->num);
115 memset (&test[1],
116 sss->num,
117 2600 - sizeof(*test));
118 GNUNET_MQ_send (mq,
119 env);
120 GNUNET_free (key);
121
122}
123
124
125/**
126 * Create command.
127 *
128 * @param label name for command.
129 * @param m The number of the local node of the actual network namespace.
130 * @param n The number of the actual namespace.
131 * @param num Number globally identifying the node.
132 * @param start_peer_label Label of the cmd to start a peer.
133 * @return command.
134 */
135struct GNUNET_TESTING_Command
136GNUNET_TRANSPORT_cmd_send_simple_v2 (const char *label,
137 const char *start_peer_label,
138 uint32_t num)
139{
140 struct SendSimpleState *sss;
141
142 sss = GNUNET_new (struct SendSimpleState);
143 sss->num = num;
144 sss->start_peer_label = start_peer_label;
145 {
146 struct GNUNET_TESTING_Command cmd = {
147 .cls = sss,
148 .label = label,
149 .run = &send_simple_run,
150 .cleanup = &send_simple_cleanup,
151 .traits = &send_simple_traits
152 };
153
154 return cmd;
155 }
156}
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
index 44fc68807..dc19f10eb 100644
--- a/src/transport/transport_api_cmd_start_peer.c
+++ b/src/transport/transport_api_cmd_start_peer.c
@@ -51,7 +51,6 @@ hello_iter_cb (void *cb_cls,
51 const char *emsg) 51 const char *emsg)
52{ 52{
53 struct StartPeerState *sps = cb_cls; 53 struct StartPeerState *sps = cb_cls;
54
55 if (NULL == record) 54 if (NULL == record)
56 { 55 {
57 sps->pic = NULL; 56 sps->pic = NULL;
@@ -66,7 +65,7 @@ hello_iter_cb (void *cb_cls,
66 65
67 GNUNET_PEERSTORE_iterate_cancel (sps->pic); 66 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
68 sps->pic = NULL; 67 sps->pic = NULL;
69 sps->finished = GNUNET_YES; 68 GNUNET_TESTING_async_finish (&sps->ac);
70} 69}
71 70
72 71
@@ -122,7 +121,7 @@ notify_connect (void *cls,
122 struct StartPeerState *sps = cls; 121 struct StartPeerState *sps = cls;
123 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode); 122 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
124 struct GNUNET_HashCode hc; 123 struct GNUNET_HashCode hc;
125 int node_number; 124 struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
126 125
127 void *ret = NULL; 126 void *ret = NULL;
128 127
@@ -133,9 +132,7 @@ notify_connect (void *cls,
133 sps->no, 132 sps->no,
134 GNUNET_i2s (&sps->id)); 133 GNUNET_i2s (&sps->id));
135 134
136 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node. 135 GNUNET_CRYPTO_hash (&public_key, sizeof(public_key), &hc);
137 node_number = 1;
138 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
139 136
140 137
141 memcpy (key, 138 memcpy (key,
@@ -147,6 +144,11 @@ notify_connect (void *cls,
147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
148 145
149 GNUNET_free (key); 146 GNUNET_free (key);
147
148 sps->notify_connect (cls,
149 peer,
150 mq);
151
150 // TODO what does the handler function need? 152 // TODO what does the handler function need?
151 return ret; 153 return ret;
152} 154}
@@ -167,7 +169,8 @@ start_peer_run (void *cls,
167 struct GNUNET_TESTING_System *tl_system; 169 struct GNUNET_TESTING_System *tl_system;
168 char *home; 170 char *home;
169 char *transport_unix_path; 171 char *transport_unix_path;
170 char *communicator_unix_path; 172 char *tcp_communicator_unix_path;
173 char *udp_communicator_unix_path;
171 char *bindto; 174 char *bindto;
172 175
173 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname)) 176 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
@@ -192,7 +195,11 @@ start_peer_run (void *cls,
192 "$GNUNET_RUNTIME_DIR/tng-p%u.sock", 195 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
193 sps->no); 196 sps->no);
194 197
195 GNUNET_asprintf (&communicator_unix_path, 198 GNUNET_asprintf (&tcp_communicator_unix_path,
199 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
200 sps->no);
201
202 GNUNET_asprintf (&udp_communicator_unix_path,
196 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock", 203 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
197 sps->no); 204 sps->no);
198 205
@@ -200,6 +207,9 @@ start_peer_run (void *cls,
200 "%s:60002", 207 "%s:60002",
201 sps->node_ip); 208 sps->node_ip);
202 209
210 LOG (GNUNET_ERROR_TYPE_ERROR,
211 "node_ip %s\n",
212 bindto);
203 213
204 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME", 214 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
205 home); 215 home);
@@ -208,9 +218,15 @@ start_peer_run (void *cls,
208 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp", 218 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
209 "BINDTO", 219 "BINDTO",
210 bindto); 220 bindto);
221 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
222 "BINDTO",
223 bindto);
211 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp", 224 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
212 "UNIXPATH", 225 "UNIXPATH",
213 communicator_unix_path); 226 tcp_communicator_unix_path);
227 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
228 "UNIXPATH",
229 udp_communicator_unix_path);
214 230
215 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is, 231 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
216 sps->system_label); 232 sps->system_label);
@@ -417,10 +433,9 @@ start_peer_traits (void *cls,
417 * 433 *
418 */ 434 */
419int 435int
420GNUNET_TRANSPORT_get_trait_state (const struct 436GNUNET_TRANSPORT_get_trait_state (
421 GNUNET_TESTING_Command 437 const struct GNUNET_TESTING_Command *cmd,
422 *cmd, 438 struct StartPeerState **sps)
423 struct StartPeerState **sps)
424{ 439{
425 return cmd->traits (cmd->cls, 440 return cmd->traits (cmd->cls,
426 (const void **) sps, 441 (const void **) sps,
@@ -481,7 +496,8 @@ GNUNET_TRANSPORT_get_trait_connected_peers_map (const struct
481 GNUNET_TESTING_Command 496 GNUNET_TESTING_Command
482 *cmd, 497 *cmd,
483 struct 498 struct
484 GNUNET_CONTAINER_MultiShortmap * 499 GNUNET_CONTAINER_MultiShortmap
500 *
485 * 501 *
486 connected_peers_map) 502 connected_peers_map)
487{ 503{
@@ -545,12 +561,11 @@ GNUNET_TRANSPORT_get_trait_peer_id (const struct
545struct GNUNET_TESTING_Command 561struct GNUNET_TESTING_Command
546GNUNET_TRANSPORT_cmd_start_peer (const char *label, 562GNUNET_TRANSPORT_cmd_start_peer (const char *label,
547 const char *system_label, 563 const char *system_label,
548 char *m, 564 uint32_t no,
549 char *n,
550 char *local_m,
551 char *node_ip, 565 char *node_ip,
552 struct GNUNET_MQ_MessageHandler *handlers, 566 struct GNUNET_MQ_MessageHandler *handlers,
553 const char *cfgname) 567 const char *cfgname,
568 GNUNET_TRANSPORT_NotifyConnect notify_connect)
554{ 569{
555 struct StartPeerState *sps; 570 struct StartPeerState *sps;
556 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map = 571 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
@@ -558,14 +573,12 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
558 unsigned int i; 573 unsigned int i;
559 574
560 sps = GNUNET_new (struct StartPeerState); 575 sps = GNUNET_new (struct StartPeerState);
561 sps->m = m; 576 sps->no = no;
562 sps->n = n;
563 sps->local_m = local_m;
564 sps->no = (atoi (n) - 1) * atoi (sps->local_m) + atoi (m);
565 sps->system_label = system_label; 577 sps->system_label = system_label;
566 sps->connected_peers_map = connected_peers_map; 578 sps->connected_peers_map = connected_peers_map;
567 sps->cfgname = cfgname; 579 sps->cfgname = cfgname;
568 sps->node_ip = node_ip; 580 sps->node_ip = node_ip;
581 sps->notify_connect = notify_connect;
569 582
570 if (NULL != handlers) 583 if (NULL != handlers)
571 { 584 {
@@ -578,15 +591,14 @@ GNUNET_TRANSPORT_cmd_start_peer (const char *label,
578 i * sizeof(struct GNUNET_MQ_MessageHandler)); 591 i * sizeof(struct GNUNET_MQ_MessageHandler));
579 } 592 }
580 593
581 { 594 struct GNUNET_TESTING_Command cmd = {
582 struct GNUNET_TESTING_Command cmd = { 595 .cls = sps,
583 .cls = sps, 596 .label = label,
584 .label = label, 597 .run = &start_peer_run,
585 .run = &start_peer_run, 598 .ac = &sps->ac,
586 .cleanup = &start_peer_cleanup, 599 .cleanup = &start_peer_cleanup,
587 .traits = &start_peer_traits 600 .traits = &start_peer_traits
588 }; 601 };
589 602
590 return cmd; 603 return cmd;
591 }
592} 604}
diff --git a/src/transport/transport_api_cmd_start_peer_v2.c b/src/transport/transport_api_cmd_start_peer_v2.c
deleted file mode 100644
index 94799eddd..000000000
--- a/src/transport/transport_api_cmd_start_peer_v2.c
+++ /dev/null
@@ -1,605 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_peerstore_service.h"
30#include "gnunet_transport_core_service.h"
31#include "gnunet_transport_application_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39
40static void
41retrieve_hello (void *cls);
42
43
44/**
45 * Callback delivering the hello of this peer from peerstore.
46 *
47 */
48static void
49hello_iter_cb (void *cb_cls,
50 const struct GNUNET_PEERSTORE_Record *record,
51 const char *emsg)
52{
53 struct StartPeerState_v2 *sps = cb_cls;
54 if (NULL == record)
55 {
56 sps->pic = NULL;
57 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
58 return;
59 }
60 // Check record type et al?
61 sps->hello_size = record->value_size;
62 sps->hello = GNUNET_malloc (sps->hello_size);
63 memcpy (sps->hello, record->value, sps->hello_size);
64 sps->hello[sps->hello_size - 1] = '\0';
65
66 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
67 sps->pic = NULL;
68 sps->finished = GNUNET_YES;
69}
70
71
72/**
73 * Function to start the retrieval task to retrieve the hello of this peer
74 * from the peerstore.
75 *
76 */
77static void
78retrieve_hello (void *cls)
79{
80 struct StartPeerState_v2 *sps = cls;
81 sps->rh_task = NULL;
82 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
83 "transport",
84 &sps->id,
85 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
86 hello_iter_cb,
87 sps);
88
89}
90
91
92/**
93 * This function checks StartPeerState_v2#finished, which is set when the hello was retrieved.
94 *
95 */
96static int
97start_peer_finish (void *cls,
98 GNUNET_SCHEDULER_TaskCallback cont,
99 void *cont_cls)
100{
101 struct StartPeerState_v2 *sps = cls;
102
103 if (GNUNET_YES == sps->finished)
104 {
105 cont (cont_cls);
106 }
107
108 return sps->finished;
109}
110
111
112/**
113 * Disconnect callback for the connection to the core service.
114 *
115 */
116static void
117notify_disconnect (void *cls,
118 const struct GNUNET_PeerIdentity *peer,
119 void *handler_cls)
120{
121 struct StartPeerState_v2 *sps = cls;
122
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Peer %s disconnected from peer %u (`%s')\n",
125 GNUNET_i2s (peer),
126 sps->no,
127 GNUNET_i2s (&sps->id));
128
129}
130
131
132/**
133 * Connect callback for the connection to the core service.
134 *
135 */
136static void *
137notify_connect (void *cls,
138 const struct GNUNET_PeerIdentity *peer,
139 struct GNUNET_MQ_Handle *mq)
140{
141 struct StartPeerState_v2 *sps = cls;
142 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
143 struct GNUNET_HashCode hc;
144 int node_number;
145
146 void *ret = NULL;
147
148
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
150 "Peer %s connected to peer %u (`%s')\n",
151 GNUNET_i2s (peer),
152 sps->no,
153 GNUNET_i2s (&sps->id));
154
155 // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
156 node_number = 1;
157 GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
158
159
160 memcpy (key,
161 &hc,
162 sizeof (*key));
163 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
164 key,
165 mq,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
167
168 GNUNET_free (key);
169 // TODO what does the handler function need?
170 return ret;
171}
172
173
174/**
175 * The run method of this cmd will start all services of a peer to test the transport service.
176 *
177 */
178static void
179start_peer_run (void *cls,
180 struct GNUNET_TESTING_Interpreter *is)
181{
182 struct StartPeerState_v2 *sps = cls;
183 char *emsg = NULL;
184 struct GNUNET_PeerIdentity dummy;
185 const struct GNUNET_TESTING_Command *system_cmd;
186 struct GNUNET_TESTING_System *tl_system;
187 char *home;
188 char *transport_unix_path;
189 char *communicator_unix_path;
190 char *bindto;
191
192 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
193 {
194 LOG (GNUNET_ERROR_TYPE_ERROR,
195 "File not found: `%s'\n",
196 sps->cfgname);
197 GNUNET_TESTING_interpreter_fail (is);
198 return;
199 }
200
201
202 sps->cfg = GNUNET_CONFIGURATION_create ();
203 GNUNET_assert (GNUNET_OK ==
204 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
205
206 GNUNET_asprintf (&home,
207 "$GNUNET_TMP/test-transport/api-tcp-p%u",
208 sps->no);
209
210 GNUNET_asprintf (&transport_unix_path,
211 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
212 sps->no);
213
214 GNUNET_asprintf (&communicator_unix_path,
215 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
216 sps->no);
217
218 GNUNET_asprintf (&bindto,
219 "%s:60002",
220 sps->node_ip);
221
222
223 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
224 home);
225 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
226 transport_unix_path);
227 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
228 "BINDTO",
229 bindto);
230 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
231 "UNIXPATH",
232 communicator_unix_path);
233
234 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
235 sps->system_label);
236 GNUNET_TESTING_get_trait_test_system (system_cmd,
237 &tl_system);
238
239 sps->tl_system = tl_system;
240
241 if (GNUNET_SYSERR ==
242 GNUNET_TESTING_configuration_create (tl_system,
243 sps->cfg))
244 {
245 LOG (GNUNET_ERROR_TYPE_ERROR,
246 "Testing library failed to create unique configuration based on `%s'\n",
247 sps->cfgname);
248 GNUNET_CONFIGURATION_destroy (sps->cfg);
249 GNUNET_TESTING_interpreter_fail (is);
250 return;
251 }
252
253 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
254 sps->cfg,
255 sps->no,
256 NULL,
257 &emsg);
258 if (NULL == sps->peer)
259 {
260 LOG (GNUNET_ERROR_TYPE_ERROR,
261 "Testing library failed to create unique configuration based on `%s': `%s'\n",
262 sps->cfgname,
263 emsg);
264 GNUNET_free (emsg);
265 GNUNET_TESTING_interpreter_fail (is);
266 return;
267 }
268
269 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
270 {
271 LOG (GNUNET_ERROR_TYPE_ERROR,
272 "Testing library failed to create unique configuration based on `%s'\n",
273 sps->cfgname);
274 GNUNET_free (emsg);
275 GNUNET_TESTING_interpreter_fail (is);
276 return;
277 }
278
279 memset (&dummy,
280 '\0',
281 sizeof(dummy));
282
283 GNUNET_TESTING_peer_get_identity (sps->peer,
284 &sps->id);
285
286 if (0 == memcmp (&dummy,
287 &sps->id,
288 sizeof(struct GNUNET_PeerIdentity)))
289 {
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 "Testing library failed to obtain peer identity for peer %u\n",
292 sps->no);
293 GNUNET_free (emsg);
294 GNUNET_TESTING_interpreter_fail (is);
295 return;
296 }
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Peer %u configured with identity `%s'\n",
299 sps->no,
300 GNUNET_i2s_full (&sps->id));
301
302 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
303 NULL,
304 sps->handlers,
305 sps,
306 &notify_connect,
307 &notify_disconnect);
308 if (NULL == sps->th)
309 {
310 LOG (GNUNET_ERROR_TYPE_ERROR,
311 "Failed to connect to transport service for peer `%s': `%s'\n",
312 sps->cfgname,
313 emsg);
314 GNUNET_free (emsg);
315 GNUNET_TESTING_interpreter_fail (is);
316 return;
317 }
318
319 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
320 if (NULL == sps->th)
321 {
322 LOG (GNUNET_ERROR_TYPE_ERROR,
323 "Failed to connect to peerstore service for peer `%s': `%s'\n",
324 sps->cfgname,
325 emsg);
326 GNUNET_free (emsg);
327 GNUNET_TESTING_interpreter_fail (is);
328 return;
329 }
330
331 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
332 if (NULL == sps->ah)
333 {
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
336 sps->cfgname,
337 emsg);
338 GNUNET_free (emsg);
339 GNUNET_TESTING_interpreter_fail (is);
340 return;
341 }
342 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
343}
344
345
346/**
347 * The cleanup function of this cmd frees resources the cmd allocated.
348 *
349 */
350static void
351start_peer_cleanup (void *cls)
352{
353 struct StartPeerState_v2 *sps = cls;
354
355 if (NULL != sps->handlers)
356 {
357 GNUNET_free (sps->handlers);
358 sps->handlers = NULL;
359 }
360 if (NULL != sps->cfg)
361 {
362 GNUNET_CONFIGURATION_destroy (sps->cfg);
363 sps->cfg = NULL;
364 }
365 GNUNET_free (sps->hello);
366 GNUNET_free (sps->connected_peers_map);
367 GNUNET_free (sps);
368}
369
370
371/**
372 * This function prepares an array with traits.
373 *
374 */
375static int
376start_peer_traits (void *cls,
377 const void **ret,
378 const char *trait,
379 unsigned int index)
380{
381 struct StartPeerState_v2 *sps = cls;
382 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
383 struct GNUNET_PeerIdentity *id = &sps->id;
384 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
385 sps->connected_peers_map;
386 char *hello = sps->hello;
387 size_t hello_size = sps->hello_size;
388
389
390 struct GNUNET_TESTING_Trait traits[] = {
391 {
392 .index = 0,
393 .trait_name = "application_handle",
394 .ptr = (const void *) ah,
395 },
396 {
397 .index = 1,
398 .trait_name = "peer_id",
399 .ptr = (const void *) id,
400 },
401 {
402 .index = 2,
403 .trait_name = "connected_peers_map",
404 .ptr = (const void *) connected_peers_map,
405 },
406 {
407 .index = 3,
408 .trait_name = "hello",
409 .ptr = (const void *) hello,
410 },
411 {
412 .index = 4,
413 .trait_name = "hello_size",
414 .ptr = (const void *) hello_size,
415 },
416 {
417 .index = 5,
418 .trait_name = "state",
419 .ptr = (const void *) sps,
420 },
421 GNUNET_TESTING_trait_end ()
422 };
423
424 return GNUNET_TESTING_get_trait (traits,
425 ret,
426 trait,
427 index);
428}
429
430
431/**
432 * Function to get the trait with the struct StartPeerState_v2.
433 *
434 * @param[out] sps struct StartPeerState_v2.
435 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
436 *
437 */
438int
439GNUNET_TRANSPORT_get_trait_state_v2 (const struct
440 GNUNET_TESTING_Command
441 *cmd,
442 struct StartPeerState_v2 **sps)
443{
444 return cmd->traits (cmd->cls,
445 (const void **) sps,
446 "state",
447 (unsigned int) 5);
448}
449
450
451/**
452 * Function to get the trait with the size of the hello.
453 *
454 * @param[out] hello_size size of hello.
455 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
456 *
457 */
458int
459GNUNET_TRANSPORT_get_trait_hello_size_v2 (const struct
460 GNUNET_TESTING_Command
461 *cmd,
462 size_t **hello_size)
463{
464 return cmd->traits (cmd->cls,
465 (const void **) hello_size,
466 "hello_size",
467 (unsigned int) 4);
468}
469
470
471/**
472 * Function to get the trait with the hello.
473 *
474 * @param[out] hello The hello for the peer.
475 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
476 *
477 */
478int
479GNUNET_TRANSPORT_get_trait_hello_v2 (const struct
480 GNUNET_TESTING_Command
481 *cmd,
482 char **hello)
483{
484 return cmd->traits (cmd->cls,
485 (const void **) hello,
486 "hello",
487 (unsigned int) 3);
488}
489
490
491/**
492 * Function to get the trait with the map of connected peers.
493 *
494 * @param[out] connected_peers_map The map with connected peers.
495 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
496 *
497 */
498int
499GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (const struct
500 GNUNET_TESTING_Command
501 *cmd,
502 struct
503 GNUNET_CONTAINER_MultiShortmap
504 *
505 *
506 connected_peers_map)
507{
508 return cmd->traits (cmd->cls,
509 (const void **) connected_peers_map,
510 "connected_peers_map",
511 (unsigned int) 2);
512}
513
514
515/**
516 * Function to get the trait with the transport application handle.
517 *
518 * @param[out] ah The application handle.
519 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
520 */
521int
522GNUNET_TRANSPORT_get_trait_application_handle_v2 (const struct
523 GNUNET_TESTING_Command *cmd,
524 struct
525 GNUNET_TRANSPORT_ApplicationHandle
526 **ah)
527{
528 return cmd->traits (cmd->cls,
529 (const void **) ah,
530 "application_handle",
531 (unsigned int) 0);
532}
533
534
535/**
536 * Function to get the trait with the peer id.
537 *
538 * @param[out] id The peer id.
539 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
540 */
541int
542GNUNET_TRANSPORT_get_trait_peer_id_v2 (const struct
543 GNUNET_TESTING_Command *cmd,
544 struct GNUNET_PeerIdentity **id)
545{
546 return cmd->traits (cmd->cls,
547 (const void **) id,
548 "peer_id",
549 (unsigned int) 1);
550}
551
552
553/**
554 * Create command.
555 *
556 * @param label name for command.
557 * @param system_label Label of the cmd to setup a test environment.
558 * @param m The number of the local node of the actual network namespace.
559 * @param n The number of the actual namespace.
560 * @param local_m Number of local nodes in each namespace.
561 * @param handlers Handler for messages received by this peer.
562 * @param cfgname Configuration file name for this peer.
563 * @return command.
564 */
565struct GNUNET_TESTING_Command
566GNUNET_TRANSPORT_cmd_start_peer_v2 (const char *label,
567 const char *system_label,
568 uint32_t no,
569 char *node_ip,
570 struct GNUNET_MQ_MessageHandler *handlers,
571 const char *cfgname)
572{
573 struct StartPeerState_v2 *sps;
574 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
575 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
576 unsigned int i;
577
578 sps = GNUNET_new (struct StartPeerState_v2);
579 sps->no = no;
580 sps->system_label = system_label;
581 sps->connected_peers_map = connected_peers_map;
582 sps->cfgname = cfgname;
583 sps->node_ip = node_ip;
584
585 if (NULL != handlers)
586 {
587 for (i = 0; NULL != handlers[i].cb; i++)
588 ;
589 sps->handlers = GNUNET_new_array (i + 1,
590 struct GNUNET_MQ_MessageHandler);
591 GNUNET_memcpy (sps->handlers,
592 handlers,
593 i * sizeof(struct GNUNET_MQ_MessageHandler));
594 }
595
596 struct GNUNET_TESTING_Command cmd = {
597 .cls = sps,
598 .label = label,
599 .run = &start_peer_run,
600 .cleanup = &start_peer_cleanup,
601 .traits = &start_peer_traits
602 };
603
604 return cmd;
605}
diff --git a/src/transport/transport_api_cmd_start_peer_v3.c b/src/transport/transport_api_cmd_start_peer_v3.c
deleted file mode 100644
index 1a19ce60e..000000000
--- a/src/transport/transport_api_cmd_start_peer_v3.c
+++ /dev/null
@@ -1,617 +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_api_cmd_start_peer.c
23 * @brief cmd to start a peer.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_peerstore_service.h"
30#include "gnunet_transport_core_service.h"
31#include "gnunet_transport_application_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39
40static void
41retrieve_hello (void *cls);
42
43
44/**
45 * Callback delivering the hello of this peer from peerstore.
46 *
47 */
48static void
49hello_iter_cb (void *cb_cls,
50 const struct GNUNET_PEERSTORE_Record *record,
51 const char *emsg)
52{
53 struct StartPeerState_v2 *sps = cb_cls;
54 if (NULL == record)
55 {
56 sps->pic = NULL;
57 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
58 return;
59 }
60 // Check record type et al?
61 sps->hello_size = record->value_size;
62 sps->hello = GNUNET_malloc (sps->hello_size);
63 memcpy (sps->hello, record->value, sps->hello_size);
64 sps->hello[sps->hello_size - 1] = '\0';
65
66 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
67 sps->pic = NULL;
68 sps->finished = GNUNET_YES;
69}
70
71
72/**
73 * Function to start the retrieval task to retrieve the hello of this peer
74 * from the peerstore.
75 *
76 */
77static void
78retrieve_hello (void *cls)
79{
80 struct StartPeerState_v2 *sps = cls;
81 sps->rh_task = NULL;
82 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
83 "transport",
84 &sps->id,
85 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
86 hello_iter_cb,
87 sps);
88
89}
90
91
92/**
93 * This function checks StartPeerState_v2#finished, which is set when the hello was retrieved.
94 *
95 */
96static int
97start_peer_finish (void *cls,
98 GNUNET_SCHEDULER_TaskCallback cont,
99 void *cont_cls)
100{
101 struct StartPeerState_v2 *sps = cls;
102
103 if (GNUNET_YES == sps->finished)
104 {
105 cont (cont_cls);
106 }
107
108 return sps->finished;
109}
110
111
112/**
113 * Disconnect callback for the connection to the core service.
114 *
115 */
116static void
117notify_disconnect (void *cls,
118 const struct GNUNET_PeerIdentity *peer,
119 void *handler_cls)
120{
121 struct StartPeerState_v2 *sps = cls;
122
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Peer %s disconnected from peer %u (`%s')\n",
125 GNUNET_i2s (peer),
126 sps->no,
127 GNUNET_i2s (&sps->id));
128
129}
130
131
132/**
133 * Connect callback for the connection to the core service.
134 *
135 */
136static void *
137notify_connect (void *cls,
138 const struct GNUNET_PeerIdentity *peer,
139 struct GNUNET_MQ_Handle *mq)
140{
141 struct StartPeerState_v2 *sps = cls;
142 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
143 struct GNUNET_HashCode hc;
144 struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
145
146 void *ret = NULL;
147
148
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
150 "Peer %s connected to peer %u (`%s')\n",
151 GNUNET_i2s (peer),
152 sps->no,
153 GNUNET_i2s (&sps->id));
154
155 GNUNET_CRYPTO_hash (&public_key, sizeof(public_key), &hc);
156
157
158 memcpy (key,
159 &hc,
160 sizeof (*key));
161 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
162 key,
163 mq,
164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
165
166 GNUNET_free (key);
167 // TODO what does the handler function need?
168 return ret;
169}
170
171
172/**
173 * The run method of this cmd will start all services of a peer to test the transport service.
174 *
175 */
176static void
177start_peer_run (void *cls,
178 struct GNUNET_TESTING_Interpreter *is)
179{
180 struct StartPeerState_v2 *sps = cls;
181 char *emsg = NULL;
182 struct GNUNET_PeerIdentity dummy;
183 const struct GNUNET_TESTING_Command *system_cmd;
184 struct GNUNET_TESTING_System *tl_system;
185 char *home;
186 char *transport_unix_path;
187 char *tcp_communicator_unix_path;
188 char *udp_communicator_unix_path;
189 char *bindto;
190
191 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
192 {
193 LOG (GNUNET_ERROR_TYPE_ERROR,
194 "File not found: `%s'\n",
195 sps->cfgname);
196 GNUNET_TESTING_interpreter_fail (is);
197 return;
198 }
199
200
201 sps->cfg = GNUNET_CONFIGURATION_create ();
202 GNUNET_assert (GNUNET_OK ==
203 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
204
205 GNUNET_asprintf (&home,
206 "$GNUNET_TMP/test-transport/api-tcp-p%u",
207 sps->no);
208
209 GNUNET_asprintf (&transport_unix_path,
210 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
211 sps->no);
212
213 GNUNET_asprintf (&tcp_communicator_unix_path,
214 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
215 sps->no);
216
217 GNUNET_asprintf (&udp_communicator_unix_path,
218 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
219 sps->no);
220
221 GNUNET_asprintf (&bindto,
222 "%s:60002",
223 sps->node_ip);
224
225 LOG (GNUNET_ERROR_TYPE_ERROR,
226 "node_ip %s\n",
227 bindto);
228
229 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
230 home);
231 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
232 transport_unix_path);
233 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
234 "BINDTO",
235 bindto);
236 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
237 "BINDTO",
238 bindto);
239 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
240 "UNIXPATH",
241 tcp_communicator_unix_path);
242 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
243 "UNIXPATH",
244 udp_communicator_unix_path);
245
246 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
247 sps->system_label);
248 GNUNET_TESTING_get_trait_test_system (system_cmd,
249 &tl_system);
250
251 sps->tl_system = tl_system;
252
253 if (GNUNET_SYSERR ==
254 GNUNET_TESTING_configuration_create (tl_system,
255 sps->cfg))
256 {
257 LOG (GNUNET_ERROR_TYPE_ERROR,
258 "Testing library failed to create unique configuration based on `%s'\n",
259 sps->cfgname);
260 GNUNET_CONFIGURATION_destroy (sps->cfg);
261 GNUNET_TESTING_interpreter_fail (is);
262 return;
263 }
264
265 sps->peer = GNUNET_TESTING_peer_configure (sps->tl_system,
266 sps->cfg,
267 sps->no,
268 NULL,
269 &emsg);
270 if (NULL == sps->peer)
271 {
272 LOG (GNUNET_ERROR_TYPE_ERROR,
273 "Testing library failed to create unique configuration based on `%s': `%s'\n",
274 sps->cfgname,
275 emsg);
276 GNUNET_free (emsg);
277 GNUNET_TESTING_interpreter_fail (is);
278 return;
279 }
280
281 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
282 {
283 LOG (GNUNET_ERROR_TYPE_ERROR,
284 "Testing library failed to create unique configuration based on `%s'\n",
285 sps->cfgname);
286 GNUNET_free (emsg);
287 GNUNET_TESTING_interpreter_fail (is);
288 return;
289 }
290
291 memset (&dummy,
292 '\0',
293 sizeof(dummy));
294
295 GNUNET_TESTING_peer_get_identity (sps->peer,
296 &sps->id);
297
298 if (0 == memcmp (&dummy,
299 &sps->id,
300 sizeof(struct GNUNET_PeerIdentity)))
301 {
302 LOG (GNUNET_ERROR_TYPE_ERROR,
303 "Testing library failed to obtain peer identity for peer %u\n",
304 sps->no);
305 GNUNET_free (emsg);
306 GNUNET_TESTING_interpreter_fail (is);
307 return;
308 }
309 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "Peer %u configured with identity `%s'\n",
311 sps->no,
312 GNUNET_i2s_full (&sps->id));
313
314 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
315 NULL,
316 sps->handlers,
317 sps,
318 &notify_connect,
319 &notify_disconnect);
320 if (NULL == sps->th)
321 {
322 LOG (GNUNET_ERROR_TYPE_ERROR,
323 "Failed to connect to transport service for peer `%s': `%s'\n",
324 sps->cfgname,
325 emsg);
326 GNUNET_free (emsg);
327 GNUNET_TESTING_interpreter_fail (is);
328 return;
329 }
330
331 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
332 if (NULL == sps->th)
333 {
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 "Failed to connect to peerstore service for peer `%s': `%s'\n",
336 sps->cfgname,
337 emsg);
338 GNUNET_free (emsg);
339 GNUNET_TESTING_interpreter_fail (is);
340 return;
341 }
342
343 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
344 if (NULL == sps->ah)
345 {
346 LOG (GNUNET_ERROR_TYPE_ERROR,
347 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
348 sps->cfgname,
349 emsg);
350 GNUNET_free (emsg);
351 GNUNET_TESTING_interpreter_fail (is);
352 return;
353 }
354 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
355}
356
357
358/**
359 * The cleanup function of this cmd frees resources the cmd allocated.
360 *
361 */
362static void
363start_peer_cleanup (void *cls)
364{
365 struct StartPeerState_v2 *sps = cls;
366
367 if (NULL != sps->handlers)
368 {
369 GNUNET_free (sps->handlers);
370 sps->handlers = NULL;
371 }
372 if (NULL != sps->cfg)
373 {
374 GNUNET_CONFIGURATION_destroy (sps->cfg);
375 sps->cfg = NULL;
376 }
377 GNUNET_free (sps->hello);
378 GNUNET_free (sps->connected_peers_map);
379 GNUNET_free (sps);
380}
381
382
383/**
384 * This function prepares an array with traits.
385 *
386 */
387static int
388start_peer_traits (void *cls,
389 const void **ret,
390 const char *trait,
391 unsigned int index)
392{
393 struct StartPeerState_v2 *sps = cls;
394 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
395 struct GNUNET_PeerIdentity *id = &sps->id;
396 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
397 sps->connected_peers_map;
398 char *hello = sps->hello;
399 size_t hello_size = sps->hello_size;
400
401
402 struct GNUNET_TESTING_Trait traits[] = {
403 {
404 .index = 0,
405 .trait_name = "application_handle",
406 .ptr = (const void *) ah,
407 },
408 {
409 .index = 1,
410 .trait_name = "peer_id",
411 .ptr = (const void *) id,
412 },
413 {
414 .index = 2,
415 .trait_name = "connected_peers_map",
416 .ptr = (const void *) connected_peers_map,
417 },
418 {
419 .index = 3,
420 .trait_name = "hello",
421 .ptr = (const void *) hello,
422 },
423 {
424 .index = 4,
425 .trait_name = "hello_size",
426 .ptr = (const void *) hello_size,
427 },
428 {
429 .index = 5,
430 .trait_name = "state",
431 .ptr = (const void *) sps,
432 },
433 GNUNET_TESTING_trait_end ()
434 };
435
436 return GNUNET_TESTING_get_trait (traits,
437 ret,
438 trait,
439 index);
440}
441
442
443/**
444 * Function to get the trait with the struct StartPeerState_v2.
445 *
446 * @param[out] sps struct StartPeerState_v2.
447 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
448 *
449 */
450int
451GNUNET_TRANSPORT_get_trait_state_v3 (const struct
452 GNUNET_TESTING_Command
453 *cmd,
454 struct StartPeerState_v2 **sps)
455{
456 return cmd->traits (cmd->cls,
457 (const void **) sps,
458 "state",
459 (unsigned int) 5);
460}
461
462
463/**
464 * Function to get the trait with the size of the hello.
465 *
466 * @param[out] hello_size size of hello.
467 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
468 *
469 */
470int
471GNUNET_TRANSPORT_get_trait_hello_size_v3 (const struct
472 GNUNET_TESTING_Command
473 *cmd,
474 size_t **hello_size)
475{
476 return cmd->traits (cmd->cls,
477 (const void **) hello_size,
478 "hello_size",
479 (unsigned int) 4);
480}
481
482
483/**
484 * Function to get the trait with the hello.
485 *
486 * @param[out] hello The hello for the peer.
487 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
488 *
489 */
490int
491GNUNET_TRANSPORT_get_trait_hello_v3 (const struct
492 GNUNET_TESTING_Command
493 *cmd,
494 char **hello)
495{
496 return cmd->traits (cmd->cls,
497 (const void **) hello,
498 "hello",
499 (unsigned int) 3);
500}
501
502
503/**
504 * Function to get the trait with the map of connected peers.
505 *
506 * @param[out] connected_peers_map The map with connected peers.
507 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
508 *
509 */
510int
511GNUNET_TRANSPORT_get_trait_connected_peers_map_v3 (const struct
512 GNUNET_TESTING_Command
513 *cmd,
514 struct
515 GNUNET_CONTAINER_MultiShortmap
516 *
517 *
518 connected_peers_map)
519{
520 return cmd->traits (cmd->cls,
521 (const void **) connected_peers_map,
522 "connected_peers_map",
523 (unsigned int) 2);
524}
525
526
527/**
528 * Function to get the trait with the transport application handle.
529 *
530 * @param[out] ah The application handle.
531 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
532 */
533int
534GNUNET_TRANSPORT_get_trait_application_handle_v3 (const struct
535 GNUNET_TESTING_Command *cmd,
536 struct
537 GNUNET_TRANSPORT_ApplicationHandle
538 **ah)
539{
540 return cmd->traits (cmd->cls,
541 (const void **) ah,
542 "application_handle",
543 (unsigned int) 0);
544}
545
546
547/**
548 * Function to get the trait with the peer id.
549 *
550 * @param[out] id The peer id.
551 * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise.
552 */
553int
554GNUNET_TRANSPORT_get_trait_peer_id_v3 (const struct
555 GNUNET_TESTING_Command *cmd,
556 struct GNUNET_PeerIdentity **id)
557{
558 return cmd->traits (cmd->cls,
559 (const void **) id,
560 "peer_id",
561 (unsigned int) 1);
562}
563
564
565/**
566 * Create command.
567 *
568 * @param label name for command.
569 * @param system_label Label of the cmd to setup a test environment.
570 * @param m The number of the local node of the actual network namespace.
571 * @param n The number of the actual namespace.
572 * @param local_m Number of local nodes in each namespace.
573 * @param handlers Handler for messages received by this peer.
574 * @param cfgname Configuration file name for this peer.
575 * @return command.
576 */
577struct GNUNET_TESTING_Command
578GNUNET_TRANSPORT_cmd_start_peer_v3 (const char *label,
579 const char *system_label,
580 uint32_t no,
581 char *node_ip,
582 struct GNUNET_MQ_MessageHandler *handlers,
583 const char *cfgname)
584{
585 struct StartPeerState_v2 *sps;
586 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
587 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
588 unsigned int i;
589
590 sps = GNUNET_new (struct StartPeerState_v2);
591 sps->no = no;
592 sps->system_label = system_label;
593 sps->connected_peers_map = connected_peers_map;
594 sps->cfgname = cfgname;
595 sps->node_ip = node_ip;
596
597 if (NULL != handlers)
598 {
599 for (i = 0; NULL != handlers[i].cb; i++)
600 ;
601 sps->handlers = GNUNET_new_array (i + 1,
602 struct GNUNET_MQ_MessageHandler);
603 GNUNET_memcpy (sps->handlers,
604 handlers,
605 i * sizeof(struct GNUNET_MQ_MessageHandler));
606 }
607
608 struct GNUNET_TESTING_Command cmd = {
609 .cls = sps,
610 .label = label,
611 .run = &start_peer_run,
612 .cleanup = &start_peer_cleanup,
613 .traits = &start_peer_traits
614 };
615
616 return cmd;
617}
diff --git a/src/util/child_management.c b/src/util/child_management.c
index 4ef42dba2..832e05ece 100644
--- a/src/util/child_management.c
+++ b/src/util/child_management.c
@@ -19,7 +19,7 @@
19 */ 19 */
20 20
21/** 21/**
22 * @file testing/child_management.c 22 * @file util/child_management.c
23 * @brief Handling of child processes in GNUnet. 23 * @brief Handling of child processes in GNUnet.
24 * @author Christian Grothoff (ANASTASIS) 24 * @author Christian Grothoff (ANASTASIS)
25 * @author Dominik Meister (ANASTASIS) 25 * @author Dominik Meister (ANASTASIS)
@@ -153,7 +153,9 @@ sighandler_child_death (void)
153 errno = old_errno; /* restore errno */ 153 errno = old_errno; /* restore errno */
154} 154}
155 155
156 156/**
157 * Initializing the signal pipe for child handling.
158 */
157static void 159static void
158child_management_start (void) 160child_management_start (void)
159{ 161{
@@ -191,6 +193,14 @@ child_management_done (void)
191} 193}
192 194
193 195
196/**
197 * Adding a child process to be monitored by the child management.
198 *
199 * @param proc The child process to be monitored.
200 * @param cp The callback to be called, when the child process completed.
201 * @param cb_cls The closure for the callback.
202 * @return An handle for the the child being monitored.
203 */
194struct GNUNET_ChildWaitHandle * 204struct GNUNET_ChildWaitHandle *
195GNUNET_wait_child (struct GNUNET_OS_Process *proc, 205GNUNET_wait_child (struct GNUNET_OS_Process *proc,
196 GNUNET_ChildCompletedCallback cb, 206 GNUNET_ChildCompletedCallback cb,
@@ -219,6 +229,11 @@ GNUNET_wait_child (struct GNUNET_OS_Process *proc,
219} 229}
220 230
221 231
232/**
233 * Removing child handle.
234 *
235 * @param cwh The handle to be removed.
236 */
222void 237void
223GNUNET_wait_child_cancel (struct GNUNET_ChildWaitHandle *cwh) 238GNUNET_wait_child_cancel (struct GNUNET_ChildWaitHandle *cwh)
224{ 239{