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
//go:build linux
package desktop
import (
"errors"
"path/filepath"
"testing"
)
// fakeGSettings replaces gsettings and the backup file for a test, starting
// from the user's own manual proxy.
func fakeGSettings(t *testing.T) map[string]string {
t.Helper()
store := map[string]string{
proxySchema + " mode": "'manual'",
proxySchema + " autoconfig-url": "''",
proxySchema + " ignore-hosts": "['localhost']",
proxySchema + ".http host": "'proxy.example'",
proxySchema + ".http port": "3128",
proxySchema + ".https host": "'proxy.example'",
proxySchema + ".https port": "3128",
proxySchema + ".socks host": "''",
proxySchema + ".socks port": "0",
}
oldG, oldB := gsettings, backupPath
gsettings = func(args ...string) (string, error) {
k := args[1] + " " + args[2]
switch args[0] {
case "get":
v, ok := store[k]
if !ok {
return "", errors.New("no such key")
}
return v, nil
case "set":
store[k] = args[3]
return "", nil
}
return "", errors.New("unexpected command")
}
backup := filepath.Join(t.TempDir(), "previous-proxy.json")
backupPath = func() (string, error) { return backup, nil }
t.Cleanup(func() { gsettings, backupPath = oldG, oldB })
return store
}
func TestSystemProxySetAndRestore(t *testing.T) {
store := fakeGSettings(t)
if err := setSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
for _, kind := range []string{"http", "https", "socks"} {
if store[proxySchema+"."+kind+" host"] != "'127.0.0.1'" || store[proxySchema+"."+kind+" port"] != "10810" {
t.Fatalf("%s proxy = %s:%s", kind, store[proxySchema+"."+kind+" host"], store[proxySchema+"."+kind+" port"])
}
}
if store[proxySchema+" mode"] != "'manual'" {
t.Fatalf("mode = %s", store[proxySchema+" mode"])
}
// Setting it again keeps the user's setting as the one to restore.
if err := setSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
if err := restoreSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
if store[proxySchema+".http host"] != "'proxy.example'" || store[proxySchema+".http port"] != "3128" || store[proxySchema+" ignore-hosts"] != "['localhost']" {
t.Fatalf("the user's proxy did not come back: %v", store)
}
}
func TestSystemProxyRestoreLeavesTheUsersChoice(t *testing.T) {
store := fakeGSettings(t)
if err := setSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
store[proxySchema+".http host"] = "'other.example'"
if err := restoreSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
if store[proxySchema+".http host"] != "'other.example'" {
t.Fatalf("a proxy set meanwhile was replaced: %s", store[proxySchema+".http host"])
}
}
func TestSystemProxyRestoreWithoutBackupTurnsItOff(t *testing.T) {
store := fakeGSettings(t)
if err := setSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
deleteBackup()
if err := restoreSystemProxy("127.0.0.1:10810"); err != nil {
t.Fatal(err)
}
if store[proxySchema+" mode"] != "'none'" {
t.Fatalf("mode = %s", store[proxySchema+" mode"])
}
}
func TestSystemProxyClear(t *testing.T) {
store := fakeGSettings(t)
if err := clearSystemProxy(); err != nil {
t.Fatal(err)
}
if store[proxySchema+" mode"] != "'none'" {
t.Fatalf("mode = %s", store[proxySchema+" mode"])
}
}