Skip to content

Commit c429e0d

Browse files
authored
test(cryptorand): re-enable number error tests (#16956)
Realized it was only the `StringCharset` test that lead to panic, the number tests bypass it by reading via the `binary` package.
1 parent e6983d8 commit c429e0d

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

cryptorand/errors_go123_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//go:build !go1.24
2+
3+
package cryptorand_test
4+
5+
import (
6+
"crypto/rand"
7+
"io"
8+
"testing"
9+
"testing/iotest"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/coder/coder/v2/cryptorand"
14+
)
15+
16+
// TestRandError_pre_Go1_24 checks that the code handles errors when
17+
// reading from the rand.Reader.
18+
//
19+
// This test replaces the global rand.Reader, so cannot be parallelized
20+
//
21+
//nolint:paralleltest
22+
func TestRandError_pre_Go1_24(t *testing.T) {
23+
origReader := rand.Reader
24+
t.Cleanup(func() {
25+
rand.Reader = origReader
26+
})
27+
28+
rand.Reader = iotest.ErrReader(io.ErrShortBuffer)
29+
30+
// Testing `rand.Reader.Read` for errors will panic in Go 1.24 and later.
31+
t.Run("StringCharset", func(t *testing.T) {
32+
_, err := cryptorand.HexString(10)
33+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected HexString error")
34+
})
35+
}

cryptorand/errors_test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//go:build !go1.24
2-
3-
// Testing `rand.Reader.Read` for errors will panic in Go 1.24 and later.
4-
51
package cryptorand_test
62

73
import (
@@ -49,8 +45,5 @@ func TestRandError(t *testing.T) {
4945
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float64 error")
5046
})
5147

52-
t.Run("StringCharset", func(t *testing.T) {
53-
_, err := cryptorand.HexString(10)
54-
require.ErrorIs(t, err, io.ErrShortBuffer, "expected HexString error")
55-
})
48+
// See errors_go123_test.go for the StringCharset test.
5649
}

0 commit comments

Comments
 (0)