• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2package parser2v2
3
4import (
5	"testing"
6
7	"github.com/spdx/tools-golang/spdx/v2_2"
8)
9
10// ===== Parser other license section state change tests =====
11func TestParser2_2OLStartsNewOtherLicenseAfterParsingLicenseIDTag(t *testing.T) {
12	// create the first other license
13	olid1 := "LicenseRef-Lic11"
14	olname1 := "License 11"
15
16	parser := tvParser2_2{
17		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
18		st:   psOtherLicense2_2,
19		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
20		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
21		otherLic: &v2_2.OtherLicense{
22			LicenseIdentifier: olid1,
23			LicenseName:       olname1,
24		},
25	}
26	olic1 := parser.otherLic
27	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
28	parser.pkg.Files = append(parser.pkg.Files, parser.file)
29	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
30
31	// the Document's OtherLicenses should have this one only
32	if parser.doc.OtherLicenses[0] != olic1 {
33		t.Errorf("Expected other license %v in OtherLicenses[0], got %v", olic1, parser.doc.OtherLicenses[0])
34	}
35	if parser.doc.OtherLicenses[0].LicenseName != olname1 {
36		t.Errorf("expected other license name %s in OtherLicenses[0], got %s", olname1, parser.doc.OtherLicenses[0].LicenseName)
37	}
38
39	// now add a new other license
40	olid2 := "LicenseRef-22"
41	olname2 := "License 22"
42	err := parser.parsePair2_2("LicenseID", olid2)
43	if err != nil {
44		t.Errorf("got error when calling parsePair2_2: %v", err)
45	}
46	// state should be correct
47	if parser.st != psOtherLicense2_2 {
48		t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st)
49	}
50	// and an other license should be created
51	if parser.otherLic == nil {
52		t.Fatalf("parser didn't create new other license")
53	}
54	// also parse the new license's name
55	err = parser.parsePair2_2("LicenseName", olname2)
56	if err != nil {
57		t.Errorf("got error when calling parsePair2_2: %v", err)
58	}
59	// state should still be correct
60	if parser.st != psOtherLicense2_2 {
61		t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st)
62	}
63	// and the other license name should be as expected
64	if parser.otherLic.LicenseName != olname2 {
65		t.Errorf("expected other license name %s, got %s", olname2, parser.otherLic.LicenseName)
66	}
67	// and the Document's Other Licenses should be of size 2 and have these two
68	if len(parser.doc.OtherLicenses) != 2 {
69		t.Fatalf("Expected OtherLicenses to have len 2, got %d", len(parser.doc.OtherLicenses))
70	}
71	if parser.doc.OtherLicenses[0] != olic1 {
72		t.Errorf("Expected other license %v in OtherLicenses[0], got %v", olic1, parser.doc.OtherLicenses[0])
73	}
74	if parser.doc.OtherLicenses[0].LicenseIdentifier != olid1 {
75		t.Errorf("expected other license ID %s in OtherLicenses[0], got %s", olid1, parser.doc.OtherLicenses[0].LicenseIdentifier)
76	}
77	if parser.doc.OtherLicenses[0].LicenseName != olname1 {
78		t.Errorf("expected other license name %s in OtherLicenses[0], got %s", olname1, parser.doc.OtherLicenses[0].LicenseName)
79	}
80	if parser.doc.OtherLicenses[1] != parser.otherLic {
81		t.Errorf("Expected other license %v in OtherLicenses[1], got %v", parser.otherLic, parser.doc.OtherLicenses[1])
82	}
83	if parser.doc.OtherLicenses[1].LicenseIdentifier != olid2 {
84		t.Errorf("expected other license ID %s in OtherLicenses[1], got %s", olid2, parser.doc.OtherLicenses[1].LicenseIdentifier)
85	}
86	if parser.doc.OtherLicenses[1].LicenseName != olname2 {
87		t.Errorf("expected other license name %s in OtherLicenses[1], got %s", olname2, parser.doc.OtherLicenses[1].LicenseName)
88	}
89}
90
91func TestParser2_2OLMovesToReviewAfterParsingReviewerTag(t *testing.T) {
92	parser := tvParser2_2{
93		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
94		st:   psOtherLicense2_2,
95		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
96		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
97	}
98	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
99	parser.pkg.Files = append(parser.pkg.Files, parser.file)
100	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
101
102	err := parser.parsePair2_2("Reviewer", "Person: John Doe")
103	if err != nil {
104		t.Errorf("got error when calling parsePair2_2: %v", err)
105	}
106	if parser.st != psReview2_2 {
107		t.Errorf("expected state to be %v, got %v", psReview2_2, parser.st)
108	}
109}
110
111func TestParser2_2OtherLicenseStaysAfterParsingRelationshipTags(t *testing.T) {
112	parser := tvParser2_2{
113		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
114		st:   psOtherLicense2_2,
115		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
116		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
117		otherLic: &v2_2.OtherLicense{
118			LicenseIdentifier: "LicenseRef-whatever",
119			LicenseName:       "the whatever license",
120		},
121	}
122	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
123	parser.pkg.Files = append(parser.pkg.Files, parser.file)
124	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
125
126	err := parser.parsePair2_2("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else")
127	if err != nil {
128		t.Errorf("got error when calling parsePair2_2: %v", err)
129	}
130	// state should remain unchanged
131	if parser.st != psOtherLicense2_2 {
132		t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st)
133	}
134	// and the relationship should be in the Document's Relationships
135	if len(parser.doc.Relationships) != 1 {
136		t.Fatalf("expected doc.Relationships to have len 1, got %d", len(parser.doc.Relationships))
137	}
138	deID := parser.doc.Relationships[0].RefA
139	if deID.DocumentRefID != "" || deID.ElementRefID != "blah" {
140		t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA)
141	}
142
143	err = parser.parsePair2_2("RelationshipComment", "blah")
144	if err != nil {
145		t.Errorf("got error when calling parsePair2_2: %v", err)
146	}
147	// state should still remain unchanged
148	if parser.st != psOtherLicense2_2 {
149		t.Errorf("expected state to be %v, got %v", psOtherLicense2_2, parser.st)
150	}
151}
152
153func TestParser2_2OtherLicenseStaysAfterParsingAnnotationTags(t *testing.T) {
154	parser := tvParser2_2{
155		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
156		st:   psOtherLicense2_2,
157		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
158		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
159		otherLic: &v2_2.OtherLicense{
160			LicenseIdentifier: "LicenseRef-whatever",
161			LicenseName:       "the whatever license",
162		},
163	}
164	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
165	parser.pkg.Files = append(parser.pkg.Files, parser.file)
166	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
167
168	err := parser.parsePair2_2("Annotator", "Person: John Doe ()")
169	if err != nil {
170		t.Errorf("got error when calling parsePair2_2: %v", err)
171	}
172	if parser.st != psOtherLicense2_2 {
173		t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2)
174	}
175
176	err = parser.parsePair2_2("AnnotationDate", "2018-09-15T00:36:00Z")
177	if err != nil {
178		t.Errorf("got error when calling parsePair2_2: %v", err)
179	}
180	if parser.st != psOtherLicense2_2 {
181		t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2)
182	}
183
184	err = parser.parsePair2_2("AnnotationType", "REVIEW")
185	if err != nil {
186		t.Errorf("got error when calling parsePair2_2: %v", err)
187	}
188	if parser.st != psOtherLicense2_2 {
189		t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2)
190	}
191
192	err = parser.parsePair2_2("SPDXREF", "SPDXRef-45")
193	if err != nil {
194		t.Errorf("got error when calling parsePair2_2: %v", err)
195	}
196	if parser.st != psOtherLicense2_2 {
197		t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2)
198	}
199
200	err = parser.parsePair2_2("AnnotationComment", "i guess i had something to say about this particular file")
201	if err != nil {
202		t.Errorf("got error when calling parsePair2_2: %v", err)
203	}
204	if parser.st != psOtherLicense2_2 {
205		t.Errorf("parser is in state %v, expected %v", parser.st, psOtherLicense2_2)
206	}
207
208	// and the annotation should be in the Document's Annotations
209	if len(parser.doc.Annotations) != 1 {
210		t.Fatalf("expected doc.Annotations to have len 1, got %d", len(parser.doc.Annotations))
211	}
212	if parser.doc.Annotations[0].Annotator.Annotator != "John Doe ()" {
213		t.Errorf("expected Annotator to be %s, got %s", "John Doe ()", parser.doc.Annotations[0].Annotator)
214	}
215}
216
217func TestParser2_2OLFailsAfterParsingOtherSectionTags(t *testing.T) {
218	parser := tvParser2_2{
219		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
220		st:   psOtherLicense2_2,
221		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
222		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
223		otherLic: &v2_2.OtherLicense{
224			LicenseIdentifier: "LicenseRef-Lic11",
225			LicenseName:       "License 11",
226		},
227	}
228	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
229	parser.pkg.Files = append(parser.pkg.Files, parser.file)
230	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
231
232	// can't go back to old sections
233	err := parser.parsePair2_2("SPDXVersion", "SPDX-2.2")
234	if err == nil {
235		t.Errorf("expected error when calling parsePair2_2, got nil")
236	}
237	err = parser.parsePair2_2("PackageName", "whatever")
238	if err == nil {
239		t.Errorf("expected error when calling parsePair2_2, got nil")
240	}
241	err = parser.parsePair2_2("FileName", "whatever")
242	if err == nil {
243		t.Errorf("expected error when calling parsePair2_2, got nil")
244	}
245}
246
247// ===== Other License data section tests =====
248func TestParser2_2CanParseOtherLicenseTags(t *testing.T) {
249	parser := tvParser2_2{
250		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
251		st:   psOtherLicense2_2,
252		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
253		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
254	}
255	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
256	parser.pkg.Files = append(parser.pkg.Files, parser.file)
257	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
258
259	// License Identifier
260	err := parser.parsePairFromOtherLicense2_2("LicenseID", "LicenseRef-Lic11")
261	if err != nil {
262		t.Errorf("expected nil error, got %v", err)
263	}
264	if parser.otherLic.LicenseIdentifier != "LicenseRef-Lic11" {
265		t.Errorf("got %v for LicenseID", parser.otherLic.LicenseIdentifier)
266	}
267
268	// Extracted Text
269	err = parser.parsePairFromOtherLicense2_2("ExtractedText", "You are permitted to do anything with the software, hooray!")
270	if err != nil {
271		t.Errorf("expected nil error, got %v", err)
272	}
273	if parser.otherLic.ExtractedText != "You are permitted to do anything with the software, hooray!" {
274		t.Errorf("got %v for ExtractedText", parser.otherLic.ExtractedText)
275	}
276
277	// License Name
278	err = parser.parsePairFromOtherLicense2_2("LicenseName", "License 11")
279	if err != nil {
280		t.Errorf("expected nil error, got %v", err)
281	}
282	if parser.otherLic.LicenseName != "License 11" {
283		t.Errorf("got %v for LicenseName", parser.otherLic.LicenseName)
284	}
285
286	// License Cross Reference
287	crossRefs := []string{
288		"https://example.com/1",
289		"https://example.com/2",
290		"https://example.com/3",
291	}
292	for _, cr := range crossRefs {
293		err = parser.parsePairFromOtherLicense2_2("LicenseCrossReference", cr)
294		if err != nil {
295			t.Errorf("expected nil error, got %v", err)
296		}
297	}
298	for _, refWant := range crossRefs {
299		flagFound := false
300		for _, refCheck := range parser.otherLic.LicenseCrossReferences {
301			if refWant == refCheck {
302				flagFound = true
303			}
304		}
305		if flagFound == false {
306			t.Errorf("didn't find %s in LicenseCrossReferences", refWant)
307		}
308	}
309	if len(crossRefs) != len(parser.otherLic.LicenseCrossReferences) {
310		t.Errorf("expected %d types in LicenseCrossReferences, got %d", len(crossRefs),
311			len(parser.otherLic.LicenseCrossReferences))
312	}
313
314	// License Comment
315	err = parser.parsePairFromOtherLicense2_2("LicenseComment", "this is a comment")
316	if err != nil {
317		t.Errorf("expected nil error, got %v", err)
318	}
319	if parser.otherLic.LicenseComment != "this is a comment" {
320		t.Errorf("got %v for LicenseComment", parser.otherLic.LicenseComment)
321	}
322}
323
324func TestParser2_2OLUnknownTagFails(t *testing.T) {
325	parser := tvParser2_2{
326		doc:  &v2_2.Document{Packages: []*v2_2.Package{}},
327		st:   psOtherLicense2_2,
328		pkg:  &v2_2.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_2.File{}},
329		file: &v2_2.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"},
330	}
331	parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
332	parser.pkg.Files = append(parser.pkg.Files, parser.file)
333	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic)
334
335	err := parser.parsePairFromOtherLicense2_2("blah", "something")
336	if err == nil {
337		t.Errorf("expected error from parsing unknown tag")
338	}
339}
340