File: datagram_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 (126 lines) | stat: -rw-r--r-- 4,100 bytes parent folder | download | duplicates (2)
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
package wire

import (
	"io"
	"testing"

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

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

func TestParseDatagramFrameWithLength(t *testing.T) {
	data := encodeVarInt(0x6) // length
	data = append(data, []byte("foobar")...)
	frame, l, err := parseDatagramFrame(data, 0x30^0x1, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, []byte("foobar"), frame.Data)
	require.True(t, frame.DataLenPresent)
	require.Equal(t, len(data), l)
}

func TestParseDatagramFrameWithoutLength(t *testing.T) {
	data := []byte("Lorem ipsum dolor sit amet")
	frame, l, err := parseDatagramFrame(data, 0x30, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, []byte("Lorem ipsum dolor sit amet"), frame.Data)
	require.False(t, frame.DataLenPresent)
	require.Equal(t, len(data), l)
}

func TestParseDatagramFrameErrorsOnLengthLongerThanFrame(t *testing.T) {
	data := encodeVarInt(0x6) // length
	data = append(data, []byte("fooba")...)
	_, _, err := parseDatagramFrame(data, 0x30^0x1, protocol.Version1)
	require.Equal(t, io.EOF, err)
}

func TestParseDatagramFrameErrorsOnEOFs(t *testing.T) {
	const typ = 0x30 ^ 0x1
	data := encodeVarInt(6) // length
	data = append(data, []byte("foobar")...)
	_, l, err := parseDatagramFrame(data, typ, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, len(data), l)
	for i := range data {
		_, _, err = parseDatagramFrame(data[0:i], typ, protocol.Version1)
		require.Equal(t, io.EOF, err)
	}
}

func TestWriteDatagramFrameWithLength(t *testing.T) {
	f := &DatagramFrame{
		DataLenPresent: true,
		Data:           []byte("foobar"),
	}
	b, err := f.Append(nil, protocol.Version1)
	require.NoError(t, err)
	expected := []byte{0x30 ^ 0x1}
	expected = append(expected, encodeVarInt(0x6)...)
	expected = append(expected, []byte("foobar")...)
	require.Equal(t, expected, b)
	require.Len(t, b, int(f.Length(protocol.Version1)))
}

func TestWriteDatagramFrameWithoutLength(t *testing.T) {
	f := &DatagramFrame{Data: []byte("Lorem ipsum")}
	b, err := f.Append(nil, protocol.Version1)
	require.NoError(t, err)
	expected := []byte{0x30}
	expected = append(expected, []byte("Lorem ipsum")...)
	require.Equal(t, expected, b)
	require.Len(t, b, int(f.Length(protocol.Version1)))
}

func TestMaxDatagramLenWithoutDataLenPresent(t *testing.T) {
	const maxSize = 3000
	data := make([]byte, maxSize)
	f := &DatagramFrame{}
	for i := 1; i < 3000; i++ {
		f.Data = nil
		maxDataLen := f.MaxDataLen(protocol.ByteCount(i), protocol.Version1)
		if maxDataLen == 0 { // 0 means that no valid DATAGRAM frame can be written
			// check that writing a minimal size DATAGRAM frame (i.e. with 1 byte data) is actually larger than the desired size
			f.Data = []byte{0}
			b, err := f.Append(nil, protocol.Version1)
			require.NoError(t, err)
			require.Greater(t, len(b), i)
			continue
		}
		f.Data = data[:int(maxDataLen)]
		b, err := f.Append(nil, protocol.Version1)
		require.NoError(t, err)
		require.Len(t, b, i)
	}
}

func TestMaxDatagramLenWithDataLenPresent(t *testing.T) {
	const maxSize = 3000
	data := make([]byte, maxSize)
	f := &DatagramFrame{DataLenPresent: true}
	var frameOneByteTooSmallCounter int
	for i := 1; i < 3000; i++ {
		f.Data = nil
		maxDataLen := f.MaxDataLen(protocol.ByteCount(i), protocol.Version1)
		if maxDataLen == 0 { // 0 means that no valid DATAGRAM frame can be written
			// check that writing a minimal size DATAGRAM frame (i.e. with 1 byte data) is actually larger than the desired size
			f.Data = []byte{0}
			b, err := f.Append(nil, protocol.Version1)
			require.NoError(t, err)
			require.Greater(t, len(b), i)
			continue
		}
		f.Data = data[:int(maxDataLen)]
		b, err := f.Append(nil, protocol.Version1)
		require.NoError(t, err)
		// There's *one* pathological case, where a data length of x can be encoded into 1 byte
		// but a data lengths of x+1 needs 2 bytes
		// In that case, it's impossible to create a DATAGRAM frame of the desired size
		if len(b) == i-1 {
			frameOneByteTooSmallCounter++
			continue
		}
		require.Len(t, b, i)
	}
	require.Equal(t, 1, frameOneByteTooSmallCounter)
}