Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d6804c

Browse files
authored
chore: improve coverage of cryptorand package (#377)
Check error cases in cryptorand functions, such as failures to read random data and input parameter validation.
1 parent 0c005ce commit 2d6804c

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

cryptorand/errors_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cryptorand_test
2+
3+
import (
4+
"crypto/rand"
5+
"io"
6+
"testing"
7+
"testing/iotest"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/coder/coder/cryptorand"
12+
)
13+
14+
// TestRandError checks that the code handles errors when reading from
15+
// the rand.Reader.
16+
//
17+
// This test replaces the global rand.Reader, so cannot be parallelized
18+
//nolint:paralleltest
19+
func TestRandError(t *testing.T) {
20+
var origReader = rand.Reader
21+
t.Cleanup(func() {
22+
rand.Reader = origReader
23+
})
24+
25+
rand.Reader = iotest.ErrReader(io.ErrShortBuffer)
26+
27+
t.Run("Int63", func(t *testing.T) {
28+
_, err := cryptorand.Int63()
29+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int63 error")
30+
})
31+
32+
t.Run("Uint64", func(t *testing.T) {
33+
_, err := cryptorand.Uint64()
34+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Uint64 error")
35+
})
36+
37+
t.Run("Int31", func(t *testing.T) {
38+
_, err := cryptorand.Int31()
39+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int31 error")
40+
})
41+
42+
t.Run("Int31n", func(t *testing.T) {
43+
_, err := cryptorand.Int31n(100)
44+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int31n error")
45+
})
46+
47+
t.Run("Uint32", func(t *testing.T) {
48+
_, err := cryptorand.Uint32()
49+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Uint32 error")
50+
})
51+
52+
t.Run("Int", func(t *testing.T) {
53+
_, err := cryptorand.Int()
54+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int error")
55+
})
56+
57+
t.Run("Intn_32bit", func(t *testing.T) {
58+
_, err := cryptorand.Intn(100)
59+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Intn error")
60+
})
61+
62+
t.Run("Intn_64bit", func(t *testing.T) {
63+
_, err := cryptorand.Intn(int(1 << 35))
64+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Intn error")
65+
})
66+
67+
t.Run("Float64", func(t *testing.T) {
68+
_, err := cryptorand.Float64()
69+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float64 error")
70+
})
71+
72+
t.Run("Float32", func(t *testing.T) {
73+
_, err := cryptorand.Float32()
74+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float32 error")
75+
})
76+
77+
t.Run("Bool", func(t *testing.T) {
78+
_, err := cryptorand.Bool()
79+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected Bool error")
80+
})
81+
82+
t.Run("StringCharset", func(t *testing.T) {
83+
_, err := cryptorand.HexString(10)
84+
require.ErrorIs(t, err, io.ErrShortBuffer, "expected HexString error")
85+
})
86+
}

cryptorand/numbers_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func TestInt63n(t *testing.T) {
9292
require.True(t, v >= 0, "values must be positive")
9393
require.True(t, v < 1<<35, "values must be less than 1<<35")
9494
}
95+
96+
// Expect a panic if max is negative
97+
require.PanicsWithValue(t, "invalid argument to Int63n", func() {
98+
cryptorand.Int63n(0)
99+
})
95100
}
96101

97102
func TestInt31n(t *testing.T) {
@@ -116,6 +121,15 @@ func TestIntn(t *testing.T) {
116121
require.True(t, v >= 0, "values must be positive")
117122
require.True(t, v < 100, "values must be less than 100")
118123
}
124+
125+
// Ensure Intn works for int larger than 32 bits
126+
_, err := cryptorand.Intn(1 << 35)
127+
require.NoError(t, err, "expected Intn to work for 64-bit int")
128+
129+
// Expect a panic if max is negative
130+
require.PanicsWithValue(t, "n must be a positive nonzero number", func() {
131+
cryptorand.Intn(0)
132+
})
119133
}
120134

121135
func TestFloat64(t *testing.T) {

0 commit comments

Comments
 (0)