-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrandombytes_buf.js
48 lines (39 loc) · 1.1 KB
/
randombytes_buf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var test = require('tape')
var freq = require('buffer-byte-frequency')
module.exports = function (sodium) {
test.skip('Various test cases', function (assert) {
sodium.randombytes_buf(Buffer.alloc(0))
sodium.randombytes_buf(new Uint8Array(16))
assert.throws(function () {
sodium.randombytes_buf([])
})
assert.end()
})
test('Generates random bytes', function (assert) {
var bufConst = Buffer.alloc(64)
sodium.randombytes_buf(bufConst)
var buf1 = Buffer.alloc(64)
for (var i = 0; i < 1e4; i++) {
sodium.randombytes_buf(buf1)
if (Buffer.compare(buf1, bufConst) === 0) {
assert.fail('Constant buffer should not be equal')
assert.end()
return
}
}
assert.pass('Generated unique buffers')
assert.end()
})
test('Exceed quota', function (assert) {
var buf = Buffer.alloc(1 << 17)
sodium.randombytes_buf(buf)
freq(buf)
.map(function (cnt) {
return (cnt / 256) | 0
})
.forEach(function (cnt) {
if (cnt < 1 && cnt > 3) assert.fail('Statistically unreasonable')
})
assert.end()
})
}