883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
144 lines
8.4 KiB
Python
144 lines
8.4 KiB
Python
#!/usr/bin/env python3
|
|
"""One mocked facade boundary over the actual registration callback source.
|
|
|
|
Original Lua helper semantics and Source snapshot lifecycle are owning evidence;
|
|
this checks exact marshalling, public-field isolation and explicit selection hold.
|
|
"""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
FIXTURE = r'''
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#define __declspec(x)
|
|
#define GW_REGISTRATION_TEST 1
|
|
#include "engine_registration.c"
|
|
int gw_registration_test_top(void*L){(void)L;return 0;}
|
|
void*gw_registration_test_slot(void*L,int index){(void)L;(void)index;return 0;}
|
|
void gw_registration_test_read_gate(void*L,void*slot,const void*context){(void)L;(void)slot;(void)context;}
|
|
|
|
enum {NUMBER=1,STRING=2,TABLE=3};
|
|
struct Table;
|
|
struct Value {int kind,number;size_t bytes;char string[300];struct Table*table;};
|
|
struct Field {char key[64];struct Value value;};
|
|
struct Table {int count;struct Field fields[96];};
|
|
struct Lua {int count,tables;struct Value stack[16];struct Table pool[80];};
|
|
static struct Lua lua,other_lua;
|
|
static int scenario,calls;
|
|
static struct Value*top(void*L){struct Lua*s=L;assert(s->count>0);return &s->stack[s->count-1];}
|
|
static struct Value*push(void*L){struct Lua*s=L;assert(s->count<16);return &s->stack[s->count++];}
|
|
void gw_registration_test_create(void*L,int array,int hash){
|
|
struct Lua*s=L;assert(array==0&&hash>=0&&hash<=80&&s->tables<80);
|
|
struct Value*v=push(L);*v=(struct Value){.kind=TABLE,.table=&s->pool[s->tables++]};
|
|
}
|
|
void gw_registration_test_integer(void*L,int value){*push(L)=(struct Value){.kind=NUMBER,.number=value};}
|
|
void gw_registration_test_string(void*L,const char*bytes,size_t length){
|
|
assert(length<300);struct Value*v=push(L);*v=(struct Value){.kind=STRING,.bytes=length};
|
|
memcpy(v->string,bytes,length);v->string[length]=0;
|
|
}
|
|
void gw_registration_test_set(void*L,int index){
|
|
struct Lua*s=L;assert(index==-3&&s->count>=3);
|
|
struct Value*t=&s->stack[s->count+index],*k=&s->stack[s->count-2],*v=top(L);
|
|
assert(t->kind==TABLE&&k->kind==STRING&&k->bytes<64&&t->table->count<96);
|
|
struct Field*f=&t->table->fields[t->table->count++];
|
|
memcpy(f->key,k->string,k->bytes+1);f->value=*v;s->count-=2;
|
|
}
|
|
static struct Value*field(struct Table*t,const char*key){
|
|
for(int i=0;i<t->count;++i)if(!strcmp(t->fields[i].key,key))return &t->fields[i].value;
|
|
assert(!"Missing field");return 0;
|
|
}
|
|
static struct Value*maybe_field(struct Table*t,const char*key){
|
|
for(int i=0;i<t->count;++i)if(!strcmp(t->fields[i].key,key))return &t->fields[i].value;
|
|
return 0;
|
|
}
|
|
static int32_t source_snapshot(struct gw_wire_account_ui_state*h,struct gw_native_realm*r,uint32_t capacity){
|
|
++calls;assert(capacity==64);
|
|
*h=(struct gw_wire_account_ui_state){.abi=1,.size=64,.generation=7,.endpoint_kind=1,
|
|
.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};
|
|
r[0]=(struct gw_native_realm){.type=1,.flags=2,.characters=3,.category=4,.wire_id=5,
|
|
.population=.1f,.name="Source realm \xc3\xa9",.address="203.0.113.2:8085"};
|
|
r[1]=(struct gw_native_realm){.type=6,.flags=7,.characters=8,.category=9,.wire_id=10,
|
|
.population=-0.0f,.name="Other realm",.address="203.0.113.3:8085"};
|
|
if(scenario==1){h->state=1;h->realm_count=0;h->copied_realms=0;h->genuine_discovery=0;h->server_proof_verified=0;h->error=-17;}
|
|
if(scenario==2)return -19;
|
|
if(scenario==3)h->size=65;
|
|
if(scenario==4)h->copied_realms=65;
|
|
if(scenario==5)memset(r[0].name,'X',sizeof(r[0].name));
|
|
if(scenario==6)memset(r[0].address,'X',sizeof(r[0].address));
|
|
if(scenario==7)h->generation=0x80000000u;
|
|
if(scenario==8)r[0].wire_id=0x80000000u;
|
|
return 0;
|
|
}
|
|
static void reset(void){memset(&lua,0,sizeof(lua));}
|
|
static int run(void){reset();return gw_registration_get_realm_snapshot(&lua);}
|
|
int main(void){
|
|
_Static_assert(sizeof(gw_registration_data)==56,"Existing prefix changed");
|
|
_Static_assert(sizeof(gw_registration_rows)/sizeof(gw_registration_rows[0])==32,"Append capacity");
|
|
gw_registration_data.lua_state=(u64)(uintptr_t)&lua;
|
|
assert(run()==1&&top(&lua)->kind==NUMBER&&top(&lua)->number<0&&calls==0);
|
|
gw_registration_realm_binding.module=0x1000;
|
|
gw_registration_realm_binding.snapshot=(u64)(uintptr_t)source_snapshot;
|
|
gw_registration_realm_binding.ready=1;
|
|
scenario=0;assert(run()==1&&lua.count==1&&top(&lua)->kind==TABLE&&calls==1);
|
|
struct Table*t=top(&lua)->table;assert(t->count==18);
|
|
assert(field(t,"state")->number==5&&field(t,"generation")->number==7);
|
|
assert(field(t,"selected_index")->number==-1&&field(t,"genuine_discovery")->number==1);
|
|
struct Table*r0=field(t,"r0")->table,*r1=field(t,"r1")->table;
|
|
assert(r0->count==8&&field(r0,"wire_id")->number==5);
|
|
assert(!strcmp(field(r0,"name")->string,"Source realm \xc3\xa9"));
|
|
assert(!strcmp(field(r0,"address")->string,"203.0.113.2:8085"));
|
|
assert((uint32_t)field(r0,"population_bits")->number==0x3dcccccdu);
|
|
assert((uint32_t)field(r1,"population_bits")->number==0x80000000u);
|
|
assert(!maybe_field(t,"username")&&!maybe_field(t,"password")&&!maybe_field(t,"password_path"));
|
|
scenario=1;assert(run()==1&&top(&lua)->kind==TABLE);t=top(&lua)->table;
|
|
assert(t->count==16&&field(t,"state")->number==1&&field(t,"error")->number==-17);
|
|
assert(field(t,"copied_realms")->number==0&&!maybe_field(t,"r0"));
|
|
for(scenario=2;scenario<=8;++scenario){assert(run()==1&&lua.count==1&&top(&lua)->kind==NUMBER&&top(&lua)->number<0);}
|
|
int old=calls;reset();assert(gw_registration_request_realm(&lua)==1&&top(&lua)->number==REALM_SELECT_HELD&&calls==old);
|
|
reset();assert(gw_registration_get_realm_snapshot(&other_lua)==0&&lua.count==0&&calls==old);
|
|
assert(gw_registration_request_realm(&other_lua)==0&&calls==old&&other_lua.count==0);
|
|
puts("PASS: actual snapshot facade/public fields/UTF8/float bits and held cases; no native Lua or selection call");
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
class RegistrationRealmBoundary(unittest.TestCase):
|
|
def test_actual_marshaller_and_explicit_holds(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
source = root / 'facade.c'
|
|
source.write_text(FIXTURE)
|
|
binary = root / 'facade'
|
|
compiled = subprocess.run(['cc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', '-Wno-missing-field-initializers',
|
|
'-I', str(ROOT / 'native'), str(source), '-o', str(binary)],
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
|
self.assertEqual(compiled.returncode, 0, compiled.stdout)
|
|
completed = subprocess.run([str(binary)], check=True, capture_output=True, text=True)
|
|
self.assertIn('PASS: actual snapshot facade', completed.stdout)
|
|
|
|
|
|
def test_profile5_snapshot_emits_binding_only_for_proven_discovery(self):
|
|
fixture = FIXTURE.replace('#define GW_REGISTRATION_TEST 1', '#define GW_REGISTRATION_TEST 1\n#define GW_SOURCE_ONLY_PROFILE 5')
|
|
fixture = fixture.replace('#include "engine_registration.c"', '#include "engine_registration.c"\nint gw_source_login_readiness_value(uint64_t module){(void)module;return 1;}')
|
|
fixture = fixture.replace('.auth_port=3724,', '.auth_port=3725,')
|
|
fixture = fixture.replace('assert(t->count==18);', 'assert(t->count==22);assert(field(t,"source_bound_profile")->number==5);assert(field(t,"source_bound_auth_port")->number==3725);assert(field(t,"source_bound_generation")->number==7);')
|
|
fixture = fixture.replace('assert(t->count==16&&field(t,"state")->number==1&&field(t,"error")->number==-17);', 'assert(t->count==20&&field(t,"state")->number==1&&field(t,"error")->number==-17);assert(field(t,"source_bound_auth_port")->number==0&&field(t,"source_bound_generation")->number==0);')
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root=Path(temporary);source=root/'profile5-facade.c';source.write_text(fixture);binary=root/'facade'
|
|
compiled=subprocess.run(['cc','-std=c11','-O2','-Wall','-Wextra','-Werror','-Wno-missing-field-initializers','-I',str(ROOT/'native'),str(source),'-o',str(binary)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True)
|
|
self.assertEqual(compiled.returncode,0,compiled.stdout)
|
|
result=subprocess.run([str(binary)],capture_output=True,text=True)
|
|
self.assertEqual(result.returncode,0,result.stderr)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|