-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults_test.go
More file actions
110 lines (93 loc) · 3.02 KB
/
defaults_test.go
File metadata and controls
110 lines (93 loc) · 3.02 KB
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
package purl
import (
"testing"
)
func TestIsDefaultRegistry(t *testing.T) {
tests := []struct {
purlType string
registryURL string
want bool
}{
// Empty URL is always default
{"npm", "", true},
{"pypi", "", true},
// npm default registry (from types.json: https://registry.npmjs.org)
{"npm", "https://registry.npmjs.org", true},
{"npm", "https://npm.example.com", false},
// pypi default registry (from types.json: https://pypi.org)
{"pypi", "https://pypi.org", true},
{"pypi", "https://pypi.example.com", false},
// cargo default registry (from types.json: https://crates.io)
{"cargo", "https://crates.io", true},
{"cargo", "https://cargo.example.com", false},
// gem default registry (from types.json: https://rubygems.org)
{"gem", "https://rubygems.org", true},
{"gem", "https://gems.example.com", false},
// maven default registry (from types.json: https://repo.maven.apache.org/maven2)
{"maven", "https://repo.maven.apache.org", true},
{"maven", "https://maven.example.com", false},
// golang default registry (from types.json: https://pkg.go.dev)
{"golang", "https://pkg.go.dev", true},
{"golang", "https://go.example.com", false},
// docker default registry (from types.json: https://hub.docker.com)
{"docker", "https://hub.docker.com", true},
{"docker", "https://gcr.io", false},
// subdomain matching
{"npm", "https://cdn.registry.npmjs.org", true},
// Types with no default registry
{"apk", "https://example.com", false},
{"deb", "https://example.com", false},
// Unknown type
{"unknown-type", "https://example.com", false},
}
for _, tt := range tests {
name := tt.purlType + ":" + tt.registryURL
if tt.registryURL == "" {
name = tt.purlType + ":empty"
}
t.Run(name, func(t *testing.T) {
if got := IsDefaultRegistry(tt.purlType, tt.registryURL); got != tt.want {
t.Errorf("IsDefaultRegistry(%q, %q) = %v, want %v", tt.purlType, tt.registryURL, got, tt.want)
}
})
}
}
func TestIsNonDefaultRegistry(t *testing.T) {
tests := []struct {
purlType string
registryURL string
want bool
}{
{"npm", "", false},
{"npm", "https://registry.npmjs.org", false},
{"npm", "https://npm.example.com", true},
}
for _, tt := range tests {
t.Run(tt.purlType+":"+tt.registryURL, func(t *testing.T) {
if got := IsNonDefaultRegistry(tt.purlType, tt.registryURL); got != tt.want {
t.Errorf("IsNonDefaultRegistry(%q, %q) = %v, want %v", tt.purlType, tt.registryURL, got, tt.want)
}
})
}
}
func TestIsPrivateRegistry(t *testing.T) {
tests := []struct {
purl string
want bool
}{
{"pkg:npm/lodash", false},
{"pkg:npm/lodash?repository_url=https://registry.npmjs.org", false},
{"pkg:npm/lodash?repository_url=https://npm.example.com", true},
}
for _, tt := range tests {
t.Run(tt.purl, func(t *testing.T) {
p, err := Parse(tt.purl)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if got := p.IsPrivateRegistry(); got != tt.want {
t.Errorf("IsPrivateRegistry() = %v, want %v", got, tt.want)
}
})
}
}