-
Notifications
You must be signed in to change notification settings - Fork 2
/
public_header_test.go
484 lines (440 loc) · 18.8 KB
/
public_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
package quic
import (
"bytes"
"encoding/binary"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Public Header", func() {
Context("when parsing", func() {
It("accepts a sample client header", func() {
b := bytes.NewReader([]byte{0x09, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0x51, 0x30, 0x33, 0x34, 0x01})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.VersionFlag).To(BeTrue())
Expect(hdr.ResetFlag).To(BeFalse())
Expect(hdr.ConnectionID).To(Equal(protocol.ConnectionID(0x4cfa9f9b668619f6)))
Expect(hdr.VersionNumber).To(Equal(protocol.VersionNumber(34)))
Expect(hdr.SupportedVersions).To(BeEmpty())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(1)))
Expect(b.Len()).To(BeZero())
})
It("does not accept truncated connection ID as a server", func() {
b := bytes.NewReader([]byte{0x00, 0x01})
_, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).To(MatchError(errReceivedTruncatedConnectionID))
})
It("accepts a truncated connection ID as a client", func() {
b := bytes.NewReader([]byte{0x00, 0x01})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.TruncateConnectionID).To(BeTrue())
Expect(hdr.ConnectionID).To(BeZero())
Expect(b.Len()).To(BeZero())
})
It("rejects 0 as a connection ID", func() {
b := bytes.NewReader([]byte{0x09, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x51, 0x30, 0x33, 0x30, 0x01})
_, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).To(MatchError(errInvalidConnectionID))
})
It("reads a PublicReset packet", func() {
b := bytes.NewReader([]byte{0xa, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.ResetFlag).To(BeTrue())
Expect(hdr.ConnectionID).ToNot(BeZero())
})
It("parses a public reset packet", func() {
b := bytes.NewReader([]byte{0xa, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.ResetFlag).To(BeTrue())
Expect(hdr.VersionFlag).To(BeFalse())
Expect(hdr.ConnectionID).To(Equal(protocol.ConnectionID(0x0807060504030201)))
})
It("reads a diversification nonce sent by the server", func() {
divNonce := []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}
Expect(divNonce).To(HaveLen(32))
b := bytes.NewReader(append(append([]byte{0x0c, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c}, divNonce...), 0x37))
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.ConnectionID).To(Not(BeZero()))
Expect(hdr.DiversificationNonce).To(Equal(divNonce))
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x37)))
Expect(b.Len()).To(BeZero())
})
PIt("rejects diversification nonces sent by the client", func() {
b := bytes.NewReader([]byte{0x0c, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
0x01,
})
_, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).To(MatchError("diversification nonces should only be sent by servers"))
})
Context("version negotiation packets", func() {
appendVersion := func(data []byte, v protocol.VersionNumber) []byte {
data = append(data, []byte{0, 0, 0, 0}...)
binary.LittleEndian.PutUint32(data[len(data)-4:], protocol.VersionNumberToTag(v))
return data
}
It("parses version negotiation packets sent by the server", func() {
b := bytes.NewReader(composeVersionNegotiation(0x1337, protocol.SupportedVersions))
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.VersionFlag).To(BeTrue())
Expect(hdr.VersionNumber).To(BeZero()) // unitialized
Expect(hdr.SupportedVersions).To(Equal(protocol.SupportedVersions))
Expect(b.Len()).To(BeZero())
})
It("parses a version negotiation packet that contains 0 versions", func() {
b := bytes.NewReader([]byte{0x9, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.VersionFlag).To(BeTrue())
Expect(hdr.VersionNumber).To(BeZero()) // unitialized
Expect(hdr.SupportedVersions).To(BeEmpty())
Expect(b.Len()).To(BeZero())
})
It("reads version negotiation packets containing unsupported versions", func() {
data := []byte{0x9, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c}
data = appendVersion(data, 1) // unsupported version
data = appendVersion(data, protocol.SupportedVersions[0])
data = appendVersion(data, 99) // unsupported version
b := bytes.NewReader(data)
hdr, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.VersionFlag).To(BeTrue())
Expect(hdr.SupportedVersions).To(Equal([]protocol.VersionNumber{1, protocol.SupportedVersions[0], 99}))
Expect(b.Len()).To(BeZero())
})
It("errors on invalid version tags", func() {
data := composeVersionNegotiation(0x1337, protocol.SupportedVersions)
data = append(data, []byte{0x13, 0x37}...)
b := bytes.NewReader(data)
_, err := ParsePublicHeader(b, protocol.PerspectiveServer)
Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket))
})
})
Context("Packet Number lengths", func() {
It("accepts 1-byte packet numbers", func() {
b := bytes.NewReader([]byte{0x08, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xde})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xde)))
Expect(b.Len()).To(BeZero())
})
It("accepts 2-byte packet numbers", func() {
b := bytes.NewReader([]byte{0x18, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xde, 0xca})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xcade)))
Expect(b.Len()).To(BeZero())
})
It("accepts 4-byte packet numbers", func() {
b := bytes.NewReader([]byte{0x28, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xad, 0xfb, 0xca, 0xde})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdecafbad)))
Expect(b.Len()).To(BeZero())
})
It("accepts 6-byte packet numbers", func() {
b := bytes.NewReader([]byte{0x38, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0x23, 0x42, 0xad, 0xfb, 0xca, 0xde})
hdr, err := ParsePublicHeader(b, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdecafbad4223)))
Expect(b.Len()).To(BeZero())
})
})
})
Context("when writing", func() {
It("writes a sample header as a server", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 2,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.Version35, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x38, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 2, 0, 0, 0, 0, 0}))
})
It("writes a sample header as a client", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0x1337,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.Version35, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x38, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0x37, 0x13, 0, 0, 0, 0}))
})
It("refuses to write a Public Header if the PacketNumberLen is not set", func() {
hdr := PublicHeader{
ConnectionID: 1,
PacketNumber: 2,
}
b := &bytes.Buffer{}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).To(MatchError(errPacketNumberLenNotSet))
})
It("truncates the connection ID", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
TruncateConnectionID: true,
PacketNumberLen: protocol.PacketNumberLen6,
PacketNumber: 1,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x30, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0}))
})
It("writes proper v33 packets", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 1,
PacketNumberLen: protocol.PacketNumberLen1,
}
err := hdr.Write(b, protocol.Version35, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x08, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0x01}))
})
It("writes diversification nonces", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 1,
PacketNumberLen: protocol.PacketNumberLen1,
DiversificationNonce: bytes.Repeat([]byte{1}, 32),
}
err := hdr.Write(b, protocol.Version35, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{
0x0c, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0x01,
}))
})
It("throws an error if both Reset Flag and Version Flag are set", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
VersionFlag: true,
ResetFlag: true,
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 2,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).To(MatchError(errResetAndVersionFlagSet))
})
Context("Version Negotiation packets", func() {
It("sets the Version Flag for packets sent as a server", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
VersionFlag: true,
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 2,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
// must be the first assertion
Expect(b.Len()).To(Equal(1 + 8)) // 1 FlagByte + 8 ConnectionID
firstByte, _ := b.ReadByte()
Expect(firstByte & 0x01).To(Equal(uint8(1)))
Expect(firstByte & 0x30).To(BeZero()) // no packet number present
})
It("sets the Version Flag for packets sent as a client, and adds a packet number", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
VersionFlag: true,
VersionNumber: protocol.Version36,
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0x1337,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
// must be the first assertion
Expect(b.Len()).To(Equal(1 + 8 + 4 + 6)) // 1 FlagByte + 8 ConnectionID + 4 version number + 6 PacketNumber
firstByte, _ := b.ReadByte()
Expect(firstByte & 0x01).To(Equal(uint8(1)))
Expect(firstByte & 0x30).To(Equal(uint8(0x30)))
Expect(string(b.Bytes()[8:12])).To(Equal("Q036"))
Expect(b.Bytes()[12:18]).To(Equal([]byte{0x37, 0x13, 0, 0, 0, 0}))
})
})
Context("PublicReset packets", func() {
It("sets the Reset Flag", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ResetFlag: true,
ConnectionID: 0x4cfa9f9b668619f6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
// must be the first assertion
Expect(b.Len()).To(Equal(1 + 8)) // 1 FlagByte + 8 ConnectionID
firstByte, _ := b.ReadByte()
Expect((firstByte & 0x02) >> 1).To(Equal(uint8(1)))
})
It("doesn't add a packet number for headers with Reset Flag sent as a client", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ResetFlag: true,
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 2,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
// must be the first assertion
Expect(b.Len()).To(Equal(1 + 8)) // 1 FlagByte + 8 ConnectionID
})
})
Context("GetLength", func() {
It("errors when calling GetLength for Version Negotiation packets", func() {
hdr := PublicHeader{VersionFlag: true}
_, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).To(MatchError(errGetLengthNotForVersionNegotiation))
})
It("errors when calling GetLength for packets that have the VersionFlag and the ResetFlag set", func() {
hdr := PublicHeader{
ResetFlag: true,
VersionFlag: true,
}
_, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).To(MatchError(errResetAndVersionFlagSet))
})
It("errors when PacketNumberLen is not set", func() {
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
}
_, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).To(MatchError(errPacketNumberLenNotSet))
})
It("gets the length of a packet with longest packet number length and connectionID", func() {
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen6,
}
length, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 8 + 6))) // 1 byte public flag, 8 bytes connectionID, and packet number
})
It("gets the lengths of a packet sent by the client with the VersionFlag set", func() {
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
TruncateConnectionID: true,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen6,
VersionFlag: true,
VersionNumber: protocol.Version36,
}
length, err := hdr.GetLength(protocol.PerspectiveClient)
Expect(err).ToNot(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 4 + 6))) // 1 byte public flag, 4 version number, and packet number
})
It("gets the length of a packet with longest packet number length and truncated connectionID", func() {
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
TruncateConnectionID: true,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen6,
}
length, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 6))) // 1 byte public flag, and packet number
})
It("gets the length of a packet 2 byte packet number length ", func() {
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen2,
}
length, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 8 + 2))) // 1 byte public flag, 8 byte connectionID, and packet number
})
It("works with diversification nonce", func() {
hdr := PublicHeader{
DiversificationNonce: []byte("foo"),
PacketNumberLen: protocol.PacketNumberLen1,
}
length, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).NotTo(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 8 + 3 + 1))) // 1 byte public flag, 8 byte connectionID, 3 byte DiversificationNonce, 1 byte PacketNumber
})
It("gets the length of a PublicReset", func() {
hdr := PublicHeader{
ResetFlag: true,
ConnectionID: 0x4cfa9f9b668619f6,
}
length, err := hdr.GetLength(protocol.PerspectiveServer)
Expect(err).NotTo(HaveOccurred())
Expect(length).To(Equal(protocol.ByteCount(1 + 8))) // 1 byte public flag, 8 byte connectionID
})
})
Context("packet number length", func() {
It("doesn't write a header if the packet number length is not set", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).To(MatchError(errPacketNumberLenNotSet))
})
It("writes a header with a 1-byte packet number", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen1,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x08, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xAD}))
})
It("writes a header with a 2-byte packet number", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xDECAFBAD,
PacketNumberLen: protocol.PacketNumberLen2,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x18, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xAD, 0xFB}))
})
It("writes a header with a 4-byte packet number", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0x13DECAFBAD,
PacketNumberLen: protocol.PacketNumberLen4,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x28, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xAD, 0xFB, 0xCA, 0xDE}))
})
It("writes a header with a 6-byte packet number", func() {
b := &bytes.Buffer{}
hdr := PublicHeader{
ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0xBE1337DECAFBAD,
PacketNumberLen: protocol.PacketNumberLen6,
}
err := hdr.Write(b, protocol.VersionWhatever, protocol.PerspectiveServer)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal([]byte{0x38, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0xAD, 0xFB, 0xCA, 0xDE, 0x37, 0x13}))
})
})
})
})