-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy patherrors_test.go
More file actions
74 lines (65 loc) · 2.62 KB
/
Copy patherrors_test.go
File metadata and controls
74 lines (65 loc) · 2.62 KB
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
package webtransport
import (
"math"
"math/rand/v2"
"testing"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)
func TestErrorCodeRoundTrip(t *testing.T) {
for range int(1e4) {
n := StreamErrorCode(rand.Int64())
httpCode := webtransportCodeToHTTPCode(n)
errorCode, err := httpCodeToWebtransportCode(httpCode)
require.NoError(t, err)
require.Equal(t, n, errorCode)
}
}
func TestErrorCodeConversionErrors(t *testing.T) {
t.Run("too small", func(t *testing.T) {
first, err := httpCodeToWebtransportCode(firstErrorCode)
require.NoError(t, err)
require.Zero(t, first)
_, err = httpCodeToWebtransportCode(firstErrorCode - 1)
require.EqualError(t, err, "error code outside of expected range")
})
t.Run("too large", func(t *testing.T) {
last, err := httpCodeToWebtransportCode(lastErrorCode)
require.NoError(t, err)
require.Equal(t, StreamErrorCode(math.MaxUint32), last)
_, err = httpCodeToWebtransportCode(lastErrorCode + 1)
require.EqualError(t, err, "error code outside of expected range")
})
t.Run("greased value", func(t *testing.T) {
var counter int
for range int(1e4) {
c := firstErrorCode + uint64(rand.Uint32())
if (c-0x21)%0x1f != 0 {
continue
}
counter++
_, err := httpCodeToWebtransportCode(quic.StreamErrorCode(c))
require.EqualError(t, err, "invalid error code")
}
t.Logf("checked %d greased values", counter)
require.NotZero(t, counter)
})
}
func TestStreamError(t *testing.T) {
require.ErrorIs(t, &StreamError{ErrorCode: 2, Remote: true}, &StreamError{ErrorCode: 2, Remote: true})
require.NotErrorIs(t, &StreamError{ErrorCode: 2, Remote: true}, &StreamError{ErrorCode: 2, Remote: false})
require.NotErrorIs(t, &StreamError{ErrorCode: 1}, &StreamError{ErrorCode: 2})
require.Equal(t, "stream canceled with error code 2", (&StreamError{ErrorCode: 2, Remote: true}).Error())
require.Equal(t, "stream canceled with error code 1337", (&StreamError{ErrorCode: 1337, Remote: false}).Error())
}
func TestSessionError(t *testing.T) {
require.ErrorIs(t, &SessionError{ErrorCode: 2, Remote: true}, &SessionError{ErrorCode: 2, Remote: true})
require.ErrorIs(t,
&SessionError{ErrorCode: 2, Remote: true, Message: "foo"},
&SessionError{ErrorCode: 2, Remote: true, Message: "bar"},
)
require.NotErrorIs(t, &SessionError{ErrorCode: 2, Remote: true}, &SessionError{ErrorCode: 2, Remote: false})
require.NotErrorIs(t, &SessionError{ErrorCode: 1}, &SessionError{ErrorCode: 2})
require.Equal(t, "foo", (&SessionError{ErrorCode: 2, Remote: true, Message: "foo"}).Error())
require.Equal(t, "bar", (&SessionError{ErrorCode: 1337, Remote: false, Message: "bar"}).Error())
}