mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-06 21:57:36 +03:00
use a single bytes.Reader for frame parsing (#3536)
This commit is contained in:
parent
93e1d031b9
commit
dfd35cb071
8 changed files with 104 additions and 61 deletions
|
@ -1,7 +1,6 @@
|
|||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
|
@ -18,37 +17,52 @@ var _ = Describe("Frame parsing", func() {
|
|||
})
|
||||
|
||||
It("returns nil if there's nothing more to read", func() {
|
||||
f, err := parser.ParseNext(bytes.NewReader(nil), protocol.Encryption1RTT)
|
||||
l, f, err := parser.ParseNext(nil, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(l).To(BeZero())
|
||||
Expect(f).To(BeNil())
|
||||
})
|
||||
|
||||
It("skips PADDING frames", func() {
|
||||
b := []byte{0} // PADDING frame
|
||||
b := []byte{0, 0} // 2 PADDING frames
|
||||
b, err := (&PingFrame{}).Append(b, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
f, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, f, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(f).To(Equal(&PingFrame{}))
|
||||
Expect(l).To(Equal(2 + 1))
|
||||
})
|
||||
|
||||
It("handles PADDING at the end", func() {
|
||||
r := bytes.NewReader([]byte{0, 0, 0})
|
||||
f, err := parser.ParseNext(r, protocol.Encryption1RTT)
|
||||
l, f, err := parser.ParseNext([]byte{0, 0, 0}, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(f).To(BeNil())
|
||||
Expect(r.Len()).To(BeZero())
|
||||
Expect(l).To(Equal(3))
|
||||
})
|
||||
|
||||
It("parses a single frame", func() {
|
||||
var b []byte
|
||||
for i := 0; i < 10; i++ {
|
||||
var err error
|
||||
b, err = (&PingFrame{}).Append(b, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
}
|
||||
l, f, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(f).To(BeAssignableToTypeOf(&PingFrame{}))
|
||||
Expect(l).To(Equal(1))
|
||||
})
|
||||
|
||||
It("unpacks ACK frames", func() {
|
||||
f := &AckFrame{AckRanges: []AckRange{{Smallest: 1, Largest: 0x13}}}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(BeAssignableToTypeOf(f))
|
||||
Expect(frame.(*AckFrame).LargestAcked()).To(Equal(protocol.PacketNumber(0x13)))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("uses the custom ack delay exponent for 1RTT packets", func() {
|
||||
|
@ -59,7 +73,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
_, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
// The ACK frame is always written using the protocol.AckDelayExponent.
|
||||
// That's why we expect a different value when parsing.
|
||||
|
@ -74,7 +88,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.EncryptionHandshake)
|
||||
_, frame, err := parser.ParseNext(b, protocol.EncryptionHandshake)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame.(*AckFrame).DelayTime).To(Equal(time.Second))
|
||||
})
|
||||
|
@ -87,18 +101,20 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks STOP_SENDING frames", func() {
|
||||
f := &StopSendingFrame{StreamID: 0x42}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks CRYPTO frames", func() {
|
||||
|
@ -108,20 +124,22 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks NEW_TOKEN frames", func() {
|
||||
f := &NewTokenFrame{Token: []byte("foobar")}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks STREAM frames", func() {
|
||||
|
@ -133,10 +151,11 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks MAX_DATA frames", func() {
|
||||
|
@ -145,9 +164,10 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks MAX_STREAM_DATA frames", func() {
|
||||
|
@ -157,9 +177,10 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks MAX_STREAMS frames", func() {
|
||||
|
@ -169,18 +190,20 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks DATA_BLOCKED frames", func() {
|
||||
f := &DataBlockedFrame{MaximumData: 0x1234}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks STREAM_DATA_BLOCKED frames", func() {
|
||||
|
@ -190,9 +213,10 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks STREAMS_BLOCKED frames", func() {
|
||||
|
@ -202,9 +226,10 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks NEW_CONNECTION_ID frames", func() {
|
||||
|
@ -215,40 +240,44 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks RETIRE_CONNECTION_ID frames", func() {
|
||||
f := &RetireConnectionIDFrame{SequenceNumber: 0x1337}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks PATH_CHALLENGE frames", func() {
|
||||
f := &PathChallengeFrame{Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(BeAssignableToTypeOf(f))
|
||||
Expect(frame.(*PathChallengeFrame).Data).To(Equal([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks PATH_RESPONSE frames", func() {
|
||||
f := &PathResponseFrame{Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).ToNot(BeNil())
|
||||
Expect(frame).To(BeAssignableToTypeOf(f))
|
||||
Expect(frame.(*PathResponseFrame).Data).To(Equal([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks CONNECTION_CLOSE frames", func() {
|
||||
|
@ -258,27 +287,30 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks HANDSHAKE_DONE frames", func() {
|
||||
f := &HandshakeDoneFrame{}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("unpacks DATAGRAM frames", func() {
|
||||
f := &DatagramFrame{Data: []byte("foobar")}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
frame, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
l, frame, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(frame).To(Equal(f))
|
||||
Expect(l).To(Equal(len(b)))
|
||||
})
|
||||
|
||||
It("errors when DATAGRAM frames are not supported", func() {
|
||||
|
@ -286,7 +318,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
f := &DatagramFrame{Data: []byte("foobar")}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err = parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
_, _, err = parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).To(MatchError(&qerr.TransportError{
|
||||
ErrorCode: qerr.FrameEncodingError,
|
||||
FrameType: 0x30,
|
||||
|
@ -295,7 +327,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
})
|
||||
|
||||
It("errors on invalid type", func() {
|
||||
_, err := parser.ParseNext(bytes.NewReader([]byte{0x42}), protocol.Encryption1RTT)
|
||||
_, _, err := parser.ParseNext([]byte{0x42}, protocol.Encryption1RTT)
|
||||
Expect(err).To(MatchError(&qerr.TransportError{
|
||||
ErrorCode: qerr.FrameEncodingError,
|
||||
FrameType: 0x42,
|
||||
|
@ -310,7 +342,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
}
|
||||
b, err := f.Append(nil, protocol.Version1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err = parser.ParseNext(bytes.NewReader(b[:len(b)-2]), protocol.Encryption1RTT)
|
||||
_, _, err = parser.ParseNext(b[:len(b)-2], protocol.Encryption1RTT)
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.(*qerr.TransportError).ErrorCode).To(Equal(qerr.FrameEncodingError))
|
||||
})
|
||||
|
@ -352,7 +384,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
|
||||
It("rejects all frames but ACK, CRYPTO, PING and CONNECTION_CLOSE in Initial packets", func() {
|
||||
for i, b := range framesSerialized {
|
||||
_, err := parser.ParseNext(bytes.NewReader(b), protocol.EncryptionInitial)
|
||||
_, _, err := parser.ParseNext(b, protocol.EncryptionInitial)
|
||||
switch frames[i].(type) {
|
||||
case *AckFrame, *ConnectionCloseFrame, *CryptoFrame, *PingFrame:
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
@ -366,7 +398,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
|
||||
It("rejects all frames but ACK, CRYPTO, PING and CONNECTION_CLOSE in Handshake packets", func() {
|
||||
for i, b := range framesSerialized {
|
||||
_, err := parser.ParseNext(bytes.NewReader(b), protocol.EncryptionHandshake)
|
||||
_, _, err := parser.ParseNext(b, protocol.EncryptionHandshake)
|
||||
switch frames[i].(type) {
|
||||
case *AckFrame, *ConnectionCloseFrame, *CryptoFrame, *PingFrame:
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
@ -380,7 +412,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
|
||||
It("rejects all frames but ACK, CRYPTO, CONNECTION_CLOSE, NEW_TOKEN, PATH_RESPONSE and RETIRE_CONNECTION_ID in 0-RTT packets", func() {
|
||||
for i, b := range framesSerialized {
|
||||
_, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption0RTT)
|
||||
_, _, err := parser.ParseNext(b, protocol.Encryption0RTT)
|
||||
switch frames[i].(type) {
|
||||
case *AckFrame, *ConnectionCloseFrame, *CryptoFrame, *NewTokenFrame, *PathResponseFrame, *RetireConnectionIDFrame:
|
||||
Expect(err).To(BeAssignableToTypeOf(&qerr.TransportError{}))
|
||||
|
@ -394,7 +426,7 @@ var _ = Describe("Frame parsing", func() {
|
|||
|
||||
It("accepts all frame types in 1-RTT packets", func() {
|
||||
for _, b := range framesSerialized {
|
||||
_, err := parser.ParseNext(bytes.NewReader(b), protocol.Encryption1RTT)
|
||||
_, _, err := parser.ParseNext(b, protocol.Encryption1RTT)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue