summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/crypto/worker-random-values-concurrent.html
blob: 5f42f84758dad2412777fe2f365104433bc8ddde (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
<!DOCTYPE html>
<html>
<head>
<script src="../fast/js/resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
    description("Tests concurrent calls to crypto.randomValues from workers.");

    // Generate random values from different 10 workers, and wait until they
    // have all finished.
    var randomValues = {};
    var numRandomValues = 0;
    var NUM_WORKERS = 10;

    self.jsTestIsAsync = true;

    // Asserts that each call to receivedRandomValue() contains unique bytes.
    // The bytes are 100 randomly generated, and as such have an extremely low
    // probability of matching each other.
    function workerGeneratedRandomBytes(bytes)
    { 
        if (bytes.length != 100)
            throw "bytes is not the right length: " + bytes.length;

        var bytesStr = Array.prototype.join.call(bytes, '');

        if (bytesStr in randomValues)
            debug("Generated a duplicate 'random' number: " + bytesStr);
        else
            debug("Received random bytes from worker");

        randomValues[bytesStr] = true;

        if (++numRandomValues == NUM_WORKERS)
            finishJSTest();
    }

    for (var i = 0; i < NUM_WORKERS; ++i) {
        var worker = new Worker('resources/random-values-concurrent.js');
        worker.onmessage = function(event)
        {
            if (event.data instanceof Uint8Array)
                workerGeneratedRandomBytes(event.data);
            else
                debug('[Worker] ' + event.data);
        }
        worker.onerror = function(event)
        {
            debug('Got error from worker: ' + event.message);
            finishJSTest();
        }
    }
</script>

<script src="../fast/js/resources/js-test-post.js"></script>
</body>
</html>