883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
101 lines
8.9 KiB
Python
101 lines
8.9 KiB
Python
"""Pure realm discovery/preference tests: no client, network, Wine or account IO."""
|
|
from pathlib import Path
|
|
from copy import deepcopy
|
|
from unittest.mock import Mock
|
|
import json,os,sys,tempfile,unittest
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import launcher_realm_preference as memory
|
|
import forever_launcher_backend as backend
|
|
from native_manual_login import require_realm
|
|
|
|
def account(order=("C'Thun","N'Zoth","Y'Shaarj"),**changes):
|
|
addresses={"C'Thun":'8.8.8.8:8085',"N'Zoth":'8.8.4.4:8085',"Y'Shaarj":'1.1.1.1:8085'}
|
|
rows=[dict(index=i,name=n,address=addresses[n],wire_id=0,flags=0,characters={'C\'Thun':2,'N\'Zoth':4,'Y\'Shaarj':0}[n])for i,n in enumerate(order)]
|
|
value=dict(state=5,error=0,auth_result=0,discovery_result=0,auth_port=3724,server_proof_verified=1,realm_count=len(rows),realms=rows,transport_prepared=False,selected_index=-1)
|
|
value.update(changes);return value
|
|
class Controller:
|
|
def __init__(self,fresh):self.fresh=fresh;self.calls=[]
|
|
def rpc(self,name,**kw):
|
|
self.calls.append((name,kw))
|
|
if name=='inspect-wire-account':return self.fresh
|
|
if name=='select-native-realm':return {'accepted':True}
|
|
raise AssertionError('Unexpected client action')
|
|
class RealmMemoryTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup);self.root=Path(self.tmp.name);self.path=self.root/'last-realm.json';self.row=memory.identity('octo',account()['realms'][1])
|
|
def save(self):self.assertTrue(memory.save(self.path,self.row))
|
|
def choose(self,a):return memory.matching_index(a,memory.read(self.path),'octo',require_realm)
|
|
def test_atomic_public_only_mode600_and_successful_replacement(self):
|
|
self.save();self.assertEqual(memory.read(self.path),self.row);self.assertEqual(self.path.stat().st_mode&0o777,0o600)
|
|
value=json.loads(self.path.read_text());self.assertEqual(set(value['identity']),{'profile','name','address','wire_id'});self.assertNotIn('index',value['identity']);self.assertNotIn('characters',value['identity'])
|
|
other=memory.identity('octo',account()['realms'][0]);self.assertTrue(memory.save(self.path,other));self.assertEqual(memory.read(self.path),other);self.assertEqual([p.name for p in self.root.iterdir()],['last-realm.json'])
|
|
def test_all_zero_wire_ids_resolve_name_address_not_index_or_count(self):
|
|
self.save();self.assertEqual(self.choose(account()),1);self.assertEqual(self.choose(account(("N'Zoth","Y'Shaarj","C'Thun"))),0)
|
|
def test_changed_address_wire_id_or_profile_prompts(self):
|
|
self.save()
|
|
for key,value in(('address','1.0.0.1:8085'),('wire_id',1)):
|
|
a=account();a['realms'][1][key]=value;self.assertIsNone(self.choose(a))
|
|
self.assertIsNone(memory.matching_index(account(),self.row,'local-explicit',require_realm))
|
|
def test_offline_or_duplicate_online_identity_prompts(self):
|
|
self.save();a=account();a['realms'][1]['flags']=2;self.assertIsNone(self.choose(a))
|
|
a=account();a['realms'].append(dict(a['realms'][1],index=3));a['realm_count']=4;self.assertIsNone(self.choose(a))
|
|
def test_unverified_account_does_not_auto_select(self):
|
|
self.save();self.assertIsNone(self.choose(account(server_proof_verified=0)))
|
|
def test_missing_malformed_duplicate_or_overlong_preference_prompts(self):
|
|
self.assertIsNone(memory.read(self.path))
|
|
for raw in(b'broken',b'{"schema_version":1,"schema_version":1}',b'x'*4097,b'\xff'):
|
|
self.path.write_bytes(raw);self.path.chmod(0o600);self.assertIsNone(memory.read(self.path))
|
|
def test_symlink_target_and_parent_not_read_or_overwritten(self):
|
|
target=self.root/'outside';target.write_text('PRESERVE');target.chmod(0o600);self.path.symlink_to(target)
|
|
self.assertIsNone(memory.read(self.path));self.assertFalse(memory.save(self.path,self.row));self.assertEqual(target.read_text(),'PRESERVE');self.assertTrue(self.path.is_symlink())
|
|
self.path.unlink();self.save();parent=self.root/'linked';parent.symlink_to(self.root,target_is_directory=True)
|
|
self.assertIsNone(memory.read(parent/self.path.name));self.assertFalse(memory.save(parent/self.path.name,self.row))
|
|
def test_insecure_or_oversized_existing_file_is_preserved(self):
|
|
self.path.write_bytes(b'x'*4097);self.path.chmod(0o600);self.assertFalse(memory.save(self.path,self.row));self.assertEqual(self.path.stat().st_size,4097)
|
|
self.path.write_text('PRESERVE');self.path.chmod(0o644);self.assertIsNone(memory.read(self.path));self.assertFalse(memory.save(self.path,self.row));self.assertEqual(self.path.read_text(),'PRESERVE')
|
|
def test_credential_or_unknown_keys_cannot_enter_store(self):
|
|
self.assertFalse(memory.save(self.path,dict(self.row,password='MUST-NOT-WRITE')));self.assertFalse(self.path.exists())
|
|
for bad in(True,-1,256):
|
|
with self.assertRaises(ValueError):memory.identity('octo',dict(self.row,wire_id=bad))
|
|
def test_remembered_choice_is_freshly_validated_immediately_before_rpc(self):
|
|
self.save();initial=account();fresh=account(("N'Zoth","C'Thun","Y'Shaarj"));controller=Controller(fresh);events=[]
|
|
def validator(a,i,p):events.append(('validate',a is fresh,i));return require_realm(a,i,p)
|
|
def rpc(name,**kw):events.append(('rpc',name,kw));return Controller.rpc(controller,name,**kw)
|
|
controller.rpc=rpc;chooser=Mock(side_effect=AssertionError('Must not prompt'))
|
|
self.assertEqual(backend.select_pending_realm(controller,initial,'octo',preference_path=self.path,validator=validator,chooser=chooser),0)
|
|
self.assertEqual(events[-2],('validate',True,0));self.assertEqual(events[-1],('rpc','select-native-realm',{'index':0}));chooser.assert_not_called();self.assertEqual([x[0]for x in controller.calls],['inspect-wire-account','select-native-realm'])
|
|
def test_missing_or_stale_memory_uses_existing_chooser(self):
|
|
for stale in(False,True):
|
|
if stale:self.save();a=account();a['realms'][1]['address']='1.0.0.1:8085'
|
|
else:a=account()
|
|
controller=Controller(a);chooser=Mock(return_value=2)
|
|
self.assertEqual(backend.select_pending_realm(controller,a,'octo',preference_path=self.path,chooser=chooser),2);chooser.assert_called_once_with(a);self.assertFalse(any('auth'in x[0]for x in controller.calls))
|
|
def test_cancel_makes_no_selection_or_preference_write(self):
|
|
controller=Controller(account());chooser=Mock(side_effect=RuntimeError('realm-choice-cancelled'))
|
|
with self.assertRaisesRegex(RuntimeError,'cancelled'):backend.select_pending_realm(controller,account(),'octo',preference_path=self.path,chooser=chooser)
|
|
self.assertEqual(controller.calls,[]);self.assertFalse(self.path.exists())
|
|
def test_changed_fresh_discovery_or_expired_transport_rejects_before_selection(self):
|
|
for fresh in(account(state=2),account(transport_prepared=True),account(server_proof_verified=0)):
|
|
controller=Controller(fresh)
|
|
with self.assertRaises(ValueError):backend.select_pending_realm(controller,account(),'octo',preference_path=self.path,chooser=lambda _:1)
|
|
self.assertEqual([n for n,_ in controller.calls],['inspect-wire-account'])
|
|
fresh=account();fresh['realms'][1]['address']='1.0.0.1:8085';controller=Controller(fresh)
|
|
with self.assertRaisesRegex(ValueError,'discovery changed'):backend.select_pending_realm(controller,account(),'octo',preference_path=self.path,chooser=lambda _:1)
|
|
self.assertEqual([n for n,_ in controller.calls],['inspect-wire-account'])
|
|
def test_invalid_or_offline_user_index_rejected(self):
|
|
for index in(-1,3,True):
|
|
controller=Controller(account())
|
|
with self.assertRaises(ValueError):backend.select_pending_realm(controller,account(),'octo',preference_path=self.path,chooser=lambda _,i=index:i)
|
|
self.assertEqual(controller.calls,[])
|
|
def test_remember_only_after_verified_character_selection(self):
|
|
a=account(state=2,transport_prepared=True,selected_index=1)
|
|
for report in({},dict(actual_character_selection_verified=False),dict(actual_character_selection_verified=1)):
|
|
self.assertFalse(backend.remember_verified_realm(a,'octo',report,preference_path=self.path));self.assertFalse(self.path.exists())
|
|
report=dict(actual_character_selection_verified=True)
|
|
self.assertFalse(backend.remember_verified_realm(account(selected_index=1),'octo',report,preference_path=self.path));self.assertFalse(self.path.exists())
|
|
self.assertTrue(backend.remember_verified_realm(a,'octo',report,preference_path=self.path));self.assertEqual(memory.read(self.path),self.row)
|
|
def test_failed_auth_cannot_remember_despite_stale_success_report(self):
|
|
a=account(state=2,transport_prepared=True,selected_index=1,server_proof_verified=0)
|
|
self.assertFalse(backend.remember_verified_realm(a,'octo',dict(actual_character_selection_verified=True),preference_path=self.path));self.assertFalse(self.path.exists())
|
|
if __name__=='__main__':unittest.main()
|