Backed out changeset bb54bd88c1ec (bug 1936282) for causing bc failures @browser_tab_...
[gecko.git] / dom / tests / mochitest / crypto / test_getRandomValues.html
blob6281241f4657cafa8c9d8fce94fcd4ad48973ed2
1 <!DOCTYPE HTML>
2 <html><head>
3 <title>Test window.crypto.getRandomValues</title>
4 <script src="/tests/SimpleTest/SimpleTest.js"></script>
5 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
6 </head>
7 <body onload="onWindowLoad()">
8 <script class="testbody" type="text/javascript">
9 SimpleTest.waitForExplicitFinish();
11 var testData = [ { len: 32, type: "Int8", pass: true },
12 { len: 32, type: "Int16", pass: true },
13 { len: 32, type: "Int32", pass: true },
14 { len: 32, type: "Uint8", pass: true },
15 { len: 32, type: "Uint16", pass: true },
16 { len: 32, type: "Uint32", pass: true },
17 { len: 65536, type: "Uint8", pass: true },
18 { len: 32, type: "Uint8Clamped", pass: true },
19 { len: 65537, type: "Uint8", pass: false },
20 { len: 32, type: "Float32", pass: false },
21 { len: 32, type: "Float64", pass: false } ];
24 var testCount = 0;
26 function testNsCryptoGetRandomValues(aLength, aType)
28 var arrayTypes = {
29 Int8: Int8Array,
30 Int16: Int16Array,
31 Int32: Int32Array,
32 Uint8: Uint8Array,
33 Uint16: Uint16Array,
34 Uint32: Uint32Array,
35 Float32: Float32Array,
36 Float64: Float64Array,
37 Uint8Clamped: Uint8ClampedArray,
40 testCount++;
42 var buf = new ArrayBuffer(aLength);
43 var arBuf = new arrayTypes[aType](buf);
45 var pass = false;
46 var b = window.crypto.getRandomValues(arBuf);
47 ok(b === arBuf, "ArrayBuffer result is argument buffer");
49 for (var i = 0; i < arBuf.length; i++) {
50 if (arBuf.length > 6) {
51 // XXXddahl: THIS MIGHT FAIL EVERY FULL MOON, SORRY QA!!!
52 if (arBuf[i] != 0) {
53 pass = true;
54 break;
57 else {
58 pass = true;
61 is(pass, true, "Non-zero result: " + i + " found in the " + aType + ": " + aLength + " ArrayBufferView");
64 function onWindowLoad()
66 window.removeEventListener("load", onWindowLoad);
67 var failedWithCorrectError = false;
68 try {
69 for (var i = 0; i < testData.length; i++) {
70 if (testData[i].pass) {
71 try {
72 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
74 catch (ex) {
75 ok(false, "testNsCryptoGetRandomValues failed, test should have passed: " + testData[i].type);
78 else {
79 // failing tests are dealt with here
80 if (i == 8) {
81 try {
82 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
84 catch (ex) {
85 todo("QuotaExceededError" in window && ex instanceof QuotaExceededError,
86 "Exception was the correct type");
87 failedWithCorrectError = ex.toString().search(/QUOTA_EXCEEDED_ERR/);
88 ok(failedWithCorrectError, "Extended length array buffer fails, NS_ERROR_DOM_QUOTA_EXCEEDED_ERR thrown");
90 } // 8
92 if (i == 9) {
93 try {
94 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
96 catch (ex) {
97 failedWithCorrectError = ex.toString().search(/TYPE_MISMATCH_ERR/);
98 ok(failedWithCorrectError,
99 "Expected TYPE_MISMATCH_ERR: Float32Array is not valid, got " + ex + ".");
101 } // 9
103 if (i == 10) {
104 try {
105 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
107 catch (ex) {
108 failedWithCorrectError = ex.toString().search(/TYPE_MISMATCH_ERR/);
109 ok(failedWithCorrectError,
110 "Expected TYPE_MISMATCH_ERR: Float64Array is not valid, got " + ex + ".");
114 } // end main for loop
116 catch (ex) {
117 ok(false, "Unexpected Error: " + ex);
119 // Count the tests in the testData array
120 ok(testCount == 11, "11 tests run via testData");
122 // Test a null argument
123 try {
124 window.crypto.getRandomValues(null);
126 catch (ex) {
127 var test = ex.toString().search(/1003|TypeError/);
128 ok((test > -1), "Expected TYPE_ERR, got " + ex + ".");
131 // Test a zero-length buffer view
132 try {
133 var a = new Int8Array(0);
134 window.crypto.getRandomValues(a);
135 ok(a[0] === undefined, "The array buffer is unchanged, still 0 length");
137 catch (ex) {
138 ok(false, "A zero-length array buffer view should not fail");
141 // Test a one-length buffer view
142 var randomVal = 0;
143 // The probability of getRandomValues generating a zero value is 1/256 so we
144 // run this in a loop until it returns a non-zero value to guard against
145 // false failures
146 do {
147 try {
148 var a = new Uint8Array(1);
149 var b = window.crypto.getRandomValues(a);
150 randomVal = a[0];
151 ok(a === b, "ArrayBuffer result is argument buffer");
153 catch (ex) {
154 ok(false, "A one-length array buffer view should not fail");
157 while (randomVal == 0);
158 ok(randomVal !== 0, "The array buffer eventually had one random value");
160 // Test a 16 byte length buffer
161 var testConfig = { len: 16, type: "Int8", pass: true };
162 testNsCryptoGetRandomValues(testConfig.len,
163 testConfig.type,
164 testConfig.pass);
166 // Test a 31 byte length buffer
167 testConfig = { len: 31, type: "Int8", pass: true };
168 testNsCryptoGetRandomValues(testConfig.len,
169 testConfig.type,
170 testConfig.pass);
172 // Test a 33 byte length buffer
173 testConfig = { len: 33, type: "Int8", pass: true };
174 testNsCryptoGetRandomValues(testConfig.len,
175 testConfig.type,
176 testConfig.pass);
178 // Test a range of an array buffer view
179 var buffer = new ArrayBuffer(32);
180 var view = new Int8Array(buffer, 0, 16);
181 var view2 = new Int8Array(buffer, 16, 16);
182 for (var i = 0; i < view2.byteLength; i++) {
183 view2[i] = 1;
185 var b = window.crypto.getRandomValues(view);
186 ok(b === view, "ArrayBuffer result is argument buffer");
187 for (var i = 0; i < view.byteLength; i++) {
188 is(view2[i], 1, "view2 is unchanged");
191 // test an offset view
192 var result = false;
193 var b = window.crypto.getRandomValues(view2);
194 for (var i = 0; i < view2.length; i++) {
195 if (view2[i] != 1) {
196 result = true;
197 break;
200 ok(result, "view2 has been updated correctly");
201 ok(b === view2, "ArrayBuffer result is argument buffer");
202 // test the return value
203 buffer = new ArrayBuffer(32);
204 view = new Int8Array(buffer, 0, 16);
205 var retval = window.crypto.getRandomValues(view);
206 ok(view === retval, "The view and return value are the same");
208 SimpleTest.finish();
210 </script>
211 </body></html>