Files

1041 lines
38 KiB
Python

from __future__ import annotations
from decimal import Decimal
import hashlib
import json
import os
from pathlib import Path
import subprocess
from typing import Any
import pytest
import dx_productivity.part_replace.compatibility_profile as compatibility_profile_module
from dx_productivity.part_replace.compatibility_profile import (
CompatibilityProfileError,
deploy_compatibility_profile,
promote_compatibility_profile,
validate_compatibility_profile_bytes,
)
ROOT = Path(__file__).resolve().parents[2]
PROFILE = ROOT / "tests" / "fixtures" / "part_replace" / "compatibility-profile-v1.json"
COMMON = ROOT / "integration" / "dxdesigner795" / "BridgeCommon.vbs"
BRIDGE = ROOT / "integration" / "dxdesigner795" / "PartReplaceBridge.vbs"
LIVE_CHECKS = (
"exact_795_build",
"active_project_identity",
"dirty_state_rejection",
"full_inventory",
"exact_group_selection",
"native_dialog_route",
"correct_target",
"wrong_target",
"compatible_single_unit",
"compatible_multi_unit",
"origin_mismatch",
"pin_number_or_count_mismatch",
"changed_net",
"unrelated_object_protection",
"stale_inventory",
"backup_restore",
"undo",
"close_without_save",
"persisted_save_verification",
"multi_project_resume",
"packager",
"erc",
"netlist_comparison",
)
def _valid_bytes() -> bytes:
return PROFILE.read_bytes()
def _acceptance_bytes(root: Path, profile_bytes: bytes) -> bytes:
checks: dict[str, object] = {}
evidence_root = root / "evidence"
evidence_root.mkdir()
for check in LIVE_CHECKS:
evidence = evidence_root / f"{check}.txt"
evidence_bytes = f"observed {check}\n".encode()
evidence.write_bytes(evidence_bytes)
checks[check] = {
"date": "2026-09-03",
"evidence_path": evidence.relative_to(root).as_posix(),
"evidence_sha256": hashlib.sha256(evidence_bytes).hexdigest(),
"fixture": "disposable-site-copy",
"mapping": "OLD-1 -> NEW-1",
"observation": f"verified {check} on the recorded route",
"status": "passed",
"tester": "site-operator",
}
value = {
"central_library_identity": "site-library-r42",
"checks": checks,
"completed_utc": "2026-09-03T08:30:00Z",
"dxdesigner": {"build": "7.9.5.1234", "version": "7.9.5"},
"format_version": 1,
"operator": "site-operator",
"probe_profile_sha256": hashlib.sha256(profile_bytes).hexdigest(),
}
return (
json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
+ "\n"
).encode()
def _mutated_bytes(path: tuple[str | int, ...], value: object) -> bytes:
profile = json.loads(_valid_bytes())
target = profile
for part in path[:-1]:
target = target[part]
target[path[-1]] = value
return (json.dumps(profile, separators=(",", ":")) + "\n").encode()
def _mutated_profile_bytes(
*mutations: tuple[tuple[str | int, ...], object],
) -> bytes:
profile = json.loads(_valid_bytes())
for path, value in mutations:
target = profile
for part in path[:-1]:
target = target[part]
target[path[-1]] = value
return (json.dumps(profile, separators=(",", ":")) + "\n").encode()
def _vbs_string(value: str) -> str:
return '"' + value.replace('"', '""') + '"'
def _vbscript_accepts(payload: bytes, tmp_path: Path) -> bool:
profile = tmp_path / "profile.json"
profile.write_bytes(payload)
wrapper = tmp_path / "validate-profile.vbs"
wrapper.write_text(
f'''Option Explicit
Dim Fso, Source, Profile, Failure, ParseError
Set Fso = CreateObject("Scripting.FileSystemObject")
Source = Fso.OpenTextFile({_vbs_string(str(COMMON))}, 1, False, 0).ReadAll
ExecuteGlobal Source
Source = Fso.OpenTextFile({_vbs_string(str(BRIDGE))}, 1, False, 0).ReadAll
ExecuteGlobal Source
On Error Resume Next
Set Profile = DxBridgeParseJson(DxReadUtf8({_vbs_string(str(profile))}))
ParseError = Err.Number
Err.Clear
On Error GoTo 0
If ParseError <> 0 Then
WScript.Echo "rejected"
WScript.Quit 0
End If
Set Failure = DxBridgeNewFailure()
If DxBridgeValidateProfile(Profile, Failure) Then
WScript.Echo "accepted"
Else
WScript.Echo "rejected"
End If
''',
encoding="ascii",
newline="",
)
completed = subprocess.run(
["cscript", "//nologo", str(wrapper)],
cwd=ROOT,
check=False,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
output = completed.stdout + completed.stderr
assert completed.returncode == 0, output
assert "microsoft vbscript" not in output.casefold(), output
assert completed.stdout.strip() in {"accepted", "rejected"}, output
return completed.stdout.strip() == "accepted"
def _vbscript_number_matrix(tmp_path: Path) -> dict[tuple[int, str], Decimal]:
wrapper = tmp_path / "parse-numbers-by-locale.vbs"
wrapper.write_text(
f'''Option Explicit
Dim Fso, Source, LocaleId, PreviousLocale, NumberText, Parsed
Set Fso = CreateObject("Scripting.FileSystemObject")
Source = Fso.OpenTextFile({_vbs_string(str(COMMON))}, 1, False, 0).ReadAll
ExecuteGlobal Source
Source = Fso.OpenTextFile({_vbs_string(str(BRIDGE))}, 1, False, 0).ReadAll
ExecuteGlobal Source
For Each LocaleId In Array(1033, 1031, 1036)
PreviousLocale = SetLocale(LocaleId)
For Each NumberText In Array("0.5", "-12.75", "6.022e2", "1E-3", "-2.5e+2")
Set Parsed = DxBridgeParseJson("{{""value"":" & NumberText & "}}")
WScript.Echo CStr(LocaleId) & "|" & NumberText & "|" & _
DxInvariantNumber(Parsed("value"))
Next
SetLocale PreviousLocale
Next
''',
encoding="ascii",
newline="",
)
completed = subprocess.run(
["cscript", "//nologo", str(wrapper)],
cwd=ROOT,
check=False,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
output = completed.stdout + completed.stderr
assert completed.returncode == 0, output
assert "microsoft vbscript" not in output.casefold(), output
result: dict[tuple[int, str], Decimal] = {}
for line in completed.stdout.splitlines():
locale_text, number_text, value_text = line.split("|", 2)
result[(int(locale_text), number_text)] = Decimal(value_text)
return result
def test_validator_accepts_the_literal_version_one_profile() -> None:
validate_compatibility_profile_bytes(_valid_bytes())
@pytest.mark.parametrize(
"payload",
(
b'{"format_version":1,"format_version":1}',
_valid_bytes().replace(b'"x": 0.5', b'"x": NaN', 1),
_valid_bytes().replace(b'"x": 0.5', b'"x": Infinity', 1),
_valid_bytes().replace(b'"x": 0.5', b'"x": -Infinity', 1),
),
ids=("duplicate-key", "nan", "infinity", "negative-infinity"),
)
def test_validator_rejects_non_interoperable_json(payload: bytes) -> None:
with pytest.raises(CompatibilityProfileError):
validate_compatibility_profile_bytes(payload)
@pytest.mark.parametrize(
("path", "value"),
(
(("format_version",), True),
(("format_version",), 2_147_483_648),
(("format_version",), -2_147_483_649),
(("enumeration", "component_count"), True),
(("enumeration", "component_count"), 2_147_483_648),
(("components", 0, "units"), True),
(("components", 0, "units"), -2_147_483_649),
(("api_errors", 0, "number"), True),
(("api_errors", 0, "number"), 2_147_483_648),
),
ids=(
"format-bool",
"format-high",
"format-low",
"count-bool",
"count-high",
"units-bool",
"units-low",
"error-bool",
"error-high",
),
)
def test_validator_rejects_values_vbscript_cannot_treat_as_protocol_integers(
path: tuple[str | int, ...],
value: object,
) -> None:
with pytest.raises(CompatibilityProfileError):
validate_compatibility_profile_bytes(_mutated_bytes(path, value))
@pytest.mark.parametrize(
("path", "value"),
(
(("routes", "save", "route"), "Application.SaveAs"),
(("routes", "save", "attempts", 0, "route"), "Application.SaveAs"),
(("production_replace_route",), "ccomp_research"),
(("routes", "ccomp_research", "production_enabled"), True),
),
ids=("route", "attempt-route", "production-route", "research-enabled"),
)
def test_validator_rejects_noncanonical_routes(
path: tuple[str | int, ...],
value: object,
) -> None:
with pytest.raises(CompatibilityProfileError):
validate_compatibility_profile_bytes(_mutated_bytes(path, value))
@pytest.mark.parametrize(
"payload",
(
_mutated_bytes(("dxdesigner", "version"), "7.9.6"),
_mutated_bytes(("dxdesigner", "build"), "8.0.0.1"),
_mutated_bytes(("routes", "save", "attempted"), False),
_mutated_bytes(("routes", "save", "available"), False),
_mutated_profile_bytes(
(("routes", "save", "attempted"), False),
(("routes", "save", "available"), True),
(("routes", "save", "attempts", 0, "attempted"), False),
),
_mutated_bytes(
("routes", "save", "attempts", 0, "error"),
{
"description": "contradictory error",
"number": 5,
"operation": "route.save.Application.Save",
"source": "fixture",
},
),
_mutated_profile_bytes(
(("routes", "save", "available"), False),
(("routes", "save", "attempts", 0, "available"), False),
),
_mutated_bytes(("safety", "mutating_probe_authorized"), False),
_mutated_bytes(("safety", "mutating_probe_attempted"), False),
_mutated_bytes(("safety", "disposable_project_marker"), False),
_mutated_bytes(("live_acceptance", "checked"), True),
_mutated_bytes(("live_acceptance", "reason"), "acceptance_complete"),
),
ids=(
"wrong-version",
"wrong-build-family",
"route-attempt-summary",
"route-availability-summary",
"available-without-attempt",
"successful-attempt-with-error",
"failed-attempt-without-error",
"authorization-not-derived",
"mutating-attempt-summary",
"authorization-facts-disagree",
"unchecked-promotion",
"unchecked-reason",
),
)
def test_python_and_vbscript_reject_incoherent_or_non_795_profile(
payload: bytes,
tmp_path: Path,
) -> None:
# Catches individually well-typed fields forming contradictory safety evidence.
with pytest.raises(CompatibilityProfileError):
validate_compatibility_profile_bytes(payload)
assert not _vbscript_accepts(payload, tmp_path)
Mutation = tuple[str, tuple[str | int, ...], object]
DELETE = object()
def _set_mutation(root: object, path: tuple[str | int, ...], value: object) -> None:
target: Any = root
for part in path[:-1]:
target = target[part]
if value is DELETE:
del target[path[-1]]
else:
target[path[-1]] = value
PROFILE_MUTATIONS: tuple[Mutation, ...] = (
("root extra", ("unexpected",), "extra"),
("root missing", ("live_acceptance",), DELETE),
("format type", ("format_version",), True),
("format value", ("format_version",), 2),
("dxdesigner object", ("dxdesigner",), []),
("dxdesigner exact fields", ("dxdesigner", "extra"), "x"),
("dxdesigner missing field", ("dxdesigner", "version"), DELETE),
("dxdesigner build", ("dxdesigner", "build"), False),
("dxdesigner version", ("dxdesigner", "version"), []),
("dxdesigner evidence", ("dxdesigner", "version"), ""),
("project object", ("project",), []),
("project exact fields", ("project", "extra"), "x"),
("project active view", ("project", "active_view"), False),
("project dirty", ("project", "dirty"), 0),
("project identity", ("project", "identity"), None),
("enumeration object", ("enumeration",), []),
("enumeration exact fields", ("enumeration", "extra"), "x"),
("enumeration available type", ("enumeration", "available"), 1),
("enumeration required", ("enumeration", "available"), False),
("enumeration count type", ("enumeration", "component_count"), 1.5),
("enumeration count range", ("enumeration", "component_count"), -1),
("enumeration route type", ("enumeration", "route"), True),
("enumeration route value", ("enumeration", "route"), "Other.Route"),
("components array", ("components",), {}),
("component object", ("components", 0), True),
("component exact fields", ("components", 0, "extra"), "x"),
("component missing field", ("components", 0, "units"), DELETE),
("component hierarchy", ("components", 0, "hierarchy"), 1),
("component logical identity", ("components", 0, "logical_identity"), []),
("component part number", ("components", 0, "part_number"), False),
("component refdes", ("components", 0, "refdes"), None),
("component symbol", ("components", 0, "symbol_identity"), {}),
("component units", ("components", 0, "units"), 1.5),
("component rotation", ("components", 0, "rotation"), "90"),
("component mirror", ("components", 0, "mirror"), 0),
("origin object", ("components", 0, "origin"), []),
("origin exact fields", ("components", 0, "origin", "extra"), 0),
("origin x", ("components", 0, "origin", "x"), False),
("origin y", ("components", 0, "origin", "y"), "1"),
("position object", ("components", 0, "position"), []),
("position x", ("components", 0, "position", "x"), None),
("position y", ("components", 0, "position", "y"), "2"),
("gates array", ("components", 0, "gates"), {}),
("gate object", ("components", 0, "gates", 0), True),
("gate exact fields", ("components", 0, "gates", 0, "extra"), 0),
("gate name", ("components", 0, "gates", 0, "name"), 1),
("gate unit", ("components", 0, "gates", 0, "unit_number"), False),
("attributes array", ("components", 0, "attributes"), {}),
("attribute object", ("components", 0, "attributes", 0), True),
("attribute exact fields", ("components", 0, "attributes", 0, "extra"), 0),
("attribute name", ("components", 0, "attributes", 0, "name"), 1),
("attribute value", ("components", 0, "attributes", 0, "value"), []),
("pins array", ("components", 0, "pins"), {}),
("pin object", ("components", 0, "pins", 0), True),
("pin exact fields", ("components", 0, "pins", 0, "extra"), 0),
("pin name", ("components", 0, "pins", 0, "name"), 1),
("pin number", ("components", 0, "pins", 0, "number"), 1),
("pin net object", ("components", 0, "pins", 0, "net_identity"), {}),
("pin net primitive", ("components", 0, "pins", 0, "net_identity"), 1),
("pin x", ("components", 0, "pins", 0, "x"), False),
("pin y", ("components", 0, "pins", 0, "y"), "1"),
("nets array", ("components", 0, "nets"), {}),
("net item", ("components", 0, "nets", 0), 1),
("component errors array", ("components", 0, "api_errors"), {}),
("component error object", ("components", 0, "api_errors", 0), True),
("component error exact fields", ("components", 0, "api_errors", 0, "extra"), 0),
("component error missing field", ("components", 0, "api_errors", 0, "source"), DELETE),
("component error description", ("components", 0, "api_errors", 0, "description"), 1),
("component error number", ("components", 0, "api_errors", 0, "number"), 1.5),
("component error operation", ("components", 0, "api_errors", 0, "operation"), []),
("component error source", ("components", 0, "api_errors", 0, "source"), False),
("interfaces object", ("interfaces",), []),
("interfaces exact fields", ("interfaces", "extra"), {"available": True}),
("interfaces missing field", ("interfaces", "pins"), DELETE),
("interface record", ("interfaces", "pins"), True),
("interface exact fields", ("interfaces", "pins", "extra"), 0),
("interface available type", ("interfaces", "pins", "available"), 1),
("interface evidence", ("interfaces", "pins", "available"), False),
("routes object", ("routes",), []),
("routes allowed names", ("routes", "unexpected"), {}),
("routes missing name", ("routes", "save"), DELETE),
("route record", ("routes", "save"), True),
("route exact fields", ("routes", "save", "extra"), 0),
("route missing field", ("routes", "save", "attempts"), DELETE),
("route attempted", ("routes", "save", "attempted"), 1),
("route available", ("routes", "save", "available"), "true"),
("route fallback", ("routes", "save", "manual_fallback"), []),
("route path type", ("routes", "save", "route"), False),
("route path value", ("routes", "save", "route"), "Application.SaveAs"),
("route attempts array", ("routes", "save", "attempts"), {}),
("route attempts length", ("routes", "save", "attempts"), []),
("route attempt object", ("routes", "save", "attempts", 0), True),
("route attempt exact fields", ("routes", "save", "attempts", 0, "extra"), 0),
("route attempt missing field", ("routes", "save", "attempts", 0, "error"), DELETE),
("route attempt attempted", ("routes", "save", "attempts", 0, "attempted"), 1),
("route attempt available", ("routes", "save", "attempts", 0, "available"), 1),
("route attempt route", ("routes", "save", "attempts", 0, "route"), []),
("route attempt route value", ("routes", "save", "attempts", 0, "route"), "Application.SaveAs"),
("route attempt error", ("routes", "save", "attempts", 0, "error"), []),
("research production field", ("routes", "ccomp_research", "production_enabled"), True),
("research production missing", ("routes", "ccomp_research", "production_enabled"), DELETE),
("production route type", ("production_replace_route",), 1),
("production route value", ("production_replace_route",), "ccomp_research"),
("safety object", ("safety",), []),
("safety exact fields", ("safety", "extra"), False),
("safety missing field", ("safety", "request_valid"), DELETE),
("safety disposable", ("safety", "disposable_project_marker"), 0),
("safety attempted", ("safety", "mutating_probe_attempted"), 0),
("safety authorized", ("safety", "mutating_probe_authorized"), 0),
("safety confirmation", ("safety", "operator_confirmation"), 0),
("safety request", ("safety", "request_valid"), 0),
("api errors array", ("api_errors",), {}),
("api error item", ("api_errors", 0), True),
("live acceptance object", ("live_acceptance",), []),
("live acceptance exact fields", ("live_acceptance", "extra"), False),
("live acceptance missing field", ("live_acceptance", "reason"), DELETE),
("live acceptance checked", ("live_acceptance", "checked"), 0),
("live acceptance reason", ("live_acceptance", "reason"), []),
)
@pytest.mark.parametrize(
("name", "path", "value"),
PROFILE_MUTATIONS,
ids=[mutation[0] for mutation in PROFILE_MUTATIONS],
)
def test_validator_rejects_every_version_one_schema_mutation(
name: str,
path: tuple[str | int, ...],
value: object,
) -> None:
# Catches drift anywhere in the exact nested bridge schema.
del name
profile = json.loads(_valid_bytes())
_set_mutation(profile, path, value)
payload = (json.dumps(profile, separators=(",", ":")) + "\n").encode()
with pytest.raises(CompatibilityProfileError):
validate_compatibility_profile_bytes(payload)
def _renamed_route_bytes() -> bytes:
profile = json.loads(_valid_bytes())
profile["routes"]["save_as"] = profile["routes"].pop("save")
return (json.dumps(profile, separators=(",", ":")) + "\n").encode()
@pytest.mark.parametrize(
("payload", "expected"),
(
(_valid_bytes(), True),
(
_valid_bytes().replace(
b'"format_version": 1,',
b'"format_version": 1,\n "format_version": 1,',
1,
),
False,
),
(_valid_bytes().replace(b'"x": 0.5', b'"x": NaN', 1), False),
(_mutated_bytes(("api_errors", 0, "number"), 2_147_483_648), False),
(_renamed_route_bytes(), False),
(_mutated_bytes(("routes", "save", "route"), "Application.SaveAs"), False),
(
_mutated_bytes(
("routes", "save", "attempts", 0, "route"),
"Application.SaveAs",
),
False,
),
(_mutated_bytes(("production_replace_route",), "ccomp_research"), False),
(
_mutated_bytes(
("routes", "ccomp_research", "production_enabled"),
True,
),
False,
),
),
ids=(
"valid",
"duplicate-key",
"nan",
"int32-overflow",
"route-name",
"route-value",
"attempt-route-value",
"production-route",
"research-enabled",
),
)
def test_python_and_vbscript_profile_validators_have_the_same_verdict(
payload: bytes,
expected: bool,
tmp_path: Path,
) -> None:
try:
validate_compatibility_profile_bytes(payload)
except CompatibilityProfileError:
python_accepted = False
else:
python_accepted = True
assert python_accepted is expected
assert _vbscript_accepts(payload, tmp_path) is expected
@pytest.mark.parametrize(
("number_text", "expected"),
(
("-2147483648", True),
("2147483647", True),
("-0", True),
("2147483648.0", True),
("6.022e23", True),
("1E-3", True),
("1.7976931348623157e308", True),
("1.7976931348623158e308", True),
("1.797693134862315807e308", True),
("1.797693134862315808e308", False),
("1.7976931348623159e308", False),
("-1.7976931348623159e308", False),
("0." + "0" * 9_691 + "1e100000", False),
(str(2**1024 - 2**970) + "0" * 10_000 + "e-100000", True),
("2147483648", False),
("-2147483649", False),
("+1", False),
("01", False),
(".5", False),
("1.", False),
("1e", False),
("1e+", False),
("1e309", False),
("9" * 5000, False),
),
ids=(
"int32-min",
"int32-max",
"negative-zero",
"large-finite-decimal",
"positive-exponent",
"negative-exponent",
"double-max-finite",
"double-max-rounds-finite",
"double-rounding-edge-finite",
"double-rounding-edge-overflow",
"double-positive-overflow",
"double-negative-overflow",
"long-positive-exponent-overflow",
"long-negative-exponent-underflow",
"int32-high",
"int32-low",
"leading-plus",
"leading-zero",
"missing-integer-part",
"missing-fraction",
"missing-exponent",
"missing-signed-exponent",
"non-finite-exponent",
"integer-token-over-python-limit",
),
)
def test_python_and_cscript_share_strict_json_number_grammar_and_int32_integers(
number_text: str,
expected: bool,
tmp_path: Path,
) -> None:
# Catches generic numeric fields bypassing CLng range or VBS accepting non-JSON spellings.
payload = _valid_bytes().replace(
b'"x": 0.5',
b'"x": ' + number_text.encode("ascii"),
1,
)
try:
validate_compatibility_profile_bytes(payload)
except CompatibilityProfileError:
python_accepted = False
else:
python_accepted = True
assert python_accepted is expected
assert _vbscript_accepts(payload, tmp_path) is expected
def test_cscript_parses_json_decimals_identically_across_supported_locales(
tmp_path: Path,
) -> None:
# Catches CDbl interpreting the JSON period as a locale-specific thousands separator.
observed = _vbscript_number_matrix(tmp_path)
expected = {
"0.5": Decimal("0.5"),
"-12.75": Decimal("-12.75"),
"6.022e2": Decimal("602.2"),
"1E-3": Decimal("0.001"),
"-2.5e+2": Decimal("-250"),
}
assert observed == {
(locale_id, number_text): value
for locale_id in (1033, 1031, 1036)
for number_text, value in expected.items()
}
def test_promotion_binds_complete_live_evidence_and_emits_bridge_valid_profile(
tmp_path: Path,
) -> None:
# Catches live acceptance being enabled by a bare boolean or an unbound checklist.
source = tmp_path / "probe.json"
source_bytes = _valid_bytes()
source.write_bytes(source_bytes)
acceptance = tmp_path / "acceptance.json"
acceptance_bytes = _acceptance_bytes(tmp_path, source_bytes)
acceptance.write_bytes(acceptance_bytes)
destination = tmp_path / "runtime" / "compatibility-profile.json"
promoted = promote_compatibility_profile(source, acceptance, destination)
assert promoted == destination
output = destination.read_bytes()
profile = json.loads(output)
assert profile["live_acceptance"] == {
"checked": True,
"evidence_sha256": hashlib.sha256(acceptance_bytes).hexdigest(),
"reason": "live_795_acceptance_completed",
}
assert source.read_bytes() == source_bytes
validate_compatibility_profile_bytes(output)
vbs_root = tmp_path / "vbs-validation"
vbs_root.mkdir()
assert _vbscript_accepts(output, vbs_root)
@pytest.mark.parametrize(
"mutation",
(
"wrong-profile-hash",
"missing-check",
"failed-check",
"wrong-evidence-hash",
"duplicate-evidence-hash",
"wrong-build",
"blank-operator",
),
)
def test_promotion_rejects_incomplete_or_unbound_live_evidence(
tmp_path: Path,
mutation: str,
) -> None:
# Catches promotion accepting a manifest that cannot be audited back to evidence bytes.
source = tmp_path / "probe.json"
source_bytes = _valid_bytes()
source.write_bytes(source_bytes)
acceptance = tmp_path / "acceptance.json"
value = json.loads(_acceptance_bytes(tmp_path, source_bytes))
if mutation == "wrong-profile-hash":
value["probe_profile_sha256"] = "0" * 64
elif mutation == "missing-check":
del value["checks"]["wrong_target"]
elif mutation == "failed-check":
value["checks"]["wrong_target"]["status"] = "failed"
elif mutation == "wrong-evidence-hash":
value["checks"]["wrong_target"]["evidence_sha256"] = "0" * 64
elif mutation == "duplicate-evidence-hash":
first = value["checks"]["correct_target"]
duplicate = value["checks"]["wrong_target"]
first_bytes = (tmp_path / first["evidence_path"]).read_bytes()
(tmp_path / duplicate["evidence_path"]).write_bytes(first_bytes)
duplicate["evidence_sha256"] = first["evidence_sha256"]
elif mutation == "wrong-build":
value["dxdesigner"]["build"] = "7.9.5.9999"
elif mutation == "blank-operator":
value["operator"] = ""
acceptance.write_text(
json.dumps(value, sort_keys=True, separators=(",", ":")) + "\n",
encoding="utf-8",
)
destination = tmp_path / "runtime" / "compatibility-profile.json"
destination.parent.mkdir()
destination.write_bytes(b"previous\n")
with pytest.raises(CompatibilityProfileError):
promote_compatibility_profile(source, acceptance, destination)
assert destination.read_bytes() == b"previous\n"
def test_deploy_rejects_prechecked_profile_without_acceptance_manifest(
tmp_path: Path,
) -> None:
# Catches the generic deploy path bypassing evidence-bound promotion.
value = json.loads(_valid_bytes())
value["live_acceptance"] = {
"checked": True,
"evidence_sha256": "a" * 64,
"reason": "live_795_acceptance_completed",
}
checked = (json.dumps(value, separators=(",", ":")) + "\n").encode()
validate_compatibility_profile_bytes(checked)
source = tmp_path / "hand-authored-checked-profile.json"
source.write_bytes(checked)
destination = tmp_path / "runs" / "compatibility-profile.json"
destination.parent.mkdir()
destination.write_bytes(b"previous\n")
with pytest.raises(CompatibilityProfileError, match="promote-profile"):
deploy_compatibility_profile(source, destination)
assert destination.read_bytes() == b"previous\n"
def test_deploy_publishes_the_exact_bytes_that_were_validated(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches validation followed by a second source read that can deploy changed bytes.
source = tmp_path / "incoming.json"
destination = tmp_path / "runs" / "compatibility-profile.json"
accepted = _valid_bytes()
source.write_bytes(accepted)
real_read_bytes = Path.read_bytes
def mutate_after_read(path: Path) -> bytes:
data = real_read_bytes(path)
if path == source:
source.write_bytes(b'{"bad":true}\n')
return data
monkeypatch.setattr(Path, "read_bytes", mutate_after_read)
deployed = deploy_compatibility_profile(source, destination)
assert deployed == destination
assert real_read_bytes(destination) == accepted
assert real_read_bytes(source) == b'{"bad":true}\n'
assert not list(destination.parent.glob(".*.tmp"))
@pytest.mark.skipif(os.name != "nt", reason="Windows handle-relative publication")
def test_deploy_publishes_validated_file_identity_after_temp_path_substitution(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches final publication reopening a substituted temporary path by name.
source = tmp_path / "source.json"
parent = tmp_path / "runs"
destination = parent / "compatibility-profile.json"
displaced = parent / "validated-displaced.tmp"
accepted = _valid_bytes()
attacker = b"attacker-controlled-path-bytes"
parent.mkdir()
source.write_bytes(accepted)
destination.write_bytes(b"previous-profile")
replacement_path: Path | None = None
def substitute_temp_path(_: Path, temporary: Path) -> None:
nonlocal replacement_path
temporary.rename(displaced)
temporary.write_bytes(attacker)
replacement_path = temporary
monkeypatch.setattr(
compatibility_profile_module,
"_profile_publication_hook",
substitute_temp_path,
)
deploy_compatibility_profile(source, destination)
assert destination.read_bytes() == accepted
assert replacement_path is not None
assert replacement_path.read_bytes() == attacker
assert not displaced.exists()
released = tmp_path / "released-runs"
parent.rename(released)
released.rename(parent)
@pytest.mark.skipif(os.name != "nt", reason="Windows handle-relative publication")
def test_deploy_failure_cleans_held_identity_without_unlinking_substituted_path(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches failure cleanup unlinking a later same-name file and leaking validated bytes.
source = tmp_path / "source.json"
parent = tmp_path / "runs"
destination = parent / "compatibility-profile.json"
displaced = parent / "validated-displaced.tmp"
attacker = b"external-path-bytes"
parent.mkdir()
source.write_bytes(_valid_bytes())
destination.write_bytes(b"previous-profile")
replacement_path: Path | None = None
def substitute_then_fail(_: Path, temporary: Path) -> None:
nonlocal replacement_path
temporary.rename(displaced)
temporary.write_bytes(attacker)
replacement_path = temporary
raise RuntimeError("publication stopped after substitution")
monkeypatch.setattr(
compatibility_profile_module,
"_profile_publication_hook",
substitute_then_fail,
)
with pytest.raises(RuntimeError, match="publication stopped after substitution"):
deploy_compatibility_profile(source, destination)
assert destination.read_bytes() == b"previous-profile"
assert replacement_path is not None
assert replacement_path.read_bytes() == attacker
assert not displaced.exists()
released = tmp_path / "released-runs"
parent.rename(released)
released.rename(parent)
def test_deploy_rejects_unsupported_platform_before_path_access(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches non-Windows deployment silently falling back to weaker path-only publication.
source = tmp_path / "missing-source.json"
destination = tmp_path / "must-not-exist" / "profile.json"
monkeypatch.setattr(
compatibility_profile_module,
"_WINDOWS_PROFILE_DEPLOYMENT",
False,
raising=False,
)
with pytest.raises(CompatibilityProfileError) as raised:
deploy_compatibility_profile(source, destination)
assert raised.value.code == "unsupported_platform"
assert not source.exists()
assert not destination.parent.exists()
@pytest.mark.parametrize(
"invalid",
(
b'{"format_version":1,"format_version":1}\n',
_valid_bytes().replace(b'"x": 0.5', b'"x": NaN', 1),
_mutated_bytes(("api_errors", 0, "number"), 2_147_483_648),
),
ids=("duplicate-key", "non-finite", "int32-overflow"),
)
def test_invalid_deploy_preserves_previous_destination_and_leaves_no_temp(
tmp_path: Path,
invalid: bytes,
) -> None:
source = tmp_path / "invalid.json"
destination = tmp_path / "runs" / "compatibility-profile.json"
destination.parent.mkdir()
destination.write_bytes(b"previous-profile")
source.write_bytes(invalid)
with pytest.raises(CompatibilityProfileError):
deploy_compatibility_profile(source, destination)
assert destination.read_bytes() == b"previous-profile"
assert not list(destination.parent.glob(".*.tmp"))
def test_atomic_publish_failure_preserves_destination_and_removes_temp(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
source = tmp_path / "profile.json"
destination = tmp_path / "runs" / "compatibility-profile.json"
destination.parent.mkdir()
source.write_bytes(_valid_bytes())
destination.write_bytes(b"previous-profile")
def fail_replace(_: object, __: object, ___: object) -> None:
raise OSError("simulated atomic publication failure")
monkeypatch.setattr(
compatibility_profile_module,
"_publish_staged_file",
fail_replace,
)
with pytest.raises(OSError, match="simulated atomic publication failure"):
deploy_compatibility_profile(source, destination)
assert destination.read_bytes() == b"previous-profile"
assert not list(destination.parent.glob(".*.tmp"))
@pytest.mark.skipif(os.name != "nt", reason="Windows junction coverage")
def test_deploy_rejects_a_junction_destination_before_replacing_profile(
tmp_path: Path,
) -> None:
# Catches the final atomic publisher following a reparse-point parent.
source = tmp_path / "source.json"
target = tmp_path / "outside-runs"
junction = tmp_path / "runs"
target.mkdir()
destination = target / "compatibility-profile.json"
destination.write_bytes(b"previous-profile")
source.write_bytes(_valid_bytes())
created = subprocess.run(
["cmd", "/c", "mklink", "/J", str(junction), str(target)],
check=False,
capture_output=True,
text=True,
)
if created.returncode != 0:
pytest.skip(f"junctions unavailable: {created.stderr or created.stdout}")
try:
with pytest.raises(CompatibilityProfileError, match="reparse"):
deploy_compatibility_profile(
source,
junction / "compatibility-profile.json",
)
finally:
junction.rmdir()
assert destination.read_bytes() == b"previous-profile"
assert not list(target.glob(".*.tmp"))
@pytest.mark.skipif(os.name != "nt", reason="Windows junction coverage")
def test_deploy_rechecks_destination_for_a_junction_before_publication(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches a destination-parent swap after the temporary file is written.
source = tmp_path / "source.json"
runs = tmp_path / "runs"
displaced = tmp_path / "original-runs"
target = tmp_path / "outside-runs"
destination = runs / "compatibility-profile.json"
outside_destination = target / destination.name
runs.mkdir()
target.mkdir()
destination.write_bytes(b"original-profile")
outside_destination.write_bytes(b"outside-profile")
source.write_bytes(_valid_bytes())
swap_attempted = False
junction_created = False
outside_temporary: Path | None = None
def swap_after_final_validation(
checked_destination: Path,
temporary: Path,
) -> None:
nonlocal swap_attempted, junction_created, outside_temporary
assert checked_destination == destination
swap_attempted = True
outside_temporary = target / temporary.name
outside_temporary.write_bytes(b"outside-temporary")
try:
runs.rename(displaced)
except OSError as error:
raise RuntimeError("directory swap blocked") from error
else:
created = subprocess.run(
["cmd", "/c", "mklink", "/J", str(runs), str(target)],
check=False,
capture_output=True,
text=True,
)
if created.returncode != 0:
pytest.fail(f"junction setup failed: {created.stderr or created.stdout}")
junction_created = True
raise RuntimeError("directory swap was not blocked")
monkeypatch.setattr(
compatibility_profile_module,
"_profile_publication_hook",
swap_after_final_validation,
raising=False,
)
try:
with pytest.raises(RuntimeError, match="directory swap blocked"):
deploy_compatibility_profile(source, destination)
finally:
if junction_created:
runs.rmdir()
displaced.rename(runs)
released = tmp_path / "released-runs"
runs.rename(released)
released.rename(runs)
assert swap_attempted is True
assert outside_destination.read_bytes() == b"outside-profile"
assert outside_temporary is not None
assert outside_temporary.read_bytes() == b"outside-temporary"
assert destination.read_bytes() == b"original-profile"
assert not displaced.exists()
assert not list(runs.glob(".*.tmp"))