"""Trusted expectations stay here; children only build and print API observations.""" import json import os from pathlib import Path import shutil import subprocess import sys import tempfile ROOT = Path(__file__).resolve().parent def payload(seed, n): return bytes((seed + j * 37) % 256 for j in range(n)) def cases(): result = [] # mode, flash page size, cache bytes, command stream, independent expectations. def add(name, mode, page, cache, commands, expected): result.append((name, mode, page, cache, '\n'.join(commands)+'\n', [('INIT', 0)] + expected)) for mode in (0, 1): for page in (128, 256, 512): for direct in (0, 32): for n, m in ((1, 1), (7, 13), (page-9, 23), (page+3, page+11)): a, b = payload(29, n), payload(61, m) commands = ['OPEN 0 %d log' % (4|24|1|direct), 'EOF 0','TELL 0', 'WRITE 0 %d 29' % n,'TELL 0','EOF 0', 'SEEK 0 0 0', 'EOF 0','WRITE 0 %d 61' % m,'TELL 0','EOF 0','FLUSH 0', 'TELL 0','CLOSE 0','OPEN 0 24 log','TELL 0','EOF 0', 'READ 0 %d' % (n+m), 'EOF 0','CLOSE 0'] expected = [('OPEN','positive'),('EOF',1),('TELL',0),('WRITE',n),('TELL',n), ('EOF',1),('SEEK',0),('EOF',0),('WRITE',m),('TELL',n+m),('EOF',1), ('FLUSH',0),('TELL',n+m),('CLOSE',0),('OPEN','positive'),('TELL',0),('EOF',0), ('READ',(n+m,(a+b).hex())),('EOF',1),('CLOSE',0)] add('append-%d-%d-%d-%d' % (mode,page,direct,n),mode,page,4096,commands,expected) # Nonappend read/write position and EOF regressions. n=page+17;a=bytearray(payload(9,n));a[2:7]=payload(97,5) add('overwrite-%d-%d'%(mode,page),mode,page,4096, ['OPEN 0 28 regular','WRITE 0 %d 9'%n,'SEEK 0 2 0','WRITE 0 5 97','TELL 0','EOF 0', 'SEEK 0 0 2','EOF 0','SEEK 0 -3 1','TELL 0','EOF 0','SEEK 0 0 0','READ 0 %d'%n,'CLOSE 0'], [('OPEN','positive'),('WRITE',n),('SEEK',2),('WRITE',5),('TELL',7),('EOF',0), ('SEEK',n),('EOF',1),('SEEK',n-3),('TELL',n-3),('EOF',0),('SEEK',0),('READ',(n,a.hex())),('CLOSE',0)]) # Untouched empty files include append, normal, and direct configurations. for flags in (28,29,60,61): add('empty-%d-%d-%d'%(mode,page,flags),mode,page,4096, ['OPEN 0 %d empty'%flags,'TELL 0','EOF 0','FLUSH 0','EOF 0','CLOSE 0'], [('OPEN','positive'),('TELL',0),('EOF',1),('FLUSH',0),('EOF',1),('CLOSE',0)]) for mode in (0,1): for page in (128,256): # The same closed descriptor must report an error, and another API can follow. add('closed-seek-%d-%d'%(mode,page),mode,page,4096, ['OPEN 0 28 old','CLOSE 0','SEEK 0 0 0','INFO','OPEN 1 28 next','WRITE 1 7 43','CLOSE 1'], [('OPEN','positive'),('CLOSE',0),('SEEK','negative'),('INFO','capacity'),('OPEN','positive'),('WRITE',7),('CLOSE',0)]) # Ordinary permission and closed-write errors already working in the baseline. add('ordinary-errors-%d-%d'%(mode,page),mode,page,4096, ['OPEN 0 12 readonly','WRITE 0 9 1','INFO','CLOSE 0','WRITE 0 3 1','INFO'], [('OPEN','positive'),('WRITE','negative'),('INFO','capacity'),('CLOSE',0),('WRITE','negative'),('INFO','capacity')]) setups = [('direct',4096,['OPEN 0 60 target'],[('OPEN','positive')],1), ('large',4096,['OPEN 0 28 target'],[('OPEN','positive')],page+17)] if mode: setups += [('boundary',4096,['OPEN 0 28 target','WRITE 0 %d 7'%(page-12)], [('OPEN','positive'),('WRITE',page-12)],25), ('cached-large',4096,['OPEN 0 28 target','WRITE 0 9 7'], [('OPEN','positive'),('WRITE',9)],page+23), ('no-write-cache-room',page+128,['OPEN 1 28 holding','WRITE 1 7 4','OPEN 0 28 target'], [('OPEN','positive'),('WRITE',7),('OPEN','positive')],13)] for name, cache, commands, expected, count in setups: for fail_after in range(0,12): add('io-%d-%d-%s-%d'%(mode,page,name,fail_after),mode,page,cache, commands+['FAULT %d'%fail_after,'WRITE 0 %d 73'%count,'FAULT -1','INFO', 'OPEN 2 60 survivor','WRITE 2 7 53','SEEK 2 0 0','READ 2 7','CLOSE 2'], expected+[('FAULT',fail_after),('WRITE',('result',count)),('FAULT',-1),('INFO','capacity'), ('OPEN','positive'),('WRITE',7),('SEEK',0),('READ',(7,payload(53,7).hex())),('CLOSE',0)]) return result def matches(lines, expected): if len(lines) != len(expected): return False for line, (tag, wanted) in zip(lines,expected): p=line.split() if not p or p[0]!=tag or len(p)<2: return False try: value=int(p[1]) except ValueError: return False if wanted=='positive': ok=len(p)==2 and value>0 elif wanted=='negative': ok=len(p)==2 and value<0 elif wanted=='capacity': ok=len(p)==4 and value==0 and int(p[2])>0 and 0<=int(p[3])<=int(p[2]) elif isinstance(wanted,tuple) and wanted[0]=='result': ok=len(p)==2 and (value<0 or value==wanted[1]) elif tag=='READ': ok=value==wanted[0] and p[2:]==[wanted[1]] else: ok=len(p)==2 and value==wanted if not ok: return False return True def main(): source=Path(sys.argv[1])/'src/spiffs_hydrogen.c' support=json.loads((ROOT/'support.dat').read_text()) all_cases=cases() scratch=Path(tempfile.mkdtemp(prefix='spiffs-observe-')) for name,content in support.items(): p=scratch/name;p.parent.mkdir(parents=True,exist_ok=True);p.write_text(content) (scratch/'src/spiffs_hydrogen.c').write_bytes(source.read_bytes()) (scratch/'observe.c').write_bytes((ROOT/'observe.c').read_bytes()) # All source, output directories and inputs exist before the first compiler child. commands=[] for mode in (0,1): binary=scratch/('observe-'+str(mode)) argv=['gcc','-std=c99','-O1','-w','-Isrc','-Isrc/default','-Iport', '-DSPIFFS_CACHE='+str(mode),'-DSPIFFS_CACHE_WR='+str(mode), 'src/spiffs_hydrogen.c','src/spiffs_nucleus.c','src/spiffs_cache.c', 'src/spiffs_gc.c','src/spiffs_check.c','port/flash.c','observe.c','-pthread','-o',str(binary)] commands.append(argv) for p in [scratch,*scratch.rglob('*')]: if os.geteuid()==0: os.chown(p,65534,65534) # No trusted filesystem accesses under scratch occur below this point, including cleanup. for argv in commands: built=subprocess.run(argv,cwd=str(scratch),capture_output=True,text=True,timeout=20) if built.returncode: print('build failed',file=sys.stderr);print('FAIL');return 1 failed=[];errors_observed=0 for name,mode,page,cache,stdin,expected in all_cases: out=subprocess.run([str(scratch/('observe-'+str(mode))),str(page),str(cache)], input=stdin,capture_output=True,text=True,timeout=3) lines=out.stdout.splitlines() if name.startswith('io-') and any(s.startswith('WRITE -') for s in lines):errors_observed+=1 if out.returncode or not matches(lines,expected): failed.append(name) if len(failed)<=3:print(name, 'rc='+str(out.returncode), out.stdout[-1600:],file=sys.stderr) if failed or not errors_observed: print('failed %d of %d scenarios; I/O failures observed %d'%(len(failed),len(all_cases),errors_observed),file=sys.stderr) print('FAIL');return 1 print('scenarios=%d; I/O failures observed=%d'%(len(all_cases),errors_observed),file=sys.stderr) print('PASS');return 0 if __name__=='__main__': raise SystemExit(main())