github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/qerr/errors_test.go (about)

     1  package qerr
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  
     8  	"github.com/apernet/quic-go/internal/protocol"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  type myError int
    15  
    16  var _ error = myError(0)
    17  
    18  func (e myError) Error() string { return fmt.Sprintf("my error %d", e) }
    19  
    20  var _ = Describe("QUIC Errors", func() {
    21  	Context("Transport Errors", func() {
    22  		It("has a string representation", func() {
    23  			Expect((&TransportError{
    24  				ErrorCode:    FlowControlError,
    25  				ErrorMessage: "foobar",
    26  			}).Error()).To(Equal("FLOW_CONTROL_ERROR (local): foobar"))
    27  		})
    28  
    29  		It("has a string representation for empty error phrases", func() {
    30  			Expect((&TransportError{ErrorCode: FlowControlError}).Error()).To(Equal("FLOW_CONTROL_ERROR (local)"))
    31  		})
    32  
    33  		It("includes the frame type, for errors without a message", func() {
    34  			Expect((&TransportError{
    35  				Remote:    true,
    36  				ErrorCode: FlowControlError,
    37  				FrameType: 0x1337,
    38  			}).Error()).To(Equal("FLOW_CONTROL_ERROR (remote) (frame type: 0x1337)"))
    39  		})
    40  
    41  		It("includes the frame type, for errors with a message", func() {
    42  			Expect((&TransportError{
    43  				ErrorCode:    FlowControlError,
    44  				FrameType:    0x1337,
    45  				ErrorMessage: "foobar",
    46  			}).Error()).To(Equal("FLOW_CONTROL_ERROR (local) (frame type: 0x1337): foobar"))
    47  		})
    48  
    49  		Context("crypto errors", func() {
    50  			It("has a string representation for errors with a message", func() {
    51  				myErr := myError(1337)
    52  				err := NewLocalCryptoError(0x42, myErr)
    53  				Expect(err.Error()).To(Equal("CRYPTO_ERROR 0x142 (local): my error 1337"))
    54  			})
    55  
    56  			It("unwraps errors", func() {
    57  				var myErr myError
    58  				err := NewLocalCryptoError(0x42, myError(1337))
    59  				Expect(errors.As(err, &myErr)).To(BeTrue())
    60  				Expect(myErr).To(BeEquivalentTo(1337))
    61  			})
    62  
    63  			It("has a string representation for errors without a message", func() {
    64  				err := NewLocalCryptoError(0x2a, nil)
    65  				Expect(err.Error()).To(Equal("CRYPTO_ERROR 0x12a (local): tls: bad certificate"))
    66  			})
    67  		})
    68  	})
    69  
    70  	Context("Application Errors", func() {
    71  		It("has a string representation for errors with a message", func() {
    72  			Expect((&ApplicationError{
    73  				ErrorCode:    0x42,
    74  				ErrorMessage: "foobar",
    75  			}).Error()).To(Equal("Application error 0x42 (local): foobar"))
    76  		})
    77  
    78  		It("has a string representation for errors without a message", func() {
    79  			Expect((&ApplicationError{
    80  				ErrorCode: 0x42,
    81  				Remote:    true,
    82  			}).Error()).To(Equal("Application error 0x42 (remote)"))
    83  		})
    84  	})
    85  
    86  	Context("timeout errors", func() {
    87  		It("handshake timeouts", func() {
    88  			//nolint:gosimple // we need to assign to an interface here
    89  			var err error
    90  			err = &HandshakeTimeoutError{}
    91  			nerr, ok := err.(net.Error)
    92  			Expect(ok).To(BeTrue())
    93  			Expect(nerr.Timeout()).To(BeTrue())
    94  			Expect(err.Error()).To(Equal("timeout: handshake did not complete in time"))
    95  		})
    96  
    97  		It("idle timeouts", func() {
    98  			//nolint:gosimple // we need to assign to an interface here
    99  			var err error
   100  			err = &IdleTimeoutError{}
   101  			nerr, ok := err.(net.Error)
   102  			Expect(ok).To(BeTrue())
   103  			Expect(nerr.Timeout()).To(BeTrue())
   104  			Expect(err.Error()).To(Equal("timeout: no recent network activity"))
   105  		})
   106  	})
   107  
   108  	Context("Version Negotiation errors", func() {
   109  		It("has a string representation", func() {
   110  			Expect((&VersionNegotiationError{
   111  				Ours:   []protocol.Version{2, 3},
   112  				Theirs: []protocol.Version{4, 5, 6},
   113  			}).Error()).To(Equal("no compatible QUIC version found (we support [0x2 0x3], server offered [0x4 0x5 0x6])"))
   114  		})
   115  	})
   116  
   117  	Context("Stateless Reset errors", func() {
   118  		token := protocol.StatelessResetToken{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
   119  
   120  		It("has a string representation", func() {
   121  			Expect((&StatelessResetError{Token: token}).Error()).To(Equal("received a stateless reset with token 000102030405060708090a0b0c0d0e0f"))
   122  		})
   123  
   124  		It("is a net.Error", func() {
   125  			//nolint:gosimple // we need to assign to an interface here
   126  			var err error
   127  			err = &StatelessResetError{}
   128  			nerr, ok := err.(net.Error)
   129  			Expect(ok).To(BeTrue())
   130  			Expect(nerr.Timeout()).To(BeFalse())
   131  		})
   132  	})
   133  
   134  	It("says that errors are net.ErrClosed errors", func() {
   135  		Expect(errors.Is(&TransportError{}, net.ErrClosed)).To(BeTrue())
   136  		Expect(errors.Is(&ApplicationError{}, net.ErrClosed)).To(BeTrue())
   137  		Expect(errors.Is(&IdleTimeoutError{}, net.ErrClosed)).To(BeTrue())
   138  		Expect(errors.Is(&HandshakeTimeoutError{}, net.ErrClosed)).To(BeTrue())
   139  		Expect(errors.Is(&StatelessResetError{}, net.ErrClosed)).To(BeTrue())
   140  		Expect(errors.Is(&VersionNegotiationError{}, net.ErrClosed)).To(BeTrue())
   141  	})
   142  })