-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
datagram_frame_test.go
156 lines (142 loc) · 5.16 KB
/
datagram_frame_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
package wire
import (
"bytes"
"io"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/quicvarint"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("STREAM frame", func() {
Context("when parsing", func() {
It("parses a frame containing a length", func() {
data := []byte{0x30 ^ 0x1}
data = append(data, encodeVarInt(0x6)...) // length
data = append(data, []byte("foobar")...)
r := bytes.NewReader(data)
frame, err := parseDatagramFrame(r, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(frame.Data).To(Equal([]byte("foobar")))
Expect(frame.DataLenPresent).To(BeTrue())
Expect(r.Len()).To(BeZero())
})
It("parses a frame without length", func() {
data := []byte{0x30}
data = append(data, []byte("Lorem ipsum dolor sit amet")...)
r := bytes.NewReader(data)
frame, err := parseDatagramFrame(r, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(frame.Data).To(Equal([]byte("Lorem ipsum dolor sit amet")))
Expect(frame.DataLenPresent).To(BeFalse())
Expect(r.Len()).To(BeZero())
})
It("errors when the length is longer than the rest of the frame", func() {
data := []byte{0x30 ^ 0x1}
data = append(data, encodeVarInt(0x6)...) // length
data = append(data, []byte("fooba")...)
r := bytes.NewReader(data)
_, err := parseDatagramFrame(r, protocol.Version1)
Expect(err).To(MatchError(io.EOF))
})
It("errors on EOFs", func() {
data := []byte{0x30 ^ 0x1}
data = append(data, encodeVarInt(6)...) // length
data = append(data, []byte("foobar")...)
_, err := parseDatagramFrame(bytes.NewReader(data), protocol.Version1)
Expect(err).NotTo(HaveOccurred())
for i := range data {
_, err := parseDatagramFrame(bytes.NewReader(data[0:i]), protocol.Version1)
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("when writing", func() {
It("writes a frame with length", func() {
f := &DatagramFrame{
DataLenPresent: true,
Data: []byte("foobar"),
}
b, err := f.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
expected := []byte{0x30 ^ 0x1}
expected = append(expected, encodeVarInt(0x6)...)
expected = append(expected, []byte("foobar")...)
Expect(b).To(Equal(expected))
})
It("writes a frame without length", func() {
f := &DatagramFrame{Data: []byte("Lorem ipsum")}
b, err := f.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
expected := []byte{0x30}
expected = append(expected, []byte("Lorem ipsum")...)
Expect(b).To(Equal(expected))
})
})
Context("length", func() {
It("has the right length for a frame with length", func() {
f := &DatagramFrame{
DataLenPresent: true,
Data: []byte("foobar"),
}
Expect(f.Length(protocol.Version1)).To(Equal(1 + quicvarint.Len(6) + 6))
})
It("has the right length for a frame without length", func() {
f := &DatagramFrame{Data: []byte("foobar")}
Expect(f.Length(protocol.Version1)).To(Equal(protocol.ByteCount(1 + 6)))
})
})
Context("max data length", func() {
const maxSize = 3000
It("returns a data length such that the resulting frame has the right size, if data length is not present", func() {
data := make([]byte, maxSize)
f := &DatagramFrame{}
b := &bytes.Buffer{}
for i := 1; i < 3000; i++ {
b.Reset()
f.Data = nil
maxDataLen := f.MaxDataLen(protocol.ByteCount(i), protocol.Version1)
if maxDataLen == 0 { // 0 means that no valid STREAM frame can be written
// check that writing a minimal size STREAM 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)
Expect(err).ToNot(HaveOccurred())
Expect(len(b)).To(BeNumerically(">", i))
continue
}
f.Data = data[:int(maxDataLen)]
b, err := f.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(b).To(HaveLen(i))
}
})
It("always returns a data length such that the resulting frame has the right size, if data length is present", func() {
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 STREAM frame can be written
// check that writing a minimal size STREAM 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)
Expect(err).ToNot(HaveOccurred())
Expect(len(b)).To(BeNumerically(">", i))
continue
}
f.Data = data[:int(maxDataLen)]
b, err := f.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
// 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
}
Expect(b).To(HaveLen(i))
}
Expect(frameOneByteTooSmallCounter).To(Equal(1))
})
})
})