File: new_connection_id_frame_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (88 lines) | stat: -rw-r--r-- 4,292 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
package wire

import (
	"io"
	"testing"

	"github.com/quic-go/quic-go/internal/protocol"

	"github.com/stretchr/testify/require"
)

func TestParseNewConnectionIDFrame(t *testing.T) {
	data := encodeVarInt(0xdeadbeef)                              // sequence number
	data = append(data, encodeVarInt(0xcafe)...)                  // retire prior to
	data = append(data, 10)                                       // connection ID length
	data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // connection ID
	data = append(data, []byte("deadbeefdecafbad")...)            // stateless reset token
	frame, l, err := parseNewConnectionIDFrame(data, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, uint64(0xdeadbeef), frame.SequenceNumber)
	require.Equal(t, uint64(0xcafe), frame.RetirePriorTo)
	require.Equal(t, protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), frame.ConnectionID)
	require.Equal(t, "deadbeefdecafbad", string(frame.StatelessResetToken[:]))
	require.Equal(t, len(data), l)
}

func TestParseNewConnectionIDRetirePriorToLargerThanSequenceNumber(t *testing.T) {
	data := encodeVarInt(1000)                 // sequence number
	data = append(data, encodeVarInt(1001)...) // retire prior to
	data = append(data, 3)
	data = append(data, []byte{1, 2, 3}...)
	data = append(data, []byte("deadbeefdecafbad")...) // stateless reset token
	_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
	require.EqualError(t, err, "Retire Prior To value (1001) larger than Sequence Number (1000)")
}

func TestParseNewConnectionIDZeroLengthConnID(t *testing.T) {
	data := encodeVarInt(42)                 // sequence number
	data = append(data, encodeVarInt(12)...) // retire prior to
	data = append(data, 0)                   // connection ID length
	_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
	require.EqualError(t, err, "invalid zero-length connection ID")
}

func TestParseNewConnectionIDInvalidConnIDLength(t *testing.T) {
	data := encodeVarInt(0xdeadbeef)                                                                          // sequence number
	data = append(data, encodeVarInt(0xcafe)...)                                                              // retire prior to
	data = append(data, 21)                                                                                   // connection ID length
	data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}...) // connection ID
	data = append(data, []byte("deadbeefdecafbad")...)                                                        // stateless reset token
	_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
	require.Equal(t, protocol.ErrInvalidConnectionIDLen, err)
}

func TestParseNewConnectionIDErrorsOnEOFs(t *testing.T) {
	data := encodeVarInt(0xdeadbeef)                              // sequence number
	data = append(data, encodeVarInt(0xcafe1234)...)              // retire prior to
	data = append(data, 10)                                       // connection ID length
	data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // connection ID
	data = append(data, []byte("deadbeefdecafbad")...)            // stateless reset token
	_, l, err := parseNewConnectionIDFrame(data, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, len(data), l)
	for i := range data {
		_, _, err := parseNewConnectionIDFrame(data[:i], protocol.Version1)
		require.Equal(t, io.EOF, err)
	}
}

func TestWriteNewConnectionIDFrame(t *testing.T) {
	token := protocol.StatelessResetToken{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
	frame := &NewConnectionIDFrame{
		SequenceNumber:      0x1337,
		RetirePriorTo:       0x42,
		ConnectionID:        protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6}),
		StatelessResetToken: token,
	}
	b, err := frame.Append(nil, protocol.Version1)
	require.NoError(t, err)
	expected := []byte{byte(FrameTypeNewConnectionID)}
	expected = append(expected, encodeVarInt(0x1337)...)
	expected = append(expected, encodeVarInt(0x42)...)
	expected = append(expected, 6)
	expected = append(expected, []byte{1, 2, 3, 4, 5, 6}...)
	expected = append(expected, token[:]...)
	require.Equal(t, expected, b)
	require.Equal(t, int(frame.Length(protocol.Version1)), len(b))
}