forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
retransmission_queue_test.go
185 lines (165 loc) · 7.03 KB
/
retransmission_queue_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
183
184
185
package quic
import (
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/wire"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Retransmission queue", func() {
var q *retransmissionQueue
BeforeEach(func() {
q = newRetransmissionQueue()
})
Context("Initial data", func() {
It("doesn't dequeue anything when it's empty", func() {
Expect(q.HasInitialData()).To(BeFalse())
Expect(q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)).To(BeNil())
})
It("queues and retrieves a control frame", func() {
f := &wire.MaxDataFrame{MaximumData: 0x42}
q.AddInitial(f)
Expect(q.HasInitialData()).To(BeTrue())
Expect(q.GetInitialFrame(f.Length(protocol.Version1)-1, protocol.Version1)).To(BeNil())
Expect(q.GetInitialFrame(f.Length(protocol.Version1), protocol.Version1)).To(Equal(f))
Expect(q.HasInitialData()).To(BeFalse())
})
It("queues and retrieves a CRYPTO frame", func() {
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddInitial(f)
Expect(q.HasInitialData()).To(BeTrue())
Expect(q.GetInitialFrame(f.Length(protocol.Version1), protocol.Version1)).To(Equal(f))
Expect(q.HasInitialData()).To(BeFalse())
})
It("returns split CRYPTO frames", func() {
f := &wire.CryptoFrame{
Offset: 100,
Data: []byte("foobar"),
}
q.AddInitial(f)
Expect(q.HasInitialData()).To(BeTrue())
f1 := q.GetInitialFrame(f.Length(protocol.Version1)-3, protocol.Version1)
Expect(f1).ToNot(BeNil())
Expect(f1).To(BeAssignableToTypeOf(&wire.CryptoFrame{}))
Expect(f1.(*wire.CryptoFrame).Data).To(Equal([]byte("foo")))
Expect(f1.(*wire.CryptoFrame).Offset).To(Equal(protocol.ByteCount(100)))
Expect(q.HasInitialData()).To(BeTrue())
f2 := q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)
Expect(f2).ToNot(BeNil())
Expect(f2).To(BeAssignableToTypeOf(&wire.CryptoFrame{}))
Expect(f2.(*wire.CryptoFrame).Data).To(Equal([]byte("bar")))
Expect(f2.(*wire.CryptoFrame).Offset).To(Equal(protocol.ByteCount(103)))
Expect(q.HasInitialData()).To(BeFalse())
})
It("returns other frames when a CRYPTO frame wouldn't fit", func() {
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddInitial(f)
q.AddInitial(&wire.PingFrame{})
f1 := q.GetInitialFrame(2, protocol.Version1) // too small for a CRYPTO frame
Expect(f1).ToNot(BeNil())
Expect(f1).To(BeAssignableToTypeOf(&wire.PingFrame{}))
Expect(q.HasInitialData()).To(BeTrue())
f2 := q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)
Expect(f2).To(Equal(f))
})
It("retrieves both a CRYPTO frame and a control frame", func() {
cf := &wire.MaxDataFrame{MaximumData: 0x42}
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddInitial(f)
q.AddInitial(cf)
Expect(q.HasInitialData()).To(BeTrue())
Expect(q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)).To(Equal(f))
Expect(q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)).To(Equal(cf))
Expect(q.HasInitialData()).To(BeFalse())
})
It("drops all Initial frames", func() {
q.AddInitial(&wire.CryptoFrame{Data: []byte("foobar")})
q.AddInitial(&wire.MaxDataFrame{MaximumData: 0x42})
q.DropPackets(protocol.EncryptionInitial)
Expect(q.HasInitialData()).To(BeFalse())
Expect(q.GetInitialFrame(protocol.MaxByteCount, protocol.Version1)).To(BeNil())
})
})
Context("Handshake data", func() {
It("doesn't dequeue anything when it's empty", func() {
Expect(q.HasHandshakeData()).To(BeFalse())
Expect(q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)).To(BeNil())
})
It("queues and retrieves a control frame", func() {
f := &wire.MaxDataFrame{MaximumData: 0x42}
q.AddHandshake(f)
Expect(q.HasHandshakeData()).To(BeTrue())
Expect(q.GetHandshakeFrame(f.Length(protocol.Version1)-1, protocol.Version1)).To(BeNil())
Expect(q.GetHandshakeFrame(f.Length(protocol.Version1), protocol.Version1)).To(Equal(f))
Expect(q.HasHandshakeData()).To(BeFalse())
})
It("queues and retrieves a CRYPTO frame", func() {
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddHandshake(f)
Expect(q.HasHandshakeData()).To(BeTrue())
Expect(q.GetHandshakeFrame(f.Length(protocol.Version1), protocol.Version1)).To(Equal(f))
Expect(q.HasHandshakeData()).To(BeFalse())
})
It("returns split CRYPTO frames", func() {
f := &wire.CryptoFrame{
Offset: 100,
Data: []byte("foobar"),
}
q.AddHandshake(f)
Expect(q.HasHandshakeData()).To(BeTrue())
f1 := q.GetHandshakeFrame(f.Length(protocol.Version1)-3, protocol.Version1)
Expect(f1).ToNot(BeNil())
Expect(f1).To(BeAssignableToTypeOf(&wire.CryptoFrame{}))
Expect(f1.(*wire.CryptoFrame).Data).To(Equal([]byte("foo")))
Expect(f1.(*wire.CryptoFrame).Offset).To(Equal(protocol.ByteCount(100)))
Expect(q.HasHandshakeData()).To(BeTrue())
f2 := q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)
Expect(f2).ToNot(BeNil())
Expect(f2).To(BeAssignableToTypeOf(&wire.CryptoFrame{}))
Expect(f2.(*wire.CryptoFrame).Data).To(Equal([]byte("bar")))
Expect(f2.(*wire.CryptoFrame).Offset).To(Equal(protocol.ByteCount(103)))
Expect(q.HasHandshakeData()).To(BeFalse())
})
It("returns other frames when a CRYPTO frame wouldn't fit", func() {
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddHandshake(f)
q.AddHandshake(&wire.PingFrame{})
f1 := q.GetHandshakeFrame(2, protocol.Version1) // too small for a CRYPTO frame
Expect(f1).ToNot(BeNil())
Expect(f1).To(BeAssignableToTypeOf(&wire.PingFrame{}))
Expect(q.HasHandshakeData()).To(BeTrue())
f2 := q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)
Expect(f2).To(Equal(f))
})
It("retrieves both a CRYPTO frame and a control frame", func() {
cf := &wire.MaxDataFrame{MaximumData: 0x42}
f := &wire.CryptoFrame{Data: []byte("foobar")}
q.AddHandshake(f)
q.AddHandshake(cf)
Expect(q.HasHandshakeData()).To(BeTrue())
Expect(q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)).To(Equal(f))
Expect(q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)).To(Equal(cf))
Expect(q.HasHandshakeData()).To(BeFalse())
})
It("drops all Handshake frames", func() {
q.AddHandshake(&wire.CryptoFrame{Data: []byte("foobar")})
q.AddHandshake(&wire.MaxDataFrame{MaximumData: 0x42})
q.DropPackets(protocol.EncryptionHandshake)
Expect(q.HasHandshakeData()).To(BeFalse())
Expect(q.GetHandshakeFrame(protocol.MaxByteCount, protocol.Version1)).To(BeNil())
})
})
Context("Application data", func() {
It("doesn't dequeue anything when it's empty", func() {
Expect(q.GetAppDataFrame(protocol.MaxByteCount, protocol.Version1)).To(BeNil())
})
It("queues and retrieves a control frame", func() {
f := &wire.MaxDataFrame{MaximumData: 0x42}
Expect(q.HasAppData()).To(BeFalse())
q.AddAppData(f)
Expect(q.HasAppData()).To(BeTrue())
Expect(q.GetAppDataFrame(f.Length(protocol.Version1)-1, protocol.Version1)).To(BeNil())
Expect(q.GetAppDataFrame(f.Length(protocol.Version1), protocol.Version1)).To(Equal(f))
Expect(q.HasAppData()).To(BeFalse())
})
})
})