forked from magoo/AuthTables
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_test.go
210 lines (171 loc) · 4.5 KB
/
build_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/willf/bloom"
)
var badRec = Record{
UID: "test*UID",
Mid: "test*MID",
IP: "1.1.*.1",
}
var testRec = Record{
UID: "testUID",
Mid: "testMID",
IP: "1.1.1.1",
}
var filterTest = bloom.NewWithEstimates(c.BloomSize, 1e-3) // Configurable via environment var.
func TestRedisConnectivity(t *testing.T) {
_, err := client.Ping().Result()
if err != nil {
t.Errorf("We can't ping redis.")
}
}
func TestLoad(t *testing.T) {
writeRecord([]byte("asdf"))
}
func TestWWWServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Testing the client")
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
err = res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", greeting)
}
func TestBloom(t *testing.T) {
if filterTest.Test([]byte("shouldnotexist")) {
log.Fatal("Bloom filter detected a string that wasn't in filter")
}
filterTest.Add([]byte("exists"))
if !filterTest.Test([]byte("exists")) {
log.Fatal("Bloom filter could not detect a string that was in filter")
}
}
func TestBadRequest(t *testing.T) {
req, err := http.NewRequest("POST", "/check", bytes.NewBuffer(badRec.Marshaler()))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(checkRequest)
handler.ServeHTTP(rr, req)
// Correct Response?
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `BAD`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestCheckRequest(t *testing.T) {
req, err := http.NewRequest("POST", "/check", bytes.NewBuffer(testRec.Marshaler()))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(checkRequest)
handler.ServeHTTP(rr, req)
// Correct Response?
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `OK`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestResetRequest(t *testing.T) {
req, err := http.NewRequest("POST", "/reset", bytes.NewBuffer(testRec.Marshaler()))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(resetRequest)
handler.ServeHTTP(rr, req)
// Correct Response?
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `RESET`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestAddRequest(t *testing.T) {
jsonStr, err := json.Marshal(testRec)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/add", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(addRequest)
handler.ServeHTTP(rr, req)
// Correct Response?
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `ADD`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
// Check a key was written
if !(canGetKey("testUID:1.1.1.1") && canGetKey("testUID:testMID")) {
t.Errorf("keys not being written")
}
}
func BenchmarkBloomAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
// We reset the timer because the bloom filter is only created on boot.
//b.ResetTimer()
filterTest.Add([]byte("exists"))
}
}
func BenchmarkBloomTest(b *testing.B) {
for i := 0; i < b.N; i++ {
// We reset the timer because the bloom filter is only created on boot.
filter.Test([]byte("shouldnotexist"))
}
}
func BenchmarkWriteRecord(b *testing.B) {
for i := 0; i < b.N; i++ {
add(testRec)
}
}
func BenchmarkReadRecord(b *testing.B) {
for i := 0; i < b.N; i++ {
check(testRec)
}
}