xref: /aosp_15_r20/external/spdx-tools/builder/build_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package builder
4
5import (
6	"fmt"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/common"
10)
11
12// ===== 2.1 Builder top-level Document test =====
13func TestBuild2_1CreatesDocument(t *testing.T) {
14	dirRoot := "../testdata/project1/"
15
16	config := &Config2_1{
17		NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-",
18		CreatorType:     "Person",
19		Creator:         "John Doe",
20		TestValues:      make(map[string]string),
21	}
22	config.TestValues["Created"] = "2018-10-19T04:38:00Z"
23
24	wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"}
25
26	doc, err := Build2_1("project1", dirRoot, config)
27	if err != nil {
28		t.Errorf("expected nil error, got %v", err)
29	}
30	if doc == nil {
31		t.Fatalf("expected non-nil Document, got nil")
32	}
33
34	// check CI section
35	if doc.CreationInfo == nil {
36		t.Fatalf("expected non-nil CreationInfo section, got nil")
37	}
38	if doc.SPDXVersion != "SPDX-2.1" {
39		t.Errorf("expected %s, got %s", "SPDX-2.1", doc.SPDXVersion)
40	}
41	if doc.DataLicense != "CC0-1.0" {
42		t.Errorf("expected %s, got %s", "CC0-1.0", doc.DataLicense)
43	}
44	if doc.SPDXIdentifier != common.ElementID("DOCUMENT") {
45		t.Errorf("expected %s, got %v", "DOCUMENT", doc.SPDXIdentifier)
46	}
47	if doc.DocumentName != "project1" {
48		t.Errorf("expected %s, got %s", "project1", doc.DocumentName)
49	}
50	wantNamespace := fmt.Sprintf("https://github.com/swinslow/spdx-docs/spdx-go/testdata-project1-%s", wantVerificationCode)
51	if doc.DocumentNamespace != wantNamespace {
52		t.Errorf("expected %s, got %s", wantNamespace, doc.DocumentNamespace)
53	}
54	if len(doc.CreationInfo.Creators) != 2 {
55		t.Fatalf("expected %d, got %d", 2, len(doc.CreationInfo.Creators))
56	}
57	if doc.CreationInfo.Creators[1].Creator != "John Doe" {
58		t.Errorf("expected %s, got %+v", "John Doe", doc.CreationInfo.Creators[1])
59	}
60	if doc.CreationInfo.Creators[0].Creator != "github.com/spdx/tools-golang/builder" {
61		t.Errorf("expected %s, got %+v", "github.com/spdx/tools-golang/builder", doc.CreationInfo.Creators[0])
62	}
63	if doc.CreationInfo.Created != "2018-10-19T04:38:00Z" {
64		t.Errorf("expected %s, got %s", "2018-10-19T04:38:00Z", doc.CreationInfo.Created)
65	}
66
67	// check Package section
68	if doc.Packages == nil {
69		t.Fatalf("expected non-nil doc.Packages, got nil")
70	}
71	if len(doc.Packages) != 1 {
72		t.Fatalf("expected %d, got %d", 1, len(doc.Packages))
73	}
74	pkg := doc.Packages[0]
75	if pkg == nil {
76		t.Fatalf("expected non-nil pkg, got nil")
77	}
78	if pkg.PackageName != "project1" {
79		t.Errorf("expected %v, got %v", "project1", pkg.PackageName)
80	}
81	if pkg.PackageSPDXIdentifier != common.ElementID("Package-project1") {
82		t.Errorf("expected %v, got %v", "Package-project1", pkg.PackageSPDXIdentifier)
83	}
84	if pkg.PackageDownloadLocation != "NOASSERTION" {
85		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageDownloadLocation)
86	}
87	if pkg.FilesAnalyzed != true {
88		t.Errorf("expected %v, got %v", true, pkg.FilesAnalyzed)
89	}
90	if pkg.PackageVerificationCode.Value != wantVerificationCode.Value {
91		t.Errorf("expected %v, got %v", wantVerificationCode, pkg.PackageVerificationCode)
92	}
93	if pkg.PackageLicenseConcluded != "NOASSERTION" {
94		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseConcluded)
95	}
96	if len(pkg.PackageLicenseInfoFromFiles) != 0 {
97		t.Errorf("expected %v, got %v", 0, len(pkg.PackageLicenseInfoFromFiles))
98	}
99	if pkg.PackageLicenseDeclared != "NOASSERTION" {
100		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseDeclared)
101	}
102	if pkg.PackageCopyrightText != "NOASSERTION" {
103		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageCopyrightText)
104	}
105
106	// check Files section
107	if pkg.Files == nil {
108		t.Fatalf("expected non-nil pkg.Files, got nil")
109	}
110	if len(pkg.Files) != 5 {
111		t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
112	}
113
114	// files should be in order of identifier, which is numeric,
115	// created based on alphabetical order of files:
116	// emptyfile, file1, file3, folder/file4, lastfile
117
118	// check emptyfile.testdata.txt
119	fileEmpty := pkg.Files[0]
120	if fileEmpty == nil {
121		t.Fatalf("expected non-nil file, got nil")
122	}
123	if fileEmpty.FileName != "./emptyfile.testdata.txt" {
124		t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
125	}
126	if fileEmpty.FileSPDXIdentifier != common.ElementID("File0") {
127		t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
128	}
129
130	for _, checksum := range fileEmpty.Checksums {
131		switch checksum.Algorithm {
132		case common.SHA1:
133			if checksum.Value != "da39a3ee5e6b4b0d3255bfef95601890afd80709" {
134				t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", checksum.Value)
135			}
136		case common.SHA256:
137			if checksum.Value != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
138				t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", checksum.Value)
139			}
140		case common.MD5:
141			if checksum.Value != "d41d8cd98f00b204e9800998ecf8427e" {
142				t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", checksum.Value)
143			}
144		}
145	}
146
147	if fileEmpty.LicenseConcluded != "NOASSERTION" {
148		t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded)
149	}
150	if len(fileEmpty.LicenseInfoInFiles) != 1 {
151		t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFiles))
152	} else {
153		if fileEmpty.LicenseInfoInFiles[0] != "NOASSERTION" {
154			t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFiles[0])
155		}
156	}
157	if fileEmpty.FileCopyrightText != "NOASSERTION" {
158		t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText)
159	}
160
161	// check file1.testdata.txt
162	file1 := pkg.Files[1]
163	if file1 == nil {
164		t.Fatalf("expected non-nil file, got nil")
165	}
166	if file1.FileName != "./file1.testdata.txt" {
167		t.Errorf("expected %v, got %v", "./file1.testdata.txt", file1.FileName)
168	}
169	if file1.FileSPDXIdentifier != common.ElementID("File1") {
170		t.Errorf("expected %v, got %v", "File1", file1.FileSPDXIdentifier)
171	}
172
173	for _, checksum := range file1.Checksums {
174		switch checksum.Algorithm {
175		case common.SHA1:
176			if checksum.Value != "024f870eb6323f532515f7a09d5646a97083b819" {
177				t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", checksum.Value)
178			}
179		case common.SHA256:
180			if checksum.Value != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" {
181				t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", checksum.Value)
182			}
183		case common.MD5:
184			if checksum.Value != "37c8208479dfe42d2bb29debd6e32d4a" {
185				t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", checksum.Value)
186			}
187		}
188	}
189	if file1.LicenseConcluded != "NOASSERTION" {
190		t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseConcluded)
191	}
192	if len(file1.LicenseInfoInFiles) != 1 {
193		t.Errorf("expected %v, got %v", 1, len(file1.LicenseInfoInFiles))
194	} else {
195		if file1.LicenseInfoInFiles[0] != "NOASSERTION" {
196			t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseInfoInFiles[0])
197		}
198	}
199	if file1.FileCopyrightText != "NOASSERTION" {
200		t.Errorf("expected %v, got %v", "NOASSERTION", file1.FileCopyrightText)
201	}
202
203	// check file3.testdata.txt
204	file3 := pkg.Files[2]
205	if file3 == nil {
206		t.Fatalf("expected non-nil file, got nil")
207	}
208	if file3.FileName != "./file3.testdata.txt" {
209		t.Errorf("expected %v, got %v", "./file3.testdata.txt", file3.FileName)
210	}
211	if file3.FileSPDXIdentifier != common.ElementID("File2") {
212		t.Errorf("expected %v, got %v", "File2", file3.FileSPDXIdentifier)
213	}
214
215	for _, checksum := range file3.Checksums {
216		switch checksum.Algorithm {
217		case common.SHA1:
218			if checksum.Value != "a46114b70e163614f01c64adf44cdd438f158fce" {
219				t.Errorf("expected %v, got %v", "a46114b70e163614f01c64adf44cdd438f158fce", checksum.Value)
220			}
221		case common.SHA256:
222			if checksum.Value != "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4" {
223				t.Errorf("expected %v, got %v", "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4", checksum.Value)
224			}
225		case common.MD5:
226			if checksum.Value != "3e02d3ab9c58eec6911dbba37570934f" {
227				t.Errorf("expected %v, got %v", "3e02d3ab9c58eec6911dbba37570934f", checksum.Value)
228			}
229		}
230	}
231	if file3.LicenseConcluded != "NOASSERTION" {
232		t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseConcluded)
233	}
234	if len(file3.LicenseInfoInFiles) != 1 {
235		t.Errorf("expected %v, got %v", 1, len(file3.LicenseInfoInFiles))
236	} else {
237		if file3.LicenseInfoInFiles[0] != "NOASSERTION" {
238			t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseInfoInFiles[0])
239		}
240	}
241	if file3.FileCopyrightText != "NOASSERTION" {
242		t.Errorf("expected %v, got %v", "NOASSERTION", file3.FileCopyrightText)
243	}
244
245	// check folder1/file4.testdata.txt
246	file4 := pkg.Files[3]
247	if file4 == nil {
248		t.Fatalf("expected non-nil file, got nil")
249	}
250	if file4.FileName != "./folder1/file4.testdata.txt" {
251		t.Errorf("expected %v, got %v", "./folder1/file4.testdata.txt", file4.FileName)
252	}
253	if file4.FileSPDXIdentifier != common.ElementID("File3") {
254		t.Errorf("expected %v, got %v", "File3", file4.FileSPDXIdentifier)
255	}
256
257	for _, checksum := range file4.Checksums {
258		switch checksum.Algorithm {
259		case common.SHA1:
260			if checksum.Value != "e623d7d7d782a7c8323c4d436acee4afab34320f" {
261				t.Errorf("expected %v, got %v", "e623d7d7d782a7c8323c4d436acee4afab34320f", checksum.Value)
262			}
263		case common.SHA256:
264			if checksum.Value != "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59" {
265				t.Errorf("expected %v, got %v", "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59", checksum.Value)
266			}
267		case common.MD5:
268			if checksum.Value != "96e6a25d35df5b1c477710ef4d0c7210" {
269				t.Errorf("expected %v, got %v", "96e6a25d35df5b1c477710ef4d0c7210", checksum.Value)
270			}
271		}
272	}
273	if file4.LicenseConcluded != "NOASSERTION" {
274		t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseConcluded)
275	}
276	if len(file4.LicenseInfoInFiles) != 1 {
277		t.Errorf("expected %v, got %v", 1, len(file4.LicenseInfoInFiles))
278	} else {
279		if file4.LicenseInfoInFiles[0] != "NOASSERTION" {
280			t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseInfoInFiles[0])
281		}
282	}
283	if file4.FileCopyrightText != "NOASSERTION" {
284		t.Errorf("expected %v, got %v", "NOASSERTION", file4.FileCopyrightText)
285	}
286
287	// check lastfile.testdata.txt
288	lastfile := pkg.Files[4]
289	if lastfile == nil {
290		t.Fatalf("expected non-nil file, got nil")
291	}
292	if lastfile.FileName != "./lastfile.testdata.txt" {
293		t.Errorf("expected %v, got %v", "./lastfile.testdata.txt", lastfile.FileName)
294	}
295	if lastfile.FileSPDXIdentifier != common.ElementID("File4") {
296		t.Errorf("expected %v, got %v", "File4", lastfile.FileSPDXIdentifier)
297	}
298
299	for _, checksum := range lastfile.Checksums {
300		switch checksum.Algorithm {
301		case common.SHA1:
302			if checksum.Value != "26d6221d682d9ba59116f9753a701f34271c8ce1" {
303				t.Errorf("expected %v, got %v", "26d6221d682d9ba59116f9753a701f34271c8ce1", checksum.Value)
304			}
305		case common.SHA256:
306			if checksum.Value != "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805" {
307				t.Errorf("expected %v, got %v", "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805", checksum.Value)
308			}
309		case common.MD5:
310			if checksum.Value != "f60baa793870d9085461ad6bbab50b7f" {
311				t.Errorf("expected %v, got %v", "f60baa793870d9085461ad6bbab50b7f", checksum.Value)
312			}
313		}
314	}
315	if lastfile.LicenseConcluded != "NOASSERTION" {
316		t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseConcluded)
317	}
318	if len(lastfile.LicenseInfoInFiles) != 1 {
319		t.Errorf("expected %v, got %v", 1, len(lastfile.LicenseInfoInFiles))
320	} else {
321		if lastfile.LicenseInfoInFiles[0] != "NOASSERTION" {
322			t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseInfoInFiles[0])
323		}
324	}
325	if lastfile.FileCopyrightText != "NOASSERTION" {
326		t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.FileCopyrightText)
327	}
328
329	// check Relationship section -- should be a relationship for doc DESCRIBES pkg
330	if doc.Relationships == nil {
331		t.Fatalf("expected non-nil Relationships section, got nil")
332	}
333	if len(doc.Relationships) == 0 {
334		t.Fatalf("expected %v, got %v", 0, len(doc.Relationships))
335	}
336	rln := doc.Relationships[0]
337	if rln == nil {
338		t.Fatalf("expected non-nil Relationship, got nil")
339	}
340	if rln.RefA != common.MakeDocElementID("", "DOCUMENT") {
341		t.Errorf("expected %v, got %v", "DOCUMENT", rln.RefA)
342	}
343	if rln.RefB != common.MakeDocElementID("", "Package-project1") {
344		t.Errorf("expected %v, got %v", "Package-project1", rln.RefB)
345	}
346	if rln.Relationship != "DESCRIBES" {
347		t.Errorf("expected %v, got %v", "DESCRIBES", rln.Relationship)
348	}
349
350	// and check that other sections are present, but empty
351	if doc.OtherLicenses != nil {
352		t.Fatalf("expected nil OtherLicenses section, got non-nil")
353	}
354	if doc.Annotations != nil {
355		t.Fatalf("expected nil Annotations section, got non-nil")
356	}
357	if doc.Reviews != nil {
358		t.Fatalf("expected nil Reviews section, got non-nil")
359	}
360
361}
362
363func TestBuild2_1CanIgnoreFiles(t *testing.T) {
364	dirRoot := "../testdata/project3/"
365
366	config := &Config2_1{
367		NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-",
368		CreatorType:     "Person",
369		Creator:         "John Doe",
370		PathsIgnored: []string{
371			"**/ignoredir/",
372			"/excludedir/",
373			"**/ignorefile.txt",
374			"/alsoEXCLUDEthis.txt",
375		},
376		TestValues: make(map[string]string),
377	}
378	config.TestValues["Created"] = "2018-10-19T04:38:00Z"
379
380	doc, err := Build2_1("project1", dirRoot, config)
381	if err != nil {
382		t.Errorf("expected nil error, got %v", err)
383	}
384	pkg := doc.Packages[0]
385	if pkg == nil {
386		t.Fatalf("expected non-nil pkg, got nil")
387	}
388	if len(pkg.Files) != 5 {
389		t.Fatalf("expected len %d, got %d", 5, len(pkg.Files))
390	}
391
392	want := "./dontscan.txt"
393	got := pkg.Files[0].FileName
394	if want != got {
395		t.Errorf("expected %v, got %v", want, got)
396	}
397
398	want = "./keep/keep.txt"
399	got = pkg.Files[1].FileName
400	if want != got {
401		t.Errorf("expected %v, got %v", want, got)
402	}
403
404	want = "./keep.txt"
405	got = pkg.Files[2].FileName
406	if want != got {
407		t.Errorf("expected %v, got %v", want, got)
408	}
409
410	want = "./subdir/keep/dontscan.txt"
411	got = pkg.Files[3].FileName
412	if want != got {
413		t.Errorf("expected %v, got %v", want, got)
414	}
415
416	want = "./subdir/keep/keep.txt"
417	got = pkg.Files[4].FileName
418	if want != got {
419		t.Errorf("expected %v, got %v", want, got)
420	}
421}
422
423// ===== 2.2 Builder top-level Document test =====
424func TestBuild2_2CreatesDocument(t *testing.T) {
425	dirRoot := "../testdata/project1/"
426
427	config := &Config2_2{
428		NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-",
429		CreatorType:     "Person",
430		Creator:         "John Doe",
431		TestValues:      make(map[string]string),
432	}
433	config.TestValues["Created"] = "2018-10-19T04:38:00Z"
434
435	wantVerificationCode := common.PackageVerificationCode{Value: "fc9ac4a370af0a471c2e52af66d6b4cf4e2ba12b"}
436
437	doc, err := Build2_2("project1", dirRoot, config)
438	if err != nil {
439		t.Errorf("expected nil error, got %v", err)
440	}
441	if doc == nil {
442		t.Fatalf("expected non-nil Document, got nil")
443	}
444
445	// check CI section
446	if doc.CreationInfo == nil {
447		t.Fatalf("expected non-nil CreationInfo section, got nil")
448	}
449	if doc.SPDXVersion != "SPDX-2.2" {
450		t.Errorf("expected %s, got %s", "SPDX-2.2", doc.SPDXVersion)
451	}
452	if doc.DataLicense != "CC0-1.0" {
453		t.Errorf("expected %s, got %s", "CC0-1.0", doc.DataLicense)
454	}
455	if doc.SPDXIdentifier != common.ElementID("DOCUMENT") {
456		t.Errorf("expected %s, got %v", "DOCUMENT", doc.SPDXIdentifier)
457	}
458	if doc.DocumentName != "project1" {
459		t.Errorf("expected %s, got %s", "project1", doc.DocumentName)
460	}
461	wantNamespace := fmt.Sprintf("https://github.com/swinslow/spdx-docs/spdx-go/testdata-project1-%s", wantVerificationCode)
462	if doc.DocumentNamespace != wantNamespace {
463		t.Errorf("expected %s, got %s", wantNamespace, doc.DocumentNamespace)
464	}
465	if len(doc.CreationInfo.Creators) != 2 {
466		t.Fatalf("expected %d, got %d", 2, len(doc.CreationInfo.Creators))
467	}
468	if doc.CreationInfo.Creators[1].Creator != "John Doe" {
469		t.Errorf("expected %s, got %+v", "John Doe", doc.CreationInfo.Creators[1])
470	}
471	if doc.CreationInfo.Creators[0].Creator != "github.com/spdx/tools-golang/builder" {
472		t.Errorf("expected %s, got %+v", "github.com/spdx/tools-golang/builder", doc.CreationInfo.Creators[0])
473	}
474	if doc.CreationInfo.Created != "2018-10-19T04:38:00Z" {
475		t.Errorf("expected %s, got %s", "2018-10-19T04:38:00Z", doc.CreationInfo.Created)
476	}
477
478	// check Package section
479	if doc.Packages == nil {
480		t.Fatalf("expected non-nil doc.Packages, got nil")
481	}
482	if len(doc.Packages) != 1 {
483		t.Fatalf("expected %d, got %d", 1, len(doc.Packages))
484	}
485	pkg := doc.Packages[0]
486	if pkg == nil {
487		t.Fatalf("expected non-nil pkg, got nil")
488	}
489	if pkg.PackageName != "project1" {
490		t.Errorf("expected %v, got %v", "project1", pkg.PackageName)
491	}
492	if pkg.PackageSPDXIdentifier != common.ElementID("Package-project1") {
493		t.Errorf("expected %v, got %v", "Package-project1", pkg.PackageSPDXIdentifier)
494	}
495	if pkg.PackageDownloadLocation != "NOASSERTION" {
496		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageDownloadLocation)
497	}
498	if pkg.FilesAnalyzed != true {
499		t.Errorf("expected %v, got %v", true, pkg.FilesAnalyzed)
500	}
501	if pkg.PackageVerificationCode.Value != wantVerificationCode.Value {
502		t.Errorf("expected %v, got %v", wantVerificationCode, pkg.PackageVerificationCode)
503	}
504	if pkg.PackageLicenseConcluded != "NOASSERTION" {
505		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseConcluded)
506	}
507	if len(pkg.PackageLicenseInfoFromFiles) != 0 {
508		t.Errorf("expected %v, got %v", 0, len(pkg.PackageLicenseInfoFromFiles))
509	}
510	if pkg.PackageLicenseDeclared != "NOASSERTION" {
511		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageLicenseDeclared)
512	}
513	if pkg.PackageCopyrightText != "NOASSERTION" {
514		t.Errorf("expected %v, got %v", "NOASSERTION", pkg.PackageCopyrightText)
515	}
516
517	// check Files section
518	if pkg.Files == nil {
519		t.Fatalf("expected non-nil pkg.Files, got nil")
520	}
521	if len(pkg.Files) != 5 {
522		t.Fatalf("expected %d, got %d", 5, len(pkg.Files))
523	}
524
525	// files should be in order of identifier, which is numeric,
526	// created based on alphabetical order of files:
527	// emptyfile, file1, file3, folder/file4, lastfile
528
529	// check emptyfile.testdata.txt
530	fileEmpty := pkg.Files[0]
531	if fileEmpty == nil {
532		t.Fatalf("expected non-nil file, got nil")
533	}
534	if fileEmpty.FileName != "./emptyfile.testdata.txt" {
535		t.Errorf("expected %v, got %v", "./emptyfile.testdata.txt", fileEmpty.FileName)
536	}
537	if fileEmpty.FileSPDXIdentifier != common.ElementID("File0") {
538		t.Errorf("expected %v, got %v", "File0", fileEmpty.FileSPDXIdentifier)
539	}
540	for _, checksum := range fileEmpty.Checksums {
541		switch checksum.Algorithm {
542		case common.SHA1:
543			if checksum.Value != "da39a3ee5e6b4b0d3255bfef95601890afd80709" {
544				t.Errorf("expected %v, got %v", "da39a3ee5e6b4b0d3255bfef95601890afd80709", checksum.Value)
545			}
546		case common.SHA256:
547			if checksum.Value != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
548				t.Errorf("expected %v, got %v", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", checksum.Value)
549			}
550		case common.MD5:
551			if checksum.Value != "d41d8cd98f00b204e9800998ecf8427e" {
552				t.Errorf("expected %v, got %v", "d41d8cd98f00b204e9800998ecf8427e", checksum.Value)
553			}
554		}
555	}
556	if fileEmpty.LicenseConcluded != "NOASSERTION" {
557		t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseConcluded)
558	}
559	if len(fileEmpty.LicenseInfoInFiles) != 1 {
560		t.Errorf("expected %v, got %v", 1, len(fileEmpty.LicenseInfoInFiles))
561	} else {
562		if fileEmpty.LicenseInfoInFiles[0] != "NOASSERTION" {
563			t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.LicenseInfoInFiles[0])
564		}
565	}
566	if fileEmpty.FileCopyrightText != "NOASSERTION" {
567		t.Errorf("expected %v, got %v", "NOASSERTION", fileEmpty.FileCopyrightText)
568	}
569
570	// check file1.testdata.txt
571	file1 := pkg.Files[1]
572	if file1 == nil {
573		t.Fatalf("expected non-nil file, got nil")
574	}
575	if file1.FileName != "./file1.testdata.txt" {
576		t.Errorf("expected %v, got %v", "./file1.testdata.txt", file1.FileName)
577	}
578	if file1.FileSPDXIdentifier != common.ElementID("File1") {
579		t.Errorf("expected %v, got %v", "File1", file1.FileSPDXIdentifier)
580	}
581	for _, checksum := range file1.Checksums {
582		switch checksum.Algorithm {
583		case common.SHA1:
584			if checksum.Value != "024f870eb6323f532515f7a09d5646a97083b819" {
585				t.Errorf("expected %v, got %v", "024f870eb6323f532515f7a09d5646a97083b819", checksum.Value)
586			}
587		case common.SHA256:
588			if checksum.Value != "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf" {
589				t.Errorf("expected %v, got %v", "b14e44284ca477b4c0db34b15ca4c454b2947cce7883e22321cf2984050e15bf", checksum.Value)
590			}
591		case common.MD5:
592			if checksum.Value != "37c8208479dfe42d2bb29debd6e32d4a" {
593				t.Errorf("expected %v, got %v", "37c8208479dfe42d2bb29debd6e32d4a", checksum.Value)
594			}
595		}
596	}
597	if file1.LicenseConcluded != "NOASSERTION" {
598		t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseConcluded)
599	}
600	if len(file1.LicenseInfoInFiles) != 1 {
601		t.Errorf("expected %v, got %v", 1, len(file1.LicenseInfoInFiles))
602	} else {
603		if file1.LicenseInfoInFiles[0] != "NOASSERTION" {
604			t.Errorf("expected %v, got %v", "NOASSERTION", file1.LicenseInfoInFiles[0])
605		}
606	}
607	if file1.FileCopyrightText != "NOASSERTION" {
608		t.Errorf("expected %v, got %v", "NOASSERTION", file1.FileCopyrightText)
609	}
610
611	// check file3.testdata.txt
612	file3 := pkg.Files[2]
613	if file3 == nil {
614		t.Fatalf("expected non-nil file, got nil")
615	}
616	if file3.FileName != "./file3.testdata.txt" {
617		t.Errorf("expected %v, got %v", "./file3.testdata.txt", file3.FileName)
618	}
619	if file3.FileSPDXIdentifier != common.ElementID("File2") {
620		t.Errorf("expected %v, got %v", "File2", file3.FileSPDXIdentifier)
621	}
622	for _, checksum := range file3.Checksums {
623		switch checksum.Algorithm {
624		case common.SHA1:
625			if checksum.Value != "a46114b70e163614f01c64adf44cdd438f158fce" {
626				t.Errorf("expected %v, got %v", "a46114b70e163614f01c64adf44cdd438f158fce", checksum.Value)
627			}
628		case common.SHA256:
629			if checksum.Value != "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4" {
630				t.Errorf("expected %v, got %v", "9fc181b9892720a15df1a1e561860318db40621bd4040ccdf18e110eb01d04b4", checksum.Value)
631			}
632		case common.MD5:
633			if checksum.Value != "3e02d3ab9c58eec6911dbba37570934f" {
634				t.Errorf("expected %v, got %v", "3e02d3ab9c58eec6911dbba37570934f", checksum.Value)
635			}
636		}
637	}
638	if file3.LicenseConcluded != "NOASSERTION" {
639		t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseConcluded)
640	}
641	if len(file3.LicenseInfoInFiles) != 1 {
642		t.Errorf("expected %v, got %v", 1, len(file3.LicenseInfoInFiles))
643	} else {
644		if file3.LicenseInfoInFiles[0] != "NOASSERTION" {
645			t.Errorf("expected %v, got %v", "NOASSERTION", file3.LicenseInfoInFiles[0])
646		}
647	}
648	if file3.FileCopyrightText != "NOASSERTION" {
649		t.Errorf("expected %v, got %v", "NOASSERTION", file3.FileCopyrightText)
650	}
651
652	// check folder1/file4.testdata.txt
653	file4 := pkg.Files[3]
654	if file4 == nil {
655		t.Fatalf("expected non-nil file, got nil")
656	}
657	if file4.FileName != "./folder1/file4.testdata.txt" {
658		t.Errorf("expected %v, got %v", "./folder1/file4.testdata.txt", file4.FileName)
659	}
660	if file4.FileSPDXIdentifier != common.ElementID("File3") {
661		t.Errorf("expected %v, got %v", "File3", file4.FileSPDXIdentifier)
662	}
663	for _, checksum := range file4.Checksums {
664		switch checksum.Algorithm {
665		case common.SHA1:
666			if checksum.Value != "e623d7d7d782a7c8323c4d436acee4afab34320f" {
667				t.Errorf("expected %v, got %v", "e623d7d7d782a7c8323c4d436acee4afab34320f", checksum.Value)
668			}
669		case common.SHA256:
670			if checksum.Value != "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59" {
671				t.Errorf("expected %v, got %v", "574fa42c5e0806c0f8906a44884166540206f021527729407cd5326838629c59", checksum.Value)
672			}
673		case common.MD5:
674			if checksum.Value != "96e6a25d35df5b1c477710ef4d0c7210" {
675				t.Errorf("expected %v, got %v", "96e6a25d35df5b1c477710ef4d0c7210", checksum.Value)
676			}
677		}
678	}
679	if file4.LicenseConcluded != "NOASSERTION" {
680		t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseConcluded)
681	}
682	if len(file4.LicenseInfoInFiles) != 1 {
683		t.Errorf("expected %v, got %v", 1, len(file4.LicenseInfoInFiles))
684	} else {
685		if file4.LicenseInfoInFiles[0] != "NOASSERTION" {
686			t.Errorf("expected %v, got %v", "NOASSERTION", file4.LicenseInfoInFiles[0])
687		}
688	}
689	if file4.FileCopyrightText != "NOASSERTION" {
690		t.Errorf("expected %v, got %v", "NOASSERTION", file4.FileCopyrightText)
691	}
692
693	// check lastfile.testdata.txt
694	lastfile := pkg.Files[4]
695	if lastfile == nil {
696		t.Fatalf("expected non-nil file, got nil")
697	}
698	if lastfile.FileName != "./lastfile.testdata.txt" {
699		t.Errorf("expected %v, got %v", "/lastfile.testdata.txt", lastfile.FileName)
700	}
701	if lastfile.FileSPDXIdentifier != common.ElementID("File4") {
702		t.Errorf("expected %v, got %v", "File4", lastfile.FileSPDXIdentifier)
703	}
704	for _, checksum := range lastfile.Checksums {
705		switch checksum.Algorithm {
706		case common.SHA1:
707			if checksum.Value != "26d6221d682d9ba59116f9753a701f34271c8ce1" {
708				t.Errorf("expected %v, got %v", "26d6221d682d9ba59116f9753a701f34271c8ce1", checksum.Value)
709			}
710		case common.SHA256:
711			if checksum.Value != "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805" {
712				t.Errorf("expected %v, got %v", "0a4bdaf990e9b330ff72022dd78110ae98b60e08337cf2105b89856373416805", checksum.Value)
713			}
714		case common.MD5:
715			if checksum.Value != "f60baa793870d9085461ad6bbab50b7f" {
716				t.Errorf("expected %v, got %v", "f60baa793870d9085461ad6bbab50b7f", checksum.Value)
717			}
718		}
719	}
720	if lastfile.LicenseConcluded != "NOASSERTION" {
721		t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseConcluded)
722	}
723	if len(lastfile.LicenseInfoInFiles) != 1 {
724		t.Errorf("expected %v, got %v", 1, len(lastfile.LicenseInfoInFiles))
725	} else {
726		if lastfile.LicenseInfoInFiles[0] != "NOASSERTION" {
727			t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.LicenseInfoInFiles[0])
728		}
729	}
730	if lastfile.FileCopyrightText != "NOASSERTION" {
731		t.Errorf("expected %v, got %v", "NOASSERTION", lastfile.FileCopyrightText)
732	}
733
734	// check Relationship section -- should be a relationship for doc DESCRIBES pkg
735	if doc.Relationships == nil {
736		t.Fatalf("expected non-nil Relationships section, got nil")
737	}
738	if len(doc.Relationships) == 0 {
739		t.Fatalf("expected %v, got %v", 0, len(doc.Relationships))
740	}
741	rln := doc.Relationships[0]
742	if rln == nil {
743		t.Fatalf("expected non-nil Relationship, got nil")
744	}
745	if rln.RefA != common.MakeDocElementID("", "DOCUMENT") {
746		t.Errorf("expected %v, got %v", "DOCUMENT", rln.RefA)
747	}
748	if rln.RefB != common.MakeDocElementID("", "Package-project1") {
749		t.Errorf("expected %v, got %v", "Package-project1", rln.RefB)
750	}
751	if rln.Relationship != "DESCRIBES" {
752		t.Errorf("expected %v, got %v", "DESCRIBES", rln.Relationship)
753	}
754
755	// and check that other sections are present, but empty
756	if doc.OtherLicenses != nil {
757		t.Fatalf("expected nil OtherLicenses section, got non-nil")
758	}
759	if doc.Annotations != nil {
760		t.Fatalf("expected nil Annotations section, got non-nil")
761	}
762	if doc.Reviews != nil {
763		t.Fatalf("expected nil Reviews section, got non-nil")
764	}
765
766}
767
768func TestBuild2_2CanIgnoreFiles(t *testing.T) {
769	dirRoot := "../testdata/project3/"
770
771	config := &Config2_2{
772		NamespacePrefix: "https://github.com/swinslow/spdx-docs/spdx-go/testdata-",
773		CreatorType:     "Person",
774		Creator:         "John Doe",
775		PathsIgnored: []string{
776			"**/ignoredir/",
777			"/excludedir/",
778			"**/ignorefile.txt",
779			"/alsoEXCLUDEthis.txt",
780		},
781		TestValues: make(map[string]string),
782	}
783	config.TestValues["Created"] = "2018-10-19T04:38:00Z"
784
785	doc, err := Build2_2("project1", dirRoot, config)
786	if err != nil {
787		t.Errorf("expected nil error, got %v", err)
788	}
789	pkg := doc.Packages[0]
790	if pkg == nil {
791		t.Fatalf("expected non-nil pkg, got nil")
792	}
793	if len(pkg.Files) != 5 {
794		t.Fatalf("expected len %d, got %d", 5, len(pkg.Files))
795	}
796
797	want := "./dontscan.txt"
798	got := pkg.Files[0].FileName
799	if want != got {
800		t.Errorf("expected %v, got %v", want, got)
801	}
802
803	want = "./keep/keep.txt"
804	got = pkg.Files[1].FileName
805	if want != got {
806		t.Errorf("expected %v, got %v", want, got)
807	}
808
809	want = "./keep.txt"
810	got = pkg.Files[2].FileName
811	if want != got {
812		t.Errorf("expected %v, got %v", want, got)
813	}
814
815	want = "./subdir/keep/dontscan.txt"
816	got = pkg.Files[3].FileName
817	if want != got {
818		t.Errorf("expected %v, got %v", want, got)
819	}
820
821	want = "./subdir/keep/keep.txt"
822	got = pkg.Files[4].FileName
823	if want != got {
824		t.Errorf("expected %v, got %v", want, got)
825	}
826}
827