Files
mu300-linux/tests/test_android_install.py
T
Abdurrahman Kaan DikeçandClaude Opus 5.5 4f2a3e1994 Merge main: the SD card, the driver fixes, the Magisk installer, mu300-extra and the eMMC by type next to openwrt-luci
Conflicts kept both sides: boot/init's region marker before the usb-net and pick-root blocks; android-vendor-start
links the partitions of the eMMC found by type, through the fork-free link_partitions; the OpenWrt build links the
shared path-commands list and keeps the panel's own services and mu300-sms; no VPN engine in either image (the vpn
extra) while the panel's docker mounts stay positional; assemble.sh enables mu300-atd2 next to the update notice;
android-install keeps openwrt-luci and the extras on a legacy wipe; make-release audits both the panel asset and the
vpn extra.

The third system in main's new places: mu300-extra counts a disk with only openwrt-luci as mounted, the Magisk
installer sees an installed openwrt-luci (space, plan, MU300_BOOT_OS) and names the installed systems in its last
hint (the fixed "ubuntu|openwrt" text became the {1} form install.sh already uses). Tests that read what git tracks
skip where there is no checkout (a container with only the worktree mounted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DqKKtBQZCfxYnHuBCuDqqn
2026-10-05 17:25:26 +03:00

418 lines
22 KiB
Python

"""tools/android-install.sh: the SD card branch, cut out between its markers and run with stubs for Android's
tools (sm, mke2fs, umount) and files standing in for block devices."""
import io
import os
import re
import shutil
import tarfile
from helpers import TOP, ShellTest
from test_boot_init import fake_ext4
SRC = (TOP / 'tools' / 'android-install.sh').read_text()
class SdCard(ShellTest):
def setUp(self):
super().setUp()
m = re.search(r'# --- sd begin\n(.*?)# --- sd end', SRC, re.S)
self.assertIsNotNone(m, 'android-install.sh has no sd block')
self.fn = m.group(1)
self.dev = self.tmp / 'mmcblk1p1'
self.stub('mke2fs', 'echo "mke2fs $*" >> "$STUBLOG/calls"')
self.stub('umount', 'echo "umount $*" >> "$STUBLOG/calls"')
(self.tmp / 'mounts').write_text('')
def run_fn(self, shell, call, volumes='', still_mounted=False, sm_fails=False, pre=''):
(self.tmp / 'volumes').write_text(volumes)
after = volumes if still_mounted else volumes.replace(' mounted ', ' unmounted ')
(self.tmp / 'volumes.after').write_text(after)
fail = 'echo "Error: no volume manager" >&2; exit 3' if sm_fails else 'cat "$STUBLOG/volumes"'
self.stub('sm', f'case "$1" in list-volumes) {fail} ;; '
'unmount) echo "sm unmount $2" >> "$STUBLOG/calls"; cp "$STUBLOG/volumes.after" "$STUBLOG/volumes" ;; esac')
code = ('say() { echo "[device] $*"; }\n' + f'MU300_MOUNTS="{self.tmp}/mounts"\nMU300_SYSFS="{self.tmp}/sys"\n'
+ f'T="{self.tmp}"\n' + self.fn + f'\n{pre}\n{call}\necho "rc=$?"')
r = self.sh(shell, code)
calls = (self.tmp / 'calls').read_text() if (self.tmp / 'calls').exists() else ''
(self.tmp / 'calls').unlink(missing_ok=True)
return r.stdout, calls
def test_blank_card_is_formatted(self):
self.dev.write_bytes(bytes(4096))
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0\n', out)
# a card whose size cannot be read gets mke2fs's own ratio
self.assertIn(f'mke2fs -t ext4 -F -b 4096 -m 0 -i 16384 -L mu300sd {self.dev}', calls)
def test_big_card_gets_fewer_inodes(self):
# one inode per 16 KiB on a 128 GB card is millions of inodes and a slow, failing format: the ratio grows
# with the card, up to 1 MiB, and never below 65536 inodes (a small card keeps the default)
self.dev.write_bytes(bytes(4096))
size = self.tmp / 'sys' / 'class' / 'block' / 'mmcblk1p1' / 'size'
size.parent.mkdir(parents=True)
for sectors, ratio in ((1433600, 16384), (62333952, 262144), (249737216, 1048576), (3900000000, 1048576)):
size.write_text(f'{sectors}\n')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0\n', out)
self.assertIn(f'-m 0 -i {ratio} -L mu300sd', calls, (sectors, shell))
def test_foreign_ext4_is_refused(self):
fake_ext4(self.dev, 'photos')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=1\n', out)
self.assertIn('photos', out)
self.assertNotIn('mke2fs', calls)
def test_unlabelled_ext4_is_refused(self):
# an ext4 without a label is still someone's Linux data
fake_ext4(self.dev, '')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=1\n', out)
self.assertIn('refusing to format', out)
self.assertNotIn('mke2fs', calls)
def test_non_ext4_card_is_formatted(self):
# a FAT/exFAT card (magic bytes not 53ef): formatted after ERASE on the host
fake_ext4(self.dev, 'NO NAME', magic=b'\x00\x00')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0\n', out)
self.assertIn('mke2fs', calls)
def test_existing_installation_is_kept_or_replaced(self):
fake_ext4(self.dev, 'mu300sd')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 0')
self.assertIn('rc=0\n', out); self.assertNotIn('mke2fs', calls)
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0\n', out); self.assertIn('mke2fs', calls)
def test_keep_without_installation_fails(self):
self.dev.write_bytes(bytes(4096))
for shell in self.each_shell():
out, _ = self.run_fn(shell, f'sd_prepare {self.dev} 0')
self.assertIn('rc=1\n', out)
def test_android_releases_the_card(self):
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}', volumes='public:179,1 mounted ABCD-1234\n')
self.assertIn('rc=0\n', out)
self.assertIn('sm unmount public:179,1', calls)
def test_card_android_keeps_is_refused(self):
for shell in self.each_shell():
out, _ = self.run_fn(shell, f'sd_release {self.dev}', volumes='public:179,1 mounted ABCD-1234\n',
still_mounted=True)
self.assertIn('rc=1\n', out)
def test_adopted_card_is_refused(self):
# a card adopted as internal storage is encrypted and part of Android's data: never ours to take
for shell in self.each_shell():
for vols in ('private:179,3 mounted 1234-uuid\n', 'private:179,2 unmounted 1234-uuid\n'):
out, calls = self.run_fn(shell, f'sd_release {self.dev}', volumes=vols)
self.assertIn('rc=1\n', out)
self.assertNotIn('sm unmount', calls)
def test_failing_volume_manager_is_a_refusal(self):
# no answer from sm is no proof that Android has let go of the card
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}', sm_fails=True)
self.assertIn('rc=1\n', out)
self.assertNotIn('umount', calls)
def test_usb_stick_is_left_alone(self):
# public:8,1 is a USB disk (major 8): neither unmounted nor a reason to refuse; only the card (179) counts
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}', still_mounted=True,
volumes='public:8,1 mounted 5555-6666\n')
self.assertIn('rc=0\n', out)
self.assertNotIn('sm unmount', calls)
def test_builtin_private_storage_is_not_an_adopted_card(self):
# Android lists its own data partition as "private mounted null" (no colon): that is no reason to refuse
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}',
volumes='private mounted null\nemulated;0 mounted null\n'
'public:179,1 mounted ABCD-1234\n')
self.assertIn('rc=0\n', out)
self.assertIn('sm unmount public:179,1', calls)
def test_leftover_mounts_of_the_card_only(self):
# vold mounts the card through /dev/block/vold/public:179,N, which sm unmount takes care of; these are
# mounts of the card's own nodes made by hand. They go; the eMMC (mmcblk0) and another card (mmcblk10) stay
t = self.tmp
(t / 'mounts').write_text(f'{t}/mmcblk0p40 /data ext4 rw 0 0\n'
f'{t}/mmcblk1p1 /mnt/media_rw/ABCD vfat rw 0 0\n'
f'{t}/mmcblk1p2 /mnt/other ext4 rw 0 0\n'
f'{t}/mmcblk10p1 /mnt/ten vfat rw 0 0\n')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}')
self.assertIn('rc=0\n', out)
self.assertIn('umount /mnt/media_rw/ABCD', calls)
self.assertIn('umount /mnt/other', calls)
self.assertNotIn('/data', calls); self.assertNotIn('/mnt/ten', calls)
self.assertNotIn('mmcblk0', calls); self.assertNotIn('mmcblk10', calls)
def test_whole_card_never_reaches_the_emmc(self):
# a card without a partition table: SD_DEV is mmcblk1 itself, and mmcblk0 must stay mounted
t = self.tmp
(t / 'mounts').write_text(f'{t}/mmcblk0 /emmc ext4 rw 0 0\n'
f'{t}/mmcblk0p40 /data ext4 rw 0 0\n'
f'{t}/mmcblk1 /mnt/media_rw/ABCD vfat rw 0 0\n')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {t}/mmcblk1')
self.assertIn('rc=0\n', out)
self.assertIn('umount /mnt/media_rw/ABCD', calls)
self.assertNotIn('/data', calls); self.assertNotIn('/emmc', calls)
self.assertNotIn('mmcblk0', calls)
def sys_type(self, disk, kind):
d = self.tmp / 'sys' / 'block' / disk / 'device'
d.mkdir(parents=True, exist_ok=True)
(d / 'type').write_text(kind + '\n')
def test_emmc_is_never_the_card(self):
self.sys_type('mmcblk0', 'SD') # even when sysfs would claim it
for shell in self.each_shell():
for dev in ('/dev/block/mmcblk0p40', '/dev/block/mmcblk0'):
out, _ = self.run_fn(shell, f'sd_check {dev}')
self.assertIn('rc=1\n', out, dev)
def test_non_sd_device_is_refused(self):
self.sys_type('mmcblk1', 'SDIO')
for shell in self.each_shell():
out, _ = self.run_fn(shell, 'sd_check /dev/block/mmcblk1p1')
self.assertIn('rc=1\n', out)
out, _ = self.run_fn(shell, 'sd_check /dev/block/mmcblk2p1') # no sysfs entry at all
self.assertIn('rc=1\n', out)
def test_sd_card_is_accepted(self):
self.sys_type('mmcblk1', 'SD')
for shell in self.each_shell():
for dev in ('/dev/block/mmcblk1p1', '/dev/block/mmcblk1'):
out, _ = self.run_fn(shell, f'sd_check {dev}')
self.assertIn('rc=0\n', out, dev)
def test_fstab_names_the_card(self):
r = self.tmp / 'ubuntu'
(r / 'etc').mkdir(parents=True)
(r / 'etc' / 'fstab').write_text('# static\nLABEL=mu300root / ext4 defaults,noatime 0 1\n'
'tmpfs /tmp tmpfs defaults 0 0\n')
for shell in self.each_shell():
out, _ = self.run_fn(shell, f'sd_fstab {r}', pre='set -e')
self.assertIn('rc=0\n', out)
self.assertEqual((r / 'etc' / 'fstab').read_text(),
'# static\nLABEL=mu300sd / ext4 defaults,noatime 0 1\ntmpfs /tmp tmpfs defaults 0 0\n')
out, _ = self.run_fn(shell, f'sd_fstab {self.tmp}/openwrt', pre='set -e') # no fstab: nothing to do
self.assertIn('rc=0\n', out)
def fake_mount_helper(self, body):
(self.tmp / 'android-mount-mu300root.sh').write_text(
'echo "helper $*" >> "$STUBLOG/calls"\n' + body + '\n')
def test_internal_installation_is_marked(self):
self.fake_mount_helper('[ "$1" = -u ] || mkdir -p "$1"')
mark = self.tmp / 'mu300root-internal' / '.mu300' / 'root-on-sd'
for shell in self.each_shell():
out, calls = self.run_fn(shell, 'sd_mark_internal', pre='set -e; SD_MODE=1 OFF=1 SIZE=2 INTERNAL_EXISTS=1')
self.assertIn('rc=0\n', out)
self.assertTrue(mark.exists())
self.assertIn(f'helper -u {self.tmp}/mu300root-internal', calls)
mark.unlink()
def test_marker_needs_an_internal_installation(self):
self.fake_mount_helper('[ "$1" = -u ] || mkdir -p "$1"')
for shell in self.each_shell():
for pre in ('SD_MODE=1 OFF=1 SIZE=2 INTERNAL_EXISTS=0', 'SD_MODE=1 INTERNAL_EXISTS=1',
'SD_MODE=0 OFF=1 SIZE=2 INTERNAL_EXISTS=1'):
out, calls = self.run_fn(shell, 'sd_mark_internal', pre='set -e; ' + pre)
self.assertIn('rc=0\n', out, pre)
self.assertNotIn('helper', calls, pre)
def test_marker_never_fails_the_install(self):
for shell in self.each_shell():
self.fake_mount_helper('exit 1') # the internal region does not mount
out, _ = self.run_fn(shell, 'sd_mark_internal', pre='set -e; SD_MODE=1 OFF=1 SIZE=2 INTERNAL_EXISTS=1')
self.assertIn('rc=0\n', out)
# it mounts, but the marker cannot be written (the mount point is a file)
self.fake_mount_helper('[ "$1" = -u ] || : > "$1"')
out, calls = self.run_fn(shell, 'sd_mark_internal', pre='set -e; SD_MODE=1 OFF=1 SIZE=2 INTERNAL_EXISTS=1')
self.assertIn('rc=0\n', out)
self.assertIn('helper -u', calls)
(self.tmp / 'mu300root-internal').unlink()
# the directory is there but the marker is not writable (a directory in its place)
self.fake_mount_helper('[ "$1" = -u ] || mkdir -p "$1/.mu300/root-on-sd"')
out, calls = self.run_fn(shell, 'sd_mark_internal', pre='set -e; SD_MODE=1 OFF=1 SIZE=2 INTERNAL_EXISTS=1')
self.assertIn('rc=0\n', out)
self.assertIn('helper -u', calls)
shutil.rmtree(self.tmp / 'mu300root-internal')
def test_internal_install_removes_a_stale_marker(self):
# SD_MODE=0 installs into the internal filesystem; a root-on-sd left there from an earlier card
# installation would make init wait for a card that is no longer what should boot
m = self.tmp / 'root'
for shell in self.each_shell():
(m / '.mu300').mkdir(parents=True, exist_ok=True)
(m / '.mu300' / 'root-on-sd').touch()
out, _ = self.run_fn(shell, f'sd_unmark {m}', pre='set -e; SD_MODE=1')
self.assertIn('rc=0\n', out)
self.assertTrue((m / '.mu300' / 'root-on-sd').exists())
out, _ = self.run_fn(shell, f'sd_unmark {m}', pre='set -e; SD_MODE=0')
self.assertIn('rc=0\n', out)
self.assertFalse((m / '.mu300' / 'root-on-sd').exists())
out, _ = self.run_fn(shell, f'sd_unmark {m}', pre='set -e; unset SD_MODE') # older env files
self.assertIn('rc=0\n', out)
def test_unmark_runs_on_the_installed_filesystem(self):
self.assertIn('\nsd_unmark $M\n', SRC)
self.assertLess(SRC.index('mkdir -p $M/.mu300\n'), SRC.index('\nsd_unmark $M\n'))
class Extras(ShellTest):
"""the extras block: pushed mu300-extra-<name>.tar.gz go onto the Linux partition, and an update of a system that
uses the VPN with the engines of its (older) image keeps them as the vpn extra"""
def setUp(self):
super().setUp()
m = re.search(r'# --- extra begin\n(.*?)# --- extra end', SRC, re.S)
self.assertIsNotNone(m, 'android-install.sh has no extra block')
self.fn = m.group(1)
self.m = self.tmp / 'mu300root'
(self.m / 'ubuntu/etc/mu300').mkdir(parents=True)
def run_fn(self, shell, call):
code = 'set -e\nsay() { echo "[device] $*"; }\n' + f'T="{self.tmp}"\nM="{self.m}"\n' + self.fn + f'\n{call}\necho "rc=$?"'
return self.sh(shell, code)
def test_pushed_extra_is_installed(self):
from test_extra import extra_tarball
for shell in self.each_shell():
shutil.rmtree(self.m / 'extra', ignore_errors=True)
extra_tarball(self.tmp / 'mu300-extra-vpn.tar.gz', release='v2026.10.10')
r = self.run_fn(shell, 'extra_from_push $M')
self.assertIn('rc=0', r.stdout, r.stderr)
self.assertEqual((self.m / 'extra/vpn/release').read_text().strip(), 'v2026.10.10')
self.assertTrue(os.access(self.m / 'extra/vpn/bin/xray', os.X_OK))
self.assertFalse((self.tmp / 'mu300-extra-vpn.tar.gz').exists())
# a broken push is skipped, never the install
(self.tmp / 'mu300-extra-vpn.tar.gz').write_bytes(b'junk')
r = self.run_fn(shell, 'extra_from_push $M')
self.assertIn('rc=0', r.stdout, r.stderr)
self.assertIn('skipped', r.stdout)
self.assertEqual((self.m / 'extra/vpn/release').read_text().strip(), 'v2026.10.10')
# nothing pushed: nothing happens
r = self.run_fn(shell, 'extra_from_push $M')
self.assertIn('rc=0', r.stdout, r.stderr)
def test_update_keeps_the_engines_of_a_vpn_in_use(self):
old = self.m / 'ubuntu'
b = old / 'opt/mu300/bin'
b.mkdir(parents=True, exist_ok=True)
for e in ('xray', 'hev-socks5-tunnel', 'sing-box'):
(b / e).write_text('#!/bin/sh\n')
(b / e).chmod(0o755)
(old / 'etc/mu300/image-version').write_text('v2026.10.06\n')
for shell in self.each_shell():
for enable, kept in (('0', False), ('1', True)):
shutil.rmtree(self.m / 'extra', ignore_errors=True)
(old / 'etc/mu300/vpn.conf').write_text(f'ENABLE={enable}\n')
r = self.run_fn(shell, 'extra_keep_vpn $M $M/ubuntu')
self.assertIn('rc=0', r.stdout, r.stderr)
self.assertEqual((self.m / 'extra/vpn/bin/sing-box').exists(), kept, enable)
self.assertEqual((self.m / 'extra/vpn/release').read_text().strip(), 'v2026.10.06')
# an extra that is there (pushed, or installed earlier) is not replaced
(self.m / 'extra/vpn/release').write_text('v2026.10.10\n')
r = self.run_fn(shell, 'extra_keep_vpn $M $M/ubuntu')
self.assertEqual((self.m / 'extra/vpn/release').read_text().strip(), 'v2026.10.10')
def test_order_in_the_install(self):
self.assertIn('\nextra_from_push $M\n', SRC)
# the engines are taken before the old system is removed
self.assertLess(SRC.index('extra_keep_vpn $M $M/$os'), SRC.index('rm -rf $M/$os && mv $M/$os.new $M/$os'))
def make_tar(path, files):
with tarfile.open(path, 'w:gz') as t:
for name, text in files.items():
data = text.encode()
ti = tarfile.TarInfo(name)
ti.size = len(data)
t.addfile(ti, io.BytesIO(data))
class Systems(ShellTest):
"""The wipe of a root-level Ubuntu and the per-system install loop, run on a fake /data/local/tmp (T) and a
fake mounted filesystem (M)."""
def setUp(self):
super().setUp()
self.T = self.tmp / 'T'
self.M = self.T / 'mu300root'
(self.M / '.mu300').mkdir(parents=True)
# Android's sed has -i like GNU's; the Mac's wants an argument
real = shutil.which('sed') # resolved before the stubs are on PATH; /usr/bin/sed is not there on Alpine
self.stub('sed', f'S={real}\nif [ "$1" = -i ]; then shift; if "$S" --version >/dev/null 2>&1; then exec "$S" -i "$@"; '
'else exec "$S" -i "" "$@"; fi; fi; exec "$S" "$@"')
self.wipe = self.block('legacy-wipe')
self.install = self.block('install-os')
def block(self, name):
m = re.search(rf'# --- {name} begin\n(.*?)# --- {name} end', SRC, re.S)
self.assertIsNotNone(m, f'android-install.sh has no {name} block')
return m.group(1)
def tarball(self, os, extra=None):
files = {'etc/shadow': 'root:*:19000:0:99999:7:::\nubuntu:*:19000:0:99999:7:::\n', 'etc/config/network': 'new\n'}
files.update(extra or {})
make_tar(self.T / f'mu300-{os}.tar.gz', files)
def run_install(self, shell, **env):
e = dict(OSES='openwrt-luci', UPDATE='0', PWHASH='', DEFAULT_LINUX='0', BOOT_OS='openwrt-luci', WIPE_LEGACY='0')
e.update(env)
# sd_unmark and extra_keep_vpn are other blocks of the script (SdCard and Extras test them)
pre = ('set -e\nsay() { echo "[device] $*"; }\nsd_unmark() { :; }\nextra_keep_vpn() { :; }\nssid=; psk=\n'
+ f'T="{self.T}"; M="{self.M}"\n')
r = self.sh(shell, pre + self.install, **e)
self.assertEqual(r.returncode, 0, r.stderr + r.stdout)
return r
def test_installs_openwrt_luci(self):
for shell in self.each_shell():
shutil.rmtree(self.M / 'openwrt-luci', ignore_errors=True)
self.tarball('openwrt-luci')
self.run_install(shell, PWHASH='$6$salt$hash')
self.assertTrue((self.M / 'openwrt-luci' / 'etc' / 'config' / 'network').exists())
shadow = (self.M / 'openwrt-luci' / 'etc' / 'shadow').read_text()
self.assertIn('root:$6$salt$hash:', shadow)
self.assertIn('ubuntu:*:', shadow)
self.assertEqual((self.M / '.mu300' / 'boot-os').read_text().strip(), 'openwrt-luci')
self.assertFalse((self.T / 'mu300-openwrt-luci.tar.gz').exists())
def test_update_keeps_openwrt_luci_config(self):
for shell in self.each_shell():
old = self.M / 'openwrt-luci' / 'etc' / 'config'
old.mkdir(parents=True, exist_ok=True)
(old / 'network').write_text('mine\n')
self.tarball('openwrt-luci')
self.run_install(shell, UPDATE='1')
self.assertEqual((self.M / 'openwrt-luci' / 'etc' / 'config' / 'network').read_text(), 'mine\n')
def test_legacy_wipe_keeps_openwrt_luci(self):
(self.M / 'lib' / 'systemd').mkdir(parents=True)
(self.M / 'lib' / 'systemd' / 'systemd').write_text('x'); (self.M / 'lib' / 'systemd' / 'systemd').chmod(0o755)
(self.M / 'etc').mkdir()
(self.M / 'openwrt-luci' / 'etc').mkdir(parents=True)
(self.M / 'ubuntu').mkdir()
for shell in self.each_shell():
self.sh(shell, f'say() {{ :; }}; M="{self.M}"; WIPE_LEGACY=1\n' + self.wipe)
self.assertTrue((self.M / 'openwrt-luci' / 'etc').is_dir())
self.assertTrue((self.M / 'ubuntu').is_dir())
self.assertTrue((self.M / '.mu300').is_dir())
self.assertFalse((self.M / 'lib').exists())
self.assertFalse((self.M / 'etc').exists())
(self.M / 'lib' / 'systemd').mkdir(parents=True)
(self.M / 'lib' / 'systemd' / 'systemd').write_text('x'); (self.M / 'lib' / 'systemd' / 'systemd').chmod(0o755)