123 lines
6.9 KiB
Python
123 lines
6.9 KiB
Python
"""Trusted selection uses actual frozen assets; no generation, device or gateway."""
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import zipfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
spec = importlib.util.spec_from_file_location('character_assets', ROOT/'scripts/select-character-assets.py')
|
|
select = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(select)
|
|
ASSETS = ROOT/'app/src/main/assets'
|
|
|
|
|
|
class CharacterProfilesTests(unittest.TestCase):
|
|
def full(self, profile='main', **changes):
|
|
flags = dict(emotion_sha=select.catalog()['mediaBundles']['black-correct03']['indexSha256'],
|
|
user_assets=True, mouth=True, startup=True)
|
|
flags.update(changes)
|
|
return select.selection(ASSETS, profile, **flags)
|
|
|
|
def test_identities_are_distinct_media_is_identical(self):
|
|
results=[self.full(p) for p in ('main','lvbu-test','liam-test')]
|
|
for row in results:
|
|
self.assertEqual(results[0]['files'],row['files'])
|
|
self.assertEqual((row['file_count'],row['media_identity']),(4051,'a01'))
|
|
self.assertEqual([r['persona'] for r in results],['default','lvbu','liam'])
|
|
self.assertEqual(len({r['application_id'] for r in results}),3)
|
|
self.assertEqual(results[2]['application_id'],'com.gorios.lanvoice.liamtest')
|
|
self.assertNotIn('voice_id', json.dumps(select.catalog()))
|
|
|
|
def test_complete_inventory_is_exact_code33(self):
|
|
selected = self.full()['files']
|
|
with zipfile.ZipFile(ROOT/'artifacts/lan-voice-v0714-black-tulle-correct03-code33-arm64-debug.apk') as z:
|
|
actual = {n[7:]: {'sha256': select.digest(z.read(n)), 'bytes': z.getinfo(n).file_size}
|
|
for n in z.namelist() if n.startswith('assets/') and not n.endswith('/')}
|
|
self.assertEqual(selected, actual)
|
|
self.assertEqual(sum(n.startswith('avatar/a01-neutral-graph-v1/') for n in selected), 593)
|
|
self.assertNotIn('avatar/a01-expression-action-v1/manifest.json', selected)
|
|
|
|
def test_default_legacy_flags_preserve_108_files(self):
|
|
selected = select.selection(ASSETS)
|
|
self.assertEqual(selected['file_count'], 108)
|
|
self.assertFalse(any('runtime-04/' in n for n in selected['files']))
|
|
self.assertFalse(any('startup-' in n or 'black-tulle' in n for n in selected['files']))
|
|
|
|
def test_unlisted_source_files_do_not_enter_selection(self):
|
|
import shutil
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
copied = Path(folder)/'assets'
|
|
shutil.copytree(ASSETS, copied)
|
|
(copied/'unapproved').mkdir()
|
|
(copied/'unapproved/wrong-character.png').write_bytes(b'not media')
|
|
self.assertEqual(select.selection(copied)['files'], select.selection(ASSETS)['files'])
|
|
|
|
def test_wrong_persona_flags_or_pin_are_rejected(self):
|
|
cases=[('unknown',{}),('main',{'emotion_sha':'0'*64})]
|
|
cases += [(p,{flag:False}) for p in ('lvbu-test','liam-test') for flag in ('startup','mouth','user_assets')]
|
|
for profile, changes in cases:
|
|
with self.subTest(profile=profile, changes=changes), self.assertRaises(ValueError):
|
|
self.full(profile, **changes)
|
|
|
|
def test_index_bootstrap_and_poster_corruption_rejected(self):
|
|
media = select.catalog()['mediaBundles']['black-correct03']
|
|
targets = [ASSETS/media['assetRoot']/'manifest.json',
|
|
ASSETS/media['bootstrapRoot']/'manifest.json',
|
|
ROOT/'app/src/main/res/drawable-nodpi'/ (media['posterResource']+'.png')]
|
|
original = Path.read_bytes
|
|
for target in targets:
|
|
def corrupted(p):
|
|
return b'corrupted' if p == target else original(p)
|
|
with self.subTest(target=target.name), patch.object(Path, 'read_bytes', corrupted), self.assertRaises(ValueError):
|
|
self.full()
|
|
|
|
def test_selected_symlink_and_escape_rejected(self):
|
|
original = Path.is_symlink
|
|
target = ASSETS/'face_landmarker.task'
|
|
with patch.object(Path, 'is_symlink', lambda p: p == target or original(p)), self.assertRaises(ValueError):
|
|
self.full()
|
|
for name in ('../x', '/x', 'x/../y', 'x//y', 'x\\y'):
|
|
with self.subTest(name=name), self.assertRaises(ValueError):
|
|
select.relative(name)
|
|
|
|
def test_staging_cannot_overwrite_other_profile_or_build_root(self):
|
|
for profile,other in [('lvbu-test','liam-test'),('liam-test','lvbu-test')]:
|
|
result=self.full(profile)
|
|
for dest in (ROOT/'app/build',ROOT/'app/build/generated/packagedAssets',ASSETS,
|
|
ROOT/'app/build/characters'/other/'generated/packagedAssets'):
|
|
with self.subTest(profile=profile,dest=dest),self.assertRaises(ValueError):
|
|
select.stage(ASSETS,dest,result)
|
|
|
|
def test_backup_lifecycle_component_contracts(self):
|
|
profiles = select.catalog()['profiles']
|
|
self.assertTrue(profiles['main']['allowBackup'])
|
|
self.assertTrue(profiles['main']['permissionConnectionRetention'])
|
|
for profile in ('lvbu-test','liam-test'):
|
|
self.assertFalse(profiles[profile]['allowBackup'])
|
|
self.assertFalse(profiles[profile]['permissionConnectionRetention'])
|
|
text = (ROOT/'app/src/main/AndroidManifest.xml').read_text()
|
|
self.assertIn('android:allowBackup="${characterAllowBackup}"', text)
|
|
self.assertIn('android:name="com.gorios.lanvoice.MainActivity"', text)
|
|
self.assertIn('android:name="com.gorios.lanvoice.CameraDiagnosticsActivity"', text)
|
|
self.assertNotIn('android:name=".MainActivity"', text)
|
|
build = (ROOT/'app/build.gradle').read_text()
|
|
self.assertIn("build/characters/", build)
|
|
self.assertIn("applicationId characterProfile.applicationId", build)
|
|
activity=(ROOT/'app/src/main/java/com/gorios/lanvoice/MainActivity.kt').read_text()
|
|
pause=activity[activity.index("override fun onPause()"):activity.index("override fun onStop()")]
|
|
self.assertLess(pause.index("if(releaseSessionFirst"),pause.index("closeCamera()"))
|
|
self.assertIn("if(!releaseSessionFirst",pause)
|
|
|
|
def test_artifact_names_and_build_directories_are_isolated(self):
|
|
spec=importlib.util.spec_from_file_location('character_pack',ROOT/'scripts/package-character-test.py')
|
|
pack=importlib.util.module_from_spec(spec);spec.loader.exec_module(pack)
|
|
self.assertEqual(pack.artifact_name('lvbu-test',34),'lan-voice-v0714-lvbu-test-code34-arm64-debug.apk')
|
|
self.assertEqual(pack.artifact_name('liam-test',35),'lan-voice-v0714-liam-test-code35-arm64-debug.apk')
|
|
self.assertNotEqual(pack.build_dir('liam-test'),pack.build_dir('lvbu-test'))
|
|
self.assertIn('-PcharacterProfile=liam-test',pack.flags('liam-test',35))
|
|
self.assertEqual(select.catalog()['profiles']['liam-test']['minimumVersionCode'],35)
|
|
with self.assertRaises(ValueError):pack.artifact_name('main',35)
|