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

@ -12,12 +12,11 @@ import (
var _ = Describe("Capsule", func() {
It("parses Capsules", func() {
var buf bytes.Buffer
quicvarint.Write(&buf, 1337)
quicvarint.Write(&buf, 6)
buf.WriteString("foobar")
b := quicvarint.Append(nil, 1337)
b = quicvarint.Append(b, 6)
b = append(b, []byte("foobar")...)
ct, r, err := ParseCapsule(&buf)
ct, r, err := ParseCapsule(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(ct).To(BeEquivalentTo(1337))
val, err := io.ReadAll(r)
@ -38,14 +37,12 @@ var _ = Describe("Capsule", func() {
})
It("errors on EOF", func() {
var buf bytes.Buffer
quicvarint.Write(&buf, 1337)
quicvarint.Write(&buf, 6)
buf.WriteString("foobar")
data := buf.Bytes()
b := quicvarint.Append(nil, 1337)
b = quicvarint.Append(b, 6)
b = append(b, []byte("foobar")...)
for i := range data {
ct, r, err := ParseCapsule(bytes.NewReader(data[:i]))
for i := range b {
ct, r, err := ParseCapsule(bytes.NewReader(b[:i]))
if err != nil {
Expect(err).To(MatchError(io.ErrUnexpectedEOF))
continue

View file

@ -226,8 +226,7 @@ var _ = Describe("Client", func() {
return true, nil
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptStream(gomock.Any()).Return(unknownStr, nil)
@ -249,8 +248,7 @@ var _ = Describe("Client", func() {
return false, nil
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptStream(gomock.Any()).Return(unknownStr, nil)
@ -272,8 +270,7 @@ var _ = Describe("Client", func() {
return false, errors.New("error in hijacker")
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptStream(gomock.Any()).Return(unknownStr, nil)
@ -354,8 +351,7 @@ var _ = Describe("Client", func() {
return true
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x54)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x54))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptUniStream(gomock.Any()).DoAndReturn(func(context.Context) (quic.ReceiveStream, error) {
@ -403,8 +399,7 @@ var _ = Describe("Client", func() {
return false
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x54)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x54))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
unknownStr.EXPECT().CancelRead(quic.StreamErrorCode(errorStreamCreationError))
@ -480,8 +475,7 @@ var _ = Describe("Client", func() {
}
It(fmt.Sprintf("ignores the QPACK %s streams", name), func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, streamType)
buf := bytes.NewBuffer(quicvarint.Append(nil, streamType))
str := mockquic.NewMockStream(mockCtrl)
str.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
@ -499,8 +493,7 @@ var _ = Describe("Client", func() {
}
It("resets streams Other than the control stream and the QPACK streams", func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, 1337)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x1337))
str := mockquic.NewMockStream(mockCtrl)
str.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
done := make(chan struct{})
@ -569,8 +562,7 @@ var _ = Describe("Client", func() {
})
It("errors when parsing the server opens a push stream", func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, streamTypePushStream)
buf := bytes.NewBuffer(quicvarint.Append(nil, streamTypePushStream))
controlStr := mockquic.NewMockStream(mockCtrl)
controlStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptUniStream(gomock.Any()).DoAndReturn(func(context.Context) (quic.ReceiveStream, error) {

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)

View file

@ -260,8 +260,7 @@ var _ = Describe("Server", func() {
return true, nil
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptStream(gomock.Any()).Return(unknownStr, nil)
@ -283,8 +282,7 @@ var _ = Describe("Server", func() {
return false, nil
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
unknownStr.EXPECT().CancelWrite(quic.StreamErrorCode(errorRequestIncomplete))
@ -307,8 +305,7 @@ var _ = Describe("Server", func() {
return false, errors.New("error in hijacker")
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x41)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x41))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
unknownStr.EXPECT().CancelWrite(quic.StreamErrorCode(errorRequestIncomplete))
@ -373,8 +370,7 @@ var _ = Describe("Server", func() {
return true
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x54)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x54))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
conn.EXPECT().AcceptUniStream(gomock.Any()).DoAndReturn(func(context.Context) (quic.ReceiveStream, error) {
@ -420,8 +416,7 @@ var _ = Describe("Server", func() {
return false
}
buf := &bytes.Buffer{}
quicvarint.Write(buf, 0x54)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0x54))
unknownStr := mockquic.NewMockStream(mockCtrl)
unknownStr.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
unknownStr.EXPECT().CancelRead(quic.StreamErrorCode(errorStreamCreationError))
@ -480,8 +475,7 @@ var _ = Describe("Server", func() {
}
It(fmt.Sprintf("ignores the QPACK %s streams", name), func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, streamType)
buf := bytes.NewBuffer(quicvarint.Append(nil, streamType))
str := mockquic.NewMockStream(mockCtrl)
str.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
@ -498,8 +492,7 @@ var _ = Describe("Server", func() {
}
It("reset streams other than the control stream and the QPACK streams", func() {
buf := &bytes.Buffer{}
quicvarint.Write(buf, 1337)
buf := bytes.NewBuffer(quicvarint.Append(nil, 0o1337))
str := mockquic.NewMockStream(mockCtrl)
str.EXPECT().Read(gomock.Any()).DoAndReturn(buf.Read).AnyTimes()
done := make(chan struct{})