forked from mikehelmick/go-chaff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_test.go
112 lines (94 loc) · 3.01 KB
/
tracker_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
// Copyright 2020 Mike Helmick
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chaff
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func checkLength(t *testing.T, expected int, length int) {
t.Helper()
lower := float64(expected) * 0.99
upper := float64(expected) * 1.01
if l := float64(length); l < lower || l > upper {
t.Errorf("genrated data not within 1%% of %v, %v - %v, got %v", expected, lower, upper, l)
}
}
func TestChaff(t *testing.T) {
track := New()
defer track.Close()
// Seed the tracker with a single request.
track.recordRequest(&request{25, 250, 100})
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/", strings.NewReader(""))
if err != nil {
t.Fatalf("http.NewRequest: %v", err)
}
before := time.Now()
track.ServeHTTP(w, r)
after := time.Now()
if d := after.Sub(before); d < 25*time.Millisecond {
t.Errorf("not enough time passed, want >= 25ms, got: %v", d)
}
if w.Code != http.StatusOK {
t.Errorf("wrong code, want: %v, got: %v", http.StatusOK, w.Code)
}
if header := w.Header().Get(Header); header == "" {
t.Errorf("expected header '%v' missing", Header)
} else {
checkLength(t, 100, len(header))
}
checkLength(t, 250, len(w.Body.Bytes()))
}
func TestTracking(t *testing.T) {
track := New()
defer track.Close()
{
want := &request{}
got := track.CalculateProfile()
if diff := cmp.Diff(want, got, cmp.AllowUnexported(request{})); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
}
for i := 0; i <= DefaultCapacity*2; i++ {
wrapped := track.Track(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Millisecond)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("padding", strings.Repeat("a", i+1))
fmt.Fprintf(w, "%s", strings.Repeat("b", i+1))
}))
recorder := httptest.NewRecorder()
request, err := http.NewRequest("GET", "/", strings.NewReader(""))
if err != nil {
t.Fatalf("http.NewRequest: %v", err)
}
wrapped.ServeHTTP(recorder, request)
if recorder.Code != http.StatusAccepted {
t.Fatalf("wrong error code: want: %v, got: %v", http.StatusAccepted, recorder.Code)
}
}
got := track.CalculateProfile()
// requests are fast enough that 1ms is reasonable.
// sum(101:200)/100 -> 150
// for header there is an extra 7 bytes for header name
want := &request{1, 150, 157}
if diff := cmp.Diff(want, got, cmp.AllowUnexported(request{})); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
}