forked from mdlayher/ndp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
197 lines (166 loc) · 4.07 KB
/
conn_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package ndp
import (
"bytes"
"errors"
"net"
"net/netip"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestConn(t *testing.T) {
tests := []struct {
name string
fn func(t *testing.T, c1, c2 *Conn, addr netip.Addr)
}{
{
name: "echo",
fn: testConnEcho,
},
{
name: "filter invalid",
fn: testConnFilterInvalid,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c1, c2, addr := testICMPConn(t)
tt.fn(t, c1, c2, addr)
})
}
}
func testConnEcho(t *testing.T, c1, c2 *Conn, addr netip.Addr) {
// Echo this message between two connections.
rs := &RouterSolicitation{}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// Read and bounce the message back to the second Conn.
m, _, _, err := c2.ReadFrom()
if err != nil {
panicf("failed to read from c2: %v", err)
}
if err := c2.WriteTo(m, nil, addr); err != nil {
panicf("failed to write from c2: %v", err)
}
}()
if err := c1.WriteTo(rs, nil, addr); err != nil {
t.Fatalf("failed to write from c1: %v", err)
}
m, _, _, err := c1.ReadFrom()
if err != nil {
t.Fatalf("failed to read from c1: %v", err)
}
wg.Wait()
if diff := cmp.Diff(rs, m); diff != "" {
t.Fatalf("unexpected message (-want +got):\n%s", diff)
}
}
func testConnFilterInvalid(t *testing.T, c1, c2 *Conn, addr netip.Addr) {
// Echo this message between two connections.
rs := &RouterSolicitation{}
var wg sync.WaitGroup
wg.Add(1)
sigC := make(chan struct{})
go func() {
defer wg.Done()
// Wait for the caller to send us a message, then send:
// - invalid message (filtered)
// - valid message
// And finally force a timeout to verify the ReadFrom error check.
m, _, _, err := c2.ReadFrom()
if err != nil {
panicf("failed to read from c2: %v", err)
}
if err := c2.writeRaw(bytes.Repeat([]byte{0xff}, 255), nil, addr); err != nil {
panicf("failed to write invalid from c2: %v", err)
}
// Write in lockstep and wait for the consumer to acknowledge the write.
if err := c2.WriteTo(m, nil, addr); err != nil {
panicf("failed to write valid from c2: %v", err)
}
<-sigC
if err := c1.SetReadDeadline(time.Unix(1, 0)); err != nil {
panicf("failed to interrupt c1: %v", err)
}
<-sigC
}()
if err := c1.WriteTo(rs, nil, addr); err != nil {
t.Fatalf("failed to write from c1: %v", err)
}
var m Message
for i := 0; i < 2; i++ {
// Acknowledge each write from the other Conn.
msg, _, _, err := c1.ReadFrom()
sigC <- struct{}{}
if err == nil {
m = msg
continue
}
switch i {
case 0:
t.Fatalf("failed to read from c1: %v", err)
case 1:
var nerr net.Error
if !errors.As(err, &nerr) {
t.Fatalf("error is not net.Error: %v", err)
}
if !nerr.Timeout() {
t.Fatal("error did not indicate a timeout")
}
default:
panic("too many loop iterations")
}
}
wg.Wait()
if diff := cmp.Diff(rs, m); diff != "" {
t.Fatalf("unexpected message (-want +got):\n%s", diff)
}
}
func TestSolicitedNodeMulticast(t *testing.T) {
tests := []struct {
name string
ip netip.Addr
snm netip.Addr
ok bool
}{
{
name: "bad, IPv4",
ip: netip.MustParseAddr("192.168.1.1"),
},
{
name: "ok, link-local",
ip: netip.MustParseAddr("fe80::1234:5678"),
snm: netip.MustParseAddr("ff02::1:ff34:5678"),
ok: true,
},
{
name: "ok, global",
ip: netip.MustParseAddr("2001:db8::dead:beef"),
snm: netip.MustParseAddr("ff02::1:ffad:beef"),
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
snm, err := SolicitedNodeMulticast(tt.ip)
if err != nil && tt.ok {
t.Fatalf("unexpected error: %v", err)
}
if err == nil && !tt.ok {
t.Fatal("expected an error, but none occurred")
}
if err != nil {
t.Logf("OK error: %v", err)
return
}
if diff := cmp.Diff(tt.snm, snm, cmp.Comparer(addrEqual)); diff != "" {
t.Fatalf("unexpected solicited-node multicast address (-want +got):\n%s", diff)
}
})
}
}
func addrEqual(x, y netip.Addr) bool { return x == y }
func prefixEqual(x, y netip.Prefix) bool { return x == y }