-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
header_test.go
588 lines (544 loc) · 24.1 KB
/
header_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package wire
import (
"bytes"
"encoding/binary"
"io"
"github.com/lucas-clemente/quic-go/internal/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Header Parsing", func() {
Context("Parsing the Connection ID", func() {
It("parses the connection ID of a long header packet", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6},
Version: protocol.Version1,
},
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
connID, err := ParseConnectionID(buf.Bytes(), 8)
Expect(err).ToNot(HaveOccurred())
Expect(connID).To(Equal(protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}))
})
It("parses the connection ID of a short header packet", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
DestConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
},
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
buf.Write([]byte("foobar"))
connID, err := ParseConnectionID(buf.Bytes(), 4)
Expect(err).ToNot(HaveOccurred())
Expect(connID).To(Equal(protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}))
})
It("errors on EOF, for short header packets", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
},
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
data := buf.Bytes()[:buf.Len()-2] // cut the packet number
_, err := ParseConnectionID(data, 8)
Expect(err).ToNot(HaveOccurred())
for i := 0; i < len(data); i++ {
b := make([]byte, i)
copy(b, data[:i])
_, err := ParseConnectionID(b, 8)
Expect(err).To(MatchError(io.EOF))
}
})
It("errors on EOF, for long header packets", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 8, 9},
Version: protocol.Version1,
},
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
data := buf.Bytes()[:buf.Len()-2] // cut the packet number
_, err := ParseConnectionID(data, 8)
Expect(err).ToNot(HaveOccurred())
for i := 0; i < 1 /* first byte */ +4 /* version */ +1 /* conn ID lengths */ +6; /* dest conn ID */ i++ {
b := make([]byte, i)
copy(b, data[:i])
_, err := ParseConnectionID(b, 8)
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("identifying 0-RTT packets", func() {
It("recognizes 0-RTT packets, for QUIC v1", func() {
zeroRTTHeader := make([]byte, 5)
zeroRTTHeader[0] = 0x80 | 0b01<<4
binary.BigEndian.PutUint32(zeroRTTHeader[1:], uint32(protocol.Version1))
Expect(Is0RTTPacket(zeroRTTHeader)).To(BeTrue())
Expect(Is0RTTPacket(zeroRTTHeader[:4])).To(BeFalse()) // too short
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0], 1, 2, 3, 4})).To(BeFalse()) // unknown version
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0] | 0x80, 1, 2, 3, 4})).To(BeFalse()) // short header
Expect(Is0RTTPacket(append(zeroRTTHeader, []byte("foobar")...))).To(BeTrue())
})
It("recognizes 0-RTT packets, for QUIC v2", func() {
zeroRTTHeader := make([]byte, 5)
zeroRTTHeader[0] = 0x80 | 0b10<<4
binary.BigEndian.PutUint32(zeroRTTHeader[1:], uint32(protocol.Version2))
Expect(Is0RTTPacket(zeroRTTHeader)).To(BeTrue())
Expect(Is0RTTPacket(zeroRTTHeader[:4])).To(BeFalse()) // too short
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0], 1, 2, 3, 4})).To(BeFalse()) // unknown version
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0] | 0x80, 1, 2, 3, 4})).To(BeFalse()) // short header
Expect(Is0RTTPacket(append(zeroRTTHeader, []byte("foobar")...))).To(BeTrue())
})
})
Context("Identifying Version Negotiation Packets", func() {
It("identifies version negotiation packets", func() {
Expect(IsVersionNegotiationPacket([]byte{0x80 | 0x56, 0, 0, 0, 0})).To(BeTrue())
Expect(IsVersionNegotiationPacket([]byte{0x56, 0, 0, 0, 0})).To(BeFalse())
Expect(IsVersionNegotiationPacket([]byte{0x80, 1, 0, 0, 0})).To(BeFalse())
Expect(IsVersionNegotiationPacket([]byte{0x80, 0, 1, 0, 0})).To(BeFalse())
Expect(IsVersionNegotiationPacket([]byte{0x80, 0, 0, 1, 0})).To(BeFalse())
Expect(IsVersionNegotiationPacket([]byte{0x80, 0, 0, 0, 1})).To(BeFalse())
})
It("returns false on EOF", func() {
vnp := []byte{0x80, 0, 0, 0, 0}
for i := range vnp {
Expect(IsVersionNegotiationPacket(vnp[:i])).To(BeFalse())
}
})
})
Context("Long Headers", func() {
It("parses a Long Header", func() {
destConnID := protocol.ConnectionID{9, 8, 7, 6, 5, 4, 3, 2, 1}
srcConnID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}
data := []byte{0xc0 ^ 0x3}
data = appendVersion(data, protocol.Version1)
data = append(data, 0x9) // dest conn id length
data = append(data, destConnID...)
data = append(data, 0x4) // src conn id length
data = append(data, srcConnID...)
data = append(data, encodeVarInt(6)...) // token length
data = append(data, []byte("foobar")...) // token
data = append(data, encodeVarInt(10)...) // length
hdrLen := len(data)
data = append(data, []byte{0, 0, 0xbe, 0xef}...) // packet number
data = append(data, []byte("foobar")...)
Expect(IsVersionNegotiationPacket(data)).To(BeFalse())
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(pdata).To(Equal(data))
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(hdr.Length).To(Equal(protocol.ByteCount(10)))
Expect(hdr.Version).To(Equal(protocol.Version1))
Expect(rest).To(BeEmpty())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
Expect(b.Len()).To(Equal(6)) // foobar
Expect(hdr.ParsedLen()).To(BeEquivalentTo(hdrLen))
Expect(extHdr.ParsedLen()).To(Equal(hdr.ParsedLen() + 4))
})
It("errors if 0x40 is not set", func() {
data := []byte{
0x80 | 0x2<<4,
0x11, // connection ID lengths
0xde, 0xca, 0xfb, 0xad, // dest conn ID
0xde, 0xad, 0xbe, 0xef, // src conn ID
}
_, _, _, err := ParsePacket(data, 0)
Expect(err).To(MatchError("not a QUIC packet"))
})
It("stops parsing when encountering an unsupported version", func() {
data := []byte{
0xc0,
0xde, 0xad, 0xbe, 0xef,
0x8, // dest conn ID len
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, // dest conn ID
0x8, // src conn ID len
0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, // src conn ID
'f', 'o', 'o', 'b', 'a', 'r', // unspecified bytes
}
hdr, _, rest, err := ParsePacket(data, 0)
Expect(err).To(MatchError(ErrUnsupportedVersion))
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.Version).To(Equal(protocol.VersionNumber(0xdeadbeef)))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1}))
Expect(rest).To(BeEmpty())
})
It("parses a Long Header without a destination connection ID", func() {
data := []byte{0xc0 ^ 0x1<<4}
data = appendVersion(data, protocol.Version1)
data = append(data, 0x0) // dest conn ID len
data = append(data, 0x4) // src conn ID len
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // source connection ID
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketType0RTT))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}))
Expect(hdr.DestConnectionID).To(BeEmpty())
})
It("parses a Long Header without a source connection ID", func() {
data := []byte{0xc0 ^ 0x2<<4}
data = appendVersion(data, protocol.Version1)
data = append(data, 0xa) // dest conn ID len
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // dest connection ID
data = append(data, 0x0) // src conn ID len
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.SrcConnectionID).To(BeEmpty())
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
})
It("parses a Long Header with a 2 byte packet number", func() {
data := []byte{0xc0 ^ 0x1}
data = appendVersion(data, protocol.Version1) // version number
data = append(data, []byte{0x0, 0x0}...) // connection ID lengths
data = append(data, encodeVarInt(0)...) // token length
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0x1, 0x23}...)
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x123)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
Expect(b.Len()).To(BeZero())
})
It("parses a Retry packet, for QUIC v1", func() {
data := []byte{0xc0 | 0b11<<4 | (10 - 3) /* connection ID length */}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{6}...) // dest conn ID len
data = append(data, []byte{6, 5, 4, 3, 2, 1}...) // dest conn ID
data = append(data, []byte{10}...) // src conn ID len
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}...)
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version1))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{6, 5, 4, 3, 2, 1}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("parses a Retry packet, for QUIC v2", func() {
data := []byte{0xc0 | 0b00<<4 | (10 - 3) /* connection ID length */}
data = appendVersion(data, protocol.Version2)
data = append(data, []byte{6}...) // dest conn ID len
data = append(data, []byte{6, 5, 4, 3, 2, 1}...) // dest conn ID
data = append(data, []byte{10}...) // src conn ID len
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}...)
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version2))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{6, 5, 4, 3, 2, 1}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("errors if the Retry packet is too short for the integrity tag", func() {
data := []byte{0xc0 | 0x3<<4 | (10 - 3) /* connection ID length */}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{0, 0}...) // conn ID lens
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...)
// this results in a token length of 0
_, _, _, err := ParsePacket(data, 0)
Expect(err).To(MatchError(io.EOF))
})
It("errors if the token length is too large", func() {
data := []byte{0xc0 ^ 0x1}
data = appendVersion(data, protocol.Version1)
data = append(data, 0x0) // connection ID lengths
data = append(data, encodeVarInt(4)...) // token length: 4 bytes (1 byte too long)
data = append(data, encodeVarInt(0x42)...) // length, 1 byte
data = append(data, []byte{0x12, 0x34}...) // packet number
_, _, _, err := ParsePacket(data, 0)
Expect(err).To(MatchError(io.EOF))
})
It("errors if the 5th or 6th bit are set", func() {
data := []byte{0xc0 | 0x2<<4 | 0x8 /* set the 5th bit */ | 0x1 /* 2 byte packet number */}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{0x0, 0x0}...) // connection ID lengths
data = append(data, encodeVarInt(2)...) // length
data = append(data, []byte{0x12, 0x34}...) // packet number
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeHandshake))
extHdr, err := hdr.ParseExtended(bytes.NewReader(data), protocol.Version1)
Expect(err).To(MatchError(ErrInvalidReservedBits))
Expect(extHdr).ToNot(BeNil())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x1234)))
})
It("errors on EOF, when parsing the header", func() {
data := []byte{0xc0 ^ 0x2<<4}
data = appendVersion(data, protocol.Version1)
data = append(data, 0x8) // dest conn ID len
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // dest conn ID
data = append(data, 0x8) // src conn ID len
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // src conn ID
for i := 0; i < len(data); i++ {
_, _, _, err := ParsePacket(data[:i], 0)
Expect(err).To(Equal(io.EOF))
}
})
It("errors on EOF, when parsing the extended header", func() {
data := []byte{0xc0 | 0x2<<4 | 0x3}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{0x0, 0x0}...) // connection ID lengths
data = append(data, encodeVarInt(0)...) // length
hdrLen := len(data)
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // packet number
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, protocol.Version1)
Expect(err).To(Equal(io.EOF))
}
})
It("errors on EOF, for a Retry packet", func() {
data := []byte{0xc0 ^ 0x3<<4}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{0x0, 0x0}...) // connection ID lengths
data = append(data, 0xa) // Orig Destination Connection ID length
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
hdrLen := len(data)
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, _, _, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, protocol.Version1)
Expect(err).To(Equal(io.EOF))
}
})
Context("coalesced packets", func() {
It("cuts packets", func() {
buf := &bytes.Buffer{}
hdr := Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 2 + 6,
Version: protocol.Version1,
}
Expect((&ExtendedHeader{
Header: hdr,
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
hdrRaw := append([]byte{}, buf.Bytes()...)
buf.Write([]byte("foobar")) // payload of the first packet
buf.Write([]byte("raboof")) // second packet
parsedHdr, data, rest, err := ParsePacket(buf.Bytes(), 4)
Expect(err).ToNot(HaveOccurred())
Expect(parsedHdr.Type).To(Equal(hdr.Type))
Expect(parsedHdr.DestConnectionID).To(Equal(hdr.DestConnectionID))
Expect(data).To(Equal(append(hdrRaw, []byte("foobar")...)))
Expect(rest).To(Equal([]byte("raboof")))
})
It("errors on packets that are smaller than the length in the packet header, for too small packet number", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 3,
Version: protocol.Version1,
},
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
_, _, _, err := ParsePacket(buf.Bytes(), 4)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("packet length (2 bytes) is smaller than the expected length (3 bytes)"))
})
It("errors on packets that are smaller than the length in the packet header, for too small payload", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Length: 1000,
Version: protocol.Version1,
},
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
buf.Write(make([]byte, 500-2 /* for packet number length */))
_, _, _, err := ParsePacket(buf.Bytes(), 4)
Expect(err).To(MatchError("packet length (500 bytes) is smaller than the expected length (1000 bytes)"))
})
})
})
Context("Short Headers", func() {
It("reads a Short Header with a 8 byte connection ID", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
data := append([]byte{0x40}, connID...)
data = append(data, 0x42) // packet number
Expect(IsVersionNegotiationPacket(data)).To(BeFalse())
hdr, pdata, rest, err := ParsePacket(data, 8)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
Expect(hdr.DestConnectionID).To(Equal(connID))
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.KeyPhase).To(Equal(protocol.KeyPhaseZero))
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
Expect(hdr.ParsedLen()).To(BeEquivalentTo(len(data) - 1))
Expect(extHdr.ParsedLen()).To(Equal(hdr.ParsedLen() + 1))
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("errors if 0x40 is not set", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
data := append([]byte{0x0}, connID...)
_, _, _, err := ParsePacket(data, 8)
Expect(err).To(MatchError("not a QUIC packet"))
})
It("errors if the 4th or 5th bit are set", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
data := append([]byte{0x40 | 0x10 /* set the 4th bit */}, connID...)
data = append(data, 0x42) // packet number
hdr, _, _, err := ParsePacket(data, 5)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
extHdr, err := hdr.ParseExtended(bytes.NewReader(data), protocol.Version1)
Expect(err).To(MatchError(ErrInvalidReservedBits))
Expect(extHdr).ToNot(BeNil())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
})
It("reads a Short Header with a 5 byte connection ID", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5}
data := append([]byte{0x40}, connID...)
data = append(data, 0x42) // packet number
hdr, pdata, rest, err := ParsePacket(data, 5)
Expect(err).ToNot(HaveOccurred())
Expect(pdata).To(HaveLen(len(data)))
Expect(hdr.IsLongHeader).To(BeFalse())
Expect(hdr.DestConnectionID).To(Equal(connID))
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.KeyPhase).To(Equal(protocol.KeyPhaseZero))
Expect(extHdr.DestConnectionID).To(Equal(connID))
Expect(extHdr.SrcConnectionID).To(BeEmpty())
Expect(rest).To(BeEmpty())
})
It("reads the Key Phase Bit", func() {
data := []byte{
0x40 ^ 0x4,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
}
data = append(data, 11) // packet number
hdr, _, _, err := ParsePacket(data, 6)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.KeyPhase).To(Equal(protocol.KeyPhaseOne))
Expect(b.Len()).To(BeZero())
})
It("reads a header with a 2 byte packet number", func() {
data := []byte{
0x40 | 0x1,
0xde, 0xad, 0xbe, 0xef, // connection ID
}
data = append(data, []byte{0x13, 0x37}...) // packet number
hdr, _, _, err := ParsePacket(data, 4)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.IsLongHeader).To(BeFalse())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x1337)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
Expect(b.Len()).To(BeZero())
})
It("reads a header with a 3 byte packet number", func() {
data := []byte{
0x40 | 0x2,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x1, 0x2, 0x3, 0x4, // connection ID
}
data = append(data, []byte{0x99, 0xbe, 0xef}...) // packet number
hdr, _, _, err := ParsePacket(data, 10)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
Expect(extHdr.IsLongHeader).To(BeFalse())
Expect(extHdr.PacketNumber).To(Equal(protocol.PacketNumber(0x99beef)))
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen3))
Expect(b.Len()).To(BeZero())
})
It("errors on EOF, when parsing the header", func() {
data := []byte{
0x40 ^ 0x2,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // connection ID
}
for i := 0; i < len(data); i++ {
data = data[:i]
_, _, _, err := ParsePacket(data, 8)
Expect(err).To(Equal(io.EOF))
}
})
It("errors on EOF, when parsing the extended header", func() {
data := []byte{
0x40 ^ 0x3,
0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
}
hdrLen := len(data)
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // packet number
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, _, _, err := ParsePacket(data, 6)
Expect(err).ToNot(HaveOccurred())
_, err = hdr.ParseExtended(bytes.NewReader(data), protocol.Version1)
Expect(err).To(Equal(io.EOF))
}
})
})
It("distinguishes long and short header packets", func() {
Expect(IsLongHeaderPacket(0x40)).To(BeFalse())
Expect(IsLongHeaderPacket(0x80 ^ 0x40 ^ 0x12)).To(BeTrue())
})
It("tells its packet type for logging", func() {
Expect((&Header{IsLongHeader: true, Type: protocol.PacketTypeHandshake}).PacketType()).To(Equal("Handshake"))
Expect((&Header{}).PacketType()).To(Equal("1-RTT"))
})
})