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