react / wstein / node_modules / browserify / node_modules / crypto-browserify / test / random-bytes.js
80537 viewsvar test = require('tape')1var crypto = require('../')2var randomBytes = require('randombytes')3var randomBytesFunctions = {4randomBytes: randomBytes,5pseudoRandomBytes: crypto.pseudoRandomBytes6}7for (var randomBytesName in randomBytesFunctions) {8// Both randomBytes and pseudoRandomBytes should provide the same interface9var randomBytes = randomBytesFunctions[randomBytesName];10test('get error message', function (t) {1112try {13var b = randomBytes(10)14t.ok(Buffer.isBuffer(b))15t.end()16} catch (err) {17t.ok(/not supported/.test(err.message), '"not supported" is in error message')18t.end()19}2021})2223test(randomBytesName, function (t) {24t.plan(5);25t.equal(randomBytes(10).length, 10);26t.ok(Buffer.isBuffer(randomBytes(10)))27randomBytes(10, function(ex, bytes) {28t.error(ex);29t.equal(bytes.length, 10);30t.ok(Buffer.isBuffer(bytes))31t.end();32});33});3435test(randomBytesName + ' seem random', function (t) {3637var L = 100038var b = randomBytes(L)3940var mean = [].reduce.call(b, function (a, b) { return a + b}, 0) / L4142// test that the random numbers are plausably random.43// Math.random() will pass this, but this will catch44// terrible mistakes such as this blunder:45// https://github.com/dominictarr/crypto-browserify/commit/3267955e1df7edd1680e52aeede9a89506ed2464#commitcomment-79168354647// this doesn't check that the bytes are in a random *order*48// but it's better than nothing.4950var expected = 256/251var smean = Math.sqrt(mean)52//console.log doesn't work right on testling, *grumble grumble*53console.log(JSON.stringify([expected - smean, mean, expected + smean]))54t.ok(mean < expected + smean)55t.ok(mean > expected - smean)5657t.end()5859})60}61626364