-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_test.go
163 lines (139 loc) · 3.79 KB
/
block_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
package block
import (
"os"
"strings"
"testing"
)
func TestBlocked(t *testing.T) {
var list = `
127.0.0.1 005.free-counter.co.uk
127.0.0.1 006.free-adult-counters.x-xtra.com
127.0.0.1 006.free-counter.co.uk
127.0.0.1 007.free-counter.co.uk
127.0.0.1 007.go2cloud.org
008.free-counter.co.uk
com
`
b := new(Block)
b.superapi_enabled = true
os.Remove("/tmp/block_test.db")
b.setupDB("/tmp/block_test.db")
r := strings.NewReader(list)
l := make(map[string]DomainValue)
listRead(r, l, 0)
b.update = l
err := b.UpdateDomains(b.update)
if err != nil {
log.Fatal("failed to update block_test -- ", err)
}
_, found := b.getDomain("no.exist")
if found {
log.Fatal("found missing domain")
}
_, found = b.getDomain("007.go2cloud.org.")
if !found {
log.Fatal("failed to look up stored domain")
}
tests := []struct {
name string
blocked bool
}{
{"example.org.", false},
{"com.", true},
{"005.free-counter.co.uk.", true},
{"www.005.free-counter.co.uk.", true},
{"008.free-counter.co.uk.", true},
{"www.008.free-counter.co.uk.", true},
}
//func (b *Block) blocked(IP string, name string, returnIP *string) bool {
for _, test := range tests {
retIP := ""
retCNAME := ""
hasPermit := false
categories := []string{}
got := b.blocked("1.2.3.4", test.name, &retIP, &retCNAME, &hasPermit, &categories)
if got != test.blocked {
t.Errorf("Expected %s to be blocked", test.name)
}
}
}
func TestOverrides(t *testing.T) {
var list = `
127.0.0.1 005.free-counter.co.uk
127.0.0.1 006.free-adult-counters.x-xtra.com
127.0.0.1 006.free-counter.co.uk
127.0.0.1 007.free-counter.co.uk
127.0.0.1 007.go2cloud.org
127.0.0.1 override.com
127.0.0.1 ip.permit.com
127.0.0.1 cname.permit.com
008.free-counter.co.uk
com
`
b := new(Block)
os.Remove("/tmp/block_test.db")
b.setupDB("/tmp/block_test.db")
r := strings.NewReader(list)
l := make(map[string]DomainValue)
listRead(r, l, 0)
b.update = l
b.superapi_enabled = true
err := b.UpdateDomains(b.update)
if err != nil {
log.Fatal("failed to update block_test -- ", err)
}
permit := DomainOverride{"Permit", "override.com.", "", "", "*", 0, []string{}}
ip_permit := DomainOverride{"Permit", "ip.permit.com.", "1.1.1.1", "", "*", 0, []string{}}
cname_permit := DomainOverride{"Permit", "cname.permit.com.", "", "safesearch.permit.com", "*", 0, []string{}}
override := OverrideList{}
override.PermitDomains = []DomainOverride{permit, ip_permit, cname_permit}
override.Enabled = true
b.config.OverrideLists = []OverrideList{override}
_, found := b.getDomain("no.exist")
if found {
log.Fatal("found missing domain")
}
_, found = b.getDomain("007.go2cloud.org.")
if !found {
log.Fatal("failed to look up stored domain")
}
tests := []struct {
name string
blocked bool
}{
{"example.org.", false},
{"com.", true},
{"005.free-counter.co.uk.", true},
{"www.005.free-counter.co.uk.", true},
{"008.free-counter.co.uk.", true},
{"www.008.free-counter.co.uk.", true},
{"override.com.", false},
{"ip.permit.com.", false},
{"cname.permit.com.", false},
}
//func (b *Block) blocked(IP string, name string, returnIP *string) bool {
for _, test := range tests {
retIP := ""
retCNAME := ""
categories := []string{}
hasPermit := false
got := b.blocked("1.2.3.4", test.name, &retIP, &retCNAME, &hasPermit, &categories)
if got != test.blocked {
if test.blocked == false {
t.Errorf("Expected `%s` to be permitted", test.name)
} else {
t.Errorf("Expected `%s` to be blocked", test.name)
}
}
if test.name == "ip.permit.com." {
if retIP != "1.1.1.1" {
t.Errorf("Expected IP override `%s` to be 1.1.1.1", retIP)
}
}
if test.name == "cname.permit.com." {
if retCNAME != "safesearch.permit.com" {
t.Errorf("Expected CNAME override `%s` to be safesearch.permit.com", retCNAME)
}
}
}
}