-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
130 lines (109 loc) · 3.18 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
121
122
123
124
125
126
127
128
129
130
package water_test
import (
"crypto/rand"
"net"
"reflect"
"testing"
"github.com/refraction-networking/water/internal/log"
"github.com/refraction-networking/water"
)
func TestConfig_Clone(t *testing.T) {
t.Run("Nil Config", testConfigCloneNil)
t.Run("Valid Config", testConfigCloneValid)
}
func testConfigCloneNil(t *testing.T) {
var c *water.Config = nil
ccloned := c.Clone()
if ccloned != nil {
t.Errorf("Clone() = %v, want %v", ccloned, c)
}
}
func testConfigCloneValid(t *testing.T) {
c1 := water.Config{}
v := reflect.ValueOf(&c1).Elem()
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
f := v.Field(i)
// testing/quick can't handle functions or interfaces and so
// isn't used here.
switch fn := typ.Field(i).Name; fn {
case "TransportModuleBin":
f.Set(reflect.ValueOf(make([]byte, 256)))
case "TransportModuleConfig":
f.Set(reflect.ValueOf(water.TransportModuleConfigFromBytes([]byte("foo"))))
case "NetworkDialerFunc", "DialedAddressValidator": // functions aren't deeply equal unless nil
continue
case "NetworkListener":
f.Set(reflect.ValueOf(&net.TCPListener{}))
case "ModuleConfigFactory", "RuntimeConfigFactory":
continue
case "OverrideLogger":
f.Set(reflect.ValueOf(log.DefaultLogger()))
default:
t.Fatalf("unhandled field: %s", fn)
}
}
_, err := rand.Read(c1.TransportModuleBin)
if err != nil {
t.Fatalf("rand.Read error: %v", err)
}
c2 := c1.Clone()
if !reflect.DeepEqual(&c1, c2) {
t.Errorf("Clone() = %v, want %v", c2, &c1)
}
}
func TestConfig_NetworkDialerFuncOrDefault(t *testing.T) {
t.Run("Nil NetworkDialerFunc", testConfigNetworkDialerFuncNil)
t.Run("Valid NetworkDialerFunc", testConfigNetworkDialerFuncValid)
}
func testConfigNetworkDialerFuncNil(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panicked: %v", r)
}
}()
c := &water.Config{}
dialer := c.NetworkDialerFuncOrDefault()
dialer("tcp", "localhost:0") //nolint:errcheck
}
func testConfigNetworkDialerFuncValid(t *testing.T) {
var networkBuf, addressBuf string
var netDialerFunc func(network, address string) (net.Conn, error) = func(network, address string) (net.Conn, error) {
networkBuf = network
addressBuf = address
return nil, nil
}
c := &water.Config{
NetworkDialerFunc: netDialerFunc,
}
dialer := c.NetworkDialerFuncOrDefault()
_, err := dialer("tcp", "localhost:0")
if err != nil {
t.Errorf("NetworkDialerFuncOrDefault() error = %v, want nil", err)
}
if networkBuf != "tcp" || addressBuf != "localhost:0" {
t.Errorf("NetworkDialerFuncOrDefault() = %v, want %v", &dialer, &netDialerFunc)
}
}
func TestConfig_NetworkListenerOrPanic(t *testing.T) {
t.Run("Nil NetworkListener", testConfigNetworkListenerNil)
t.Run("Valid NetworkListener", testConfigNetworkListenerValid)
}
func testConfigNetworkListenerNil(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Did not panic")
}
}()
c := &water.Config{}
c.NetworkListenerOrPanic()
}
func testConfigNetworkListenerValid(t *testing.T) {
c := &water.Config{
NetworkListener: &net.TCPListener{},
}
l := c.NetworkListenerOrPanic()
if l == nil {
t.Errorf("NetworkListenerOrPanic() = %v, want %v", l, &net.TCPListener{})
}
}