diff --git a/internal/qerr/errorcodes_test.go b/internal/qerr/errorcodes_test.go index 4fce7b0e..d3dcd53b 100644 --- a/internal/qerr/errorcodes_test.go +++ b/internal/qerr/errorcodes_test.go @@ -7,46 +7,41 @@ import ( "path" "runtime" "strconv" + "testing" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" ) -var _ = Describe("error codes", func() { - // If this test breaks, you should run `go generate ./...` - It("has a string representation for every error code", func() { - // We parse the error code file, extract all constants, and verify that - // each of them has a string version. Go FTW! - _, thisfile, _, ok := runtime.Caller(0) - if !ok { - panic("Failed to get current frame") - } - filename := path.Join(path.Dir(thisfile), "error_codes.go") - fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0) - Expect(err).NotTo(HaveOccurred()) - constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs - Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing - for _, c := range constSpecs { - valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value - val, err := strconv.ParseInt(valString, 0, 64) - Expect(err).NotTo(HaveOccurred()) - Expect(TransportErrorCode(val).String()).ToNot(Equal("unknown error code")) - } - }) +func TestTransportErrorStringer(t *testing.T) { + _, thisfile, _, ok := runtime.Caller(0) + require.True(t, ok, "Failed to get current frame") - It("has a string representation for unknown error codes", func() { - Expect(TransportErrorCode(0x1337).String()).To(Equal("unknown error code: 0x1337")) - }) + filename := path.Join(path.Dir(thisfile), "error_codes.go") + fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0) + require.NoError(t, err) - It("says if an error is a crypto error", func() { - for i := 0; i < 0x100; i++ { - Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse()) - } - for i := 0x100; i < 0x200; i++ { - Expect(TransportErrorCode(i).IsCryptoError()).To(BeTrue()) - } - for i := 0x200; i < 0x300; i++ { - Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse()) - } - }) -}) + constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs + require.Greater(t, len(constSpecs), 4, "Expected more than 4 constants") + + for _, c := range constSpecs { + valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value + val, err := strconv.ParseInt(valString, 0, 64) + require.NoError(t, err) + require.NotEqual(t, "unknown error code", TransportErrorCode(val).String()) + } + + // test that there's a string representation for unknown error codes + require.Equal(t, "unknown error code: 0x1337", TransportErrorCode(0x1337).String()) +} + +func TestIsCryptoError(t *testing.T) { + for i := 0; i < 0x100; i++ { + require.False(t, TransportErrorCode(i).IsCryptoError()) + } + for i := 0x100; i < 0x200; i++ { + require.True(t, TransportErrorCode(i).IsCryptoError()) + } + for i := 0x200; i < 0x300; i++ { + require.False(t, TransportErrorCode(i).IsCryptoError()) + } +} diff --git a/internal/qerr/errors_suite_test.go b/internal/qerr/errors_suite_test.go deleted file mode 100644 index 801e9508..00000000 --- a/internal/qerr/errors_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package qerr - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestErrorcodes(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Errors Suite") -} diff --git a/internal/qerr/errors_test.go b/internal/qerr/errors_test.go index 0eeb0408..fabac98d 100644 --- a/internal/qerr/errors_test.go +++ b/internal/qerr/errors_test.go @@ -4,139 +4,138 @@ import ( "errors" "fmt" "net" + "testing" "github.com/quic-go/quic-go/internal/protocol" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" ) +func TestTransportErrorStriner(t *testing.T) { + t.Run("with error message", func(t *testing.T) { + err := &TransportError{ + ErrorCode: FlowControlError, + ErrorMessage: "foobar", + } + require.Equal(t, "FLOW_CONTROL_ERROR (local): foobar", err.Error()) + }) + + t.Run("without error message", func(t *testing.T) { + err := &TransportError{ErrorCode: FlowControlError} + require.Equal(t, "FLOW_CONTROL_ERROR (local)", err.Error()) + }) + + t.Run("with frame type", func(t *testing.T) { + err := &TransportError{ + Remote: true, + ErrorCode: FlowControlError, + FrameType: 0x1337, + } + require.Equal(t, "FLOW_CONTROL_ERROR (remote) (frame type: 0x1337)", err.Error()) + }) + + t.Run("with frame type and error message", func(t *testing.T) { + err := &TransportError{ + ErrorCode: FlowControlError, + FrameType: 0x1337, + ErrorMessage: "foobar", + } + require.Equal(t, "FLOW_CONTROL_ERROR (local) (frame type: 0x1337): foobar", err.Error()) + }) +} + type myError int var _ error = myError(0) func (e myError) Error() string { return fmt.Sprintf("my error %d", e) } -var _ = Describe("QUIC Errors", func() { - Context("Transport Errors", func() { - It("has a string representation", func() { - Expect((&TransportError{ - ErrorCode: FlowControlError, - ErrorMessage: "foobar", - }).Error()).To(Equal("FLOW_CONTROL_ERROR (local): foobar")) - }) +func TestCryptoErrorUnwrapsErrors(t *testing.T) { + var myErr myError + err := NewLocalCryptoError(0x42, myError(1337)) + require.True(t, errors.As(err, &myErr)) + require.Equal(t, myError(1337), myErr) +} - It("has a string representation for empty error phrases", func() { - Expect((&TransportError{ErrorCode: FlowControlError}).Error()).To(Equal("FLOW_CONTROL_ERROR (local)")) - }) - - It("includes the frame type, for errors without a message", func() { - Expect((&TransportError{ - Remote: true, - ErrorCode: FlowControlError, - FrameType: 0x1337, - }).Error()).To(Equal("FLOW_CONTROL_ERROR (remote) (frame type: 0x1337)")) - }) - - It("includes the frame type, for errors with a message", func() { - Expect((&TransportError{ - ErrorCode: FlowControlError, - FrameType: 0x1337, - ErrorMessage: "foobar", - }).Error()).To(Equal("FLOW_CONTROL_ERROR (local) (frame type: 0x1337): foobar")) - }) - - Context("crypto errors", func() { - It("has a string representation for errors with a message", func() { - myErr := myError(1337) - err := NewLocalCryptoError(0x42, myErr) - Expect(err.Error()).To(Equal("CRYPTO_ERROR 0x142 (local): my error 1337")) - }) - - It("unwraps errors", func() { - var myErr myError - err := NewLocalCryptoError(0x42, myError(1337)) - Expect(errors.As(err, &myErr)).To(BeTrue()) - Expect(myErr).To(BeEquivalentTo(1337)) - }) - - It("has a string representation for errors without a message", func() { - err := NewLocalCryptoError(0x2a, nil) - Expect(err.Error()).To(Equal("CRYPTO_ERROR 0x12a (local): tls: bad certificate")) - }) - }) +func TestCryptoErrorStringRepresentation(t *testing.T) { + t.Run("with error message", func(t *testing.T) { + myErr := myError(1337) + err := NewLocalCryptoError(0x42, myErr) + require.Equal(t, "CRYPTO_ERROR 0x142 (local): my error 1337", err.Error()) }) - Context("Application Errors", func() { - It("has a string representation for errors with a message", func() { - Expect((&ApplicationError{ - ErrorCode: 0x42, - ErrorMessage: "foobar", - }).Error()).To(Equal("Application error 0x42 (local): foobar")) - }) + t.Run("without error message", func(t *testing.T) { + err := NewLocalCryptoError(0x2a, nil) + require.Equal(t, "CRYPTO_ERROR 0x12a (local): tls: bad certificate", err.Error()) + }) +} - It("has a string representation for errors without a message", func() { - Expect((&ApplicationError{ - ErrorCode: 0x42, - Remote: true, - }).Error()).To(Equal("Application error 0x42 (remote)")) - }) +func TestApplicationError(t *testing.T) { + t.Run("with error message", func(t *testing.T) { + err := &ApplicationError{ + ErrorCode: 0x42, + ErrorMessage: "foobar", + } + require.Equal(t, "Application error 0x42 (local): foobar", err.Error()) }) - Context("timeout errors", func() { - It("handshake timeouts", func() { - //nolint:gosimple // we need to assign to an interface here - var err error - err = &HandshakeTimeoutError{} - nerr, ok := err.(net.Error) - Expect(ok).To(BeTrue()) - Expect(nerr.Timeout()).To(BeTrue()) - Expect(err.Error()).To(Equal("timeout: handshake did not complete in time")) - }) - - It("idle timeouts", func() { - //nolint:gosimple // we need to assign to an interface here - var err error - err = &IdleTimeoutError{} - nerr, ok := err.(net.Error) - Expect(ok).To(BeTrue()) - Expect(nerr.Timeout()).To(BeTrue()) - Expect(err.Error()).To(Equal("timeout: no recent network activity")) - }) + t.Run("without error message", func(t *testing.T) { + err := &ApplicationError{ + ErrorCode: 0x42, + Remote: true, + } + require.Equal(t, "Application error 0x42 (remote)", err.Error()) }) +} - Context("Version Negotiation errors", func() { - It("has a string representation", func() { - Expect((&VersionNegotiationError{ - Ours: []protocol.Version{2, 3}, - Theirs: []protocol.Version{4, 5, 6}, - }).Error()).To(Equal("no compatible QUIC version found (we support [0x2 0x3], server offered [0x4 0x5 0x6])")) - }) - }) +func TestHandshakeTimeoutError(t *testing.T) { + //nolint:gosimple // we need to assign to an interface here + var err error + err = &HandshakeTimeoutError{} + nerr, ok := err.(net.Error) + require.True(t, ok) + require.True(t, nerr.Timeout()) + require.Equal(t, "timeout: handshake did not complete in time", err.Error()) +} - Context("Stateless Reset errors", func() { - token := protocol.StatelessResetToken{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf} +func TestIdleTimeoutError(t *testing.T) { + //nolint:gosimple // we need to assign to an interface here + var err error + err = &IdleTimeoutError{} + nerr, ok := err.(net.Error) + require.True(t, ok) + require.True(t, nerr.Timeout()) + require.Equal(t, "timeout: no recent network activity", err.Error()) +} - It("has a string representation", func() { - Expect((&StatelessResetError{Token: token}).Error()).To(Equal("received a stateless reset with token 000102030405060708090a0b0c0d0e0f")) - }) +func TestVersionNegotiationErrorString(t *testing.T) { + err := &VersionNegotiationError{ + Ours: []protocol.Version{2, 3}, + Theirs: []protocol.Version{4, 5, 6}, + } + require.Equal(t, "no compatible QUIC version found (we support [0x2 0x3], server offered [0x4 0x5 0x6])", err.Error()) +} - It("is a net.Error", func() { - //nolint:gosimple // we need to assign to an interface here - var err error - err = &StatelessResetError{} - nerr, ok := err.(net.Error) - Expect(ok).To(BeTrue()) - Expect(nerr.Timeout()).To(BeFalse()) - }) - }) +func TestStatelessResetErrorString(t *testing.T) { + token := protocol.StatelessResetToken{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf} + err := &StatelessResetError{Token: token} + require.Equal(t, "received a stateless reset with token 000102030405060708090a0b0c0d0e0f", err.Error()) +} - It("says that errors are net.ErrClosed errors", func() { - Expect(errors.Is(&TransportError{}, net.ErrClosed)).To(BeTrue()) - Expect(errors.Is(&ApplicationError{}, net.ErrClosed)).To(BeTrue()) - Expect(errors.Is(&IdleTimeoutError{}, net.ErrClosed)).To(BeTrue()) - Expect(errors.Is(&HandshakeTimeoutError{}, net.ErrClosed)).To(BeTrue()) - Expect(errors.Is(&StatelessResetError{}, net.ErrClosed)).To(BeTrue()) - Expect(errors.Is(&VersionNegotiationError{}, net.ErrClosed)).To(BeTrue()) - }) -}) +func TestStatelessResetErrorIsNetError(t *testing.T) { + //nolint:gosimple // we need to assign to an interface here + var err error + err = &StatelessResetError{} + nerr, ok := err.(net.Error) + require.True(t, ok) + require.False(t, nerr.Timeout()) +} + +func TestErrorsAreNetErrClosed(t *testing.T) { + require.True(t, errors.Is(&TransportError{}, net.ErrClosed)) + require.True(t, errors.Is(&ApplicationError{}, net.ErrClosed)) + require.True(t, errors.Is(&IdleTimeoutError{}, net.ErrClosed)) + require.True(t, errors.Is(&HandshakeTimeoutError{}, net.ErrClosed)) + require.True(t, errors.Is(&StatelessResetError{}, net.ErrClosed)) + require.True(t, errors.Is(&VersionNegotiationError{}, net.ErrClosed)) +}