View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.crypto;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link SubtleCrypto}.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   * @author Atsushi Nakagawa
28   */
29  public class SubtleCryptoTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"function", "TypeError"})
36      public void ctor() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "<head>\n"
40              + "  <script>\n"
41              + LOG_TEXTAREA_FUNCTION
42  
43              + "    function test() {\n"
44              + "      try {\n"
45              + "        log(typeof SubtleCrypto);\n"
46              + "        new SubtleCrypto();\n"
47              + "      } catch(e) { logEx(e); }\n"
48              + "    }\n"
49              + "  </script>\n"
50              + "</head>\n"
51              + "<body onload='test()'>\n"
52              + LOG_TEXTAREA
53              + "</body>\n"
54              + "</html>";
55  
56          loadPageVerifyTextArea2(html);
57      }
58  
59      /**
60       * Methods in SubtleCrypto should always wraps errors in a Promise and never throw directly.
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts("TypeError true")
65      @HtmlUnitNYI(CHROME = "TypeError false",
66              EDGE = "TypeError false",
67              FF = "TypeError false",
68              FF_ESR = "TypeError false")
69      public void unsupportedCall() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html><head><script>\n"
72              + LOG_TITLE_FUNCTION
73              + "  function test() {\n"
74              + "    if (window.crypto) {\n"
75              + "      window.crypto.subtle.generateKey(\n"
76              + "        { name: 'x' }\n"
77              + "      )\n"
78              + "      .catch(function(e) {\n"
79              + "        log('TypeError ' + (e instanceof TypeError));\n"
80              + "      });\n"
81              + "    }\n"
82              + "  }\n"
83              + "</script></head><body onload='test()'>\n"
84              + "</body></html>";
85  
86          loadPage2(html, URL_FIRST);
87          verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts({"[object Crypto]", "public", "true", "verify",
95               "name RSASSA-PKCS1-v1_5", "hash [object Object]", "modulusLength 2048",
96               "publicExponent 1,0,1",
97               "private", "false", "sign",
98               "name RSASSA-PKCS1-v1_5", "hash [object Object]", "modulusLength 2048",
99               "publicExponent 1,0,1", "done"})
100     @HtmlUnitNYI(CHROME = {"[object Crypto]", "[object DOMException]"},
101             EDGE = {"[object Crypto]", "[object DOMException]"},
102             FF = {"[object Crypto]", "[object DOMException]"},
103             FF_ESR = {"[object Crypto]", "[object DOMException]"})
104     public void rsassa() throws Exception {
105         final String html = DOCTYPE_HTML
106             + "<html><head><script>\n"
107             + LOG_TITLE_FUNCTION
108             + "  function test() {\n"
109             + "    log(window.crypto);\n"
110             + "    if (window.crypto) {\n"
111             + "      window.crypto.subtle.generateKey(\n"
112             + "        {\n"
113             + "          name: 'RSASSA-PKCS1-v1_5',\n"
114             + "          modulusLength: 2048,\n"
115             + "          publicExponent: new Uint8Array([0x01, 0x00, 0x01]),\n"
116             + "          hash: {name: 'SHA-256'}\n"
117             + "        },\n"
118             + "        false, //whether the key is extractable (i.e. can be used in exportKey)\n"
119             + "        ['sign', 'verify']\n"
120             + "      )\n"
121             + "      .then(function(key) {\n"
122             + "        log(key.publicKey.type);\n"
123             + "        log(key.publicKey.extractable);\n"
124             + "        log(key.publicKey.usages);\n"
125             + "        for(var x in key.publicKey.algorithm) {\n"
126             + "          log(x + ' ' + key.publicKey.algorithm[x]);\n"
127             + "        }\n"
128             + "        log(key.privateKey.type);\n"
129             + "        log(key.privateKey.extractable);\n"
130             + "        log(key.privateKey.usages);\n"
131             + "        for(var x in key.privateKey.algorithm) {\n"
132             + "          log(x + ' ' + key.publicKey.algorithm[x]);\n"
133             + "        }\n"
134             + "        log('done');\n"
135             + "      })\n"
136             + "      .catch(function(err) {\n"
137             + "        log(err);\n"
138             + "      });\n"
139             + "    } else { log('no window.crypto'); }\n"
140             + "  }\n"
141             + "</script></head><body onload='test()'>\n"
142             + "</body></html>";
143 
144         loadPage2(html);
145         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
146     }
147 }