From e93c1d741c73f8b5a275c7b375de912b445fc1bf Mon Sep 17 00:00:00 2001
From: Pavel Punsky <eakraly@users.noreply.github.com>
Date: Thu, 23 Jul 2026 23:09:01 -0700
Subject: [PATCH] debian/tests: make each test self-contained to fix tcp/tls
 failures

autopkgtest gives every test a pristine testbed when the backend
supports revert, so the state installed by the separate "setup" test
(turnserver.conf with allow-loopback-peers, TLS certs, user db) never
survives into the udp/tcp/tls tests on current Ubuntu and debci
infrastructure.  Those tests then run against the stock configuration:
tcp fails with "create permission error 403" (loopback peers are denied
by default) and tls fails with connection refused (no certificates, so
the TLS listener never starts).  This has kept newer coturn versions
stuck in Ubuntu's -proposed pocket.

Turn the setup test into a helper script (debian/tests/prepare) that
every test runs first, drop "setup" from the Tests list, and wait for
the 3478/5349 listeners to come up after restarting the service before
driving a client.  Add iproute2 to the test dependencies for ss.

Validated against the coturn 4.15.0-1 binaries from unstable: with the
test configuration applied per-test, udp, tcp and tls all pass with 0%
packet loss.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---
 debian/tests/control |  4 ++--
 debian/tests/prepare | 29 +++++++++++++++++++++++++++++
 debian/tests/setup   | 16 ----------------
 debian/tests/tcp     |  3 +++
 debian/tests/tls     |  3 +++
 debian/tests/udp     |  3 +++
 6 files changed, 40 insertions(+), 18 deletions(-)
 create mode 100755 debian/tests/prepare
 delete mode 100644 debian/tests/setup
 mode change 100644 => 100755 debian/tests/tcp
 mode change 100644 => 100755 debian/tests/tls
 mode change 100644 => 100755 debian/tests/udp

diff --git a/debian/tests/control b/debian/tests/control
index 90222cc..8382d14 100644
--- a/debian/tests/control
+++ b/debian/tests/control
@@ -1,3 +1,3 @@
-Depends: @
+Depends: @, iproute2
 Restrictions: needs-root, allow-stderr, isolation-container
-Tests: setup, udp, tcp, tls
+Tests: udp, tcp, tls
diff --git a/debian/tests/prepare b/debian/tests/prepare
new file mode 100755
index 0000000..82a071c
--- /dev/null
+++ b/debian/tests/prepare
@@ -0,0 +1,29 @@
+#!/bin/sh
+# Install the test configuration and restart coturn.
+#
+# This is a helper sourced by each test, not a test itself: autopkgtest
+# gives every test a pristine testbed when the backend supports revert,
+# so state written by a previous "setup" test does not survive.  Each
+# test must therefore do its own setup.
+set -ex
+
+# Cert
+cp examples/etc/cacert.pem /etc/cacert.pem
+cp examples/etc/turn_server_cert.pem /etc/turn_server_cert.pem
+cp examples/etc/turn_server_pkey.pem /etc/turn_server_pkey.pem
+
+# Config
+cp debian/tests/turnserver.conf /etc/turnserver.conf
+
+# Db
+cp examples/var/db/turndb /var/lib/turn/turndb
+
+systemctl restart coturn
+
+# Wait until the plain and TLS listeners are up before driving a client.
+for _ in $(seq 1 30); do
+  if ss -ltn | grep -q ':3478 ' && ss -ltn | grep -q ':5349 '; then
+    break
+  fi
+  sleep 1
+done
diff --git a/debian/tests/setup b/debian/tests/setup
deleted file mode 100644
index 0f284aa..0000000
--- a/debian/tests/setup
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/bin/sh
-
-set -ex
-
-# Cert
-cp examples/etc/cacert.pem /etc/cacert.pem
-cp examples/etc/turn_server_cert.pem /etc/turn_server_cert.pem
-cp examples/etc/turn_server_pkey.pem /etc/turn_server_pkey.pem
-
-# Config
-cp debian/tests/turnserver.conf /etc/turnserver.conf
-
-# Db
-cp examples/var/db/turndb /var/lib/turn/turndb
-
-systemctl restart coturn
diff --git a/debian/tests/tcp b/debian/tests/tcp
old mode 100644
new mode 100755
index f9dd902..baf0a14
--- a/debian/tests/tcp
+++ b/debian/tests/tcp
@@ -1,3 +1,6 @@
 #!/bin/sh
+set -e
+
+debian/tests/prepare
 
 /usr/share/doc/coturn/examples/scripts/longtermsecure/secure_tcp_client_c2c_tcp_relay.sh
diff --git a/debian/tests/tls b/debian/tests/tls
old mode 100644
new mode 100755
index b302c72..b24ec61
--- a/debian/tests/tls
+++ b/debian/tests/tls
@@ -1,3 +1,6 @@
 #!/bin/sh
+set -e
+
+debian/tests/prepare
 
 turnutils_uclient -T -S -k ./examples/etc/turn_client_pkey.pem -E ./examples/etc/cacert.pem -i ./examples/etc/turn_client_cert.pem -n 1000 -m 10 -l 170 -y -g -u gorst -w hero $@ ::1
diff --git a/debian/tests/udp b/debian/tests/udp
old mode 100644
new mode 100755
index 107c5c7..65bc7fe
--- a/debian/tests/udp
+++ b/debian/tests/udp
@@ -1,3 +1,6 @@
 #!/bin/sh
+set -e
+
+debian/tests/prepare
 
 /usr/share/doc/coturn/examples/scripts/longtermsecure/secure_udp_c2c.sh
-- 
2.55.0

