TestRandError_pre_Go1_24 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)
| 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 | } |