|
| 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 | +} |
0 commit comments