Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend the error code space to 32 bits #74

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

// StreamErrorCode is an error code used for stream termination.
type StreamErrorCode uint8
type StreamErrorCode uint32

// SessionErrorCode is an error code for session termination.
type SessionErrorCode uint32

const (
firstErrorCode = 0x52e4a40fa8db
lastErrorCode = 0x52e4a40fa9e2
lastErrorCode = 0x52e5ac983162
)

func webtransportCodeToHTTPCode(n StreamErrorCode) quic.StreamErrorCode {
Expand Down
34 changes: 26 additions & 8 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,57 @@ 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 < math.MaxUint8; i++ {
httpCode := webtransportCodeToHTTPCode(StreamErrorCode(i))
for i := 0; i < 1e4; i++ {
n := StreamErrorCode(random.Int63())
httpCode := webtransportCodeToHTTPCode(n)
errorCode, err := httpCodeToWebtransportCode(httpCode)
require.NoError(t, err)
require.Equal(t, StreamErrorCode(i), errorCode)
require.Equal(t, n, errorCode)
}
}

func TestErrorCodeConversionErrors(t *testing.T) {
t.Run("too small", func(t *testing.T) {
_, err := httpCodeToWebtransportCode(firstErrorCode - 1)
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) {
_, err := httpCodeToWebtransportCode(lastErrorCode + 1)
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) {
invalids := []quic.StreamErrorCode{0x52e4a40fa8f9, 0x52e4a40fa918, 0x52e4a40fa937, 0x52e4a40fa956, 0x52e4a40fa975, 0x52e4a40fa994, 0x52e4a40fa9b3, 0x52e4a40fa9d2}
for _, c := range invalids {
_, err := httpCodeToWebtransportCode(c)
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)
})
}

Expand Down