#!/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 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('