Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tests: the adb-closed tests run in an environment they own

xbsheng05229bdaaa8f
diff --git a/tests/test_adb_closed.py b/tests/test_adb_closed.py
index 3a4cc30b7..fd08b1e32 100644
--- a/tests/test_adb_closed.py
+++ b/tests/test_adb_closed.py
@@ -11,15 +11,32 @@ import socket
import subprocess
import sys
import unittest
+from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "tools" / "adb-closed.py"
+def env_with(**overrides):
+ """This environment without the shell's own TV_HOST/TV_PORT, plus the given ones.
+
+ adb-closed.py reads both (tools/relay.md tells the reader to `export TV_HOST=127.0.0.1
+ TV_PORT=15555` before running the tools through a relay), so a caller with them exported changed
+ what these tests asserted: five of them answered "TV_PORT=15555 is a relay's port" instead of the
+ address they typed. tests/test_adb_closed_only_a_refusal.py and
+ tests/test_adb_closed_macos_permission.py already run in an environment they own, by this same
+ rule; this file passed env=None and inherited whatever the shell had.
+ """
+ env = {k: v for k, v in os.environ.items() if k not in ("TV_HOST", "TV_PORT")}
+ env.update(overrides)
+ return env
+
+
def check(*args, env=None):
return subprocess.run([sys.executable, "-I", str(SCRIPT), *args],
- capture_output=True, text=True, timeout=30, env=env)
+ capture_output=True, text=True, timeout=30,
+ env=env if env is not None else env_with())
class AdbClosedTest(unittest.TestCase):
@@ -57,7 +74,7 @@ class AdbClosedTest(unittest.TestCase):
with socket.socket() as probe:
probe.bind(("127.0.0.1", 0))
closed_port = probe.getsockname()[1]
- env = dict(os.environ, TV_HOST=f"127.0.0.1:{open_port}", TV_PORT=str(closed_port))
+ env = env_with(TV_HOST=f"127.0.0.1:{open_port}", TV_PORT=str(closed_port))
result = check(env=env)
self.assertEqual(result.returncode, 1, result.stderr)
self.assertIn(f"127.0.0.1:{open_port} is still open", result.stderr)
@@ -67,7 +84,7 @@ class AdbClosedTest(unittest.TestCase):
server.bind(("127.0.0.1", 0))
server.listen(1)
port = server.getsockname()[1]
- env = dict(os.environ, TV_HOST="127.0.0.1", TV_PORT=str(port))
+ env = env_with(TV_HOST="127.0.0.1", TV_PORT=str(port))
result = check(env=env)
self.assertEqual(result.returncode, 1, result.stderr)
self.assertIn(f"127.0.0.1:{port} is still open", result.stderr)
@@ -93,6 +110,26 @@ class AdbClosedTest(unittest.TestCase):
result = check("192.168.1..50")
self.assertIn("could not check 192.168.1..50:5555", result.stderr)
+ def test_a_relay_shell_does_not_change_what_these_tests_ask(self):
+ # tools/relay.md's line 13 is `export TV_HOST=127.0.0.1 TV_PORT=15555`, and CONTRIBUTING asks a
+ # contributor to run this suite before opening a PR. In that shell the five tests above failed
+ # on addresses they never typed, because adb-closed.py reads those two variables and, rightly,
+ # refuses a relay's port beside this computer's address: a red suite that said nothing about
+ # the change under test. The tests now hand the script an environment of their own.
+ with mock.patch.dict(os.environ, {"TV_HOST": "127.0.0.1", "TV_PORT": "15555"}):
+ refused = check("192.168.1..50")
+ self.assertEqual(refused.returncode, 2, refused.stderr)
+ self.assertIn("could not check 192.168.1..50:5555", refused.stderr)
+ self.assertNotIn("is a relay's port", refused.stderr)
+ with mock.patch.dict(os.environ, {"TV_HOST": "127.0.0.1", "TV_PORT": "15555"}):
+ with socket.socket() as server:
+ server.bind(("127.0.0.1", 0))
+ server.listen(1)
+ port = server.getsockname()[1]
+ open_port = check(f"127.0.0.1:{port}")
+ self.assertEqual(open_port.returncode, 1, open_port.stderr)
+ self.assertIn(f"127.0.0.1:{port} is still open", open_port.stderr)
+
def test_bad_arguments(self):
self.assertEqual(check("127.0.0.1", "not-a-port").returncode, 2)
self.assertEqual(check("127.0.0.1:not-a-port").returncode, 2)