aboutsummaryrefslogtreecommitdiff
path: root/licensediff/licensediff_test.go
blob: 33f65f7a3c5b2c86afc8d7dd0ddf28c355048884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package licensediff

import (
	"testing"

	"github.com/spdx/tools-golang/spdx"
)

// ===== License diff top-level function tests =====
func TestDifferCanCreateDiffPairs(t *testing.T) {
	// create files to be used in diff
	// f1 will be identical in both
	f1 := &spdx.File2_1{
		FileName:           "/project/file1.txt",
		FileSPDXIdentifier: "SPDXRef-File561",
		FileChecksumSHA1:   "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3",
		LicenseConcluded:   "Apache-2.0",
		LicenseInfoInFile: []string{
			"LicenseRef-We-will-ignore-LicenseInfoInFile",
		},
		FileCopyrightText: "We'll ignore copyright values",
	}

	// f2 will only appear in the first Package
	f2 := &spdx.File2_1{
		FileName:           "/project/file2.txt",
		FileSPDXIdentifier: "SPDXRef-File562",
		FileChecksumSHA1:   "066c5139bd9a43d15812ec1a1755b08ccf199824",
		LicenseConcluded:   "GPL-2.0-or-later",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f3 will only appear in the second Package
	f3 := &spdx.File2_1{
		FileName:           "/project/file3.txt",
		FileSPDXIdentifier: "SPDXRef-File563",
		FileChecksumSHA1:   "bd0f4863b15fad2b79b35303af54fcb5baaf7c68",
		LicenseConcluded:   "MPL-2.0",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f4_1 and f4_2 will appear in first and second,
	// with same name, same hash and different license
	f4_1 := &spdx.File2_1{
		FileName:           "/project/file4.txt",
		FileSPDXIdentifier: "SPDXRef-File564",
		FileChecksumSHA1:   "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5",
		LicenseConcluded:   "MIT",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f4_2 := &spdx.File2_1{
		FileName:           "/project/file4.txt",
		FileSPDXIdentifier: "SPDXRef-File564",
		FileChecksumSHA1:   "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5",
		LicenseConcluded:   "Apache-2.0 AND MIT",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f5_1 and f5_2 will appear in first and second,
	// with same name, different hash and same license
	f5_1 := &spdx.File2_1{
		FileName:           "/project/file5.txt",
		FileSPDXIdentifier: "SPDXRef-File565",
		FileChecksumSHA1:   "ba226db943bbbf455da77afab6f16dbab156d000",
		LicenseConcluded:   "BSD-3-Clause",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f5_2 := &spdx.File2_1{
		FileName:           "/project/file5.txt",
		FileSPDXIdentifier: "SPDXRef-File565",
		FileChecksumSHA1:   "b6e0ec7d085c5699b46f6f8d425413702652874d",
		LicenseConcluded:   "BSD-3-Clause",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f6_1 and f6_2 will appear in first and second,
	// with same name, different hash and different license
	f6_1 := &spdx.File2_1{
		FileName:           "/project/file6.txt",
		FileSPDXIdentifier: "SPDXRef-File566",
		FileChecksumSHA1:   "ba226db943bbbf455da77afab6f16dbab156d000",
		LicenseConcluded:   "CC0-1.0",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f6_2 := &spdx.File2_1{
		FileName:           "/project/file6.txt",
		FileSPDXIdentifier: "SPDXRef-File566",
		FileChecksumSHA1:   "b6e0ec7d085c5699b46f6f8d425413702652874d",
		LicenseConcluded:   "Unlicense",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// create Packages
	p1 := &spdx.Package2_1{
		PackageName:               "p1",
		PackageSPDXIdentifier:     "SPDXRef-p1",
		PackageDownloadLocation:   "NOASSERTION",
		FilesAnalyzed:             true,
		IsFilesAnalyzedTagPresent: true,
		// fake the verification code for present purposes
		PackageVerificationCode: "abc123abc123",
		PackageLicenseConcluded: "NOASSERTION",
		PackageLicenseInfoFromFiles: []string{
			"NOASSERTION",
		},
		PackageLicenseDeclared: "NOASSERTION",
		PackageCopyrightText:   "NOASSERTION",
		Files: []*spdx.File2_1{
			f1,
			f2,
			f4_1,
			f5_1,
			f6_1,
		},
	}
	p2 := &spdx.Package2_1{
		PackageName:               "p2",
		PackageSPDXIdentifier:     "SPDXRef-p2",
		PackageDownloadLocation:   "NOASSERTION",
		FilesAnalyzed:             true,
		IsFilesAnalyzedTagPresent: true,
		// fake the verification code for present purposes
		PackageVerificationCode: "def456def456",
		PackageLicenseConcluded: "NOASSERTION",
		PackageLicenseInfoFromFiles: []string{
			"NOASSERTION",
		},
		PackageLicenseDeclared: "NOASSERTION",
		PackageCopyrightText:   "NOASSERTION",
		Files: []*spdx.File2_1{
			f1,
			f3,
			f4_2,
			f5_2,
			f6_2,
		},
	}

	// run the diff between the two packages
	diffMap, err := MakePairs(p1, p2)
	if err != nil {
		t.Fatalf("Expected nil error, got %v", err)
	}

	// check that the diff results are what we expect
	// there should be 6 entries, one for each unique filename
	if len(diffMap) != 6 {
		t.Fatalf("Expected %d, got %d", 6, len(diffMap))
	}

	// check each filename is present, and check its pair
	// pair 1 -- same in both
	pair1, ok := diffMap["/project/file1.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair1")
	}
	if pair1.First != f1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f1.LicenseConcluded, pair1.First)
	}
	if pair1.Second != f1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair1.Second)
	}

	// pair 2 -- only in first
	pair2, ok := diffMap["/project/file2.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair2")
	}
	if pair2.First != f2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f2.LicenseConcluded, pair2.First)
	}
	if pair2.Second != "" {
		t.Errorf("Expected %s, got %s", "", pair2.Second)
	}

	// pair 3 -- only in second
	pair3, ok := diffMap["/project/file3.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair3")
	}
	if pair3.First != "" {
		t.Errorf("Expected %s, got %s", "", pair3.First)
	}
	if pair3.Second != f3.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f3.LicenseConcluded, pair3.Second)
	}

	// pair 4 -- in both but different license
	pair4, ok := diffMap["/project/file4.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair4")
	}
	if pair4.First != f4_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, pair4.First)
	}
	if pair4.Second != f4_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, pair4.Second)
	}

	// pair 5 -- in both but different hash, same license
	pair5, ok := diffMap["/project/file5.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair5")
	}
	if pair5.First != f5_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, pair5.First)
	}
	if pair5.Second != f5_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, pair5.Second)
	}

	// pair 6 -- in both but different hash, different license
	pair6, ok := diffMap["/project/file6.txt"]
	if !ok {
		t.Fatalf("Couldn't get pair6")
	}
	if pair6.First != f6_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, pair6.First)
	}
	if pair6.Second != f6_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, pair6.Second)
	}
}

func TestDifferCanCreateDiffStructuredResults(t *testing.T) {
	// create files to be used in diff
	// f1 will be identical in both
	f1 := &spdx.File2_1{
		FileName:           "/project/file1.txt",
		FileSPDXIdentifier: "SPDXRef-File561",
		FileChecksumSHA1:   "6c92dc8bc462b6889d9b1c0bc16c54d19a2cbdd3",
		LicenseConcluded:   "Apache-2.0",
		LicenseInfoInFile: []string{
			"LicenseRef-We-will-ignore-LicenseInfoInFile",
		},
		FileCopyrightText: "We'll ignore copyright values",
	}

	// f2 will only appear in the first Package
	f2 := &spdx.File2_1{
		FileName:           "/project/file2.txt",
		FileSPDXIdentifier: "SPDXRef-File562",
		FileChecksumSHA1:   "066c5139bd9a43d15812ec1a1755b08ccf199824",
		LicenseConcluded:   "GPL-2.0-or-later",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f3 will only appear in the second Package
	f3 := &spdx.File2_1{
		FileName:           "/project/file3.txt",
		FileSPDXIdentifier: "SPDXRef-File563",
		FileChecksumSHA1:   "bd0f4863b15fad2b79b35303af54fcb5baaf7c68",
		LicenseConcluded:   "MPL-2.0",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f4_1 and f4_2 will appear in first and second,
	// with same name, same hash and different license
	f4_1 := &spdx.File2_1{
		FileName:           "/project/file4.txt",
		FileSPDXIdentifier: "SPDXRef-File564",
		FileChecksumSHA1:   "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5",
		LicenseConcluded:   "MIT",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f4_2 := &spdx.File2_1{
		FileName:           "/project/file4.txt",
		FileSPDXIdentifier: "SPDXRef-File564",
		FileChecksumSHA1:   "bc417a575ceae93435bcb7bfd382ac28cbdaa8b5",
		LicenseConcluded:   "Apache-2.0 AND MIT",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f5_1 and f5_2 will appear in first and second,
	// with same name, different hash and same license
	f5_1 := &spdx.File2_1{
		FileName:           "/project/file5.txt",
		FileSPDXIdentifier: "SPDXRef-File565",
		FileChecksumSHA1:   "ba226db943bbbf455da77afab6f16dbab156d000",
		LicenseConcluded:   "BSD-3-Clause",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f5_2 := &spdx.File2_1{
		FileName:           "/project/file5.txt",
		FileSPDXIdentifier: "SPDXRef-File565",
		FileChecksumSHA1:   "b6e0ec7d085c5699b46f6f8d425413702652874d",
		LicenseConcluded:   "BSD-3-Clause",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// f6_1 and f6_2 will appear in first and second,
	// with same name, different hash and different license
	f6_1 := &spdx.File2_1{
		FileName:           "/project/file6.txt",
		FileSPDXIdentifier: "SPDXRef-File566",
		FileChecksumSHA1:   "ba226db943bbbf455da77afab6f16dbab156d000",
		LicenseConcluded:   "CC0-1.0",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}
	f6_2 := &spdx.File2_1{
		FileName:           "/project/file6.txt",
		FileSPDXIdentifier: "SPDXRef-File566",
		FileChecksumSHA1:   "b6e0ec7d085c5699b46f6f8d425413702652874d",
		LicenseConcluded:   "Unlicense",
		LicenseInfoInFile: []string{
			"NOASSERTION",
		},
		FileCopyrightText: "NOASSERTION",
	}

	// create Packages
	p1 := &spdx.Package2_1{
		PackageName:               "p1",
		PackageSPDXIdentifier:     "SPDXRef-p1",
		PackageDownloadLocation:   "NOASSERTION",
		FilesAnalyzed:             true,
		IsFilesAnalyzedTagPresent: true,
		// fake the verification code for present purposes
		PackageVerificationCode: "abc123abc123",
		PackageLicenseConcluded: "NOASSERTION",
		PackageLicenseInfoFromFiles: []string{
			"NOASSERTION",
		},
		PackageLicenseDeclared: "NOASSERTION",
		PackageCopyrightText:   "NOASSERTION",
		Files: []*spdx.File2_1{
			f1,
			f2,
			f4_1,
			f5_1,
			f6_1,
		},
	}
	p2 := &spdx.Package2_1{
		PackageName:               "p2",
		PackageSPDXIdentifier:     "SPDXRef-p2",
		PackageDownloadLocation:   "NOASSERTION",
		FilesAnalyzed:             true,
		IsFilesAnalyzedTagPresent: true,
		// fake the verification code for present purposes
		PackageVerificationCode: "def456def456",
		PackageLicenseConcluded: "NOASSERTION",
		PackageLicenseInfoFromFiles: []string{
			"NOASSERTION",
		},
		PackageLicenseDeclared: "NOASSERTION",
		PackageCopyrightText:   "NOASSERTION",
		Files: []*spdx.File2_1{
			f1,
			f3,
			f4_2,
			f5_2,
			f6_2,
		},
	}

	// run the diff between the two packages
	diffMap, err := MakePairs(p1, p2)
	if err != nil {
		t.Fatalf("Expected nil error, got %v", err)
	}

	// now, create the LicenseDiff structured results from the pairs
	diffResults, err := MakeResults(diffMap)
	if err != nil {
		t.Fatalf("Expected nil error, got %v", err)
	}

	// check that the diff results are the expected lengths
	if len(diffResults.InBothChanged) != 2 {
		t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothChanged))
	}
	if len(diffResults.InBothSame) != 2 {
		t.Fatalf("Expected %d, got %d", 2, len(diffResults.InBothSame))
	}
	if len(diffResults.InFirstOnly) != 1 {
		t.Fatalf("Expected %d, got %d", 1, len(diffResults.InFirstOnly))
	}
	if len(diffResults.InSecondOnly) != 1 {
		t.Fatalf("Expected %d, got %d", 1, len(diffResults.InSecondOnly))
	}

	// check each filename is present where it belongs, and check license(s)

	// in both and different license: f4 and f6
	// filename will map to a LicensePair
	check4, ok := diffResults.InBothChanged["/project/file4.txt"]
	if !ok {
		t.Fatalf("Couldn't get check4")
	}
	if check4.First != f4_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f4_1.LicenseConcluded, check4.First)
	}
	if check4.Second != f4_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f4_2.LicenseConcluded, check4.Second)
	}
	check6, ok := diffResults.InBothChanged["/project/file6.txt"]
	if !ok {
		t.Fatalf("Couldn't get check6")
	}
	if check6.First != f6_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f6_1.LicenseConcluded, check6.First)
	}
	if check6.Second != f6_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f6_2.LicenseConcluded, check6.Second)
	}

	// in both and same license: f1 and f5
	// filename will map to a string
	check1, ok := diffResults.InBothSame["/project/file1.txt"]
	if !ok {
		t.Fatalf("Couldn't get check1")
	}
	if check1 != f1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f1.LicenseConcluded, check1)
	}
	check5, ok := diffResults.InBothSame["/project/file5.txt"]
	if !ok {
		t.Fatalf("Couldn't get check5")
	}
	if check5 != f5_1.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f5_1.LicenseConcluded, check5)
	}
	if check5 != f5_2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f5_2.LicenseConcluded, check5)
	}

	// in first only: f2
	// filename will map to a string
	check2, ok := diffResults.InFirstOnly["/project/file2.txt"]
	if !ok {
		t.Fatalf("Couldn't get check2")
	}
	if check2 != f2.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f2.LicenseConcluded, check2)
	}

	// in second only: f3
	// filename will map to a string
	check3, ok := diffResults.InSecondOnly["/project/file3.txt"]
	if !ok {
		t.Fatalf("Couldn't get check3")
	}
	if check3 != f3.LicenseConcluded {
		t.Errorf("Expected %s, got %s", f3.LicenseConcluded, check2)
	}

}