forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
413 lines (369 loc) · 12.2 KB
/
middleware_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//lint:file-ignore U1001 Ignore all unused code, staticcheck doesn't understand testify/suite
package horizon
import (
"context"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/go-chi/chi"
"github.com/stellar/throttled"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/stellar/go/services/horizon/internal/actions"
horizonContext "github.com/stellar/go/services/horizon/internal/context"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/services/horizon/internal/ingest"
"github.com/stellar/go/services/horizon/internal/ledger"
hProblem "github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/services/horizon/internal/test"
tdb "github.com/stellar/go/services/horizon/internal/test/db"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"
)
func requestHelperRemoteAddr(ip string) func(r *http.Request) {
return func(r *http.Request) {
r.RemoteAddr = ip
}
}
func requestHelperXFF(xff string) func(r *http.Request) {
return func(r *http.Request) {
r.Header.Set("X-Forwarded-For", xff)
}
}
type RateLimitMiddlewareTestSuite struct {
suite.Suite
ht *HTTPT
c Config
app *App
rh test.RequestHelper
}
func (suite *RateLimitMiddlewareTestSuite) SetupSuite() {
suite.ht = StartHTTPTest(suite.T(), "base")
}
func (suite *RateLimitMiddlewareTestSuite) SetupTest() {
suite.c = NewTestConfig(tdb.HorizonURL())
suite.c.RateQuota = &throttled.RateQuota{
MaxRate: throttled.PerHour(10),
MaxBurst: 9,
}
app, err := NewApp(suite.c)
if err != nil {
log.Fatal("cannot initialize app", err)
}
suite.app = app
suite.rh = NewRequestHelper(suite.app)
}
func (suite *RateLimitMiddlewareTestSuite) TearDownSuite() {
suite.ht.Finish()
}
func (suite *RateLimitMiddlewareTestSuite) TearDownTest() {
suite.app.Close()
}
// Sets X-RateLimit-Limit headers correctly.
func (suite *RateLimitMiddlewareTestSuite) TestRateLimit_LimitHeaders() {
w := suite.rh.Get("/")
assert.Equal(suite.T(), 200, w.Code)
assert.Equal(suite.T(), "10", w.Header().Get("X-RateLimit-Limit"))
}
// Sets X-RateLimit-Remaining headers correctly.
func (suite *RateLimitMiddlewareTestSuite) TestRateLimit_RemainingHeaders() {
// test that SSE requests are ignored
for i := 0; i < 10; i++ {
w := suite.rh.Get("/", test.RequestHelperStreaming)
assert.Equal(suite.T(), "", w.Header().Get("X-RateLimit-Remaining"))
assert.NotEqual(suite.T(), http.StatusTooManyRequests, w.Code)
}
for i := 0; i < 10; i++ {
w := suite.rh.Get("/")
expected := 10 - (i + 1)
assert.Equal(suite.T(), strconv.Itoa(expected), w.Header().Get("X-RateLimit-Remaining"))
assert.NotEqual(suite.T(), http.StatusTooManyRequests, w.Code)
}
// confirm remaining stays at 0
for i := 0; i < 10; i++ {
w := suite.rh.Get("/")
assert.Equal(suite.T(), "0", w.Header().Get("X-RateLimit-Remaining"))
assert.Equal(suite.T(), http.StatusTooManyRequests, w.Code)
}
}
// Sets X-RateLimit-Reset header correctly. Should reset after 360 seconds since it's limited to 10 requests/hour.
func (suite *RateLimitMiddlewareTestSuite) TestRateLimit_ResetHeaders() {
w := suite.rh.Get("/")
assert.Equal(suite.T(), "360", w.Header().Get("X-RateLimit-Reset"))
}
// Restricts based on RemoteAddr IP after too many requests.
func (suite *RateLimitMiddlewareTestSuite) TestRateLimit_RemoteAddr() {
for i := 0; i < 10; i++ {
w := suite.rh.Get("/")
assert.Equal(suite.T(), 200, w.Code)
}
w := suite.rh.Get("/")
assert.Equal(suite.T(), 429, w.Code)
w = suite.rh.Get("/", requestHelperRemoteAddr("127.0.0.2"))
assert.Equal(suite.T(), 200, w.Code)
// Ignores ports
w = suite.rh.Get("/", requestHelperRemoteAddr("127.0.0.1:4312"))
assert.Equal(suite.T(), 429, w.Code)
}
// Restrict based upon X-Forwarded-For correctly.
func (suite *RateLimitMiddlewareTestSuite) TestRateLimit_XForwardedFor() {
for i := 0; i < 10; i++ {
w := suite.rh.Get("/", requestHelperXFF("4.4.4.4"))
assert.Equal(suite.T(), 200, w.Code)
}
w := suite.rh.Get("/", requestHelperXFF("4.4.4.4"))
assert.Equal(suite.T(), 429, w.Code)
// allow other ips
w = suite.rh.Get("/", requestHelperRemoteAddr("4.4.4.3"))
assert.Equal(suite.T(), 200, w.Code)
// Ignores trailing ips
w = suite.rh.Get("/", requestHelperXFF("4.4.4.4, 4.4.4.5, 127.0.0.1"))
assert.Equal(suite.T(), 429, w.Code)
}
func TestRateLimitMiddlewareTestSuite(t *testing.T) {
suite.Run(t, new(RateLimitMiddlewareTestSuite))
}
func TestStateMiddleware(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)
q := &history.Q{tt.HorizonSession()}
request, err := http.NewRequest("GET", "http://localhost/", nil)
tt.Assert.NoError(err)
expectTransaction := true
endpoint := func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(&horizonContext.SessionContextKey).(db.SessionInterface)
if (session.GetTx() == nil) == expectTransaction {
t.Fatalf("expected transaction to be in session: %v", expectTransaction)
}
w.WriteHeader(http.StatusOK)
}
stateMiddleware := &httpx.StateMiddleware{
HorizonSession: tt.HorizonSession(),
}
handler := chi.NewRouter()
handler.With(stateMiddleware.Wrap).MethodFunc("GET", "/", endpoint)
for i, testCase := range []struct {
name string
noStateVerification bool
stateInvalid bool
latestHistoryLedger xdr.Uint32
lastIngestedLedger uint32
ingestionVersion int
sseRequest bool
expectedStatus int
expectTransaction bool
}{
{
name: "responds with 500 if q.GetExpStateInvalid returns true",
stateInvalid: true,
latestHistoryLedger: 2,
lastIngestedLedger: 2,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: http.StatusInternalServerError,
expectTransaction: false,
},
{
name: "responds with still ingesting if lastIngestedLedger <= 0",
stateInvalid: false,
latestHistoryLedger: 1,
lastIngestedLedger: 0,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: hProblem.StillIngesting.Status,
expectTransaction: false,
},
{
name: "responds with still ingesting if lastIngestedLedger < latestHistoryLedger",
stateInvalid: false,
latestHistoryLedger: 3,
lastIngestedLedger: 2,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: hProblem.StillIngesting.Status,
expectTransaction: false,
},
{
name: "responds with still ingesting if lastIngestedLedger > latestHistoryLedger",
stateInvalid: false,
latestHistoryLedger: 4,
lastIngestedLedger: 5,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: hProblem.StillIngesting.Status,
expectTransaction: false,
},
{
name: "responds with still ingesting if version != ingest.CurrentVersion",
stateInvalid: false,
latestHistoryLedger: 5,
lastIngestedLedger: 5,
ingestionVersion: ingest.CurrentVersion - 1,
sseRequest: false,
expectedStatus: hProblem.StillIngesting.Status,
expectTransaction: false,
},
{
name: "succeeds",
stateInvalid: false,
latestHistoryLedger: 6,
lastIngestedLedger: 6,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: http.StatusOK,
expectTransaction: true,
},
{
name: "succeeds with SSE request",
stateInvalid: false,
latestHistoryLedger: 7,
lastIngestedLedger: 7,
ingestionVersion: ingest.CurrentVersion,
sseRequest: true,
expectedStatus: http.StatusOK,
expectTransaction: false,
},
{
name: "succeeds without state verification",
noStateVerification: true,
stateInvalid: false,
latestHistoryLedger: 8,
lastIngestedLedger: 8,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: http.StatusOK,
expectTransaction: true,
},
{
name: "succeeds without state verification and invalid state",
noStateVerification: true,
stateInvalid: true,
latestHistoryLedger: 9,
lastIngestedLedger: 9,
ingestionVersion: ingest.CurrentVersion,
sseRequest: false,
expectedStatus: http.StatusOK,
expectTransaction: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
stateMiddleware.NoStateVerification = testCase.noStateVerification
tt.Assert.NoError(q.UpdateExpStateInvalid(context.Background(), testCase.stateInvalid))
tt.Assert.NoError(q.Begin(tt.Ctx))
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Hash: xdr.Hash{byte(i)},
Header: xdr.LedgerHeader{
LedgerSeq: testCase.latestHistoryLedger,
PreviousLedgerHash: xdr.Hash{byte(i)},
},
}, 0, 0, 0, 0, 0)
tt.Assert.NoError(err)
tt.Assert.NoError(ledgerBatch.Exec(tt.Ctx, q))
tt.Assert.NoError(q.Commit())
tt.Assert.NoError(q.UpdateLastLedgerIngest(context.Background(), testCase.lastIngestedLedger))
tt.Assert.NoError(q.UpdateIngestVersion(context.Background(), testCase.ingestionVersion))
if testCase.sseRequest {
request.Header.Set("Accept", "text/event-stream")
} else {
request.Header.Del("Accept")
}
w := httptest.NewRecorder()
expectTransaction = testCase.expectTransaction
handler.ServeHTTP(w, request)
tt.Assert.Equal(testCase.expectedStatus, w.Code)
if testCase.expectedStatus == http.StatusOK && !testCase.sseRequest {
tt.Assert.Equal(
w.Header().Get(actions.LastLedgerHeaderName),
strconv.FormatInt(int64(testCase.lastIngestedLedger), 10))
} else {
tt.Assert.Equal(w.Header().Get(actions.LastLedgerHeaderName), "")
}
})
}
}
func TestClientDisconnect(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)
request, err := http.NewRequest("GET", "http://localhost/", nil)
tt.Assert.NoError(err)
endpoint := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
stateMiddleware := &httpx.StateMiddleware{
HorizonSession: tt.HorizonSession(),
NoStateVerification: true,
}
handler := chi.NewRouter()
handler.With(stateMiddleware.Wrap).MethodFunc("GET", "/", endpoint)
w := httptest.NewRecorder()
ctx, cancel := context.WithCancel(request.Context())
defer cancel()
request = request.WithContext(ctx)
// cancel invocation simulates client disconnect in the context
cancel()
handler.ServeHTTP(w, request)
tt.Assert.Equal(499, w.Code)
}
func TestCheckHistoryStaleMiddleware(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
request, err := http.NewRequest("GET", "http://localhost/", nil)
tt.Assert.NoError(err)
endpoint := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
for _, testCase := range []struct {
name string
coreLatest int32
historyLatest int32
expectedStatus int
staleThreshold int32
}{
{
name: "responds with a service unavailable if history is stale",
coreLatest: 4,
historyLatest: 2,
expectedStatus: http.StatusServiceUnavailable,
staleThreshold: 1,
},
{
name: "succeeds",
coreLatest: 6,
historyLatest: 6,
expectedStatus: http.StatusOK,
staleThreshold: 1,
},
{
name: "succeeds with threshold 0",
coreLatest: 6,
historyLatest: 5,
expectedStatus: http.StatusOK,
staleThreshold: 0,
},
} {
t.Run(testCase.name, func(t *testing.T) {
state := ledger.Status{
CoreStatus: ledger.CoreStatus{
CoreLatest: testCase.coreLatest,
},
HorizonStatus: ledger.HorizonStatus{
HistoryLatest: testCase.historyLatest,
},
}
ledgerState := &ledger.State{}
ledgerState.SetStatus(state)
historyMiddleware := httpx.NewHistoryMiddleware(ledgerState, testCase.staleThreshold, tt.HorizonSession(), 0)
handler := chi.NewRouter()
handler.With(historyMiddleware).MethodFunc("GET", "/", endpoint)
w := httptest.NewRecorder()
handler.ServeHTTP(w, request)
tt.Assert.Equal(testCase.expectedStatus, w.Code)
})
}
}