-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
132 lines (105 loc) · 2.69 KB
/
options_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
package wg
import (
"errors"
"github.com/point-c/wgapi/wgconfig"
"github.com/stretchr/testify/require"
"golang.zx2c4.com/wireguard/device"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"testing"
)
func TestOptionNop(t *testing.T) {
o := &options{}
err := OptionNop()(o)
require.NoError(t, err)
}
func TestOptionErr(t *testing.T) {
o := &options{}
testError := errors.New("test error")
err := OptionErr(testError)(o)
require.Error(t, err)
require.Equal(t, testError, err)
}
func TestOptionDevice(t *testing.T) {
o := &options{}
var n Netstack
err := OptionDevice(&n)(o)
require.NoError(t, err)
require.Equal(t, &n, o.tun)
err = OptionDevice(nil)(o)
require.NoError(t, err)
}
func TestOptionBind(t *testing.T) {
o := &options{}
var b Bind
err := OptionBind(b)(o)
require.NoError(t, err)
require.Equal(t, b, o.bind)
err = OptionBind(nil)(o)
require.NoError(t, err)
}
func TestOptionLogger(t *testing.T) {
o := &options{}
var l device.Logger
err := OptionLogger(&l)(o)
require.NoError(t, err)
require.Contains(t, o.loggers, &l)
err = OptionLogger(nil)(o)
require.NoError(t, err)
}
func TestOptionConfig(t *testing.T) {
o := &options{}
cfg := wgconfig.Client{}
err := OptionConfig(&cfg)(o)
require.NoError(t, err)
require.Equal(t, &cfg, *o.cfg)
err = OptionConfig(nil)(o)
require.NoError(t, err)
}
func TestOptionNetDevice(t *testing.T) {
o := &options{}
var p *Net
err := OptionNetDevice(&p)(o)
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, o.tun)
require.Len(t, o.closer, 1)
err = OptionNetDevice(nil)(o)
require.Error(t, err)
defer func(fn func(s *stack.Stack, ep *channel.Endpoint, id *tcpip.NICID) error) { SetStackOptions = fn }(SetStackOptions)
errExp := errors.New("test")
SetStackOptions = func(*stack.Stack, *channel.Endpoint, *tcpip.NICID) error { return errExp }
err = OptionNetDevice(&p)(o)
require.ErrorIs(t, err, errExp)
}
func TestOptionCloser(t *testing.T) {
o := &options{}
called := false
closer := func() error {
called = true
return nil
}
err := OptionCloser(closer)(o)
require.NoError(t, err)
require.Len(t, o.closer, 1)
_ = o.closer[0]()
require.True(t, called)
}
func TestOptionsApply(t *testing.T) {
o := &options{}
opts := []Option{OptionNop(), OptionErr(errors.New("test error")), OptionNop()}
err := o.apply(opts)
require.Error(t, err)
}
func TestOptionsCleanUp(t *testing.T) {
o := &options{}
failed := true
var err error
o.cleanUp(&failed, &err)
require.Nil(t, err)
o.closer = append(o.closer, func() error { return errors.New("close error") })
o.cleanUp(&failed, &err)
require.Error(t, err)
require.EqualError(t, err, "close error")
}