• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package spdxlib
4
5import (
6	"testing"
7
8	"github.com/spdx/tools-golang/spdx/common"
9	"github.com/spdx/tools-golang/spdx/v2_1"
10	"github.com/spdx/tools-golang/spdx/v2_2"
11	"github.com/spdx/tools-golang/spdx/v2_3"
12)
13
14// ===== 2.1 tests =====
15
16func Test2_1ValidDocumentPassesValidation(t *testing.T) {
17	// set up document and some packages and relationships
18	doc := &v2_1.Document{
19		SPDXVersion:    "SPDX-2.1",
20		DataLicense:    "CC0-1.0",
21		SPDXIdentifier: common.ElementID("DOCUMENT"),
22		CreationInfo:   &v2_1.CreationInfo{},
23		Packages: []*v2_1.Package{
24			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
25			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
26			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
27			{PackageName: "pkg4", PackageSPDXIdentifier: "p4"},
28			{PackageName: "pkg5", PackageSPDXIdentifier: "p5"},
29		},
30		Relationships: []*v2_1.Relationship{
31			{
32				RefA:         common.MakeDocElementID("", "DOCUMENT"),
33				RefB:         common.MakeDocElementID("", "p1"),
34				Relationship: "DESCRIBES",
35			},
36			{
37				RefA:         common.MakeDocElementID("", "DOCUMENT"),
38				RefB:         common.MakeDocElementID("", "p5"),
39				Relationship: "DESCRIBES",
40			},
41			// inverse relationship -- should also get detected
42			{
43				RefA:         common.MakeDocElementID("", "p4"),
44				RefB:         common.MakeDocElementID("", "DOCUMENT"),
45				Relationship: "DESCRIBED_BY",
46			},
47			// different relationship
48			{
49				RefA:         common.MakeDocElementID("", "p1"),
50				RefB:         common.MakeDocElementID("", "p2"),
51				Relationship: "DEPENDS_ON",
52			},
53		},
54	}
55
56	err := ValidateDocument2_1(doc)
57	if err != nil {
58		t.Fatalf("expected nil error, got: %s", err.Error())
59	}
60}
61
62func Test2_1InvalidDocumentFailsValidation(t *testing.T) {
63	// set up document and some packages and relationships
64	doc := &v2_1.Document{
65		SPDXVersion:    "SPDX-2.1",
66		DataLicense:    "CC0-1.0",
67		SPDXIdentifier: common.ElementID("DOCUMENT"),
68		CreationInfo:   &v2_1.CreationInfo{},
69		Packages: []*v2_1.Package{
70			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
71			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
72			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
73		},
74		Relationships: []*v2_1.Relationship{
75			{
76				RefA:         common.MakeDocElementID("", "DOCUMENT"),
77				RefB:         common.MakeDocElementID("", "p1"),
78				Relationship: "DESCRIBES",
79			},
80			{
81				RefA:         common.MakeDocElementID("", "DOCUMENT"),
82				RefB:         common.MakeDocElementID("", "p2"),
83				Relationship: "DESCRIBES",
84			},
85			// invalid ID p99
86			{
87				RefA:         common.MakeDocElementID("", "p1"),
88				RefB:         common.MakeDocElementID("", "p99"),
89				Relationship: "DEPENDS_ON",
90			},
91		},
92	}
93
94	err := ValidateDocument2_1(doc)
95	if err == nil {
96		t.Fatalf("expected non-nil error, got nil")
97	}
98}
99
100// ===== 2.2 tests =====
101
102func Test2_2ValidDocumentPassesValidation(t *testing.T) {
103	// set up document and some packages and relationships
104	doc := &v2_2.Document{
105		SPDXVersion:    "SPDX-2.1",
106		DataLicense:    "CC0-1.0",
107		SPDXIdentifier: common.ElementID("DOCUMENT"),
108		CreationInfo:   &v2_2.CreationInfo{},
109		Packages: []*v2_2.Package{
110			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
111			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
112			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
113			{PackageName: "pkg4", PackageSPDXIdentifier: "p4"},
114			{PackageName: "pkg5", PackageSPDXIdentifier: "p5"},
115		},
116		Relationships: []*v2_2.Relationship{
117			{
118				RefA:         common.MakeDocElementID("", "DOCUMENT"),
119				RefB:         common.MakeDocElementID("", "p1"),
120				Relationship: "DESCRIBES",
121			},
122			{
123				RefA:         common.MakeDocElementID("", "DOCUMENT"),
124				RefB:         common.MakeDocElementID("", "p5"),
125				Relationship: "DESCRIBES",
126			},
127			// inverse relationship -- should also get detected
128			{
129				RefA:         common.MakeDocElementID("", "p4"),
130				RefB:         common.MakeDocElementID("", "DOCUMENT"),
131				Relationship: "DESCRIBED_BY",
132			},
133			// different relationship
134			{
135				RefA:         common.MakeDocElementID("", "p1"),
136				RefB:         common.MakeDocElementID("", "p2"),
137				Relationship: "DEPENDS_ON",
138			},
139		},
140	}
141
142	err := ValidateDocument2_2(doc)
143	if err != nil {
144		t.Fatalf("expected nil error, got: %s", err.Error())
145	}
146}
147
148func Test2_2InvalidDocumentFailsValidation(t *testing.T) {
149	// set up document and some packages and relationships
150	doc := &v2_2.Document{
151		SPDXVersion:    "SPDX-2.1",
152		DataLicense:    "CC0-1.0",
153		SPDXIdentifier: common.ElementID("DOCUMENT"),
154		CreationInfo:   &v2_2.CreationInfo{},
155		Packages: []*v2_2.Package{
156			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
157			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
158			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
159		},
160		Relationships: []*v2_2.Relationship{
161			{
162				RefA:         common.MakeDocElementID("", "DOCUMENT"),
163				RefB:         common.MakeDocElementID("", "p1"),
164				Relationship: "DESCRIBES",
165			},
166			{
167				RefA:         common.MakeDocElementID("", "DOCUMENT"),
168				RefB:         common.MakeDocElementID("", "p5"),
169				Relationship: "DESCRIBES",
170			},
171			// invalid ID p99
172			{
173				RefA:         common.MakeDocElementID("", "p1"),
174				RefB:         common.MakeDocElementID("", "p99"),
175				Relationship: "DEPENDS_ON",
176			},
177		},
178	}
179
180	err := ValidateDocument2_2(doc)
181	if err == nil {
182		t.Fatalf("expected non-nil error, got nil")
183	}
184}
185
186// ===== 2.3 tests =====
187
188func Test2_3ValidDocumentPassesValidation(t *testing.T) {
189	// set up document and some packages and relationships
190	doc := &v2_3.Document{
191		SPDXVersion:    "SPDX-2.3",
192		DataLicense:    "CC0-1.0",
193		SPDXIdentifier: common.ElementID("DOCUMENT"),
194		CreationInfo:   &v2_3.CreationInfo{},
195		Packages: []*v2_3.Package{
196			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
197			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
198			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
199			{PackageName: "pkg4", PackageSPDXIdentifier: "p4"},
200			{PackageName: "pkg5", PackageSPDXIdentifier: "p5"},
201		},
202		Relationships: []*v2_3.Relationship{
203			{
204				RefA:         common.MakeDocElementID("", "DOCUMENT"),
205				RefB:         common.MakeDocElementID("", "p1"),
206				Relationship: "DESCRIBES",
207			},
208			{
209				RefA:         common.MakeDocElementID("", "DOCUMENT"),
210				RefB:         common.MakeDocElementID("", "p5"),
211				Relationship: "DESCRIBES",
212			},
213			// inverse relationship -- should also get detected
214			{
215				RefA:         common.MakeDocElementID("", "p4"),
216				RefB:         common.MakeDocElementID("", "DOCUMENT"),
217				Relationship: "DESCRIBED_BY",
218			},
219			// different relationship
220			{
221				RefA:         common.MakeDocElementID("", "p1"),
222				RefB:         common.MakeDocElementID("", "p2"),
223				Relationship: "DEPENDS_ON",
224			},
225		},
226	}
227
228	err := ValidateDocument2_3(doc)
229	if err != nil {
230		t.Fatalf("expected nil error, got: %s", err.Error())
231	}
232}
233
234func Test2_3InvalidDocumentFailsValidation(t *testing.T) {
235	// set up document and some packages and relationships
236	doc := &v2_3.Document{
237		SPDXVersion:    "SPDX-2.3",
238		DataLicense:    "CC0-1.0",
239		SPDXIdentifier: common.ElementID("DOCUMENT"),
240		CreationInfo:   &v2_3.CreationInfo{},
241		Packages: []*v2_3.Package{
242			{PackageName: "pkg1", PackageSPDXIdentifier: "p1"},
243			{PackageName: "pkg2", PackageSPDXIdentifier: "p2"},
244			{PackageName: "pkg3", PackageSPDXIdentifier: "p3"},
245		},
246		Relationships: []*v2_3.Relationship{
247			{
248				RefA:         common.MakeDocElementID("", "DOCUMENT"),
249				RefB:         common.MakeDocElementID("", "p1"),
250				Relationship: "DESCRIBES",
251			},
252			{
253				RefA:         common.MakeDocElementID("", "DOCUMENT"),
254				RefB:         common.MakeDocElementID("", "p5"),
255				Relationship: "DESCRIBES",
256			},
257			// invalid ID p99
258			{
259				RefA:         common.MakeDocElementID("", "p1"),
260				RefB:         common.MakeDocElementID("", "p99"),
261				Relationship: "DEPENDS_ON",
262			},
263		},
264	}
265
266	err := ValidateDocument2_3(doc)
267	if err == nil {
268		t.Fatalf("expected non-nil error, got nil")
269	}
270}
271