2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2011-09-22 21:26:33 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2015-03-04 12:11:21 +11:00
|
|
|
if (!common.hasCrypto) {
|
|
|
|
console.log('1..0 # Skipped: missing crypto');
|
2015-07-07 20:55:55 +05:30
|
|
|
return;
|
2011-09-22 21:26:33 +02:00
|
|
|
}
|
2015-03-04 12:11:21 +11:00
|
|
|
var crypto = require('crypto');
|
2011-09-22 21:26:33 +02:00
|
|
|
|
2012-10-22 10:37:20 -07:00
|
|
|
crypto.DEFAULT_ENCODING = 'buffer';
|
|
|
|
|
2011-09-22 21:26:33 +02:00
|
|
|
// bump, we register a lot of exit listeners
|
|
|
|
process.setMaxListeners(256);
|
|
|
|
|
2011-10-04 18:08:18 -04:00
|
|
|
[crypto.randomBytes,
|
2011-09-22 21:26:33 +02:00
|
|
|
crypto.pseudoRandomBytes
|
|
|
|
].forEach(function(f) {
|
2011-10-04 18:08:18 -04:00
|
|
|
[-1,
|
2011-09-22 21:26:33 +02:00
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
].forEach(function(value) {
|
|
|
|
assert.throws(function() { f(value); });
|
2011-10-04 18:08:18 -04:00
|
|
|
assert.throws(function() { f(value, function() {}); });
|
2011-09-22 21:26:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
|
|
|
|
f(len, checkCall(function(ex, buf) {
|
|
|
|
assert.equal(null, ex);
|
|
|
|
assert.equal(len, buf.length);
|
|
|
|
assert.ok(Buffer.isBuffer(buf));
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// assert that the callback is indeed called
|
|
|
|
function checkCall(cb, desc) {
|
|
|
|
var called_ = false;
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
assert.equal(true, called_, desc || ('callback not called: ' + cb));
|
|
|
|
});
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
return called_ = true, cb.apply(cb, Array.prototype.slice.call(arguments));
|
|
|
|
};
|
|
|
|
}
|
2013-03-23 15:48:56 +01:00
|
|
|
|
|
|
|
// #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData()
|
|
|
|
// length exceeds max acceptable value"
|
|
|
|
assert.throws(function() {
|
2015-06-02 13:10:47 -06:00
|
|
|
crypto.randomBytes((-1 >>> 0) + 1);
|
2013-03-23 15:48:56 +01:00
|
|
|
}, TypeError);
|