Skip to content

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

tools: preserve UTF-8 output from Google ADB on Windows

Matheus Herbertsd4d0fbe28417
diff --git a/tests/test_native_adb.py b/tests/test_native_adb.py
index 0f2763c1e..ec4006f17 100644
--- a/tests/test_native_adb.py
+++ b/tests/test_native_adb.py
@@ -15,6 +15,63 @@ from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
+def load_tv():
+ spec = importlib.util.spec_from_file_location("tv_native_test", ROOT / "tools" / "tv.py")
+ tv = importlib.util.module_from_spec(spec)
+ # Native commands do not use Python ADB keys or sockets. Stub those imports so
+ # these tests also run under a Python which has no adb_shell installed.
+ stubs = {name: types.ModuleType(name) for name in (
+ "adb_shell", "adb_shell.auth", "adb_shell.adb_device",
+ "adb_shell.auth.keygen", "adb_shell.auth.sign_pythonrsa")}
+ stubs["adb_shell.adb_device"].AdbDeviceTcp = object
+ stubs["adb_shell.auth.keygen"].keygen = lambda path: None
+ stubs["adb_shell.auth.sign_pythonrsa"].PythonRSASigner = lambda pub, priv: None
+ with mock.patch.dict(sys.modules, stubs):
+ spec.loader.exec_module(tv)
+ return tv
+
+
+class NativeAdbEncodingTest(unittest.TestCase):
+ def setUp(self):
+ with mock.patch.dict(os.environ, {"TV_HOST": "192.0.2.8:37123"}):
+ self.tv = load_tv()
+
+ @contextlib.contextmanager
+ def device_output(self, stdout=b"", stderr=b"", status=0):
+ run = subprocess.run
+
+ def fake_adb(args, **kwargs):
+ # Use a real child process and pipe decoding, without an ADB binary or TV.
+ # The Python child writes bytes so its own console encoding cannot affect them.
+ out, err, code = (b"device\n", b"", 0) if args[3] == "get-state" else (stdout, stderr, status)
+ script = (f"import sys; sys.stdout.buffer.write({out!r}); "
+ f"sys.stderr.buffer.write({err!r}); sys.exit({code})")
+ return run([sys.executable, "-c", script], **kwargs)
+
+ # Emulate the locale default on Windows even on CI's UTF-8 Linux runner.
+ with mock.patch.object(subprocess, "_text_encoding", return_value="cp1252"), \
+ mock.patch.object(self.tv.subprocess, "run", side_effect=fake_adb):
+ yield self.tv.NativeAdbDevice("fake-adb", "192.0.2.8", 37123, 5)
+
+ def test_utf8_shell_output_survives_a_non_utf8_host_locale(self):
+ text = "Sala de estar: A\u00e7\u00e3o, \u65e5\u672c\u8a9e, \u0422\u0412\n"
+ with self.device_output(stdout=text.encode("utf-8")) as device:
+ self.assertEqual(device.shell("getprop ro.product.model"), text)
+
+ def test_utf8_transport_errors_survive_a_non_utf8_host_locale(self):
+ text = "n\u00e3o foi poss\u00edvel abrir /sdcard/\u65e5\u672c\u8a9e.png"
+ for stream in ("stdout", "stderr"):
+ with self.subTest(stream=stream), \
+ self.device_output(**{stream: text.encode("utf-8")}, status=1) as device:
+ with self.assertRaises(self.tv.NativeAdbError) as caught:
+ device.shell("getprop ro.product.model")
+ self.assertIn(text, str(caught.exception))
+
+ def test_invalid_utf8_is_replaced_without_losing_the_response(self):
+ with self.device_output(stdout=b"before\xffafter\n") as device:
+ self.assertEqual(device.shell("getprop ro.product.model"), "before\ufffdafter\n")
+
+
@unittest.skipIf(os.name == "nt", "fake adb executable uses a POSIX shebang")
class NativeAdbTest(unittest.TestCase):
def setUp(self):
@@ -55,18 +112,7 @@ elif operation == 'pull':
})
self.environment.start()
self.addCleanup(self.environment.stop)
- spec = importlib.util.spec_from_file_location("tv_native_test", ROOT / "tools" / "tv.py")
- self.tv = importlib.util.module_from_spec(spec)
- # Native commands do not use Python ADB keys or sockets. Stub those imports so
- # this test also runs under CI's system Python, which has no adb_shell installed.
- stubs = {name: types.ModuleType(name) for name in (
- "adb_shell", "adb_shell.auth", "adb_shell.adb_device",
- "adb_shell.auth.keygen", "adb_shell.auth.sign_pythonrsa")}
- stubs["adb_shell.adb_device"].AdbDeviceTcp = object
- stubs["adb_shell.auth.keygen"].keygen = lambda path: None
- stubs["adb_shell.auth.sign_pythonrsa"].PythonRSASigner = lambda pub, priv: None
- with mock.patch.dict(sys.modules, stubs):
- spec.loader.exec_module(self.tv)
+ self.tv = load_tv()
def calls(self):
return [json.loads(line) for line in self.log.read_text().splitlines()]
diff --git a/tools/tv.py b/tools/tv.py
index 3ca9f3099..68ae1a383 100755
--- a/tools/tv.py
+++ b/tools/tv.py
@@ -269,8 +269,9 @@ class NativeAdbDevice:
def _run(self, args, timeout=None):
limit = self.timeout if timeout is None else float(timeout)
try:
+ # Android shell text is UTF-8, including when the host uses a Windows code page.
result = subprocess.run([self.binary, "-s", self.serial, *args],
- capture_output=True, text=True, errors="replace", timeout=limit)
+ capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=limit)
except subprocess.TimeoutExpired as e:
raise NativeAdbError(f"Google ADB {args[0]} on {self.serial} timed out after {limit:g}s.") from e
except OSError as e: