File: test_document_validator.py

package info (click to toggle)
python-spdx-tools 0.8.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,084 kB
  • sloc: python: 18,675; xml: 12,553; sh: 46; makefile: 7
file content (152 lines) | stat: -rw-r--r-- 5,938 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import os
from typing import List, Optional

import pytest

from spdx_tools.spdx.constants import DOCUMENT_SPDX_ID
from spdx_tools.spdx.model import CreationInfo, Document, Relationship, RelationshipType
from spdx_tools.spdx.parser.parse_anything import parse_file
from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document
from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage
from tests.spdx.fixtures import creation_info_fixture, document_fixture, file_fixture, package_fixture, snippet_fixture


def test_valid_document():
    document = document_fixture()
    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

    assert validation_messages == []


def test_spdx_lite_validation():
    document = parse_file(os.path.join(os.path.dirname(__file__), "../data/SPDXLite.spdx"))

    assert validate_full_spdx_document(document) == []


@pytest.mark.parametrize(
    "creation_info, version_input, expected_message",
    [
        (creation_info_fixture(spdx_version="SPDX-2.3"), "SPDX-2.3", None),
        (creation_info_fixture(spdx_version="SPDX-2.3"), None, None),
        (
            creation_info_fixture(spdx_version="SPDX-2.3"),
            "SPDX-2.2",
            "provided SPDX version SPDX-2.2 does not match the document's SPDX version SPDX-2.3",
        ),
        (
            creation_info_fixture(spdx_version="SPDX-2.3"),
            "SPDX2.3",
            "provided SPDX version SPDX2.3 does not match the document's SPDX version SPDX-2.3",
        ),
        (
            creation_info_fixture(spdx_version="SPDX2.3"),
            "SPDX-2.3",
            'only SPDX versions "SPDX-2.2" and "SPDX-2.3" are supported, but the document\'s spdx_version is: SPDX2.3',
        ),
        (
            creation_info_fixture(spdx_version="SPDX2.3"),
            None,
            'only SPDX versions "SPDX-2.2" and "SPDX-2.3" are supported, but the document\'s spdx_version is: SPDX2.3',
        ),
        (
            creation_info_fixture(spdx_version="SPDX2.3"),
            "SPDX2.3",
            'only SPDX versions "SPDX-2.2" and "SPDX-2.3" are supported, but the document\'s spdx_version is: SPDX2.3',
        ),
        (
            creation_info_fixture(spdx_version="SPDX-2.1"),
            "SPDX-2.1",
            'only SPDX versions "SPDX-2.2" and "SPDX-2.3" are supported, but the document\'s '
            "spdx_version is: SPDX-2.1",
        ),
    ],
)
def test_spdx_version_handling(creation_info: CreationInfo, version_input: str, expected_message: Optional[str]):
    document: Document = document_fixture(creation_info=creation_info)
    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version_input)

    context = ValidationContext(spdx_id=creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)
    expected: List[ValidationMessage] = []

    if expected_message:
        expected.append(ValidationMessage(expected_message, context))
        expected.append(
            ValidationMessage(
                "There are issues concerning the SPDX version of the document. "
                "As subsequent validation relies on the correct version, "
                "the validation process has been cancelled.",
                context,
            )
        )

    assert validation_messages == expected


@pytest.mark.parametrize(
    "relationships",
    [
        [Relationship(DOCUMENT_SPDX_ID, RelationshipType.DESCRIBES, "SPDXRef-File")],
        [Relationship("SPDXRef-File", RelationshipType.DESCRIBED_BY, DOCUMENT_SPDX_ID)],
    ],
)
def test_document_describes_at_least_one_element(relationships):
    document = document_fixture(relationships=relationships)
    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

    assert validation_messages == []


def test_document_does_not_describe_an_element_with_only_one_package():
    document = document_fixture(
        packages=[package_fixture()],
        files=[],
        snippets=[],
        relationships=[],
        annotations=[],
    )
    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

    assert validation_messages == []


def test_document_does_not_describe_an_element_with_multiple_elements():
    document = document_fixture(
        relationships=[Relationship("SPDXRef-Package", RelationshipType.DESCRIBES, "SPDXRef-File")]
    )
    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

    assert validation_messages == [
        ValidationMessage(
            f'there must be at least one relationship "{DOCUMENT_SPDX_ID} DESCRIBES ..." or "... DESCRIBED_BY '
            f'{DOCUMENT_SPDX_ID}" when there is not only a single package present',
            ValidationContext(spdx_id=DOCUMENT_SPDX_ID, element_type=SpdxElementType.DOCUMENT),
        )
    ]


def test_duplicated_spdx_ids():
    document = document_fixture(
        files=[
            file_fixture(spdx_id="SPDXRef-File"),
            file_fixture(spdx_id="SPDXRef-2"),
            file_fixture(spdx_id="SPDXRef-3"),
        ],
        packages=[package_fixture(spdx_id="SPDXRef-2"), package_fixture(spdx_id=DOCUMENT_SPDX_ID)],
        snippets=[snippet_fixture(spdx_id="SPDXRef-2"), snippet_fixture(spdx_id="SPDXRef-3")],
    )

    context = ValidationContext(spdx_id=document.creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT)

    validation_messages: List[ValidationMessage] = validate_full_spdx_document(document)

    assert validation_messages == [
        ValidationMessage(
            "every spdx_id must be unique within the document, but found the following duplicates: ['SPDXRef-2', "
            f"'SPDXRef-3', '{DOCUMENT_SPDX_ID}']",
            context,
        )
    ]