-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblocking_test.go
More file actions
70 lines (61 loc) · 2.02 KB
/
blocking_test.go
File metadata and controls
70 lines (61 loc) · 2.02 KB
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
package httprelay
import (
"bytes"
"net"
"testing"
"github.com/cobratbq/goutils/std/builtin/set"
assert "github.com/cobratbq/goutils/std/testing"
)
func TestBlocklistDialerNilDialer(t *testing.T) {
defer assert.RequirePanic(t)
b := BlocklistDialer{List: make(map[string]struct{}, 0), Dialer: nil}
b.Dial("tcp", "hello.world:80")
t.FailNow()
}
func TestBlocklistDialerDirectDialer(t *testing.T) {
b := BlocklistDialer{List: make(map[string]struct{}, 0), Dialer: &TestNopDialer{}}
if _, err := b.Dial("tcp", "hello.world:80"); err != nil {
t.FailNow()
}
}
func TestBlocklistDialerBlockedAddress(t *testing.T) {
b := BlocklistDialer{List: set.Create("hello.world"), Dialer: &TestNopDialer{}}
if _, err := b.Dial("tcp", "hello.world:80"); err == ErrBlockedHost {
return
}
t.FailNow()
}
func TestBlocklistDialerLoadHosts(t *testing.T) {
hostsFile := []byte("127.0.0.1 localhost\n0.0.0.0 hello.world\n# the next line tests 2 host names for one destination address\n0.0.0.0 hello.world.too hello.world.future\n")
b := BlocklistDialer{List: make(map[string]struct{}, 0), Dialer: &TestNopDialer{}}
b.Load(bytes.NewReader(hostsFile))
if _, err := b.Dial("tcp", "hello.world:80"); err != ErrBlockedHost {
t.FailNow()
}
if _, err := b.Dial("tcp", "hello.world.too:443"); err != ErrBlockedHost {
t.FailNow()
}
if _, err := b.Dial("tcp", "hello.world.future:443"); err != ErrBlockedHost {
t.FailNow()
}
if _, err := b.Dial("tcp", "hello.world.past:80"); err != nil {
t.FailNow()
}
}
func TestLoadBlocklistFromFile(t *testing.T) {
dialer := BlocklistDialer{List: make(map[string]struct{}, 0), Dialer: &TestNopDialer{}}
loadHostsFile(&dialer, "test/hosts")
if _, err := dialer.Dial("tcp", "hello.world:443"); err != ErrBlockedHost {
t.Fail()
}
if _, err := dialer.Dial("tcp", "hello.past:80"); err != ErrBlockedHost {
t.Fail()
}
if _, err := dialer.Dial("tcp", "hello.future:443"); err != nil {
t.Fail()
}
}
type TestNopDialer struct{}
func (*TestNopDialer) Dial(network, addr string) (net.Conn, error) {
return nil, nil
}