forked from ChelseaKR/obligation-receipts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_canonical.py
More file actions
46 lines (34 loc) · 1.57 KB
/
Copy pathtest_canonical.py
File metadata and controls
46 lines (34 loc) · 1.57 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
import copy
from pathlib import Path
import pytest
from obligation_receipts.canonical import (
MAX_JSON_NODES,
StrictJsonError,
canonical_json_bytes,
sha256_bytes,
sha256_file,
validate_json_value,
)
def test_canonical_json_is_stable() -> None:
assert canonical_json_bytes({"b": 1, "a": "é"}) == b'{"a":"\xc3\xa9","b":1}'
def test_hash_helpers(tmp_path: Path) -> None:
artifact = tmp_path / "artifact"
artifact.write_bytes(b"hello")
expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
assert sha256_bytes(b"hello") == expected
assert sha256_file(artifact) == expected
def test_json_shape_rejects_excessive_node_count() -> None:
with pytest.raises(StrictJsonError, match="node limit"):
validate_json_value([None] * MAX_JSON_NODES)
def test_validate_json_value_accepts_finite_floats() -> None:
# validate_json_value returns the exact same object it was given, not a
# rebuilt copy, so comparing the result to `data` itself is tautological
# (data == data is always true) and can't detect the traversal silently
# corrupting a nested value. Comparing against an independent snapshot
# taken *before* the call actually exercises that nothing changed.
data = {"score": 0.5, "values": [12.0, -3.14, 0.0]}
snapshot = copy.deepcopy(data)
assert validate_json_value(data) == snapshot
def test_canonical_json_bytes_formats_finite_floats() -> None:
data = {"score": 0.5, "values": [12.0, -3.14, 0.0]}
assert canonical_json_bytes(data) == b'{"score":0.5,"values":[12.0,-3.14,0.0]}'