Repository navigation
Expand file tree
/
Copy pathdefaults_test.go
More file actions
128 lines (110 loc) · 3.47 KB
/
Copy pathdefaults_test.go
File metadata and controls
128 lines (110 loc) · 3.47 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 TestExtractHostRejectsUserinfo(t *testing.T) {
tests := []struct {
input string
want string
}{
{"https://evil.com@registry.npmjs.org/path", "registry.npmjs.org"},
{"https://registry.npmjs.org", "registry.npmjs.org"},
{"https://user:pass@evil.com/path", "evil.com"},
{"not-a-url", ""},
}
for _, tt := range tests {
got := extractHost(tt.input)
if got != tt.want {
t.Errorf("extractHost(%q) = %q, want %q", tt.input, 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)
}
})
}
}