deprecate quicvarint.Write in favor of quicvarint.Append (#3690)

This commit is contained in:
Marten Seemann 2023-02-13 23:52:25 +13:00 committed by GitHub
parent 849e53dc3a
commit 04c3fd0756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 169 additions and 267 deletions

View file

@ -17,15 +17,9 @@ type errReader struct{ err error }
func (e errReader) Read([]byte) (int, error) { return 0, e.err }
var _ = Describe("Frames", func() {
appendVarInt := func(b []byte, val uint64) []byte {
buf := &bytes.Buffer{}
quicvarint.Write(buf, val)
return append(b, buf.Bytes()...)
}
It("skips unknown frames", func() {
b := appendVarInt(nil, 0xdeadbeef) // type byte
b = appendVarInt(b, 0x42)
b := quicvarint.Append(nil, 0xdeadbeef) // type byte
b = quicvarint.Append(b, 0x42)
b = append(b, make([]byte, 0x42)...)
b = (&dataFrame{Length: 0x1234}).Append(b)
r := bytes.NewReader(b)
@ -37,8 +31,8 @@ var _ = Describe("Frames", func() {
Context("DATA frames", func() {
It("parses", func() {
data := appendVarInt(nil, 0) // type byte
data = appendVarInt(data, 0x1337)
data := quicvarint.Append(nil, 0) // type byte
data = quicvarint.Append(data, 0x1337)
frame, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).ToNot(HaveOccurred())
Expect(frame).To(BeAssignableToTypeOf(&dataFrame{}))
@ -57,8 +51,8 @@ var _ = Describe("Frames", func() {
Context("HEADERS frames", func() {
It("parses", func() {
data := appendVarInt(nil, 1) // type byte
data = appendVarInt(data, 0x1337)
data := quicvarint.Append(nil, 1) // type byte
data = quicvarint.Append(data, 0x1337)
frame, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).ToNot(HaveOccurred())
Expect(frame).To(BeAssignableToTypeOf(&headersFrame{}))
@ -77,12 +71,12 @@ var _ = Describe("Frames", func() {
Context("SETTINGS frames", func() {
It("parses", func() {
settings := appendVarInt(nil, 13)
settings = appendVarInt(settings, 37)
settings = appendVarInt(settings, 0xdead)
settings = appendVarInt(settings, 0xbeef)
data := appendVarInt(nil, 4) // type byte
data = appendVarInt(data, uint64(len(settings)))
settings := quicvarint.Append(nil, 13)
settings = quicvarint.Append(settings, 37)
settings = quicvarint.Append(settings, 0xdead)
settings = quicvarint.Append(settings, 0xbeef)
data := quicvarint.Append(nil, 4) // type byte
data = quicvarint.Append(data, uint64(len(settings)))
data = append(data, settings...)
frame, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).ToNot(HaveOccurred())
@ -93,12 +87,12 @@ var _ = Describe("Frames", func() {
})
It("rejects duplicate settings", func() {
settings := appendVarInt(nil, 13)
settings = appendVarInt(settings, 37)
settings = appendVarInt(settings, 13)
settings = appendVarInt(settings, 38)
data := appendVarInt(nil, 4) // type byte
data = appendVarInt(data, uint64(len(settings)))
settings := quicvarint.Append(nil, 13)
settings = quicvarint.Append(settings, 37)
settings = quicvarint.Append(settings, 13)
settings = quicvarint.Append(settings, 38)
data := quicvarint.Append(nil, 4) // type byte
data = quicvarint.Append(data, uint64(len(settings)))
data = append(data, settings...)
_, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).To(MatchError("duplicate setting: 13"))
@ -135,10 +129,10 @@ var _ = Describe("Frames", func() {
Context("H3_DATAGRAM", func() {
It("reads the H3_DATAGRAM value", func() {
settings := appendVarInt(nil, settingDatagram)
settings = appendVarInt(settings, 1)
data := appendVarInt(nil, 4) // type byte
data = appendVarInt(data, uint64(len(settings)))
settings := quicvarint.Append(nil, settingDatagram)
settings = quicvarint.Append(settings, 1)
data := quicvarint.Append(nil, 4) // type byte
data = quicvarint.Append(data, uint64(len(settings)))
data = append(data, settings...)
f, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).ToNot(HaveOccurred())
@ -148,22 +142,22 @@ var _ = Describe("Frames", func() {
})
It("rejects duplicate H3_DATAGRAM entries", func() {
settings := appendVarInt(nil, settingDatagram)
settings = appendVarInt(settings, 1)
settings = appendVarInt(settings, settingDatagram)
settings = appendVarInt(settings, 1)
data := appendVarInt(nil, 4) // type byte
data = appendVarInt(data, uint64(len(settings)))
settings := quicvarint.Append(nil, settingDatagram)
settings = quicvarint.Append(settings, 1)
settings = quicvarint.Append(settings, settingDatagram)
settings = quicvarint.Append(settings, 1)
data := quicvarint.Append(nil, 4) // type byte
data = quicvarint.Append(data, uint64(len(settings)))
data = append(data, settings...)
_, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).To(MatchError(fmt.Sprintf("duplicate setting: %d", settingDatagram)))
})
It("rejects invalid values for the H3_DATAGRAM entry", func() {
settings := appendVarInt(nil, settingDatagram)
settings = appendVarInt(settings, 1337)
data := appendVarInt(nil, 4) // type byte
data = appendVarInt(data, uint64(len(settings)))
settings := quicvarint.Append(nil, settingDatagram)
settings = quicvarint.Append(settings, 1337)
data := quicvarint.Append(nil, 4) // type byte
data = quicvarint.Append(data, uint64(len(settings)))
data = append(data, settings...)
_, err := parseNextFrame(bytes.NewReader(data), nil)
Expect(err).To(MatchError("invalid value for H3_DATAGRAM: 1337"))
@ -180,8 +174,7 @@ var _ = Describe("Frames", func() {
Context("hijacking", func() {
It("reads a frame without hijacking the stream", func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, 1337)
buf := bytes.NewBuffer(quicvarint.Append(nil, 1337))
customFrameContents := []byte("foobar")
buf.Write(customFrameContents)