xref: /aosp_15_r20/external/spdx-tools/rdfloader/parser2v3/license_utils_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package parser2v3
4
5import (
6	"reflect"
7	"testing"
8)
9
10func Test_getLicenseStringFromURI(t *testing.T) {
11	// TestCase 1: NONE license
12	input := SPDX_NONE_CAPS
13	output := getLicenseStringFromURI(input)
14	expectedOutput := "NONE"
15	if output != expectedOutput {
16		t.Errorf("expected: %s, found %s", expectedOutput, output)
17	}
18
19	// TestCase 2: NOASSERTION license
20	input = SPDX_NOASSERTION_SMALL
21	output = getLicenseStringFromURI(input)
22	expectedOutput = "NOASSERTION"
23	if output != expectedOutput {
24		t.Errorf("expected: %s, found %s", expectedOutput, output)
25	}
26
27	// TestCase 3: Other license
28	input = NS_SPDX + "LicenseRef-1"
29	output = getLicenseStringFromURI(input)
30	expectedOutput = "LicenseRef-1"
31	if output != expectedOutput {
32		t.Errorf("expected: %s, found %s", expectedOutput, output)
33	}
34}
35
36func Test_rdfParser2_3_getChecksumFromNode(t *testing.T) {
37	var parser *rdfParser2_3
38	var err error
39	// TestCase 1: invalid checksum algorithm
40	parser, _ = parserFromBodyContent(`
41		<spdx:Checksum>
42			<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
43			<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha999"/>
44		</spdx:Checksum>
45	`)
46	checksumNode := parser.gordfParserObj.Triples[0].Subject
47	_, _, err = parser.getChecksumFromNode(checksumNode)
48	if err == nil {
49		t.Errorf("expected an error saying invalid checksum algorithm")
50	}
51
52	// TestCase 2: invalid predicate
53	parser, _ = parserFromBodyContent(`
54		<spdx:Checksum>
55			<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
56			<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/>
57			<spdx:invalidPredicate />
58		</spdx:Checksum>
59	`)
60	checksumNode = parser.gordfParserObj.Triples[0].Subject
61	_, _, err = parser.getChecksumFromNode(checksumNode)
62	if err == nil {
63		t.Errorf("expected an error saying invalid predicate")
64	}
65
66	// TestCase 3: valid input
67	parser, _ = parserFromBodyContent(`
68		<spdx:Checksum>
69			<spdx:checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</spdx:checksumValue>
70			<spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/>
71		</spdx:Checksum>
72	`)
73	checksumNode = parser.gordfParserObj.Triples[0].Subject
74	algorithm, value, err := parser.getChecksumFromNode(checksumNode)
75	if err != nil {
76		t.Errorf("unexpected error: %v", err)
77	}
78	if algorithm != "SHA1" {
79		t.Errorf("expected checksum algorithm to be sha1, found %s", algorithm)
80	}
81	expectedValue := "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
82	if value != expectedValue {
83		t.Errorf("expected checksumValue to be %s, found %s", expectedValue, value)
84	}
85}
86
87func Test_rdfParser2_3_getAlgorithmFromURI(t *testing.T) {
88	var algorithmURI string
89	var err error
90
91	// TestCase 1: checksumAlgorithm uri doesn't start with checksumAlgorithm_
92	algorithmURI = NS_SPDX + "sha1"
93	_, err = getAlgorithmFromURI(algorithmURI)
94	if err == nil {
95		t.Errorf("should've raised an error for algorithmURI that doesn't start with checksumAlgorithm_")
96	}
97
98	// TestCase 2: unknown checksum algorithm
99	algorithmURI = NS_SPDX + "checksumAlgorithm_sha999"
100	_, err = getAlgorithmFromURI(algorithmURI)
101	if err == nil {
102		t.Errorf("should've raised an error for invalid algorithm")
103	}
104
105	// TestCase 3: valid input
106	algorithmURI = NS_SPDX + "checksumAlgorithm_sha256"
107	algorithm, err := getAlgorithmFromURI(algorithmURI)
108	if err != nil {
109		t.Errorf("unexpected error: %v", err)
110	}
111	if algorithm != "SHA256" {
112		t.Errorf("expected: SHA256, found: %s", algorithm)
113	}
114}
115
116func Test_mapLicensesToStrings(t *testing.T) {
117	// nothing much to test here.
118	// just a dummy dry run.
119	licenses := []AnyLicenseInfo{
120		SpecialLicense{
121			value: NONE,
122		},
123		SpecialLicense{
124			value: NOASSERTION,
125		},
126	}
127	licenseStrings := mapLicensesToStrings(licenses)
128	expectedLicenseStrings := []string{"NONE", "NOASSERTION"}
129	if !reflect.DeepEqual(licenseStrings, expectedLicenseStrings) {
130		t.Errorf("expected: %+v\nfound %+v", expectedLicenseStrings, licenseStrings)
131	}
132}
133
134func TestConjunctiveLicenseSet_ToLicenseString(t *testing.T) {
135	var lic ConjunctiveLicenseSet
136	var output, expectedOutput string
137
138	// TestCase 1: no license in the set
139	lic = ConjunctiveLicenseSet{
140		members: nil,
141	}
142	output = lic.ToLicenseString()
143	expectedOutput = ""
144	if output != expectedOutput {
145		t.Errorf("expected: %s, found %s", output, expectedOutput)
146	}
147
148	// TestCase 2: single license in the set
149	lic = ConjunctiveLicenseSet{
150		members: []AnyLicenseInfo{
151			SpecialLicense{value: NOASSERTION},
152		},
153	}
154	output = lic.ToLicenseString()
155	expectedOutput = "NOASSERTION"
156	if output != expectedOutput {
157		t.Errorf("expected: %s, found %s", output, expectedOutput)
158	}
159
160	// TestCase 3: more than one license in the set.
161	lic = ConjunctiveLicenseSet{
162		members: []AnyLicenseInfo{
163			SpecialLicense{value: NOASSERTION},
164			SpecialLicense{value: NONE},
165		},
166	}
167	output = lic.ToLicenseString()
168	expectedOutput = "NOASSERTION AND NONE"
169	if output != expectedOutput {
170		t.Errorf("expected: %s, found %s", output, expectedOutput)
171	}
172
173	// TestCase 4: nested conjunctive license.
174	lic = ConjunctiveLicenseSet{
175		members: []AnyLicenseInfo{
176			SpecialLicense{value: NOASSERTION},
177			ConjunctiveLicenseSet{
178				members: []AnyLicenseInfo{
179					SpecialLicense{value: "LicenseRef-1"},
180					SpecialLicense{value: NONE},
181				},
182			},
183		},
184	}
185	output = lic.ToLicenseString()
186	expectedOutput = "NOASSERTION AND LicenseRef-1 AND NONE"
187	if output != expectedOutput {
188		t.Errorf("expected: %s, found %s", output, expectedOutput)
189	}
190}
191
192func TestDisjunctiveLicenseSet_ToLicenseString(t *testing.T) {
193	var lic DisjunctiveLicenseSet
194	var output, expectedOutput string
195
196	// TestCase 1: no license in the set
197	lic = DisjunctiveLicenseSet{
198		members: nil,
199	}
200	output = lic.ToLicenseString()
201	expectedOutput = ""
202	if output != expectedOutput {
203		t.Errorf("expected: %s, found %s", output, expectedOutput)
204	}
205
206	// TestCase 2: single license in the set
207	lic = DisjunctiveLicenseSet{
208		members: []AnyLicenseInfo{
209			SpecialLicense{value: NOASSERTION},
210		},
211	}
212	output = lic.ToLicenseString()
213	expectedOutput = "NOASSERTION"
214	if output != expectedOutput {
215		t.Errorf("expected: %s, found %s", output, expectedOutput)
216	}
217
218	// TestCase 3: more than one license in the set.
219	lic = DisjunctiveLicenseSet{
220		members: []AnyLicenseInfo{
221			SpecialLicense{value: NOASSERTION},
222			SpecialLicense{value: NONE},
223		},
224	}
225	output = lic.ToLicenseString()
226	expectedOutput = "NOASSERTION OR NONE"
227	if output != expectedOutput {
228		t.Errorf("expected: %s, found %s", output, expectedOutput)
229	}
230
231	// TestCase 4: nested conjunctive license.
232	lic = DisjunctiveLicenseSet{
233		members: []AnyLicenseInfo{
234			SpecialLicense{value: NOASSERTION},
235			DisjunctiveLicenseSet{
236				members: []AnyLicenseInfo{
237					SpecialLicense{value: "LicenseRef-1"},
238					SpecialLicense{value: NONE},
239				},
240			},
241		},
242	}
243	output = lic.ToLicenseString()
244	expectedOutput = "NOASSERTION OR LicenseRef-1 OR NONE"
245	if output != expectedOutput {
246		t.Errorf("expected: %s, found %s", output, expectedOutput)
247	}
248}
249
250func TestExtractedLicensingInfo_ToLicenseString(t *testing.T) {
251	// nothing to test (just a dry run)
252	extractedLicense := ExtractedLicensingInfo{
253		SimpleLicensingInfo: SimpleLicensingInfo{
254			licenseID: "license",
255		},
256		extractedText: "extracted Text",
257	}
258	expectedOutput := "license"
259	output := extractedLicense.ToLicenseString()
260	if output != expectedOutput {
261		t.Errorf("expected: %s, found: %s", expectedOutput, output)
262	}
263}
264
265func TestOrLaterOperator_ToLicenseString(t *testing.T) {
266	// nothing to test (just a dry run)
267	orLater := OrLaterOperator{
268		member: SimpleLicensingInfo{
269			licenseID: "license",
270		},
271	}
272	expectedOutput := "license"
273	output := orLater.ToLicenseString()
274	if output != expectedOutput {
275		t.Errorf("expected: %s, found: %s", expectedOutput, output)
276	}
277}
278
279func TestLicense_ToLicenseString(t *testing.T) {
280	// nothing to test (just a dry run)
281	license := License{
282		SimpleLicensingInfo: SimpleLicensingInfo{
283			licenseID: "license",
284		},
285	}
286	expectedOutput := "license"
287	output := license.ToLicenseString()
288	if output != expectedOutput {
289		t.Errorf("expected: %s, found: %s", expectedOutput, output)
290	}
291}
292
293func TestListedLicense_ToLicenseString(t *testing.T) {
294	// nothing to test (just a dry run)
295	ll := ListedLicense{License{
296		SimpleLicensingInfo: SimpleLicensingInfo{
297			licenseID: "license",
298		},
299	},
300	}
301	expectedOutput := "license"
302	output := ll.ToLicenseString()
303	if output != expectedOutput {
304		t.Errorf("expected: %s, found: %s", expectedOutput, output)
305	}
306}
307
308func TestWithExceptionOperator_ToLicenseString(t *testing.T) {
309	// nothing to test (just a dry run)
310	withException := WithExceptionOperator{
311		member: SimpleLicensingInfo{
312			licenseID: "license",
313		},
314		licenseException: LicenseException{},
315	}
316	expectedOutput := "license"
317	output := withException.ToLicenseString()
318	if output != expectedOutput {
319		t.Errorf("expected: %s, found: %s", expectedOutput, output)
320	}
321}
322
323func TestSpecialLicense_ToLicenseString(t *testing.T) {
324	// nothing to test (just a dry run)
325	specialLicense := SpecialLicense{
326		value: "license",
327	}
328	expectedOutput := "license"
329	output := specialLicense.ToLicenseString()
330	if output != expectedOutput {
331		t.Errorf("expected: %s, found: %s", expectedOutput, output)
332	}
333}
334
335func TestSimpleLicensingInfo_ToLicenseString(t *testing.T) {
336	// nothing to test (just a dry run)
337	sli := SimpleLicensingInfo{
338		licenseID: "license",
339	}
340	expectedOutput := "license"
341	output := sli.ToLicenseString()
342	if output != expectedOutput {
343		t.Errorf("expected: %s, found: %s", expectedOutput, output)
344	}
345}
346