-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathblocklist_test.go
187 lines (160 loc) · 4.82 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
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{200044}
blockedID := "sexy"
unblockedID := "d171vI"
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{200044}
id := "sexy"
s, err := NewCustom(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{200044}
id := "sexy"
s, err := NewCustom(Options{
Blocklist: &[]string{
"AvTg", // 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("AvTg")
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 != "7T1X8k" {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", []uint64{100_000}, "7T1X8k", generatedID)
}
decodedNumbers = s.Decode("7T1X8k")
if !reflect.DeepEqual([]uint64{100_000}, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, []uint64{100_000}, decodedNumbers)
}
}
func TestNewCustomBlocklist(t *testing.T) {
numbers := []uint64{1, 2, 3}
id := "TM0x1Mxz"
s, err := NewCustom(Options{
Blocklist: &[]string{
"8QRLaD", // normal result of 1st encoding, let's block that word on purpose
"7T1cd0dL", // result of 2nd encoding
"UeIe", // result of 3rd encoding is `RA8UeIe7`, let's block a substring
"imhw", // result of 4th encoding is `WM3Limhw`, let's block the postfix
"LfUQ", // result of 4th encoding is `LfUQh4HN`, 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{
"8QRLaD", "7T1cd0dL", "RA8UeIe7", "WM3Limhw", "LfUQh4HN",
}
s, err := NewCustom(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 := NewCustom(Options{
Blocklist: &[]string{"pPQ"},
})
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)
}
}