summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/WebCryptoAPI/getRandomValues.any.js
blob: 2b82b9bedb9082876a7f47b8e3e0b33766c04144 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Step 1.
test(function() {
    assert_throws_dom("TypeMismatchError", function() {
        self.crypto.getRandomValues(new Float16Array(6))
    }, "Float16Array")
    assert_throws_dom("TypeMismatchError", function() {
        self.crypto.getRandomValues(new Float32Array(6))
    }, "Float32Array")
    assert_throws_dom("TypeMismatchError", function() {
        self.crypto.getRandomValues(new Float64Array(6))
    }, "Float64Array")

    assert_throws_dom("TypeMismatchError", function() {
        const len = 65536 / Float16Array.BYTES_PER_ELEMENT + 1;
        self.crypto.getRandomValues(new Float16Array(len));
    }, "Float16Array (too long)")
    assert_throws_dom("TypeMismatchError", function() {
        const len = 65536 / Float32Array.BYTES_PER_ELEMENT + 1;
        self.crypto.getRandomValues(new Float32Array(len));
    }, "Float32Array (too long)")
    assert_throws_dom("TypeMismatchError", function() {
        const len = 65536 / Float64Array.BYTES_PER_ELEMENT + 1;
        self.crypto.getRandomValues(new Float64Array(len))
    }, "Float64Array (too long)")
}, "Float arrays");

test(function() {
    assert_throws_dom("TypeMismatchError", function() {
        self.crypto.getRandomValues(new DataView(new ArrayBuffer(6)))
    }, "DataView")

    assert_throws_dom("TypeMismatchError", function() {
        self.crypto.getRandomValues(new DataView(new ArrayBuffer(65536 + 1)))
    }, "DataView (too long)")
}, "DataView");

const arrays = [
    'Int8Array',
    'Int16Array',
    'Int32Array',
    'BigInt64Array',
    'Uint8Array',
    'Uint8ClampedArray',
    'Uint16Array',
    'Uint32Array',
    'BigUint64Array',
];

for (const array of arrays) {
    const ctor = globalThis[array];

    test(function() {
        assert_equals(self.crypto.getRandomValues(new ctor(8)).constructor,
                      ctor, "crypto.getRandomValues(new " + array + "(8))")
    }, "Integer array: " + array);

    test(function() {
        const maxlength = 65536 / ctor.BYTES_PER_ELEMENT;
        assert_throws_dom("QuotaExceededError", function() {
            self.crypto.getRandomValues(new ctor(maxlength + 1))
        }, "crypto.getRandomValues length over 65536")
    }, "Large length: " + array);

    test(function() {
        assert_true(self.crypto.getRandomValues(new ctor(0)).length == 0)
    }, "Null arrays: " + array);
}