Skip to content

Instantly share code, notes, and snippets.

@timhuff
Created August 13, 2017 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timhuff/d7d107248ee611b2dad1d675c2feccb5 to your computer and use it in GitHub Desktop.
Save timhuff/d7d107248ee611b2dad1d675c2feccb5 to your computer and use it in GitHub Desktop.
const crypto = require('crypto')
describe('Predictable Randomness', () => {
describe('Math.random', () => {
const firstResult = 0.37454011430963874
const secondResult = 0.7965429842006415
beforeEach(() => Math.__clearChances__())
it('should produce these two numbers first by default', () => {
expect(Math.random()).toEqual(firstResult)
expect(Math.random()).toEqual(secondResult)
})
it('should produce these two numbers again (due to the beforeEach)', () => {
expect(Math.random()).toEqual(firstResult)
expect(Math.random()).toEqual(secondResult)
})
it('should be affected by intermittent calls', () => {
expect(Math.random()).toEqual(firstResult)
Math.random()
expect(Math.random()).not.toEqual(secondResult)
})
it('should not be affected by new calls with a different seed', () => {
expect(Math.random()).toEqual(firstResult)
Math.random('never mind me')
expect(Math.random()).toEqual(secondResult)
})
it('should produce a different but repeatable sequence for a custom seed', () => {
expect(Math.random("I'm different!")).toEqual(0.16998715861700475)
expect(Math.random("I'm different!")).toEqual(0.5475940068718046)
})
it('should produce a different but repeatable sequence for a custom seed', () => {
expect(Math.random("I'm different!")).toEqual(0.16998715861700475)
expect(Math.random("I'm different!")).toEqual(0.5475940068718046)
})
it('should produce yet a different sequence for a different custom seed', () => {
expect(Math.random('hi')).toEqual(0.6855331424158067)
expect(Math.random('hi')).toEqual(0.565650706877932)
})
})
describe('crypto.randomBytes', () => {
const b64it = buffer => buffer.toString('base64') // for brevity's sake
const firstResult = 'X8vzLrvHmZgncicZDnXdVQ=='
const secondResult = 'mSS1pgUO+LjV8DYALv4ung=='
beforeEach(() => crypto.__clearChances__())
it('should produce these two strings first by default', () => {
expect(b64it(crypto.randomBytes(16))).toEqual(firstResult)
expect(b64it(crypto.randomBytes(16))).toEqual(secondResult)
})
it('should produce a buffer of the correct length', () => {
expect(crypto.randomBytes(16).length).toEqual(16)
expect(crypto.randomBytes(Math.floor(Math.random() * 100) + 50).length).toEqual(87)
})
it('should pass the buffer back through the callback without a custom seed', () => {
const callback = jest.fn()
crypto.randomBytes(16, callback)
expect(callback.mock.calls.length).toEqual(1)
expect(callback.mock.calls[0][0]).toBeNull()
expect(callback.mock.calls[0][1]).toBeInstanceOf(Buffer)
})
it('should pass the buffer back through the callback with a custom seed', () => {
const callback = jest.fn()
crypto.randomBytes(16, 'custom seed', callback)
expect(callback.mock.calls.length).toEqual(1)
expect(callback.mock.calls[0][0]).toBeNull()
expect(callback.mock.calls[0][1]).toBeInstanceOf(Buffer)
})
it('should produce these two strings again (due to the beforeEach)', () => {
expect(b64it(crypto.randomBytes(16))).toEqual(firstResult)
expect(b64it(crypto.randomBytes(16))).toEqual(secondResult)
})
it('should be affected by intermittent calls', () => {
expect(b64it(crypto.randomBytes(16))).toEqual(firstResult)
crypto.randomBytes(16)
expect(b64it(crypto.randomBytes(16))).not.toEqual(secondResult)
})
it('should not be affected by new calls with a different seed', () => {
expect(b64it(crypto.randomBytes(16))).toEqual(firstResult)
crypto.randomBytes(16, 'Never mind me')
expect(b64it(crypto.randomBytes(16))).toEqual(secondResult)
})
it('should produce a different but repeatable sequence for a custom seed', () => {
expect(b64it(crypto.randomBytes(16, "I'm different!"))).toEqual('K4yK2B8VGxxISK0d73h46Q==')
expect(b64it(crypto.randomBytes(16, "I'm different!"))).toEqual('BqU35hvdIw0EZiIm77Dltg==')
})
it('should produce a different but repeatable sequence for a custom seed', () => {
expect(b64it(crypto.randomBytes(16, "I'm different!"))).toEqual('K4yK2B8VGxxISK0d73h46Q==')
expect(b64it(crypto.randomBytes(16, "I'm different!"))).toEqual('BqU35hvdIw0EZiIm77Dltg==')
})
it('should produce yet a different sequence for a different custom seed', () => {
expect(b64it(crypto.randomBytes(16, 'hi'))).toEqual('r5DNZ0l1eCaYnMrzazXzNQ==')
expect(b64it(crypto.randomBytes(16, 'hi'))).toEqual('zUzV3TuaKZlnp7vRDRxo4w==')
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment