github.com/tumi8/quic-go@v0.37.4-tum/packet_packer_test.go (about) 1 package quic 2 3 import ( 4 "bytes" 5 "fmt" 6 "net" 7 "time" 8 9 "golang.org/x/exp/rand" 10 11 "github.com/tumi8/quic-go/noninternal/ackhandler" 12 "github.com/tumi8/quic-go/noninternal/handshake" 13 "github.com/tumi8/quic-go/noninternal/mocks" 14 mockackhandler "github.com/tumi8/quic-go/noninternal/mocks/ackhandler" 15 "github.com/tumi8/quic-go/noninternal/protocol" 16 "github.com/tumi8/quic-go/noninternal/qerr" 17 "github.com/tumi8/quic-go/noninternal/utils" 18 "github.com/tumi8/quic-go/noninternal/wire" 19 20 "github.com/golang/mock/gomock" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 ) 25 26 var _ = Describe("Packet packer", func() { 27 const maxPacketSize protocol.ByteCount = 1357 28 const version = protocol.Version1 29 30 var ( 31 packer *packetPacker 32 retransmissionQueue *retransmissionQueue 33 datagramQueue *datagramQueue 34 framer *MockFrameSource 35 ackFramer *MockAckFrameSource 36 initialStream *MockCryptoStream 37 handshakeStream *MockCryptoStream 38 sealingManager *MockSealingManager 39 pnManager *mockackhandler.MockSentPacketHandler 40 ) 41 connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 42 43 parsePacket := func(data []byte) (hdrs []*wire.ExtendedHeader, more []byte) { 44 for len(data) > 0 { 45 if !wire.IsLongHeaderPacket(data[0]) { 46 break 47 } 48 hdr, _, more, err := wire.ParsePacket(data) 49 Expect(err).ToNot(HaveOccurred()) 50 r := bytes.NewReader(data) 51 extHdr, err := hdr.ParseExtended(r, version) 52 Expect(err).ToNot(HaveOccurred()) 53 ExpectWithOffset(1, extHdr.Length).To(BeEquivalentTo(r.Len() - len(more) + int(extHdr.PacketNumberLen))) 54 ExpectWithOffset(1, extHdr.Length+protocol.ByteCount(extHdr.PacketNumberLen)).To(BeNumerically(">=", 4)) 55 data = more 56 hdrs = append(hdrs, extHdr) 57 } 58 return hdrs, data 59 } 60 61 parseShortHeaderPacket := func(data []byte) { 62 l, _, pnLen, _, err := wire.ParseShortHeader(data, connID.Len()) 63 ExpectWithOffset(1, err).ToNot(HaveOccurred()) 64 ExpectWithOffset(1, len(data)-l+int(pnLen)).To(BeNumerically(">=", 4)) 65 } 66 67 expectAppendStreamFrames := func(frames ...ackhandler.StreamFrame) { 68 framer.EXPECT().AppendStreamFrames(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(fs []ackhandler.StreamFrame, _ protocol.ByteCount, v protocol.VersionNumber) ([]ackhandler.StreamFrame, protocol.ByteCount) { 69 var length protocol.ByteCount 70 for _, f := range frames { 71 length += f.Frame.Length(v) 72 } 73 return append(fs, frames...), length 74 }) 75 } 76 77 expectAppendControlFrames := func(frames ...ackhandler.Frame) { 78 framer.EXPECT().AppendControlFrames(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(fs []ackhandler.Frame, _ protocol.ByteCount, v protocol.VersionNumber) ([]ackhandler.Frame, protocol.ByteCount) { 79 var length protocol.ByteCount 80 for _, f := range frames { 81 length += f.Frame.Length(v) 82 } 83 return append(fs, frames...), length 84 }) 85 } 86 87 BeforeEach(func() { 88 rand.Seed(uint64(GinkgoRandomSeed())) 89 retransmissionQueue = newRetransmissionQueue() 90 mockSender := NewMockStreamSender(mockCtrl) 91 mockSender.EXPECT().onHasStreamData(gomock.Any()).AnyTimes() 92 initialStream = NewMockCryptoStream(mockCtrl) 93 handshakeStream = NewMockCryptoStream(mockCtrl) 94 framer = NewMockFrameSource(mockCtrl) 95 ackFramer = NewMockAckFrameSource(mockCtrl) 96 sealingManager = NewMockSealingManager(mockCtrl) 97 pnManager = mockackhandler.NewMockSentPacketHandler(mockCtrl) 98 datagramQueue = newDatagramQueue(func() {}, utils.DefaultLogger) 99 100 packer = newPacketPacker(protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}), func() protocol.ConnectionID { return connID }, initialStream, handshakeStream, pnManager, retransmissionQueue, sealingManager, framer, ackFramer, datagramQueue, protocol.PerspectiveServer) 101 }) 102 103 Context("determining the maximum packet size", func() { 104 It("uses the minimum initial size, if it can't determine if the remote address is IPv4 or IPv6", func() { 105 Expect(getMaxPacketSize(&net.TCPAddr{})).To(BeEquivalentTo(protocol.MinInitialPacketSize)) 106 }) 107 108 It("uses the maximum IPv4 packet size, if the remote address is IPv4", func() { 109 addr := &net.UDPAddr{IP: net.IPv4(11, 12, 13, 14), Port: 1337} 110 Expect(getMaxPacketSize(addr)).To(BeEquivalentTo(protocol.InitialPacketSizeIPv4)) 111 }) 112 113 It("uses the maximum IPv6 packet size, if the remote address is IPv6", func() { 114 ip := net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334") 115 addr := &net.UDPAddr{IP: ip, Port: 1337} 116 Expect(getMaxPacketSize(addr)).To(BeEquivalentTo(protocol.InitialPacketSizeIPv6)) 117 }) 118 }) 119 120 Context("generating a packet header", func() { 121 It("uses the Long Header format", func() { 122 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen3) 123 h := packer.getLongHeader(protocol.EncryptionHandshake, protocol.Version1) 124 Expect(h.PacketNumber).To(Equal(protocol.PacketNumber(0x42))) 125 Expect(h.PacketNumberLen).To(Equal(protocol.PacketNumberLen3)) 126 Expect(h.Version).To(Equal(protocol.Version1)) 127 }) 128 129 It("sets source and destination connection ID", func() { 130 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 131 srcConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 132 destConnID := protocol.ParseConnectionID([]byte{8, 7, 6, 5, 4, 3, 2, 1}) 133 packer.srcConnID = srcConnID 134 packer.getDestConnID = func() protocol.ConnectionID { return destConnID } 135 h := packer.getLongHeader(protocol.EncryptionHandshake, protocol.Version1) 136 Expect(h.SrcConnectionID).To(Equal(srcConnID)) 137 Expect(h.DestConnectionID).To(Equal(destConnID)) 138 }) 139 }) 140 141 Context("encrypting packets", func() { 142 It("encrypts a packet", func() { 143 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x1337), protocol.PacketNumberLen2) 144 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x1337)) 145 sealer := mocks.NewMockShortHeaderSealer(mockCtrl) 146 sealer.EXPECT().Overhead().Return(4).AnyTimes() 147 var hdrRaw []byte 148 gomock.InOrder( 149 sealer.EXPECT().KeyPhase().Return(protocol.KeyPhaseOne), 150 sealer.EXPECT().Seal(gomock.Any(), gomock.Any(), protocol.PacketNumber(0x1337), gomock.Any()).DoAndReturn(func(_, src []byte, _ protocol.PacketNumber, aad []byte) []byte { 151 hdrRaw = append([]byte{}, aad...) 152 return append(src, []byte{0xde, 0xca, 0xfb, 0xad}...) 153 }), 154 sealer.EXPECT().EncryptHeader(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(sample []byte, firstByte *byte, pnBytes []byte) { 155 Expect(firstByte).To(Equal(&hdrRaw[0])) 156 Expect(pnBytes).To(Equal(hdrRaw[len(hdrRaw)-2:])) 157 *firstByte ^= 0xff // invert the first byte 158 // invert the packet number bytes 159 for i := range pnBytes { 160 pnBytes[i] ^= 0xff 161 } 162 }), 163 ) 164 framer.EXPECT().HasData().Return(true) 165 sealingManager.EXPECT().GetInitialSealer().Return(nil, nil) 166 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, nil) 167 sealingManager.EXPECT().Get1RTTSealer().Return(sealer, nil) 168 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 169 expectAppendControlFrames() 170 f := &wire.StreamFrame{Data: []byte{0xde, 0xca, 0xfb, 0xad}} 171 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: f}) 172 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 173 Expect(err).ToNot(HaveOccurred()) 174 Expect(p).ToNot(BeNil()) 175 Expect(p.longHdrPackets).To(BeEmpty()) 176 Expect(p.shortHdrPacket).ToNot(BeNil()) 177 Expect(p.shortHdrPacket.Frames).To(BeEmpty()) 178 Expect(p.shortHdrPacket.StreamFrames).To(HaveLen(1)) 179 Expect(p.shortHdrPacket.StreamFrames[0].Frame).To(Equal(f)) 180 hdrRawEncrypted := append([]byte{}, hdrRaw...) 181 hdrRawEncrypted[0] ^= 0xff 182 hdrRawEncrypted[len(hdrRaw)-2] ^= 0xff 183 hdrRawEncrypted[len(hdrRaw)-1] ^= 0xff 184 Expect(p.buffer.Data[0:len(hdrRaw)]).To(Equal(hdrRawEncrypted)) 185 Expect(p.buffer.Data[p.buffer.Len()-4:]).To(Equal([]byte{0xde, 0xca, 0xfb, 0xad})) 186 }) 187 }) 188 189 Context("packing packets", func() { 190 // getSealer gets a sealer that's expected to seal exactly one packet 191 getSealer := func() *mocks.MockShortHeaderSealer { 192 sealer := mocks.NewMockShortHeaderSealer(mockCtrl) 193 sealer.EXPECT().KeyPhase().Return(protocol.KeyPhaseOne).AnyTimes() 194 sealer.EXPECT().Overhead().Return(7).AnyTimes() 195 sealer.EXPECT().EncryptHeader(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() 196 sealer.EXPECT().Seal(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(dst, src []byte, pn protocol.PacketNumber, associatedData []byte) []byte { 197 return append(src, bytes.Repeat([]byte{'s'}, sealer.Overhead())...) 198 }).AnyTimes() 199 return sealer 200 } 201 202 Context("packing ACK packets", func() { 203 It("doesn't pack a packet if there's no ACK to send", func() { 204 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 205 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 206 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 207 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 208 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true) 209 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, true) 210 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true) 211 p, err := packer.PackCoalescedPacket(true, maxPacketSize, protocol.Version1) 212 Expect(err).ToNot(HaveOccurred()) 213 Expect(p).To(BeNil()) 214 }) 215 216 It("packs Initial ACK-only packets, and pads them (for the client)", func() { 217 packer.perspective = protocol.PerspectiveClient 218 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 219 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 220 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 221 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 10}}} 222 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true).Return(ack) 223 p, err := packer.PackCoalescedPacket(true, maxPacketSize, protocol.Version1) 224 Expect(err).NotTo(HaveOccurred()) 225 Expect(p).ToNot(BeNil()) 226 Expect(p.longHdrPackets).To(HaveLen(1)) 227 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 228 Expect(p.longHdrPackets[0].ack).To(Equal(ack)) 229 Expect(p.longHdrPackets[0].frames).To(BeEmpty()) 230 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 231 parsePacket(p.buffer.Data) 232 }) 233 234 It("packs Initial ACK-only packets, and doesn't pads them (for the server)", func() { 235 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 236 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 237 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 238 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 10}}} 239 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true).Return(ack) 240 p, err := packer.PackCoalescedPacket(true, maxPacketSize, protocol.Version1) 241 Expect(err).NotTo(HaveOccurred()) 242 Expect(p).ToNot(BeNil()) 243 Expect(p.longHdrPackets).To(HaveLen(1)) 244 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 245 Expect(p.longHdrPackets[0].ack).To(Equal(ack)) 246 Expect(p.longHdrPackets[0].frames).To(BeEmpty()) 247 Expect(p.buffer.Len()).To(BeNumerically("<", 100)) 248 parsePacket(p.buffer.Data) 249 }) 250 251 It("packs 1-RTT ACK-only packets, before handshake confirmation", func() { 252 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 253 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 254 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 255 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 256 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 257 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 10}}} 258 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, true) 259 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true).Return(ack) 260 p, err := packer.PackCoalescedPacket(true, maxPacketSize, protocol.Version1) 261 Expect(err).NotTo(HaveOccurred()) 262 Expect(p).ToNot(BeNil()) 263 Expect(p.longHdrPackets).To(BeEmpty()) 264 Expect(p.shortHdrPacket).ToNot(BeNil()) 265 Expect(p.shortHdrPacket.Ack).To(Equal(ack)) 266 Expect(p.shortHdrPacket.Frames).To(BeEmpty()) 267 parsePacket(p.buffer.Data) 268 }) 269 270 It("packs 1-RTT ACK-only packets, after handshake confirmation", func() { 271 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 272 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 273 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 274 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 10}}} 275 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true).Return(ack) 276 p, buffer, err := packer.PackAckOnlyPacket(maxPacketSize, protocol.Version1) 277 Expect(err).NotTo(HaveOccurred()) 278 Expect(p).ToNot(BeNil()) 279 Expect(p.Ack).To(Equal(ack)) 280 Expect(p.Frames).To(BeEmpty()) 281 parsePacket(buffer.Data) 282 }) 283 }) 284 285 Context("packing 0-RTT packets", func() { 286 BeforeEach(func() { 287 packer.perspective = protocol.PerspectiveClient 288 sealingManager.EXPECT().GetInitialSealer().Return(nil, nil).AnyTimes() 289 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, nil).AnyTimes() 290 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable).AnyTimes() 291 initialStream.EXPECT().HasData().AnyTimes() 292 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true).AnyTimes() 293 handshakeStream.EXPECT().HasData().AnyTimes() 294 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, true).AnyTimes() 295 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true).AnyTimes() 296 }) 297 298 It("packs a 0-RTT packet", func() { 299 sealingManager.EXPECT().Get0RTTSealer().Return(getSealer(), nil).AnyTimes() 300 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 301 pnManager.EXPECT().PopPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(0x42)) 302 cf := ackhandler.Frame{Frame: &wire.MaxDataFrame{MaximumData: 0x1337}} 303 framer.EXPECT().HasData().Return(true) 304 framer.EXPECT().AppendControlFrames(gomock.Any(), gomock.Any(), protocol.Version1).DoAndReturn(func(frames []ackhandler.Frame, _ protocol.ByteCount, v protocol.VersionNumber) ([]ackhandler.Frame, protocol.ByteCount) { 305 Expect(frames).To(BeEmpty()) 306 return append(frames, cf), cf.Frame.Length(v) 307 }) 308 // TODO: check sizes 309 framer.EXPECT().AppendStreamFrames(gomock.Any(), gomock.Any(), protocol.Version1).DoAndReturn(func(frames []ackhandler.StreamFrame, _ protocol.ByteCount, _ protocol.VersionNumber) ([]ackhandler.StreamFrame, protocol.ByteCount) { 310 return frames, 0 311 }) 312 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 313 Expect(p).ToNot(BeNil()) 314 Expect(err).ToNot(HaveOccurred()) 315 Expect(p.longHdrPackets).To(HaveLen(1)) 316 Expect(p.longHdrPackets[0].header.Type).To(Equal(protocol.PacketType0RTT)) 317 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.Encryption0RTT)) 318 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 319 Expect(p.longHdrPackets[0].frames[0].Frame).To(Equal(cf.Frame)) 320 Expect(p.longHdrPackets[0].frames[0].Handler).ToNot(BeNil()) 321 }) 322 323 It("doesn't add an ACK-only 0-RTT packet", func() { // ACK frames cannot be sent in 0-RTT packets 324 p, err := packer.PackCoalescedPacket(true, protocol.MaxByteCount, protocol.Version1) 325 Expect(err).ToNot(HaveOccurred()) 326 Expect(p).To(BeNil()) 327 }) 328 }) 329 330 Context("packing CONNECTION_CLOSE", func() { 331 It("clears the reason phrase for crypto errors", func() { 332 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 333 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 334 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 335 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 336 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 337 quicErr := qerr.NewLocalCryptoError(0x42, "crypto error") 338 quicErr.FrameType = 0x1234 339 p, err := packer.PackConnectionClose(quicErr, maxPacketSize, protocol.Version1) 340 Expect(err).ToNot(HaveOccurred()) 341 Expect(p.longHdrPackets).To(HaveLen(1)) 342 Expect(p.longHdrPackets[0].header.Type).To(Equal(protocol.PacketTypeHandshake)) 343 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 344 Expect(p.longHdrPackets[0].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 345 ccf := p.longHdrPackets[0].frames[0].Frame.(*wire.ConnectionCloseFrame) 346 Expect(ccf.IsApplicationError).To(BeFalse()) 347 Expect(ccf.ErrorCode).To(BeEquivalentTo(0x100 + 0x42)) 348 Expect(ccf.FrameType).To(BeEquivalentTo(0x1234)) 349 Expect(ccf.ReasonPhrase).To(BeEmpty()) 350 }) 351 352 It("packs a CONNECTION_CLOSE in 1-RTT", func() { 353 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 354 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 355 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 356 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysDropped) 357 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 358 // expect no framer.PopStreamFrames 359 p, err := packer.PackConnectionClose(&qerr.TransportError{ 360 ErrorCode: qerr.CryptoBufferExceeded, 361 ErrorMessage: "test error", 362 }, maxPacketSize, protocol.Version1) 363 Expect(err).ToNot(HaveOccurred()) 364 Expect(p.longHdrPackets).To(BeEmpty()) 365 Expect(p.shortHdrPacket.Frames).To(HaveLen(1)) 366 Expect(p.shortHdrPacket.Frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 367 ccf := p.shortHdrPacket.Frames[0].Frame.(*wire.ConnectionCloseFrame) 368 Expect(ccf.IsApplicationError).To(BeFalse()) 369 Expect(ccf.ErrorCode).To(BeEquivalentTo(qerr.CryptoBufferExceeded)) 370 Expect(ccf.ReasonPhrase).To(Equal("test error")) 371 }) 372 373 It("packs a CONNECTION_CLOSE in all available encryption levels, and replaces application errors in Initial and Handshake", func() { 374 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(1), protocol.PacketNumberLen2) 375 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(1)) 376 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(2), protocol.PacketNumberLen2) 377 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(2)) 378 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(3), protocol.PacketNumberLen2) 379 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(3)) 380 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 381 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 382 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 383 p, err := packer.PackApplicationClose(&qerr.ApplicationError{ 384 ErrorCode: 0x1337, 385 ErrorMessage: "test error", 386 }, maxPacketSize, protocol.Version1) 387 Expect(err).ToNot(HaveOccurred()) 388 Expect(p.longHdrPackets).To(HaveLen(2)) 389 Expect(p.longHdrPackets[0].header.Type).To(Equal(protocol.PacketTypeInitial)) 390 Expect(p.longHdrPackets[0].header.PacketNumber).To(Equal(protocol.PacketNumber(1))) 391 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 392 Expect(p.longHdrPackets[0].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 393 ccf := p.longHdrPackets[0].frames[0].Frame.(*wire.ConnectionCloseFrame) 394 Expect(ccf.IsApplicationError).To(BeFalse()) 395 Expect(ccf.ErrorCode).To(BeEquivalentTo(qerr.ApplicationErrorErrorCode)) 396 Expect(ccf.ReasonPhrase).To(BeEmpty()) 397 Expect(p.longHdrPackets[1].header.Type).To(Equal(protocol.PacketTypeHandshake)) 398 Expect(p.longHdrPackets[1].header.PacketNumber).To(Equal(protocol.PacketNumber(2))) 399 Expect(p.longHdrPackets[1].frames).To(HaveLen(1)) 400 Expect(p.longHdrPackets[1].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 401 ccf = p.longHdrPackets[1].frames[0].Frame.(*wire.ConnectionCloseFrame) 402 Expect(ccf.IsApplicationError).To(BeFalse()) 403 Expect(ccf.ErrorCode).To(BeEquivalentTo(qerr.ApplicationErrorErrorCode)) 404 Expect(ccf.ReasonPhrase).To(BeEmpty()) 405 Expect(p.shortHdrPacket).ToNot(BeNil()) 406 Expect(p.shortHdrPacket.PacketNumber).To(Equal(protocol.PacketNumber(3))) 407 Expect(p.shortHdrPacket.Frames).To(HaveLen(1)) 408 Expect(p.shortHdrPacket.Frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 409 ccf = p.shortHdrPacket.Frames[0].Frame.(*wire.ConnectionCloseFrame) 410 Expect(ccf.IsApplicationError).To(BeTrue()) 411 Expect(ccf.ErrorCode).To(BeEquivalentTo(0x1337)) 412 Expect(ccf.ReasonPhrase).To(Equal("test error")) 413 }) 414 415 It("packs a CONNECTION_CLOSE in all available encryption levels, as a client", func() { 416 packer.perspective = protocol.PerspectiveClient 417 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(1), protocol.PacketNumberLen2) 418 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(1)) 419 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(2), protocol.PacketNumberLen2) 420 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(2)) 421 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 422 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 423 sealingManager.EXPECT().Get0RTTSealer().Return(nil, handshake.ErrKeysDropped) 424 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 425 p, err := packer.PackApplicationClose(&qerr.ApplicationError{ 426 ErrorCode: 0x1337, 427 ErrorMessage: "test error", 428 }, maxPacketSize, protocol.Version1) 429 Expect(err).ToNot(HaveOccurred()) 430 Expect(p.longHdrPackets).To(HaveLen(1)) 431 Expect(p.buffer.Len()).To(BeNumerically("<", protocol.MinInitialPacketSize)) 432 Expect(p.longHdrPackets[0].header.Type).To(Equal(protocol.PacketTypeHandshake)) 433 Expect(p.longHdrPackets[0].header.PacketNumber).To(Equal(protocol.PacketNumber(1))) 434 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 435 Expect(p.longHdrPackets[0].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 436 ccf := p.longHdrPackets[0].frames[0].Frame.(*wire.ConnectionCloseFrame) 437 Expect(ccf.IsApplicationError).To(BeFalse()) 438 Expect(ccf.ErrorCode).To(BeEquivalentTo(qerr.ApplicationErrorErrorCode)) 439 Expect(ccf.ReasonPhrase).To(BeEmpty()) 440 Expect(p.shortHdrPacket).ToNot(BeNil()) 441 Expect(p.shortHdrPacket.PacketNumber).To(Equal(protocol.PacketNumber(2))) 442 Expect(p.shortHdrPacket.Frames).To(HaveLen(1)) 443 Expect(p.shortHdrPacket.Frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 444 ccf = p.shortHdrPacket.Frames[0].Frame.(*wire.ConnectionCloseFrame) 445 Expect(ccf.IsApplicationError).To(BeTrue()) 446 Expect(ccf.ErrorCode).To(BeEquivalentTo(0x1337)) 447 Expect(ccf.ReasonPhrase).To(Equal("test error")) 448 }) 449 450 It("packs a CONNECTION_CLOSE in all available encryption levels and pads, as a client", func() { 451 packer.perspective = protocol.PerspectiveClient 452 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(1), protocol.PacketNumberLen2) 453 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(1)) 454 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(2), protocol.PacketNumberLen2) 455 pnManager.EXPECT().PopPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(2)) 456 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 457 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 458 sealingManager.EXPECT().Get0RTTSealer().Return(getSealer(), nil) 459 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 460 p, err := packer.PackApplicationClose(&qerr.ApplicationError{ 461 ErrorCode: 0x1337, 462 ErrorMessage: "test error", 463 }, maxPacketSize, protocol.Version1) 464 Expect(err).ToNot(HaveOccurred()) 465 Expect(p.longHdrPackets).To(HaveLen(2)) 466 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 467 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 468 Expect(p.longHdrPackets[0].header.Type).To(Equal(protocol.PacketTypeInitial)) 469 Expect(p.longHdrPackets[0].header.PacketNumber).To(Equal(protocol.PacketNumber(1))) 470 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 471 Expect(p.longHdrPackets[0].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 472 ccf := p.longHdrPackets[0].frames[0].Frame.(*wire.ConnectionCloseFrame) 473 Expect(ccf.IsApplicationError).To(BeFalse()) 474 Expect(ccf.ErrorCode).To(BeEquivalentTo(qerr.ApplicationErrorErrorCode)) 475 Expect(ccf.ReasonPhrase).To(BeEmpty()) 476 Expect(p.longHdrPackets[1].header.Type).To(Equal(protocol.PacketType0RTT)) 477 Expect(p.longHdrPackets[1].header.PacketNumber).To(Equal(protocol.PacketNumber(2))) 478 Expect(p.longHdrPackets[1].frames).To(HaveLen(1)) 479 Expect(p.longHdrPackets[1].frames[0].Frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{})) 480 ccf = p.longHdrPackets[1].frames[0].Frame.(*wire.ConnectionCloseFrame) 481 Expect(ccf.IsApplicationError).To(BeTrue()) 482 Expect(ccf.ErrorCode).To(BeEquivalentTo(0x1337)) 483 Expect(ccf.ReasonPhrase).To(Equal("test error")) 484 hdrs, more := parsePacket(p.buffer.Data) 485 Expect(hdrs).To(HaveLen(2)) 486 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 487 Expect(hdrs[1].Type).To(Equal(protocol.PacketType0RTT)) 488 Expect(more).To(BeEmpty()) 489 }) 490 }) 491 492 Context("packing normal packets", func() { 493 It("returns nil when no packet is queued", func() { 494 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 495 // don't expect any calls to PopPacketNumber 496 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 497 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true) 498 framer.EXPECT().HasData() 499 _, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 500 Expect(err).To(MatchError(errNothingToPack)) 501 }) 502 503 It("appends single packets", func() { 504 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 505 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 506 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 507 framer.EXPECT().HasData().Return(true) 508 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 509 expectAppendControlFrames() 510 f := &wire.StreamFrame{ 511 StreamID: 5, 512 Data: []byte{0xde, 0xca, 0xfb, 0xad}, 513 } 514 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: f}) 515 buffer := getPacketBuffer() 516 buffer.Data = append(buffer.Data, []byte("foobar")...) 517 p, err := packer.AppendPacket(buffer, maxPacketSize, protocol.Version1) 518 Expect(err).ToNot(HaveOccurred()) 519 Expect(p).ToNot(BeNil()) 520 b, err := f.Append(nil, protocol.Version1) 521 Expect(err).ToNot(HaveOccurred()) 522 Expect(p.Frames).To(BeEmpty()) 523 Expect(p.StreamFrames).To(HaveLen(1)) 524 Expect(p.StreamFrames[0].Frame.StreamID).To(Equal(f.StreamID)) 525 Expect(buffer.Data[:6]).To(Equal([]byte("foobar"))) // make sure the packet was actually appended 526 Expect(buffer.Data).To(ContainSubstring(string(b))) 527 }) 528 529 It("packs a single ACK", func() { 530 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 531 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 532 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 42, Smallest: 1}}} 533 framer.EXPECT().HasData() 534 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true).Return(ack) 535 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 536 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 537 Expect(err).NotTo(HaveOccurred()) 538 Expect(p).ToNot(BeNil()) 539 Expect(p.Ack).To(Equal(ack)) 540 }) 541 542 It("packs control frames", func() { 543 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 544 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 545 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 546 framer.EXPECT().HasData().Return(true) 547 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 548 frames := []ackhandler.Frame{ 549 {Frame: &wire.ResetStreamFrame{}}, 550 {Frame: &wire.MaxDataFrame{}}, 551 } 552 expectAppendControlFrames(frames...) 553 expectAppendStreamFrames() 554 buffer := getPacketBuffer() 555 p, err := packer.AppendPacket(buffer, maxPacketSize, protocol.Version1) 556 Expect(p).ToNot(BeNil()) 557 Expect(err).ToNot(HaveOccurred()) 558 Expect(p.Frames).To(HaveLen(2)) 559 for i, f := range p.Frames { 560 Expect(f).To(BeAssignableToTypeOf(frames[i])) 561 } 562 Expect(buffer.Len()).ToNot(BeZero()) 563 }) 564 565 It("packs DATAGRAM frames", func() { 566 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true) 567 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 568 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 569 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 570 f := &wire.DatagramFrame{ 571 DataLenPresent: true, 572 Data: []byte("foobar"), 573 } 574 done := make(chan struct{}) 575 go func() { 576 defer GinkgoRecover() 577 defer close(done) 578 datagramQueue.AddAndWait(f) 579 }() 580 // make sure the DATAGRAM has actually been queued 581 time.Sleep(scaleDuration(20 * time.Millisecond)) 582 583 framer.EXPECT().HasData() 584 buffer := getPacketBuffer() 585 p, err := packer.AppendPacket(buffer, maxPacketSize, protocol.Version1) 586 Expect(p).ToNot(BeNil()) 587 Expect(err).ToNot(HaveOccurred()) 588 Expect(p.Frames).To(HaveLen(1)) 589 Expect(p.Frames[0].Frame).To(Equal(f)) 590 Expect(buffer.Data).ToNot(BeEmpty()) 591 Eventually(done).Should(BeClosed()) 592 }) 593 594 It("doesn't pack a DATAGRAM frame if the ACK frame is too large", func() { 595 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true).Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 100}}}) 596 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 597 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 598 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 599 f := &wire.DatagramFrame{ 600 DataLenPresent: true, 601 Data: make([]byte, maxPacketSize-10), 602 } 603 done := make(chan struct{}) 604 go func() { 605 defer GinkgoRecover() 606 defer close(done) 607 datagramQueue.AddAndWait(f) 608 }() 609 // make sure the DATAGRAM has actually been queued 610 time.Sleep(scaleDuration(20 * time.Millisecond)) 611 612 framer.EXPECT().HasData() 613 buffer := getPacketBuffer() 614 p, err := packer.AppendPacket(buffer, maxPacketSize, protocol.Version1) 615 Expect(p).ToNot(BeNil()) 616 Expect(err).ToNot(HaveOccurred()) 617 Expect(p.Ack).ToNot(BeNil()) 618 Expect(p.Frames).To(BeEmpty()) 619 Expect(buffer.Data).ToNot(BeEmpty()) 620 datagramQueue.CloseWithError(nil) 621 Eventually(done).Should(BeClosed()) 622 }) 623 624 It("accounts for the space consumed by control frames", func() { 625 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 626 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 627 framer.EXPECT().HasData().Return(true) 628 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 629 var maxSize protocol.ByteCount 630 gomock.InOrder( 631 framer.EXPECT().AppendControlFrames(gomock.Any(), gomock.Any(), protocol.Version1).DoAndReturn(func(fs []ackhandler.Frame, maxLen protocol.ByteCount, _ protocol.VersionNumber) ([]ackhandler.Frame, protocol.ByteCount) { 632 maxSize = maxLen 633 return fs, 444 634 }), 635 framer.EXPECT().AppendStreamFrames(gomock.Any(), gomock.Any(), protocol.Version1).Do(func(fs []ackhandler.StreamFrame, maxLen protocol.ByteCount, _ protocol.VersionNumber) ([]ackhandler.StreamFrame, protocol.ByteCount) { 636 Expect(maxLen).To(Equal(maxSize - 444)) 637 return fs, 0 638 }), 639 ) 640 _, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 641 Expect(err).To(MatchError(errNothingToPack)) 642 }) 643 644 It("pads if payload length + packet number length is smaller than 4, for Long Header packets", func() { 645 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 646 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 647 sealer := getSealer() 648 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 649 sealingManager.EXPECT().GetHandshakeSealer().Return(sealer, nil) 650 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 651 packer.retransmissionQueue.addHandshake(&wire.PingFrame{}) 652 handshakeStream.EXPECT().HasData() 653 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 654 packet, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 655 Expect(err).ToNot(HaveOccurred()) 656 Expect(packet).ToNot(BeNil()) 657 Expect(packet.longHdrPackets).To(HaveLen(1)) 658 // cut off the tag that the mock sealer added 659 // packet.buffer.Data = packet.buffer.Data[:packet.buffer.Len()-protocol.ByteCount(sealer.Overhead())] 660 hdr, _, _, err := wire.ParsePacket(packet.buffer.Data) 661 Expect(err).ToNot(HaveOccurred()) 662 data := packet.buffer.Data 663 r := bytes.NewReader(data) 664 extHdr, err := hdr.ParseExtended(r, protocol.Version1) 665 Expect(err).ToNot(HaveOccurred()) 666 Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen1)) 667 Expect(r.Len()).To(Equal(4 - 1 /* packet number length */ + sealer.Overhead())) 668 // the first bytes of the payload should be a 2 PADDING frames... 669 firstPayloadByte, err := r.ReadByte() 670 Expect(err).ToNot(HaveOccurred()) 671 Expect(firstPayloadByte).To(Equal(byte(0))) 672 secondPayloadByte, err := r.ReadByte() 673 Expect(err).ToNot(HaveOccurred()) 674 Expect(secondPayloadByte).To(Equal(byte(0))) 675 // ... followed by the PING 676 frameParser := wire.NewFrameParser(false) 677 l, frame, err := frameParser.ParseNext(data[len(data)-r.Len():], protocol.Encryption1RTT, protocol.Version1) 678 Expect(err).ToNot(HaveOccurred()) 679 Expect(frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 680 Expect(r.Len() - l).To(Equal(sealer.Overhead())) 681 }) 682 683 It("pads if payload length + packet number length is smaller than 4", func() { 684 f := &wire.StreamFrame{ 685 StreamID: 0x10, // small stream ID, such that only a single byte is consumed 686 Fin: true, 687 } 688 Expect(f.Length(protocol.Version1)).To(BeEquivalentTo(2)) 689 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 690 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 691 sealer := getSealer() 692 sealingManager.EXPECT().Get1RTTSealer().Return(sealer, nil) 693 framer.EXPECT().HasData().Return(true) 694 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 695 expectAppendControlFrames() 696 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: f}) 697 buffer := getPacketBuffer() 698 _, err := packer.AppendPacket(buffer, maxPacketSize, protocol.Version1) 699 Expect(err).ToNot(HaveOccurred()) 700 // cut off the tag that the mock sealer added 701 buffer.Data = buffer.Data[:buffer.Len()-protocol.ByteCount(sealer.Overhead())] 702 data := buffer.Data 703 l, _, pnLen, _, err := wire.ParseShortHeader(data, connID.Len()) 704 Expect(err).ToNot(HaveOccurred()) 705 r := bytes.NewReader(data[l:]) 706 Expect(pnLen).To(Equal(protocol.PacketNumberLen1)) 707 Expect(r.Len()).To(Equal(4 - 1 /* packet number length */)) 708 // the first byte of the payload should be a PADDING frame... 709 firstPayloadByte, err := r.ReadByte() 710 Expect(err).ToNot(HaveOccurred()) 711 Expect(firstPayloadByte).To(Equal(byte(0))) 712 // ... followed by the STREAM frame 713 frameParser := wire.NewFrameParser(true) 714 l, frame, err := frameParser.ParseNext(buffer.Data[len(data)-r.Len():], protocol.Encryption1RTT, protocol.Version1) 715 Expect(err).ToNot(HaveOccurred()) 716 Expect(frame).To(BeAssignableToTypeOf(&wire.StreamFrame{})) 717 sf := frame.(*wire.StreamFrame) 718 Expect(sf.StreamID).To(Equal(f.StreamID)) 719 Expect(sf.Fin).To(Equal(f.Fin)) 720 Expect(sf.Data).To(BeEmpty()) 721 Expect(r.Len() - l).To(BeZero()) 722 }) 723 724 It("packs multiple small STREAM frames into single packet", func() { 725 f1 := &wire.StreamFrame{ 726 StreamID: 5, 727 Data: []byte("frame 1"), 728 DataLenPresent: true, 729 } 730 f2 := &wire.StreamFrame{ 731 StreamID: 5, 732 Data: []byte("frame 2"), 733 DataLenPresent: true, 734 } 735 f3 := &wire.StreamFrame{ 736 StreamID: 3, 737 Data: []byte("frame 3"), 738 DataLenPresent: true, 739 } 740 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 741 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 742 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 743 framer.EXPECT().HasData().Return(true) 744 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 745 expectAppendControlFrames() 746 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: f1}, ackhandler.StreamFrame{Frame: f2}, ackhandler.StreamFrame{Frame: f3}) 747 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 748 Expect(p).ToNot(BeNil()) 749 Expect(err).ToNot(HaveOccurred()) 750 Expect(p.Frames).To(BeEmpty()) 751 Expect(p.StreamFrames).To(HaveLen(3)) 752 Expect(p.StreamFrames[0].Frame.Data).To(Equal([]byte("frame 1"))) 753 Expect(p.StreamFrames[1].Frame.Data).To(Equal([]byte("frame 2"))) 754 Expect(p.StreamFrames[2].Frame.Data).To(Equal([]byte("frame 3"))) 755 }) 756 757 Context("making ACK packets ack-eliciting", func() { 758 sendMaxNumNonAckElicitingAcks := func() { 759 for i := 0; i < protocol.MaxNonAckElicitingAcks; i++ { 760 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 761 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 762 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 763 framer.EXPECT().HasData().Return(true) 764 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false).Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) 765 expectAppendControlFrames() 766 expectAppendStreamFrames() 767 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 768 Expect(p).ToNot(BeNil()) 769 Expect(err).ToNot(HaveOccurred()) 770 Expect(p.Ack).ToNot(BeNil()) 771 Expect(p.Frames).To(BeEmpty()) 772 } 773 } 774 775 It("adds a PING frame when it's supposed to send a ack-eliciting packet", func() { 776 sendMaxNumNonAckElicitingAcks() 777 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 778 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 779 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 780 framer.EXPECT().HasData().Return(true) 781 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false).Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) 782 expectAppendControlFrames() 783 expectAppendStreamFrames() 784 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 785 Expect(p).ToNot(BeNil()) 786 Expect(err).ToNot(HaveOccurred()) 787 var hasPing bool 788 for _, f := range p.Frames { 789 if _, ok := f.Frame.(*wire.PingFrame); ok { 790 hasPing = true 791 Expect(f.Handler).To(BeNil()) // make sure the PING is not retransmitted if lost 792 } 793 } 794 Expect(hasPing).To(BeTrue()) 795 // make sure the next packet doesn't contain another PING 796 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 797 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 798 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 799 framer.EXPECT().HasData().Return(true) 800 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false).Return(&wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}) 801 expectAppendControlFrames() 802 expectAppendStreamFrames() 803 p, err = packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 804 Expect(p).ToNot(BeNil()) 805 Expect(err).ToNot(HaveOccurred()) 806 Expect(p.Ack).ToNot(BeNil()) 807 Expect(p.Frames).To(BeEmpty()) 808 }) 809 810 It("waits until there's something to send before adding a PING frame", func() { 811 sendMaxNumNonAckElicitingAcks() 812 // nothing to send 813 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 814 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 815 framer.EXPECT().HasData().Return(true) 816 expectAppendControlFrames() 817 expectAppendStreamFrames() 818 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 819 _, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 820 Expect(err).To(MatchError(errNothingToPack)) 821 // now add some frame to send 822 expectAppendControlFrames() 823 expectAppendStreamFrames() 824 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 825 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 826 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 827 framer.EXPECT().HasData().Return(true) 828 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}} 829 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false).Return(ack) 830 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 831 Expect(err).ToNot(HaveOccurred()) 832 Expect(p.Ack).To(Equal(ack)) 833 var hasPing bool 834 for _, f := range p.Frames { 835 if _, ok := f.Frame.(*wire.PingFrame); ok { 836 hasPing = true 837 Expect(f.Handler).To(BeNil()) // make sure the PING is not retransmitted if lost 838 } 839 } 840 Expect(hasPing).To(BeTrue()) 841 }) 842 843 It("doesn't send a PING if it already sent another ack-eliciting frame", func() { 844 sendMaxNumNonAckElicitingAcks() 845 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 846 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 847 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 848 framer.EXPECT().HasData().Return(true) 849 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 850 expectAppendStreamFrames() 851 expectAppendControlFrames(ackhandler.Frame{Frame: &wire.MaxDataFrame{}}) 852 p, err := packer.AppendPacket(getPacketBuffer(), maxPacketSize, protocol.Version1) 853 Expect(err).ToNot(HaveOccurred()) 854 Expect(p).ToNot(BeNil()) 855 Expect(p.Frames).ToNot(ContainElement(&wire.PingFrame{})) 856 }) 857 }) 858 }) 859 860 Context("packing crypto packets", func() { 861 It("sets the length", func() { 862 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 863 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 864 f := &wire.CryptoFrame{ 865 Offset: 0x1337, 866 Data: []byte("foobar"), 867 } 868 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 869 handshakeStream.EXPECT().HasData().Return(true).AnyTimes() 870 handshakeStream.EXPECT().PopCryptoFrame(gomock.Any()).Return(f) 871 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 872 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 873 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 874 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 875 Expect(err).ToNot(HaveOccurred()) 876 Expect(p).ToNot(BeNil()) 877 parsePacket(p.buffer.Data) 878 }) 879 880 It("packs an Initial packet and pads it", func() { 881 packer.perspective = protocol.PerspectiveClient 882 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen1) 883 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 884 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 885 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 886 sealingManager.EXPECT().Get0RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 887 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 888 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 889 initialStream.EXPECT().HasData().Return(true).Times(2) 890 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 891 return &wire.CryptoFrame{Offset: 0x42, Data: []byte("initial")} 892 }) 893 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 894 Expect(err).ToNot(HaveOccurred()) 895 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 896 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 897 Expect(p.longHdrPackets).To(HaveLen(1)) 898 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 899 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 900 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("initial"))) 901 hdrs, more := parsePacket(p.buffer.Data) 902 Expect(hdrs).To(HaveLen(1)) 903 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 904 Expect(more).To(BeEmpty()) 905 }) 906 907 It("packs a maximum size Handshake packet", func() { 908 var f *wire.CryptoFrame 909 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 910 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 911 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 912 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 913 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true) 914 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 915 initialStream.EXPECT().HasData() 916 handshakeStream.EXPECT().HasData().Return(true).Times(2) 917 handshakeStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 918 f = &wire.CryptoFrame{Offset: 0x1337} 919 f.Data = bytes.Repeat([]byte{'f'}, int(size-f.Length(protocol.Version1)-1)) 920 Expect(f.Length(protocol.Version1)).To(Equal(size)) 921 return f 922 }) 923 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 924 Expect(err).ToNot(HaveOccurred()) 925 Expect(p.longHdrPackets).To(HaveLen(1)) 926 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 927 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 928 parsePacket(p.buffer.Data) 929 }) 930 931 It("packs a coalesced packet with Initial / Handshake, and pads it", func() { 932 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 933 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 934 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 935 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 936 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 937 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 938 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 939 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 940 // don't EXPECT any calls for a Handshake ACK frame 941 initialStream.EXPECT().HasData().Return(true).Times(2) 942 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 943 return &wire.CryptoFrame{Offset: 0x42, Data: []byte("initial")} 944 }) 945 handshakeStream.EXPECT().HasData().Return(true).Times(2) 946 handshakeStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 947 return &wire.CryptoFrame{Offset: 0x1337, Data: []byte("handshake")} 948 }) 949 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 950 Expect(err).ToNot(HaveOccurred()) 951 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 952 Expect(p.longHdrPackets).To(HaveLen(2)) 953 Expect(p.shortHdrPacket).To(BeNil()) 954 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 955 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 956 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("initial"))) 957 Expect(p.longHdrPackets[1].EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 958 Expect(p.longHdrPackets[1].frames).To(HaveLen(1)) 959 Expect(p.longHdrPackets[1].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("handshake"))) 960 hdrs, more := parsePacket(p.buffer.Data) 961 Expect(hdrs).To(HaveLen(2)) 962 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 963 Expect(hdrs[1].Type).To(Equal(protocol.PacketTypeHandshake)) 964 Expect(more).To(BeEmpty()) 965 }) 966 967 It("packs a coalesced packet with Initial / super short Handshake, and pads it", func() { 968 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 969 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 970 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 971 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 972 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 973 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 974 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 975 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 976 // don't EXPECT any calls for a Handshake ACK frame 977 initialStream.EXPECT().HasData().Return(true).Times(2) 978 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 979 return &wire.CryptoFrame{Offset: 0x42, Data: []byte("initial")} 980 }) 981 handshakeStream.EXPECT().HasData() 982 packer.retransmissionQueue.addHandshake(&wire.PingFrame{}) 983 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 984 Expect(err).ToNot(HaveOccurred()) 985 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 986 Expect(p.longHdrPackets).To(HaveLen(2)) 987 Expect(p.shortHdrPacket).To(BeNil()) 988 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 989 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 990 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("initial"))) 991 Expect(p.longHdrPackets[1].EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 992 Expect(p.longHdrPackets[1].frames).To(HaveLen(1)) 993 Expect(p.longHdrPackets[1].frames[0].Frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 994 hdrs, more := parsePacket(p.buffer.Data) 995 Expect(hdrs).To(HaveLen(2)) 996 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 997 Expect(hdrs[1].Type).To(Equal(protocol.PacketTypeHandshake)) 998 Expect(more).To(BeEmpty()) 999 }) 1000 1001 It("packs a coalesced packet with super short Initial / super short Handshake, and pads it", func() { 1002 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen1) 1003 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 1004 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 1005 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 1006 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1007 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1008 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1009 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, gomock.Any()) 1010 initialStream.EXPECT().HasData() 1011 handshakeStream.EXPECT().HasData() 1012 packer.retransmissionQueue.addInitial(&wire.PingFrame{}) 1013 packer.retransmissionQueue.addHandshake(&wire.PingFrame{}) 1014 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1015 Expect(err).ToNot(HaveOccurred()) 1016 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1017 Expect(p.longHdrPackets).To(HaveLen(2)) 1018 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1019 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1020 Expect(p.longHdrPackets[0].frames[0].Frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 1021 Expect(p.longHdrPackets[1].EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 1022 Expect(p.longHdrPackets[1].frames).To(HaveLen(1)) 1023 Expect(p.longHdrPackets[1].frames[0].Frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 1024 hdrs, more := parsePacket(p.buffer.Data) 1025 Expect(hdrs).To(HaveLen(2)) 1026 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 1027 Expect(hdrs[1].Type).To(Equal(protocol.PacketTypeHandshake)) 1028 Expect(more).To(BeEmpty()) 1029 }) 1030 1031 It("packs a coalesced packet with Initial / super short 1-RTT, and pads it", func() { 1032 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 1033 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 1034 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 1035 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 1036 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1037 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1038 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1039 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1040 initialStream.EXPECT().HasData().Return(true).Times(2) 1041 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 1042 return &wire.CryptoFrame{Offset: 0x42, Data: []byte("initial")} 1043 }) 1044 expectAppendControlFrames() 1045 expectAppendStreamFrames() 1046 framer.EXPECT().HasData().Return(true) 1047 packer.retransmissionQueue.addAppData(&wire.PingFrame{}) 1048 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1049 Expect(err).ToNot(HaveOccurred()) 1050 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1051 Expect(p.longHdrPackets).To(HaveLen(1)) 1052 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1053 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1054 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("initial"))) 1055 Expect(p.shortHdrPacket).ToNot(BeNil()) 1056 Expect(p.shortHdrPacket.Frames).To(HaveLen(1)) 1057 Expect(p.shortHdrPacket.Frames[0].Frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 1058 hdrs, more := parsePacket(p.buffer.Data) 1059 Expect(hdrs).To(HaveLen(1)) 1060 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 1061 Expect(more).ToNot(BeEmpty()) 1062 parseShortHeaderPacket(more) 1063 }) 1064 1065 It("packs a coalesced packet with Initial / 0-RTT, and pads it", func() { 1066 packer.perspective = protocol.PerspectiveClient 1067 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 1068 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x24)) 1069 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1070 pnManager.EXPECT().PopPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(0x42)) 1071 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1072 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1073 sealingManager.EXPECT().Get0RTTSealer().Return(getSealer(), nil) 1074 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1075 framer.EXPECT().HasData().Return(true) 1076 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1077 // don't EXPECT any calls for a Handshake ACK frame 1078 initialStream.EXPECT().HasData().Return(true).Times(2) 1079 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 1080 return &wire.CryptoFrame{Offset: 0x42, Data: []byte("initial")} 1081 }) 1082 expectAppendControlFrames() 1083 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: &wire.StreamFrame{Data: []byte("foobar")}}) 1084 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1085 Expect(err).ToNot(HaveOccurred()) 1086 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 1087 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1088 Expect(p.longHdrPackets).To(HaveLen(2)) 1089 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1090 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1091 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("initial"))) 1092 Expect(p.longHdrPackets[0].streamFrames).To(BeEmpty()) 1093 Expect(p.longHdrPackets[1].EncryptionLevel()).To(Equal(protocol.Encryption0RTT)) 1094 Expect(p.longHdrPackets[1].frames).To(BeEmpty()) 1095 Expect(p.longHdrPackets[1].streamFrames).To(HaveLen(1)) 1096 Expect(p.longHdrPackets[1].streamFrames[0].Frame.Data).To(Equal([]byte("foobar"))) 1097 hdrs, more := parsePacket(p.buffer.Data) 1098 Expect(hdrs).To(HaveLen(2)) 1099 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeInitial)) 1100 Expect(hdrs[1].Type).To(Equal(protocol.PacketType0RTT)) 1101 Expect(more).To(BeEmpty()) 1102 }) 1103 1104 It("packs a coalesced packet with Handshake / 1-RTT", func() { 1105 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 1106 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x24)) 1107 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1108 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 1109 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 1110 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1111 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1112 framer.EXPECT().HasData().Return(true) 1113 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 1114 // don't EXPECT any calls for a 1-RTT ACK frame 1115 handshakeStream.EXPECT().HasData().Return(true).Times(2) 1116 handshakeStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 1117 return &wire.CryptoFrame{Offset: 0x1337, Data: []byte("handshake")} 1118 }) 1119 expectAppendControlFrames() 1120 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: &wire.StreamFrame{Data: []byte("foobar")}}) 1121 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1122 Expect(err).ToNot(HaveOccurred()) 1123 Expect(p.buffer.Len()).To(BeNumerically("<", 100)) 1124 Expect(p.longHdrPackets).To(HaveLen(1)) 1125 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 1126 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1127 Expect(p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame).Data).To(Equal([]byte("handshake"))) 1128 Expect(p.shortHdrPacket).ToNot(BeNil()) 1129 Expect(p.shortHdrPacket.Frames).To(BeEmpty()) 1130 Expect(p.shortHdrPacket.StreamFrames).To(HaveLen(1)) 1131 Expect(p.shortHdrPacket.StreamFrames[0].Frame.Data).To(Equal([]byte("foobar"))) 1132 hdrs, more := parsePacket(p.buffer.Data) 1133 Expect(hdrs).To(HaveLen(1)) 1134 Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeHandshake)) 1135 Expect(more).ToNot(BeEmpty()) 1136 parseShortHeaderPacket(more) 1137 }) 1138 1139 It("doesn't add a coalesced packet if the remaining size is smaller than MaxCoalescedPacketSize", func() { 1140 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x24), protocol.PacketNumberLen2) 1141 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x24)) 1142 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 1143 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1144 // don't EXPECT any calls to GetHandshakeSealer and Get1RTTSealer 1145 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 1146 handshakeStream.EXPECT().HasData().Return(true).Times(2) 1147 handshakeStream.EXPECT().PopCryptoFrame(gomock.Any()).DoAndReturn(func(size protocol.ByteCount) *wire.CryptoFrame { 1148 s := size - protocol.MinCoalescedPacketSize 1149 f := &wire.CryptoFrame{Offset: 0x1337} 1150 f.Data = bytes.Repeat([]byte{'f'}, int(s-f.Length(protocol.Version1)-1)) 1151 Expect(f.Length(protocol.Version1)).To(Equal(s)) 1152 return f 1153 }) 1154 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1155 Expect(err).ToNot(HaveOccurred()) 1156 Expect(p.longHdrPackets).To(HaveLen(1)) 1157 Expect(p.shortHdrPacket).To(BeNil()) 1158 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 1159 Expect(len(p.buffer.Data)).To(BeEquivalentTo(maxPacketSize - protocol.MinCoalescedPacketSize)) 1160 parsePacket(p.buffer.Data) 1161 }) 1162 1163 It("pads if payload length + packet number length is smaller than 4, for Long Header packets", func() { 1164 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 1165 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 1166 sealer := getSealer() 1167 sealingManager.EXPECT().GetInitialSealer().Return(nil, handshake.ErrKeysDropped) 1168 sealingManager.EXPECT().GetHandshakeSealer().Return(sealer, nil) 1169 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1170 packer.retransmissionQueue.addHandshake(&wire.PingFrame{}) 1171 handshakeStream.EXPECT().HasData() 1172 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 1173 packet, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1174 Expect(err).ToNot(HaveOccurred()) 1175 Expect(packet).ToNot(BeNil()) 1176 Expect(packet.longHdrPackets).To(HaveLen(1)) 1177 Expect(packet.shortHdrPacket).To(BeNil()) 1178 // cut off the tag that the mock sealer added 1179 // packet.buffer.Data = packet.buffer.Data[:packet.buffer.Len()-protocol.ByteCount(sealer.Overhead())] 1180 hdr, _, _, err := wire.ParsePacket(packet.buffer.Data) 1181 Expect(err).ToNot(HaveOccurred()) 1182 data := packet.buffer.Data 1183 r := bytes.NewReader(data) 1184 extHdr, err := hdr.ParseExtended(r, protocol.Version1) 1185 Expect(err).ToNot(HaveOccurred()) 1186 Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen1)) 1187 Expect(r.Len()).To(Equal(4 - 1 /* packet number length */ + sealer.Overhead())) 1188 // the first bytes of the payload should be a 2 PADDING frames... 1189 firstPayloadByte, err := r.ReadByte() 1190 Expect(err).ToNot(HaveOccurred()) 1191 Expect(firstPayloadByte).To(Equal(byte(0))) 1192 secondPayloadByte, err := r.ReadByte() 1193 Expect(err).ToNot(HaveOccurred()) 1194 Expect(secondPayloadByte).To(Equal(byte(0))) 1195 // ... followed by the PING 1196 frameParser := wire.NewFrameParser(false) 1197 l, frame, err := frameParser.ParseNext(data[len(data)-r.Len():], protocol.Encryption1RTT, protocol.Version1) 1198 Expect(err).ToNot(HaveOccurred()) 1199 Expect(frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 1200 Expect(r.Len() - l).To(Equal(sealer.Overhead())) 1201 }) 1202 1203 It("adds retransmissions", func() { 1204 f := &wire.CryptoFrame{Data: []byte("Initial")} 1205 retransmissionQueue.addInitial(f) 1206 retransmissionQueue.addHandshake(&wire.CryptoFrame{Data: []byte("Handshake")}) 1207 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1208 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1209 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1210 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1211 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1212 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1213 initialStream.EXPECT().HasData() 1214 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1215 Expect(err).ToNot(HaveOccurred()) 1216 Expect(p.longHdrPackets).To(HaveLen(1)) 1217 Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1218 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1219 Expect(p.longHdrPackets[0].frames[0].Frame).To(Equal(f)) 1220 Expect(p.longHdrPackets[0].frames[0].Handler).ToNot(BeNil()) 1221 }) 1222 1223 It("sends an Initial packet containing only an ACK", func() { 1224 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 20}}} 1225 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true).Return(ack) 1226 initialStream.EXPECT().HasData().Times(2) 1227 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1228 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1229 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1230 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1231 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1232 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1233 Expect(err).ToNot(HaveOccurred()) 1234 Expect(p.longHdrPackets).To(HaveLen(1)) 1235 Expect(p.longHdrPackets[0].ack).To(Equal(ack)) 1236 }) 1237 1238 It("doesn't pack anything if there's nothing to send at Initial and Handshake keys are not yet available", func() { 1239 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1240 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1241 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1242 initialStream.EXPECT().HasData() 1243 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true) 1244 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1245 Expect(err).ToNot(HaveOccurred()) 1246 Expect(p).To(BeNil()) 1247 }) 1248 1249 It("sends a Handshake packet containing only an ACK", func() { 1250 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 20}}} 1251 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, true) 1252 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, true).Return(ack) 1253 initialStream.EXPECT().HasData() 1254 handshakeStream.EXPECT().HasData().Times(2) 1255 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1256 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1257 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1258 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1259 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 1260 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1261 Expect(err).ToNot(HaveOccurred()) 1262 Expect(p.longHdrPackets).To(HaveLen(1)) 1263 Expect(p.longHdrPackets[0].ack).To(Equal(ack)) 1264 }) 1265 1266 for _, pers := range []protocol.Perspective{protocol.PerspectiveServer, protocol.PerspectiveClient} { 1267 perspective := pers 1268 1269 It(fmt.Sprintf("pads Initial packets to the required minimum packet size, for the %s", perspective), func() { 1270 token := []byte("initial token") 1271 packer.SetToken(token) 1272 f := &wire.CryptoFrame{Data: []byte("foobar")} 1273 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1274 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1275 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1276 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1277 sealingManager.EXPECT().Get0RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1278 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1279 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1280 initialStream.EXPECT().HasData().Return(true).Times(2) 1281 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).Return(f) 1282 packer.perspective = protocol.PerspectiveClient 1283 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1284 Expect(err).ToNot(HaveOccurred()) 1285 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 1286 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1287 Expect(p.longHdrPackets).To(HaveLen(1)) 1288 Expect(p.longHdrPackets[0].header.Token).To(Equal(token)) 1289 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1290 cf := p.longHdrPackets[0].frames[0].Frame.(*wire.CryptoFrame) 1291 Expect(cf.Data).To(Equal([]byte("foobar"))) 1292 }) 1293 } 1294 1295 It("adds an ACK frame", func() { 1296 f := &wire.CryptoFrame{Data: []byte("foobar")} 1297 ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 42, Largest: 1337}}} 1298 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1299 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1300 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1301 sealingManager.EXPECT().GetHandshakeSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1302 sealingManager.EXPECT().Get0RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1303 sealingManager.EXPECT().Get1RTTSealer().Return(nil, handshake.ErrKeysNotYetAvailable) 1304 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false).Return(ack) 1305 initialStream.EXPECT().HasData().Return(true).Times(2) 1306 initialStream.EXPECT().PopCryptoFrame(gomock.Any()).Return(f) 1307 packer.perspective = protocol.PerspectiveClient 1308 p, err := packer.PackCoalescedPacket(false, maxPacketSize, protocol.Version1) 1309 Expect(err).ToNot(HaveOccurred()) 1310 Expect(p.longHdrPackets).To(HaveLen(1)) 1311 Expect(p.longHdrPackets[0].ack).To(Equal(ack)) 1312 Expect(p.longHdrPackets[0].frames).To(HaveLen(1)) 1313 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1314 }) 1315 }) 1316 1317 Context("packing probe packets", func() { 1318 for _, pers := range []protocol.Perspective{protocol.PerspectiveServer, protocol.PerspectiveClient} { 1319 perspective := pers 1320 1321 It(fmt.Sprintf("packs an Initial probe packet and pads it, for the %s", perspective), func() { 1322 packer.perspective = perspective 1323 f := &wire.CryptoFrame{Data: []byte("Initial")} 1324 retransmissionQueue.addInitial(f) 1325 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1326 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1327 initialStream.EXPECT().HasData() 1328 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1329 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1330 1331 p, err := packer.MaybePackProbePacket(protocol.EncryptionInitial, maxPacketSize, protocol.Version1) 1332 Expect(err).ToNot(HaveOccurred()) 1333 Expect(p).ToNot(BeNil()) 1334 Expect(p.longHdrPackets).To(HaveLen(1)) 1335 packet := p.longHdrPackets[0] 1336 Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1337 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 1338 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1339 Expect(packet.frames).To(HaveLen(1)) 1340 Expect(packet.frames[0].Frame).To(Equal(f)) 1341 parsePacket(p.buffer.Data) 1342 }) 1343 1344 It(fmt.Sprintf("packs an Initial probe packet with 1 byte payload, for the %s", perspective), func() { 1345 packer.perspective = perspective 1346 retransmissionQueue.addInitial(&wire.PingFrame{}) 1347 sealingManager.EXPECT().GetInitialSealer().Return(getSealer(), nil) 1348 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionInitial, false) 1349 initialStream.EXPECT().HasData() 1350 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen1) 1351 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionInitial).Return(protocol.PacketNumber(0x42)) 1352 1353 p, err := packer.MaybePackProbePacket(protocol.EncryptionInitial, maxPacketSize, protocol.Version1) 1354 Expect(err).ToNot(HaveOccurred()) 1355 Expect(err).ToNot(HaveOccurred()) 1356 Expect(p).ToNot(BeNil()) 1357 Expect(p.longHdrPackets).To(HaveLen(1)) 1358 packet := p.longHdrPackets[0] 1359 Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionInitial)) 1360 Expect(p.buffer.Len()).To(BeNumerically(">=", protocol.MinInitialPacketSize)) 1361 Expect(p.buffer.Len()).To(BeEquivalentTo(maxPacketSize)) 1362 Expect(packet.frames).To(HaveLen(1)) 1363 Expect(packet.frames[0].Frame).To(BeAssignableToTypeOf(&wire.PingFrame{})) 1364 parsePacket(p.buffer.Data) 1365 }) 1366 } 1367 1368 It("packs a Handshake probe packet", func() { 1369 f := &wire.CryptoFrame{Data: []byte("Handshake")} 1370 retransmissionQueue.addHandshake(f) 1371 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1372 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 1373 handshakeStream.EXPECT().HasData() 1374 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1375 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 1376 1377 p, err := packer.MaybePackProbePacket(protocol.EncryptionHandshake, maxPacketSize, protocol.Version1) 1378 Expect(err).ToNot(HaveOccurred()) 1379 Expect(err).ToNot(HaveOccurred()) 1380 Expect(p).ToNot(BeNil()) 1381 Expect(p.longHdrPackets).To(HaveLen(1)) 1382 packet := p.longHdrPackets[0] 1383 Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 1384 Expect(packet.frames).To(HaveLen(1)) 1385 Expect(packet.frames[0].Frame).To(Equal(f)) 1386 parsePacket(p.buffer.Data) 1387 }) 1388 1389 It("packs a full size Handshake probe packet", func() { 1390 f := &wire.CryptoFrame{Data: make([]byte, 2000)} 1391 retransmissionQueue.addHandshake(f) 1392 sealingManager.EXPECT().GetHandshakeSealer().Return(getSealer(), nil) 1393 ackFramer.EXPECT().GetAckFrame(protocol.EncryptionHandshake, false) 1394 handshakeStream.EXPECT().HasData() 1395 pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1396 pnManager.EXPECT().PopPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42)) 1397 1398 p, err := packer.MaybePackProbePacket(protocol.EncryptionHandshake, maxPacketSize, protocol.Version1) 1399 Expect(err).ToNot(HaveOccurred()) 1400 Expect(err).ToNot(HaveOccurred()) 1401 Expect(p).ToNot(BeNil()) 1402 Expect(p.longHdrPackets).To(HaveLen(1)) 1403 packet := p.longHdrPackets[0] 1404 Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionHandshake)) 1405 Expect(packet.frames).To(HaveLen(1)) 1406 Expect(packet.frames[0].Frame).To(BeAssignableToTypeOf(&wire.CryptoFrame{})) 1407 Expect(packet.length).To(Equal(maxPacketSize)) 1408 parsePacket(p.buffer.Data) 1409 }) 1410 1411 It("packs a 1-RTT probe packet", func() { 1412 f := &wire.StreamFrame{Data: []byte("1-RTT")} 1413 retransmissionQueue.addInitial(f) 1414 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1415 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 1416 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1417 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 1418 framer.EXPECT().HasData().Return(true) 1419 expectAppendControlFrames() 1420 expectAppendStreamFrames(ackhandler.StreamFrame{Frame: f}) 1421 1422 p, err := packer.MaybePackProbePacket(protocol.Encryption1RTT, maxPacketSize, protocol.Version1) 1423 Expect(err).ToNot(HaveOccurred()) 1424 Expect(p).ToNot(BeNil()) 1425 Expect(p.longHdrPackets).To(BeEmpty()) 1426 Expect(p.shortHdrPacket).ToNot(BeNil()) 1427 packet := p.shortHdrPacket 1428 Expect(packet.Frames).To(BeEmpty()) 1429 Expect(packet.StreamFrames).To(HaveLen(1)) 1430 Expect(packet.StreamFrames[0].Frame).To(Equal(f)) 1431 }) 1432 1433 It("packs a full size 1-RTT probe packet", func() { 1434 f := &wire.StreamFrame{Data: make([]byte, 2000)} 1435 retransmissionQueue.addInitial(f) 1436 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1437 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, false) 1438 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1439 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42)) 1440 framer.EXPECT().HasData().Return(true) 1441 expectAppendControlFrames() 1442 framer.EXPECT().AppendStreamFrames(gomock.Any(), gomock.Any(), protocol.Version1).DoAndReturn(func(fs []ackhandler.StreamFrame, maxSize protocol.ByteCount, v protocol.VersionNumber) ([]ackhandler.StreamFrame, protocol.ByteCount) { 1443 sf, split := f.MaybeSplitOffFrame(maxSize, v) 1444 Expect(split).To(BeTrue()) 1445 return append(fs, ackhandler.StreamFrame{Frame: sf}), sf.Length(v) 1446 }) 1447 1448 p, err := packer.MaybePackProbePacket(protocol.Encryption1RTT, maxPacketSize, protocol.Version1) 1449 Expect(err).ToNot(HaveOccurred()) 1450 Expect(p).ToNot(BeNil()) 1451 Expect(p.longHdrPackets).To(BeEmpty()) 1452 Expect(p.shortHdrPacket).ToNot(BeNil()) 1453 packet := p.shortHdrPacket 1454 Expect(packet.Frames).To(BeEmpty()) 1455 Expect(packet.StreamFrames).To(HaveLen(1)) 1456 Expect(packet.Length).To(Equal(maxPacketSize)) 1457 }) 1458 1459 It("returns nil if there's no probe data to send", func() { 1460 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1461 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen2) 1462 ackFramer.EXPECT().GetAckFrame(protocol.Encryption1RTT, true) 1463 framer.EXPECT().HasData() 1464 1465 packet, err := packer.MaybePackProbePacket(protocol.Encryption1RTT, maxPacketSize, protocol.Version1) 1466 Expect(err).ToNot(HaveOccurred()) 1467 Expect(packet).To(BeNil()) 1468 }) 1469 1470 It("packs an MTU probe packet", func() { 1471 sealingManager.EXPECT().Get1RTTSealer().Return(getSealer(), nil) 1472 pnManager.EXPECT().PeekPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x43), protocol.PacketNumberLen2) 1473 pnManager.EXPECT().PopPacketNumber(protocol.Encryption1RTT).Return(protocol.PacketNumber(0x43)) 1474 ping := ackhandler.Frame{Frame: &wire.PingFrame{}} 1475 const probePacketSize = maxPacketSize + 42 1476 p, buffer, err := packer.PackMTUProbePacket(ping, probePacketSize, protocol.Version1) 1477 Expect(err).ToNot(HaveOccurred()) 1478 Expect(p.Length).To(BeEquivalentTo(probePacketSize)) 1479 Expect(p.PacketNumber).To(Equal(protocol.PacketNumber(0x43))) 1480 Expect(buffer.Data).To(HaveLen(int(probePacketSize))) 1481 Expect(p.IsPathMTUProbePacket).To(BeTrue()) 1482 }) 1483 }) 1484 }) 1485 })