-
Notifications
You must be signed in to change notification settings - Fork 4
/
engines.browserCryptoSpec.js
39 lines (34 loc) · 1.38 KB
/
engines.browserCryptoSpec.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
(function (Random) {
describe("engines.browserCrypto", function () {
if (typeof crypto === "undefined" || typeof crypto.getRandomValues !== "function" || typeof Uint32Array !== "function") {
it("is null due to lack of support", function () {
expect(Random.engines.browserCrypto).toBe(null);
});
} else {
var discard = function (count) {
for (var i = 0; i < count; ++i) {
Random.engines.browserCrypto();
}
};
it("calls crypto.getRandomValues on a 128-length Uint32Array", function () {
spyOn(crypto, "getRandomValues").andCallThrough();
Random.engines.browserCrypto();
expect(crypto.getRandomValues).toHaveBeenCalled();
var arg = crypto.getRandomValues.mostRecentCall.args[0];
expect(arg instanceof Uint32Array).toBe(true);
expect(arg.length).toBe(128);
discard(127);
});
it("returns the values returned by crypto.getRandomValues until exhausted", function () {
spyOn(crypto, "getRandomValues").andCallFake(function (array) {
for (var i = 0; i < 128; ++i) {
array[i] = i;
}
});
for (var i = 0; i < 128; ++i) {
expect(Random.engines.browserCrypto()).toBe(i);
}
});
}
});
}(typeof module !== "undefined" ? require("../lib/random") : Random));