test_ffi_notices.py (5634B)
1 from __future__ import annotations 2 3 import copy 4 import hashlib 5 import os 6 import subprocess 7 import sys 8 import tempfile 9 import unittest 10 from pathlib import Path 11 from unittest.mock import patch 12 13 SCRIPTS = Path(__file__).resolve().parent 14 if str(SCRIPTS) not in sys.path: 15 sys.path.insert(0, str(SCRIPTS)) 16 17 import ffi_notices as notices # noqa: E402 18 import ffi_source as source # noqa: E402 19 import package_contract as contract # noqa: E402 20 21 22 class RedistributionTests(unittest.TestCase): 23 def setUp(self) -> None: 24 temporary = tempfile.TemporaryDirectory() 25 self.addCleanup(temporary.cleanup) 26 self.root = Path(temporary.name).resolve() 27 self.package = self.root / "provider" 28 self.package.mkdir() 29 (self.package / "Cargo.toml").write_text("# synthetic metadata fixture\n") 30 self.data = b"synthetic complete redistribution material\n" 31 self.path = self.package / "NOTICE" 32 self.path.write_bytes(self.data) 33 self.foundation = {"repository": contract.LIB_REMOTE, "revision": "b" * 40} 34 self.metadata = { 35 "packages": [ 36 { 37 "name": "radroots_domain", 38 "version": "0.1.0-alpha", 39 "source": f"git+{contract.LIB_REMOTE}?rev={'b' * 40}#{'b' * 40}", 40 "manifest_path": str(self.package / "Cargo.toml"), 41 } 42 ] 43 } 44 45 def capture(self) -> dict: 46 with ( 47 patch.object(contract, "_cargo_workspace", return_value=self.metadata), 48 patch.dict( 49 notices.CONTRACT, {"sha256": hashlib.sha256(self.data).hexdigest()} 50 ), 51 ): 52 return notices.capture(self.root, self.foundation) 53 54 def test_exact_selected_material_is_copied_without_transformation(self) -> None: 55 binding = self.capture() 56 with ( 57 patch.object(contract, "_cargo_workspace", return_value=self.metadata), 58 patch.dict(notices.CONTRACT, {"sha256": binding["sha256"]}), 59 ): 60 destination = self.root / "candidate" 61 notices.install(self.root, destination, binding, self.foundation) 62 self.assertEqual((destination / notices.ARTIFACT_PATH).read_bytes(), self.data) 63 self.assertEqual(notices.verify_material(self.path, binding), self.data) 64 65 def test_provider_must_be_unique_and_exact_git_source(self) -> None: 66 original = copy.deepcopy(self.metadata["packages"][0]) 67 for changes in ( 68 {"source": None}, 69 {"source": "registry+synthetic"}, 70 {"version": "0.2.0"}, 71 ): 72 self.metadata["packages"] = [{**original, **changes}] 73 with self.assertRaises(source.ProvenanceError): 74 self.capture() 75 for packages in ([], [original, original]): 76 self.metadata["packages"] = packages 77 with self.assertRaisesRegex(source.ProvenanceError, "ambiguous"): 78 self.capture() 79 80 def test_empty_oversized_non_utf8_and_symlink_material_is_rejected(self) -> None: 81 for data in (b"", b"x" * (notices.MAX_BYTES + 1), b"\xff"): 82 self.path.write_bytes(data) 83 with self.assertRaises(source.ProvenanceError): 84 notices.read_material(self.path) 85 self.path.unlink() 86 self.path.symlink_to(self.package / "Cargo.toml") 87 with self.assertRaisesRegex(source.ProvenanceError, "unavailable"): 88 notices.read_material(self.path) 89 90 def test_truncation_and_same_length_substitution_are_rejected(self) -> None: 91 binding = self.capture() 92 for data in (self.data[:-1], b"x" * len(self.data)): 93 self.path.write_bytes(data) 94 with self.assertRaisesRegex(source.ProvenanceError, "differs"): 95 notices.verify_material(self.path, binding) 96 97 def test_non_regular_material_is_rejected_without_waiting_for_a_writer( 98 self, 99 ) -> None: 100 self.path.unlink() 101 os.mkfifo(self.path) 102 result = subprocess.run( 103 [ 104 sys.executable, 105 "-c", 106 "from pathlib import Path; import ffi_notices; " 107 "ffi_notices.read_material(Path(__import__('sys').argv[1]))", 108 str(self.path), 109 ], 110 cwd=SCRIPTS, 111 stdin=subprocess.DEVNULL, 112 capture_output=True, 113 timeout=5, 114 check=False, 115 ) 116 self.assertNotEqual(result.returncode, 0) 117 self.assertIn(b"redistribution material is not bounded", result.stderr) 118 119 def test_binding_and_inventory_cannot_change_notice_identity(self) -> None: 120 binding = self.capture() 121 row = { 122 "path": notices.ARTIFACT_PATH, 123 "bytes": binding["bytes"], 124 "sha256": binding["sha256"], 125 } 126 notices.verify_inventory([row], binding, notices.ARTIFACT_PATH) 127 for files in ([], [row, row], [{**row, "sha256": "f" * 64}]): 128 with self.assertRaises(contract.PackageContractError): 129 notices.verify_inventory(files, binding, notices.ARTIFACT_PATH) 130 with self.assertRaises(contract.PackageContractError): 131 notices.verify_expected( 132 binding, 133 {**self.foundation, "revision": "c" * 40}, 134 {"sha256": binding["sha256"]}, 135 ) 136 with self.assertRaises(contract.PackageContractError): 137 notices.validate_contract( 138 {**notices.CONTRACT, "maximum_bytes": notices.MAX_BYTES + 1} 139 ) 140 141 142 if __name__ == "__main__": 143 unittest.main()