This repository was archived by the owner on Aug 29, 2023. It is now read-only.
generated from IBM/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlicense_test.go
410 lines (383 loc) · 12.3 KB
/
license_test.go
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
// SPDX-License-Identifier: Apache-2.0
//go:build unit
package licenses
import (
"fmt"
"regexp"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/IBM/license-scanner/configurer"
)
const (
expectedLicenseCount = 540
expectedPrecheckCount = 556
acceptablePatternsCount = 0
)
func TestCreateLicense(t *testing.T) {
re := regexp.MustCompile(`permission is hereby granted, free of charge, to any person obtaining a copy of <<match=this|the>>`)
tests := []struct {
name string
id string
want License
wantErr bool
}{
{
name: "expect fail with no_such_file",
id: "no_such_file",
want: License{},
wantErr: true,
},
{
name: "MIT",
id: "MIT",
want: License{
SPDXLicenseID: "MIT",
LicenseInfo: LicenseInfo{
Name: "MIT License",
Family: "MIT",
SPDXStandard: true,
SPDXException: false,
OSIApproved: true,
IgnoreIDMatch: true,
IgnoreNameMatch: false,
URLs: []string{"http://www.opensource.org/licenses/mit-license.php", "https://opensource.org/licenses/MIT"},
},
PrimaryPatterns: []*PrimaryPatterns{
{
Text: "\n\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of <<match=this|the>>",
re: re,
FileName: "license_MIT.txt",
},
},
PrimaryPatternsSources: []PrimaryPatternsSources{
{
SourceText: "\n\n\nPermission is hereby granted, free of charge, " +
"to any person obtaining a copy of <<match=this|the>" +
"> <<match=.*>> <<beginOptional>>software <<match=and/?o?r?>> ass" +
"ociated documentation<<endOptional>> <<beginOptional>>SOFTWARE<<" +
`endOptional>> <<beginOptional>><<match=files?>> (the <<match="?S` +
`oftware"?|"?Materials"?>>),<<endOptional>> to deal in the <<matc` +
"h=Software|Code|Materials>> without restriction, including witho" +
"ut <<match=limitation,?>> <<beginOptional>>on<<endOptional>> the " +
"<<beginOptional>>irrevocable, perpetual, worldwide, and royalty" +
"-free<<endOptional>> rights to use, copy, modify, merge, publish" +
", distribute, <<beginOptional>>sublicense,<<endOptional>> <<begi" +
"nOptional>>distribute with modifications,<<endOptional>> <<begin" +
"Optional>><<match=sub ?license,?>><<endOptional>> <<beginOptiona" +
`l>>display, perform, create derivative works from<<endOptional>> ` +
"<<match=and ?/ ?or>> sell copies of the <<match=Software|code|M" +
"aterials>>, <<beginOptional>> both in source<<endOptional>> and " +
"<<beginOptional>>object code form, and<<endOptional>> to permit " +
"persons to whom the <<match=Software|code|materials>> <<match=is" +
"|are>> furnished to do so, subject to the following <<match=cond" +
"itions|disclaimer>>:\n\n<<beginOptional>>\nThe above copyright noti" +
"ce<<match= and|,>> this permission notice <<beginOptional>>and t" +
"he disclaimer statement<<endOptional>> <<beginOptional>>(includi" +
"ng the next\nparagraph)<<endOptional>> <<match=shall|must>> be in" +
"cluded in all copies or substantial portions of the <<match=Soft" +
"ware|code|materials>>.\n<<endOptional>>\n",
Filename: "license_MIT.txt",
},
},
AssociatedPatterns: nil,
AssociatedPatternsSources: nil,
Text: LicenseText{
ContentType: "",
Encoding: "",
Content: "",
},
},
wantErr: false,
},
{
name: "Apache-2.0 needs retry to map URL to URLs",
id: "Apache-2.0",
want: License{
SPDXLicenseID: "Apache-2.0",
LicenseInfo: LicenseInfo{
Name: "Apache License 2.0",
Family: "Apache",
SPDXStandard: true,
SPDXException: false,
OSIApproved: true,
IgnoreIDMatch: false,
IgnoreNameMatch: false,
Aliases: []string{"Apache License, Version 2.0", "Apache License v. 2.0", "Apache License Version 2.0", "Apache Software License v2.0"},
URLs: []string{"http://www.apache.org/licenses/LICENSE-2.0"},
},
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
ll, err := NewLicenseLibrary(nil)
if err != nil {
t.Errorf("NewLicenseLibrary() error = %v", err)
}
err = AddLicense(tt.id, ll)
if err != nil {
if tt.wantErr == false {
t.Fatalf("AddLicense() error = %v, wantErr %v", err, tt.wantErr)
} else {
Logger.Infof("AddLicense() wantErr %v, error = %v", tt.wantErr, err)
}
} else {
if d := cmp.Diff(tt.want.LicenseInfo, ll.LicenseMap[tt.id].LicenseInfo); d != "" {
t.Errorf("Didn't get expected License: (-want, +got): %v", d)
}
}
})
}
}
func TestLicenses_AddLicenses(t *testing.T) {
tests := []struct {
name string
expected int
wantErr bool
}{
{
name: "add and count licenses",
expected: expectedLicenseCount,
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
ll, err := NewLicenseLibrary(nil)
if err != nil {
t.Fatalf("NewLicenseLibrary() error = %v", err)
}
if err := ll.AddAll(); (err != nil) != tt.wantErr {
t.Errorf("AddAll() error = %v, wantErr %v", err, tt.wantErr)
}
numGot := len(ll.LicenseMap)
if numGot != tt.expected {
t.Errorf("AddAll() length check wanted = %v, got %v", tt.expected, numGot)
}
})
}
}
// TestLicenses_PrintLicenses is for handy listing or our inventory. Not really for testing.
func TestLicenses_PrintLicenses(t *testing.T) {
tests := []struct {
name string
expected int
wantErr bool
}{
{
name: "license listing",
expected: expectedLicenseCount,
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
ll, err := NewLicenseLibrary(nil)
if err != nil {
t.Fatalf("NewLicenseLibrary() error = %v", err)
}
if err := ll.AddLicenses(); (err != nil) != tt.wantErr {
t.Errorf("AddLicenses() error = %v, wantErr %v", err, tt.wantErr)
}
var osiApproved []License
var notApproved []License
for _, lic := range ll.LicenseMap {
if lic.LicenseInfo.OSIApproved {
osiApproved = append(osiApproved, lic)
} else {
notApproved = append(notApproved, lic)
}
}
sort.Slice(osiApproved, func(i, j int) bool {
return osiApproved[i].GetID() < osiApproved[j].GetID()
})
sort.Slice(notApproved, func(i, j int) bool {
return notApproved[i].GetID() < notApproved[j].GetID()
})
fmt.Println("OSI APPROVED LICENSES:")
fmt.Println("======================")
for _, lic := range osiApproved {
fmt.Printf("SPDXID: %v, Name: %v, Alias: %v, SPDXStandard: %v, SPDXException: %v, OSIApproved: %v URLS: %v\n", lic.SPDXLicenseID, lic.LicenseInfo.Name, lic.LicenseInfo.Aliases, lic.LicenseInfo.SPDXStandard, lic.LicenseInfo.SPDXException, lic.LicenseInfo.OSIApproved, lic.LicenseInfo.URLs)
}
fmt.Println("")
fmt.Println("NOT APPROVED LICENSES:")
fmt.Println("======================")
for _, lic := range notApproved {
fmt.Printf("SPDXID: %v, Name: %v, Alias: %v, SPDXStandard: %v, SPDXException: %v, OSIApproved: %v URLS: %v\n", lic.SPDXLicenseID, lic.LicenseInfo.Name, lic.LicenseInfo.Aliases, lic.LicenseInfo.SPDXStandard, lic.LicenseInfo.SPDXException, lic.LicenseInfo.OSIApproved, lic.LicenseInfo.URLs)
}
})
}
}
// TestLicenseLibrary_AddAll makes sure that the loading of license maps works as expected
// The hard-coded sizes here might make this test only of temporary usefulness (could count files?).
func TestLicenseLibrary_AddAll(t *testing.T) {
config, err := configurer.InitConfig(nil)
if err != nil {
t.Fatal(err)
}
resourcesFlag := configurer.NewDefaultFlags()
err = resourcesFlag.Set(configurer.ConfigPathFlag, "../testdata/config/")
if err != nil {
t.Fatal(err)
}
configWithResources, err := configurer.InitConfig(resourcesFlag)
if err != nil {
t.Fatal(err)
}
defaultLL, err := NewLicenseLibrary(nil)
if err != nil {
t.Fatalf("NewLicenseLibrary(nil) error = %v", err)
}
configLL, err := NewLicenseLibrary(config)
if err != nil {
t.Fatalf("NewLicenseLibrary(config) error = %v", err)
}
configWithResourcesLL, err := NewLicenseLibrary(configWithResources)
if err != nil {
t.Fatalf("NewLicenseLibrary(configWithResources) error = %v", err)
}
tests := []struct {
name string
ll *LicenseLibrary
expectedSizes map[string]int
}{
{
name: "default license library",
ll: defaultLL,
expectedSizes: map[string]int{
"LicenseMap": expectedLicenseCount,
"PrimaryPatternPreCheckMap": expectedPrecheckCount,
"AcceptablePatternsMap": acceptablePatternsCount,
},
},
{
name: "config license library",
ll: configLL,
expectedSizes: map[string]int{
"LicenseMap": expectedLicenseCount,
"PrimaryPatternPreCheckMap": expectedPrecheckCount,
"AcceptablePatternsMap": acceptablePatternsCount,
},
},
{
name: "config resources path",
ll: configWithResourcesLL,
expectedSizes: map[string]int{
"LicenseMap": 1,
"PrimaryPatternPreCheckMap": 0,
"AcceptablePatternsMap": 1,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// t.Parallel()
if err := tt.ll.AddAll(); err != nil {
t.Fatalf("AddAll() error = %v", err)
}
actual := map[string]int{
"LicenseMap": len(tt.ll.LicenseMap),
"PrimaryPatternPreCheckMap": len(tt.ll.PrimaryPatternPreCheckMap),
"AcceptablePatternsMap": len(tt.ll.AcceptablePatternsMap),
}
if d := cmp.Diff(tt.expectedSizes, actual); d != "" {
t.Errorf("Didn't get expected LicenseLibrary map sizes: (-want, +got): %v", d)
}
})
}
}
func TestLicenseLibrary_PreChecks(t *testing.T) {
tests := []struct {
name string
configPath string
expectedSizes map[string]int
}{
{
name: "no_prechecks",
configPath: "../testdata/prechecks/no_prechecks",
expectedSizes: map[string]int{
"LicenseMapLen": 1,
"PrimaryPatternPreCheckMap": 0,
},
},
{
name: "static_prechecks",
configPath: "../testdata/prechecks/static_prechecks",
expectedSizes: map[string]int{
"LicenseMapLen": 1,
"PrimaryPatternPreCheckMap": 1,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
flagSet := configurer.NewDefaultFlags()
if err := flagSet.Set(configurer.ConfigPathFlag, tt.configPath); err != nil {
t.Fatal(err)
}
config, err := configurer.InitConfig(flagSet)
if err != nil {
t.Fatal(err)
}
ll, err := NewLicenseLibrary(config)
if err != nil {
t.Fatalf("NewLicenseLibrary(config) error = %v", err)
}
if err := ll.AddAll(); err != nil {
t.Fatalf("AddAll() error = %v", err)
}
actual := map[string]int{
"LicenseMapLen": len(ll.LicenseMap),
"PrimaryPatternPreCheckMap": len(ll.PrimaryPatternPreCheckMap),
}
if d := cmp.Diff(tt.expectedSizes, actual); d != "" {
t.Errorf("Didn't get expected LicenseLibrary map sizes: (-want, +got): %v", d)
}
})
}
}
// Test to make sure the bools are not defaulting to false and the strings are getting wrapped as slices where needed
func TestLicenseUnmarshal(t *testing.T) {
fileContents := []byte(`
{
"name": "test",
"family": "test",
"spdx_standard": true,
"spdx_exception": true,
"osi_approved": true,
"ignore_id_match": true,
"ignore_name_match": true,
"aliases": "test",
"urls": "test",
"eligible_licenses": "test",
"is_mutator": true
}
`)
li, err := readLicenseInfoJSON(fileContents)
if err != nil {
t.Fatal(err)
}
// Make sure the bools all read true after reading (defaulted fields are false)
if !li.SPDXStandard || !li.SPDXException || !li.OSIApproved || !li.IgnoreIDMatch || !li.IgnoreNameMatch || !li.IsMutator {
t.Fatal("readLicenseInfoJson failed to read a bool as true")
}
if li.Name != "test" || li.Family != "test" {
t.Logf("%+v", li)
t.Fatal("readLicenseInfoJson failed to read a string (that is supposed to be a string)")
}
expectedSlice := []string{"test"}
gotSlices := [][]string{li.Aliases, li.URLs, li.EligibleLicenses}
for _, got := range gotSlices {
if d := cmp.Diff(expectedSlice, got); d != "" {
t.Logf("%+v", li)
t.Errorf("Didn't get expected slice: (-want, +got): %v", d)
}
}
}