Skip to content

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

tools: a screenshot that stops before its PNG end is refused, not announced

xbsheng9cb67d727417
diff --git a/tests/test_tv_screenshot.py b/tests/test_tv_screenshot.py
index 9b480c8c3..5c9720ec3 100644
--- a/tests/test_tv_screenshot.py
+++ b/tests/test_tv_screenshot.py
@@ -31,7 +31,9 @@ from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "tools" / "tv.py"
-PNG = b"\x89PNG\r\n\x1a\n" + b"0" * 32
+# A complete PNG: the signature, some chunk bytes, and the IEND chunk `screencap -p` always ends with.
+PNG_END = b"IEND\xaeB`\x82"
+PNG = b"\x89PNG\r\n\x1a\n" + b"0" * 32 + PNG_END
class FakeDevice:
@@ -175,6 +177,29 @@ class ScreenshotTest(unittest.TestCase):
self.assertEqual(code, 1)
self.assertIn("is not a PNG", err)
+ def test_a_png_that_stops_before_its_end_chunk_is_not_a_screenshot(self):
+ # A pull that ended early keeps the signature: 40 bytes of PNG header and chunks, then nothing.
+ # The run used to print "screenshot <file> (40 bytes)", return 0 and leave a picture that stops
+ # in the middle, which is the one thing a screenshot in a PR description must not be.
+ cut = PNG[:len(PNG) - len(PNG_END)]
+ with tempfile.TemporaryDirectory() as tmp:
+ local = Path(tmp) / "home.png"
+ code, out, err, _ = self.run_tv(["--screenshot", str(local)], capture=cut)
+ self.assertEqual(code, 1)
+ self.assertIn(f"{local} is {len(cut)} bytes of a PNG that stops before its IEND chunk", err)
+ self.assertIn("try again", err)
+ self.assertEqual("", out, "a screenshot that is not one is not announced")
+ self.assertEqual(local.read_bytes(), cut, "the short file stays for the user to look at")
+
+ def test_a_truncated_signature_alone_is_refused_too(self):
+ # Fewer bytes than the end marker itself: the seek cannot land where the IEND would start, and
+ # an empty tail must not read as an empty marker.
+ with tempfile.TemporaryDirectory() as tmp:
+ code, out, err, _ = self.run_tv(["--screenshot", str(Path(tmp) / "tiny.png")], capture=PNG[:8])
+ self.assertEqual(code, 1)
+ self.assertIn("stops before its IEND chunk", err)
+ self.assertEqual("", out)
+
def test_keys_are_pressed_in_order_before_the_capture(self):
with tempfile.TemporaryDirectory() as tmp:
local = Path(tmp) / "right.png"
diff --git a/tests/test_tv_screenshot_target.py b/tests/test_tv_screenshot_target.py
index 9087606d5..4b2fe652a 100644
--- a/tests/test_tv_screenshot_target.py
+++ b/tests/test_tv_screenshot_target.py
@@ -30,7 +30,7 @@ import unittest
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "tools" / "tv.py"
SOURCE = SCRIPT.read_text(encoding="utf-8")
-PNG = b"\x89PNG\r\n\x1a\n" + b"0" * 32
+PNG = b"\x89PNG\r\n\x1a\n" + b"0" * 32 + b"IEND\xaeB`\x82" # a complete PNG: signature, chunks, IEND
STUB_DEVICE = '''\
import os
@@ -62,7 +62,7 @@ class AdbDeviceTcp:
raise DevicePathInvalidError("Cannot pull from an empty device path")
self._note(f"pull {remote} -> {local}")
with open(local, "wb") as f:
- f.write(b"\\x89PNG\\r\\n\\x1a\\n" + b"0" * 32)
+ f.write(b"\\x89PNG\\r\\n\\x1a\\n" + b"0" * 32 + b"IEND\\xaeB`\\x82")
def shell(self, command, read_timeout_s=None, **kwargs):
self._note(f"shell {command}")
diff --git a/tools/tv.py b/tools/tv.py
index f0b0a44a8..a49b0c9ad 100755
--- a/tools/tv.py
+++ b/tools/tv.py
@@ -314,6 +314,11 @@ ARITY = {"--probe": 1, "--push": 3, "--pull": 3, "--batch": 1, "--reboot": 1}
# PNG on some MediaTek sets (docs/TROUBLESHOOTING.md). /data/local/tmp is writable from shell.
SHOT_REMOTE = "/data/local/tmp/cleanyourtv-shot.png"
PNG = b"\x89PNG\r\n\x1a\n"
+# The last 12 bytes of every PNG: the IEND chunk (length 0, type, CRC). `screencap -p` writes one, so a
+# file that starts with the signature and does not end with this is a picture the pull stopped in the
+# middle of - not a screen. The signature alone let that through: the file was announced with its size
+# and exit 0, and the reader got half a screenshot that GitHub draws as a picture cut off at the bottom.
+PNG_END = b"IEND\xaeB`\x82"
# Key names go into a shell command on the TV, so only plain key names and numeric keycodes get through.
KEY_NAME = re.compile(r"^[A-Z0-9_]+$")
KEY_GAP_S = 0.6 # between two keys, so the UI has finished reacting to the first
@@ -527,6 +532,16 @@ def screenshot(dev, local, keys=(), wait=0.0):
if f.read(len(PNG)) != PNG:
print(f"{local} is not a PNG: the pull from the TV came back damaged; try again", file=sys.stderr)
return 1
+ # A PNG that stops before its IEND chunk kept the signature and went through the check above:
+ # the run printed "screenshot <file> (N bytes)", returned 0 and left the reader a picture that
+ # ends in the middle - a pull that ran out (a transport that dropped, a fill that stopped on
+ # the TV) reads as a plain short screenshot, and the docs ask for those to go in a PR
+ # description. The end marker is what tells the two apart.
+ f.seek(max(0, size - len(PNG_END)))
+ if f.read() != PNG_END:
+ print(f"{local} is {size} bytes of a PNG that stops before its IEND chunk: the pull from the "
+ "TV ended before the picture did; try again", file=sys.stderr)
+ return 1
print(f"screenshot {local} ({size} bytes)")
return 0