forked from ChelseaKR/oscal-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_determinism.py
More file actions
56 lines (43 loc) · 1.89 KB
/
Copy pathtest_determinism.py
File metadata and controls
56 lines (43 loc) · 1.89 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
55
56
"""Same input, byte-identical output. Twice, and in separate processes."""
from __future__ import annotations
import json
import subprocess
import sys
from oscal_validate import __version__, validate_file
from oscal_validate.findings import render_findings_json
from .conftest import fixture_path
FIXTURES = ["clean_catalog.json", "clean_profile.json"]
def test_validating_twice_gives_identical_findings() -> None:
for name in FIXTURES:
assert validate_file(fixture_path(name)) == validate_file(fixture_path(name)), name
def test_json_rendering_is_byte_identical_across_runs() -> None:
for name in FIXTURES:
first = render_findings_json(validate_file(fixture_path(name)), __version__, "catalog")
second = render_findings_json(validate_file(fixture_path(name)), __version__, "catalog")
assert first.encode("utf-8") == second.encode("utf-8"), name
def test_cli_output_is_byte_identical_across_processes() -> None:
# Two separate interpreter processes: catches any hidden ordering that
# depends on hash randomization or import order.
command = [
sys.executable,
"-m",
"oscal_validate",
str(fixture_path("clean_profile.json")),
"--resolve",
str(fixture_path("clean_catalog.json")),
"--format",
"json",
]
runs = [subprocess.run(command, capture_output=True, check=False) for _ in range(2)]
assert runs[0].stdout == runs[1].stdout
assert runs[0].stdout, "expected findings output"
json.loads(runs[0].stdout) # and it is valid JSON
def test_the_report_carries_no_timestamp_or_duration() -> None:
payload = json.loads(
render_findings_json(
validate_file(fixture_path("clean_catalog.json")), __version__, "catalog"
)
)
rendered = json.dumps(payload)
for word in ("timestamp", "generated_at", "duration", "elapsed"):
assert word not in rendered