• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package reporter
4
5import (
6	"bytes"
7	"testing"
8
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 Reporter top-level function tests =====
15func Test2_1ReporterCanMakeReportFromPackage(t *testing.T) {
16	pkg := &v2_1.Package{
17		FilesAnalyzed: true,
18		Files: []*v2_1.File{
19			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
20			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
21			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
22			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
23			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
24			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
25			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
26			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
27			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
28			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
29			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
30			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
31		},
32	}
33
34	// what we want to get, as a buffer of bytes
35	want := bytes.NewBufferString(`   9  License found
36   3  License not found
37  12  TOTAL
38
39  5  GPL-2.0-only
40  4  MIT
41  9  TOTAL FOUND
42`)
43
44	// render as buffer of bytes
45	var got bytes.Buffer
46	err := Generate2_1(pkg, &got)
47	if err != nil {
48		t.Errorf("Expected nil error, got %v", err)
49	}
50
51	// check that they match
52	c := bytes.Compare(want.Bytes(), got.Bytes())
53	if c != 0 {
54		t.Errorf("Expected %v, got %v", want.String(), got.String())
55	}
56}
57
58func Test2_1ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) {
59	pkg := &v2_1.Package{
60		FilesAnalyzed: false,
61	}
62
63	// render as buffer of bytes
64	var got bytes.Buffer
65	err := Generate2_1(pkg, &got)
66	if err == nil {
67		t.Errorf("Expected non-nil error, got nil")
68	}
69}
70
71// ===== 2.1 Utility functions =====
72
73func Test2_1CanGetCountsOfLicenses(t *testing.T) {
74	pkg := &v2_1.Package{
75		FilesAnalyzed: true,
76		Files: []*v2_1.File{
77			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
78			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
79			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
80			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
81			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
82			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
83			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
84			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
85			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
86			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
87			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
88			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
89		},
90	}
91
92	totalFound, totalNotFound, foundCounts := countLicenses2_1(pkg)
93	if totalFound != 9 {
94		t.Errorf("expected %v, got %v", 9, totalFound)
95	}
96	if totalNotFound != 3 {
97		t.Errorf("expected %v, got %v", 3, totalNotFound)
98	}
99	if len(foundCounts) != 2 {
100		t.Fatalf("expected %v, got %v", 2, len(foundCounts))
101	}
102
103	// foundCounts is a map of license ID to count of licenses
104	// confirm that the results are as expected
105	if foundCounts["GPL-2.0-only"] != 5 {
106		t.Errorf("expected %v, got %v", 5, foundCounts["GPL-2.0-only"])
107	}
108	if foundCounts["MIT"] != 4 {
109		t.Errorf("expected %v, got %v", 4, foundCounts["MIT"])
110	}
111}
112
113func Test2_1NilPackageReturnsZeroCountsOfLicenses(t *testing.T) {
114	totalFound, totalNotFound, foundCounts := countLicenses2_1(nil)
115	if totalFound != 0 {
116		t.Errorf("expected %v, got %v", 0, totalFound)
117	}
118	if totalNotFound != 0 {
119		t.Errorf("expected %v, got %v", 0, totalNotFound)
120	}
121	if len(foundCounts) != 0 {
122		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
123	}
124
125	pkg := &v2_1.Package{}
126	totalFound, totalNotFound, foundCounts = countLicenses2_1(pkg)
127	if totalFound != 0 {
128		t.Errorf("expected %v, got %v", 0, totalFound)
129	}
130	if totalNotFound != 0 {
131		t.Errorf("expected %v, got %v", 0, totalNotFound)
132	}
133	if len(foundCounts) != 0 {
134		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
135	}
136}
137
138// ===== 2.2 Reporter top-level function tests =====
139func Test2_2ReporterCanMakeReportFromPackage(t *testing.T) {
140	pkg := &v2_2.Package{
141		FilesAnalyzed: true,
142		Files: []*v2_2.File{
143			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
144			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
145			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
146			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
147			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
148			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
149			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
150			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
151			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
152			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
153			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
154			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
155		},
156	}
157
158	// what we want to get, as a buffer of bytes
159	want := bytes.NewBufferString(`   9  License found
160   3  License not found
161  12  TOTAL
162
163  5  GPL-2.0-only
164  4  MIT
165  9  TOTAL FOUND
166`)
167
168	// render as buffer of bytes
169	var got bytes.Buffer
170	err := Generate2_2(pkg, &got)
171	if err != nil {
172		t.Errorf("Expected nil error, got %v", err)
173	}
174
175	// check that they match
176	c := bytes.Compare(want.Bytes(), got.Bytes())
177	if c != 0 {
178		t.Errorf("Expected %v, got %v", want.String(), got.String())
179	}
180}
181
182func Test2_2ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) {
183	pkg := &v2_2.Package{
184		FilesAnalyzed: false,
185	}
186
187	// render as buffer of bytes
188	var got bytes.Buffer
189	err := Generate2_2(pkg, &got)
190	if err == nil {
191		t.Errorf("Expected non-nil error, got nil")
192	}
193}
194
195// ===== 2.2 Utility functions =====
196
197func Test2_2CanGetCountsOfLicenses(t *testing.T) {
198	pkg := &v2_2.Package{
199		FilesAnalyzed: true,
200		Files: []*v2_2.File{
201			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
202			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
203			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
204			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
205			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
206			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
207			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
208			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
209			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
210			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
211			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
212			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
213		},
214	}
215
216	totalFound, totalNotFound, foundCounts := countLicenses2_2(pkg)
217	if totalFound != 9 {
218		t.Errorf("expected %v, got %v", 9, totalFound)
219	}
220	if totalNotFound != 3 {
221		t.Errorf("expected %v, got %v", 3, totalNotFound)
222	}
223	if len(foundCounts) != 2 {
224		t.Fatalf("expected %v, got %v", 2, len(foundCounts))
225	}
226
227	// foundCounts is a map of license ID to count of licenses
228	// confirm that the results are as expected
229	if foundCounts["GPL-2.0-only"] != 5 {
230		t.Errorf("expected %v, got %v", 5, foundCounts["GPL-2.0-only"])
231	}
232	if foundCounts["MIT"] != 4 {
233		t.Errorf("expected %v, got %v", 4, foundCounts["MIT"])
234	}
235}
236
237func Test2_2NilPackageReturnsZeroCountsOfLicenses(t *testing.T) {
238	totalFound, totalNotFound, foundCounts := countLicenses2_2(nil)
239	if totalFound != 0 {
240		t.Errorf("expected %v, got %v", 0, totalFound)
241	}
242	if totalNotFound != 0 {
243		t.Errorf("expected %v, got %v", 0, totalNotFound)
244	}
245	if len(foundCounts) != 0 {
246		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
247	}
248
249	pkg := &v2_2.Package{}
250	totalFound, totalNotFound, foundCounts = countLicenses2_2(pkg)
251	if totalFound != 0 {
252		t.Errorf("expected %v, got %v", 0, totalFound)
253	}
254	if totalNotFound != 0 {
255		t.Errorf("expected %v, got %v", 0, totalNotFound)
256	}
257	if len(foundCounts) != 0 {
258		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
259	}
260}
261
262// ===== 2.3 Reporter top-level function tests =====
263func Test2_3ReporterCanMakeReportFromPackage(t *testing.T) {
264	pkg := &v2_3.Package{
265		FilesAnalyzed: true,
266		Files: []*v2_3.File{
267			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
268			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
269			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
270			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
271			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
272			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
273			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
274			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
275			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
276			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
277			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
278			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
279		},
280	}
281
282	// what we want to get, as a buffer of bytes
283	want := bytes.NewBufferString(`   9  License found
284   3  License not found
285  12  TOTAL
286
287  5  GPL-2.0-only
288  4  MIT
289  9  TOTAL FOUND
290`)
291
292	// render as buffer of bytes
293	var got bytes.Buffer
294	err := Generate2_3(pkg, &got)
295	if err != nil {
296		t.Errorf("Expected nil error, got %v", err)
297	}
298
299	// check that they match
300	c := bytes.Compare(want.Bytes(), got.Bytes())
301	if c != 0 {
302		t.Errorf("Expected %v, got %v", want.String(), got.String())
303	}
304}
305
306func Test2_3ReporterReturnsErrorIfPackageFilesNotAnalyzed(t *testing.T) {
307	pkg := &v2_3.Package{
308		FilesAnalyzed: false,
309	}
310
311	// render as buffer of bytes
312	var got bytes.Buffer
313	err := Generate2_3(pkg, &got)
314	if err == nil {
315		t.Errorf("Expected non-nil error, got nil")
316	}
317}
318
319// ===== 2.3 Utility functions =====
320
321func Test2_3CanGetCountsOfLicenses(t *testing.T) {
322	pkg := &v2_3.Package{
323		FilesAnalyzed: true,
324		Files: []*v2_3.File{
325			{FileSPDXIdentifier: "File0", LicenseConcluded: "MIT"},
326			{FileSPDXIdentifier: "File1", LicenseConcluded: "NOASSERTION"},
327			{FileSPDXIdentifier: "File2", LicenseConcluded: "MIT"},
328			{FileSPDXIdentifier: "File3", LicenseConcluded: "MIT"},
329			{FileSPDXIdentifier: "File4", LicenseConcluded: "GPL-2.0-only"},
330			{FileSPDXIdentifier: "File5", LicenseConcluded: "NOASSERTION"},
331			{FileSPDXIdentifier: "File6", LicenseConcluded: "GPL-2.0-only"},
332			{FileSPDXIdentifier: "File7", LicenseConcluded: "GPL-2.0-only"},
333			{FileSPDXIdentifier: "File8", LicenseConcluded: "MIT"},
334			{FileSPDXIdentifier: "File9", LicenseConcluded: "GPL-2.0-only"},
335			{FileSPDXIdentifier: "File10", LicenseConcluded: "GPL-2.0-only"},
336			{FileSPDXIdentifier: "File11", LicenseConcluded: "NOASSERTION"},
337		},
338	}
339
340	totalFound, totalNotFound, foundCounts := countLicenses2_3(pkg)
341	if totalFound != 9 {
342		t.Errorf("expected %v, got %v", 9, totalFound)
343	}
344	if totalNotFound != 3 {
345		t.Errorf("expected %v, got %v", 3, totalNotFound)
346	}
347	if len(foundCounts) != 2 {
348		t.Fatalf("expected %v, got %v", 2, len(foundCounts))
349	}
350
351	// foundCounts is a map of license ID to count of licenses
352	// confirm that the results are as expected
353	if foundCounts["GPL-2.0-only"] != 5 {
354		t.Errorf("expected %v, got %v", 5, foundCounts["GPL-2.0-only"])
355	}
356	if foundCounts["MIT"] != 4 {
357		t.Errorf("expected %v, got %v", 4, foundCounts["MIT"])
358	}
359}
360
361func Test2_3NilPackageReturnsZeroCountsOfLicenses(t *testing.T) {
362	totalFound, totalNotFound, foundCounts := countLicenses2_3(nil)
363	if totalFound != 0 {
364		t.Errorf("expected %v, got %v", 0, totalFound)
365	}
366	if totalNotFound != 0 {
367		t.Errorf("expected %v, got %v", 0, totalNotFound)
368	}
369	if len(foundCounts) != 0 {
370		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
371	}
372
373	pkg := &v2_3.Package{}
374	totalFound, totalNotFound, foundCounts = countLicenses2_3(pkg)
375	if totalFound != 0 {
376		t.Errorf("expected %v, got %v", 0, totalFound)
377	}
378	if totalNotFound != 0 {
379		t.Errorf("expected %v, got %v", 0, totalNotFound)
380	}
381	if len(foundCounts) != 0 {
382		t.Fatalf("expected %v, got %v", 0, len(foundCounts))
383	}
384}
385