xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v3/save_file_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v3
4
5import (
6	"bytes"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_3"
11)
12
13// ===== File section Saver tests =====
14func TestSaver2_3FileSavesText(t *testing.T) {
15	f := &v2_3.File{
16		FileName:           "/tmp/whatever.txt",
17		FileSPDXIdentifier: common.ElementID("File123"),
18		FileTypes: []string{
19			"TEXT",
20			"DOCUMENTATION",
21		},
22		Checksums: []common.Checksum{
23			{Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"},
24			{Algorithm: common.SHA256, Value: "11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd"},
25			{Algorithm: common.MD5, Value: "624c1abb3664f4b35547e7c73864ad24"},
26		},
27		LicenseConcluded: "Apache-2.0",
28		LicenseInfoInFiles: []string{
29			"Apache-2.0",
30			"Apache-1.1",
31		},
32		LicenseComments:   "this is a license comment(s)",
33		FileCopyrightText: "Copyright (c) Jane Doe",
34		ArtifactOfProjects: []*v2_3.ArtifactOfProject{
35			{
36				Name:     "project1",
37				HomePage: "http://example.com/1/",
38				URI:      "http://example.com/1/uri.whatever",
39			},
40			{
41				Name: "project2",
42			},
43			{
44				Name:     "project3",
45				HomePage: "http://example.com/3/",
46			},
47			{
48				Name: "project4",
49				URI:  "http://example.com/4/uri.whatever",
50			},
51		},
52		FileComment: "this is a file comment",
53		FileNotice:  "This file may be used under either Apache-2.0 or Apache-1.1.",
54		FileContributors: []string{
55			"John Doe [email protected]",
56			"EvilCorp",
57		},
58		FileAttributionTexts: []string{
59			"attributions",
60			`multi-line
61attribution`,
62		},
63		FileDependencies: []string{
64			"f-1.txt",
65			"g.txt",
66		},
67	}
68
69	// what we want to get, as a buffer of bytes
70	want := bytes.NewBufferString(`FileName: /tmp/whatever.txt
71SPDXID: SPDXRef-File123
72FileType: TEXT
73FileType: DOCUMENTATION
74FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
75FileChecksum: SHA256: 11b6d3ee554eedf79299905a98f9b9a04e498210b59f15094c916c91d150efcd
76FileChecksum: MD5: 624c1abb3664f4b35547e7c73864ad24
77LicenseConcluded: Apache-2.0
78LicenseInfoInFile: Apache-2.0
79LicenseInfoInFile: Apache-1.1
80LicenseComments: this is a license comment(s)
81FileCopyrightText: Copyright (c) Jane Doe
82ArtifactOfProjectName: project1
83ArtifactOfProjectHomePage: http://example.com/1/
84ArtifactOfProjectURI: http://example.com/1/uri.whatever
85ArtifactOfProjectName: project2
86ArtifactOfProjectName: project3
87ArtifactOfProjectHomePage: http://example.com/3/
88ArtifactOfProjectName: project4
89ArtifactOfProjectURI: http://example.com/4/uri.whatever
90FileComment: this is a file comment
91FileNotice: This file may be used under either Apache-2.0 or Apache-1.1.
92FileContributor: John Doe jdoe@example.com
93FileContributor: EvilCorp
94FileAttributionText: attributions
95FileAttributionText: <text>multi-line
96attribution</text>
97FileDependency: f-1.txt
98FileDependency: g.txt
99
100`)
101
102	// render as buffer of bytes
103	var got bytes.Buffer
104	err := renderFile2_3(f, &got)
105	if err != nil {
106		t.Errorf("Expected nil error, got %v", err)
107	}
108
109	// check that they match
110	c := bytes.Compare(want.Bytes(), got.Bytes())
111	if c != 0 {
112		t.Errorf("Expected %v, got %v", want.String(), got.String())
113	}
114}
115
116func TestSaver2_3FileSavesSnippetsAlso(t *testing.T) {
117	sn1 := &v2_3.Snippet{
118		SnippetSPDXIdentifier:         common.ElementID("Snippet19"),
119		SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID,
120		Ranges:                        []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 17}, EndPointer: common.SnippetRangePointer{Offset: 209}}},
121		SnippetLicenseConcluded:       "GPL-2.0-or-later",
122		SnippetCopyrightText:          "Copyright (c) John Doe 20x6",
123	}
124
125	sn2 := &v2_3.Snippet{
126		SnippetSPDXIdentifier:         common.ElementID("Snippet20"),
127		SnippetFromFileSPDXIdentifier: common.MakeDocElementID("", "File123").ElementRefID,
128		Ranges:                        []common.SnippetRange{{StartPointer: common.SnippetRangePointer{Offset: 268}, EndPointer: common.SnippetRangePointer{Offset: 309}}},
129		SnippetLicenseConcluded:       "WTFPL",
130		SnippetCopyrightText:          "NOASSERTION",
131	}
132
133	sns := map[common.ElementID]*v2_3.Snippet{
134		common.ElementID("Snippet19"): sn1,
135		common.ElementID("Snippet20"): sn2,
136	}
137
138	f := &v2_3.File{
139		FileName:           "/tmp/whatever.txt",
140		FileSPDXIdentifier: common.ElementID("File123"),
141		Checksums: []common.Checksum{
142			{Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"},
143		},
144		LicenseConcluded: "Apache-2.0",
145		LicenseInfoInFiles: []string{
146			"Apache-2.0",
147		},
148		FileCopyrightText: "Copyright (c) Jane Doe",
149		Snippets:          sns,
150	}
151
152	// what we want to get, as a buffer of bytes
153	want := bytes.NewBufferString(`FileName: /tmp/whatever.txt
154SPDXID: SPDXRef-File123
155FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
156LicenseConcluded: Apache-2.0
157LicenseInfoInFile: Apache-2.0
158FileCopyrightText: Copyright (c) Jane Doe
159
160SnippetSPDXID: SPDXRef-Snippet19
161SnippetFromFileSPDXID: SPDXRef-File123
162SnippetByteRange: 17:209
163SnippetLicenseConcluded: GPL-2.0-or-later
164SnippetCopyrightText: Copyright (c) John Doe 20x6
165
166SnippetSPDXID: SPDXRef-Snippet20
167SnippetFromFileSPDXID: SPDXRef-File123
168SnippetByteRange: 268:309
169SnippetLicenseConcluded: WTFPL
170SnippetCopyrightText: NOASSERTION
171
172`)
173
174	// render as buffer of bytes
175	var got bytes.Buffer
176	err := renderFile2_3(f, &got)
177	if err != nil {
178		t.Errorf("Expected nil error, got %v", err)
179	}
180
181	// check that they match
182	c := bytes.Compare(want.Bytes(), got.Bytes())
183	if c != 0 {
184		t.Errorf("Expected %v, got %v", want.String(), got.String())
185	}
186}
187
188func TestSaver2_3FileOmitsOptionalFieldsIfEmpty(t *testing.T) {
189	f := &v2_3.File{
190		FileName:           "/tmp/whatever.txt",
191		FileSPDXIdentifier: common.ElementID("File123"),
192		Checksums: []common.Checksum{
193			{Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"},
194		},
195		LicenseConcluded: "Apache-2.0",
196		LicenseInfoInFiles: []string{
197			"Apache-2.0",
198		},
199		FileCopyrightText: "Copyright (c) Jane Doe",
200	}
201
202	// what we want to get, as a buffer of bytes
203	want := bytes.NewBufferString(`FileName: /tmp/whatever.txt
204SPDXID: SPDXRef-File123
205FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
206LicenseConcluded: Apache-2.0
207LicenseInfoInFile: Apache-2.0
208FileCopyrightText: Copyright (c) Jane Doe
209
210`)
211
212	// render as buffer of bytes
213	var got bytes.Buffer
214	err := renderFile2_3(f, &got)
215	if err != nil {
216		t.Errorf("Expected nil error, got %v", err)
217	}
218
219	// check that they match
220	c := bytes.Compare(want.Bytes(), got.Bytes())
221	if c != 0 {
222		t.Errorf("Expected %v, got %v", want.String(), got.String())
223	}
224}
225
226func TestSaver2_3FileWrapsCopyrightMultiLine(t *testing.T) {
227	f := &v2_3.File{
228		FileName:           "/tmp/whatever.txt",
229		FileSPDXIdentifier: common.ElementID("File123"),
230		Checksums: []common.Checksum{
231			{Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"},
232		},
233		LicenseConcluded: "Apache-2.0",
234		LicenseInfoInFiles: []string{
235			"Apache-2.0",
236		},
237		FileCopyrightText: `Copyright (c) Jane Doe
238Copyright (c) John Doe`,
239	}
240
241	// what we want to get, as a buffer of bytes
242	want := bytes.NewBufferString(`FileName: /tmp/whatever.txt
243SPDXID: SPDXRef-File123
244FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
245LicenseConcluded: Apache-2.0
246LicenseInfoInFile: Apache-2.0
247FileCopyrightText: <text>Copyright (c) Jane Doe
248Copyright (c) John Doe</text>
249
250`)
251
252	// render as buffer of bytes
253	var got bytes.Buffer
254	err := renderFile2_3(f, &got)
255	if err != nil {
256		t.Errorf("Expected nil error, got %v", err)
257	}
258
259	// check that they match
260	c := bytes.Compare(want.Bytes(), got.Bytes())
261	if c != 0 {
262		t.Errorf("Expected %v, got %v", want.String(), got.String())
263	}
264}
265
266func TestSaver2_3FileWrapsCommentsAndNoticesMultiLine(t *testing.T) {
267	f := &v2_3.File{
268		FileName:           "/tmp/whatever.txt",
269		FileSPDXIdentifier: common.ElementID("File123"),
270		Checksums: []common.Checksum{
271			{Algorithm: common.SHA1, Value: "85ed0817af83a24ad8da68c2b5094de69833983c"},
272		},
273		LicenseComments: `this is a
274multi-line license comment`,
275		LicenseConcluded: "Apache-2.0",
276		LicenseInfoInFiles: []string{
277			"Apache-2.0",
278		},
279		FileCopyrightText: "Copyright (c) Jane Doe",
280		FileComment: `this is a
281multi-line file comment`,
282		FileNotice: `This file may be used
283under either Apache-2.0 or Apache-1.1.`,
284	}
285
286	// what we want to get, as a buffer of bytes
287	want := bytes.NewBufferString(`FileName: /tmp/whatever.txt
288SPDXID: SPDXRef-File123
289FileChecksum: SHA1: 85ed0817af83a24ad8da68c2b5094de69833983c
290LicenseConcluded: Apache-2.0
291LicenseInfoInFile: Apache-2.0
292LicenseComments: <text>this is a
293multi-line license comment</text>
294FileCopyrightText: Copyright (c) Jane Doe
295FileComment: <text>this is a
296multi-line file comment</text>
297FileNotice: <text>This file may be used
298under either Apache-2.0 or Apache-1.1.</text>
299
300`)
301
302	// render as buffer of bytes
303	var got bytes.Buffer
304	err := renderFile2_3(f, &got)
305	if err != nil {
306		t.Errorf("Expected nil error, got %v", err)
307	}
308
309	// check that they match
310	c := bytes.Compare(want.Bytes(), got.Bytes())
311	if c != 0 {
312		t.Errorf("Expected %v, got %v", want.String(), got.String())
313	}
314}
315