File: crypto_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 (123 lines) | stat: -rw-r--r-- 3,829 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
package wire

import (
	"io"
	"testing"

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

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

func TestParseCryptoFrame(t *testing.T) {
	data := encodeVarInt(0xdecafbad)        // offset
	data = append(data, encodeVarInt(6)...) // length
	data = append(data, []byte("foobar")...)
	frame, l, err := parseCryptoFrame(data, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, protocol.ByteCount(0xdecafbad), frame.Offset)
	require.Equal(t, []byte("foobar"), frame.Data)
	require.Equal(t, len(data), l)
}

func TestParseCryptoFrameErrorsOnEOFs(t *testing.T) {
	data := encodeVarInt(0xdecafbad)        // offset
	data = append(data, encodeVarInt(6)...) // data length
	data = append(data, []byte("foobar")...)
	_, l, err := parseCryptoFrame(data, protocol.Version1)
	require.NoError(t, err)
	require.Equal(t, len(data), l)
	for i := range data {
		_, _, err := parseCryptoFrame(data[:i], protocol.Version1)
		require.Equal(t, io.EOF, err)
	}
}

func TestWriteCryptoFrame(t *testing.T) {
	f := &CryptoFrame{
		Offset: 0x123456,
		Data:   []byte("foobar"),
	}
	b, err := f.Append(nil, protocol.Version1)
	require.NoError(t, err)
	expected := []byte{byte(FrameTypeCrypto)}
	expected = append(expected, encodeVarInt(0x123456)...) // offset
	expected = append(expected, encodeVarInt(6)...)        // length
	expected = append(expected, []byte("foobar")...)
	require.Equal(t, expected, b)
	require.Equal(t, int(f.Length(protocol.Version1)), len(b))
}

func TestCryptoFrameMaxDataLength(t *testing.T) {
	const maxSize = 3000

	data := make([]byte, maxSize)
	f := &CryptoFrame{
		Offset: 0xdeadbeef,
	}
	var frameOneByteTooSmallCounter int
	for i := 1; i < maxSize; i++ {
		f.Data = nil
		maxDataLen := f.MaxDataLen(protocol.ByteCount(i))
		if maxDataLen == 0 { // 0 means that no valid CRYPTO frame can be written
			// check that writing a minimal size CRYPTO 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 STREAM frame of the desired size
		if len(b) == i-1 {
			frameOneByteTooSmallCounter++
			continue
		}
		require.Equal(t, i, len(b))
	}
	require.Equal(t, 1, frameOneByteTooSmallCounter)
}

func TestCryptoFrameSplitting(t *testing.T) {
	f := &CryptoFrame{
		Offset: 0x1337,
		Data:   []byte("foobar"),
	}
	hdrLen := f.Length(protocol.Version1) - 6
	new, needsSplit := f.MaybeSplitOffFrame(hdrLen+3, protocol.Version1)
	require.True(t, needsSplit)
	require.Equal(t, []byte("foo"), new.Data)
	require.Equal(t, protocol.ByteCount(0x1337), new.Offset)
	require.Equal(t, []byte("bar"), f.Data)
	require.Equal(t, protocol.ByteCount(0x1337+3), f.Offset)
}

func TestCryptoFrameNoSplitWhenEnoughSpace(t *testing.T) {
	f := &CryptoFrame{
		Offset: 0x1337,
		Data:   []byte("foobar"),
	}
	splitFrame, needsSplit := f.MaybeSplitOffFrame(f.Length(protocol.Version1), protocol.Version1)
	require.False(t, needsSplit)
	require.Nil(t, splitFrame)
}

func TestCryptoFrameNoSplitWhenSizeTooSmall(t *testing.T) {
	f := &CryptoFrame{
		Offset: 0x1337,
		Data:   []byte("foobar"),
	}
	length := f.Length(protocol.Version1) - 6
	for i := protocol.ByteCount(0); i <= length; i++ {
		splitFrame, needsSplit := f.MaybeSplitOffFrame(i, protocol.Version1)
		require.True(t, needsSplit)
		require.Nil(t, splitFrame)
	}
	splitFrame, needsSplit := f.MaybeSplitOffFrame(length+1, protocol.Version1)
	require.True(t, needsSplit)
	require.NotNil(t, splitFrame)
}