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.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
26
27
28
29
30 @RunWith(BrowserRunner.class)
31 public class CryptoTest extends WebDriverTestCase {
32
33
34
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
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
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
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
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 }