Files
SpotiFLAC-Mobile/scripts/test_check_backend_apk.py
zarzet 728d179d69 refactor(discord): remove rich presence and bundled SDK
Remove the presence setting, platform bridge, SDK archive, and SDK preparation from local and CI builds. Reject leftover Discord native libraries in release APK audits.
2026-09-26 03:07:04 +07:00

73 lines
2.9 KiB
Python

"""Verify native backend payloads and reject removed SDKs in release APKs."""
import struct
import tempfile
import unittest
import zipfile
from pathlib import Path
import check_backend_apk as checker
class BackendApkAuditTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.apk = Path(directory.name) / "release.apk"
self.entries = {"classes.dex": b"backend classes"}
for abi, (elf_class, machine) in checker.ABI_LAYOUT.items():
header = bytearray(20)
header[:6] = b"\x7fELF" + bytes([elf_class, 1])
struct.pack_into("<H", header, 18, machine)
for library in checker.CORE_LIBRARIES + (
"libspotiflac_mobile.so", "libjnidispatch.so",
):
self.entries[f"lib/{abi}/{library}"] = bytes(header)
def audit(self, abis=("arm64-v8a", "armeabi-v7a")):
with zipfile.ZipFile(self.apk, "w") as zf:
for name, data in self.entries.items():
zf.writestr(name, data)
return checker.audit(self.apk, "rust", abis)
def test_universal_passes(self):
self.assertEqual(len(self.audit()), 64)
def test_split_apks_pass(self):
original = self.entries.copy()
for abi in checker.ABI_LAYOUT:
self.entries = {name: data for name, data in original.items()
if not name.startswith("lib/") or name.startswith(f"lib/{abi}/")}
self.assertEqual(len(self.audit((abi,))), 64)
def test_missing_backend_in_either_abi_fails(self):
for abi in checker.ABI_LAYOUT:
for library in ("libspotiflac_mobile.so", "libjnidispatch.so"):
with self.subTest(abi=abi, library=library):
path = f"lib/{abi}/{library}"
data = self.entries.pop(path)
with self.assertRaisesRegex(checker.AuditError, "missing APK entry"):
self.audit()
self.entries[path] = data
def test_wrong_architecture_fails(self):
self.entries["lib/armeabi-v7a/libspotiflac_mobile.so"] = self.entries[
"lib/arm64-v8a/libspotiflac_mobile.so"
]
with self.assertRaisesRegex(checker.AuditError, "ELF class"):
self.audit()
def test_removed_sdk_cannot_leak_from_build_cache(self):
for abi in checker.ABI_LAYOUT:
for library in checker.REMOVED_LIBRARIES:
with self.subTest(abi=abi, library=library):
path = f"lib/{abi}/{library}"
self.entries[path] = self.entries[f"lib/{abi}/libapp.so"]
with self.assertRaisesRegex(checker.AuditError, "removed SDK"):
self.audit()
del self.entries[path]
if __name__ == "__main__":
unittest.main()