-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
175 lines (153 loc) · 3.73 KB
/
server_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
164
165
166
167
168
169
170
171
172
173
174
175
package wgdynamic_test
import (
"context"
"errors"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/wgdynamic-go"
)
type subtest struct {
name string
s *wgdynamic.Server
fn func(t *testing.T, c *wgdynamic.Client)
}
func TestServer(t *testing.T) {
tests := []struct {
name string
subs []subtest
}{
{
name: "RequestIP",
subs: requestIPTests(t),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, st := range tt.subs {
t.Run(st.name, func(t *testing.T) {
c, done := testServer(t, st.s)
defer done()
st.fn(t, c)
})
}
})
}
}
func requestIPTests(t *testing.T) []subtest {
var (
ipv4 = mustIPNet("192.0.2.1/32")
ipv6 = mustIPNet("2001:db8::1/128")
ips = []*net.IPNet{ipv4, ipv6}
want = &wgdynamic.RequestIP{
IPs: ips,
LeaseStart: time.Unix(1, 0),
LeaseTime: 10 * time.Second,
}
)
return []subtest{
{
name: "not implemented",
s: &wgdynamic.Server{
RequestIP: nil,
},
fn: func(t *testing.T, c *wgdynamic.Client) {
_, err := c.RequestIP(context.Background(), nil)
if diff := cmp.Diff(wgdynamic.ErrInvalidRequest, err); diff != "" {
t.Fatalf("unexpected error (-want +got):\n%s", diff)
}
},
},
{
name: "generic error",
s: &wgdynamic.Server{
RequestIP: func(_ net.Addr, _ *wgdynamic.RequestIP) (*wgdynamic.RequestIP, error) {
return nil, errors.New("some error")
},
},
fn: func(t *testing.T, c *wgdynamic.Client) {
_, err := c.RequestIP(context.Background(), nil)
if diff := cmp.Diff(wgdynamic.ErrInvalidRequest, err); diff != "" {
t.Fatalf("unexpected error (-want +got):\n%s", diff)
}
},
},
{
name: "OK client request",
s: &wgdynamic.Server{
RequestIP: func(_ net.Addr, r *wgdynamic.RequestIP) (*wgdynamic.RequestIP, error) {
// Return the addresses requested by client, but also
// populate lease time fields.
r.LeaseStart = want.LeaseStart
r.LeaseTime = want.LeaseTime
return r, nil
},
},
fn: func(t *testing.T, c *wgdynamic.Client) {
got, err := c.RequestIP(context.Background(), &wgdynamic.RequestIP{
IPs: ips,
})
if err != nil {
t.Fatalf("failed to request IP: %v", err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected RequestIP (-want +got):\n%s", diff)
}
},
},
{
name: "OK auto assign",
s: &wgdynamic.Server{
RequestIP: func(_ net.Addr, r *wgdynamic.RequestIP) (*wgdynamic.RequestIP, error) {
// Ensure the Client does not request any addresses.
for len(r.IPs) > 0 {
return nil, errors.New("could not assign requested addresses")
}
return want, nil
},
},
fn: func(t *testing.T, c *wgdynamic.Client) {
got, err := c.RequestIP(context.Background(), nil)
if err != nil {
t.Fatalf("failed to request IP: %v", err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected RequestIP (-want +got):\n%s", diff)
}
},
},
}
}
func testServer(t *testing.T, s *wgdynamic.Server) (*wgdynamic.Client, func()) {
t.Helper()
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Serve(l); err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
panicf("failed to serve: %v", err)
}
}()
c := &wgdynamic.Client{
Dial: func(ctx context.Context) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "tcp", l.Addr().String())
},
}
return c, func() {
defer wg.Wait()
if err := s.Close(); err != nil {
t.Fatalf("failed to close server listener: %v", err)
}
}
}