Bug 1443942 - Rewrite test_mixed_principals. r=jya
authorChris Pearce <cpearce@mozilla.com>
Wed, 28 Mar 2018 16:56:37 +1300
changeset 779714 482dbcc619ce83241380b6e9376b9f9d1014b433
parent 779713 ee486ce2d6604682d4f6fa5a3264120829ba2cbe
child 779715 e02b7f9e296d7f2373d4e77ab2f3567975f4aae3
push id105836
push userbmo:ato@sny.no
push dateTue, 10 Apr 2018 12:07:22 +0000
reviewersjya
bugs1443942
milestone61.0a1
Bug 1443942 - Rewrite test_mixed_principals. r=jya The original test is failing, as it assumed we'd not error when origins were mixed without CORS, and the original test was using outdated practises, so rewrite it. MozReview-Commit-ID: KlOH83GUOk
dom/media/test/test_mixed_principals.html
--- a/dom/media/test/test_mixed_principals.html
+++ b/dom/media/test/test_mixed_principals.html
@@ -2,93 +2,84 @@
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=489415
 -->
 <head>
   <title>Test for Bug 489415</title>
   <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
   <script type="text/javascript" src="manifest.js"></script>
+  <style>
+    video {
+      width: 40%;
+      border: solid black 1px;
+    }
+  </style>
 </head>
-<body>
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=489415">Mozilla Bug 489415</a>
-<p id="display"></p>
-
-<video id="v1" preload="metadata" onended="onendedcb('v1')"></video>
-<video id="v2" preload="metadata" onended="onendedcb('v2')"></video>
-
-<pre id="test">
-<script type="text/javascript">
-SimpleTest.waitForExplicitFinish();
-
-var pushPrefs = (...p) => SpecialPowers.pushPrefEnv({set: p});
-
-var v1 = document.getElementById("v1");
-var v2 = document.getElementById("v2");
-var count = 0;
 
-function onendedcb(id) {
-  var c = document.createElement("canvas");
-  var ctx = c.getContext("2d");
-  var v = document.getElementById(id);
-  ctx.drawImage(v, 0, 0);
-  try {
-    c.toDataURL();
-    ok(false, "Failed to throw exception in toDataURL for " + id);
-  } catch (ex) {
-    ok(true, "Threw exception in toDataURL for " + id);
-  }
-  if (++count == 2) {
-    SimpleTest.finish();
-  }
-}
+<body>
+  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=489415">Mozilla Bug 489415</a>
+  <p id="display"></p>
+  <pre id="test">
+  <script type="text/javascript">
+    SimpleTest.waitForExplicitFinish();
+
+    var pushPrefs = (...p) => SpecialPowers.pushPrefEnv({ set: p });
+    var count = 0;
 
-function testMixedPrincipals(resource) {
-  // In some OS(XP) can not play mp4. Add this checking for the test failure.
-  if (!resource || !v1.canPlayType(resource.type)) {
-    todo(false, "No types supported");
-    SimpleTest.finish();
-    return;
-  }
-  // Make sure the media cache size(50MB) is large enough that the data could
-  // be download in MediaCache completely.
-  return pushPrefs(['media.cache_size', 50*1024])
-  .then(() => {
-
-    // Generate a random key. The first load with that key will return
-    // data, the second and subsequent loads with that key will return a redirect
-    // to a different origin ('localhost:8888' will be redirected to 'example.org',
-    // and 'example.org' will be redirected to 'localhost:8888'). We rely on the
-    // fact that Ogg will do a seek to the end of the resource, triggering a new
-    // load with the same key which will return a same-origin resource.
-    // Loading data from two different origins should be detected by the media
-    // cache and result in a null principal so that the canvas usage above fails.
-    var key = Math.floor(Math.random()*100000000);
-
-    // In v1, try loading from same-origin first and then getting redirected to
-    // another origin.
-    v1.src =
-        "http://mochi.test:8888/tests/dom/media/test/dynamic_redirect.sjs?key=v1_" +
-        key + "&res=" + resource.name;
-    v1.onloadeddata = function () {
-      // To limit readahead, avoid racing with playback and "catching up" mode.
-      v1.play();
+    function canReadBack(video) {
+      var c = document.createElement("canvas");
+      var ctx = c.getContext("2d");
+      ctx.drawImage(video, 0, 0);
+      try {
+        c.toDataURL();
+        return true;
+      } catch (ex) {
+        return false;
+      }
     }
 
-    // In v2, try loading cross-origin first and then getting redirected to
-    // our origin.
-    v2.src = "http://example.org/tests/dom/media/test/dynamic_redirect.sjs?key=v2_" + key + "&res=" + resource.name;
-    v2.onloadeddata = function () {
-      // To limit readahead, avoid racing with playback and "catching up" mode.
-      v2.play();
+    function runTest(origin, shouldReadBackOnLoad) {
+      return new Promise(function (resolve, reject) {
+        // Load will redirect mid-flight, which will be detected and should error,
+        // and we should no longer be able to readback.
+        var noise = Math.floor(Math.random() * 100000000);
+        var video = document.createElement("video");
+        video.preload = "metadata";
+        video.controls = true;
+        var url = "http://" + origin + "/tests/dom/media/test/dynamic_redirect.sjs?key="
+                  + noise + "&res=pixel_aspect_ratio.mp4"
+                  + "&type=video/mp4";
+        SimpleTest.info("Loading from " + url);
+        video.src = url;
+        document.body.appendChild(video);
+
+        once(video, "loadeddata", () => {
+          is(canReadBack(video), shouldReadBackOnLoad, "Should be able to readback");
+          video.play();
+        });
+
+        once(video, "error", () => {
+          is(canReadBack(video), false, "Should not be able to readback after error");
+          removeNodeAndSource(video);
+          resolve();
+        });
+
+        once(video, "ended", () => {
+          ok(false, "Should not be able to playback to end, we should have errored!");
+          removeNodeAndSource(video);
+          resolve();
+        });
+
+      });
     }
-  });
-}
 
-testMixedPrincipals({ name:"pixel_aspect_ratio.mp4", type:"video/mp4", duration:28});
+    Promise.all([
+      runTest("mochi.test:8888", true),
+      runTest("example.org", false),
+    ]).then(SimpleTest.finish);
 
-</script>
+  </script>
 </pre>
-
 </body>
 </html>