-
Notifications
You must be signed in to change notification settings - Fork 2
/
pacparser_test.go
281 lines (216 loc) · 5.78 KB
/
pacparser_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
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
package pacparser
// go-pacparser - golang bindings for pacparser library
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/suite"
)
// everything we need to run our tests
type pacparserTestSuite struct {
suite.Suite
basePath string
pacFiles map[string]string
}
// get test pacfiles
func (s *pacparserTestSuite) readFiles(fpath string) map[string]string {
fullpath := path.Join(s.basePath, fpath) // path to test files
fileMap := make(map[string]string) // return map of file names and content
// read all files and check error
files, err := ioutil.ReadDir(fullpath)
if err != nil {
panic(err.Error())
}
// loop over read files
for _, file := range files {
contents, err := ioutil.ReadFile(path.Join(fullpath, file.Name()))
if err != nil {
panic(err.Error())
}
fileMap[file.Name()] = string(contents)
}
// return file list
return fileMap
}
// initiate test suite
func (s *pacparserTestSuite) SetupSuite() {
var err error // error holder
// set base path and check error
if s.basePath, err = os.Getwd(); err != nil {
panic(err.Error())
}
// read files
s.pacFiles = s.readFiles("test")
}
// run tests
func TestSuite(t *testing.T) {
ts := new(pacparserTestSuite)
suite.Run(t, ts)
}
// bad pacfile test
func (s *pacparserTestSuite) TestBad() {
// load pac body
pp := New(s.pacFiles["bad1.pac"])
// assert on parse
s.False(pp.Parse())
// execute FindProxyForURL and log
ok, pxy := pp.FindProxy("http://www.google.com/")
s.T().Logf("[bad1.pac] ok: %t, pxy: %s", ok, pxy)
// assert returns
s.False(ok)
s.Empty(pxy)
// pull error and log
lastError := pp.LastError()
s.T().Logf("[bad1.pac] lastError: %s", lastError)
// assert on error
s.NotNil(lastError)
// load pac body
pp = New(s.pacFiles["bad2.pac"])
// assert on parse
s.True(pp.Parse())
// execute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://www.google.com/")
s.T().Logf("[bad2.pac] ok: %t, pxy: %s", ok, pxy)
// assert returns
s.False(ok)
s.Equal("undefined", pxy)
// pull error and log
lastError = pp.LastError()
s.T().Logf("[bad2.pac] lastError: %s", lastError)
// assert on error
s.NotNil(lastError)
// load pac body
pp = New(s.pacFiles["bad3.pac"])
// assert on parse
s.True(pp.Parse())
// execute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://www.google.com/")
s.T().Logf("[bad3.pac] ok: %t, pxy: %s", ok, pxy)
// assert returns
s.False(ok)
s.Equal("undefined", pxy)
// pull error and log
lastError = pp.LastError()
s.T().Logf("[bad3.pac] lastError: %s", lastError)
// assert on error
s.NotNil(lastError)
}
// good pacfile
func (s *pacparserTestSuite) TestGood() {
var ok bool // status return
var pxy string // proxy line
// init good instance
pp := New(s.pacFiles["good1.pac"])
// assert on parse
s.True(pp.Parse())
// set client ip
pp.SetMyIp("10.10.5.6")
// assert setting
s.Equal("10.10.5.6", pp.MyIp())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://www.google.com/")
s.T().Logf("[good1.pac] url: http://www.google.com/ ip: %s pxy: %s", pp.MyIp(), pxy)
// assert returns
s.True(ok)
s.Equal("PROXY 1.2.3.4:8080", pxy)
s.Nil(pp.LastError())
// reset the instance
pp.Reset()
// ensure IP was reset
s.Equal(myIpDefault, pp.MyIp())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://www.google.com/")
s.T().Logf("[good1.pac] url: http://www.google.com/ ip: %s pxy: %s", pp.MyIp(), pxy)
// assert returns
s.True(ok)
s.Equal("PROXY 4.5.6.7:8080; PROXY 7.8.9.10:8080", pxy)
s.Nil(pp.LastError())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://test.local/")
s.T().Logf("[good1.pac] url: http://test.local/ pxy: %s", pxy)
// assert returns
s.True(ok)
s.Equal("DIRECT", pxy)
s.Nil(pp.LastError())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://localhost")
s.T().Logf("[good1.pac] url: http://localhost/ pxy: %s", pxy)
// assert returns
s.True(ok)
s.Equal("DIRECT", pxy)
s.Nil(pp.LastError())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("http://www.abcdomain.com/")
s.T().Logf("[good1.pac] url: http://www.abcdomain.com/ pxy: %s", pxy)
// assert returns
s.True(ok)
s.Equal("DIRECT", pxy)
s.Nil(pp.LastError())
// exectute FindProxyForURL and log
ok, pxy = pp.FindProxy("https://192.168.1.1.xip.io:4433/path")
s.T().Logf("[good1.pac] url: https://192.168.1.1.xip.io:4433/path pxy: %s", pxy)
// assert returns
s.True(ok)
s.Equal("DIRECT", pxy)
s.Nil(pp.LastError())
}
// test with IsValid
func (s *pacparserTestSuite) TestValid() {
// load pacfile
pp := New(s.pacFiles["bad1.pac"])
// check validity and log
ok := pp.IsValid()
s.T().Logf("[bad1.pac] IsValid: %t", ok)
// assert result
s.False(ok)
// load pacfile
pp = New(s.pacFiles["bad2.pac"])
// check validity and log
ok = pp.IsValid()
s.T().Logf("[bad2.pac] IsValid: %t", ok)
// assert result
s.False(ok)
// load pacfile
pp = New(s.pacFiles["bad3.pac"])
// check validity and log
ok = pp.IsValid()
s.T().Logf("[bad3.pac] IsValid: %t", ok)
// assert result
s.False(ok)
// load pacfile
pp = New(s.pacFiles["good1.pac"])
// check validity and log
ok = pp.IsValid()
s.T().Logf("[good1.pac] IsValid: %t", ok)
// assert result
s.True(ok)
}
// benchmark parse
func BenchmarkParse(b *testing.B) {
ts := new(pacparserTestSuite)
ts.SetupSuite()
pp := New(ts.pacFiles["good1.pac"])
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok := pp.Parse()
err := pp.LastError()
if !ok || err != nil {
panic(err.Error())
}
}
}
// benchmark find
func BenchmarkFind(b *testing.B) {
ts := new(pacparserTestSuite)
ts.SetupSuite()
pp := New(ts.pacFiles["good1.pac"])
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok, pxy := pp.FindProxy("http://www.google.com/")
err := pp.LastError()
if !ok || pxy == "" || err != nil {
panic(err.Error())
}
}
}