-
Notifications
You must be signed in to change notification settings - Fork 7
/
processor_internal_test.go
387 lines (361 loc) · 8.79 KB
/
processor_internal_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
package gomodguard
import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
func TestIsModuleBlocked(t *testing.T) {
var tests = []struct {
testName string
processor Processor
testModule string
wantBlockReasons int
}{
{
"previous version blocked",
Processor{
blockedModulesFromModFile: map[string][]string{
"github.com/foo/bar": {blockReasonNotInAllowedList},
},
},
"github.com/foo/bar/v2",
0,
},
{
"ensure modules with similar prefixes are not blocked",
Processor{
blockedModulesFromModFile: map[string][]string{
"github.com/aws/aws-sdk-go": {blockReasonNotInAllowedList},
},
},
"github.com/aws/aws-sdk-go-v2",
0,
},
{
"ensure the same module is blocked",
Processor{
blockedModulesFromModFile: map[string][]string{
"github.com/foo/bar/v3": {blockReasonNotInAllowedList},
},
},
"github.com/foo/bar/v3",
1,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
blockReasons := tt.processor.isBlockedPackageFromModFile(tt.testModule)
if len(blockReasons) != tt.wantBlockReasons {
t.Logf(
"Testing %v, expected %v blockReasons, got %v blockReasons",
tt.testModule,
tt.wantBlockReasons,
len(blockReasons),
)
t.Fail()
}
})
}
}
func Test_packageInModule(t *testing.T) { //nolint:funlen
type args struct {
pkg string
mod string
}
tests := []struct {
name string
args args
wantPkgIsInMod bool
}{
{
name: "package with path bar",
args: args{
pkg: "github.com/acme/foo/bar",
mod: "github.com/acme/foo",
},
wantPkgIsInMod: true,
},
{
name: "package with path bar/baz",
args: args{
pkg: "github.com/acme/foo/bar/baz",
mod: "github.com/acme/foo",
},
wantPkgIsInMod: true,
},
{
name: "aws v1 package should not match v2 module",
args: args{
pkg: "github.com/aws/aws-sdk-go",
mod: "github.com/aws/aws-sdk-go-v2",
},
wantPkgIsInMod: false,
},
{
name: "aws v1 package should not match v2 module with a package path",
args: args{
pkg: "github.com/aws/aws-sdk-go/foo",
mod: "github.com/aws/aws-sdk-go-v2",
},
wantPkgIsInMod: false,
},
{
name: "aws v2 package should match v2 module",
args: args{
pkg: "github.com/aws/aws-sdk-go-v2",
mod: "github.com/aws/aws-sdk-go-v2",
},
wantPkgIsInMod: true,
},
{
name: "aws v2 package should match v2 module with a package path",
args: args{
pkg: "github.com/aws/aws-sdk-go-v2/foo",
mod: "github.com/aws/aws-sdk-go-v2",
},
wantPkgIsInMod: true,
},
{
name: "package with different major version",
args: args{
pkg: "github.com/foo/bar/v20",
mod: "github.com/foo/bar",
},
wantPkgIsInMod: false,
},
{
name: "package with no major version",
args: args{
pkg: "github.com/foo/bar",
mod: "github.com/foo/bar/v10",
},
wantPkgIsInMod: false,
},
{
name: "with different major version",
args: args{
pkg: "github.com/foo/bar/v40",
mod: "github.com/foo/bar/v41",
},
wantPkgIsInMod: false,
},
{
name: "with same major version",
args: args{
pkg: "github.com/foo/bar/v50",
mod: "github.com/foo/bar/v50",
},
wantPkgIsInMod: true,
},
{
name: "same major version with path",
args: args{
pkg: "github.com/foo/bar/v60/baz/taz",
mod: "github.com/foo/bar/v60",
},
wantPkgIsInMod: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPkgIsInMod := isPackageInModule(tt.args.pkg, tt.args.mod)
if gotPkgIsInMod != tt.wantPkgIsInMod {
t.Errorf("packageInModule() gotPkgIsInMod = %v, want %v", gotPkgIsInMod, tt.wantPkgIsInMod)
}
})
}
}
func TestProcessorSetBlockedModulesWithAllowList(t *testing.T) {
config := &Configuration{
Allowed: Allowed{
Modules: []string{
"gopkg.in/yaml.v3",
"github.com/go-xmlfmt/xmlfmt",
"github.com/Masterminds/semver/v3",
"github.com/ryancurrah/gomodguard",
},
Domains: []string{
"golang.org",
},
},
Blocked: Blocked{
Modules: BlockedModules{
{
"github.com/uudashr/go-module": BlockedModule{
Recommendations: []string{"golang.org/x/mod"},
Reason: "`mod` is the official go.mod parser library.",
},
},
{
"github.com/gofrs/uuid": BlockedModule{
Recommendations: []string{"github.com/ryancurrah/gomodguard"},
Reason: "testing if module is not blocked when it is recommended.",
},
},
},
Versions: BlockedVersions{
{
"github.com/mitchellh/go-homedir": BlockedVersion{
Version: "<= 1.1.0",
Reason: "testing if blocked version constraint works.",
},
},
},
LocalReplaceDirectives: true,
},
}
modfile := &modfile.File{
Module: &modfile.Module{
Mod: module.Version{
Path: "github.com/ryancurrah/gomodguard",
Version: "v1.0.0",
},
},
Require: []*modfile.Require{
{
Mod: module.Version{
Path: "gopkg.in/yaml.v3",
Version: "v3.0.1",
},
},
{
Mod: module.Version{
Path: "github.com/uudashr/go-module",
Version: "v1.0.0",
},
},
{
Mod: module.Version{
Path: "github.com/gofrs/uuid",
Version: "v1.2.3",
},
},
{
Mod: module.Version{
Path: "github.com/mitchellh/go-homedir",
Version: "v1.0.0",
},
},
},
Replace: []*modfile.Replace{
{
Old: module.Version{
Path: "github.com/gofrs/uuid",
Version: "v1.2.3",
},
New: module.Version{
Path: "/path/to/local/package",
Version: "",
},
},
},
}
processor := &Processor{
Config: config,
Modfile: modfile,
}
processor.SetBlockedModules()
// Assert number of blocked modules
assert.Len(t, processor.blockedModulesFromModFile, 3)
// Assert blocked modules
assert.Equal(t,
[]string{"import of package `%s` is blocked because the module is in the blocked modules list. " +
"`golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library."},
processor.blockedModulesFromModFile["github.com/uudashr/go-module"],
)
assert.Equal(t,
[]string{"import of package `%s` is blocked because the module has a local replace directive."},
processor.blockedModulesFromModFile["github.com/gofrs/uuid"],
)
assert.Equal(t,
[]string{"import of package `%s` is blocked because the module is in the blocked modules list. " +
"version `v1.0.0` is blocked because it does not meet the version constraint `<= 1.1.0`. testing " +
"if blocked version constraint works."},
processor.blockedModulesFromModFile["github.com/mitchellh/go-homedir"],
)
}
func TestProcessorSetBlockedModulesWithoutAllowList(t *testing.T) {
config := &Configuration{
Blocked: Blocked{
Modules: BlockedModules{
{
"gotest.tools/v3": BlockedModule{
Recommendations: []string{"github.com/stretchr/testify/assert"},
Reason: "We have standardized on `github.com/stretchr/testify/assert`.",
},
},
},
},
}
modfile := &modfile.File{
Module: &modfile.Module{
Mod: module.Version{
Path: "github.com/ryancurrah/gomodguard",
Version: "v1.0.0",
},
},
Require: []*modfile.Require{
{
Mod: module.Version{
Path: "gotest.tools/v3",
Version: "v3.4.0",
},
},
},
}
processor := &Processor{
Config: config,
Modfile: modfile,
}
processor.SetBlockedModules()
// Assert number of blocked modules
assert.Len(t, processor.blockedModulesFromModFile, 1)
// Assert blocked modules
assert.Equal(t,
[]string{"import of package `%s` is blocked because the module is in the blocked modules list. " +
"`github.com/stretchr/testify/assert` is a recommended module. We have standardized on `github.com/stretchr/testify/assert`."},
processor.blockedModulesFromModFile["gotest.tools/v3"],
)
}
func TestProcessorSetBlockedModulesWithInvalidVersionConstraint(t *testing.T) {
config := &Configuration{
Blocked: Blocked{
Versions: BlockedVersions{
{
"github.com/gin-gonic/gin": BlockedVersion{
Version: "== 1.0.0",
},
},
},
},
}
modfile := &modfile.File{
Module: &modfile.Module{
Mod: module.Version{
Path: "github.com/ryancurrah/gomodguard",
Version: "v1.0.0",
},
},
Require: []*modfile.Require{
{
Mod: module.Version{
Path: "github.com/gin-gonic/gin",
Version: "v1.0.0",
},
},
},
}
processor := &Processor{
Config: config,
Modfile: modfile,
}
processor.SetBlockedModules()
// Assert number of blocked modules
assert.Len(t, processor.blockedModulesFromModFile, 1)
// Assert blocked modules
assert.Equal(t,
[]string{"import of package `%s` is blocked because the version constraint is invalid. improper constraint: == 1.0.0"},
processor.blockedModulesFromModFile["github.com/gin-gonic/gin"],
)
}