mirror of
https://github.com/dikeckaan/mu300-linux.git
synced 2026-12-06 08:03:36 +00:00
The images carried Xray, hev-socks5-tunnel and sing-box (120 MB) whether the VPN was used or not. They are now a release asset, mu300-extra-vpn.tar.gz (tools/make-extra.sh, 41 MB), installed with mu300-extra install vpn onto the Linux partition (extra/vpn), where both systems share it and no update or reinstall of a system touches it. - mu300-update: the extra core (extra_unpack, extra_adopt, extras_to_fetch, extras_install), fetch_verified takes MU300_RELEASE_URL; apply brings installed extras to the release and gives a system that uses the VPN with the engines of its image the vpn extra (downloaded with the rest, before anything changes; else the old image's engines are kept); check lists the extras - mu300-extra: list, status, install, remove, link, adopt; downloads from the release of the running system (MU300_RELEASE, MU300_RELEASE_URL, MU300_EXTRA_FILE for tests and offline use) - mu300-vpn: engines from the extra first, the image second; a missing engine says mu300-extra install vpn, and a VPN that is on gets its engines back by itself (adopt, else download) before the kill switch - one list of commands on the PATH (opt/mu300/lib/path-commands), read by both image builds and by mu300-extra link at boot (rootfs-fixups, OpenWrt mu300-post); mu300-ussd, sms, mu300-voice and mu300-extra join it - make-release builds the extra, audits that no image carries an engine; the toolkit installs it on Turn on Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DqKKtBQZCfxYnHuBCuDqqn
176 lines
9.8 KiB
Python
176 lines
9.8 KiB
Python
"""Checks over every script without running it: syntax under each shell that runs it, executable bits, and rules
|
|
that past bugs taught (see each test)."""
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import unittest
|
|
|
|
from helpers import BIN, TOP, shells
|
|
|
|
OPENWRT = TOP / 'openwrt' / 'overlay'
|
|
|
|
|
|
def shebang(p):
|
|
try:
|
|
with open(p, 'rb') as f:
|
|
first = f.readline()
|
|
except OSError:
|
|
return ''
|
|
return first.decode(errors='replace').strip() if first.startswith(b'#!') else ''
|
|
|
|
|
|
def shell_scripts():
|
|
"""(path, 'sh'|'bash') of every shell script the project ships or runs."""
|
|
cands = list(BIN.iterdir()) + list((TOP / 'tools').glob('*.sh')) + [
|
|
TOP / 'install.sh', TOP / 'uninstall.sh', TOP / 'boot' / 'init', TOP / 'rootfs' / 'assemble.sh',
|
|
TOP / 'kernel' / 'build-all.sh', TOP / 'tools' / 'i18n.sh', TOP / 'tools' / 'self-update.sh',
|
|
TOP / 'tools' / 'linux-mode.sh', TOP / 'tools' / 'storage.sh', TOP / 'android-vendor' / 'ueventd-perms.sh']
|
|
cands += [p for p in OPENWRT.rglob('*') if p.is_file()]
|
|
out = []
|
|
for p in sorted(set(cands)):
|
|
if not p.is_file():
|
|
continue
|
|
sb = shebang(p)
|
|
if 'bash' in sb:
|
|
out.append((p, 'bash'))
|
|
elif sb.endswith('sh') or 'rc.common' in sb or (not sb and p.suffix == '.sh'):
|
|
out.append((p, 'sh'))
|
|
return out
|
|
|
|
|
|
class Syntax(unittest.TestCase):
|
|
def test_every_script_parses(self):
|
|
sh_shells = [s for s in shells() if s[0] != 'bash'] or shells()
|
|
scripts = shell_scripts()
|
|
self.assertGreater(len(scripts), 40)
|
|
for p, kind in scripts:
|
|
if kind == 'bash' and not shutil.which('bash'):
|
|
continue
|
|
for s in ([['bash']] if kind == 'bash' else sh_shells):
|
|
with self.subTest(script=str(p.relative_to(TOP)), shell=' '.join(s)):
|
|
r = subprocess.run(s + ['-n', str(p)], capture_output=True, text=True)
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
|
|
def test_device_programs_are_executable(self):
|
|
for p in BIN.iterdir():
|
|
if p.is_file() and shebang(p):
|
|
with self.subTest(p=p.name):
|
|
self.assertTrue(p.stat().st_mode & 0o111, 'not executable')
|
|
|
|
|
|
class Rules(unittest.TestCase):
|
|
def test_powershell_device_commands_have_no_double_quotes(self):
|
|
# Windows PowerShell 5.1 drops the double quotes inside an argument to a native program: `tr -d "\000"`
|
|
# reached the device as tr -d \000 ("delete the character 0"), and every empty region was "not empty".
|
|
# A command for the device (SuDo, adb shell) may therefore contain no `" and no "".
|
|
for name in ('install.ps1', 'uninstall.ps1'):
|
|
for n, line in enumerate((TOP / name).read_text().splitlines(), 1):
|
|
code = line.split('#', 1)[0] if not line.lstrip().startswith('#') else ''
|
|
if re.search(r'\bSuDo(ToFile)?\s+"|adb shell\s+"', code) and ('`"' in code or '""' in code):
|
|
self.fail(f'{name}:{n}: double quote inside a device command: {line.strip()}')
|
|
|
|
def test_powershell_scripts_are_ascii(self):
|
|
# Windows PowerShell 5.1 reads a file without a BOM as ANSI: non-ASCII text in the script is garbled
|
|
for name in ('install.ps1', 'uninstall.ps1'):
|
|
data = (TOP / name).read_bytes()
|
|
bad = [i for i, b in enumerate(data) if b > 127]
|
|
self.assertFalse(bad, f'{name}: non-ASCII byte at offset {bad[:1]}')
|
|
|
|
def test_windows_pushes_text_with_lf(self):
|
|
# a CRLF clone pushed its scripts as they were and Android's sh ran none of them (issue #7): the Windows
|
|
# installers send text files through PushUnix, never a plain adb push
|
|
for name in ('install.ps1', 'uninstall.ps1'):
|
|
for n, line in enumerate((TOP / name).read_text().splitlines(), 1):
|
|
if 'adb push' in line:
|
|
self.assertNotRegex(line, r'\.(sh|prop)\b|mu300-linux"|\$f"', f'{name}:{n}')
|
|
self.assertIn('eol=lf', (TOP / '.gitattributes').read_text())
|
|
|
|
def test_single_quoted_scripts_have_no_apostrophes(self):
|
|
# A script handed to `sh -c '...'` (docker run) ends at its first apostrophe: "the U30 Air's charger" in a
|
|
# comment there broke OpenWrt's build at release time, while every parser still saw balanced quotes.
|
|
for name in ('openwrt/build-rootfs.sh', 'tools/make-release.sh'):
|
|
lines = (TOP / name).read_text().splitlines()
|
|
for n, line in enumerate(lines):
|
|
if not line.rstrip().endswith("-c '"):
|
|
continue
|
|
for m in range(n + 1, len(lines)):
|
|
if "'" in lines[m]:
|
|
self.assertTrue(lines[m].rstrip().endswith("'") and lines[m].count("'") == 1,
|
|
f'{name}:{m + 1}: an apostrophe inside the script of line {n + 1}')
|
|
break
|
|
|
|
def test_every_tool_that_mounts_the_filesystem_knows_the_card(self):
|
|
for f in ('uninstall.sh', 'uninstall.ps1', 'tools/reset-password.sh', 'tools/android-import-hotspot.sh'):
|
|
self.assertIn('mu300sd', (TOP / f).read_text(), f)
|
|
|
|
def test_the_commands_on_path_are_the_same_everywhere(self):
|
|
# the Ubuntu image, the OpenWrt image and the boot-time links (mu300-extra link, for systems installed before a
|
|
# command had one) link the same commands. They used to be three copies of the list: the fixups' lagged behind
|
|
# (mu300-led, mu300-device were "command not found" on an installed U30 Air), and all three left mu300-ussd out.
|
|
# Now there is one file, and every place reads it.
|
|
lst = (TOP / 'rootfs/overlay/opt/mu300/lib/path-commands').read_text().split()
|
|
self.assertIn('$R/opt/mu300/lib/path-commands', (TOP / 'rootfs/assemble.sh').read_text())
|
|
self.assertIn('/in/opt-mu300/lib/path-commands', (TOP / 'openwrt/build-rootfs.sh').read_text())
|
|
self.assertIn('/lib/path-commands', (BIN / 'mu300-extra').read_text())
|
|
self.assertIn('mu300-extra link', (BIN / 'rootfs-fixups').read_text())
|
|
self.assertIn('mu300-extra link', (OPENWRT / 'etc/init.d/mu300-post').read_text())
|
|
for c in ('mu300-device', 'mu300-led', 'mobile-data', 'mu300-update', 'mu300-ussd', 'sms', 'mu300-extra'):
|
|
self.assertIn(c, lst)
|
|
self.assertEqual(len(lst), len(set(lst)))
|
|
for c in lst:
|
|
self.assertTrue((BIN / c).is_file(), c)
|
|
|
|
def test_images_carry_no_vpn_engine(self):
|
|
# the engines are the vpn extra (mu300-extra): ~120 MB that a system without a VPN does not carry
|
|
for f in ('rootfs/assemble.sh', 'openwrt/build-rootfs.sh'):
|
|
src = (TOP / f).read_text()
|
|
for e in ('xray', 'sing-box', 'hev-socks5-tunnel'):
|
|
self.assertNotRegex(src, rf'opt/mu300/bin/{e}\b', (f, e))
|
|
rel = (TOP / 'tools/make-release.sh').read_text()
|
|
self.assertIn('make-extra.sh', rel)
|
|
self.assertIn('mu300-extra-', rel)
|
|
# the audit refuses an image that still has one
|
|
self.assertRegex(rel, r'opt/mu300/bin/\(xray\|sing-box\|hev-socks5-tunnel\)')
|
|
|
|
def test_init_finds_partitions_after_the_modules(self):
|
|
# the eMMC driver is one of the vendor modules: misc and boot_b cannot be found before they are loaded
|
|
init = (TOP / 'boot' / 'init').read_text()
|
|
calls = [l.strip() for l in init.splitlines() if l.strip() in ('load_vendor_modules', 'find_partitions')]
|
|
self.assertEqual(calls, ['load_vendor_modules', 'find_partitions'])
|
|
|
|
def test_every_device_has_its_files(self):
|
|
# a device the installers know needs its module order; its modules come from kernel/build-<device>.sh
|
|
for dev in ('u30air',):
|
|
self.assertTrue((TOP / 'boot' / f'module-order-{dev}.txt').is_file())
|
|
self.assertTrue((TOP / 'kernel' / f'{dev}.fragment').is_file())
|
|
self.assertIn(dev, (TOP / 'install.sh').read_text())
|
|
self.assertIn(dev, (TOP / 'install.ps1').read_text())
|
|
|
|
def test_mainline_keeps_the_sd_host(self):
|
|
# the SD card can hold the Linux filesystem: the port lets the card slot's host probe next to the eMMC, and
|
|
# still keeps any other sdhci host (the stock DT's sdio_wifi; Wi-Fi is on PCIe) out
|
|
port = (TOP / 'upstream' / 'port' / 'install.py').read_text()
|
|
self.assertIn('MU300: only the eMMC and the card slot', port)
|
|
self.assertIn('if (!of_property_read_bool(pdev->dev.of_node, "non-removable") &&', port)
|
|
self.assertIn('of_property_match_string(pdev->dev.of_node, "sprd,name", "sdio_sd") < 0)', port)
|
|
self.assertIn('MU300: CD GPIO deferred', port)
|
|
# a deferral that is only masked leaves the rest of mmc_of_parse() undone (UHS modes, no-sdio, no-mmc): the
|
|
# unresolvable cd-gpios is dropped and the parse run again
|
|
self.assertIn('of_remove_property(pdev->dev.of_node, cd)', port)
|
|
self.assertIn("'MU300: only the eMMC and the card slot', 'MU300: CD GPIO deferred', "
|
|
"'of_remove_property(pdev->dev.of_node, cd)'", port)
|
|
self.assertRegex((TOP / 'upstream' / 'make-bundle.sh').read_text(), r"printf 'sdcard\\n' > \"\$W/b/features\"")
|
|
|
|
def test_every_release_kernel_bundle_has_the_sd_host(self):
|
|
# 5.4 reads the card as well (FINDINGS 31j): its bundle says so, and the release audit fails when any of the
|
|
# three bundles does not (mu300-update refuses such a bundle for a system on the card)
|
|
mr = (TOP / 'tools' / 'make-release.sh').read_text()
|
|
self.assertIn("printf 'sdcard\\n' > \"$K/features\"", mr)
|
|
self.assertLess(mr.index('$K/features'), mr.index('tar -C "$K" -czf "$D/mu300-kernel.tar.gz" .'))
|
|
self.assertIn('for a in mu300-kernel mu300-kernel-6.18 mu300-kernel-7.2; do\n'
|
|
' tar -xzOf "$D/$a.tar.gz" ./features 2>/dev/null | grep -qx sdcard', mr)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|