-
Notifications
You must be signed in to change notification settings - Fork 17
/
whitelist_test.go
80 lines (68 loc) · 1.57 KB
/
whitelist_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
package dnsp_test
import (
"io/ioutil"
"os"
"testing"
"github.com/gophergala/dnsp"
)
func TestIsAllowedWhite(t *testing.T) {
t.Parallel()
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
tmp.Write([]byte("*.wikipedia.org\n"))
tmp.Write([]byte("github.com\n"))
tmp.Write([]byte("google.com\n"))
s, err := dnsp.NewServer(dnsp.Options{
Whitelist: tmp.Name(),
})
if err != nil {
t.Fatal(err)
}
for host, ok := range map[string]bool{
"blocked.net.": false,
"en.wikipedia.org.": true,
"example.com.": false,
"github.com.": true,
"google.com.": true,
"hu.wikipedia.org.": true,
"wikipedia.org": false,
} {
if act := s.IsAllowed(host); ok != act {
t.Errorf("expected s.IsAllowed(%q) to be %v, got %v", host, ok, act)
}
}
}
func TestIsAllowedBlack(t *testing.T) {
t.Parallel()
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
tmp.Write([]byte("*.xxx\n"))
tmp.Write([]byte("*.sex\n"))
tmp.Write([]byte("doubleclick.net\n"))
tmp.Write([]byte("porn.com\n"))
s, err := dnsp.NewServer(dnsp.Options{
Blacklist: tmp.Name(),
})
if err != nil {
t.Fatal(err)
}
for host, ok := range map[string]bool{
"bar.spam.xxx.": false,
"doubleclick.net.": false,
"example.com.": true,
"foo.sex.": false,
"github.com.": true,
"google.com.": true,
"porn.com.": false,
} {
if act := s.IsAllowed(host); ok != act {
t.Errorf("expected s.IsAllowed(%q) to be %v, got %v", host, ok, act)
}
}
}