File: transport_test.go

package info (click to toggle)
golang-github-katalix-go-l2tp 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ansic: 127; sh: 10; makefile: 4
file content (402 lines) | stat: -rw-r--r-- 10,570 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package l2tp

import (
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/go-kit/kit/log"
	"github.com/go-kit/kit/log/level"
	"golang.org/x/sys/unix"
)

func TestNilControlPlane(t *testing.T) {
	xport, err := newTransport(log.NewLogfmtLogger(os.Stderr), nil, defaulttransportConfig())
	if xport != nil {
		t.Fatalf("newTransport() with nil controlplane succeeded")
	} else if err == nil {
		t.Fatalf("newTransport() with nil controlplane didn't report error")
	}
}

func TestSeqNumIncrement(t *testing.T) {
	cases := []struct {
		in, want uint16
	}{
		{uint16(0), uint16(1)},
		{uint16(65534), uint16(65535)},
		{uint16(65535), uint16(0)},
	}
	for _, c := range cases {
		got := seqIncrement(c.in)
		if got != c.want {
			t.Errorf("seqIncrement(%d) = %d, want %d", c.in, got, c.want)
		}
	}
}

func TestSeqNumCompare(t *testing.T) {
	cases := []struct {
		seq1, seq2 uint16
		want       int
	}{
		{uint16(15), uint16(15), 0},
		{uint16(15), uint16(0), 1},
		{uint16(15), uint16(65535), 1},
		{uint16(15), uint16(32784), 1},
		{uint16(15), uint16(16), -1},
		{uint16(15), uint16(15000), -1},
		{uint16(15), uint16(32783), -1},
	}
	for _, c := range cases {
		got := seqCompare(c.seq1, c.seq2)
		if got != c.want {
			t.Errorf("seqCompare(%d, %d) = %d, want %d", c.seq1, c.seq2, got, c.want)
		}
	}
}

func checkWindowOpen(ss *slowStartState, t *testing.T) {
	if !ss.canSend() {
		t.Fatalf("transport window is closed when we expect it to be open")
	}
}

func checkWindowClosed(ss *slowStartState, t *testing.T) {
	if ss.canSend() {
		t.Fatalf("transport window is open when we expect it to be closed")
	}
}

func checkCwndThresh(ss *slowStartState, cwnd, thresh uint16, t *testing.T) {
	if ss.cwnd != cwnd {
		t.Fatalf("transport window didn't correctly reset on retransmission: expected %d, got %d", cwnd, ss.cwnd)
	}
	if ss.thresh != thresh {
		t.Fatalf("transport threshold didn't correctly reset on retransmission: expected %d, got %d", thresh, ss.thresh)
	}
}

func TestSlowStart(t *testing.T) {
	txWindow := uint16(4)

	// initialise state and validate window is open
	ss := slowStartState{thresh: txWindow, cwnd: 1}
	checkWindowOpen(&ss, t)

	// send a packet, validate window is now closed
	ss.onSend()
	checkWindowClosed(&ss, t)

	// ack the packet: should now be able to send two packets before window closes
	ss.onAck(txWindow)
	for i := 0; i < 2; i++ {
		checkWindowOpen(&ss, t)
		ss.onSend()
	}
	checkWindowClosed(&ss, t)

	// ack the two packets in flight: should now be able to send four packets
	for i := 0; i < 2; i++ {
		ss.onAck(txWindow)
	}
	for i := 0; i < 4; i++ {
		checkWindowOpen(&ss, t)
		ss.onSend()
	}
	checkWindowClosed(&ss, t)

	// ack the four packets in flight, validate the state hasn't exceeded the max window
	for i := 0; i < 4; i++ {
		ss.onAck(txWindow)
		checkWindowOpen(&ss, t)
		if ss.cwnd > txWindow {
			t.Fatalf("transport window %d exceeded max %d", ss.cwnd, txWindow)
		}
	}

	// retransmit: validate threshold is reduced and cwnd is reset
	checkWindowOpen(&ss, t)
	ss.onSend()
	ss.onRetransmit()
	checkWindowClosed(&ss, t)
	checkCwndThresh(&ss, 1, 2, t)

	// ack the retransmit, validate we're in slow-start still
	ss.onAck(txWindow)
	checkWindowOpen(&ss, t)
	checkCwndThresh(&ss, 2, 2, t)

	// send packets, recv acks, validate congestion avoidance is applied
	checkWindowOpen(&ss, t)
	ss.onSend()
	ss.onAck(txWindow)
	checkCwndThresh(&ss, 2, 2, t)
	for i := 0; i < 3; i++ {
		checkWindowOpen(&ss, t)
		ss.onSend()
		ss.onAck(txWindow)
		checkCwndThresh(&ss, 3, 2, t)
	}
	checkWindowOpen(&ss, t)
	ss.onSend()
	ss.onAck(txWindow)
	checkCwndThresh(&ss, 4, 2, t)

	// lots more transmission, validate we don't exceed max tx window in congestion avoidance
	for i := 0; i < 100; i++ {
		checkWindowOpen(&ss, t)
		ss.onSend()
		ss.onAck(txWindow)
		checkCwndThresh(&ss, 4, 2, t)
	}
}

type transportSendRecvTestInfo struct {
	local, peer      string
	tid              ControlConnID
	encap            EncapType
	xcfg             transportConfig
	sender, receiver func(xport *transport) error
}

func flipTestInfo(info *transportSendRecvTestInfo) *transportSendRecvTestInfo {
	flipped := *info

	tmp1 := flipped.local
	flipped.local = flipped.peer
	flipped.peer = tmp1

	tmp2 := flipped.tid
	flipped.tid = flipped.xcfg.PeerControlConnID
	flipped.xcfg.PeerControlConnID = tmp2

	return &flipped
}

func transportTestnewTransport(testCfg *transportSendRecvTestInfo) (xport *transport, err error) {

	var sal, sap unix.Sockaddr
	var cp *controlPlane

	switch testCfg.encap {
	case EncapTypeUDP:
		sal, sap, err = newUDPAddressPair(testCfg.local, testCfg.peer)
	case EncapTypeIP:
		sal, sap, err = newIPAddressPair(testCfg.local, testCfg.tid, testCfg.peer, testCfg.xcfg.PeerControlConnID)
	default:
		err = fmt.Errorf("unhandled encap type %v", testCfg.encap)
	}
	if err != nil {
		return nil, fmt.Errorf("failed to init tunnel address structures: %v", err)
	}

	cp, err = newL2tpControlPlane(sal, sap)
	if err != nil {
		return nil, fmt.Errorf("failed to create control plane: %v", err)
	}

	err = cp.bind()
	if err != nil {
		return nil, fmt.Errorf("failed to bind control plane socket: %v", err)
	}

	err = cp.connect()
	if err != nil {
		return nil, fmt.Errorf("failed to connect control plane socket: %v", err)
	}

	return newTransport(
		level.NewFilter(log.NewLogfmtLogger(os.Stderr),
			level.AllowDebug(), level.AllowInfo()),
		cp, testCfg.xcfg)
}

func testBasicSendRecvSenderNewHelloMsg(cfg *transportConfig) (msg controlMessage, err error) {
	if cfg.Version == ProtocolVersion2 {
		msg, err = newV2ControlMessage(cfg.PeerControlConnID, 0, []avp{})
		if err != nil {
			return nil, err
		}
	} else if cfg.Version == ProtocolVersion3 {
		msg, err = newV3ControlMessage(cfg.PeerControlConnID, []avp{})
		if err != nil {
			return nil, err
		}
	} else {
		return nil, fmt.Errorf("testBasicSendRecvSenderNewMsg: unhandled protocol version")
	}

	avp, err := newAvp(vendorIDIetf, avpTypeMessage, avpMsgTypeHello)
	if err != nil {
		return nil, err
	}
	msg.appendAvp(avp)

	return msg, nil
}

func testBasicSendRecvHelloSender(xport *transport) error {
	// Send sufficient HELLO messages to exercise slowstart a bit
	for i := uint16(0); i < 3*xport.getConfig().TxWindowSize; i++ {
		cfg := xport.getConfig()
		msg, err := testBasicSendRecvSenderNewHelloMsg(&cfg)
		if err != nil {
			return fmt.Errorf("failed to build Hello message: %v", err)
		}
		err = xport.send(msg)
		if err != nil {
			return fmt.Errorf("failed to send Hello message: %v", err)
		}
	}
	return nil
}

func testBasicSendRecvHelloReceiver(xport *transport) error {
	for i := uint16(0); i < 3*xport.getConfig().TxWindowSize; i++ {
		msg, _, err := xport.recv()
		if err != nil {
			return fmt.Errorf("failed to receive message: %v", err)
		}
		if msg.getType() != avpMsgTypeHello {
			return fmt.Errorf("expected message %v, got %v", avpMsgTypeHello, msg.getType())
		}
	}
	return nil
}

func TestBasicSendReceive(t *testing.T) {
	cases := []transportSendRecvTestInfo{
		{
			local: "127.0.0.1:9000",
			tid:   42,
			peer:  "127.0.0.1:9001",
			encap: EncapTypeUDP,
			xcfg: transportConfig{
				Version:           ProtocolVersion2,
				AckTimeout:        5 * time.Millisecond,
				PeerControlConnID: 90,
			},
			sender:   testBasicSendRecvHelloSender,
			receiver: testBasicSendRecvHelloReceiver,
		},
		{
			local: "[::1]:9000",
			tid:   42,
			peer:  "[::1]:9001",
			encap: EncapTypeUDP,
			xcfg: transportConfig{
				Version:           ProtocolVersion2,
				AckTimeout:        5 * time.Millisecond,
				PeerControlConnID: 90,
			},
			sender:   testBasicSendRecvHelloSender,
			receiver: testBasicSendRecvHelloReceiver,
		},
		{
			local: "127.0.0.1:9000",
			tid:   42,
			peer:  "127.0.0.1:9001",
			encap: EncapTypeUDP,
			xcfg: transportConfig{
				Version:           ProtocolVersion3,
				AckTimeout:        5 * time.Millisecond,
				PeerControlConnID: 90,
			},
			sender:   testBasicSendRecvHelloSender,
			receiver: testBasicSendRecvHelloReceiver,
		},
		{
			local: "[::1]:9000",
			tid:   42,
			peer:  "[::1]:9001",
			encap: EncapTypeUDP,
			xcfg: transportConfig{
				Version:           ProtocolVersion3,
				AckTimeout:        5 * time.Millisecond,
				PeerControlConnID: 90,
			},
			sender:   testBasicSendRecvHelloSender,
			receiver: testBasicSendRecvHelloReceiver,
		},
		{
			local: "127.0.0.1:9000",
			tid:   42,
			peer:  "127.0.0.1:9001",
			encap: EncapTypeIP,
			xcfg: transportConfig{
				Version:           ProtocolVersion3,
				AckTimeout:        5 * time.Millisecond,
				PeerControlConnID: 90,
			},
			sender:   testBasicSendRecvHelloSender,
			receiver: testBasicSendRecvHelloReceiver,
		},
		// FIXME: upstream kernel commit 9d4c75800f61 unfortunately breaks L2TPIP6.
		// It would be nice to figure out a way to detect whether the running kernel
		// has this change and skip this test accordingly, but in the meantime just
		// avoid it across the board.
		/*
			{
				local: "[::1]:9000",
				tid:   42,
				peer:  "[::1]:9001",
				encap: EncapTypeIP,
				xcfg: transportConfig{
					Version:           ProtocolVersion3,
					AckTimeout:        5 * time.Millisecond,
					PeerControlConnID: 90,
				},
				sender:   testBasicSendRecvHelloSender,
				receiver: testBasicSendRecvHelloReceiver,
			},
		*/
	}
	for i, c := range cases {
		t.Run(
			fmt.Sprintf("%d: send/recv %s %s L2TPv%v %s", i, c.local, c.peer, c.xcfg.Version, c.encap),
			func(t *testing.T) {
				tx, err := transportTestnewTransport(&c)
				if err != nil {
					// On systems without l2tp_[ip6] modules loaded we will
					// see EncapTypeIP failing to create the control plane
					// socket with EPROTONOSUPPORT.  Skip the test in that
					// scenario.
					if c.encap == EncapTypeIP && strings.Contains(err.Error(), "protocol not supported") {
						t.Skip("System does not support L2TP IP encapsulation")
					}
					t.Fatalf("transportTestnewTransport(%v) said: %v", c, err)
				}
				defer tx.close()

				pcfg := flipTestInfo(&c)
				rx, err := transportTestnewTransport(pcfg)
				if err != nil {
					t.Fatalf("transportTestnewTransport(%v) said: %v", pcfg, err)
				}
				defer rx.close()

				txCompletion := make(chan error)
				rxCompletion := make(chan error)

				go func() {
					txCompletion <- c.sender(tx)
				}()

				go func() {
					rxCompletion <- c.receiver(rx)
				}()

				err = <-txCompletion
				if err != nil {
					t.Errorf("test sender function reported an error: %v", err)
				}
				err = <-rxCompletion
				if err != nil {
					t.Errorf("test receiver function reported an error: %v", err)
				}
			})
	}
}