forked from syncthing/syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
268 lines (227 loc) · 5.82 KB
/
http_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
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
// +build integration
package integration
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/rc"
)
func TestHTTPGetIndex(t *testing.T) {
p := startInstance(t, 2)
defer checkedStop(t, p)
// Check for explicit index.html
res, err := http.Get("http://localhost:8082/index.html")
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Errorf("Status %d != 200", res.StatusCode)
}
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if len(bs) < 1024 {
t.Errorf("Length %d < 1024", len(bs))
}
if !bytes.Contains(bs, []byte("</html>")) {
t.Error("Incorrect response")
}
if res.Header.Get("Set-Cookie") == "" {
t.Error("No set-cookie header")
}
res.Body.Close()
// Check for implicit index.html
res, err = http.Get("http://localhost:8082/")
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Errorf("Status %d != 200", res.StatusCode)
}
bs, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if len(bs) < 1024 {
t.Errorf("Length %d < 1024", len(bs))
}
if !bytes.Contains(bs, []byte("</html>")) {
t.Error("Incorrect response")
}
if res.Header.Get("Set-Cookie") == "" {
t.Error("No set-cookie header")
}
res.Body.Close()
}
func TestHTTPGetIndexAuth(t *testing.T) {
p := startInstance(t, 1)
defer checkedStop(t, p)
// Without auth should give 401
res, err := http.Get("http://127.0.0.1:8081/")
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 401 {
t.Errorf("Status %d != 401", res.StatusCode)
}
// With wrong username/password should give 401
req, err := http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("testuser", "wrongpass")
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 401 {
t.Fatalf("Status %d != 401", res.StatusCode)
}
// With correct username/password should succeed
req, err = http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("testuser", "testpass")
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("Status %d != 200", res.StatusCode)
}
}
func TestHTTPOptions(t *testing.T) {
p := startInstance(t, 2)
defer checkedStop(t, p)
req, err := http.NewRequest("OPTIONS", "http://127.0.0.1:8082/rest/system/error/clear", nil)
if err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 204 {
t.Fatalf("Status %d != 204 for OPTIONS", res.StatusCode)
}
}
func TestHTTPPOSTWithoutCSRF(t *testing.T) {
p := startInstance(t, 2)
defer checkedStop(t, p)
// Should fail without CSRF
req, err := http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
if err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 403 {
t.Fatalf("Status %d != 403 for POST", res.StatusCode)
}
// Get CSRF
req, err = http.NewRequest("GET", "http://127.0.0.1:8082/", nil)
if err != nil {
t.Fatal(err)
}
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
hdr := res.Header.Get("Set-Cookie")
id := res.Header.Get("X-Syncthing-ID")[:5]
if !strings.Contains(hdr, "CSRF-Token") {
t.Error("Missing CSRF-Token in", hdr)
}
// Should succeed with CSRF
req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):])
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("Status %d != 200 for POST", res.StatusCode)
}
// Should fail with incorrect CSRF
req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):]+"X")
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 403 {
t.Fatalf("Status %d != 403 for POST", res.StatusCode)
}
}
func setupAPIBench() *rc.Process {
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
if err != nil {
panic(err)
}
err = generateFiles("s1", 25000, 20, "../LICENSE")
if err != nil {
panic(err)
}
err = ioutil.WriteFile("s1/knownfile", []byte("somedatahere"), 0644)
if err != nil {
panic(err)
}
// This will panic if there is an actual failure to start, when we try to
// call nil.Fatal(...)
return startInstance(nil, 1)
}
func benchmarkURL(b *testing.B, url string) {
p := setupAPIBench()
defer p.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Get(url)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkAPI_db_completion(b *testing.B) {
benchmarkURL(b, "/rest/db/completion?folder=default&device="+protocol.LocalDeviceID.String())
}
func BenchmarkAPI_db_file(b *testing.B) {
benchmarkURL(b, "/rest/db/file?folder=default&file=knownfile")
}
func BenchmarkAPI_db_ignores(b *testing.B) {
benchmarkURL(b, "/rest/db/ignores?folder=default")
}
func BenchmarkAPI_db_need(b *testing.B) {
benchmarkURL(b, "/rest/db/need?folder=default")
}
func BenchmarkAPI_db_status(b *testing.B) {
benchmarkURL(b, "/rest/db/status?folder=default")
}
func BenchmarkAPI_db_browse_dirsonly(b *testing.B) {
benchmarkURL(b, "/rest/db/browse?folder=default&dirsonly=true")
}