mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-16 13:33:02 +08:00
558 lines
30 KiB
Python
558 lines
30 KiB
Python
"""Run the real update RPC with isolated package managers, HTTP and cache files."""
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PRIMARY = 'https://primary.argon.test/updates/stable.json'
|
|
FALLBACK = 'https://fallback.argon.test/updates/stable.json'
|
|
|
|
|
|
class UpdateRpcTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temp.cleanup)
|
|
self.directory = Path(self.temp.name)
|
|
self.cache = self.directory / 'cache.json'
|
|
self.payload = self.directory / 'release.json'
|
|
self.requests = self.directory / 'requests'
|
|
fakebin = self.directory / 'bin'
|
|
fakebin.mkdir()
|
|
scripts = {
|
|
'apk': '''case "$1" in
|
|
list)
|
|
case "$3" in
|
|
luci-i18n-argon-config-*)
|
|
if [ -f "$LANG_INSTALLED" ]; then
|
|
while read -r name version; do
|
|
[ "$3" = 'luci-i18n-argon-config-*' ] || [ "$3" = "$name" ] || continue
|
|
printf '%s-%s noarch [installed]\\n' "$name" "$version"
|
|
done < "$LANG_INSTALLED"
|
|
fi
|
|
exit 0 ;;
|
|
esac
|
|
test -n "$APK_VERSION" || exit 1; v=$APK_VERSION; [ ! -f "$INSTALLED" ] || v=$(cat "$INSTALLED"); test -n "$v" || exit 1; printf 'luci-theme-argon-%s noarch [installed]\\n' "$v" ;;
|
|
version) test "$COMPARE_FAIL" != 1 || exit 1; [ "$3" = "$4" ] && printf '=\\n' || { [ "$3" = 3.0.0-r20261015 ] && printf '>\\n' || printf '<\\n'; } ;;
|
|
add) printf '%s\\n' "$@" >> "$MUTATIONS"; test "$INSTALL_FAIL" != 1 || { echo 'database locked'; exit 1; }; test "$WRONG_VERSION" = 1 || printf '3.0.0-r2' > "$INSTALLED"
|
|
for file in "$@"; do
|
|
case "$file" in */luci-i18n-argon-config-*.apk)
|
|
name=${file##*/}; name=${name%-3.0.0-r2.apk}
|
|
if [ -f "$LANG_INSTALLED" ]; then
|
|
awk -v name="$name" '$1 != name' "$LANG_INSTALLED" > "$LANG_INSTALLED.tmp"
|
|
mv "$LANG_INSTALLED.tmp" "$LANG_INSTALLED"
|
|
fi
|
|
test "$WRONG_LANGUAGE_VERSION" = 1 || printf '%s 3.0.0-r2\\n' "$name" >> "$LANG_INSTALLED" ;;
|
|
esac
|
|
done
|
|
echo 'Theme installed' ;;
|
|
esac
|
|
''',
|
|
'opkg': '''if [ "$OPKG_QUERY_GUARD" = 1 ]; then
|
|
exec 8>"$MUTATIONS.lock"
|
|
flock -n 8 || exit 75
|
|
sleep .05
|
|
fi
|
|
case "$1" in
|
|
list-installed) [ ! -f "$LANG_INSTALLED" ] || awk '{ print $1 " - " $2 }' "$LANG_INSTALLED" ;;
|
|
status)
|
|
case "$2" in luci-i18n-argon-config-*)
|
|
v=$(awk -v name="$2" '$1 == name { v=$2 } END { print v }' "$LANG_INSTALLED" 2>/dev/null)
|
|
test -n "$v" || exit 0
|
|
printf 'Package: %s\\nVersion: %s\\nStatus: install ok installed\\n' "$2" "$v"; exit 0 ;;
|
|
esac
|
|
v=$OPKG_VERSION; [ ! -f "$INSTALLED" ] || v=$(cat "$INSTALLED"); test -n "$v" || exit 1; printf 'Package: luci-theme-argon\\nVersion: %s\\nStatus: install ok installed\\n' "$v" ;;
|
|
compare-versions) [ "$3" = "<<" ] && [ "$COMPARE_FAIL" != 1 ] && [ "$2" != "$4" ] && [ "$2" != 3.0.0-20261015 ] ;;
|
|
install) printf '%s\\n' "$@" >> "$MUTATIONS"; test "$INSTALL_FAIL" != 1 || { echo 'database locked'; exit 1; }; test "$WRONG_VERSION" = 1 || printf '3.0.0-2' > "$INSTALLED"
|
|
for file in "$@"; do
|
|
case "$file" in */luci-i18n-argon-config-*.ipk)
|
|
name=${file##*/}; name=${name%_v3.0.0-2_all.ipk}
|
|
test "$WRONG_LANGUAGE_VERSION" = 1 || printf '%s v3.0.0-2\\n' "$name" >> "$LANG_INSTALLED" ;;
|
|
esac
|
|
done
|
|
echo 'Theme installed' ;;
|
|
esac
|
|
''',
|
|
'df': 'printf "Filesystem 1024-blocks Used Available Capacity Mounted on\\nroot 100000 1000 %s 1%% /\\n" "${FREE_KB:-99000}"\n',
|
|
'wget': '''printf '%s\\n' "$@" >> "$REQUESTS"
|
|
test "$FETCH_FAIL" != 1 || exit 8
|
|
while [ "$#" -gt 0 ]; do
|
|
if [ "$1" = -O ]; then shift; destination=$1; fi
|
|
url=$1
|
|
shift
|
|
done
|
|
case "$destination" in
|
|
*manifest.json)
|
|
sleep "${MANIFEST_DELAY:-0}"
|
|
case "$url" in
|
|
*primary.argon.test*) test "$PRIMARY_FAIL" != 1 || exit 8; cp "${PRIMARY_PAYLOAD:-$PAYLOAD}" "$destination" ;;
|
|
*) cp "$PAYLOAD" "$destination" ;;
|
|
esac ;;
|
|
*) sleep "${DOWNLOAD_DELAY:-0}"; test "$DOWNLOAD_FAIL" != 1 || exit 8
|
|
case "$url" in
|
|
*luci-i18n-argon-config-*) test "$LANG_DOWNLOAD_FAIL" != 1 || exit 8; cp "$LANG_DATA" "$destination" ;;
|
|
*) cp "$PACKAGE_DATA" "$destination" ;;
|
|
esac ;;
|
|
esac
|
|
''',
|
|
'uci': 'test -z "$LUCI_LANGUAGE" || printf "%s\\n" "$LUCI_LANGUAGE"\n'
|
|
}
|
|
for name, content in scripts.items():
|
|
path = fakebin / name
|
|
path.write_text('#!/bin/sh\n' + content)
|
|
path.chmod(0o755)
|
|
helper = self.directory / 'json_fixture.py'
|
|
helper.write_text('''import json,re,sys
|
|
root=json.load(sys.stdin); path=json.loads(sys.argv[1]); op=sys.argv[2]; args=sys.argv[3:]
|
|
obj=root
|
|
for key in path: obj=obj[int(key)] if isinstance(obj,list) else obj[key]
|
|
def alias(key): return re.sub(r'[^a-zA-Z0-9_]', '_', str(key))
|
|
def resolve(key):
|
|
if key in obj: return key
|
|
return next((name for name in reversed(list(obj)) if alias(name)==key), key)
|
|
if op=='select':
|
|
if args[0]=='..': path=path[:-1]
|
|
else:
|
|
key=args[0] if isinstance(obj,list) else resolve(args[0])
|
|
child=obj[int(key)] if isinstance(obj,list) else obj[key]
|
|
if not isinstance(child,(dict,list)): raise ValueError('not a container')
|
|
path.append(key)
|
|
print(json.dumps(path))
|
|
elif op=='keys': print(' '.join(map(alias,range(len(obj)) if isinstance(obj,list) else obj.keys())))
|
|
elif op=='get':
|
|
value=obj.get(resolve(args[0]),'')
|
|
print(int(value) if isinstance(value,bool) else '' if value is None else value)
|
|
elif op=='type':
|
|
value=obj.get(resolve(args[0]))
|
|
print('object' if isinstance(value,dict) else 'array' if isinstance(value,list) else
|
|
'boolean' if isinstance(value,bool) else 'int' if isinstance(value,int) else
|
|
'string' if isinstance(value,str) else '')
|
|
elif op=='add':
|
|
obj[args[1]]=int(args[2]) if args[0]=='int' else bool(int(args[2])) if args[0]=='boolean' else {} if args[0]=='object' else args[2]
|
|
print(json.dumps(root))
|
|
elif op=='validate':
|
|
if not isinstance(root,(dict,list)): raise ValueError('invalid root')
|
|
''')
|
|
shim = self.directory / 'jshn.sh'
|
|
shim.write_text(f'''fixture_json() {{ printf '%s' "$FIXTURE_JSON" | python3 '{helper}' "$FIXTURE_PATH" "$@"; }}
|
|
json_load() {{ FIXTURE_JSON=$1; FIXTURE_PATH='[]'; fixture_json validate >/dev/null 2>&1; }}
|
|
json_load_file() {{ json_load "$(cat "$1")"; }}
|
|
json_init() {{ FIXTURE_JSON='{{}}'; FIXTURE_PATH='[]'; }}
|
|
json_get_var() {{ fixture_value=$(fixture_json get "$2"); export "$1=$fixture_value"; }}
|
|
json_get_type() {{ fixture_value=$(fixture_json type "$2"); export "$1=$fixture_value"; }}
|
|
json_get_keys() {{ fixture_value=$(fixture_json keys); export "$1=$fixture_value"; }}
|
|
json_select() {{ fixture_value=$(fixture_json select "$1" 2>/dev/null) || return 1; FIXTURE_PATH=$fixture_value; }}
|
|
json_add_string() {{ FIXTURE_JSON=$(fixture_json add string "$1" "$2"); }}
|
|
json_add_int() {{ FIXTURE_JSON=$(fixture_json add int "$1" "$2"); }}
|
|
json_add_boolean() {{ FIXTURE_JSON=$(fixture_json add boolean "$1" "$2"); }}
|
|
json_add_object() {{ FIXTURE_JSON=$(fixture_json add object "$1" ''); json_select "$1"; }}
|
|
json_close_object() {{ json_select ..; }}
|
|
json_set_namespace() {{
|
|
[ "$#" -lt 2 ] || export "$2=${{FIXTURE_NAMESPACE:-base}}"
|
|
printf '%s' "$FIXTURE_JSON" > '{self.directory}/namespace-'"${{FIXTURE_NAMESPACE:-base}}"'.json'
|
|
printf '%s' "$FIXTURE_PATH" > '{self.directory}/namespace-'"${{FIXTURE_NAMESPACE:-base}}"'.path'
|
|
FIXTURE_NAMESPACE=${{1:-base}}
|
|
if [ -f '{self.directory}/namespace-'"$FIXTURE_NAMESPACE"'.json' ]; then
|
|
FIXTURE_JSON=$(cat '{self.directory}/namespace-'"$FIXTURE_NAMESPACE"'.json')
|
|
FIXTURE_PATH=$(cat '{self.directory}/namespace-'"$FIXTURE_NAMESPACE"'.path')
|
|
else json_init; fi
|
|
}}
|
|
|
|
json_dump() {{ printf '%s\\n' "$FIXTURE_JSON"; }}
|
|
''')
|
|
self.script = self.directory / 'rpc.sh'
|
|
sources = self.directory / 'sources.json'
|
|
sources.write_text(json.dumps({'primary': PRIMARY, 'fallback': FALLBACK}))
|
|
self.catalogs = self.directory / 'catalogs'
|
|
self.catalogs.mkdir()
|
|
self.script.write_text((ROOT / 'root/usr/libexec/rpcd/luci.argon_update').read_text()
|
|
.replace('/usr/share/libubox/jshn.sh', str(shim))
|
|
.replace('/usr/share/argon/update-sources.json', str(sources))
|
|
.replace('/var/run/argon-update/release.json', str(self.cache))
|
|
.replace('/var/run/argon-update', str(self.directory / 'state'))
|
|
.replace('/usr/lib/lua/luci/i18n', str(self.catalogs))
|
|
.replace('wget_bin=/usr/libexec/wget-ssl', 'wget_bin=' + str(fakebin / 'wget')))
|
|
self.env = dict(os.environ, PATH=str(fakebin) + os.pathsep + os.environ['PATH'],
|
|
APK_VERSION='3.0.0-r1', OPKG_VERSION='', PAYLOAD=str(self.payload),
|
|
REQUESTS=str(self.requests), FETCH_FAIL='0', INSTALLED=str(self.directory / 'installed'),
|
|
MUTATIONS=str(self.directory / 'mutations'), PACKAGE_DATA=str(self.directory / 'package'),
|
|
LANG_DATA=str(self.directory / 'language'), LANG_INSTALLED=str(self.directory / 'languages-installed'))
|
|
(self.directory / 'package').write_bytes(b'argon-package')
|
|
(self.directory / 'language').write_bytes(b'argon-language')
|
|
self.release = {'schema': 1, 'repository': 'jerrykuku/luci-theme-argon', 'tag': 'v3.0.0',
|
|
'published_at': '2026-10-16T01:00:00Z', 'notes': '## 更新\n- 修复布局。',
|
|
'ipk': 'luci-theme-argon_3.0.0-2_all.ipk', 'apk': 'luci-theme-argon-3.0.0-r2.apk',
|
|
'ipk_sha256': hashlib.sha256(b'argon-package').hexdigest(),
|
|
'apk_sha256': hashlib.sha256(b'argon-package').hexdigest(),
|
|
'ipk_size': 13, 'apk_size': 13}
|
|
self.payload.write_text(json.dumps(self.release))
|
|
|
|
def call(self, method, **arguments):
|
|
result = subprocess.run(['sh', str(self.script), 'call', method],
|
|
input=json.dumps(arguments) + '\n', env=self.env,
|
|
capture_output=True, text=True, check=True)
|
|
return json.loads(result.stdout)
|
|
|
|
def test_local_get_and_protocol_have_no_network_or_package_mutation(self):
|
|
result = self.call('get')
|
|
self.assertEqual(result['result'], 0)
|
|
self.assertEqual(result['current_version'], '3.0.0-r1')
|
|
self.assertFalse(result['can_upgrade'])
|
|
self.assertEqual(result['upgrade']['phase'], 'idle')
|
|
self.assertFalse(self.requests.exists())
|
|
protocol = subprocess.check_output(['sh', str(self.script), 'list'], text=True)
|
|
self.assertEqual(json.loads(protocol), {'get': {}, 'check': {}, 'status': {}, 'start': {'tag': 'String', 'package': 'String'}})
|
|
self.env.update(APK_VERSION='', OPKG_VERSION='3.0.0-2')
|
|
self.assertEqual(self.call('get')['package_manager'], 'opkg')
|
|
self.assertEqual(self.call('get')['current_version'], '3.0.0-2')
|
|
self.env['OPKG_VERSION'] = ''
|
|
self.assertEqual(self.call('get')['current_version'], '')
|
|
|
|
def test_check_uses_configured_static_source_and_ignores_rpc_urls(self):
|
|
result = self.call('check', url='http://untrusted.invalid/', version='99.0.0')
|
|
self.assertEqual(result['result'], 0)
|
|
self.assertEqual(result['apk'], 'luci-theme-argon-3.0.0-r2.apk')
|
|
self.assertEqual(result['ipk'], 'luci-theme-argon_3.0.0-2_all.ipk')
|
|
self.assertEqual(result['notes'], self.release['notes'])
|
|
self.assertEqual(result['current_version'], '3.0.0-r1')
|
|
requests = self.requests.read_text()
|
|
self.assertIn(PRIMARY, requests)
|
|
self.assertNotIn(FALLBACK, requests)
|
|
self.assertNotIn('api.github.com', requests)
|
|
self.assertNotIn('Cache-Control: no-cache', requests)
|
|
self.assertNotIn('untrusted.invalid', requests)
|
|
self.assertEqual(self.cache.stat().st_mode & 0o777, 0o600)
|
|
|
|
def test_success_cache_avoids_network_but_reloads_the_installed_version(self):
|
|
self.call('check')
|
|
before = self.requests.read_text()
|
|
self.env['APK_VERSION'] = '3.0.0-r2'
|
|
self.assertEqual(self.call('check')['current_version'], '3.0.0-r2')
|
|
self.assertEqual(self.requests.read_text(), before)
|
|
self.assertEqual(self.call('get')['tag'], 'v3.0.0')
|
|
|
|
def test_primary_failure_and_invalid_primary_each_fall_back_to_one_complete_manifest(self):
|
|
invalid = self.directory / 'invalid.json'
|
|
invalid.write_text(json.dumps(dict(self.release, apk_sha256='invalid', notes='wrong source')))
|
|
for patch in [{'PRIMARY_FAIL': '1'}, {'PRIMARY_PAYLOAD': str(invalid)}]:
|
|
with self.subTest(patch=patch):
|
|
self.cache.unlink(missing_ok=True)
|
|
self.requests.unlink(missing_ok=True)
|
|
old = self.env.copy()
|
|
self.env.update(patch)
|
|
checked = self.call('check')
|
|
self.assertEqual(checked['result'], 0)
|
|
self.assertEqual(checked['notes'], self.release['notes'])
|
|
requests = self.requests.read_text()
|
|
self.assertEqual(requests.count(PRIMARY), 1)
|
|
self.assertEqual(requests.count(FALLBACK), 1)
|
|
self.env = old
|
|
|
|
def test_failed_checks_have_a_cooldown_and_keep_the_previous_success(self):
|
|
cached = self.call('check')
|
|
cached['checked_at'] -= 400
|
|
self.cache.write_text(json.dumps(cached))
|
|
self.env['FETCH_FAIL'] = '1'
|
|
failed = self.call('check')
|
|
requests = self.requests.read_text()
|
|
self.assertEqual(failed['result'], 1)
|
|
self.assertGreater(failed['retry_at'], time.time())
|
|
self.assertEqual(self.call('check')['retry_at'], failed['retry_at'])
|
|
self.assertEqual(self.requests.read_text(), requests)
|
|
self.assertEqual(self.call('get')['notes'], cached['notes'])
|
|
(self.directory / 'state/retry_at').write_text(str(int(time.time()) - 1))
|
|
self.env['FETCH_FAIL'] = '0'
|
|
self.assertEqual(self.call('check')['result'], 0)
|
|
self.assertFalse((self.directory / 'state/retry_at').exists())
|
|
|
|
def test_concurrent_checks_share_one_network_request(self):
|
|
self.env['MANIFEST_DELAY'] = '1'
|
|
processes = [subprocess.Popen(['sh', str(self.script), 'call', 'check'], env=self.env,
|
|
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, text=True) for _ in range(3)]
|
|
outputs = [process.communicate(timeout=20) for process in processes]
|
|
for process, (output, errors) in zip(processes, outputs):
|
|
self.assertEqual(process.returncode, 0, errors)
|
|
self.assertEqual(json.loads(output)['result'], 0)
|
|
self.assertEqual(self.requests.read_text().count(PRIMARY), 1)
|
|
|
|
def test_remote_fields_cannot_override_local_state_or_add_a_download_url(self):
|
|
self.payload.write_text(json.dumps(dict(self.release, checked_at=1, result=99,
|
|
current_version='99.0.0', url='https://untrusted.invalid/')))
|
|
result = self.call('check')
|
|
self.assertEqual(result['result'], 0)
|
|
self.assertEqual(result['current_version'], '3.0.0-r1')
|
|
self.assertGreater(result['checked_at'], 1)
|
|
self.assertNotIn('url', result)
|
|
|
|
def test_parallel_opkg_queries_do_not_lose_versions_to_the_native_database_lock(self):
|
|
self.env.update(APK_VERSION='', OPKG_VERSION='3.0.0-1', OPKG_QUERY_GUARD='1')
|
|
self.assertTrue(self.call('check')['can_upgrade'])
|
|
processes = [subprocess.Popen(['sh', str(self.script), 'call', 'get'], env=self.env,
|
|
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, text=True) for _ in range(4)]
|
|
outputs = [process.communicate(timeout=20) for process in processes]
|
|
for process, (output, errors) in zip(processes, outputs):
|
|
self.assertEqual(process.returncode, 0, errors)
|
|
result = json.loads(output)
|
|
self.assertEqual(result['current_version'], '3.0.0-1')
|
|
self.assertTrue(result['can_upgrade'])
|
|
|
|
def test_expired_or_future_cache_refreshes_and_failed_refresh_keeps_notes(self):
|
|
first = self.call('check')
|
|
for checked_at in [int(time.time()) - 400, int(time.time()) + 1000]:
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
old = dict(first, checked_at=checked_at)
|
|
self.cache.write_text(json.dumps(old))
|
|
self.env['FETCH_FAIL'] = '1'
|
|
self.assertEqual(self.call('check')['result'], 1)
|
|
self.assertEqual(json.loads(self.cache.read_text()), old)
|
|
self.assertEqual(self.call('get')['notes'], first['notes'])
|
|
|
|
def test_invalid_and_prerelease_responses_never_enter_cache(self):
|
|
for value in ['not JSON', '{}', '[]', json.dumps(dict(self.release, schema=2)),
|
|
json.dumps(dict(self.release, repository='other/repo')),
|
|
json.dumps(dict(self.release, tag='v3.1.0-beta.1'))]:
|
|
with self.subTest(value=value[:60]):
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
self.payload.write_text(value)
|
|
self.assertNotEqual(self.call('check')['result'], 0)
|
|
self.assertFalse(self.cache.exists())
|
|
|
|
def test_mismatched_and_unsafe_asset_names_are_rejected(self):
|
|
for name in [
|
|
'luci-theme-argon-4.0.0-r2.apk', 'luci-theme-argon_4.0.0_all.ipk',
|
|
'luci-theme-argon-3.0.0-r2.apk?evil', 'luci-theme-argon_3.0.0-../../tmp_all.ipk']:
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
self.payload.write_text(json.dumps(dict(self.release, apk=name)))
|
|
self.assertNotEqual(self.call('check')['result'], 0)
|
|
self.assertFalse(self.cache.exists())
|
|
|
|
def test_linked_cache_is_not_read_or_overwritten_and_failed_download_is_retryable(self):
|
|
outside = self.directory / 'outside'
|
|
outside.write_text('keep me')
|
|
self.cache.symlink_to(outside)
|
|
self.assertNotIn('tag', self.call('get'))
|
|
self.env['FETCH_FAIL'] = '1'
|
|
self.assertEqual(self.call('check')['result'], 1)
|
|
self.env['FETCH_FAIL'] = '0'
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
self.assertEqual(self.call('check')['result'], 0)
|
|
self.assertEqual(outside.read_text(), 'keep me')
|
|
|
|
def test_acl_separates_read_only_status_and_write_install(self):
|
|
acl = json.loads((ROOT / 'root/usr/share/rpcd/acl.d/luci-app-argon-config.json').read_text())['luci-app-argon-config']
|
|
self.assertEqual(set(acl['read']['ubus']['luci.argon_update']), {'get', 'check', 'status'})
|
|
self.assertEqual(acl['write']['ubus']['luci.argon_update'], ['start'])
|
|
|
|
def start(self, **patch):
|
|
return self.call('start', **dict({'tag': 'v3.0.0', 'package':
|
|
'luci-theme-argon-3.0.0-r2.apk' if self.env['APK_VERSION'] else 'luci-theme-argon_3.0.0-2_all.ipk'}, **patch))
|
|
|
|
def finished(self):
|
|
for _ in range(100):
|
|
result = self.call('status')
|
|
if not result['upgrade']['active']:
|
|
return result
|
|
time.sleep(.05)
|
|
self.fail('worker did not finish')
|
|
|
|
def add_languages(self, *languages):
|
|
digest = hashlib.sha256((self.directory / 'language').read_bytes()).hexdigest()
|
|
self.release['translations'] = {language: {
|
|
'apk': f'luci-i18n-argon-config-{language}-3.0.0-r2.apk',
|
|
'ipk': f'luci-i18n-argon-config-{language}_v3.0.0-2_all.ipk',
|
|
'apk_sha256': digest, 'ipk_sha256': digest,
|
|
'apk_size': 14, 'ipk_size': 14,
|
|
} for language in languages}
|
|
self.payload.write_text(json.dumps(self.release))
|
|
|
|
def test_bundled_chinese_is_installed_with_the_theme_for_both_managers(self):
|
|
self.add_languages('zh-cn', 'de')
|
|
(self.catalogs / 'argon-config.zh-cn.lmo').write_bytes(b'bundled Chinese')
|
|
for manager in ['apk', 'opkg']:
|
|
with self.subTest(manager=manager):
|
|
for file in ['installed', 'languages-installed', 'mutations', 'requests']:
|
|
(self.directory / file).unlink(missing_ok=True)
|
|
self.env.update(APK_VERSION='3.0.0-r1' if manager == 'apk' else '',
|
|
OPKG_VERSION='3.0.0-1' if manager == 'opkg' else '')
|
|
self.assertTrue(self.call('check')['can_upgrade'])
|
|
self.assertEqual(self.start()['result'], 0)
|
|
done = self.finished()
|
|
self.assertEqual(done['upgrade']['phase'], 'complete', done)
|
|
command = (self.directory / 'mutations').read_text()
|
|
self.assertIn('luci-i18n-argon-config-zh-cn', command)
|
|
self.assertNotIn('luci-i18n-argon-config-de', command)
|
|
self.assertLess(command.index('luci-theme-argon'), command.index('luci-i18n-argon-config-zh-cn'))
|
|
self.assertNotIn('force-', command)
|
|
|
|
def test_missing_verified_language_metadata_blocks_theme_only_installation(self):
|
|
(self.catalogs / 'argon-config.zh-cn.lmo').write_bytes(b'bundled Chinese')
|
|
self.assertFalse(self.call('check')['can_upgrade'])
|
|
self.assertEqual(self.start()['result'], 3)
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_configured_locale_is_added_when_available_and_missing_catalog_packages_are_repaired(self):
|
|
self.add_languages('zh-cn', 'de')
|
|
self.env['LUCI_LANGUAGE'] = 'zh_Hans'
|
|
(self.directory / 'languages-installed').write_text('luci-i18n-argon-config-de 3.0.0-r1\n')
|
|
self.assertTrue(self.call('check')['can_upgrade'])
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertEqual(self.finished()['upgrade']['phase'], 'complete')
|
|
command = (self.directory / 'mutations').read_text()
|
|
for language in ['zh-cn', 'de']:
|
|
self.assertIn('luci-i18n-argon-config-' + language, command)
|
|
|
|
def test_language_download_or_checksum_failure_never_mutates_the_theme(self):
|
|
self.add_languages('zh-cn')
|
|
(self.catalogs / 'argon-config.zh-cn.lmo').touch()
|
|
self.call('check')
|
|
for failure in ['network', 'checksum']:
|
|
with self.subTest(failure=failure):
|
|
self.env['LANG_DOWNLOAD_FAIL'] = '1' if failure == 'network' else '0'
|
|
if failure == 'checksum':
|
|
(self.directory / 'language').write_bytes(b'corrupt')
|
|
self.assertEqual(self.start()['result'], 0)
|
|
done = self.finished()
|
|
self.assertEqual(done['upgrade']['phase'], 'failed')
|
|
self.assertEqual(done['upgrade']['error'], failure, done)
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_language_version_is_verified_before_reporting_complete(self):
|
|
self.add_languages('zh-cn')
|
|
(self.catalogs / 'argon-config.zh-cn.lmo').touch()
|
|
self.env['WRONG_LANGUAGE_VERSION'] = '1'
|
|
self.call('check')
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertEqual(self.finished()['upgrade']['error'], 'install')
|
|
|
|
def test_translation_metadata_is_validated_and_only_contract_fields_are_cached(self):
|
|
self.add_languages('zh-cn')
|
|
original = self.release['translations']['zh-cn'].copy()
|
|
for bad in [[], {'../zh-cn': original}, {'zh-cn': dict(original, apk_size=0)},
|
|
{'zh-cn': dict(original, ipk_sha256='bad')},
|
|
{'zh-cn': dict(original, apk='luci-i18n-argon-config-zh-cn-4.0.0-r1.apk')}]:
|
|
with self.subTest(metadata=bad):
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
self.payload.write_text(json.dumps(dict(self.release, translations=bad)))
|
|
self.assertNotEqual(self.call('check')['result'], 0)
|
|
self.assertFalse(self.cache.exists())
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
self.payload.write_text(json.dumps(dict(self.release, translations={'zh-cn': dict(original, url='https://untrusted.invalid/')})))
|
|
result = self.call('check')
|
|
self.assertEqual(result['translations']['zh-cn'], original)
|
|
self.assertNotIn('untrusted.invalid', self.requests.read_text())
|
|
|
|
def test_background_install_for_both_managers_and_status_survives_reloads(self):
|
|
for manager in ['apk', 'opkg']:
|
|
with self.subTest(manager=manager):
|
|
installed = self.directory / 'installed'
|
|
installed.unlink(missing_ok=True)
|
|
self.env.update(APK_VERSION='3.0.0-r1' if manager == 'apk' else '',
|
|
OPKG_VERSION='3.0.0-1' if manager == 'opkg' else '')
|
|
checked = self.call('check')
|
|
self.assertTrue(checked['can_upgrade'])
|
|
self.assertEqual(self.start()['result'], 0)
|
|
done = self.finished()
|
|
self.assertEqual(done['upgrade']['phase'], 'complete', done)
|
|
self.assertEqual(done['current_version'], '3.0.0-r2' if manager == 'apk' else '3.0.0-2')
|
|
self.assertFalse(self.call('get')['can_upgrade'])
|
|
self.assertEqual(self.call('get')['upgrade']['phase'], 'complete')
|
|
command = (self.directory / 'mutations').read_text()
|
|
self.assertNotIn('force-', command)
|
|
self.assertIn('Theme installed', done['upgrade']['log'])
|
|
self.assertEqual((self.directory / 'state/status.json').stat().st_mode & 0o777, 0o600)
|
|
self.assertEqual(list((self.directory / 'state').glob('job.*')), [])
|
|
|
|
def test_start_rejects_changed_or_expired_metadata_unknown_versions_and_downgrades(self):
|
|
self.assertEqual(self.start()['result'], 3)
|
|
initial = self.call('check')
|
|
for patch in [{'tag': 'v4.0.0'}, {'package': '../../unsafe.apk'}, {'package': 'luci-theme-argon-3.0.0-r1.apk'}]:
|
|
self.assertEqual(self.start(**patch)['result'], 3)
|
|
for value in ['unknown', '3.0.0-r2', '3.0.0-r20261015']:
|
|
self.env['APK_VERSION'] = value
|
|
self.assertEqual(self.start()['result'], 5)
|
|
self.env['APK_VERSION'] = '3.0.0-r1'
|
|
self.cache.write_text(json.dumps(dict(initial, checked_at=int(time.time()) - 301)))
|
|
self.assertEqual(self.start()['result'], 3)
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_unverified_manifests_cannot_enable_installation(self):
|
|
for patch in [{'apk_sha256': None}, {'apk_sha256': 'bad'}, {'apk_size': 0}, {'apk_size': 9000000}, {'apk_size': '08'}]:
|
|
self.cache.unlink(missing_ok=True)
|
|
(self.directory / 'state/retry_at').unlink(missing_ok=True)
|
|
release = dict(self.release, **patch)
|
|
self.payload.write_text(json.dumps(release))
|
|
checked = self.call('check')
|
|
self.assertNotEqual(checked['result'], 0)
|
|
self.assertFalse(checked['can_upgrade'])
|
|
self.assertEqual(self.start()['result'], 3)
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_download_checksum_storage_and_package_manager_failures_are_retryable(self):
|
|
self.call('check')
|
|
for patch, error in [({'DOWNLOAD_FAIL': '1'}, 'network'), ({'FREE_KB': '10'}, 'space'),
|
|
({'INSTALL_FAIL': '1'}, 'install'), ({'WRONG_VERSION': '1'}, 'install')]:
|
|
with self.subTest(error=error):
|
|
old = self.env.copy()
|
|
self.env.update(patch)
|
|
self.assertEqual(self.start()['result'], 0)
|
|
result = self.finished()
|
|
self.assertEqual(result['upgrade']['phase'], 'failed')
|
|
self.assertEqual(result['upgrade']['error'], error, result)
|
|
self.env = old
|
|
(self.directory / 'package').write_bytes(b'wrong-package')
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertEqual(self.finished()['upgrade']['error'], 'checksum')
|
|
(self.directory / 'package').write_bytes(b'argon-package')
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertEqual(self.finished()['upgrade']['phase'], 'complete')
|
|
|
|
def test_one_worker_at_a_time_and_private_metadata_snapshot(self):
|
|
self.call('check')
|
|
self.env['DOWNLOAD_DELAY'] = '4'
|
|
start = time.monotonic()
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertLess(time.monotonic() - start, 3)
|
|
self.assertEqual(self.start()['result'], 4)
|
|
self.assertTrue(self.call('get')['upgrade']['active'])
|
|
cached = json.loads(self.cache.read_text())
|
|
cached['apk_sha256'] = '0' * 64
|
|
self.cache.write_text(json.dumps(cached))
|
|
self.assertEqual(self.finished()['upgrade']['phase'], 'complete')
|
|
mutations = (self.directory / 'mutations').read_text()
|
|
self.assertEqual(mutations.count('add'), 1)
|
|
|
|
def test_version_is_rechecked_after_download_before_mutation(self):
|
|
self.call('check')
|
|
self.env['DOWNLOAD_DELAY'] = '2'
|
|
self.assertEqual(self.start()['result'], 0)
|
|
(self.directory / 'installed').write_text('3.0.0-r2')
|
|
self.assertEqual(self.finished()['upgrade']['error'], 'version')
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_internal_worker_refuses_to_run_or_clean_up_outside_its_private_job(self):
|
|
result = subprocess.run(['sh', str(self.script), 'run', 'apk'], env=self.env,
|
|
capture_output=True, text=True)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertTrue(self.script.exists())
|
|
self.assertTrue(self.payload.exists())
|
|
self.assertFalse((self.directory / 'mutations').exists())
|
|
|
|
def test_interrupted_job_is_reported_without_a_stale_lock(self):
|
|
state = self.directory / 'state'
|
|
state.mkdir()
|
|
(state / 'status.json').write_text(json.dumps({'phase': 'installing', 'version': '3.0.0-r2', 'error': ''}))
|
|
self.assertEqual(self.call('status')['upgrade']['error'], 'interrupted')
|
|
self.call('check')
|
|
self.assertEqual(self.start()['result'], 0)
|
|
self.assertEqual(self.finished()['upgrade']['phase'], 'complete')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|