Skip to content

Commit

Permalink
Handle hashes in test outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jcushman committed Dec 2, 2024
1 parent 27a3bb3 commit 0cfbaab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
26 changes: 13 additions & 13 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
from inline_snapshot import snapshot
from nabit.lib.archive import make_manifest
from .utils import validate_failing, validate_passing, append_text
from .utils import validate_failing, validate_passing, append_text, replace_hashes

## test valid packages

Expand Down Expand Up @@ -34,9 +34,9 @@ def test_valid_signed_package(signed_bag):

def test_modified_payload(test_bag):
(test_bag / "data/files/test1.txt").write_text("modified payload")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: data/files/test1.txt sha256 validation failed: expected="166cb94a04ebaef4ae79c2a0674d8cea1b7fc354eb2ea436b28c3531de10449c" found="0ef0c788f2de3fe11f1086f4a7c557ac8c812d01786b76d22477832d2e6326f9"
ERROR: bag format is invalid: Bag validation failed: data/files/test1.txt sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand Down Expand Up @@ -109,9 +109,9 @@ def test_missing_data(test_bag):

def test_signed_metadata_modified(test_bag):
append_text(test_bag / "data/signed-metadata.json", " ")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: data/signed-metadata.json sha256 validation failed: expected="54de45672f15c85afecc685b1099a34fb2371c7e1c667eeb71f576ea58031d53" found="82642a7d2637303352c00bb68515059cc9f8dcd7f8939638e5bf317afd42d567"
ERROR: bag format is invalid: Bag validation failed: data/signed-metadata.json sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand Down Expand Up @@ -141,9 +141,9 @@ def test_missing_bagit(test_bag):

def test_modified_bagit(test_bag):
append_text(test_bag / "bagit.txt", " ")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: bagit.txt sha256 validation failed: expected="e91f941be5973ff71f1dccbdd1a32d598881893a7f21be516aca743da38b1689" found="aa0a5b23d0e6a29e67136c07bc81636c4c6dbf24dc4d7d100120f8271eb02b53"
ERROR: bag format is invalid: Bag validation failed: bagit.txt sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand All @@ -161,9 +161,9 @@ def test_missing_bag_info(test_bag):

def test_modified_bag_info(test_bag):
append_text(test_bag / "bag-info.txt", " ")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: bag-info.txt sha256 validation failed: expected="927411016fb7c206d5a4cf304c1d6a1fbfea06a0b0d9ff38ca976052ecde5a49" found="b56b885f8a084f9aeb9d104385946b0799c0fc7cefbb307f36b170ae8b2968b5"
ERROR: bag format is invalid: Bag validation failed: bag-info.txt sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand All @@ -181,9 +181,9 @@ def test_missing_manifest(test_bag):

def test_simple_manifest_modification(test_bag):
append_text(test_bag / "manifest-sha256.txt", " ")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: manifest-sha256.txt sha256 validation failed: expected="1eb4ef1aeaa8f1db13cd056ce3b74060ba0cf25d60e511c3844791c41408d87f" found="deedd551235b23948fd5abfe29c7ad4ce09c721c0bb65328da210ec93c1ee757"
ERROR: bag format is invalid: Bag validation failed: manifest-sha256.txt sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand All @@ -196,9 +196,9 @@ def test_modified_manifest_new_file(test_bag):
hash = hashlib.sha256(b"extra payload").hexdigest()
manifest = test_bag / "manifest-sha256.txt"
manifest.write_text(manifest.read_text() + f"{hash} data/files/extra.txt\n")
assert validate_failing(test_bag) == snapshot("""\
assert replace_hashes(validate_failing(test_bag)) == snapshot("""\
WARNING: No headers.warc found; archive lacks request and response metadata
ERROR: bag format is invalid: Bag validation failed: manifest-sha256.txt sha256 validation failed: expected="1eb4ef1aeaa8f1db13cd056ce3b74060ba0cf25d60e511c3844791c41408d87f" found="fd34a1bf6f432cd8105c4ef2f8557130b2da249799d87a163cdc8f5080c8200b"
ERROR: bag format is invalid: Bag validation failed: manifest-sha256.txt sha256 validation failed: expected="<hash>" found="<hash>"
WARNING: No signatures found
WARNING: No timestamps found\
""")
Expand Down
11 changes: 5 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from pathlib import Path

import re
from nabit.lib.archive import validate_package

def append_text(path: Path, text: str) -> None:
"""Append text to a file."""
with open(path, 'a') as f:
f.write(text)


## helpers

def _validate(bag_path: Path):
"""Capture validation output"""
responses = []
Expand All @@ -23,16 +20,18 @@ def _validate(bag_path: Path):
out = out.replace(str(bag_path), "<bag_path>")
return out


def validate_failing(bag_path: Path):
"""Capture validation output, asserting that it fails"""
output = _validate(bag_path)
assert "ERROR:" in output
return output


def validate_passing(bag_path: Path):
"""Capture validation output, asserting that it passes"""
output = _validate(bag_path)
assert "ERROR:" not in output
return output

def replace_hashes(text: str) -> str:
"""Replace all hashes with a placeholder"""
return re.sub(r'\b[0-9a-f]{64}\b', '<hash>', text)

0 comments on commit 0cfbaab

Please sign in to comment.