-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): generateRandomString uses Math.random() #14132
base: feat/server-auth/main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes lgtm !!
(will need to double check polyfill in RN)
const length = 10; | ||
const result = generateRandomString(length); | ||
expect(result).toHaveLength(length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think it's probably ok to just be concise here
const length = 10; | |
const result = generateRandomString(length); | |
expect(result).toHaveLength(length); | |
expect(generateRandomString(length)).toHaveLength(10); |
expect(crypto.getRandomValues).toHaveBeenCalledWith(expect.any(Uint8Array)); | ||
}); | ||
|
||
it('generates different strings on subsequent calls', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a meaningful assertion given the mocked getRandomValues
?
} | ||
const array = new Uint8Array(bufferView.buffer); | ||
for (let i = 0; i < array.byteLength; i++) { | ||
array[i] = Math.floor(Math.random() * 256); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of trying to replicate randomness, we should trust that getRandomValues
would indeed give us random values and we mock here with something static that we can assert our actual logic (what we end up doing with the random values) is correct
Description of changes
Update the existing
generateRandomString()
implementation to usecrypto.getRandomValues()
.Issue #, if available
Description of how you validated changes
Checklist
yarn test
passesChecklist for repo maintainers
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.