Files

81 lines
3.2 KiB
Python

import hashlib, os, tempfile, unittest, subprocess, sys
HERE = os.path.dirname(os.path.abspath(__file__))
MOD = os.path.join(HERE, "sha256_manifest.py")
# Binding (Q85, 2026-11-02). This line read the host-absolute
# "/root/troupe-standup/wizard-work/beam/sha256_manifest.py", so every cell
# below graded the host's copy and the committed bytes were graded nowhere --
# the same defect Q153 repaired in test_next_greater.py, and invisible to
# check_bindings.py, which parses `sys.path` calls rather than path constants.
# Resolve beside this file, and assert the resolution instead of trusting it.
assert os.path.isfile(MOD), "no sha256_manifest.py beside the test: %s" % MOD
assert os.path.realpath(os.path.dirname(MOD)) == os.path.realpath(HERE), MOD
class T(unittest.TestCase):
def make_tree(self):
d = tempfile.mkdtemp()
os.makedirs(os.path.join(d, "sub"))
for p, content in [("a.txt", b"hello"), ("sub/b.txt", b"world"), ("c.bin", b"\x00\x01")]:
full = os.path.join(d, p)
with open(full, "wb") as f:
f.write(content)
return d
def runcli(self, *args):
return subprocess.run([sys.executable, MOD, *args], capture_output=True, text=True)
def test_gen_verify_roundtrip_self_skip(self):
d = self.make_tree()
man = os.path.join(d, "manifest.txt")
r = self.runcli("gen", d, "--out", man)
self.assertEqual(r.returncode, 0, r.stderr)
# manifest itself must not be listed
with open(man) as f:
self.assertNotIn("manifest.txt", f.read())
r = self.runcli("verify", d, man)
self.assertEqual(r.returncode, 0, r.stderr + r.stdout)
self.assertIn("OK: 3 files verified", r.stdout)
def test_verify_detects_tamper(self):
d = self.make_tree()
man = os.path.join(d, "manifest.txt")
self.runcli("gen", d, "--out", man)
with open(os.path.join(d, "a.txt"), "wb") as f:
f.write(b"tampered")
r = self.runcli("verify", d, man)
self.assertNotEqual(r.returncode, 0)
self.assertIn("MISMATCH", r.stderr)
def test_verify_detects_extra(self):
d = self.make_tree()
man = os.path.join(d, "manifest.txt")
self.runcli("gen", d, "--out", man)
with open(os.path.join(d, "extra.txt"), "w") as f:
f.write("x")
r = self.runcli("verify", d, man)
self.assertNotEqual(r.returncode, 0)
self.assertIn("EXTRA", r.stderr)
def test_verify_detects_missing(self):
d = self.make_tree()
man = os.path.join(d, "manifest.txt")
self.runcli("gen", d, "--out", man)
os.remove(os.path.join(d, "sub", "b.txt"))
r = self.runcli("verify", d, man)
self.assertNotEqual(r.returncode, 0)
self.assertIn("MISSING", r.stderr)
def test_manifest_outside_root_not_self_listed(self):
d = self.make_tree()
man = tempfile.mktemp()
r = self.runcli("gen", d, "--out", man)
self.assertEqual(r.returncode, 0, r.stderr)
with open(man) as f:
self.assertNotIn(man, f.read())
r = self.runcli("verify", d, man)
self.assertEqual(r.returncode, 0, r.stderr + r.stdout)
if __name__ == "__main__":
unittest.main()