crypto-random-values-types.html 1.5 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html>
<head>
4
<script src="../resources/js-test-pre.js"></script>
5 6 7 8 9 10 11 12 13 14 15 16 17 18
<script src="resources/utilities.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests which types are valid for crypto.randomValues.");

if (!window.ArrayBuffer)
    debug("This test requres ArrayBuffers to run!");

shouldBe("'crypto' in window", "true");
shouldBe("'getRandomValues' in window.crypto", "true");

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 49 50
function checkIntegerTypes() {
    var integerTypes = [
        "Uint8Array", "Int8Array", "Uint8ClampedArray",
        "Uint16Array", "Int16Array",
        "Uint32Array", "Int32Array",
    ];
    integerTypes.forEach(function(arrayType) {
        shouldBeDefined("random = crypto.getRandomValues(new "+arrayType+"(3))");
        shouldBeType("random", arrayType);

        shouldBeDefined("view = new "+arrayType+"(3)");
        shouldBeDefined("random = crypto.getRandomValues(view)");
        shouldBe("random", "view");
    });
}

function checkNonIntegerTypes() {
    var floatTypes = [
        "Float32Array", "Float64Array",
    ];
    floatTypes.forEach(function(arrayType) {
        shouldThrow("crypto.getRandomValues(new "+arrayType+"(3))");
    });

    shouldBeDefined("buffer = new Uint8Array(32)");
    shouldBeDefined("buffer.buffer");
    shouldBeDefined("view = new DataView(buffer.buffer)");
    shouldThrow("crypto.getRandomValues(view)");
}

checkIntegerTypes();
checkNonIntegerTypes();
51 52

</script>
53
<script src="../resources/js-test-post.js"></script>
54 55 56 57
</body>
</html>