-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_test.go
162 lines (146 loc) · 7.11 KB
/
common_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
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
package config
import (
"compress/gzip"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"sync/atomic"
"testing"
commonconfig "github.com/getlantern/common/config"
"github.com/getlantern/rot13"
"github.com/getlantern/yaml"
)
// withTempDir creates a temporary directory, executes the given function and
// then cleans up the temporary directory. inTempDir allows modifying filenames
// to put them in the temporary directory.
func withTempDir(t *testing.T, fn func(inTempDir func(file string) string)) {
tmpDir, err := ioutil.TempDir("", "test")
abortOnError(t, err)
defer os.RemoveAll(tmpDir)
fn(func(file string) string {
return filepath.Join(tmpDir, file)
})
}
// writeObfuscatedConfig serializes the given config onto disk as YAML with
// ROT13 encoding.
func writeObfuscatedConfig(t *testing.T, config interface{}, obfuscatedFilename string) {
log.Debugf("Writing obfuscated config to %v", obfuscatedFilename)
bytes, err := yaml.Marshal(config)
abortOnError(t, err)
// create new obfuscated file
outfile, err := os.OpenFile(obfuscatedFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
abortOnError(t, err)
defer outfile.Close()
// write ROT13-encoded config to obfuscated file
out := rot13.NewWriter(outfile)
_, err = out.Write(bytes)
abortOnError(t, err)
}
func startConfigServer(t *testing.T, config interface{}) (u string, reqCount func() int64) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
requests := int64(0)
hs := &http.Server{
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
atomic.AddInt64(&requests, 1)
bytes, _ := yaml.Marshal(config)
w := gzip.NewWriter(resp)
defer w.Close()
w.Write(bytes)
}),
}
go func() {
if err = hs.Serve(l); err != nil {
t.Errorf("Unable to serve: %v", err)
}
}()
port := l.Addr().(*net.TCPAddr).Port
url := "http://localhost:" + strconv.Itoa(port)
return url, func() int64 {
return atomic.LoadInt64(&requests)
}
}
func newGlobalConfig(t *testing.T) *Global {
global := &Global{}
err := yaml.Unmarshal([]byte(globalYamlTemplate), global)
abortOnError(t, err)
return global
}
func newProxiesConfig(t *testing.T) map[string]*commonconfig.ProxyConfig {
proxies := make(map[string]*commonconfig.ProxyConfig)
err := yaml.Unmarshal([]byte(proxiesYamlTemplate), proxies)
abortOnError(t, err)
return proxies
}
// abortOnError aborts the current test if there's an error
func abortOnError(t *testing.T, err error) {
if err != nil {
abortOnError(t, err)
}
}
// Certain tests fetch global config from a remote server and store it at
// `global.yaml`. Other tests rely on `global.yaml` matching the
// `fetched-global.yaml` fixture. For tests that fetch config remotely, we must
// delete the config file once the test has completed to avoid causing other
// these other tests to fail in the event that the remote config differs from
// the fixture.
func deleteGlobalConfig() {
os.Remove("global.yaml")
}
const globalYamlTemplate = `
version: 0
cloudconfigca: ""
autoupdateca: ""
updateserverurl: ""
bordareportinterval: 0s
bordasamplepercentage: 0
pingsamplepercentage: 0
reportissueemail: ""
client:
dumpheaders: false
fronted:
providers:
cloudfront:
hostaliases:
xyz.getiantem.org: xyz.cloudfront.net
masquerades: &id001
- domain: cloudfront.net
ipaddress: 54.182.1.120
testurl: http://zyx.cloudfront.net/ping
validator:
rejectstatus:
- 403
masqueradesets:
cloudflare: []
cloudfront: *id001
adsettings: null
proxiedsites:
delta:
additions: []
deletions: []
cloud:
- 0000a-fast-proxy.de
trustedcas:
- commonname: VeriSign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/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/\nNIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E\nBAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH\nBgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy\naXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv\nMzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE\np6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y\n5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK\nWE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ\n4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N\nhnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq\n-----END
CERTIFICATE-----\n"
globalconfigpollinterval: 3s
proxyconfigpollinterval: 1s
`
const proxiesYamlTemplate = `
fallback-104.236.192.114:
addr: 104.236.192.114:443
cert: "-----BEGIN CERTIFICATE-----\nMIIDoDCCAoigAwIBAgIEJSGW+TANBgkqhkiG9w0BAQsFADB4MQswCQYDVQQGEwJVUzETMBEGA1UE\nCBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHQWJ1c2VyczEhMB8GA1UEChMYUnVtcGVsc3RpbHRza2lu\nIEJsZW5kaW5nMR8wHQYDVQQDExZOYWlyIENvaW5hZ2VzIFJvb3RsZXNzMB4XDTE1MTIxMDE5MjI0\nNloXDTE2MTIwOTE5MjI0NloweDELM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