-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappconfig_test.go
More file actions
665 lines (559 loc) · 17.9 KB
/
appconfig_test.go
File metadata and controls
665 lines (559 loc) · 17.9 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package sysproxy
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func installFakeCommand(t *testing.T, name string) string {
t.Helper()
dir := t.TempDir()
logPath := filepath.Join(dir, name+".log")
var (
scriptPath string
content string
)
if runtime.GOOS == "windows" {
scriptPath = filepath.Join(dir, name+".bat")
content = "@echo off\r\n" +
"if not \"%SYSPROXY_TEST_FAIL_ON%\"==\"\" (\r\n" +
" echo %* | findstr /C:\"%SYSPROXY_TEST_FAIL_ON%\" >nul && exit /b 1\r\n" +
")\r\n" +
"echo %*>>\"%SYSPROXY_TEST_LOG%\"\r\n"
} else {
scriptPath = filepath.Join(dir, name)
content = "#!/bin/sh\n" +
"if [ -n \"$SYSPROXY_TEST_FAIL_ON\" ]; then\n" +
" case \"$*\" in\n" +
" *\"$SYSPROXY_TEST_FAIL_ON\"*) exit 1 ;;\n" +
" esac\n" +
"fi\n" +
"printf '%s\\n' \"$*\" >> \"$SYSPROXY_TEST_LOG\"\n"
}
if err := os.WriteFile(scriptPath, []byte(content), 0o755); err != nil { //nolint:gosec
t.Fatal(err)
}
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("SYSPROXY_TEST_LOG", logPath)
t.Setenv("SYSPROXY_TEST_FAIL_ON", "")
return logPath
}
func readCommandLog(t *testing.T, path string) []string {
t.Helper()
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
if os.IsNotExist(err) {
return nil
}
t.Fatal(err)
}
text := strings.TrimSpace(string(data))
if text == "" {
return nil
}
lines := strings.Split(text, "\n")
for i, line := range lines {
lines[i] = strings.TrimSuffix(line, "\r")
}
return lines
}
func TestWriteGitProxy(t *testing.T) {
logPath := installFakeCommand(t, "git")
if err := writeGitProxy(context.Background(), "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
want := []string{
"config --global http.proxy http://proxy.example.com:8080",
"config --global https.proxy http://proxy.example.com:8080",
}
if strings.Join(lines, "\n") != strings.Join(want, "\n") {
t.Fatalf("git commands = %q, want %q", lines, want)
}
}
func TestWriteAppConfigGit(t *testing.T) {
logPath := installFakeCommand(t, "git")
if err := WriteAppConfig(AppGit, "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
if len(lines) != 2 {
t.Fatalf("expected two git config calls, got %q", lines)
}
}
func TestWriteGitProxyNotFound(t *testing.T) {
t.Setenv("PATH", t.TempDir())
err := writeGitProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "git not found") {
t.Fatalf("expected git not found error, got %v", err)
}
}
func TestWriteGitProxyCommandFailure(t *testing.T) {
_ = installFakeCommand(t, "git")
t.Setenv("SYSPROXY_TEST_FAIL_ON", "http.proxy")
err := writeGitProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "git config http.proxy") {
t.Fatalf("expected wrapped git command error, got %v", err)
}
}
func TestClearGitProxy(t *testing.T) {
logPath := installFakeCommand(t, "git")
if err := clearGitProxy(context.Background()); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
want := []string{
"config --global --unset http.proxy",
"config --global --unset https.proxy",
}
if strings.Join(lines, "\n") != strings.Join(want, "\n") {
t.Fatalf("git commands = %q, want %q", lines, want)
}
}
func TestClearAppConfigGit(t *testing.T) {
logPath := installFakeCommand(t, "git")
if err := ClearAppConfig(AppGit); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
if len(lines) != 2 {
t.Fatalf("expected two git unset calls, got %q", lines)
}
}
func TestWriteNPMProxy(t *testing.T) {
logPath := installFakeCommand(t, "npm")
if err := writeNPMProxy(context.Background(), "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
want := []string{
"config set proxy http://proxy.example.com:8080",
"config set https-proxy http://proxy.example.com:8080",
}
if strings.Join(lines, "\n") != strings.Join(want, "\n") {
t.Fatalf("npm commands = %q, want %q", lines, want)
}
}
func TestWriteAppConfigNPM(t *testing.T) {
logPath := installFakeCommand(t, "npm")
if err := WriteAppConfig(AppNPM, "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
if len(lines) != 2 {
t.Fatalf("expected two npm config calls, got %q", lines)
}
}
func TestWriteNPMProxyCommandFailure(t *testing.T) {
_ = installFakeCommand(t, "npm")
t.Setenv("SYSPROXY_TEST_FAIL_ON", "https-proxy")
err := writeNPMProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "npm config set https-proxy") {
t.Fatalf("expected wrapped npm command error, got %v", err)
}
}
func TestClearNPMProxy(t *testing.T) {
logPath := installFakeCommand(t, "npm")
if err := clearNPMProxy(context.Background()); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
want := []string{
"config delete proxy",
"config delete https-proxy",
}
if strings.Join(lines, "\n") != strings.Join(want, "\n") {
t.Fatalf("npm commands = %q, want %q", lines, want)
}
}
func TestClearAppConfigNPM(t *testing.T) {
logPath := installFakeCommand(t, "npm")
if err := ClearAppConfig(AppNPM); err != nil {
t.Fatal(err)
}
lines := readCommandLog(t, logPath)
if len(lines) != 2 {
t.Fatalf("expected two npm delete calls, got %q", lines)
}
}
func TestClearPipConf(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".config", "pip", "pip.conf")
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
content := "[global]\nproxy = http://proxy.example.com:8080\nindex-url = https://pypi.org/simple\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := clearPipConf(); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if strings.Contains(text, "proxy = ") {
t.Fatalf("proxy entry should be removed, got %q", text)
}
if !strings.Contains(text, "index-url = https://pypi.org/simple") {
t.Fatalf("expected unrelated pip settings to stay, got %q", text)
}
}
func TestClearAppConfigPip(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".config", "pip", "pip.conf")
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("[global]\nproxy = http://proxy.example.com:8080\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := ClearAppConfig(AppPip); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "proxy = ") {
t.Fatalf("proxy entry should be removed, got %q", string(data))
}
}
func TestClearWgetRC(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".wgetrc")
content := strings.Join([]string{
"http_proxy = http://proxy.example.com:8080",
"https_proxy = http://proxy.example.com:8080",
"use_proxy = on",
"",
}, "\n")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := clearWgetRC(); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if strings.Contains(text, "http_proxy = ") || strings.Contains(text, "https_proxy = ") {
t.Fatalf("proxy entries should be removed, got %q", text)
}
if !strings.Contains(text, "use_proxy = on") {
t.Fatalf("expected unrelated wget settings to stay, got %q", text)
}
}
func TestClearAppConfigWget(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".wgetrc")
if err := os.WriteFile(path, []byte("http_proxy = http://proxy.example.com:8080\nhttps_proxy = http://proxy.example.com:8080\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := ClearAppConfig(AppWget); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "proxy = ") {
t.Fatalf("proxy entries should be removed, got %q", string(data))
}
}
func TestClearAppConfigUnsupported(t *testing.T) {
if err := ClearAppConfig("burp"); err == nil {
t.Fatal("expected unsupported app error")
}
}
func TestWriteAppConfigContextCanceled(t *testing.T) {
logPath := installFakeCommand(t, "git")
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := WriteAppConfigContext(ctx, AppGit, "http://proxy.example.com:8080")
if err == nil || err != context.Canceled {
t.Fatalf("expected context canceled, got %v", err)
}
if lines := readCommandLog(t, logPath); len(lines) != 0 {
t.Fatalf("expected no git commands after cancellation, got %q", lines)
}
}
func TestClearAppConfigContextCanceled(t *testing.T) {
logPath := installFakeCommand(t, "npm")
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := ClearAppConfigContext(ctx, AppNPM)
if err == nil || err != context.Canceled {
t.Fatalf("expected context canceled, got %v", err)
}
if lines := readCommandLog(t, logPath); len(lines) != 0 {
t.Fatalf("expected no npm commands after cancellation, got %q", lines)
}
}
func TestEditINIFileReplacesOnlyTargetSection(t *testing.T) {
path := filepath.Join(t.TempDir(), "pip.conf")
content := strings.Join([]string{
"[global]",
"proxy = http://old.example.com:8080",
"index-url = https://pypi.org/simple",
"[install]",
"proxy = http://keep.example.com:8080",
"",
}, "\n")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := editINIFile(path, "global", "proxy", "http://new.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if !strings.Contains(text, "proxy = http://new.example.com:8080") {
t.Fatalf("expected updated proxy entry, got %q", text)
}
if strings.Contains(text, "proxy = http://old.example.com:8080") {
t.Fatalf("old proxy entry should be replaced, got %q", text)
}
if !strings.Contains(text, "[install]\nproxy = http://keep.example.com:8080") {
t.Fatalf("expected other section to stay unchanged, got %q", text)
}
}
func TestEditINIFileAddsMissingSection(t *testing.T) {
path := filepath.Join(t.TempDir(), "pip.conf")
content := "[install]\ntrusted-host = pypi.org\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := editINIFile(path, "global", "proxy", "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if !strings.Contains(text, "[global]\nproxy = http://proxy.example.com:8080\n") {
t.Fatalf("expected missing section to be appended, got %q", text)
}
}
func TestRemoveINIKeyOnlyAffectsTargetSection(t *testing.T) {
path := filepath.Join(t.TempDir(), "pip.conf")
content := strings.Join([]string{
"[global]",
"proxy = http://proxy.example.com:8080",
"index-url = https://pypi.org/simple",
"[install]",
"proxy = http://keep.example.com:8080",
"",
}, "\n")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := removeINIKey(path, "global", "proxy"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if strings.Contains(text, "[global]\nproxy = http://proxy.example.com:8080") {
t.Fatalf("global proxy entry should be removed, got %q", text)
}
if !strings.Contains(text, "index-url = https://pypi.org/simple") {
t.Fatalf("expected unrelated keys in target section to stay, got %q", text)
}
if !strings.Contains(text, "[install]\nproxy = http://keep.example.com:8080") {
t.Fatalf("expected same key in another section to stay, got %q", text)
}
}
func TestWriteCurlRC(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".curlrc")
if err := writeCurlRC("http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "proxy = http://proxy.example.com:8080") {
t.Fatalf("expected proxy entry, got %q", string(data))
}
}
func TestClearCurlRC(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".curlrc")
if err := os.WriteFile(path, []byte("proxy = http://proxy.example.com:8080\nmax-time = 30\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := clearCurlRC(); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "proxy = ") {
t.Fatalf("expected proxy entry removed, got %q", string(data))
}
if !strings.Contains(string(data), "max-time = 30") {
t.Fatalf("expected unrelated entry to stay, got %q", string(data))
}
}
func TestWriteWgetRC(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".wgetrc")
if err := writeWgetRC("http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if !strings.Contains(text, "http_proxy = http://proxy.example.com:8080") {
t.Fatalf("expected http_proxy entry, got %q", text)
}
if !strings.Contains(text, "https_proxy = http://proxy.example.com:8080") {
t.Fatalf("expected https_proxy entry, got %q", text)
}
}
func TestWritePipConf(t *testing.T) {
home := setTestHome(t)
path := filepath.Join(home, ".config", "pip", "pip.conf")
if err := writePipConf("http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
text := string(data)
if !strings.Contains(text, "[global]") {
t.Fatalf("expected [global] section, got %q", text)
}
if !strings.Contains(text, "proxy = http://proxy.example.com:8080") {
t.Fatalf("expected proxy entry, got %q", text)
}
}
func TestClearGitProxyNotFound(t *testing.T) {
t.Setenv("PATH", t.TempDir())
err := clearGitProxy(context.Background())
if err == nil || !strings.Contains(err.Error(), "git not found") {
t.Fatalf("expected git not found error, got %v", err)
}
}
func TestWriteNPMProxyNotFound(t *testing.T) {
t.Setenv("PATH", t.TempDir())
err := writeNPMProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "npm not found") {
t.Fatalf("expected npm not found error, got %v", err)
}
}
func TestClearNPMProxyNotFound(t *testing.T) {
t.Setenv("PATH", t.TempDir())
err := clearNPMProxy(context.Background())
if err == nil || !strings.Contains(err.Error(), "npm not found") {
t.Fatalf("expected npm not found error, got %v", err)
}
}
func TestWriteGitProxyHTTPSFailure(t *testing.T) {
_ = installFakeCommand(t, "git")
t.Setenv("SYSPROXY_TEST_FAIL_ON", "https.proxy")
err := writeGitProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "git config https.proxy") {
t.Fatalf("expected https.proxy error, got %v", err)
}
}
func TestWriteNPMProxyFirstCmdFailure(t *testing.T) {
_ = installFakeCommand(t, "npm")
t.Setenv("SYSPROXY_TEST_FAIL_ON", "set proxy")
err := writeNPMProxy(context.Background(), "http://proxy.example.com:8080")
if err == nil || !strings.Contains(err.Error(), "npm config set proxy") {
t.Fatalf("expected proxy set error, got %v", err)
}
}
func TestWritePipConfMkdirAllError(t *testing.T) {
home := setTestHome(t)
// Block the pip config dir by creating a file where the directory would be.
if err := os.MkdirAll(filepath.Join(home, ".config"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(home, ".config", "pip"), []byte("not a dir"), 0o600); err != nil {
t.Fatal(err)
}
if err := writePipConf("http://proxy.example.com:8080"); err == nil {
t.Fatal("expected error when pip dir blocked by file")
}
}
func TestEditKeyValueFileReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission bits not enforced the same way on Windows")
}
path := filepath.Join(t.TempDir(), "conf")
if err := os.WriteFile(path, []byte("key = val\n"), 0o000); err != nil {
t.Fatal(err)
}
if err := editKeyValueFile(path, "key", "newval", " = "); err == nil {
t.Fatal("expected error reading unreadable file")
}
}
func TestRemoveKeysFromFileReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission bits not enforced the same way on Windows")
}
path := filepath.Join(t.TempDir(), "conf")
if err := os.WriteFile(path, []byte("key = val\n"), 0o000); err != nil {
t.Fatal(err)
}
if err := removeKeysFromFile(path, " = ", "key"); err == nil {
t.Fatal("expected error reading unreadable file")
}
}
func TestRemoveINIKeyReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission bits not enforced the same way on Windows")
}
path := filepath.Join(t.TempDir(), "conf.ini")
if err := os.WriteFile(path, []byte("[global]\nproxy = http://proxy.example.com\n"), 0o000); err != nil {
t.Fatal(err)
}
if err := removeINIKey(path, "global", "proxy"); err == nil {
t.Fatal("expected error reading unreadable file")
}
}
func TestEditINIFileReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission bits not enforced the same way on Windows")
}
path := filepath.Join(t.TempDir(), "conf.ini")
if err := os.WriteFile(path, []byte("[global]\nproxy = old\n"), 0o000); err != nil {
t.Fatal(err)
}
if err := editINIFile(path, "global", "proxy", "http://proxy.example.com:8080"); err == nil {
t.Fatal("expected error reading unreadable file")
}
}
func TestWriteWgetRCReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission bits not enforced the same way on Windows")
}
home := setTestHome(t)
path := filepath.Join(home, ".wgetrc")
if err := os.WriteFile(path, []byte("old = val\n"), 0o000); err != nil {
t.Fatal(err)
}
if err := writeWgetRC("http://proxy.example.com:8080"); err == nil {
t.Fatal("expected error reading unreadable .wgetrc")
}
}