improve error handling in ConnectionParametersManager

This commit is contained in:
Marten Seemann
2016-05-14 17:30:36 +07:00
parent 878d6a7140
commit 2ac53843ed
2 changed files with 31 additions and 8 deletions

View File

@@ -26,8 +26,10 @@ type ConnectionParametersManager struct {
receiveConnectionFlowControlWindow protocol.ByteCount receiveConnectionFlowControlWindow protocol.ByteCount
} }
// ErrTagNotInConnectionParameterMap is returned when a tag is not present in the connection parameters var errTagNotInConnectionParameterMap = errors.New("ConnectionParametersManager: Tag not found in ConnectionsParameter map")
var ErrTagNotInConnectionParameterMap = errors.New("Tag not found in ConnectionsParameter map")
// ErrMalformedTag is returned when the tag value cannot be read
var ErrMalformedTag = errors.New("ConnectionParametersManager: malformed Tag value")
// NewConnectionParamatersManager creates a new connection parameters manager // NewConnectionParamatersManager creates a new connection parameters manager
func NewConnectionParamatersManager() *ConnectionParametersManager { func NewConnectionParamatersManager() *ConnectionParametersManager {
@@ -53,25 +55,25 @@ func (h *ConnectionParametersManager) SetFromMap(params map[Tag][]byte) error {
case TagMSPC: case TagMSPC:
clientValue, err := utils.ReadUint32(bytes.NewBuffer(value)) clientValue, err := utils.ReadUint32(bytes.NewBuffer(value))
if err != nil { if err != nil {
return err return ErrMalformedTag
} }
h.maxStreamsPerConnection = h.negotiateMaxStreamsPerConnection(clientValue) h.maxStreamsPerConnection = h.negotiateMaxStreamsPerConnection(clientValue)
case TagICSL: case TagICSL:
clientValue, err := utils.ReadUint32(bytes.NewBuffer(value)) clientValue, err := utils.ReadUint32(bytes.NewBuffer(value))
if err != nil { if err != nil {
return err return ErrMalformedTag
} }
h.idleConnectionStateLifetime = h.negotiateIdleConnectionStateLifetime(time.Duration(clientValue) * time.Second) h.idleConnectionStateLifetime = h.negotiateIdleConnectionStateLifetime(time.Duration(clientValue) * time.Second)
case TagSFCW: case TagSFCW:
sendStreamFlowControlWindow, err := utils.ReadUint32(bytes.NewBuffer(value)) sendStreamFlowControlWindow, err := utils.ReadUint32(bytes.NewBuffer(value))
if err != nil { if err != nil {
return err return ErrMalformedTag
} }
h.sendStreamFlowControlWindow = protocol.ByteCount(sendStreamFlowControlWindow) h.sendStreamFlowControlWindow = protocol.ByteCount(sendStreamFlowControlWindow)
case TagCFCW: case TagCFCW:
sendConnectionFlowControlWindow, err := utils.ReadUint32(bytes.NewBuffer(value)) sendConnectionFlowControlWindow, err := utils.ReadUint32(bytes.NewBuffer(value))
if err != nil { if err != nil {
return err return ErrMalformedTag
} }
h.sendConnectionFlowControlWindow = protocol.ByteCount(sendConnectionFlowControlWindow) h.sendConnectionFlowControlWindow = protocol.ByteCount(sendConnectionFlowControlWindow)
} }
@@ -96,7 +98,7 @@ func (h *ConnectionParametersManager) getRawValue(tag Tag) ([]byte, error) {
h.mutex.RUnlock() h.mutex.RUnlock()
if !ok { if !ok {
return nil, ErrTagNotInConnectionParameterMap return nil, errTagNotInConnectionParameterMap
} }
return rawValue, nil return rawValue, nil
} }

View File

@@ -30,7 +30,7 @@ var _ = Describe("ConnectionsParameterManager", func() {
It("returns an error for a tag that is not set", func() { It("returns an error for a tag that is not set", func() {
_, err := cpm.getRawValue(TagKEXS) _, err := cpm.getRawValue(TagKEXS)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ErrTagNotInConnectionParameterMap)) Expect(err).To(Equal(errTagNotInConnectionParameterMap))
}) })
Context("SHLO", func() { Context("SHLO", func() {
@@ -115,6 +115,7 @@ var _ = Describe("ConnectionsParameterManager", func() {
} }
err := cpm.SetFromMap(values) err := cpm.SetFromMap(values)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ErrMalformedTag))
Expect(cpm.GetSendStreamFlowControlWindow()).To(Equal(protocol.InitialStreamFlowControlWindow)) Expect(cpm.GetSendStreamFlowControlWindow()).To(Equal(protocol.InitialStreamFlowControlWindow))
}) })
@@ -133,6 +134,7 @@ var _ = Describe("ConnectionsParameterManager", func() {
} }
err := cpm.SetFromMap(values) err := cpm.SetFromMap(values)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ErrMalformedTag))
Expect(cpm.GetSendStreamFlowControlWindow()).To(Equal(protocol.InitialConnectionFlowControlWindow)) Expect(cpm.GetSendStreamFlowControlWindow()).To(Equal(protocol.InitialConnectionFlowControlWindow))
}) })
}) })
@@ -160,6 +162,16 @@ var _ = Describe("ConnectionsParameterManager", func() {
Expect(cpm.GetIdleConnectionStateLifetime()).To(Equal(10 * time.Second)) Expect(cpm.GetIdleConnectionStateLifetime()).To(Equal(10 * time.Second))
}) })
It("does not change the idle connection state lifetime when given an invalid value", func() {
values := map[Tag][]byte{
TagSFCW: []byte{0xDE, 0xAD, 0xBE}, // 1 byte too short
}
err := cpm.SetFromMap(values)
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ErrMalformedTag))
Expect(cpm.GetIdleConnectionStateLifetime()).To(Equal(protocol.InitialIdleConnectionStateLifetime))
})
It("gets idle connection state lifetime", func() { It("gets idle connection state lifetime", func() {
value := 0xDECAFBAD * time.Second value := 0xDECAFBAD * time.Second
cpm.idleConnectionStateLifetime = value cpm.idleConnectionStateLifetime = value
@@ -186,6 +198,15 @@ var _ = Describe("ConnectionsParameterManager", func() {
Expect(cpm.GetMaxStreamsPerConnection()).To(Equal(uint32(2))) Expect(cpm.GetMaxStreamsPerConnection()).To(Equal(uint32(2)))
}) })
It("errors when given an invalid max streams per connection value", func() {
values := map[Tag][]byte{
TagMSPC: []byte{2, 0, 0}, // 1 byte too short
}
err := cpm.SetFromMap(values)
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ErrMalformedTag))
})
It("gets the max streams per connection value", func() { It("gets the max streams per connection value", func() {
var value uint32 = 0xDECAFBAD var value uint32 = 0xDECAFBAD
cpm.maxStreamsPerConnection = value cpm.maxStreamsPerConnection = value