tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_getRandomValues.html (6847B)


      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();
     10 
     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 } ];
     22 
     23 
     24 var testCount = 0;
     25 
     26 function testNsCryptoGetRandomValues(aLength, aType)
     27 {
     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,
     38  };
     39 
     40  testCount++;
     41 
     42  var buf = new ArrayBuffer(aLength);
     43  var arBuf = new arrayTypes[aType](buf);
     44 
     45  var pass = false;
     46  var b = window.crypto.getRandomValues(arBuf);
     47  ok(b === arBuf, "ArrayBuffer result is argument buffer");
     48 
     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;
     55      }
     56    }
     57    else {
     58      pass = true;
     59    }
     60  }
     61  is(pass, true,  "Non-zero result: " + i  +  " found in the  " + aType + ": " + aLength + " ArrayBufferView");
     62 }
     63 
     64 function onWindowLoad()
     65 {
     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);
     73        }
     74        catch (ex) {
     75          ok(false, "testNsCryptoGetRandomValues failed, test should have passed: " + testData[i].type);
     76        }
     77      }
     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);
     83          }
     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");
     89          }
     90        } // 8
     91 
     92        if (i == 9) {
     93          try {
     94            testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
     95          }
     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 + ".");
    100          }
    101        } // 9
    102 
    103        if (i == 10) {
    104          try {
    105            testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
    106          }
    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 + ".");
    111          }
    112        }
    113      }
    114    } // end main for loop
    115  }
    116  catch (ex) {
    117    ok(false, "Unexpected Error: " + ex);
    118  }
    119  // Count the tests in the testData array
    120  ok(testCount == 11, "11 tests run via testData");
    121 
    122  // Test a null argument
    123  try {
    124    window.crypto.getRandomValues(null);
    125  }
    126  catch (ex) {
    127    var test = ex.toString().search(/1003|TypeError/);
    128    ok((test > -1), "Expected TYPE_ERR, got " + ex + ".");
    129  }
    130 
    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");
    136  }
    137  catch (ex) {
    138    ok(false, "A zero-length array buffer view should not fail");
    139  }
    140 
    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");
    152    }
    153    catch (ex) {
    154      ok(false, "A one-length array buffer view should not fail");
    155    }
    156  }
    157  while (randomVal == 0);
    158  ok(randomVal !== 0, "The array buffer eventually had one random value");
    159 
    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);
    165 
    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);
    171 
    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);
    177 
    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;
    184  }
    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");
    189  }
    190 
    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;
    198    }
    199  }
    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");
    207 
    208  SimpleTest.finish();
    209 }
    210 </script>
    211 </body></html>