Skip to content

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

tools: adb-closed.py names macOS Local Network privacy for EHOSTUNREACH, as tv.py does (#1982)

xbsheng5f6dcfd92807
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index f72d8bdd7..89bd19bab 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -19,6 +19,11 @@ Things that bit us or contributors, so they don't bite you. Commands use `tools/
shows the TV's MAC and `dns-sd -B _androidtvremote2._tcp` still finds it by name while no port on
it can be opened. Seen on macOS 26 (Darwin 25.6) against a Hisense `hengshan` set, Google TV 12,
where the stock `adb` and `tools/tv.py` failed identically.
+ The closing check is denied the same way, and it is the one the procedure accepts: `tools/adb-closed.py
+ <tv ip>` answers `could not check <ip>:5555 ([Errno 65] No route to host). Is the TV on and on this
+ network?` followed by the Local Network note, exit 2, so a machine without the permission can neither
+ confirm nor deny that the port is closed. Grant it to the terminal, reopen it, and check again: only a
+ "closed" from a granted terminal counts.
- **No "wireless debugging" toggle** on some TVs, Xiaomi among them. USB debugging alone opens
network ADB on port 5555.
- **USB debugging is greyed out and there is no Wireless debugging entry either**, seen on a carrier
diff --git a/tests/test_adb_closed_macos_permission.py b/tests/test_adb_closed_macos_permission.py
new file mode 100644
index 000000000..13e9916c0
--- /dev/null
+++ b/tests/test_adb_closed_macos_permission.py
@@ -0,0 +1,172 @@
+"""tools/adb-closed.py names macOS Local Network privacy when EHOSTUNREACH hides the port.
+
+The closing step accepts only this tool's "closed" (PROMPT.md, AGENTS.md, the README FAQ), so it is the
+one check an owner is told to trust. On macOS the kernel reports a process that Local Network privacy
+keeps off the subnet as EHOSTUNREACH - "No route to host", the same errno as a TV that is switched off
+or on another network. tools/tv.py has named the permission at connect time since #1982; this is the
+other client, and until this branch it answered
+
+ could not check 192.0.2.1:5555 ([Errno 65] No route to host). Is the TV on and on this network?
+
+for a network that was never broken, while the port it was asked about may be open (ADB still on) or
+closed. Exit 2 was already right - the check could not tell - but the sentence sent the reader to the
+router and to the TV instead of to System Settings, which is the hour #1982's reporter lost.
+
+The errno and the platform are the two things the script reads, so the runs here supply exactly those:
+socket.create_connection raises the error, sys.platform says darwin (or linux). Both are faked rather
+than skipped, unlike tests/test_tv_connect_errors.py's macOS test, because neither value is read for
+anything else in this script: the same OSError branch, the same comparison.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import ast
+import contextlib
+import errno
+import importlib.util
+import io
+import os
+from pathlib import Path
+import socket
+import sys
+import unittest
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[1]
+SCRIPT = ROOT / "tools" / "adb-closed.py"
+TV = "192.0.2.1" # a documentation address; nothing on the network is asked
+NOTE = "On macOS this is also what Local Network privacy looks like."
+
+
+def load():
+ spec = importlib.util.spec_from_file_location("cyt_adb_closed_platform", SCRIPT)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+def run(error, platform="darwin", args=(TV,)):
+ """main() with socket.create_connection raising ERROR, on PLATFORM: (code, stdout, stderr)."""
+ module = load()
+ out, err = io.StringIO(), io.StringIO()
+ env = {k: v for k, v in os.environ.items() if k not in ("TV_HOST", "TV_PORT")}
+ argv = sys.argv
+ sys.argv = ["adb-closed.py", *args]
+ try:
+ with mock.patch.dict(os.environ, env, clear=True), \
+ mock.patch.object(socket, "create_connection", side_effect=error), \
+ mock.patch.object(sys, "platform", platform), \
+ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
+ code = module.main()
+ finally:
+ sys.argv = argv
+ return code, out.getvalue(), err.getvalue()
+
+
+class MacOsPermissionTest(unittest.TestCase):
+ def test_no_route_to_host_on_macos_names_the_permission(self):
+ code, out, err = run(OSError(errno.EHOSTUNREACH, "No route to host"))
+ self.assertEqual(code, 2, out + err)
+ self.assertEqual(out, "")
+ self.assertIn(f"could not check {TV}:5555 ([Errno {errno.EHOSTUNREACH}] No route to host). "
+ "Is the TV on and on this network?", err)
+ self.assertIn(NOTE, err)
+ # The three things a reader gets wrong: which app to allow, that another client would not help,
+ # and whether the network is at fault.
+ self.assertIn("System Settings > Privacy & Security > Local Network", err)
+ self.assertIn("Terminal, iTerm2, VS Code", err)
+ self.assertIn("granting it to python or adb does nothing", err)
+ self.assertIn("try the default gateway", err)
+ # Exit 2 already said the check could not tell; the sentence has to say it too, or the closing
+ # step reads "could not check ... Is the TV on?" as a port that is probably closed.
+ self.assertIn("Nothing was learned about the port.", err)
+ self.assertNotIn("Traceback", err)
+ self.assertNotIn("is closed", err)
+ self.assertNotIn("still open", err)
+
+ def test_the_port_it_was_given_is_named_on_macos_too(self):
+ code, out, err = run(OSError(errno.EHOSTUNREACH, "No route to host"), args=(f"{TV}:5877",))
+ self.assertEqual(code, 2, out + err)
+ self.assertIn(f"could not check {TV}:5877 ([Errno {errno.EHOSTUNREACH}]", err)
+ self.assertIn(NOTE, err)
+
+ @unittest.skipIf(sys.platform == "darwin", "this is the run for the other platforms")
+ def test_another_platform_keeps_the_plain_message(self):
+ code, out, err = run(OSError(errno.EHOSTUNREACH, "No route to host"), platform="linux")
+ self.assertEqual(code, 2, out + err)
+ self.assertEqual(out, "")
+ self.assertIn("Is the TV on and on this network?", err)
+ self.assertNotIn("Local Network privacy", err)
+ self.assertNotIn("System Settings", err)
+
+ def test_a_network_that_is_really_unreachable_is_not_the_permission(self):
+ # ENETUNREACH is this computer having no route at all, which is what the plain sentence is for.
+ code, out, err = run(OSError(errno.ENETUNREACH, "Network is unreachable"))
+ self.assertEqual(code, 2, out + err)
+ self.assertIn("Is the TV on and on this network?", err)
+ self.assertNotIn(NOTE, err)
+
+ def test_a_timeout_on_macos_is_not_the_permission_either(self):
+ # A TV that is off, asleep or on another VLAN never answers; the permission denies the subnet and
+ # answers with EHOSTUNREACH. The timeout branch has its own, longer answer.
+ code, out, err = run(socket.timeout("timed out"))
+ self.assertEqual(code, 2, out + err)
+ self.assertIn("it did not answer in 3 s, and that proves nothing", err)
+ self.assertNotIn(NOTE, err)
+
+ def test_a_refusal_on_macos_is_still_closed(self):
+ # The note must not cost the one answer the closing step waits for.
+ code, out, err = run(ConnectionRefusedError(61, "Connection refused"))
+ self.assertEqual(code, 0, out + err)
+ self.assertEqual(out, f"{TV}:5555 is closed: the TV refused the connection.\n")
+ self.assertNotIn("Local Network privacy", err)
+
+
+class TheTwoClientsAgreeTest(unittest.TestCase):
+ def test_both_name_the_same_permission_and_the_same_way_to_grant_it(self):
+ # tools/tv.py explains the permission at connect time (#1982); this tool is what the closing step
+ # trusts. A reader who is told two different things about one permission has to guess which is so.
+ def sentences(path):
+ """Every string literal in the file, joined: the sentences as a reader gets them, wrapped or not."""
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ return " ".join(node.value for node in ast.walk(tree)
+ if isinstance(node, ast.Constant) and isinstance(node.value, str))
+
+ closed = sentences(SCRIPT)
+ tv = sentences(ROOT / "tools" / "tv.py")
+ for phrase in (NOTE, "System Settings > Privacy & Security > Local Network",
+ "Terminal, iTerm2, VS Code", "granting it to python or adb does nothing",
+ "try the default gateway"):
+ with self.subTest(phrase=phrase):
+ self.assertIn(phrase, closed)
+ self.assertIn(phrase, tv)
+
+ def test_the_note_is_gated_on_macos_and_on_that_one_errno(self):
+ source = SCRIPT.read_text(encoding="utf-8")
+ self.assertIn("import errno", source)
+ gate = 'if sys.platform == "darwin" and e.errno == errno.EHOSTUNREACH:'
+ self.assertIn(gate, source)
+ # Inside the OSError branch, which is the one that answers "the TV was not reached".
+ self.assertLess(source.index("except OSError as e:"), source.index(gate))
+ self.assertLess(source.index(gate), source.index('Is the TV on and on this network?'))
+
+ def test_the_docstring_says_what_the_exit_status_still_means(self):
+ doc = " ".join(SCRIPT.read_text(encoding="utf-8").split('"""')[1].split())
+ self.assertIn("Only the TV's own refusal shows the port closed.", doc)
+ self.assertIn("Local Network privacy", doc)
+ self.assertIn("`EHOSTUNREACH`", doc)
+ self.assertIn("cannot tell an open port from a closed one", doc)
+
+ def test_the_macos_bullet_sends_the_closing_check_too(self):
+ # The page's macOS entry is where an owner reads what the two clients do with this error; it named
+ # tools/tv.py and the stock adb only, while the closing step trusts this tool alone.
+ page = " ".join((ROOT / "docs" / "TROUBLESHOOTING.md").read_text(encoding="utf-8").split())
+ start = page.index("macOS: \"No route to host\"")
+ bullet = page[start:page.index("- **No \"wireless debugging\" toggle**", start)]
+ self.assertIn("tools/adb-closed.py", bullet)
+ self.assertIn("could not check <ip>:5555 ([Errno 65] No route to host)", bullet)
+ self.assertIn("exit 2", bullet)
+ self.assertIn("only a \"closed\" from a granted terminal counts", bullet)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/adb-closed.py b/tools/adb-closed.py
index 7f50c1c98..0507f2106 100755
--- a/tools/adb-closed.py
+++ b/tools/adb-closed.py
@@ -8,10 +8,16 @@ could not tell: nothing answered (the TV off, asleep or rebooting, at another ad
from here), nothing listened on 127.0.0.1, or the check itself could not run (a name that does not
resolve or is not a hostname, no route to the TV). Only the TV's own refusal shows the port closed.
+On macOS a "No route to host" (`EHOSTUNREACH`) is also what Local Network privacy looks like: the
+permission belongs to the app that runs this check, not to python or adb, so a terminal that was never
+granted it is denied the subnet and this tool cannot tell an open port from a closed one. The message
+says so and gives the way to grant it (tools/tv.py names the same permission at connect time).
+
A TV_PORT exported with TV_HOST=127.0.0.1, or set to relay.py's 15555, is a relay's (tools/relay.md), so
it is not used for a TV's own IP. 127.0.0.1 is this computer: a relay answers there for itself, not for
the TV, so a refusal there is not the TV's.
"""
+import errno
import ipaddress
import os
import socket
@@ -133,7 +139,22 @@ def main():
sys.stderr.write(f"could not check {host}:{port}: not a hostname. Check the address.\n")
return 2
except OSError as e:
- sys.stderr.write(f"could not check {host}:{port} ({e}). Is the TV on and on this network?\n")
+ # macOS Local Network privacy denies a process the local subnet and the kernel reports that as
+ # EHOSTUNREACH, the same errno as a TV that is switched off, so "is the TV on?" sends people
+ # rebuilding a network that was never broken. tools/tv.py says so at connect time since #1982; this
+ # is the other client, and the one the closing step depends on (PROMPT.md, AGENTS.md and the README
+ # accept only its "closed"), so an assistant on a Mac whose terminal was never granted the
+ # permission was told the TV was off or on another network while the port it was asked about may
+ # be open or closed: nothing was learned about it either way. Exit 2 already means that.
+ note = ""
+ if sys.platform == "darwin" and e.errno == errno.EHOSTUNREACH:
+ note = ("On macOS this is also what Local Network privacy looks like. Allow the app that runs "
+ "this check (Terminal, iTerm2, VS Code) under System Settings > Privacy & Security > "
+ "Local Network, then quit and reopen it; granting it to python or adb does nothing. To "
+ "tell the two apart, try the default gateway: if it answers while every other address "
+ "on the subnet gives this error, it is the permission and not the TV. Nothing was "
+ "learned about the port.\n")
+ sys.stderr.write(f"could not check {host}:{port} ({e}). Is the TV on and on this network?\n" + note)
return 2
# Wireless debugging has a port of its own and stays open when USB debugging goes off, so the advice
# names it too. The second check comes before the reboot: if this is the Wireless debugging port, a