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" />
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 } ];
26 function testNsCryptoGetRandomValues(aLength, aType)
35 Float32: Float32Array,
36 Float64: Float64Array,
37 Uint8Clamped: Uint8ClampedArray,
42 var buf = new ArrayBuffer(aLength);
43 var arBuf = new arrayTypes[aType](buf);
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!!!
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;
69 for (var i =
0; i < testData.length; i++) {
70 if (testData[i].pass) {
72 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
75 ok(false,
"testNsCryptoGetRandomValues failed, test should have passed: " + testData[i].type);
79 // failing tests are dealt with here
82 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
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");
94 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
97 failedWithCorrectError = ex.toString().search(/TYPE_MISMATCH_ERR/);
98 ok(failedWithCorrectError,
99 "Expected TYPE_MISMATCH_ERR: Float32Array is not valid, got " + ex +
".");
105 testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
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
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
124 window.crypto.getRandomValues(null);
127 var test = ex.toString().search(/
1003|TypeError/);
128 ok((test
> -
1),
"Expected TYPE_ERR, got " + ex +
".");
131 // Test a zero-length buffer view
133 var a = new Int8Array(
0);
134 window.crypto.getRandomValues(a);
135 ok(a[
0] === undefined,
"The array buffer is unchanged, still 0 length");
138 ok(false,
"A zero-length array buffer view should not fail");
141 // Test a one-length buffer view
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
148 var a = new Uint8Array(
1);
149 var b = window.crypto.getRandomValues(a);
151 ok(a === b,
"ArrayBuffer result is argument buffer");
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,
166 // Test a
31 byte length buffer
167 testConfig = { len:
31, type:
"Int8", pass: true };
168 testNsCryptoGetRandomValues(testConfig.len,
172 // Test a
33 byte length buffer
173 testConfig = { len:
33, type:
"Int8", pass: true };
174 testNsCryptoGetRandomValues(testConfig.len,
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++) {
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
193 var b = window.crypto.getRandomValues(view2);
194 for (var i =
0; i < view2.length; i++) {
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");