-
Notifications
You must be signed in to change notification settings - Fork 14
/
blocklist_test.go
270 lines (220 loc) · 6.78 KB
/
blocklist_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
package sqids
import (
"reflect"
"testing"
)
func TestBlocklist(t *testing.T) {
var (
defaultBlocklist = Blocklist()
customBlocklist = Blocklist("custom1", "custom2")
)
if got, want := len(defaultBlocklist), len(newDefaultBlocklist()); got != want {
t.Fatalf("len(defaultBlocklist) = %d, want %d", got, want)
}
if got, want := len(customBlocklist), len(defaultBlocklist)+2; got != want {
t.Fatalf("len(customBlocklist) = %d, want %d", got, want)
}
}
func TestBlocklistDefault(t *testing.T) {
numbers := []uint64{4572721}
blockedID := "aho1e"
unblockedID := "JExTR"
s, err := New()
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(blockedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", blockedID, numbers, decodedNumbers)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if unblockedID != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, unblockedID, generatedID)
}
}
func TestBlocklistEmpty(t *testing.T) {
numbers := []uint64{4572721}
id := "aho1e"
s, err := New(Options{
Blocklist: []string{},
})
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
}
func TestBlocklistNonEmpty(t *testing.T) {
numbers := []uint64{4572721}
id := "aho1e"
s, err := New(Options{
Blocklist: []string{
"ArUO", // originally encoded [100000]
},
})
if err != nil {
t.Fatal(err)
}
// make sure we don't use the default blocklist
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
// make sure we are using the passed blocklist
decodedNumbers = s.Decode("ArUO")
if !reflect.DeepEqual([]uint64{100_000}, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, []uint64{100_000}, decodedNumbers)
}
generatedID, err = s.Encode([]uint64{100_000})
if err != nil {
t.Fatal(err)
}
if generatedID != "QyG4" {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", []uint64{100_000}, "QyG4", generatedID)
}
decodedNumbers = s.Decode("QyG4")
if !reflect.DeepEqual([]uint64{100_000}, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, []uint64{100_000}, decodedNumbers)
}
}
func TestNewBlocklist(t *testing.T) {
numbers := []uint64{1000000, 2000000}
id := "1aYeB7bRUt"
s, err := New(Options{
Blocklist: []string{
"JSwXFaosAN", // normal result of 1st encoding, let's block that word on purpose
"OCjV9JK64o", // result of 2nd encoding
"rBHf", // result of 3rd encoding is `4rBHfOiqd3`, let's block a substring
"79SM", // result of 4th encoding is `dyhgw479SM`, let's block the postfix
"7tE6", // result of 4th encoding is `7tE6jdAHLe`, let's block the prefix
},
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
func TestDecodingBlocklistedIDs(t *testing.T) {
numbers := []uint64{1, 2, 3}
blocklist := []string{"86Rf07", "se8ojk", "ARsz1p", "Q8AI49", "5sQRZO"}
s, err := New(Options{
Blocklist: blocklist,
})
if err != nil {
t.Fatal(err)
}
for _, id := range blocklist {
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(decodedNumbers, numbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
}
func TestShortBlocklistMatch(t *testing.T) {
numbers := []uint64{1_000}
s, err := New(Options{
Blocklist: []string{"pnd"},
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", generatedID, numbers, decodedNumbers)
}
}
func TestUpperCaseAlphabetBlocklistFiltering(t *testing.T) {
numbers := []uint64{1, 2, 3}
id := "IBSHOZ"
s, err := New(Options{
Alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Blocklist: []string{"sxnzkl"}, // lowercase blocklist in only-uppercase alphabet
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(id)
// without blocklist, would've been "SXNZKL"
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", generatedID, numbers, decodedNumbers)
}
}
func TestMaxEncodingAttempts(t *testing.T) {
alphabet := "abc"
minLength := uint8(3)
blocklist := []string{"cab", "abc", "bca"}
s, err := New(Options{
Alphabet: alphabet,
MinLength: minLength,
Blocklist: blocklist,
})
if err != nil {
t.Fatal(err)
}
if len(alphabet) != int(minLength) || len(blocklist) != int(minLength) {
t.Errorf("`TestMaxEncodingAttempts` is not setup properly")
}
if _, err := s.Encode([]uint64{0}); err == nil {
t.Errorf("Should throw error about max regeneration attempts")
}
}
func TestFilterBlocklist(t *testing.T) {
t.Run("no words less than 3 chars", func(t *testing.T) {
filtered := filterBlocklist("YESNO", []string{"yes", "no"})
if got, want := len(filtered), 1; got != want {
t.Fatalf("len(filtered) = %d, want %d", got, want)
}
if got, want := filtered[0], "yes"; got != want {
t.Fatalf("filtered[0] = %q, want %q", got, want)
}
})
t.Run("remove words containing letters not in alphabet", func(t *testing.T) {
filtered := filterBlocklist("YESNO", []string{"yes", "nope"})
if got, want := len(filtered), 1; got != want {
t.Fatalf("len(filtered) = %d, want %d", got, want)
}
if got, want := filtered[0], "yes"; got != want {
t.Fatalf("filtered[0] = %q, want %q", got, want)
}
})
}