-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
list_cache_test.go
420 lines (354 loc) · 12.6 KB
/
list_cache_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package lists
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http/httptest"
"os"
"strings"
"time"
. "github.com/0xERR0R/blocky/evt"
. "github.com/0xERR0R/blocky/helpertest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("ListCache", func() {
var (
tmpDir *TmpFolder
emptyFile, file1, file2, file3 *TmpFile
server1, server2, server3 *httptest.Server
)
BeforeEach(func() {
tmpDir = NewTmpFolder("ListCache")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)
server1 = TestServer("blocked1.com\nblocked1a.com\n192.168.178.55")
DeferCleanup(server1.Close)
server2 = TestServer("blocked2.com")
DeferCleanup(server2.Close)
server3 = TestServer("blocked3.com\nblocked1a.com")
DeferCleanup(server3.Close)
emptyFile = tmpDir.CreateStringFile("empty", "#empty file")
Expect(emptyFile.Error).Should(Succeed())
file1 = tmpDir.CreateStringFile("file1", "blocked1.com", "blocked1a.com")
Expect(file1.Error).Should(Succeed())
file2 = tmpDir.CreateStringFile("file2", "blocked2.com")
Expect(file2.Error).Should(Succeed())
file3 = tmpDir.CreateStringFile("file3", "blocked3.com", "blocked1a.com")
Expect(file3.Error).Should(Succeed())
})
Describe("List cache and matching", func() {
When("Query with empty", func() {
It("should not panic", func() {
lists := map[string][]string{
"gr0": {emptyFile.Path},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
found, group := sut.Match("", []string{"gr0"})
Expect(found).Should(BeFalse())
Expect(group).Should(BeEmpty())
})
})
When("List is empty", func() {
It("should not match anything", func() {
lists := map[string][]string{
"gr1": {emptyFile.Path},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
found, group := sut.Match("google.com", []string{"gr1"})
Expect(found).Should(BeFalse())
Expect(group).Should(BeEmpty())
})
})
When("a temporary/transient err occurs on download", func() {
It("should not delete existing elements from group cache", func() {
// should produce a transient error on second and third attempt
data := make(chan func() (io.ReadCloser, error), 3)
mockDownloader := &MockDownloader{data: data}
//nolint:unparam
data <- func() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("blocked1.com")), nil
}
//nolint:unparam
data <- func() (io.ReadCloser, error) {
return nil, &TransientError{inner: errors.New("boom")}
}
//nolint:unparam
data <- func() (io.ReadCloser, error) {
return nil, &TransientError{inner: errors.New("boom")}
}
lists := map[string][]string{
"gr1": {"http://dummy"},
}
sut, err := NewListCache(
ListCacheTypeBlacklist, lists,
4*time.Hour,
mockDownloader,
defaultProcessingConcurrency,
false,
)
Expect(err).Should(Succeed())
By("Lists loaded without timeout", func() {
Eventually(func(g Gomega) {
found, group := sut.Match("blocked1.com", []string{"gr1"})
g.Expect(found).Should(BeTrue())
g.Expect(group).Should(Equal("gr1"))
}, "1s").Should(Succeed())
})
Expect(sut.refresh(true)).Should(HaveOccurred())
By("List couldn't be loaded due to timeout", func() {
found, group := sut.Match("blocked1.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
})
sut.Refresh()
By("List couldn't be loaded due to timeout", func() {
found, group := sut.Match("blocked1.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
})
})
})
When("non transient err occurs on download", func() {
It("should delete existing elements from group cache", func() {
// should produce a 404 err on second attempt
data := make(chan func() (io.ReadCloser, error), 2)
mockDownloader := &MockDownloader{data: data}
//nolint:unparam
data <- func() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("blocked1.com")), nil
}
data <- func() (io.ReadCloser, error) {
return nil, errors.New("boom")
}
lists := map[string][]string{
"gr1": {"http://dummy"},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, mockDownloader,
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
By("Lists loaded without err", func() {
Eventually(func(g Gomega) {
found, group := sut.Match("blocked1.com", []string{"gr1"})
g.Expect(found).Should(BeTrue())
g.Expect(group).Should(Equal("gr1"))
}, "1s").Should(Succeed())
})
Expect(sut.refresh(false)).Should(HaveOccurred())
By("List couldn't be loaded due to 404 err", func() {
Eventually(func() bool {
found, _ := sut.Match("blocked1.com", []string{"gr1"})
return found
}, "1s").Should(BeFalse())
})
})
})
When("Configuration has 3 external working urls", func() {
It("should download the list and match against", func() {
lists := map[string][]string{
"gr1": {server1.URL, server2.URL},
"gr2": {server3.URL},
}
sut, _ := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
found, group := sut.Match("blocked1.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr2"))
})
})
When("Configuration has some faulty urls", func() {
It("should download the list and match against", func() {
lists := map[string][]string{
"gr1": {server1.URL, server2.URL, "doesnotexist"},
"gr2": {server3.URL, "someotherfile"},
}
sut, _ := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
found, group := sut.Match("blocked1.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr2"))
})
})
When("List will be updated", func() {
It("event should be fired and contain count of elements in downloaded lists", func() {
lists := map[string][]string{
"gr1": {server1.URL},
}
resultCnt := 0
_ = Bus().SubscribeOnce(BlockingCacheGroupChanged, func(listType ListCacheType, group string, cnt int) {
resultCnt = cnt
})
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
found, group := sut.Match("blocked1.com", []string{})
Expect(found).Should(BeFalse())
Expect(group).Should(BeEmpty())
Expect(resultCnt).Should(Equal(3))
})
})
When("multiple groups are passed", func() {
It("should match", func() {
lists := map[string][]string{
"gr1": {file1.Path, file2.Path},
"gr2": {"file://" + file3.Path},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(), defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
Expect(sut.groupCaches["gr1"].ElementCount()).Should(Equal(3))
Expect(sut.groupCaches["gr2"].ElementCount()).Should(Equal(2))
found, group := sut.Match("blocked1.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr1", "gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("blocked1a.com", []string{"gr2"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr2"))
})
})
When("group with bigger files", func() {
It("should match", func() {
file1 := createTestListFile(GinkgoT().TempDir(), 10000)
file2 := createTestListFile(GinkgoT().TempDir(), 15000)
file3 := createTestListFile(GinkgoT().TempDir(), 13000)
lists := map[string][]string{
"gr1": {file1, file2, file3},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
Expect(sut.groupCaches["gr1"].ElementCount()).Should(Equal(38000))
})
})
When("inline list content is defined", func() {
It("should match", func() {
lists := map[string][]string{
"gr1": {"inlinedomain1.com\n#some comment\ninlinedomain2.com"},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
Expect(sut.groupCaches["gr1"].ElementCount()).Should(Equal(2))
found, group := sut.Match("inlinedomain1.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("inlinedomain2.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
})
})
When("Text file can't be parsed", func() {
It("should still match already imported strings", func() {
// 2nd line is too long and will cause an error
lists := map[string][]string{
"gr1": {"inlinedomain1.com\n" + strings.Repeat("longString", 100000)},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
found, group := sut.Match("inlinedomain1.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
})
})
When("inline regex content is defined", func() {
It("should match", func() {
lists := map[string][]string{
"gr1": {"/^apple\\.(de|com)$/\n"},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, 0, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
found, group := sut.Match("apple.com", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
found, group = sut.Match("apple.de", []string{"gr1"})
Expect(found).Should(BeTrue())
Expect(group).Should(Equal("gr1"))
})
})
})
Describe("Configuration", func() {
When("refresh is enabled", func() {
It("should print list configuration", func() {
lists := map[string][]string{
"gr1": {server1.URL, server2.URL},
"gr2": {"inline\ndefinition\n"},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, time.Hour, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
c := sut.Configuration()
Expect(c).Should(ContainElement("refresh period: 1 hour"))
Expect(len(c)).Should(BeNumerically(">", 1))
})
})
When("refresh is disabled", func() {
It("should print 'refresh disabled'", func() {
lists := map[string][]string{
"gr1": {emptyFile.Path},
}
sut, err := NewListCache(ListCacheTypeBlacklist, lists, -1, NewDownloader(),
defaultProcessingConcurrency, false)
Expect(err).Should(Succeed())
c := sut.Configuration()
Expect(c).Should(ContainElement("refresh: disabled"))
})
})
})
Describe("StartStrategy", func() {
When("async load is enabled", func() {
It("should never return an error", func() {
lists := map[string][]string{
"gr1": {"doesnotexist"},
}
_, err := NewListCache(ListCacheTypeBlacklist, lists, -1, NewDownloader(),
defaultProcessingConcurrency, true)
Expect(err).Should(Succeed())
})
})
})
})
type MockDownloader struct {
data chan func() (io.ReadCloser, error)
}
func (m *MockDownloader) DownloadFile(_ string) (io.ReadCloser, error) {
fn := <-m.data
return fn()
}
func createTestListFile(dir string, totalLines int) string {
file, err := os.CreateTemp(dir, "blocky")
if err != nil {
log.Fatal(err)
}
w := bufio.NewWriter(file)
for i := 0; i < totalLines; i++ {
fmt.Fprintln(w, RandStringBytes(8+rand.Intn(10))+".com")
}
w.Flush()
return file.Name()
}
const charpool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = charpool[rand.Intn(len(charpool))]
}
return string(b)
}