import contextlib, json, os, pathlib, sqlite3, subprocess, sys, tempfile root=pathlib.Path(__file__).resolve().parents[1] sys.path.insert(0,str(root/'nds'/'tools')) import snapshot as s with tempfile.TemporaryDirectory() as t: p=pathlib.Path(t)/'db';d={'tree':'MARS_TREE','organization':'HOME','units':['USERS'],'placements':[{'name':'alice','type':1,'unit':'USERS'}]} def get(dn): with contextlib.closing(sqlite3.connect(p)) as db:return db.execute('SELECT id FROM nds_entries WHERE dn=?',(dn,)).fetchone() def wire(config,*args, db=p): env=dict(os.environ,MARS_NDS_LAB='1',MARS_NDS_LAB_CONFIG=json.dumps(config),MARS_NDS_LAB_DB=str(db)) subprocess.run([str(root/'build/test-nds-lab'),*args],env=env,check=True) config=s.publish(d,p);org=get('O=HOME');assert org and not get('CN=ALICE.OU=USERS.O=HOME');wire(config,'database') s.publish(d,p,True);a=get('CN=ALICE.OU=USERS.O=HOME');assert a and get('O=HOME')==org;wire(config,'database',str(a[0])) s.publish(d,p,True);assert get('CN=ALICE.OU=USERS.O=HOME')==a wire(config,'disabled',db=p.with_name('missing'));wire(dict(config,tree='OTHER'),'disabled') before=p.read_bytes() try:s.publish(dict(d,placements=d['placements']+[dict(d['placements'][0],type=2)]),p,True) except ValueError:pass else:raise AssertionError('collision accepted') assert p.read_bytes()==before s.publish(d,p);assert not get('CN=ALICE.OU=USERS.O=HOME');wire(config,'database') s.publish(d,p,True);assert get('CN=ALICE.OU=USERS.O=HOME')[0]>a[0] assert get('O=HOME')==org and p.stat().st_mode&0o777==0o644 print('PASS snapshot default privacy, stable IDs, no reuse, rollback on collision, absent/mismatched database fails closed')