fix encoding of the error code in the STOP_SENDING frame

This commit is contained in:
Marten Seemann
2019-11-16 15:03:19 +08:00
parent c5b76e4f86
commit 8e7a462007
2 changed files with 10 additions and 11 deletions

View File

@@ -23,7 +23,7 @@ func parseStopSendingFrame(r *bytes.Reader, _ protocol.VersionNumber) (*StopSend
if err != nil {
return nil, err
}
errorCode, err := utils.BigEndian.ReadUint16(r)
errorCode, err := utils.ReadVarInt(r)
if err != nil {
return nil, err
}
@@ -36,12 +36,12 @@ func parseStopSendingFrame(r *bytes.Reader, _ protocol.VersionNumber) (*StopSend
// Length of a written frame
func (f *StopSendingFrame) Length(_ protocol.VersionNumber) protocol.ByteCount {
return 1 + utils.VarIntLen(uint64(f.StreamID)) + 2
return 1 + utils.VarIntLen(uint64(f.StreamID)) + utils.VarIntLen(uint64(f.ErrorCode))
}
func (f *StopSendingFrame) Write(b *bytes.Buffer, _ protocol.VersionNumber) error {
b.WriteByte(0x5)
utils.WriteVarInt(b, uint64(f.StreamID))
utils.BigEndian.WriteUint16(b, uint16(f.ErrorCode))
utils.WriteVarInt(b, uint64(f.ErrorCode))
return nil
}

View File

@@ -15,7 +15,7 @@ var _ = Describe("STOP_SENDING frame", func() {
It("parses a sample frame", func() {
data := []byte{0x5}
data = append(data, encodeVarInt(0xdecafbad)...) // stream ID
data = append(data, []byte{0x13, 0x37}...) // error code
data = append(data, encodeVarInt(0x1337)...) // error code
b := bytes.NewReader(data)
frame, err := parseStopSendingFrame(b, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
@@ -27,7 +27,7 @@ var _ = Describe("STOP_SENDING frame", func() {
It("errors on EOFs", func() {
data := []byte{0x5}
data = append(data, encodeVarInt(0xdecafbad)...) // stream ID
data = append(data, []byte{0x13, 0x37}...) // error code
data = append(data, encodeVarInt(0x123456)...) // error code
_, err := parseStopSendingFrame(bytes.NewReader(data), versionIETFFrames)
Expect(err).NotTo(HaveOccurred())
for i := range data {
@@ -41,23 +41,22 @@ var _ = Describe("STOP_SENDING frame", func() {
It("writes", func() {
frame := &StopSendingFrame{
StreamID: 0xdeadbeefcafe,
ErrorCode: 0x10,
ErrorCode: 0xdecafbad,
}
buf := &bytes.Buffer{}
err := frame.Write(buf, versionIETFFrames)
Expect(err).ToNot(HaveOccurred())
Expect(frame.Write(buf, versionIETFFrames)).To(Succeed())
expected := []byte{0x5}
expected = append(expected, encodeVarInt(0xdeadbeefcafe)...)
expected = append(expected, []byte{0x0, 0x10}...)
expected = append(expected, encodeVarInt(0xdecafbad)...)
Expect(buf.Bytes()).To(Equal(expected))
})
It("has the correct min length", func() {
frame := &StopSendingFrame{
StreamID: 0xdeadbeef,
ErrorCode: 0x10,
ErrorCode: 0x1234567,
}
Expect(frame.Length(versionIETFFrames)).To(Equal(1 + 2 + utils.VarIntLen(0xdeadbeef)))
Expect(frame.Length(versionIETFFrames)).To(Equal(1 + utils.VarIntLen(0xdeadbeef) + utils.VarIntLen(0x1234567)))
})
})
})