Skip to content

Fix the panic in the TestGenerateRandomBytes test #183 #184

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sudhanvaghebbale
Copy link

@sudhanvaghebbale sudhanvaghebbale commented Apr 16, 2025

Issue:

From Go 1.24 onwards, crypto/rand.Read will do the following:

Read fills b with cryptographically secure random bytes. It never returns an error, and always fills b entirely.

So the test case isn't really valid any more.

Fix:

Remove the test from the code.

helpers_test.go Outdated
Comment on lines 207 to 209
// panic here to simulate the panic in the rand.Read method.
// The rand.Read method returns a rand_fatal which cannot be recovered from.
panic("crypto/rand: failed to read random data (see https://go.dev/issue/66821)")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crypo/rand.Read doesn't panic, as you note a failure there "...cannot be recovered from" (the difference being panic-ing is recoverable). Replacing that with a panic here is not accurate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I changed the logic to run the test that throws a fatal error inside a sub-process, make sure we get an error. Let me know what you think about that? Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I changed the logic to run the test that throws a fatal error inside a sub-process, make sure we get an error. Let me know what you think about that? Thanks!

I'm not sure it makes sense to run this test for Go 1.24+: the description of the test is:

TestGenerateRandomBytes tests the (extremely rare) case that crypto/rand does not return the expected number of bytes.

However, crypto/rand.Read from Go 1.24 onwards will:

Read fills b with cryptographically secure random bytes. It never returns an error, and always fills b entirely.

So the test case isn't really valid any more.

@sudhanvaghebbale sudhanvaghebbale force-pushed the shebbale/bug-183-fix-test branch from a32a244 to 6b93cd7 Compare April 16, 2025 15:19
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants