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