mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
This means node's crypto.randomBytes on the server, and window.crypto.getRandomValues on the client. If node's crypto.randomBytes throws an exception, we fall back to crypto.pseudoRandomBytes. If window.crypto.getRandomValues isn't supported by the browser, we fall back to the alea generator that we had been using previously.
30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
Tinytest.add('random', function (test) {
|
|
// Deterministic with a specified seed, which should generate the
|
|
// same sequence in all environments.
|
|
//
|
|
// For repeatable unit test failures using deterministic random
|
|
// number sequences it's fine if a new Meteor release changes the
|
|
// algorithm being used and it starts generating a different
|
|
// sequence for a seed, as long as the sequence is consistent for
|
|
// a particular release.
|
|
var random = Random.create(0);
|
|
test.equal(random.id(), "cp9hWvhg8GSvuZ9os");
|
|
test.equal(random.id(), "3f3k6Xo7rrHCifQhR");
|
|
test.equal(random.id(), "shxDnjWWmnKPEoLhM");
|
|
test.equal(random.id(), "6QTjB8C5SEqhmz4ni");
|
|
});
|
|
|
|
// node crypto and window.crypto.getRandomValues() don't let us specify a seed,
|
|
// but at least test that the output is in the right format.
|
|
Tinytest.add('random - format', function (test) {
|
|
var idLen = 17;
|
|
test.equal(Random.id().length, idLen);
|
|
var numDigits = 9;
|
|
var hexStr = Random.hexString(numDigits);
|
|
test.equal(hexStr.length, numDigits);
|
|
parseInt(hexStr, 16); // should not throw
|
|
var frac = Random.fraction();
|
|
test.isTrue(frac < 1.0);
|
|
test.isTrue(frac >= 0.0);
|
|
});
|