improve main package test coverage

ref #241
This commit is contained in:
Lucas Clemente 2016-08-02 11:49:25 +02:00
parent 5a00fd89f6
commit 0e05534909
5 changed files with 85 additions and 11 deletions

View file

@ -204,4 +204,21 @@ var _ = Describe("Packet unpacker", func() {
_, err := unpacker.Unpack(hdrBin, hdr, data)
Expect(err).To(MatchError("InvalidFrameData: unknown type byte 0x8"))
})
It("errors on invalid frames", func() {
for b, e := range map[byte]qerr.ErrorCode{
0x80: qerr.InvalidStreamData,
0x40: qerr.InvalidAckData,
0x01: qerr.InvalidRstStreamData,
0x02: qerr.InvalidConnectionCloseData,
0x03: qerr.InvalidGoawayData,
0x04: qerr.InvalidWindowUpdateData,
0x05: qerr.InvalidBlockedData,
0x06: qerr.InvalidStopWaitingData,
} {
setData([]byte{b})
_, err := unpacker.Unpack(hdrBin, hdr, data)
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(e))
}
})
})

View file

@ -142,10 +142,7 @@ func (s *Server) handlePacket(conn *net.UDPConn, remoteAddr *net.UDPAddr, packet
if hdr.VersionFlag && !protocol.IsSupportedVersion(hdr.VersionNumber) {
utils.Infof("Client offered version %d, sending VersionNegotiationPacket", hdr.VersionNumber)
_, err = conn.WriteToUDP(composeVersionNegotiation(hdr.ConnectionID), remoteAddr)
if err != nil {
return err
}
return nil
return err
}
s.sessionsMutex.RLock()

View file

@ -1,11 +1,13 @@
package quic
import (
"bytes"
"net"
"github.com/lucas-clemente/quic-go/crypto"
"github.com/lucas-clemente/quic-go/handshake"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
"github.com/lucas-clemente/quic-go/testdata"
. "github.com/onsi/ginkgo"
@ -88,6 +90,24 @@ var _ = Describe("Server", func() {
Expect(err).NotTo(HaveOccurred())
Expect(session.closed).To(BeTrue())
})
It("ignores packets for closed sessions", func() {
server.sessions[0x4cfa9f9b668619f6] = nil
err := server.handlePacket(nil, nil, []byte{0x08, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c, 0x01})
Expect(err).ToNot(HaveOccurred())
Expect(server.sessions).To(HaveLen(1))
Expect(server.sessions[0x4cfa9f9b668619f6]).To(BeNil())
})
It("errors on invalid public header", func() {
err := server.handlePacket(nil, nil, nil)
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.InvalidPacketHeader))
})
It("errors on large packets", func() {
err := server.handlePacket(nil, nil, bytes.Repeat([]byte{'a'}, int(protocol.MaxPacketSize)+1))
Expect(err).To(MatchError(qerr.PacketTooLarge))
})
})
It("setups and responds with version negotiation", func(done Done) {

View file

@ -306,6 +306,14 @@ var _ = Describe("Session", func() {
})
Expect(err).To(MatchError(errRstStreamOnInvalidStream))
})
It("ignores the error when the stream is not known", func() {
err := session.handleFrames([]frames.Frame{&frames.RstStreamFrame{
StreamID: 5,
ErrorCode: 42,
}})
Expect(err).NotTo(HaveOccurred())
})
})
Context("handling WINDOW_UPDATE frames", func() {
@ -346,6 +354,43 @@ var _ = Describe("Session", func() {
})
Expect(err).To(MatchError(errWindowUpdateOnClosedStream))
})
It("ignores errors when receiving a WindowUpdateFrame for a closed stream", func() {
session.streams[5] = nil // this is what the garbageCollectStreams() does when a Stream is closed
err := session.handleFrames([]frames.Frame{&frames.WindowUpdateFrame{
StreamID: 5,
ByteOffset: 1337,
}})
Expect(err).NotTo(HaveOccurred())
})
})
It("handles PING frames", func() {
err := session.handleFrames([]frames.Frame{&frames.PingFrame{}})
Expect(err).NotTo(HaveOccurred())
})
It("handles BLOCKED frames", func() {
err := session.handleFrames([]frames.Frame{&frames.BlockedFrame{}})
Expect(err).NotTo(HaveOccurred())
})
It("errors on GOAWAY frames", func() {
err := session.handleFrames([]frames.Frame{&frames.GoawayFrame{}})
Expect(err).To(MatchError("unimplemented: handling GOAWAY frames"))
})
It("handles STOP_WAITING frames", func() {
err := session.handleFrames([]frames.Frame{&frames.StopWaitingFrame{LeastUnacked: 10}})
Expect(err).NotTo(HaveOccurred())
})
It("handles CONNECTION_CLOSE frames", func() {
str, _ := session.OpenStream(5)
err := session.handleFrames([]frames.Frame{&frames.ConnectionCloseFrame{ErrorCode: 42, ReasonPhrase: "foobar"}})
Expect(err).NotTo(HaveOccurred())
_, err = str.Read([]byte{0})
Expect(err).To(MatchError(qerr.Error(42, "foobar")))
})
Context("closing", func() {

View file

@ -13,11 +13,6 @@ import (
"github.com/lucas-clemente/quic-go/utils"
)
var (
errFlowControlViolation = qerr.FlowControlReceivedTooMuchData
errConnectionFlowControlViolation = qerr.FlowControlReceivedTooMuchData
)
// A Stream assembles the data from StreamFrames and provides a super-convenient Read-Interface
//
// Read() and Write() may be called concurrently, but multiple calls to Read() or Write() individually must be synchronized manually.
@ -203,10 +198,10 @@ func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error {
err := s.flowControlManager.UpdateHighestReceived(s.streamID, maxOffset)
if err == flowcontrol.ErrStreamFlowControlViolation {
return errFlowControlViolation
return qerr.FlowControlReceivedTooMuchData
}
if err == flowcontrol.ErrConnectionFlowControlViolation {
return errConnectionFlowControlViolation
return qerr.FlowControlReceivedTooMuchData
}
if err != nil {
return err