#!/usr/bin/env python3 """Negative controls for frozen shared-star certificate/layout/maps.""" import copy,hashlib,json,pathlib,resource,subprocess,sys,tempfile,time HERE=pathlib.Path(__file__).parent original=json.loads((HERE/'global1086.json').read_text());assert original['certificate'] is not None maps=json.loads((HERE/'maps1086.json').read_text());layout=json.loads((HERE/'layout1086.json').read_text());cases=[] x=copy.deepcopy(original) if x['certificate']['kind']=='integer_farkas': index=next(i for i,label in enumerate(layout['equality_labels']) if label[0]=='marginal') x['certificate']['equality_multipliers'][index]+=10**30 else: first=next(entry for entry in x['certificate']['entries'] if entry[0]>=layout['blocks'][0][3]);first[1]+=x['certificate']['T'] cases.append(('damaged_pair_marginal',x,copy.deepcopy(maps),copy.deepcopy(layout))) x=copy.deepcopy(original);m=copy.deepcopy(maps);l=copy.deepcopy(layout);l['blocks'][0][0],l['blocks'][0][1]=l['blocks'][0][1],l['blocks'][0][0] cases.append(('damaged_anchor_transpose_layout',x,m,l)) x=copy.deepcopy(original);m=copy.deepcopy(maps);l=copy.deepcopy(layout);l['conditioned_labels'][0][2]=(l['conditioned_labels'][0][2]+1)%51 cases.append(('damaged_conditioned_row',x,m,l)) x=copy.deepcopy(original);m=copy.deepcopy(maps);l=copy.deepcopy(layout) q=next(q for q,v in m['phase_maps'].items() if [] in v['kill_indices']);v=m['phase_maps'][q];i=v['kill_indices'].index([]);v['representatives'].pop(i);v['kill_indices'].pop(i) cases.append(('omitted_empty_phase_class',x,m,l)) x=copy.deepcopy(original);m=copy.deepcopy(maps);l=copy.deepcopy(layout);assert l['blocks'][0][:3]==[101,103,101] and l['blocks'][1][:3]==[101,103,103];l['blocks'][1][3]=l['blocks'][0][3] cases.append(('aliased_anchor_joint_copy_offset',x,m,l)) start=time.monotonic();before=resource.getrusage(resource.RUSAGE_CHILDREN);out=[] with tempfile.TemporaryDirectory(prefix='controls1086-',dir=HERE) as temp: p=pathlib.Path(temp);(p/'coherence974-input.json').write_bytes((HERE/'coherence974-input.json').read_bytes()) for name,x,m,l in cases: raw=(json.dumps(m,sort_keys=True,separators=(',',':'))+'\n').encode();(p/'maps1086.json').write_bytes(raw);sha=hashlib.sha256(raw).hexdigest();x['maps_sha256']=sha;l['maps_sha256']=sha raw=(json.dumps(l,sort_keys=True,separators=(',',':'))+'\n').encode();(p/'layout1086.json').write_bytes(raw);x['layout_sha256']=hashlib.sha256(raw).hexdigest() (p/'global1086.json').write_text(json.dumps(x)) c=subprocess.run([sys.executable,str(HERE/'check1086.py'),str(p/'global1086.json')],capture_output=True,timeout=60) assert c.returncode!=0,name;out.append({'case':name,'exit_code':c.returncode,'rejected':True,'corrupted_metadata_hashes_updated':True}) after=resource.getrusage(resource.RUSAGE_CHILDREN) print(json.dumps({'job':1086,'pass':True,'controls':out,'immutable_target_preserved':json.loads((HERE/'global1086.json').read_text())==original},sort_keys=True,separators=(',',':'))) print(json.dumps({'child_cpu_seconds':after.ru_utime+after.ru_stime-before.ru_utime-before.ru_stime,'wall_seconds':time.monotonic()-start},sort_keys=True),file=sys.stderr)