Repository navigation
Expand file tree
/
Copy pathvalidations_test.go
More file actions
309 lines (281 loc) · 8.94 KB
/
Copy pathvalidations_test.go
File metadata and controls
309 lines (281 loc) · 8.94 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
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
package publiccode
import (
"bytes"
"image"
"image/png"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGetBasicAuth(t *testing.T) {
d := Domain{BasicAuth: []string{"user:pass"}}
result := getBasicAuth(d)
if !strings.HasPrefix(result, "Basic ") {
t.Errorf("expected 'Basic ' prefix, got %q", result)
}
}
func TestGetBasicAuthEmpty(t *testing.T) {
d := Domain{}
result := getBasicAuth(d)
if result != "" {
t.Errorf("expected empty string, got %q", result)
}
}
func TestIsHostInDomainTrue(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if !isHostInDomain(d, "https://example.com/foo") {
t.Error("expected true")
}
}
func TestIsHostInDomainFalse(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if isHostInDomain(d, "https://other.com/foo") {
t.Error("expected false")
}
}
func TestIsHostInDomainEmptyUseTokenFor(t *testing.T) {
d := Domain{}
if isHostInDomain(d, "https://example.com") {
t.Error("expected false for empty UseTokenFor")
}
}
func TestIsHostInDomainInvalidURL(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if isHostInDomain(d, "not-a-url") {
t.Error("expected false for relative URL with no host")
}
}
func TestIsHostInDomainMalformedURL(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
// "%" is a truly malformed URL that causes url.Parse to fail
if isHostInDomain(d, "%") {
t.Error("expected false for malformed URL")
}
}
func TestGetHeaderFromDomainNoMatch(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
headers := getHeaderFromDomain(d, "https://other.com/foo")
if headers != nil {
t.Error("expected nil headers when host not in domain")
}
}
func TestGetHeaderFromDomainMatch(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}, BasicAuth: []string{"user:pass"}}
headers := getHeaderFromDomain(d, "https://example.com/foo")
if headers == nil {
t.Fatal("expected non-nil headers")
}
if _, ok := headers["Authorization"]; !ok {
t.Error("expected Authorization header")
}
}
func TestIsReachableMissingScheme(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
_, err := p.isReachable(url.URL{Scheme: "", Host: "example.com", Path: "/"})
if err == nil {
t.Fatal("expected error for missing scheme")
}
if !strings.Contains(err.Error(), "missing URL scheme") {
t.Errorf("unexpected error: %v", err)
}
}
func TestIsReachableNon200(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true})
parsed, _ := url.Parse(srv.URL + "/path")
reachable, err := p.isReachable(*parsed)
if reachable {
t.Error("expected not reachable for 404 response")
}
if err == nil {
t.Error("expected error for non-200 response")
}
}
func TestIsImageFileInvalidExtension(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/tmp/test.gif"}
ok, err := p.isImageFile(u, false)
if ok {
t.Error("expected false for .gif extension")
}
if err == nil {
t.Error("expected error for invalid extension")
}
}
func TestIsImageFileValidExtensionMissing(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/nonexistent/test.png"}
ok, err := p.isImageFile(u, false)
if ok {
t.Error("expected false for nonexistent file")
}
if err == nil {
t.Error("expected error for nonexistent file")
}
}
func TestValidLogoInvalidExtension(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/tmp/test.gif"}
ok, err := p.validLogo(u, false)
if ok {
t.Error("expected false for .gif extension")
}
if err == nil {
t.Error("expected error for invalid extension")
}
}
func TestValidLogoMissingFile(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/nonexistent/logo.svg"}
ok, err := p.validLogo(u, false)
if ok {
t.Error("expected false for nonexistent file")
}
if err == nil {
t.Error("expected error for nonexistent file")
}
}
func TestValidLogoRemoteNoNetwork(t *testing.T) {
// Remote URL but network disabled: validLogo returns true for svg without downloading.
p, _ := NewParser(ParserConfig{DisableNetwork: true})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
parsed, _ := url.Parse(srv.URL + "/logo.svg")
// network=false: fileExists returns true for non-file scheme, and validLogo skips download
ok, err := p.validLogo(*parsed, false)
if !ok {
t.Errorf("expected true for remote SVG with network=false (no download): %v", err)
}
}
func TestValidLogoRemoteDownloadFailure(t *testing.T) {
// Remote PNG URL that is reachable (200) but returns non-PNG data.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "logo.png") {
// Return 200 but with invalid PNG content
w.Header().Set("Content-Type", "image/png")
_, _ = w.Write([]byte("not a valid PNG file at all"))
} else {
w.WriteHeader(http.StatusOK)
}
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true})
parsed, _ := url.Parse(srv.URL + "/logo.png")
ok, err := p.validLogo(*parsed, true)
// The downloaded file is not a valid PNG, so DecodeConfig should fail.
if ok {
t.Error("expected false for invalid PNG content")
}
if err == nil {
t.Error("expected error for invalid PNG content")
}
}
func TestIsReachableSuccess(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true})
parsed, _ := url.Parse(srv.URL + "/path")
reachable, err := p.isReachable(*parsed)
if !reachable {
t.Errorf("expected reachable for 200 response, got err: %v", err)
}
}
func TestValidLogoRemoteSingleFetch(t *testing.T) {
var pngBody bytes.Buffer
if err := png.Encode(&pngBody, image.NewRGBA(image.Rect(0, 0, 1, 1))); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
body string
status int
wantError string
}{
{name: "logo.svg", body: "<svg/>"},
{name: "logo.svgz", body: "compressed SVG"},
{name: "logo.jpg", body: "JPEG"},
{name: "logo.jpeg", body: "JPEG"},
{name: "logo.webp", body: "WebP"},
{name: "logo.avif", body: "AVIF"},
{name: "logo.png", body: pngBody.String()},
{name: "invalid.png", body: "invalid PNG", wantError: "image: unknown format"},
{name: "missing.svg", status: http.StatusInternalServerError, wantError: "unexpected status 500"},
} {
t.Run(tc.name, func(t *testing.T) {
tmp := t.TempDir()
t.Setenv("TMPDIR", tmp)
requests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
if r.Header.Get("Authorization") != "Basic dXNlcjpwYXNz" {
t.Error("missing configured authorization")
}
if requests > 1 {
w.WriteHeader(http.StatusInternalServerError)
return
}
if tc.status != 0 {
w.WriteHeader(tc.status)
}
_, _ = w.Write([]byte(tc.body))
}))
defer srv.Close()
p, err := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true})
if err != nil {
t.Fatal(err)
}
u, _ := url.Parse(srv.URL + "/" + tc.name)
p.domain = Domain{UseTokenFor: []string{u.Host}, BasicAuth: []string{"user:pass"}}
ok, err := p.validLogo(*u, true)
if tc.wantError == "" {
if !ok || err != nil {
t.Fatalf("expected valid logo, got %v, %v", ok, err)
}
} else if ok || err == nil || !strings.Contains(err.Error(), tc.wantError) {
t.Fatalf("expected %q, got %v, %v", tc.wantError, ok, err)
}
if tc.status != 0 && strings.Count(err.Error(), "downloading ") != 1 {
t.Errorf("expected one downloading prefix: %v", err)
}
if requests != 1 {
t.Errorf("expected one request, got %d", requests)
}
entries, err := os.ReadDir(tmp)
if err != nil || len(entries) != 0 {
t.Errorf("temporary directory is not empty: %v, %v", entries, err)
}
})
}
}
func TestValidLogoLocalPNG(t *testing.T) {
p, err := NewParser(ParserConfig{DisableNetwork: true})
if err != nil {
t.Fatal(err)
}
filename := filepath.Join(t.TempDir(), "logo.png")
var body bytes.Buffer
if err := png.Encode(&body, image.NewRGBA(image.Rect(0, 0, 1, 1))); err != nil {
t.Fatal(err)
}
for _, content := range [][]byte{body.Bytes(), []byte("invalid PNG")} {
if err := os.WriteFile(filename, content, 0o600); err != nil {
t.Fatal(err)
}
ok, err := p.validLogo(url.URL{Scheme: "file", Path: filename}, false)
wantValid := bytes.Equal(content, body.Bytes())
if ok != wantValid || (err == nil) != wantValid {
t.Errorf("expected valid=%v, got %v, %v", wantValid, ok, err)
}
}
}