|
| 1 | +# Copyright 2024 The Sigstore Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for binary signing payloads. |
| 16 | +
|
| 17 | +NOTE: This test uses a goldens setup to compare expected results with data from |
| 18 | +files. If the golden tests are failing, regenerate the golden files with |
| 19 | +
|
| 20 | + pytest model_signing/ --update_goldens |
| 21 | +""" |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from model_signing import test_support |
| 26 | +from model_signing.hashing import file |
| 27 | +from model_signing.hashing import memory |
| 28 | +from model_signing.serialization import serialize_by_file |
| 29 | +from model_signing.signing import as_bytes |
| 30 | + |
| 31 | + |
| 32 | +class TestBytesPayload: |
| 33 | + |
| 34 | + @pytest.mark.parametrize("model_fixture_name", test_support.all_test_models) |
| 35 | + def test_known_models(self, request, model_fixture_name): |
| 36 | + # Set up variables (arrange) |
| 37 | + testdata_path = request.path.parent / "testdata" |
| 38 | + test_path = testdata_path / "as_bytes" |
| 39 | + test_class_path = test_path / "TestBytesPayload" |
| 40 | + golden_path = test_class_path / model_fixture_name |
| 41 | + should_update = request.config.getoption("update_goldens") |
| 42 | + model = request.getfixturevalue(model_fixture_name) |
| 43 | + |
| 44 | + # Compute payload (act) |
| 45 | + file_hasher = file.SimpleFileHasher( |
| 46 | + test_support.UNUSED_PATH, memory.SHA256() |
| 47 | + ) |
| 48 | + serializer = serialize_by_file.DigestSerializer( |
| 49 | + file_hasher, memory.SHA256, allow_symlinks=True |
| 50 | + ) |
| 51 | + manifest = serializer.serialize(model) |
| 52 | + payload = as_bytes.BytesPayload.from_manifest(manifest) |
| 53 | + |
| 54 | + # Compare with golden, or write to golden (approximately "assert") |
| 55 | + if should_update: |
| 56 | + with open(golden_path, "w", encoding="utf-8") as f: |
| 57 | + f.write(f"{payload.digest.hex()}\n") |
| 58 | + else: |
| 59 | + with open(golden_path, "r", encoding="utf-8") as f: |
| 60 | + expected_bytes = bytes.fromhex(f.read().strip()) |
| 61 | + |
| 62 | + assert payload.digest == expected_bytes |
| 63 | + |
| 64 | + def test_only_runs_on_expected_manifest_types(self, sample_model_folder): |
| 65 | + serializer = serialize_by_file.ManifestSerializer( |
| 66 | + lambda f: file.SimpleFileHasher(f, memory.SHA256()), |
| 67 | + allow_symlinks=True, |
| 68 | + ) |
| 69 | + manifest = serializer.serialize(sample_model_folder) |
| 70 | + |
| 71 | + with pytest.raises(TypeError, match="Only DigestManifest is supported"): |
| 72 | + as_bytes.BytesPayload.from_manifest(manifest) |
0 commit comments