-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
errors_test.go
76 lines (66 loc) · 1.93 KB
/
errors_test.go
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
package webtransport
import (
"errors"
"math"
"math/rand"
"testing"
"time"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
func TestErrorCodeRoundTrip(t *testing.T) {
for i := 0; i < 1e4; i++ {
n := StreamErrorCode(random.Int63())
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 i := 0; i < 1e4; i++ {
c := firstErrorCode + uint64(uint32(random.Int63()))
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 TestErrorDetection(t *testing.T) {
is := []error{
&quic.StreamError{ErrorCode: webtransportCodeToHTTPCode(42)},
&quic.StreamError{ErrorCode: sessionCloseErrorCode},
}
for _, i := range is {
require.True(t, isWebTransportError(i))
}
isNot := []error{
errors.New("foo"),
&quic.StreamError{ErrorCode: sessionCloseErrorCode + 1},
}
for _, i := range isNot {
require.False(t, isWebTransportError(i))
}
}