-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysproxy_linux_test.go
More file actions
105 lines (89 loc) · 2.79 KB
/
sysproxy_linux_test.go
File metadata and controls
105 lines (89 loc) · 2.79 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
//go:build linux
package sysproxy
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestWriteEtcEnvironment(t *testing.T) {
path := filepath.Join(t.TempDir(), "environment")
if err := writeEtcEnvironment(path, "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path) //nolint:gosec
if err != nil {
t.Fatal(err)
}
content := string(data)
for _, want := range []string{
"http_proxy=http://proxy.example.com:8080",
"HTTP_PROXY=http://proxy.example.com:8080",
"https_proxy=http://proxy.example.com:8080",
"no_proxy=localhost,127.0.0.1,::1",
} {
if !strings.Contains(content, want) {
t.Errorf("missing line %q in:\n%s", want, content)
}
}
}
func TestWriteEtcEnvironmentPreservesExistingLines(t *testing.T) {
path := filepath.Join(t.TempDir(), "environment")
existing := "EDITOR=vim\nPATH=/usr/local/bin\n"
if err := os.WriteFile(path, []byte(existing), 0o600); err != nil {
t.Fatal(err)
}
if err := writeEtcEnvironment(path, "http://proxy.example.com:8080"); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(path) //nolint:gosec
content := string(data)
if !strings.Contains(content, "EDITOR=vim") {
t.Error("EDITOR=vim should be preserved")
}
if !strings.Contains(content, "PATH=/usr/local/bin") {
t.Error("PATH=/usr/local/bin should be preserved")
}
}
func TestWriteEtcEnvironmentReplacesOldProxy(t *testing.T) {
path := filepath.Join(t.TempDir(), "environment")
old := "http_proxy=http://old:1111\nHTTP_PROXY=http://old:1111\n"
if err := os.WriteFile(path, []byte(old), 0o600); err != nil {
t.Fatal(err)
}
if err := writeEtcEnvironment(path, "http://new:2222"); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(path) //nolint:gosec
content := string(data)
if strings.Contains(content, "old:1111") {
t.Error("old proxy value should have been replaced")
}
if !strings.Contains(content, "new:2222") {
t.Error("new proxy value should be present")
}
}
func TestClearEtcEnvironment(t *testing.T) {
path := filepath.Join(t.TempDir(), "environment")
content := "EDITOR=vim\nhttp_proxy=http://proxy.example.com:8080\nHTTP_PROXY=http://proxy.example.com:8080\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := clearEtcEnvironment(path); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(path) //nolint:gosec
result := string(data)
if strings.Contains(result, "http_proxy") || strings.Contains(result, "HTTP_PROXY") {
t.Error("proxy keys should be removed")
}
if !strings.Contains(result, "EDITOR=vim") {
t.Error("non-proxy keys should be preserved")
}
}
func TestClearEtcEnvironmentMissingFile(t *testing.T) {
err := clearEtcEnvironment(filepath.Join(t.TempDir(), "nonexistent"))
if err != nil {
t.Errorf("missing file should not return error, got: %v", err)
}
}