-
Notifications
You must be signed in to change notification settings - Fork 23
/
config_test.go
120 lines (111 loc) · 2.64 KB
/
config_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
package usocksd
import (
"net"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestEmptyConfig(t *testing.T) {
t.Parallel()
c := NewConfig()
expected := &Config{
Incoming: IncomingConfig{
Port: defaultPort,
MetricsPort: defaultMetricsPort,
},
Outgoing: OutgoingConfig{},
}
options := []cmp.Option{
cmpopts.IgnoreUnexported(Config{}),
cmpopts.IgnoreUnexported(IncomingConfig{}),
cmpopts.IgnoreUnexported(OutgoingConfig{}),
}
if diff := cmp.Diff(c, expected, options...); diff != "" {
t.Fatalf("unexpected config (-actual +expected):\n%s", diff)
}
if !c.allowPort(443) {
t.Error("port 443 must be allowed")
}
}
func TestConfig(t *testing.T) {
t.Parallel()
c := NewConfig()
if err := c.Load("test/test1.toml"); err != nil {
t.Fatal(err)
}
expected := &Config{
Incoming: IncomingConfig{
Port: 1080,
MetricsPort: 8081,
Addresses: []net.IP{
net.ParseIP("127.0.0.1"),
},
AllowFrom: []string{
"10.0.0.0/8",
"192.168.1.1",
},
},
Outgoing: OutgoingConfig{
AllowSites: []string{
"www.amazon.com",
".google.com",
},
DenySites: []string{
".2ch.net",
"bad.google.com",
},
Addresses: []net.IP{
net.ParseIP("12.34.56.78"),
},
DenyPorts: []int{22, 25},
DNSBLDomain: "zen.spamhaus.org",
},
}
options := []cmp.Option{
cmpopts.IgnoreUnexported(Config{}),
cmpopts.IgnoreUnexported(IncomingConfig{}),
cmpopts.IgnoreUnexported(OutgoingConfig{}),
}
if diff := cmp.Diff(c, expected, options...); diff != "" {
t.Fatalf("unexpected config (-actual +expected):\n%s", diff)
}
if !c.allowIP(net.ParseIP("10.1.32.4")) {
t.Error("10.1.32.4 is not allowed")
}
if !c.allowIP(net.ParseIP("192.168.1.1")) {
t.Error("192.168.1.1 is not allowed")
}
if c.allowIP(net.ParseIP("12.34.56.78")) {
t.Error("12.34.56.78 shout not be allowed")
}
if !c.allowFQDN("www.amazon.com") {
t.Error("www.amazon.com should be allowed")
}
if !c.allowFQDN("www.google.com") {
t.Error("www.google.com should be allowed")
}
if c.allowFQDN("bad.google.com") {
t.Error("bad.amazon.com should be denied")
}
if c.allowFQDN("www.2ch.net") {
t.Error("www.2ch.net should be denied")
}
if c.allowPort(25) {
t.Error("port 25 must not be allowed")
}
if !c.allowPort(443) {
t.Error("port 443 must be allowed")
}
}
func TestConfigFail(t *testing.T) {
t.Parallel()
c := NewConfig()
if err := c.Load("test/test2.toml"); err == nil {
t.Error("loadConfig should fail for test2.toml")
}
// expect type mismatch
c = NewConfig()
if err := c.Load("test/test3.toml"); err == nil {
t.Error("loadConfig should fail for test3.toml")
}
}