TestRandError checks that the code handles errors when reading from the rand.Reader. This test replaces the global rand.Reader, so cannot be parallelized nolint:paralleltest
(t *testing.T)
| 18 | // |
| 19 | //nolint:paralleltest |
| 20 | func TestRandError(t *testing.T) { |
| 21 | origReader := rand.Reader |
| 22 | t.Cleanup(func() { |
| 23 | rand.Reader = origReader |
| 24 | }) |
| 25 | |
| 26 | rand.Reader = iotest.ErrReader(io.ErrShortBuffer) |
| 27 | |
| 28 | t.Run("Int63", func(t *testing.T) { |
| 29 | _, err := cryptorand.Int63() |
| 30 | require.ErrorIs(t, err, io.ErrShortBuffer, "expected Int63 error") |
| 31 | }) |
| 32 | |
| 33 | t.Run("Intn_32bit", func(t *testing.T) { |
| 34 | _, err := cryptorand.Intn(100) |
| 35 | require.ErrorIs(t, err, io.ErrShortBuffer, "expected Intn error") |
| 36 | }) |
| 37 | |
| 38 | t.Run("Intn_64bit", func(t *testing.T) { |
| 39 | _, err := cryptorand.Intn(int(1 << 35)) |
| 40 | require.ErrorIs(t, err, io.ErrShortBuffer, "expected Intn error") |
| 41 | }) |
| 42 | |
| 43 | t.Run("Float64", func(t *testing.T) { |
| 44 | _, err := cryptorand.Float64() |
| 45 | require.ErrorIs(t, err, io.ErrShortBuffer, "expected Float64 error") |
| 46 | }) |
| 47 | |
| 48 | // See errors_go123_test.go for the StringCharset test. |
| 49 | } |