Skip to content
This repository
Browse code

test: test crypto (P)RNG functions

  • Loading branch information...
commit 413d38c43bc3fa6828e10c75736f81c3d6b7e944 1 parent c4eaf7e
Ben Noordhuis bnoordhuis authored

Showing 1 changed file with 70 additions and 0 deletions. Show diff stats Hide diff stats

  1. +70 0 test/simple/test-crypto-random.js
70 test/simple/test-crypto-random.js
... ... @@ -0,0 +1,70 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var common = require('../common');
  23 +var assert = require('assert');
  24 +
  25 +try {
  26 + var crypto = require('crypto');
  27 +} catch (e) {
  28 + console.log('Not compiled with OPENSSL support.');
  29 + process.exit();
  30 +}
  31 +
  32 +// bump, we register a lot of exit listeners
  33 +process.setMaxListeners(256);
  34 +
  35 +[ crypto.randomBytes,
  36 + crypto.pseudoRandomBytes
  37 +].forEach(function(f) {
  38 + [ -1,
  39 + undefined,
  40 + null,
  41 + false,
  42 + true,
  43 + {},
  44 + []
  45 + ].forEach(function(value) {
  46 + assert.throws(function() { f(value); });
  47 + assert.throws(function() { f(value, function(){}); });
  48 + });
  49 +
  50 + [0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
  51 + f(len, checkCall(function(ex, buf) {
  52 + assert.equal(null, ex);
  53 + assert.equal(len, buf.length);
  54 + assert.ok(Buffer.isBuffer(buf));
  55 + }));
  56 + });
  57 +});
  58 +
  59 +// assert that the callback is indeed called
  60 +function checkCall(cb, desc) {
  61 + var called_ = false;
  62 +
  63 + process.on('exit', function() {
  64 + assert.equal(true, called_, desc || ('callback not called: ' + cb));
  65 + });
  66 +
  67 + return function() {
  68 + return called_ = true, cb.apply(cb, Array.prototype.slice.call(arguments));
  69 + };
  70 +}

0 comments on commit 413d38c

Please sign in to comment.
Something went wrong with that request. Please try again.