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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.openqa.selenium.WebDriver;
23  
24  /**
25   * Tests for {@link Crypto}.
26   *
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class CryptoTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"function", "TypeError"})
38      public void ctor() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html>\n"
41              + "<head>\n"
42              + "  <script>\n"
43              + LOG_TEXTAREA_FUNCTION
44  
45              + "    function test() {\n"
46              + "      try {\n"
47              + "        log(typeof Crypto);\n"
48              + "        new Crypto();\n"
49              + "      } catch(e) { logEx(e); }\n"
50              + "    }\n"
51              + "  </script>\n"
52              + "</head>\n"
53              + "<body onload='test()'>\n"
54              + LOG_TEXTAREA
55              + "</body>\n"
56              + "</html>";
57  
58          loadPageVerifyTextArea2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts({"true", "true", "true", "false", "false", "false", "10", "true"})
66      public void getRandomValues() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html><head><script>\n"
69              + LOG_TITLE_FUNCTION
70              + "try {\n"
71              + "  var array = new Uint32Array(10);\n"
72              + "  log(array[0] == 0);\n"
73              + "  log(array[3] == 0);\n"
74              + "  log(array[9] == 0);\n"
75              + "  var res = window.crypto.getRandomValues(array);\n"
76              + "  log(array[0] == 0);\n"
77              + "  log(array[3] == 0);\n"
78              + "  log(array[9] == 0);\n"
79  
80              + "  log(res.length);\n"
81              + "  log(res === array);\n"
82              + "}\n"
83              + "catch(e) { logEx(e); }\n"
84              + "</script></head></html>";
85  
86          loadPageVerifyTitle2(html);
87      }
88  
89      /**
90       * @throws Exception if the test fails
91       */
92      @Test
93      @Alerts("[0-9a-f]{8}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{12}§")
94      public void randomUUID() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><head><script>\n"
97              + LOG_TITLE_FUNCTION
98              + "try {\n"
99              + "  log(window.crypto.randomUUID());\n"
100             + "}\n"
101             + "catch(e) { logEx(e); }\n"
102             + "</script></head></html>";
103 
104         final WebDriver driver = loadPage2(html);
105         final String title = driver.getTitle();
106 
107         assertTrue(title, title.matches(getExpectedAlerts()[0]));
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts("QuotaExceededError/DOMException")
115     public void getRandomValuesQuotaExceeded() throws Exception {
116         final String html = DOCTYPE_HTML
117             + "<html><head><script>\n"
118             + LOG_TITLE_FUNCTION
119             + "try {\n"
120             + "  var array = new Uint32Array(16385);\n"
121             + "  window.crypto.getRandomValues(array);\n"
122             + "}\n"
123             + "catch(e) { logEx(e); }\n"
124             + "</script></head></html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     @Alerts("[object SubtleCrypto]")
134     public void subtle() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html><head><script>\n"
137             + LOG_TITLE_FUNCTION
138             + "try {\n"
139             + "  log(window.crypto.subtle);\n"
140             + "}\n"
141             + "catch(e) { logEx(e); }\n"
142             + "</script></head></html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 }