xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v1/save_document_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v1
4
5import (
6	"bytes"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_1"
11)
12
13// ===== entire Document Saver tests =====
14func TestSaver2_1DocumentSavesText(t *testing.T) {
15
16	// Creation Info section
17	ci := &v2_1.CreationInfo{
18		Creators: []common.Creator{
19			{Creator: "John Doe", CreatorType: "Person"},
20		},
21		Created: "2018-10-10T06:20:00Z",
22	}
23
24	// unpackaged files
25	f1 := &v2_1.File{
26		FileName:           "/tmp/whatever1.txt",
27		FileSPDXIdentifier: common.ElementID("File1231"),
28		Checksums:          []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983c", Algorithm: common.SHA1}},
29		LicenseConcluded:   "Apache-2.0",
30		LicenseInfoInFiles: []string{"Apache-2.0"},
31		FileCopyrightText:  "Copyright (c) Jane Doe",
32	}
33
34	f2 := &v2_1.File{
35		FileName:           "/tmp/whatever2.txt",
36		FileSPDXIdentifier: common.ElementID("File1232"),
37		Checksums:          []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983d", Algorithm: common.SHA1}},
38		LicenseConcluded:   "MIT",
39		LicenseInfoInFiles: []string{"MIT"},
40		FileCopyrightText:  "Copyright (c) John Doe",
41	}
42
43	unFiles := []*v2_1.File{
44		f1,
45		f2,
46	}
47
48	// Package 1: packaged files with snippets
49	sn1 := &v2_1.Snippet{
50		SnippetSPDXIdentifier:         "Snippet19",
51		SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID,
52		Ranges:                        []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}},
53		SnippetLicenseConcluded:       "GPL-2.0-or-later",
54		SnippetCopyrightText:          "Copyright (c) John Doe 20x6",
55	}
56
57	sn2 := &v2_1.Snippet{
58		SnippetSPDXIdentifier:         "Snippet20",
59		SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "FileHasSnippets").ElementRefID,
60		Ranges:                        []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}},
61		SnippetLicenseConcluded:       "WTFPL",
62		SnippetCopyrightText:          "NOASSERTION",
63	}
64
65	f3 := &v2_1.File{
66		FileName:           "/tmp/file-with-snippets.txt",
67		FileSPDXIdentifier: common.ElementID("FileHasSnippets"),
68		Checksums:          []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983e", Algorithm: common.SHA1}},
69		LicenseConcluded:   "GPL-2.0-or-later AND WTFPL",
70		LicenseInfoInFiles: []string{
71			"Apache-2.0",
72			"GPL-2.0-or-later",
73			"WTFPL",
74		},
75		FileCopyrightText: "Copyright (c) Jane Doe",
76		Snippets: map[common.ElementID]*v2_1.Snippet{
77			common.ElementID("Snippet19"): sn1,
78			common.ElementID("Snippet20"): sn2,
79		},
80	}
81
82	f4 := &v2_1.File{
83		FileName:           "/tmp/another-file.txt",
84		FileSPDXIdentifier: common.ElementID("FileAnother"),
85		Checksums:          []common.Checksum{{Value: "85ed0817af83a24ad8da68c2b5094de69833983f", Algorithm: common.SHA1}},
86		LicenseConcluded:   "BSD-3-Clause",
87		LicenseInfoInFiles: []string{"BSD-3-Clause"},
88		FileCopyrightText:  "Copyright (c) Jane Doe LLC",
89	}
90
91	pkgWith := &v2_1.Package{
92		PackageName:               "p1",
93		PackageSPDXIdentifier:     common.ElementID("p1"),
94		PackageDownloadLocation:   "http://example.com/p1/p1-0.1.0-master.tar.gz",
95		FilesAnalyzed:             true,
96		IsFilesAnalyzedTagPresent: true,
97		PackageVerificationCode:   common.PackageVerificationCode{Value: "0123456789abcdef0123456789abcdef01234567"},
98		PackageLicenseConcluded:   "GPL-2.0-or-later AND BSD-3-Clause AND WTFPL",
99		PackageLicenseInfoFromFiles: []string{
100			"Apache-2.0",
101			"GPL-2.0-or-later",
102			"WTFPL",
103			"BSD-3-Clause",
104		},
105		PackageLicenseDeclared: "Apache-2.0 OR GPL-2.0-or-later",
106		PackageCopyrightText:   "Copyright (c) John Doe, Inc.",
107		Files: []*v2_1.File{
108			f3,
109			f4,
110		},
111	}
112
113	// Other Licenses 1 and 2
114	ol1 := &v2_1.OtherLicense{
115		LicenseIdentifier: "LicenseRef-1",
116		ExtractedText: `License 1 text
117blah blah blah
118blah blah blah blah`,
119		LicenseName: "License 1",
120	}
121
122	ol2 := &v2_1.OtherLicense{
123		LicenseIdentifier: "LicenseRef-2",
124		ExtractedText:     `License 2 text - this is a license that does some stuff`,
125		LicenseName:       "License 2",
126	}
127
128	// Relationships
129	rln1 := &v2_1.Relationship{
130		RefA:         common.MakeDocElementID("", "DOCUMENT"),
131		RefB:         common.MakeDocElementID("", "p1"),
132		Relationship: "DESCRIBES",
133	}
134
135	rln2 := &v2_1.Relationship{
136		RefA:         common.MakeDocElementID("", "DOCUMENT"),
137		RefB:         common.MakeDocElementID("", "File1231"),
138		Relationship: "DESCRIBES",
139	}
140
141	rln3 := &v2_1.Relationship{
142		RefA:         common.MakeDocElementID("", "DOCUMENT"),
143		RefB:         common.MakeDocElementID("", "File1232"),
144		Relationship: "DESCRIBES",
145	}
146
147	// Annotations
148	ann1 := &v2_1.Annotation{
149		Annotator: common.Annotator{Annotator: "John Doe",
150			AnnotatorType: "Person"},
151		AnnotationDate:           "2018-10-10T17:52:00Z",
152		AnnotationType:           "REVIEW",
153		AnnotationSPDXIdentifier: common.MakeDocElementID("", "DOCUMENT"),
154		AnnotationComment:        "This is an annotation about the SPDX document",
155	}
156
157	ann2 := &v2_1.Annotation{
158		Annotator: common.Annotator{Annotator: "John Doe, Inc.",
159			AnnotatorType: "Organization"},
160		AnnotationDate:           "2018-10-10T17:52:00Z",
161		AnnotationType:           "REVIEW",
162		AnnotationSPDXIdentifier: common.MakeDocElementID("", "p1"),
163		AnnotationComment:        "This is an annotation about Package p1",
164	}
165
166	// Reviews
167	rev1 := &v2_1.Review{
168		Reviewer:     "John Doe",
169		ReviewerType: "Person",
170		ReviewDate:   "2018-10-14T10:28:00Z",
171	}
172	rev2 := &v2_1.Review{
173		Reviewer:      "Jane Doe LLC",
174		ReviewerType:  "Organization",
175		ReviewDate:    "2018-10-14T10:28:00Z",
176		ReviewComment: "I have reviewed this SPDX document and it is awesome",
177	}
178
179	// now, build the document
180	doc := &v2_1.Document{
181		SPDXVersion:       "SPDX-2.1",
182		DataLicense:       "CC0-1.0",
183		SPDXIdentifier:    common.ElementID("DOCUMENT"),
184		DocumentName:      "spdx-go-0.0.1.abcdef",
185		DocumentNamespace: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1.abcdef.whatever",
186		CreationInfo:      ci,
187		Packages: []*v2_1.Package{
188			pkgWith,
189		},
190		Files: unFiles,
191		OtherLicenses: []*v2_1.OtherLicense{
192			ol1,
193			ol2,
194		},
195		Relationships: []*v2_1.Relationship{
196			rln1,
197			rln2,
198			rln3,
199		},
200		Annotations: []*v2_1.Annotation{
201			ann1,
202			ann2,
203		},
204		Reviews: []*v2_1.Review{
205			rev1,
206			rev2,
207		},
208	}
209
210	want := bytes.NewBufferString(`SPDXVersion: SPDX-2.1
211DataLicense: CC0-1.0
212SPDXID: SPDXRef-DOCUMENT
213DocumentName: spdx-go-0.0.1.abcdef
214DocumentNamespace: https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1.abcdef.whatever
215Creator: Person: John Doe
216Created: 2018-10-10T06:20:00Z
217
218##### Unpackaged files
219
220FileName: /tmp/whatever1.txt
221SPDXID: SPDXRef-File1231
222FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
223LicenseConcluded: Apache-2.0
224LicenseInfoInFile: Apache-2.0
225FileCopyrightText: Copyright (c) Jane Doe
226
227FileName: /tmp/whatever2.txt
228SPDXID: SPDXRef-File1232
229FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983d
230LicenseConcluded: MIT
231LicenseInfoInFile: MIT
232FileCopyrightText: Copyright (c) John Doe
233
234##### Package: p1
235
236PackageName: p1
237SPDXID: SPDXRef-p1
238PackageDownloadLocation: http://example.com/p1/p1-0.1.0-master.tar.gz
239FilesAnalyzed: true
240PackageVerificationCode: 0123456789abcdef0123456789abcdef01234567
241PackageLicenseConcluded: GPL-2.0-or-later AND BSD-3-Clause AND WTFPL
242PackageLicenseInfoFromFiles: Apache-2.0
243PackageLicenseInfoFromFiles: GPL-2.0-or-later
244PackageLicenseInfoFromFiles: WTFPL
245PackageLicenseInfoFromFiles: BSD-3-Clause
246PackageLicenseDeclared: Apache-2.0 OR GPL-2.0-or-later
247PackageCopyrightText: Copyright (c) John Doe, Inc.
248
249FileName: /tmp/another-file.txt
250SPDXID: SPDXRef-FileAnother
251FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983f
252LicenseConcluded: BSD-3-Clause
253LicenseInfoInFile: BSD-3-Clause
254FileCopyrightText: Copyright (c) Jane Doe LLC
255
256FileName: /tmp/file-with-snippets.txt
257SPDXID: SPDXRef-FileHasSnippets
258FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983e
259LicenseConcluded: GPL-2.0-or-later AND WTFPL
260LicenseInfoInFile: Apache-2.0
261LicenseInfoInFile: GPL-2.0-or-later
262LicenseInfoInFile: WTFPL
263FileCopyrightText: Copyright (c) Jane Doe
264
265SnippetSPDXID: SPDXRef-Snippet19
266SnippetFromFileSPDXID: SPDXRef-FileHasSnippets
267SnippetByteRange: 17:209
268SnippetLicenseConcluded: GPL-2.0-or-later
269SnippetCopyrightText: Copyright (c) John Doe 20x6
270
271SnippetSPDXID: SPDXRef-Snippet20
272SnippetFromFileSPDXID: SPDXRef-FileHasSnippets
273SnippetByteRange: 268:309
274SnippetLicenseConcluded: WTFPL
275SnippetCopyrightText: NOASSERTION
276
277##### Other Licenses
278
279LicenseID: LicenseRef-1
280ExtractedText: <text>License 1 text
281blah blah blah
282blah blah blah blah</text>
283LicenseName: License 1
284
285LicenseID: LicenseRef-2
286ExtractedText: License 2 text - this is a license that does some stuff
287LicenseName: License 2
288
289##### Relationships
290
291Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-p1
292Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-File1231
293Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-File1232
294
295##### Annotations
296
297Annotator: Person: John Doe
298AnnotationDate: 2018-10-10T17:52:00Z
299AnnotationType: REVIEW
300SPDXREF: SPDXRef-DOCUMENT
301AnnotationComment: This is an annotation about the SPDX document
302
303Annotator: Organization: John Doe, Inc.
304AnnotationDate: 2018-10-10T17:52:00Z
305AnnotationType: REVIEW
306SPDXREF: SPDXRef-p1
307AnnotationComment: This is an annotation about Package p1
308
309##### Reviews
310
311Reviewer: Person: John Doe
312ReviewDate: 2018-10-14T10:28:00Z
313
314Reviewer: Organization: Jane Doe LLC
315ReviewDate: 2018-10-14T10:28:00Z
316ReviewComment: I have reviewed this SPDX document and it is awesome
317
318`)
319
320	// render as buffer of bytes
321	var got bytes.Buffer
322	err := RenderDocument2_1(doc, &got)
323	if err != nil {
324		t.Errorf("Expected nil error, got %v", err)
325	}
326
327	// check that they match
328	c := bytes.Compare(want.Bytes(), got.Bytes())
329	if c != 0 {
330		t.Errorf("Expected {{{%v}}}, got {{{%v}}}", want.String(), got.String())
331	}
332
333}
334
335func TestSaver2_1DocumentReturnsErrorIfNilCreationInfo(t *testing.T) {
336	doc := &v2_1.Document{}
337
338	var got bytes.Buffer
339	err := RenderDocument2_1(doc, &got)
340	if err == nil {
341		t.Errorf("Expected error, got nil")
342	}
343}
344