bug 842265 - test_getRandomValues.html: loop until non-zero random value obtained r=bsmith
authorDavid Keeler <dkeeler@mozilla.com>
Thu, 21 Feb 2013 14:11:00 -0800
changeset 122683 3075a93108fb798871ce0db6da9ca56779b1b8b3
parent 122682 a5d725fc67d3816a4fa032bb843fd2bf005113c2
child 122684 d9bdd0a18d7812787bdb1517009cdf5e07112d81
push id24356
push usergszorc@mozilla.com
push dateSun, 24 Feb 2013 01:00:12 +0000
treeherdermozilla-central@195e706140d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmith
bugs842265
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 842265 - test_getRandomValues.html: loop until non-zero random value obtained r=bsmith
dom/tests/mochitest/crypto/test_getRandomValues.html
--- a/dom/tests/mochitest/crypto/test_getRandomValues.html
+++ b/dom/tests/mochitest/crypto/test_getRandomValues.html
@@ -135,25 +135,33 @@ function onWindowLoad()
     window.crypto.getRandomValues(a);
     ok(a[0] === undefined, "The array buffer is unchanged, still 0 length");
   }
   catch (ex) {
     ok(false, "A zero-length array buffer view should not fail");
   }
 
   // Test a one-length buffer view
-  try {
-    var a = new Int8Array(1);
-    var b = window.crypto.getRandomValues(a);
-    ok(a[0] !== 0, "The array buffer has one random value");
-    ok(a === b, "ArrayBuffer result is argument buffer");
+  var randomVal = 0;
+  // The probability of getRandomValues generating a zero value is 1/256 so we
+  // run this in a loop until it returns a non-zero value to guard against
+  // false failures
+  do {
+    try {
+      var a = new Uint8Array(1);
+      var b = window.crypto.getRandomValues(a);
+      randomVal = a[0];
+      ok(a === b, "ArrayBuffer result is argument buffer");
+    }
+    catch (ex) {
+      ok(false, "A one-length array buffer view should not fail");
+    }
   }
-  catch (ex) {
-    ok(false, "A one-length array buffer view should not fail");
-  }
+  while (randomVal == 0);
+  ok(randomVal !== 0, "The array buffer eventually had one random value");
 
   // Test a 16 byte length buffer
   var testConfig = { len: 16, type: "Int8", pass: true };
   testNsCryptoGetRandomValues(testConfig.len,
                               testConfig.type,
                               testConfig.pass);
 
   // Test a 31 byte length buffer