@@ -171,17 +171,73 @@ if [ "$RUN_CHECK_DEPS" = "true" ]; then
171171 " ${BASE_ROOT_DIR} /contrib/devtools/check-deps.sh" " ${BASE_BUILD_DIR} "
172172fi
173173
174+ # Return a list of the non-loopback network interfaces on the machine, for example: docker0 enp3s0 wlp2s0
175+ function get_interfaces()
176+ {
177+ set -o pipefail
178+ ifconfig | awk -F ' :| ' ' /^[^[:space:]]/ { if (!match($1, /^lo/)) { print $1 } }'
179+ set +o pipefail
180+ }
181+
182+ # Generate a file name for storing raw packets captured by tcpdump.
183+ function tcpdump_file()
184+ {
185+ local test_name=" $1 "
186+ local interface_name=" $2 "
187+ echo " /tmp/tcpdump_${test_name} _$interface_name "
188+ }
189+
190+ # Start tcpdump on each non-loopback interface in the background.
191+ function traffic_monitor_begin()
192+ {
193+ test_name=" $1 "
194+ for ifname in $( get_interfaces) ; do
195+ tcpdump -nU -i " $ifname " -w " $( tcpdump_file " $test_name " " $ifname " ) " &
196+ done
197+ }
198+
199+ # Read tcpdump raw packet files that are generated by traffic_monitor_begin() and if any of them contain
200+ # data, then print the packets and exit with an error.
201+ function traffic_monitor_end()
202+ {
203+ test_name=" $1 "
204+
205+ for ifname in $( get_interfaces) ; do
206+ f=$( tcpdump_file " $test_name " " $ifname " )
207+ if [ ! -e " $f " ] && [ " $CI_TCPDUMP_OK_TO_FAIL " = " 1" ] ; then
208+ # In some CI environments this script is not running as root and so the
209+ # tcpdump errors and does not create $f. Skip silently those, but we
210+ # need at least one where tcpdump can run and this is the ASAN one. So
211+ # treat the absence of $f as an error only on the ASAN task.
212+ continue
213+ fi
214+ # We are running as root and those files are created with owner:group =
215+ # tcpdump:tcpdump and then `tcpdump -r` refuses to read them with an error
216+ # "permission denied" if they are not owned by root:root.
217+ chown root:root " $f "
218+ out=" $( tcpdump -n -r " $f " --direction=out tcp or udp) "
219+ if [ -n " $out " ] ; then
220+ echo " Error: outbound TCP or UDP packets on the non loopback interface generated during $test_name tests:" >&2
221+ tcpdump -n -r " $f " tcp or udp
222+ exit 1
223+ fi
224+ done
225+ }
226+
174227if [ " $RUN_UNIT_TESTS " = " true" ]; then
228+ traffic_monitor_begin " unit"
175229 DIR_UNIT_TEST_DATA=" ${DIR_UNIT_TEST_DATA} " \
176230 LD_LIBRARY_PATH=" ${DEPENDS_DIR} /${HOST} /lib" \
177231 CTEST_OUTPUT_ON_FAILURE=ON \
178232 ctest --test-dir " ${BASE_BUILD_DIR} " \
179233 --stop-on-failure \
180234 " ${MAKEJOBS} " \
181235 --timeout $(( TEST_RUNNER_TIMEOUT_FACTOR * 60 ))
236+ traffic_monitor_end " unit"
182237fi
183238
184239if [ " $RUN_FUNCTIONAL_TESTS " = " true" ]; then
240+ traffic_monitor_begin " functional"
185241 # parses TEST_RUNNER_EXTRA as an array which allows for multiple arguments such as TEST_RUNNER_EXTRA='--exclude "rpc_bind.py --ipv6"'
186242 eval " TEST_RUNNER_EXTRA=($TEST_RUNNER_EXTRA )"
187243 LD_LIBRARY_PATH=" ${DEPENDS_DIR} /${HOST} /lib" \
@@ -194,9 +250,11 @@ if [ "$RUN_FUNCTIONAL_TESTS" = "true" ]; then
194250 " ${TEST_RUNNER_EXTRA[@]} " \
195251 --quiet \
196252 --failfast
253+ traffic_monitor_end " functional"
197254fi
198255
199256if [ " ${RUN_TIDY} " = " true" ]; then
257+ traffic_monitor_begin " tidy"
200258 cmake -B /tidy-build -DLLVM_DIR=/usr/lib/llvm-" ${TIDY_LLVM_V} " /cmake -DCMAKE_BUILD_TYPE=Release -S " ${BASE_ROOT_DIR} " /contrib/devtools/bitcoin-tidy
201259 cmake --build /tidy-build " $MAKEJOBS "
202260 cmake --build /tidy-build --target bitcoin-tidy-tests " $MAKEJOBS "
@@ -239,9 +297,11 @@ if [ "${RUN_TIDY}" = "true" ]; then
239297
240298 run_iwyu " compile_commands_iwyu_warnings.json"
241299 git --no-pager diff
300+ traffic_monitor_end " tidy"
242301fi
243302
244303if [ " $RUN_FUZZ_TESTS " = " true" ]; then
304+ traffic_monitor_begin " fuzz"
245305 # shellcheck disable=SC2086
246306 LD_LIBRARY_PATH=" ${DEPENDS_DIR} /${HOST} /lib" \
247307 " ${BASE_BUILD_DIR} /test/fuzz/test_runner.py" \
@@ -250,4 +310,5 @@ if [ "$RUN_FUZZ_TESTS" = "true" ]; then
250310 -l DEBUG \
251311 " ${DIR_FUZZ_IN} " \
252312 --empty_min_time=60
313+ traffic_monitor_end " fuzz"
253314fi
0 commit comments