883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
138 lines
8.7 KiB
Python
138 lines
8.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Private metadata retention only; no client, RPC, packet bytes or networking."""
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
from launcher_trace_journal import TraceJournal
|
|
|
|
REV='a'*40
|
|
|
|
def snapshot(count,rows=None,**fields):
|
|
if rows is None:rows=[dict(direction=1,opcode=0xa9,bytes=120)for _ in range(min(count,32))]
|
|
return dict(error=0,closed=0,world_auth=1,world=dict(trace_count=count,trace=rows,**fields))
|
|
|
|
class JournalTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp=tempfile.TemporaryDirectory();self.addCleanup(self.temp.cleanup)
|
|
self.root=Path(self.temp.name);(self.root/'runtime').mkdir()
|
|
self.journal=None
|
|
def start(self,**kwargs):
|
|
self.journal=TraceJournal(self.root/'runtime',REV,123,**kwargs)
|
|
self.addCleanup(self.journal.close);return self.journal
|
|
def rows(self):
|
|
return [json.loads(line)for p in sorted((self.root/'runtime/launcher-traces').glob('*.jsonl'))for line in p.read_text().splitlines()]
|
|
def test_only_numeric_whitelist_and_build_identity_are_persisted(self):
|
|
j=self.start();p=snapshot(1,[dict(direction=3,opcode=0x5e0000,bytes=321,payload='PRIVATE-CANARY',account='PRIVATE-ACCOUNT')])
|
|
p.update(credentials='PRIVATE-CREDENTIAL',chat='PRIVATE-MESSAGE');self.assertTrue(j.sample(p))
|
|
rows=self.rows();raw=json.dumps(rows)
|
|
self.assertNotIn('PRIVATE-',raw)
|
|
packet=next(r for r in rows if r['event']=='packet_metadata')
|
|
self.assertEqual(set(packet),{'event','epoch','sequence','direction','opcode','bytes','observed_monotonic_ns'})
|
|
self.assertEqual((packet['sequence'],packet['direction'],packet['opcode'],packet['bytes']),(0,3,0x5e0000,321))
|
|
self.assertEqual(rows[0]['build_revision'],REV)
|
|
def test_complete_numeric_drain_failure_is_retained_with_signed_result(self):
|
|
j=self.start();p=snapshot(0,[])
|
|
failure=dict(phase=1,result=-6,source_opcode=0x1f6,source_bytes=11635,
|
|
modern_opcode=0,modern_bytes=0,required=0,capacity=1048576,count=0,index=0)
|
|
p['world_failure']=dict(failure,payload='PRIVATE-PAYLOAD',account='PRIVATE-ACCOUNT')
|
|
j.sample(p);j.sample(p)
|
|
status=[r for r in self.rows()if r['event']=='status']
|
|
self.assertEqual(len(status),1)
|
|
self.assertEqual({k.removeprefix('world_failure_'):v for k,v in status[0].items()
|
|
if k.startswith('world_failure_')},failure)
|
|
self.assertNotIn('PRIVATE-',json.dumps(self.rows()))
|
|
p['world_failure']=dict(failure,phase=5,result=-2,modern_opcode=0x5e0000,modern_bytes=800)
|
|
j.sample(p)
|
|
status=[r for r in self.rows()if r['event']=='status']
|
|
self.assertEqual(len(status),2)
|
|
self.assertEqual(status[-1]['world_failure_phase'],5)
|
|
self.assertEqual(status[-1]['world_failure_modern_opcode'],0x5e0000)
|
|
def test_partial_or_invalid_failure_diagnostic_is_not_retained(self):
|
|
j=self.start();valid=dict(phase=1,result=-6,source_opcode=0x1f6,source_bytes=11635,
|
|
modern_opcode=0,modern_bytes=0,required=0,capacity=1048576,count=0,index=0)
|
|
cases=[None,{},dict(valid,phase=12),dict(valid,result=-0x80000001),
|
|
dict(valid,result=0x80000000),dict(valid,result=True),dict(valid,index=-1),
|
|
dict(valid,modern_bytes='PRIVATE')]
|
|
for failure in cases:
|
|
p=snapshot(0,[]);p['world_failure']=failure;self.assertTrue(j.sample(p))
|
|
self.assertFalse(any(k.startswith('world_failure_')for r in self.rows()for k in r))
|
|
def test_gaps_duplicates_and_empty_snapshots_are_honest(self):
|
|
j=self.start();self.assertTrue(j.sample(snapshot(0,[])))
|
|
j.sample(snapshot(5,[dict(direction=1,opcode=7,bytes=8)]))
|
|
j.sample(snapshot(5,[dict(direction=1,opcode=7,bytes=8)]))
|
|
self.assertEqual(j.lost,4);self.assertEqual(j.sampled,1)
|
|
j.sample(snapshot(40));self.assertEqual(j.lost,7);self.assertEqual(j.sampled,33)
|
|
self.assertEqual(sum(r.get('missing_rows',0)for r in self.rows()),7)
|
|
def test_counter_reset_starts_new_epoch_without_claiming_continuity(self):
|
|
j=self.start();j.sample(snapshot(3));j.sample(snapshot(1))
|
|
self.assertEqual(j.epoch,1)
|
|
self.assertTrue(any(r['event']=='counter_reset'and r['previous_count']==3 for r in self.rows()))
|
|
self.assertEqual(len([r for r in self.rows()if r['event']=='packet_metadata']),4)
|
|
def test_malformed_snapshots_and_rows_are_bounded_not_payloads(self):
|
|
j=self.start()
|
|
for p in [None,{},dict(world=[]),dict(world={'trace_count':True,'trace':[]}),snapshot(1,[{}]*33),snapshot(1,[{},{}])]:
|
|
self.assertFalse(j.sample(p));self.assertTrue(j.active)
|
|
self.assertTrue(j.sample(snapshot(2,[{'direction':True,'opcode':0,'bytes':0,'payload':'DO-NOT-STORE'},dict(direction=2,opcode=1,bytes=2)])))
|
|
self.assertEqual(j.invalid_rows,1);self.assertEqual(j.sampled,1)
|
|
self.assertNotIn('DO-NOT-STORE',json.dumps(self.rows()))
|
|
def test_owned_private_modes_and_immediate_visibility(self):
|
|
j=self.start();j.sample(snapshot(1))
|
|
folder=self.root/'runtime/launcher-traces'
|
|
self.assertEqual(stat.S_IMODE(folder.stat().st_mode),0o700)
|
|
for p in folder.iterdir():self.assertEqual(stat.S_IMODE(p.stat().st_mode),0o600)
|
|
self.assertTrue(any(r['event']=='packet_metadata'for r in self.rows()))
|
|
def test_rotation_stays_bounded_across_samples_and_same_build_sessions(self):
|
|
j=self.start(max_bytes=4096,keep_files=3)
|
|
for count in range(32,513,32):j.sample(snapshot(count))
|
|
j.close();j=self.start(max_bytes=4096,keep_files=3);j.sample(snapshot(1));j.close()
|
|
files=list((self.root/'runtime/launcher-traces').glob('*.jsonl'))
|
|
self.assertEqual(len(files),3)
|
|
self.assertTrue(all(p.stat().st_size<=4096 for p in files))
|
|
headers=[json.loads(p.read_text().splitlines()[0])for p in files]
|
|
self.assertTrue(all(h['build_revision']==REV and h['event']=='segment'for h in headers))
|
|
def test_unsafe_directory_and_symlinks_are_refused(self):
|
|
folder=self.root/'runtime/launcher-traces';folder.mkdir(mode=0o755)
|
|
with self.assertRaises(ValueError):self.start()
|
|
folder.rmdir();other=self.root/'other';other.mkdir();folder.symlink_to(other,target_is_directory=True)
|
|
with self.assertRaises(OSError):self.start()
|
|
def test_unsafe_existing_segment_is_preserved(self):
|
|
folder=self.root/'runtime/launcher-traces';folder.mkdir(mode=0o700)
|
|
external=self.root/'external';external.write_text('PRESERVE');(folder/('trace-'+REV+'.0.jsonl')).symlink_to(external)
|
|
with self.assertRaises(ValueError):self.start()
|
|
self.assertEqual(external.read_text(),'PRESERVE')
|
|
def test_hardlinked_segment_is_refused(self):
|
|
folder=self.root/'runtime/launcher-traces';folder.mkdir(mode=0o700)
|
|
external=self.root/'external';external.write_text('PRESERVE');external.chmod(0o600)
|
|
os.link(external,folder/('trace-'+REV+'.0.jsonl'))
|
|
with self.assertRaises(ValueError):self.start()
|
|
self.assertEqual(external.read_text(),'PRESERVE')
|
|
def test_write_failure_disables_capture_without_raising_into_gameplay(self):
|
|
j=self.start()
|
|
with patch('launcher_trace_journal.os.write',side_effect=OSError('private detail')):
|
|
self.assertFalse(j.sample(snapshot(1)))
|
|
self.assertFalse(j.active);self.assertFalse(j.sample(snapshot(2)));j.close()
|
|
def test_release_factory_missing_or_malformed_is_fail_soft(self):
|
|
log=io.StringIO();j=TraceJournal.from_release(self.root,123,log=log)
|
|
self.assertFalse(j.active);self.assertFalse(j.sample(snapshot(1)));j.close()
|
|
(self.root/'linux-release.json').write_text(json.dumps(dict(kind='linux-forever-client-folder-package-v1',source_git_revision='not-exact')))
|
|
self.assertFalse(TraceJournal.from_release(self.root,123).active)
|
|
self.assertNotIn(str(self.root),log.getvalue())
|
|
def test_release_factory_pins_actual_manifest_and_revision(self):
|
|
import hashlib
|
|
raw=json.dumps(dict(kind='linux-forever-client-folder-package-v1',source_git_revision=REV)).encode()
|
|
(self.root/'linux-release.json').write_bytes(raw)
|
|
j=TraceJournal.from_release(self.root,123);self.addCleanup(j.close);self.assertTrue(j.active)
|
|
self.assertEqual(self.rows()[0]['release_manifest_sha256'],hashlib.sha256(raw).hexdigest())
|
|
def test_invalid_configuration_is_rejected(self):
|
|
for changes in [dict(max_bytes=4095),dict(max_bytes=True),dict(keep_files=0),dict(keep_files=9)]:
|
|
with self.assertRaises(ValueError):self.start(**changes)
|
|
|
|
if __name__=='__main__':unittest.main()
|