104 lines
4.8 KiB
Python
104 lines
4.8 KiB
Python
"""Remembered Source realm is resolved through current authenticated discovery."""
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
LUA = r'''
|
|
local makeProvider = assert(loadfile(arg[1]))();
|
|
local makeAdapter = assert(loadfile(arg[2]))();
|
|
local saved, writes = nil, 0;
|
|
local preference = {
|
|
Read = function() return saved; end,
|
|
Write = function(value) saved = value; writes = writes + 1; end,
|
|
};
|
|
local function row(name, address, wire, flags)
|
|
return {name=name,address=address,wire_id=wire,flags=flags or 0,
|
|
type=0,characters=1,category=0,population_text="1"};
|
|
end
|
|
local function snapshot(first, second, generation)
|
|
return {abi=1,size=64,generation=generation,endpoint_kind=0,state=5,
|
|
error=0,auth_result=0,discovery_result=0,server_proof_verified=1,
|
|
realm_count=2,selected_index=-1,transport_prepared=0,cancel=0,
|
|
auth_port=3724,copied_realms=2,genuine_discovery=1,r0=first,r1=second};
|
|
end
|
|
local wanted = row("Octo PvE", "play.octowow.st:8085", 5);
|
|
local other = row("Other", "play.octowow.st:8086", 6);
|
|
local current = snapshot(other, wanted, 11);
|
|
local requests, result = {}, 0;
|
|
local backend = {
|
|
GetSnapshot = function() return current; end,
|
|
RequestRealm = function(index, generation)
|
|
requests[#requests+1] = {index, generation}; return result;
|
|
end,
|
|
};
|
|
local native = {ConnectToRealm=function() error("Native route used"); end};
|
|
local function session()
|
|
return makeAdapter(makeProvider(backend), native, true, preference);
|
|
end
|
|
|
|
-- First launch: no preference means the real user selects one realm.
|
|
local first = session();
|
|
assert(first.Refresh() and first.CanSelect() and #requests == 0);
|
|
assert(first.ConnectToRealm(1) and #requests == 1 and requests[1][1] == 1);
|
|
assert(writes == 1 and type(saved) == "string" and saved:find("v1|",1,true) == 1);
|
|
assert(not first.ConnectToRealm(1) and #requests == 1);
|
|
|
|
-- The server may reorder the list: use the matching current row, not index 1.
|
|
current = snapshot(wanted, other, 12);
|
|
local nextSession = session();
|
|
assert(nextSession.Refresh() and #requests == 2 and requests[2][1] == 0
|
|
and requests[2][2] == 12 and not nextSession.CanSelect());
|
|
nextSession.Refresh(); assert(#requests == 2 and writes == 1);
|
|
|
|
-- A changed address or wire ID cannot inherit a choice merely by name.
|
|
current = snapshot(row("Octo PvE", "play.octowow.st:8087", 5), other, 13);
|
|
local changed = session(); assert(changed.Refresh() and changed.CanSelect() and #requests == 2);
|
|
current = snapshot(row("Octo PvE", "play.octowow.st:8085", 7), other, 14);
|
|
local wireChanged = session(); assert(wireChanged.Refresh() and wireChanged.CanSelect() and #requests == 2);
|
|
|
|
-- Offline or ambiguous matches remain an explicit choice; malformed memory is inert.
|
|
current = snapshot(row("Octo PvE", "play.octowow.st:8085", 5, 2), other, 15);
|
|
local offline = session(); assert(offline.Refresh() and offline.CanSelect() and #requests == 2);
|
|
current = snapshot(wanted, wanted, 16);
|
|
local ambiguous = session(); assert(ambiguous.Refresh() and ambiguous.CanSelect() and #requests == 2);
|
|
saved = "invalid";
|
|
current = snapshot(wanted, other, 17);
|
|
local malformed = session(); assert(malformed.Refresh() and malformed.CanSelect() and #requests == 2);
|
|
|
|
-- A refused request neither persists nor loops on Refresh; manual choice remains available.
|
|
saved = nil; result = -2;
|
|
local rejected = session(); assert(rejected.Refresh() and rejected.CanSelect());
|
|
assert(not rejected.ConnectToRealm(0) and writes == 1 and #requests == 3);
|
|
rejected.Refresh(); assert(#requests == 3 and rejected.CanSelect());
|
|
|
|
-- Disabled source mode does not read or write the remembered value.
|
|
local disabledNative = {GetRealmInfo=function() return {name="Native"}; end};
|
|
local disabled = makeAdapter(makeProvider(backend), disabledNative, false, preference);
|
|
assert(disabled.Refresh() == false and disabled.GetRealmInfo(0).name == "Native");
|
|
print("PASS current-row realm memory, reorder, stale/offline/ambiguous hold");
|
|
'''
|
|
|
|
|
|
class SourceRealmPreference(unittest.TestCase):
|
|
def test_authenticated_current_row_match(self):
|
|
lua = shutil.which("lua") or shutil.which("lua5.4")
|
|
self.assertIsNotNone(lua, "Lua interpreter required")
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
fixture = Path(temporary) / "realm-preference.lua"
|
|
fixture.write_text(LUA)
|
|
result = subprocess.run(
|
|
[lua, str(fixture), str(ROOT / "compat/glue/source_realm_provider.lua"),
|
|
str(ROOT / "compat/glue/source_realm_list_adapter.lua")],
|
|
text=True, capture_output=True)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
self.assertIn("PASS current-row realm memory", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|