|
| 1 | +# SPDX-FileCopyrightText: 2023 spdx contributors |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +import pytest |
| 5 | + |
| 6 | +from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, File |
| 7 | +from spdx_tools.spdx.spdx_element_utils import calculate_file_checksum, calculate_package_verification_code |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def generate_test_files(tmp_path): |
| 12 | + file_path_1 = tmp_path.joinpath("file1") |
| 13 | + file_path_2 = tmp_path.joinpath("file2") |
| 14 | + |
| 15 | + with open(file_path_1, "wb") as file: |
| 16 | + file.write(bytes(111)) |
| 17 | + with open(file_path_2, "wb") as file: |
| 18 | + file.write(bytes(222)) |
| 19 | + |
| 20 | + yield str(file_path_1), str(file_path_2) |
| 21 | + |
| 22 | + |
| 23 | +def test_file_checksum_calculation(generate_test_files): |
| 24 | + filepath1, filepath2 = generate_test_files |
| 25 | + checksum = calculate_file_checksum(filepath1, ChecksumAlgorithm.SHA1) |
| 26 | + assert checksum == "dd90903d2f566a3922979dd5e18378a075c7ed33" |
| 27 | + checksum = calculate_file_checksum(filepath2, ChecksumAlgorithm.SHA1) |
| 28 | + assert checksum == "140dc52658e2eeee3fdc4d471cce84fec7253fe3" |
| 29 | + |
| 30 | + |
| 31 | +def test_verification_code_calculation_with_predefined_checksums(generate_test_files): |
| 32 | + filepath1, filepath2 = generate_test_files |
| 33 | + file1 = File( |
| 34 | + filepath1, |
| 35 | + "SPDXRef-hello", |
| 36 | + [Checksum(ChecksumAlgorithm.SHA1, "20862a6d08391d07d09344029533ec644fac6b21")], |
| 37 | + ) |
| 38 | + file2 = File( |
| 39 | + filepath2, |
| 40 | + "SPDXRef-Makefile", |
| 41 | + [Checksum(ChecksumAlgorithm.SHA1, "69a2e85696fff1865c3f0686d6c3824b59915c80")], |
| 42 | + ) |
| 43 | + verification_code = calculate_package_verification_code([file1, file2]) |
| 44 | + |
| 45 | + assert verification_code == "c6cb0949d7cd7439fce8690262a0946374824639" |
| 46 | + |
| 47 | + |
| 48 | +def test_verification_code_calculation_with_calculated_checksums(generate_test_files): |
| 49 | + filepath1, filepath2 = generate_test_files |
| 50 | + file1 = File( |
| 51 | + filepath1, |
| 52 | + "SPDXRef-hello", |
| 53 | + [Checksum(ChecksumAlgorithm.MD4, "20862a6d08391d07d09344029533ec644fac6b21")], |
| 54 | + ) |
| 55 | + file2 = File( |
| 56 | + filepath2, |
| 57 | + "SPDXRef-Makefile", |
| 58 | + [Checksum(ChecksumAlgorithm.MD4, "69a2e85696fff1865c3f0686d6c3824b59915c80")], |
| 59 | + ) |
| 60 | + verification_code = calculate_package_verification_code([file1, file2]) |
| 61 | + |
| 62 | + assert verification_code == "6f29d813abb63ee52a47dbcb691ea2e70f956328" |
| 63 | + |
| 64 | + |
| 65 | +def test_verification_code_calculation_with_wrong_file_location(): |
| 66 | + unknown_file_name = "./unknown_file_name" |
| 67 | + file1 = File( |
| 68 | + unknown_file_name, |
| 69 | + "SPDXRef-unknown", |
| 70 | + [Checksum(ChecksumAlgorithm.MD4, "20862a6d08391d07d09344029533ec644fac6b21")], |
| 71 | + ) |
| 72 | + |
| 73 | + with pytest.raises(FileNotFoundError) as err: |
| 74 | + calculate_package_verification_code([file1]) |
| 75 | + |
| 76 | + assert unknown_file_name in str(err.value) |
0 commit comments