-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_test.go
286 lines (233 loc) · 5.94 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
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
package goseq
import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
)
var (
err_type_coercion string = "Value returned is not value input (unexpected type coercion)!"
err_val_corruption = "Value set is not value returned (value corruption)!"
)
func testFilterSuite(keys map[string]interface{}) (Filter, error) {
fil := NewFilter()
for key, val := range keys {
err := fil.Set(key, val)
if err != nil {
return fil, err
}
}
return fil, nil
}
// some other, non-supported type
type _unknown struct {
x string
y int32
z bool
}
func TestFilter_Set_string(t *testing.T) {
_, err := testFilterSuite(map[string]interface{}{"somekey": "someval"})
if err != nil {
t.Log("Unexpected error on string value:")
t.Log(err)
t.FailNow()
}
}
func TestFilter_Set_int(t *testing.T) {
_, err := testFilterSuite(map[string]interface{}{"somekey52": 43836})
if err != nil {
t.Log("Unexpected error on int value:")
t.Log(err)
t.FailNow()
}
}
func TestFilter_Set_bool(t *testing.T) {
_, err := testFilterSuite(map[string]interface{}{"somekey3": true})
if err != nil {
t.Log("Unexpected error on bool value:")
t.Log(err)
t.FailNow()
}
}
func TestFilter_Set_unsupported(t *testing.T) {
_, err := testFilterSuite(map[string]interface{}{"letestkey": _unknown{}})
if err == nil {
t.Log("Expected error for unknown value type!")
t.FailNow()
}
}
func TestFilter_Get_string(t *testing.T) {
key := "somekey"
value := "somevalue"
fil, _ := testFilterSuite(map[string]interface{}{key: value})
interfaceback, err := fil.Get(key)
if err != nil {
t.Log("Unexpected error:")
t.Log(err)
t.FailNow()
}
valback, ok := interfaceback.(string)
if !ok {
t.Log(err_type_coercion)
}
if valback != value {
t.Log(err_val_corruption)
}
}
func TestFilter_Get_int(t *testing.T) {
key := "as83hasj_sa"
value := 38263
fil, _ := testFilterSuite(map[string]interface{}{key: value})
interfaceback, err := fil.Get(key)
if err != nil {
t.Log("Unexpected error:")
t.Log(err)
t.FailNow()
}
valback, ok := interfaceback.(int)
if !ok {
t.Log(err_type_coercion)
t.FailNow()
}
if valback != value {
t.Log(err_val_corruption)
t.FailNow()
}
}
func TestFilter_Get_bool(t *testing.T) {
key := "uhsdoygfas"
value := false
fil, _ := testFilterSuite(map[string]interface{}{key: value})
interfaceback, err := fil.Get(key)
if err != nil {
t.Log("Unexpected error:")
t.Log(err)
t.FailNow()
}
valback, ok := interfaceback.(bool)
if !ok {
t.Log(err_type_coercion)
t.FailNow()
}
if valback != value {
t.Log(err_val_corruption)
t.FailNow()
}
}
func TestFilter_Has(t *testing.T) {
key := "48ahusn4sa"
fil, _ := testFilterSuite(map[string]interface{}{key: 84724})
if !fil.Has(key) {
t.Log("Has did not register our key!")
t.FailNow()
}
}
func TestFilter_Keys(t *testing.T) {
valmap := map[string]interface{}{
"string": "what what",
"int": 3242,
"bool": true,
}
actualkeys := make([]string, len(valmap))
for key, _ := range valmap {
actualkeys = append(actualkeys, key)
}
fil, err := testFilterSuite(valmap)
if err != nil {
t.Log(err)
t.FailNow()
}
sortedActual := sort.StringSlice(actualkeys)
sortedActual.Sort()
sortedReported := sort.StringSlice(fil.Keys())
sortedReported.Sort()
if !reflect.DeepEqual(sortedActual, sortedReported) {
t.Log("Array keys are not the same!")
}
}
func TestFilter_GetFilterFormat_string(t *testing.T) {
fil, _ := testFilterSuite(map[string]interface{}{"key": "value"})
expected := "\\key\\value\x00"
if string(fil.GetFilterFormat()) != expected {
t.Log("String not serialized correctly!")
t.FailNow()
}
}
func TestFilter_GetFilterFormat_boolt(t *testing.T) {
fil, _ := testFilterSuite(map[string]interface{}{"keybool": true})
expected := "\\keybool\\1\x00"
if string(fil.GetFilterFormat()) != expected {
t.Log("Bool:True not serialized correctly!")
t.FailNow()
}
}
func TestFilter_GetFilterFormat_boolf(t *testing.T) {
fil, _ := testFilterSuite(map[string]interface{}{"keyboolfalse": false})
expected := "\\keyboolfalse\\0\x00"
if string(fil.GetFilterFormat()) != expected {
t.Log("Bool:False not serialized correctly!")
t.FailNow()
}
}
func TestFilter_GetFilterFormat_int(t *testing.T) {
fil, _ := testFilterSuite(map[string]interface{}{"keyint": 382634})
expected := "\\keyint\\382634\x00"
if string(fil.GetFilterFormat()) != expected {
t.Log("Int not serialized correctly!")
t.FailNow()
}
}
func TestFilter_GetFilterFormat_multi(t *testing.T) {
fil, _ := testFilterSuite(map[string]interface{}{
"keyint": 382634,
"keybool": true,
"keystring": "lol wat?",
})
expected := "\\keybool\\1\\keyint\\382634\\keystring\\lol wat?\x00"
// need to extract and sort since
// order is not important
rawFmt := fil.GetFilterFormat()
if rawFmt[0] != byte('\\') {
t.Log("Multi Fmt not formatted correctly.")
t.FailNow()
}
if rawFmt[len(rawFmt)-1] != 0x0 {
t.Log("Filter string must be NULL terminated.")
t.FailNow()
}
rawFmt = rawFmt[:len(rawFmt)-1] // remove trailing null temporarily
parts := strings.Split(string(rawFmt), "\\")[1:] // 1st is blank
remap := make(map[string]string)
keys := make([]string, len(parts))
if len(parts)%2 != 0 {
t.Log("Odd number of objects (missing a key or value): malformatted.")
t.FailNow()
}
for i := 0; i < len(parts); i += 2 {
key := parts[i]
val := parts[i+1]
keys = append(keys, key)
remap[key] = val
}
sorted := sort.StringSlice(keys)
sorted.Sort()
reassembled := make([]string, 0)
for _, key := range sorted {
// catch the blanks between \
if key == "" {
continue
}
reassembled = append(reassembled, key)
reassembled = append(reassembled, remap[key])
}
joined := strings.Join(reassembled, "\\")
sortedFilterOutput := fmt.Sprintf("\\%v\x00", joined)
if expected != sortedFilterOutput {
t.Log("Filter with multi-keys is not formatted correctly.")
t.Log("Expected:", expected)
t.Log("Got:", sortedFilterOutput)
t.Log("Raw:", string(fil.GetFilterFormat()))
t.FailNow()
}
}