Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / crypto / test / test_WebCrypto_Workers.html
blob66412f5c88b5a5b412ab405b0a581c8265759ab1
1 <!DOCTYPE html>
2 <html>
4 <head>
5 <title>WebCrypto Test Suite</title>
6 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
7 <link rel="stylesheet" href="./test_WebCrypto.css"/>
8 <script src="/tests/SimpleTest/SimpleTest.js"></script>
10 <!-- Utilities for manipulating ABVs -->
11 <script src="util.js"></script>
13 <!-- A simple wrapper around IndexedDB -->
14 <script src="simpledb.js"></script>
16 <!-- Test vectors drawn from the literature -->
17 <script src="./test-vectors.js"></script>
19 <!-- General testing framework -->
20 <script src="./test-array.js"></script>
22 <script>/* <![CDATA[*/
23 "use strict";
25 // -----------------------------------------------------------------------------
26 TestArray.addTest(
27 "Send a CryptoKey to a Worker and use it to encrypt data",
28 function() {
29 var worker = new Worker(`data:text/plain,
30 onmessage = ({data: {key, data, nonce}}) => {
31 var alg = { name: "AES-GCM", iv: nonce };
32 crypto.subtle.encrypt(alg, key, data).then(postMessage);
34 `);
36 var data = crypto.getRandomValues(new Uint8Array(128));
37 var nonce = crypto.getRandomValues(new Uint8Array(16));
38 var alg = { name: "AES-GCM", length: 128 };
39 var that = this;
41 // Generate a new AES key.
42 crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"]).then(key => {
43 // Wait for ciphertext, check and decrypt.
44 worker.addEventListener("message", ({data: ciphertext}) => {
45 crypto.subtle.decrypt({ name: "AES-GCM", iv: nonce }, key, ciphertext)
46 .then(memcmp_complete(that, data), error(that));
47 });
49 // Send it to the worker.
50 worker.postMessage({key, data, nonce});
51 });
55 // -----------------------------------------------------------------------------
56 TestArray.addTest(
57 "Get a CryptoKey from a Worker and encrypt/decrypt data",
58 function() {
59 var worker = new Worker(`data:text/plain,
60 var alg = { name: "AES-GCM", length: 128 };
61 crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
62 .then(postMessage);
63 `);
65 var data = crypto.getRandomValues(new Uint8Array(128));
66 var nonce = crypto.getRandomValues(new Uint8Array(16));
67 var alg = { name: "AES-GCM", iv: nonce };
68 var that = this;
70 // Wait for the key from the worker.
71 worker.addEventListener("message", ({data: key}) => {
72 // Encrypt some data with the key.
73 crypto.subtle.encrypt(alg, key, data).then(ciphertext => {
74 // Verify and decrypt.
75 crypto.subtle.decrypt(alg, key, ciphertext)
76 .then(memcmp_complete(that, data), error(that));
77 });
78 });
82 // -----------------------------------------------------------------------------
83 TestArray.addTest(
84 "Web crypto in terminating Worker",
85 function() {
86 var worker = new Worker(`data:text/plain,
87 function infiniteEncrypt(key, data, nonce) {
88 var alg = { name: "AES-GCM", iv: nonce };
89 return crypto.subtle.encrypt(alg, key, data).then(_ => {
90 infiniteEncrypt(key, data, nonce);
91 });
93 onmessage = ({data: {key, data, nonce}}) => {
94 infiniteEncrypt(key, data, nonce);
95 postMessage("started");
97 `);
99 var data = crypto.getRandomValues(new Uint8Array(128));
100 var nonce = crypto.getRandomValues(new Uint8Array(16));
101 var alg = { name: "AES-GCM", length: 128 };
102 var that = this;
104 // Generate a new AES key.
105 crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"]).then(key => {
106 worker.addEventListener("message", ({data: msg}) => {
107 if (msg === "started") {
108 // Terminate the worker while its busy doing crypto work
109 worker.terminate();
110 worker = null;
112 // Just end the test immediate since we can't receive any
113 // more messages from the worker after calling terminate().
114 // If we haven't crashed, then the test is a success.
115 that.complete(true);
119 // Send it to the worker.
120 worker.postMessage({key, data, nonce});
124 /* ]]>*/</script>
125 </head>
127 <body>
129 <div id="content">
130 <div id="head">
131 <b>Web</b>Crypto<br>
132 </div>
134 <div id="start" onclick="start();">RUN ALL</div>
136 <div id="resultDiv" class="content">
137 Summary:
138 <span class="pass"><span id="passN">0</span> passed, </span>
139 <span class="fail"><span id="failN">0</span> failed, </span>
140 <span class="pending"><span id="pendingN">0</span> pending.</span>
141 <br/>
142 <br/>
144 <table id="results">
145 <tr>
146 <th>Test</th>
147 <th>Result</th>
148 <th>Time</th>
149 </tr>
150 </table>
152 </div>
154 <div id="foot"></div>
155 </div>
157 </body>
158 </html>