Skip to content

Commit 72336c9

Browse files
committed
catch horrible mistakes that break the rng, like if it returns all zeros
1 parent c31b9d7 commit 72336c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/random-bytes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,29 @@ test('randomBytes', function (t) {
1313
});
1414
});
1515

16+
test('randomBytes seem random', function (t) {
17+
18+
var L = 1000
19+
var b = crypto.randomBytes(L)
20+
21+
var mean = [].reduce.call(b, function (a, b) { return a + b}, 0) / L
22+
23+
// test that the random numbers are plausably random.
24+
// Math.random() will pass this, but this will catch
25+
// terrible mistakes such as this blunder:
26+
// https://github.com/dominictarr/crypto-browserify/commit/3267955e1df7edd1680e52aeede9a89506ed2464#commitcomment-7916835
27+
28+
// this doesn't check that the bytes are in a random *order*
29+
// but it's better than nothing.
30+
31+
var expected = 256/2
32+
var smean = Math.sqrt(mean)
33+
34+
t.ok(mean < expected + smean)
35+
t.ok(mean > expected - smean)
36+
37+
t.end()
38+
39+
})
40+
1641

0 commit comments

Comments
 (0)