1
2
3
4
5
6
7
8
9
10
11
12
13
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 import org.openqa.selenium.WebDriver;
22
23
24
25
26
27
28
29 public class CryptoTest extends WebDriverTestCase {
30
31
32
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 Crypto);\n"
46 + " new Crypto();\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
61
62 @Test
63 @Alerts({"true", "true", "true", "false", "false", "false", "10", "true"})
64 public void getRandomValues() throws Exception {
65 final String html = DOCTYPE_HTML
66 + "<html><head><script>\n"
67 + LOG_TITLE_FUNCTION
68 + "try {\n"
69 + " var array = new Uint32Array(10);\n"
70 + " log(array[0] == 0);\n"
71 + " log(array[3] == 0);\n"
72 + " log(array[9] == 0);\n"
73 + " var res = window.crypto.getRandomValues(array);\n"
74 + " log(array[0] == 0);\n"
75 + " log(array[3] == 0);\n"
76 + " log(array[9] == 0);\n"
77
78 + " log(res.length);\n"
79 + " log(res === array);\n"
80 + "}\n"
81 + "catch(e) { logEx(e); }\n"
82 + "</script></head></html>";
83
84 loadPageVerifyTitle2(html);
85 }
86
87
88
89
90 @Test
91 @Alerts("[0-9a-f]{8}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{12}§")
92 public void randomUUID() throws Exception {
93 final String html = DOCTYPE_HTML
94 + "<html><head><script>\n"
95 + LOG_TITLE_FUNCTION
96 + "try {\n"
97 + " log(window.crypto.randomUUID());\n"
98 + "}\n"
99 + "catch(e) { logEx(e); }\n"
100 + "</script></head></html>";
101
102 final WebDriver driver = loadPage2(html);
103 final String title = driver.getTitle();
104
105 assertTrue(title, title.matches(getExpectedAlerts()[0]));
106 }
107
108
109
110
111 @Test
112 @Alerts(DEFAULT = "QuotaExceededError/DOMException",
113 CHROME = "QuotaExceededError/QuotaExceededError",
114 EDGE = "QuotaExceededError/QuotaExceededError")
115 @HtmlUnitNYI(CHROME = "QuotaExceededError/DOMException",
116 EDGE = "QuotaExceededError/DOMException")
117 public void getRandomValuesQuotaExceeded() throws Exception {
118 final String html = DOCTYPE_HTML
119 + "<html><head><script>\n"
120 + LOG_TITLE_FUNCTION
121 + "try {\n"
122 + " var array = new Uint32Array(16385);\n"
123 + " window.crypto.getRandomValues(array);\n"
124 + "}\n"
125 + "catch(e) { logEx(e); }\n"
126 + "</script></head></html>";
127
128 loadPageVerifyTitle2(html);
129 }
130
131
132
133
134 @Test
135 @Alerts("[object SubtleCrypto]")
136 public void subtle() throws Exception {
137 final String html = DOCTYPE_HTML
138 + "<html><head><script>\n"
139 + LOG_TITLE_FUNCTION
140 + "try {\n"
141 + " log(window.crypto.subtle);\n"
142 + "}\n"
143 + "catch(e) { logEx(e); }\n"
144 + "</script></head></html>";
145
146 loadPageVerifyTitle2(html);
147 }
148 }