-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_undo.py
More file actions
54 lines (43 loc) · 1.86 KB
/
fuzz_undo.py
File metadata and controls
54 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import subprocess
import json
success = 0
for i in range(10):
# we need a valid block so it actually reaches the undo parser
# let's just use the existing block fixture, and a fuzzed undo file
import shutil
# we create random undo data
rev_data = os.urandom(50000) # big chunks of random bytes
with open('fuzz_rev.dat', 'wb') as f:
f.write(rev_data)
cmd = ['python', '-m', 'chainlens.cli', '--block', 'fixtures/blocks/blk00000.dat', 'fuzz_rev.dat', 'fixtures/blocks/xor.dat']
result = subprocess.run(cmd, capture_output=True, text=True)
# the CLI must exit 1 (or 0 if somehow valid, which is statistically impossible)
# the output MUST be a valid JSON with "ok": false
try:
if not result.stdout.strip():
print(f"Failed iter {i}: Empty stdout. Stderr: {result.stderr}")
break
parsed = json.loads(result.stdout)
if parsed.get("ok") is not False:
print(f"Failed iter {i}: Returned ok=True?!")
break
if "error" not in parsed:
print(f"Failed iter {i}: No error object")
break
# ensure no traceback leaked in stdout or stderr
if "Traceback" in result.stdout or "Traceback" in result.stderr:
print(f"Failed iter {i}: Traceback leaked!")
break
success += 1
except json.JSONDecodeError:
print(f"Failed iter {i}: Invalid JSON returned -> {result.stdout}")
print(f"Stderr -> {result.stderr}")
break
if success == 10:
print("FUZZ_PASSED: 10/10 random undo files handled safely with proper JSON errors.")
else:
print(f"FUZZ_FAILED: Survived {success}/10")
# cleanup
if os.path.exists('fuzz_rev.dat'):
os.remove('fuzz_rev.dat')