-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_test.go
122 lines (104 loc) · 2.88 KB
/
filter_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
package pwned
import (
"bytes"
"context"
"encoding/hex"
"testing"
)
var apple []byte
var orange []byte
var pear []byte
var junkHex []byte
func TestMain(m *testing.M) {
apple = mustDecode("d0be2dc421be4fcd0172e5afceea3970e2f3d940")
orange = mustDecode("ef0ebbb77298e1fbd81f756a4efc35b977c93dae")
pear = mustDecode("3e2bf5faa2c3fec1f84068a073b7e51d7ad44a35")
junkHex = mustDecode("6ab688299c2e4e670cef1f94055dd7fdfbce1b32") // P0be2dc421be4fcd0172e5afceea3970e2f3d940
m.Run()
}
func mustDecode(s string) []byte {
bytes, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return bytes
}
func TestFilter(t *testing.T) {
f := NewFilter(4, 4)
if found := f.TestHash(apple); found {
t.Error("found apple, expected no apple")
}
if found := f.AddHash(apple); found {
t.Error("found apple when adding hash, expected no apple")
}
if found := f.TestHash(apple); !found {
t.Error("found no apple, expected apple")
}
if found := f.TestHash(orange); found {
t.Error("found orange, expected no orange")
}
if found := f.AddHash(orange); found {
t.Error("found orange when adding hash, expected no orange")
}
if found := f.TestHash(orange); !found {
t.Error("found no orange, expected orange")
}
if count, expected := f.Count(), int64(2); count != expected {
t.Errorf("got count of %d, expected %d", count, expected)
}
}
// func TestFilterJSON(t *testing.T) {
// f := NewFilter(4, 2)
// f.AddHash(apple)
// f.AddHash(orange)
// bytes, err := json.Marshal(&f)
// if err != nil {
// t.Errorf("unexpected error marshalling filter: %s", err)
// }
// f = NewFilter(8, 1)
// err = json.Unmarshal(bytes, f)
// if err != nil {
// t.Errorf("unexpected error unmarshalling filter: %s", err)
// }
// if count := f.Count(); count != 2 {
// t.Errorf("got count %d, expected 2", count)
// }
// if lookups := f.lookups; f.lookups != 1 {
// t.Errorf("got lookups %d, expected 1", lookups)
// }
// if found := f.TestHash(apple); !found {
// t.Errorf("found no apple, expected apple")
// }
// if found := f.TestHash(orange); !found {
// t.Errorf("found no orange, expected orange")
// }
// if found := f.TestHash(pear); found {
// t.Errorf("found pear, expected no pear")
// }
// }
func TestPB(t *testing.T) {
f := NewFilter(4, 2)
f.AddHash(apple)
f.AddHash(orange)
buf := &bytes.Buffer{}
_, err := f.writeBytes(buf)
if err != nil {
t.Fatalf("got error from writeBytes: %s", err)
}
f2, err := filterFrom(context.Background(), f.getPB(), buf)
if err != nil {
t.Fatalf("got error from filterFrom: %s", err)
}
if found := f.TestHash(apple); !found {
t.Errorf("found no apple, expected apple")
}
if found := f.TestHash(orange); !found {
t.Errorf("found no orange, expected orange")
}
if found := f.TestHash(pear); found {
t.Errorf("found pear, expected no pear")
}
if c1, c2 := f.Count(), f2.Count(); c1 != c2 {
t.Errorf("got count %d, expected %d", c2, c1)
}
}