Integrate 0.9.8 NDS read-only lab, repair upstream hook and simplify setup guidance
This commit is contained in:
92
tests/test_nds_wire.py
Normal file
92
tests/test_nds_wire.py
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent packet fixtures based on ncpfs 2.2.6 read/list/search decoders."""
|
||||
import ctypes as C, json, os, pathlib, random, sqlite3, struct, sys, tempfile
|
||||
from contextlib import closing
|
||||
root=pathlib.Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0,str(root/'nds/tools'))
|
||||
import snapshot
|
||||
END=0xffffffff
|
||||
le=lambda *v:struct.pack('<'+'I'*len(v),*v)
|
||||
be=lambda i:struct.pack('>I',i)
|
||||
def string(s):
|
||||
b=(s+'\0').encode('utf-16le');return le(len(b))+b+b'\0'*((-len(b))%4)
|
||||
class Reader:
|
||||
def __init__(self,b):self.b=b;self.i=0
|
||||
def take(self,n):
|
||||
assert self.i+n<=len(self.b);v=self.b[self.i:self.i+n];self.i+=n;return v
|
||||
def u32(self):return struct.unpack('<I',self.take(4))[0]
|
||||
def oid(self):return struct.unpack('>I',self.take(4))[0]
|
||||
def string(self):
|
||||
n=self.u32();s=self.take(n);assert self.take((-n)%4)==b'\0'*((-n)%4)
|
||||
assert n>=2 and n%2==0 and s[-2:]==b'\0\0';return s[:-2].decode('utf-16le')
|
||||
def end(self):assert self.i==len(self.b)
|
||||
def dsi(self):
|
||||
mask=self.u32();d={}
|
||||
for bit,k,read in [(2,'id',self.oid),(8,'children',self.u32),(0x100,'parent',self.oid),(0x800,'class',self.string),(0x1000,'rdn',self.string),(0x2000,'dn',self.string)]:
|
||||
if mask&bit:d[k]=read()
|
||||
assert not mask&~0x390b;return d
|
||||
def attrs(self,info):
|
||||
a={}
|
||||
for _ in range(self.u32()):
|
||||
if not info:a[self.string()]=None;continue
|
||||
syntax=self.u32();name=self.string();assert self.u32()==1
|
||||
if syntax==7:
|
||||
assert self.u32()==1;v=self.take(4);assert v in (b'\0'*4,b'\1\0\0\0');value=bool(v[0])
|
||||
else:assert syntax in (1,3,20);value=self.string()
|
||||
a[name]=(syntax,value)
|
||||
return a
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
path=pathlib.Path(td)/'directory.sqlite3'
|
||||
data={'tree':'MARS_TREE','organization':'HOME','units':['SERVERS','USERS'],'placements':[{'name':'alice','type':1,'unit':'USERS'}]}
|
||||
cfg=snapshot.publish(data,path,True,('MARS',['SYS','DATA']))
|
||||
with closing(sqlite3.connect(path)) as db:ids=dict(db.execute('SELECT dn,id FROM nds_entries'))
|
||||
os.environ.update(MARS_NDS_LAB='1',MARS_NDS_LAB_READ='1',MARS_NDS_LAB_CONFIG=json.dumps(cfg),MARS_NDS_LAB_DB=str(path))
|
||||
lib=C.CDLL(str(root/'build/test-nds-wire.so'));call=lib.mars_nds_request;call.argtypes=[C.c_uint8,C.c_uint8,C.c_void_p,C.c_size_t,C.c_void_p,C.c_size_t,C.POINTER(C.c_size_t)];call.restype=C.c_int
|
||||
seq=0
|
||||
def ncp(p):
|
||||
global seq
|
||||
seq=(seq+1)%256;out=C.create_string_buffer(4096);used=C.c_size_t();rc=call(1,seq,p,len(p),out,len(out),C.byref(used));return rc,out.raw[:used.value]
|
||||
def request(v,p,limit=65536,wire=512,split=0):
|
||||
h=b'\2'+le(END,wire,len(p)+12,0,v,limit);rc,out=ncp(h+p[:split] if split else h+p)
|
||||
if rc:return rc,b''
|
||||
if split:
|
||||
assert len(out)==8;rc,out=ncp(b'\2'+out[4:8]+p[split:]);assert not rc
|
||||
assert len(out)>=12 and len(out)-4==int.from_bytes(out[:4],'little');r=Reader(out);r.u32();handle=r.u32();status=struct.unpack('<i',r.take(4))[0];body=r.take(len(out)-12)
|
||||
while handle!=END:
|
||||
rc,out=ncp(b'\2'+le(handle));assert not rc;r=Reader(out);assert r.u32()==len(out)-4;handle=r.u32();body+=r.take(len(out)-8)
|
||||
return status,body
|
||||
def ok(v,p,**kw):
|
||||
rc,b=request(v,p,**kw);assert rc==0,(v,rc);return Reader(b)
|
||||
org=ids['O=HOME'];servers=ids['OU=SERVERS.O=HOME'];volume=ids['CN=MARS_SYS.OU=SERVERS.O=HOME'];user=ids['CN=ALICE.OU=USERS.O=HOME'];mask=0x391f
|
||||
r=ok(1,le(0,0x42,0)+string('OU=SERVERS.O=HOME')+le(0,0));assert r.u32()==1 and r.oid()==servers;r.u32();r.end()
|
||||
r=ok(1,le(0,0x42,0,0,0,0));assert r.u32()==1 and r.oid()==ids['[Root]'];r.u32();r.end()
|
||||
r=ok(2,le(2,0,mask)+be(servers));assert r.dsi()=={'id':servers,'children':3,'parent':org,'class':'Organizational Unit','rdn':'OU=SERVERS','dn':'OU=SERVERS.O=HOME'};r.end()
|
||||
def read(oid,info=1,flags=0,all=1,names=()):return le(1,flags,END)+be(oid)+le(info,all,len(names))+b''.join(string(x) for x in names)
|
||||
r=ok(3,read(volume),wire=28,split=10);assert r.u32()==END and r.u32()==1;a=r.attrs(1);r.end();assert a=={'Object Class':(20,'Volume'),'CN':(3,'MARS_SYS'),'Host Server':(1,'CN=MARS.OU=SERVERS.O=HOME'),'Host Resource Name':(3,'SYS')}
|
||||
r=ok(3,read(volume,flags=1,all=0,names=['Host Server']));r.take(8);assert r.attrs(1)=={'Host Server':(1,'MARS.SERVERS.HOME')};r.end()
|
||||
r=ok(3,read(user));r.take(8);assert r.attrs(1)['Login Disabled']==(7,False);r.end()
|
||||
r=ok(3,read(volume,0));r.take(8);assert set(r.attrs(0))==set(a);r.end()
|
||||
def compare(oid,attr,val):return le(0)+be(oid)+le(1)+string(attr)+le(1)+val
|
||||
for val,matched in [('sys',1),('DATA',0)]:r=ok(4,compare(volume,'Host Resource Name',string(val)));assert r.u32()==matched;r.end()
|
||||
r=ok(4,compare(user,'Login Disabled',le(1,0)));assert r.u32()==1;r.end()
|
||||
assert request(4,compare(volume,'Password',string('x')))==(-603,b'')
|
||||
def listq(oid,flags=0,name='',cls='',mask=mask):return le(1,flags,END)+be(oid)+le(mask)+(string(name) if name else le(0))+(string(cls) if cls else le(0))
|
||||
r=ok(5,listq(servers,cls='Volume'),wire=32);assert r.u32()==END and r.u32()==2;assert {r.dsi()['id'],r.dsi()['id']}=={volume,ids['CN=MARS_DATA.OU=SERVERS.O=HOME']};r.end()
|
||||
r=ok(5,listq(org,flags=3));assert r.u32()==END and r.u32()==2;assert {r.dsi()['dn'],r.dsi()['dn']}=={'SERVERS.HOME','USERS.HOME'};r.end()
|
||||
def search(f,scope=2,base=org):return le(3,0,END)+be(base)+le(scope,0,1,mask,1,0)+f
|
||||
for filter_,count in [(le(0,17)+string('Volume'),2),(le(0,15)+string('Host Resource Name'),2),(le(0,7)+string('Host Resource Name')+string('SYS'),1),(le(2,0),7)]:
|
||||
r=ok(6,search(filter_));assert r.u32()==END and r.u32()==7 and r.u32()==1;obj=Reader(r.take(r.u32()));assert obj.u32()==count
|
||||
for _ in range(count):d=obj.dsi();assert obj.u32()==1;assert obj.attrs(1)['Object Class'][1]==d['class']
|
||||
obj.end();assert r.u32()==4 and r.u32()==0;r.end()
|
||||
assert request(6,search(le(0,7)+string('Host Resource Name')+le(0xffffffff),0,user))==(-641,b'')
|
||||
assert request(3,read(volume),limit=12)==(-304,b'')
|
||||
assert request(2,le(2,0,4)+be(org))==(-672,b'')
|
||||
assert request(5,listq(servers,name='*'))==(-672,b'')
|
||||
assert request(3,read(0x123456))==(-601,b'')
|
||||
for v in (7,9,12,15,54,55,56,57,58,59,60):assert request(v,b'')==(0xfb,b'')
|
||||
# Valid framing, malformed inner payloads, deterministic random requests.
|
||||
rng=random.Random(8266)
|
||||
for _ in range(10000):
|
||||
v=rng.randint(2,6);p=le({2:2,3:1,4:0,5:1,6:3}[v])+rng.randbytes(rng.randrange(100));status,b=request(v,p)
|
||||
assert status!=0 and not b
|
||||
print('PASS NDAP 2/3/4/5/6 wire fixtures, root and readable resolve, multiframe, bool/DN/string, selection, search, bounds, 10000 malformed inner requests, denied writes/auth')
|
||||
Reference in New Issue
Block a user