uquic/internal/wire/max_stream_data_frame_test.go
Marten Seemann 11c5045065 move varint encoding / decoding to a separate package
... which is not internal.
2021-01-01 11:46:45 +08:00

63 lines
2 KiB
Go

package wire
import (
"bytes"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/quicvarint"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MAX_STREAM_DATA frame", func() {
Context("parsing", func() {
It("accepts sample frame", func() {
data := []byte{0x11}
data = append(data, encodeVarInt(0xdeadbeef)...) // Stream ID
data = append(data, encodeVarInt(0x12345678)...) // Offset
b := bytes.NewReader(data)
frame, err := parseMaxStreamDataFrame(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(frame.StreamID).To(Equal(protocol.StreamID(0xdeadbeef)))
Expect(frame.MaximumStreamData).To(Equal(protocol.ByteCount(0x12345678)))
Expect(b.Len()).To(BeZero())
})
It("errors on EOFs", func() {
data := []byte{0x11}
data = append(data, encodeVarInt(0xdeadbeef)...) // Stream ID
data = append(data, encodeVarInt(0x12345678)...) // Offset
_, err := parseMaxStreamDataFrame(bytes.NewReader(data), versionIETFFrames)
Expect(err).NotTo(HaveOccurred())
for i := range data {
_, err := parseMaxStreamDataFrame(bytes.NewReader(data[0:i]), versionIETFFrames)
Expect(err).To(HaveOccurred())
}
})
})
Context("writing", func() {
It("has proper min length", func() {
f := &MaxStreamDataFrame{
StreamID: 0x1337,
MaximumStreamData: 0xdeadbeef,
}
Expect(f.Length(protocol.VersionWhatever)).To(Equal(1 + quicvarint.VarIntLen(uint64(f.StreamID)) + quicvarint.VarIntLen(uint64(f.MaximumStreamData))))
})
It("writes a sample frame", func() {
b := &bytes.Buffer{}
f := &MaxStreamDataFrame{
StreamID: 0xdecafbad,
MaximumStreamData: 0xdeadbeefcafe42,
}
expected := []byte{0x11}
expected = append(expected, encodeVarInt(0xdecafbad)...)
expected = append(expected, encodeVarInt(0xdeadbeefcafe42)...)
err := f.Write(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(b.Bytes()).To(Equal(expected))
})
})
})