forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_stream_test.go
182 lines (163 loc) · 5.38 KB
/
crypto_stream_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
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
package quic
import (
"crypto/rand"
"fmt"
"github.com/boisjacques/qed/internal/protocol"
"github.com/boisjacques/qed/internal/wire"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func createHandshakeMessage(len int) []byte {
msg := make([]byte, 4+len)
rand.Read(msg[:1]) // random message type
msg[1] = uint8(len >> 16)
msg[2] = uint8(len >> 8)
msg[3] = uint8(len)
rand.Read(msg[4:])
return msg
}
var _ = Describe("Crypto Stream", func() {
var (
str cryptoStream
)
BeforeEach(func() {
str = newCryptoStream()
})
Context("handling incoming data", func() {
It("handles in-order CRYPTO frames", func() {
msg := createHandshakeMessage(6)
err := str.HandleCryptoFrame(&wire.CryptoFrame{Data: msg})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(Equal(msg))
Expect(str.GetCryptoData()).To(BeNil())
})
It("handles multiple messages in one CRYPTO frame", func() {
msg1 := createHandshakeMessage(6)
msg2 := createHandshakeMessage(10)
msg := append(append([]byte{}, msg1...), msg2...)
err := str.HandleCryptoFrame(&wire.CryptoFrame{Data: msg})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(Equal(msg1))
Expect(str.GetCryptoData()).To(Equal(msg2))
Expect(str.GetCryptoData()).To(BeNil())
})
It("errors if the frame exceeds the maximum offset", func() {
err := str.HandleCryptoFrame(&wire.CryptoFrame{
Offset: protocol.MaxCryptoStreamOffset - 5,
Data: []byte("foobar"),
})
Expect(err).To(MatchError(fmt.Sprintf("received invalid offset %d on crypto stream, maximum allowed %d", protocol.MaxCryptoStreamOffset+1, protocol.MaxCryptoStreamOffset)))
})
It("handles messages split over multiple CRYPTO frames", func() {
msg := createHandshakeMessage(6)
err := str.HandleCryptoFrame(&wire.CryptoFrame{
Data: msg[:4],
})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(BeNil())
err = str.HandleCryptoFrame(&wire.CryptoFrame{
Offset: 4,
Data: msg[4:],
})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(Equal(msg))
Expect(str.GetCryptoData()).To(BeNil())
})
It("handles out-of-order CRYPTO frames", func() {
msg := createHandshakeMessage(6)
err := str.HandleCryptoFrame(&wire.CryptoFrame{
Offset: 4,
Data: msg[4:],
})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(BeNil())
err = str.HandleCryptoFrame(&wire.CryptoFrame{
Data: msg[:4],
})
Expect(err).ToNot(HaveOccurred())
Expect(str.GetCryptoData()).To(Equal(msg))
Expect(str.GetCryptoData()).To(BeNil())
})
Context("finishing", func() {
It("errors if there's still data to read after finishing", func() {
Expect(str.HandleCryptoFrame(&wire.CryptoFrame{
Data: createHandshakeMessage(5),
Offset: 10,
})).To(Succeed())
err := str.Finish()
Expect(err).To(MatchError("encryption level changed, but crypto stream has more data to read"))
})
It("works with reordered data", func() {
f1 := &wire.CryptoFrame{
Data: []byte("foo"),
}
f2 := &wire.CryptoFrame{
Offset: 3,
Data: []byte("bar"),
}
Expect(str.HandleCryptoFrame(f2)).To(Succeed())
Expect(str.HandleCryptoFrame(f1)).To(Succeed())
Expect(str.Finish()).To(Succeed())
Expect(str.HandleCryptoFrame(f2)).To(Succeed())
})
It("rejects new crypto data after finishing", func() {
Expect(str.Finish()).To(Succeed())
err := str.HandleCryptoFrame(&wire.CryptoFrame{
Data: createHandshakeMessage(5),
})
Expect(err).To(MatchError("received crypto data after change of encryption level"))
})
It("ignores crypto data below the maximum offset received before finishing", func() {
msg := createHandshakeMessage(15)
Expect(str.HandleCryptoFrame(&wire.CryptoFrame{
Data: msg,
})).To(Succeed())
Expect(str.GetCryptoData()).To(Equal(msg))
Expect(str.Finish()).To(Succeed())
Expect(str.HandleCryptoFrame(&wire.CryptoFrame{
Offset: protocol.ByteCount(len(msg) - 6),
Data: []byte("foobar"),
})).To(Succeed())
})
})
})
Context("writing data", func() {
It("says if it has data", func() {
Expect(str.HasData()).To(BeFalse())
_, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
Expect(str.HasData()).To(BeTrue())
})
It("pops crypto frames", func() {
_, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
f := str.PopCryptoFrame(1000)
Expect(f).ToNot(BeNil())
Expect(f.Offset).To(BeZero())
Expect(f.Data).To(Equal([]byte("foobar")))
})
It("coalesces multiple writes", func() {
_, err := str.Write([]byte("foo"))
Expect(err).ToNot(HaveOccurred())
_, err = str.Write([]byte("bar"))
Expect(err).ToNot(HaveOccurred())
f := str.PopCryptoFrame(1000)
Expect(f).ToNot(BeNil())
Expect(f.Offset).To(BeZero())
Expect(f.Data).To(Equal([]byte("foobar")))
})
It("respects the maximum size", func() {
frameHeaderLen := (&wire.CryptoFrame{}).Length(protocol.VersionWhatever)
_, err := str.Write([]byte("foobar"))
Expect(err).ToNot(HaveOccurred())
f := str.PopCryptoFrame(frameHeaderLen + 3)
Expect(f).ToNot(BeNil())
Expect(f.Offset).To(BeZero())
Expect(f.Data).To(Equal([]byte("foo")))
f = str.PopCryptoFrame(frameHeaderLen + 3)
Expect(f).ToNot(BeNil())
Expect(f.Offset).To(Equal(protocol.ByteCount(3)))
Expect(f.Data).To(Equal([]byte("bar")))
})
})
})