File: errors_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (176 lines) | stat: -rw-r--r-- 5,742 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package qerr

import (
	"errors"
	"fmt"
	"net"
	"testing"

	"github.com/quic-go/quic-go/internal/protocol"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestTransportError(t *testing.T) {
	require.True(t, errors.Is(&TransportError{}, net.ErrClosed))

	require.True(t, errors.Is(
		&TransportError{Remote: true, ErrorCode: 1, FrameType: 2},
		&TransportError{Remote: true, ErrorCode: 1, FrameType: 2},
	))
	require.True(t, errors.Is(&TransportError{ErrorCode: 0x101}, &TransportError{ErrorCode: 0x101}))
	require.False(t, errors.Is(&TransportError{}, &TransportError{ErrorCode: 0x101}))
	require.False(t, errors.Is(&TransportError{}, &TransportError{FrameType: 0x1337}))
	require.False(t, errors.Is(&TransportError{Remote: false}, &TransportError{Remote: true}))
}

func TestTransportErrorStringer(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) }

func TestCryptoError(t *testing.T) {
	var myErr myError
	err := NewLocalCryptoError(0x42, myError(1337))
	require.True(t, errors.As(err, &myErr))
	require.Equal(t, myError(1337), myErr)

	err = NewLocalCryptoError(0x42, assert.AnError)
	require.True(t, errors.Is(err, assert.AnError))
	require.True(t, errors.Is(
		NewLocalCryptoError(0x42, assert.AnError),
		NewLocalCryptoError(0x42, assert.AnError),
	))
	require.False(t, errors.Is(
		NewLocalCryptoError(0x42, assert.AnError),
		NewLocalCryptoError(0x43, assert.AnError),
	))
}

func TestCryptoErrorStringer(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())
	})

	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())
	})
}

func TestApplicationError(t *testing.T) {
	require.True(t, errors.Is(&ApplicationError{}, net.ErrClosed))

	require.True(t, errors.Is(
		&ApplicationError{ErrorCode: 1, Remote: true},
		&ApplicationError{ErrorCode: 1, Remote: true},
	))
	require.True(t, errors.Is(&ApplicationError{ErrorCode: 0x101}, &ApplicationError{ErrorCode: 0x101}))
	require.False(t, errors.Is(&ApplicationError{}, &ApplicationError{ErrorCode: 0x101}))
	require.False(t, errors.Is(&ApplicationError{Remote: false}, &ApplicationError{Remote: true}))
}

func TestApplicationErrorStringer(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())
	})

	t.Run("without error message", func(t *testing.T) {
		err := &ApplicationError{
			ErrorCode: 0x42,
			Remote:    true,
		}
		require.Equal(t, "Application error 0x42 (remote)", err.Error())
	})
}

func TestHandshakeTimeoutError(t *testing.T) {
	require.True(t, errors.Is(&HandshakeTimeoutError{}, &HandshakeTimeoutError{}))
	require.False(t, errors.Is(&HandshakeTimeoutError{}, &IdleTimeoutError{}))

	//nolint:staticcheck // SA1021: 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())
	require.True(t, errors.Is(&HandshakeTimeoutError{}, net.ErrClosed))
}

func TestIdleTimeoutError(t *testing.T) {
	require.True(t, errors.Is(&IdleTimeoutError{}, &IdleTimeoutError{}))
	require.False(t, errors.Is(&IdleTimeoutError{}, &HandshakeTimeoutError{}))

	//nolint:staticcheck // SA1021: 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())
	require.True(t, errors.Is(&IdleTimeoutError{}, net.ErrClosed))
}

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())
	require.True(t, errors.Is(&VersionNegotiationError{}, net.ErrClosed))
}

func TestStatelessResetError(t *testing.T) {
	require.Equal(t, "received a stateless reset", (&StatelessResetError{}).Error())
	require.True(t, errors.Is(&StatelessResetError{}, &StatelessResetError{}))

	//nolint:staticcheck // SA1021: 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())
	require.True(t, errors.Is(&StatelessResetError{}, net.ErrClosed))
}