Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/store/test/run.py # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / store / test / run.py` 376 lines ยท 14814 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Native process tests. Python is an independent framing referee, never runtime logic.""" 3 import errno 4 import hashlib 5 import os 6 from pathlib import Path 7 import socket 8 import struct 9 import subprocess 10 import tempfile 11 12 ROOT = Path(__file__).resolve().parents[3] 13 UNIT = ROOT / "om/store" 14 COMPILER = subprocess.check_output([str(ROOT / "tools/omc.sh")], text=True).strip() 15 BASE = (ROOT / "om/blake2s/blake2s.om").read_text() 16 QUANT = (ROOT / "om/quantity/quantity.om").read_text() 17 HELP = (UNIT / "store.om").read_text() 18 POOL = (UNIT / "storepool.om").read_text() 19 DRIVER = (UNIT / "test/driver.om").read_text() 20 RESUME = (UNIT / "test/resume.om").read_text() 21 COUNT = 0 22 23 24 def compile_binary(folder, name, helpers=HELP, pool=POOL, driver=DRIVER, prefix=""): 25 source = QUANT + "\n" + BASE + "\n" + prefix + "\n" + helpers + "\n" + pool + "\n" + driver 26 result = subprocess.run([COMPILER], input=source.encode(), capture_output=True) 27 if result.returncode: 28 (folder / (name + ".om")).write_text(source) 29 raise AssertionError(f"compile {name}: {result.stderr.decode()}") 30 target = folder / name 31 target.write_bytes(result.stdout) 32 target.chmod(0o700) 33 return target 34 35 36 def call(binary, path, mode, expected=0, seed="a"): 37 global COUNT 38 r = subprocess.run([str(binary), str(path), mode, seed], capture_output=True, timeout=120) 39 assert r.returncode == 0, (mode, "driver did not complete", r.returncode, r.stderr) 40 assert r.stdout == bytes([expected]), (mode, r.stdout, expected, r.stderr) 41 COUNT += 1 42 43 44 def directory(root, name): 45 p = root / name 46 p.mkdir(mode=0o700) 47 return p 48 49 50 def frame(payloads): 51 result = bytearray(b"OMNETSNAP0000001") 52 chain = bytes(32) 53 total = 0 54 for count, payload in enumerate(payloads, 1): 55 digest = hashlib.blake2s(payload).digest() 56 total += len(payload) 57 chain = hashlib.blake2s(chain + digest + struct.pack(" 65536 251 call(binary, p, "S", 1) 252 finally: 253 holder.kill() 254 holder.wait(timeout=5) 255 holder.stdin.close() 256 holder.stdout.close() 257 call(binary, p, "L") 258 assert (p / "snapshot").read_bytes() == original 259 assert not (p / "snapshot.tmp").exists() 260 261 # Only explicit init accepts absent committed state; orphan temp is never promoted. 262 orphan = directory(root, "orphan") 263 save(orphan / "snapshot.tmp", original) 264 call(binary, orphan, "L", 1) 265 assert (orphan / "snapshot.tmp").exists() 266 call(binary, orphan, "I") 267 empty = directory(root, "empty") 268 call(binary, empty, "E") 269 assert (empty / "snapshot").read_bytes() == frame([]) 270 call(binary, empty, "L", 3) # framing accepts empty; driver expects nonempty semantic records 271 call(binary, p, "U") # too-large chunk is a declared error, not an array trap 272 call(binary, p, "Q") # exactly 65536 frames, then refuse one more 273 call(binary, p, "C") # exactly 32 MiB payload, then refuse one more byte 274 call(binary, p, "Y") # a negative running byte charge is a declared refusal, not a trap 275 call(binary, p, "B") # a negative running frame count is a declared refusal, not a trap 276 assert (p / "snapshot").read_bytes() == original 277 capacity = directory(root, "capacity") 278 call(binary, capacity, "E") 279 call(binary, capacity, "G") # durable full-capacity snapshot and streaming recovery 280 281 symlink = root / "dir-link" 282 symlink.symlink_to(p, target_is_directory=True) 283 call(binary, symlink, "L", 1) 284 for mode in (0o755, 0o750, 0o1700): 285 p.chmod(mode) 286 call(binary, p, "L", 1) 287 p.chmod(0o700) 288 regular = root / "not-directory" 289 save(regular, b"x") 290 call(binary, regular, "L", 1) 291 for name in ("snapshot", "snapshot.tmp"): 292 target = p / name 293 if target.exists(): 294 target.unlink() 295 target.symlink_to(regular) 296 call(binary, p, "L", 1) 297 assert target.is_symlink() 298 target.unlink() 299 target.mkdir(mode=0o700) 300 call(binary, p, "L", 1) 301 target.rmdir() 302 os.mkfifo(target, 0o600) 303 call(binary, p, "L", 1) 304 target.unlink() 305 sock = socket.socket(socket.AF_UNIX) 306 sock.bind(str(target)) 307 call(binary, p, "L", 1) 308 sock.close() 309 target.unlink() 310 save(target, original) 311 for mode in (0o644, 0o400, 0o1600): 312 target.chmod(mode) 313 call(binary, p, "L", 1) 314 target.chmod(0o600) 315 other = root / "extra-link" 316 os.link(target, other) 317 call(binary, p, "L", 1) 318 other.unlink() 319 if os.geteuid() == 0: 320 os.chown(target, 12345, -1) 321 call(binary, p, "L", 1) 322 os.chown(target, 0, -1) 323 target.unlink() 324 save(p / "snapshot", original) 325 if os.geteuid() == 0: 326 os.chown(p, 12345, -1) 327 call(binary, p, "L", 1) 328 os.chown(p, 0, -1) 329 330 # Truncations span header, frames, payload and footer. Corruption includes every field class. 331 for size in (0, 1, 15, 16, 23, 55, 56, 65591, 65592, len(original)-56, len(original)-1): 332 save(p / "snapshot", original[:size]) 333 call(binary, p, "L", 1) 334 for offset in (0, 15, 16, 20, 24, 55, 56, 1000, 65591, len(original)-49, len(original)-16, len(original)-1): 335 broken = bytearray(original) 336 broken[offset] ^= 1 337 save(p / "snapshot", broken) 338 call(binary, p, "L", 1) 339 save(p / "snapshot", original + b"x") 340 call(binary, p, "L", 1) 341 save(p / "snapshot", original) 342 # Oversize metadata rejected before reading attacker-controlled file contents. 343 with (p / "snapshot").open("r+b") as f: 344 f.truncate(`36175945`) 345 call(binary, p, "L", 1) 346 save(p / "snapshot", original) 347 348 # Fault controls exist only in a separately compiled test image, never in store source. 349 for mode in ("W", "Z", "R", "F", "N", "X", "V"): 350 call(fault, p, mode, 1, seed="b") 351 assert (p / "snapshot").read_bytes() == original, mode 352 call(binary, p, "L") 353 for mode in ("P", "T", "J"): 354 call(fault, p, mode, seed="b") 355 call(binary, p, "L", seed="b") 356 call(binary, p, "S") 357 call(fault, p, "D", 2, seed="b") 358 call(binary, p, "L", seed="b") # selected new snapshot, startup re-syncs it 359 360 # Completed-result mutant: removing payload checksum comparison must be observable. 361 mutant_pool = POOL.replace("if ne steqhash h 8 d 0", "if ne int 0 int 0") 362 assert mutant_pool != POOL 363 mutant = compile_binary(root, "mutant", pool=mutant_pool) 364 # A payload mismatch still reaches semantic checking (3), rather than framing refusal (1). 365 # Keep the original frame digest but forge the final chain for altered payload bytes. 366 changed = bytearray(payload) 367 changed[944] ^= 1 368 forged = bytearray(frame([bytes(changed), b"a" * 17])) 369 forged[24:56] = original[24:56] 370 save(p / "snapshot", forged) 371 call(binary, p, "L", 1) 372 call(mutant, p, "L", 3) 373 save(p / "snapshot", original) 374 print(f"PASS store: {COUNT} completed native cases; independent BLAKE2s framing; checksum mutant detected") 375 if os.geteuid() != 0: 376 print("SKIP store foreign-UID chown fixtures: runner is non-root")