-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmimic_test.go
202 lines (169 loc) · 5.03 KB
/
mimic_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
package proxy
import (
"bytes"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"regexp"
"testing"
"time"
"github.com/getlantern/proxy/v3/filters"
"github.com/stretchr/testify/assert"
"github.com/getlantern/http-proxy-lantern/v2/server"
"github.com/getlantern/http-proxy-lantern/v2/instrument"
"github.com/getlantern/http-proxy-lantern/v2/mimic"
"github.com/getlantern/http-proxy-lantern/v2/tokenfilter"
)
const target = "test/data/apache-2.4.7-ubuntu14.04.raw"
const apTemplate = "test/data/apache-2.4.7-ubuntu14.04.tpl"
const current = "test/data/http-proxy.raw"
type entry struct {
method string
path string
}
/*
For rationale behind these tests, refer following:
[1] https://github.com/getlantern/lantern/issues/1255#issuecomment-30658217
[2] https://github.com/getlantern/lantern-java/blob/master/src/main/java/org/lantern/proxy/GiveModeHttpFilters.java#L167
[3] https://github.com/apache/httpd/blob/2.4.17/server/core.c#L4379
*/
var candidates = []entry{
// 200 with default page
{"GET", "/"},
{"GET", "/index.html"},
{"GET", "/icons/ubuntu-logo.png"},
// 404
{"GET", "//cgi-bin/php"},
{"GET", "//cgi-bin/php5"},
{"GET", "//cgi-bin/php-cgi"},
{"GET", "//cgi-bin/php.cgi"},
{"GET", "//cgi-bin/php4"},
// multiple slashes
{"GET", "///cgi-bin/php4"},
{"GET", "//cgi-bin//php4"},
{"GET", "/not-existed"},
{"GET", "/end-with-slash/"},
{"HEAD", "/"},
{"HEAD", "/index.html"},
{"HEAD", "/icons/ubuntu-logo.png"},
{"HEAD", "/not-existed"},
{"POST", "/"},
{"POST", "/index.html"},
{"POST", "/icons/ubuntu-logo.png"},
{"POST", "/not-existed"},
{"OPTIONS", "/"},
{"OPTIONS", "/index.html"},
{"OPTIONS", "/icons/ubuntu-logo.png"},
{"OPTIONS", "/not-existed"},
{"PUT", "/"},
{"PUT", "/index.html"},
{"PUT", "/icons/ubuntu-logo.png"},
{"PUT", "/not-existed"},
{"CONNECT", "/"},
{"CONNECT", "/index.html"},
{"CONNECT", "/icons/ubuntu-logo.png"},
{"CONNECT", "/not-existed"},
{"INVALID", "/"},
{"INVALID", "/index.html"},
{"INVALID", "/not-existed"},
}
type entryWithHeaders struct {
method string
path string
withHostHeader bool
headers []string
}
func TestMimicApache(t *testing.T) {
tf := tokenfilter.New("arbitrary-token", instrument.NoInstrument{})
s := server.New(&server.Opts{
IdleTimeout: 30 * time.Second,
Filter: filters.Join(tf),
})
chListenOn := make(chan string)
go func() {
getListenAddr := func(addr string) {
chListenOn <- addr
}
err := s.ListenAndServeHTTP(":0", getListenAddr)
assert.NoError(t, err, "should start chained server")
}()
strAddr := <-chListenOn
host, port, err := net.SplitHostPort(strAddr)
if err != nil {
panic("should not happen")
}
mimic.Host = host
mimic.Port = port
buf := request(t, strAddr)
ioutil.WriteFile(current, buf.Bytes(), os.ModePerm)
compare(t, normalize(buf), apTemplate)
}
func TestRealApache(t *testing.T) {
t.Skip("comment out this line and run 'go test -run RealApache' when you want to record http traffic against real apache server")
addr := "128.199.100.121:80"
buf := request(t, addr)
ioutil.WriteFile(target, buf.Bytes(), os.ModePerm)
ioutil.WriteFile(apTemplate, normalize(buf).Bytes(), os.ModePerm)
}
func compare(t *testing.T, buf *bytes.Buffer, file string) {
c := exec.Command("diff", "-a", "-", file)
in, err := c.StdinPipe()
if assert.NoError(t, err, "should gain stdin pipe") {
go func() {
buf.WriteTo(in)
in.Close()
}()
b, err := c.CombinedOutput()
assert.NoError(t, err, "should not different from an real apache")
t.Log(string(b))
}
}
func request(t *testing.T, addr string) *bytes.Buffer {
var buf bytes.Buffer
for _, c := range candidates {
requestItem(t, &buf, addr, c.method, c.path, []string{"Host: " + addr})
}
return &buf
}
func requestItem(t *testing.T, buf *bytes.Buffer, addr, method, path string, headers []string) {
conn, err := net.Dial("tcp", addr)
if assert.NoError(t, err, "should connect") {
defer conn.Close()
req := method + " " + path + " HTTP/1.1\r\n"
buf.WriteString(req)
buf.WriteString("--------------------\n")
for _, h := range headers {
req = req + h + "\r\n"
}
req = req + "\r\n"
log.Trace(req)
_, err := conn.Write([]byte(req))
if assert.NoError(t, err, "should write") {
_, err := io.Copy(buf, conn)
assert.NoError(t, err, "should copy")
buf.WriteString("====================\n\n")
}
}
}
func normalize(in *bytes.Buffer) *bytes.Buffer {
s := in.String()
exps := []struct {
regexp *regexp.Regexp
with string
}{
// Date: ... GMT
{regexp.MustCompile(`[A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} GMT`), "<GMT>"},
// ETag: "xxxx-xxxxxxxxxxxxx"
{regexp.MustCompile(`ETag: "\w{3,4}-\w{12,13}"`), `ETag: "<ETAG>"`},
// Apache/2.4.7 (Ubuntu) Server at 128.199.100.121 Port 80
{regexp.MustCompile(`Apache\/\d\.\d\.\d+ \(Ubuntu\) Server at \S+ Port \d{2,}`), "Apache/<VERSION> (Ubuntu) Server at <Host> Port <Port>"},
// Content-Length: 1243
{regexp.MustCompile(`Content-Length: \d+`), "Content-Length: <...>"},
}
for _, e := range exps {
s = e.regexp.ReplaceAllString(s, e.with)
}
return bytes.NewBufferString(s)
}