d3f589bd5199cf28d24340149cca635e6c2eed05
[WebKit-https.git] / LayoutTests / crypto / subtle / argument-conversion.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="resources/common.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
10
11 <script>
12 description("Test crypto.subtle argument conversion");
13
14 jsTestIsAsync = true;
15
16 Promise.fulfill(null).then(function() {
17     // This looks like invalid input, but it actually isn't - Uint8Array is a sequence too,
18     // so it's just an empty sequence, same as passing [].
19     debug("SHA1 of new Uint8Array([]))");
20     return crypto.subtle.digest('sha-1', new Uint8Array([]));
21 }).then(function(result) {
22     digest = result;
23     shouldBe("bytesToHexString(new Uint8Array(digest))", "'da39a3ee5e6b4b0d3255bfef95601890afd80709'");
24
25     debug("\nPassing algorithm name as a string object...");
26     return crypto.subtle.digest(new String('sha-1'), []);
27 }).then(function(result) {
28     testPassed("...succeeded");
29     debug("\nPassing algorithm name as a string object in a dictionary...");
30     return crypto.subtle.digest({name: new String('sha-1')}, []);
31 }).then(function(result) {
32     testPassed("...succeeded");
33     debug("\nPassing algorithm name as an object with toString");
34     return crypto.subtle.digest({name: { toString:function() { return 'sha-1' } } }, []);
35 }).then(function(result) {
36     testPassed("...succeeded");
37
38     debug("\nPassing invalid data to digest()");
39     shouldThrow("crypto.subtle.digest({name: 'sha-1'})");
40     shouldThrow("crypto.subtle.digest({name: 'sha-1'}, null)");
41     shouldThrow("crypto.subtle.digest({name: 'sha-1'}, 10)");
42     shouldThrow("crypto.subtle.digest({name: 'sha-1'}, [10])");
43     shouldThrow("crypto.subtle.digest({name: 'sha-1'}, new Uint8Array([0]))");
44
45     debug("\nPassing invalid algorithmIdentifiers to digest()");
46     data = new Uint8Array([0]);
47     shouldThrow("crypto.subtle.digest({ toString:function() { return 'sha-1' } }, [data])"); // Algorithm normalization doesn't attempt to call toString.
48     shouldThrow("crypto.subtle.digest({name: ''}, [data])");
49     shouldThrow("crypto.subtle.digest({name: null}, [data])");
50     shouldThrow("crypto.subtle.digest({name: undefined}, [data])");
51     shouldThrow("crypto.subtle.digest({name: 'sha'}, [data])");
52     shouldThrow("crypto.subtle.digest({name: 1}, [data])");
53     shouldThrow("crypto.subtle.digest('', [data])");
54     shouldThrow("crypto.subtle.digest(null, [data])");
55     shouldThrow("crypto.subtle.digest(undefined, [data])");
56     shouldThrow("crypto.subtle.digest(1, [data])");
57     shouldThrow("crypto.subtle.digest({}, [data])");
58 }).then(finishJSTest);
59
60 </script>
61
62 <script src="../../resources/js-test-post.js"></script>
63 </body>
64 </html>