mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 20:57:36 +03:00
add tests for packet unpacker and fix a couple of bugs :)
This commit is contained in:
parent
7efc7f79d2
commit
b3e88f8019
2 changed files with 136 additions and 9 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/crypto"
|
||||
"github.com/lucas-clemente/quic-go/frames"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
@ -16,29 +17,150 @@ var _ = Describe("Packet unpacker", func() {
|
|||
hdrBin []byte
|
||||
aead crypto.AEAD
|
||||
r *bytes.Reader
|
||||
buf *bytes.Buffer
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
aead = &crypto.NullAEAD{}
|
||||
hdr = &PublicHeader{
|
||||
PacketNumber: 1,
|
||||
PacketNumber: 1,
|
||||
PacketNumberLen: 1,
|
||||
}
|
||||
hdrBin = []byte{0x04, 0x4c, 0x01}
|
||||
unpacker = &packetUnpacker{aead: aead}
|
||||
r = nil
|
||||
buf = &bytes.Buffer{}
|
||||
})
|
||||
|
||||
setReader := func(data []byte) {
|
||||
var b bytes.Buffer
|
||||
b.Write(aead.Seal(0, hdrBin, data))
|
||||
r = bytes.NewReader(b.Bytes())
|
||||
r = bytes.NewReader(aead.Seal(0, hdrBin, append([]byte{0x01}, data...)))
|
||||
}
|
||||
|
||||
It("unpacks empty packets", func() {
|
||||
setReader([]byte{0x01})
|
||||
setReader(nil)
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.entropyBit).To(BeTrue())
|
||||
Expect(packet.frames).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("unpacks stream frames", func() {
|
||||
f := &frames.StreamFrame{
|
||||
StreamID: 1,
|
||||
Data: []byte("foobar"),
|
||||
}
|
||||
err := f.Write(buf)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
setReader(buf.Bytes())
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{f}))
|
||||
})
|
||||
|
||||
It("unpacks ack frames", func() {
|
||||
f := &frames.AckFrame{
|
||||
LargestObserved: 1,
|
||||
DelayTime: 1,
|
||||
}
|
||||
err := f.Write(buf)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
setReader(buf.Bytes())
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{f}))
|
||||
})
|
||||
|
||||
It("unpacks ack frames", func() {
|
||||
f := &frames.AckFrame{
|
||||
LargestObserved: 1,
|
||||
DelayTime: 1,
|
||||
}
|
||||
err := f.Write(buf)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
setReader(buf.Bytes())
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{f}))
|
||||
})
|
||||
|
||||
It("errors on CONGESTION_FEEDBACK frames", func() {
|
||||
setReader([]byte{0x20})
|
||||
_, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).To(MatchError("unimplemented: CONGESTION_FEEDBACK"))
|
||||
})
|
||||
|
||||
It("handles pad frames", func() {
|
||||
setReader([]byte{0, 0, 0})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("unpacks RST_STREAM frames", func() {
|
||||
setReader([]byte{0x01, 0xEF, 0xBE, 0xAD, 0xDE, 0x44, 0x33, 0x22, 0x11, 0xAD, 0xFB, 0xCA, 0xDE, 0x34, 0x12, 0x37, 0x13})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{
|
||||
&frames.RstStreamFrame{
|
||||
StreamID: 0xDEADBEEF,
|
||||
ByteOffset: 0xDECAFBAD11223344,
|
||||
ErrorCode: 0x13371234,
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
It("unpacks CONNECTION_CLOSE frames", func() {
|
||||
f := &frames.ConnectionCloseFrame{ReasonPhrase: "foo"}
|
||||
err := f.Write(buf)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
setReader(buf.Bytes())
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{f}))
|
||||
})
|
||||
|
||||
It("errors on GOAWAY frames", func() {
|
||||
setReader([]byte{0x03})
|
||||
_, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).To(MatchError("unimplemented: GOAWAY"))
|
||||
})
|
||||
|
||||
It("accepts WINDOW_UPDATE frames", func() {
|
||||
setReader([]byte{0x04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("accepts BLOCKED frames", func() {
|
||||
setReader([]byte{0x05, 0, 0, 0, 0})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("unpacks STOP_WAITING frames", func() {
|
||||
setReader([]byte{0x06, 0xA4, 0x03})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(Equal([]frames.Frame{
|
||||
&frames.StopWaitingFrame{
|
||||
Entropy: 0xA4,
|
||||
LeastUnackedDelta: 0x03,
|
||||
},
|
||||
}))
|
||||
})
|
||||
|
||||
It("accepts PING frames", func() {
|
||||
setReader([]byte{0x07})
|
||||
packet, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.frames).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("errors on invalid type", func() {
|
||||
setReader([]byte{0x08})
|
||||
_, err := unpacker.Unpack(hdrBin, hdr, r)
|
||||
Expect(err).To(MatchError("unknown type byte 0x8"))
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue