io_v2_zig_digest.py (2574B)
1 #!/usr/bin/env python3 2 """Actual incremental digest source on private Zig and Haskell; external SHA oracle.""" 3 import argparse 4 import hashlib 5 import json 6 from pathlib import Path 7 from source_order import Runner 8 from http_headers import tree_list, result, number 9 from process_rss import run_guarded 10 ROOT = Path(__file__).resolve().parent.parent 11 12 def text(s): return tree_list([number(ord(c)) for c in s]) 13 def main(): 14 p=argparse.ArgumentParser(description=__doc__) 15 p.add_argument('tricu',type=Path); p.add_argument('probe',type=Path); p.add_argument('--directory',type=Path,required=True) 16 args=p.parse_args(); d=args.directory.resolve(); d.mkdir(parents=True,exist_ok=False) 17 r=Runner(args.tricu,d); bundle=d/'digest.arboricx' 18 r.run('compile',['compile','--file',ROOT/'test/io-v2/zig-digest.tri','--store',r.store,'--output',bundle]); pin=r.imported(bundle,'import') 19 def problem(op): return result(False,tree_list([text('invalid-state'),text(op),text('digest already finished'),text('none')])) 20 empty=hashlib.sha256(b'').hexdigest(); abc=hashlib.sha256(b'abc').hexdigest() 21 cases=[('hash','abc',text(abc)),('hash','ÿ',text(hashlib.sha256(bytes([255])).hexdigest())),('hash','a'*4096,text(hashlib.sha256(b'a'*4096).hexdigest())), 22 ('lifecycle',None,tree_list([text(empty),problem('digest/finish'),problem('digest/update')])), 23 ('transferred',None,tree_list([text('completed'),text(abc)]))] 24 for algorithm in ('sha512','é😀'*100): 25 cases.append(('unsupported',algorithm,result(False,tree_list([text('unsupported'),text('digest/open'),text(algorithm[:128]),text('none')])))) 26 checks=[] 27 for i,(name,argument,expected) in enumerate(cases): 28 label=f'{i}-{name}'; command=[str(args.probe.resolve()),str(bundle),name]+(['text:'+argument] if argument is not None else []) 29 output,peak=run_guarded(command,memory_mib=128,seconds=60) 30 (d/(label+'.log')).write_text(output) 31 assert bytes(map(int,output.strip()))==expected,('zig',label) 32 flags=['--io'] 33 if argument is not None: 34 arg=d/(label+'.tree');arg.write_bytes(text(argument));flags+=['--arg-tree-file',arg] 35 assert r.invoke(label,pin,name,flags)==expected,('haskell',label) 36 checks.append(dict(label=label,peak_kib=peak)) 37 (d/'report.json').write_text(json.dumps(dict(checks=checks,haskell=r.records,probe_sha256=hashlib.sha256(args.probe.read_bytes()).hexdigest()),indent=2)+'\n') 38 print(f'PASS {len(checks)} digest source controls on Haskell and private Zig') 39 if __name__=='__main__':main()