forked from samuong/alpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacfinder_test.go
103 lines (91 loc) · 2.35 KB
/
pacfinder_test.go
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
package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestFindPACURLForDarwin(t *testing.T) {
dir, err := ioutil.TempDir("", "alpaca")
require.Nil(t, err)
defer os.RemoveAll(dir)
oldpath := os.Getenv("PATH")
defer require.Nil(t, os.Setenv("PATH", oldpath))
require.Nil(t, os.Setenv("PATH", dir+":"+oldpath))
tmpfn := filepath.Join(dir, "networksetup")
mockcmd := `#!/bin/sh
listallnetworkservices() {
cat <<EOF
An asterisk (*) denotes that a network service is disabled.
iPhone USB
iPhone USB 2
(*)Wi-Fi
Bluetooth PAN
Thunderbolt Bridge
EOF
}
getautoproxyurl() {
if [ "$1" = 'Wi-Fi' ]
then
cat <<EOF
URL: http://internal.anz.com/proxy.pac
Enabled: No
EOF
else
cat <<EOF
URL: (null)
Enabled: No
EOF
fi
}
if [ "$1" = '-listallnetworkservices' ]
then
listallnetworkservices "$2"
elif [ "$1" = '-getautoproxyurl' ]
then
getautoproxyurl "$2"
else
exit 1
fi
exit 0`
require.Nil(t, ioutil.WriteFile(tmpfn, []byte(mockcmd), 0700))
pacURL, err := findPACURLForDarwin()
require.Nil(t, err)
assert.Equal(t, "http://internal.anz.com/proxy.pac", pacURL)
}
func TestFindPACURLForDarwinWhenNetworkSetupIsntAvailable(t *testing.T) {
dir, err := ioutil.TempDir("", "alpaca")
require.Nil(t, err)
defer os.RemoveAll(dir)
oldpath := os.Getenv("PATH")
defer require.Nil(t, os.Setenv("PATH", oldpath))
require.Nil(t, os.Setenv("PATH", dir))
_, err = findPACURLForDarwin()
require.NotNil(t, err)
}
func TestFindPACURLForGNOME(t *testing.T) {
dir, err := ioutil.TempDir("", "alpaca")
require.Nil(t, err)
defer os.RemoveAll(dir)
oldpath := os.Getenv("PATH")
defer require.Nil(t, os.Setenv("PATH", oldpath))
require.Nil(t, os.Setenv("PATH", dir))
tmpfn := filepath.Join(dir, "gsettings")
mockcmd := "#!/bin/sh\necho \\'http://internal.anz.com/proxy.pac\\'\n"
require.Nil(t, ioutil.WriteFile(tmpfn, []byte(mockcmd), 0700))
pacURL, err := findPACURLForGNOME()
require.Nil(t, err)
assert.Equal(t, "http://internal.anz.com/proxy.pac", pacURL)
}
func TestFindPACURLForGNOMEWhenGsettingsIsntAvailable(t *testing.T) {
dir, err := ioutil.TempDir("", "alpaca")
require.Nil(t, err)
defer os.RemoveAll(dir)
oldpath := os.Getenv("PATH")
defer require.Nil(t, os.Setenv("PATH", oldpath))
require.Nil(t, os.Setenv("PATH", dir))
_, err = findPACURLForGNOME()
require.NotNil(t, err)
}