1
|
var test = require('tape')
|
2
|
var crypto = require('../')
|
3
|
var Buffer = require('safe-buffer').Buffer
|
4
|
|
5
|
test('get error message', function (t) {
|
6
|
try {
|
7
|
var b = crypto.randomFillSync(Buffer.alloc(10))
|
8
|
t.ok(Buffer.isBuffer(b))
|
9
|
t.end()
|
10
|
} catch (err) {
|
11
|
t.ok(/not supported/.test(err.message), '"not supported" is in error message')
|
12
|
t.end()
|
13
|
}
|
14
|
})
|
15
|
|
16
|
test('randomfill', function (t) {
|
17
|
t.plan(5)
|
18
|
t.equal(crypto.randomFillSync(Buffer.alloc(10)).length, 10)
|
19
|
t.ok(Buffer.isBuffer(crypto.randomFillSync(Buffer.alloc(10))))
|
20
|
crypto.randomFill(Buffer.alloc(10), function (ex, bytes) {
|
21
|
t.error(ex)
|
22
|
t.equal(bytes.length, 10)
|
23
|
t.ok(Buffer.isBuffer(bytes))
|
24
|
t.end()
|
25
|
})
|
26
|
})
|
27
|
|
28
|
test('seems random', function (t) {
|
29
|
var L = 1000
|
30
|
var b = crypto.randomFillSync(Buffer.alloc(L))
|
31
|
|
32
|
var mean = [].reduce.call(b, function (a, b) {
|
33
|
return a + b
|
34
|
}, 0) / L
|
35
|
|
36
|
// test that the random numbers are plausably random.
|
37
|
// Math.random() will pass this, but this will catch
|
38
|
// terrible mistakes such as this blunder:
|
39
|
// https://github.com/dominictarr/crypto-browserify/commit/3267955e1df7edd1680e52aeede9a89506ed2464#commitcomment-7916835
|
40
|
|
41
|
// this doesn't check that the bytes are in a random *order*
|
42
|
// but it's better than nothing.
|
43
|
|
44
|
var expected = 256 / 2
|
45
|
var smean = Math.sqrt(mean)
|
46
|
|
47
|
// console.log doesn't work right on testling, *grumble grumble*
|
48
|
console.log(JSON.stringify([expected - smean, mean, expected + smean]))
|
49
|
t.ok(mean < expected + smean)
|
50
|
t.ok(mean > expected - smean)
|
51
|
|
52
|
t.end()
|
53
|
})
|