tor-browser

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

test_eme_createMediaKeys_iframes.html (6354B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test creation of MediaKeys in iframes</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7  <script type="text/javascript" src="manifest.js"></script>
      8  <script type="text/javascript" src="eme.js"></script>
      9 </head>
     10 <body>
     11 <pre id="test">
     12 <script class="testbody">
     13 // Helper functions.
     14 
     15 // We take navigator explicitly as an argument to avoid ambiguity in fetching
     16 // it. This is to avoid issues with the following
     17 // ```
     18 // iframe.contentWindow.createMediaKeys = createMediaKeys;
     19 // await iframe.contentWindow.createMediaKeys();
     20 // ```
     21 // If we don't pass a navigator, and just use `navigator` in the function, this
     22 // ends up being equivalent to
     23 // ```
     24 // iframe.contentWindow.createMediaKeys = createMediaKeys;
     25 // await iframe.contentWindow.createMediaKeys(window.navigator);
     26 // ```
     27 // i.e. the function will use the navigator from the global window for the top
     28 // browsing context, not the iframe's. This would result in the tests not
     29 // correctly testing within the iframe.
     30 async function createMediaKeys(aNavigator) {
     31  const clearKeyOptions = [
     32    {
     33      initDataTypes: ["webm"],
     34      videoCapabilities: [{ contentType: 'video/webm; codecs="vp9"' }],
     35    },
     36  ];
     37 
     38  let access = await aNavigator.requestMediaKeySystemAccess(
     39    "org.w3.clearkey",
     40    clearKeyOptions
     41  );
     42 
     43  return access.createMediaKeys();
     44 }
     45 // End helper functions.
     46 
     47 // These tests check that the following work using different iframe combinations
     48 // - navigator.requestMediaKeySystem(...) successfully grants access.
     49 // - the resulting MediaKeySystemAccess object's createMediaKeys() creates
     50 //   MediaKeys as expected.
     51 
     52 // Same origin iframe, using src attribute, wait for onload.
     53 add_task(async () => {
     54  info(
     55    "Starting same origin iframe, using src attribute, wait for onload test"
     56  );
     57  let iframe = document.createElement("iframe");
     58  let iframeLoadPromise = new Promise(r => {
     59    iframe.onload = r;
     60  });
     61  iframe.src = "file_eme_createMediaKeys.html";
     62  document.body.appendChild(iframe);
     63  await iframeLoadPromise;
     64  info("iframe loaded");
     65 
     66  // Setup our handler for when the iframe messages to tell us if it
     67  // created MediaKeys or not.
     68  let iframeMessagePromise = new Promise(r => {
     69    window.onmessage = message => {
     70      is(
     71        message.data,
     72        "successCreatingMediaKeys",
     73        "iframe should have posted us a message saying keys were successfully created"
     74      );
     75      r();
     76    };
     77  });
     78  // Post a message to the iframe to ask it to try and create media keys.
     79  iframe.contentWindow.postMessage("", "*");
     80  // Wait until we've got a message back from our iframe.
     81  await iframeMessagePromise;
     82 });
     83 
     84 // Same origin iframe, call via JS, wait for onload.
     85 add_task(async () => {
     86  info("Starting same origin iframe, call via JS, wait for onload test");
     87  let iframe = document.createElement("iframe");
     88  let iframeLoadPromise = new Promise(r => {
     89    iframe.onload = r;
     90  });
     91  iframe.src = ""; // No src iframes are same origin.
     92  document.body.appendChild(iframe);
     93  await iframeLoadPromise;
     94  info("iframe loaded");
     95 
     96  try {
     97    iframe.contentWindow.createMediaKeys = createMediaKeys;
     98    let mediaKeys = await iframe.contentWindow.createMediaKeys(
     99      iframe.contentWindow.navigator
    100    );
    101    ok(mediaKeys, "Should get media keys");
    102  } catch (e) {
    103    ok(
    104      false,
    105      `Should not get any errors while trying to get media keys, got ${e}`
    106    );
    107  }
    108 });
    109 
    110 // Same origin iframe, call via JS, *do not* wait for onload.
    111 //
    112 // Note, sites shouldn't do this, because
    113 // https://bugzilla.mozilla.org/show_bug.cgi?id=543435
    114 // means not waiting for onload results in weird behavior, however
    115 // https://bugzilla.mozilla.org/show_bug.cgi?id=1675360
    116 // shows sites doing this in the wild because historically this worked in
    117 // Firefox.
    118 //
    119 // Breaking this test case isn't necessarily against any specifications
    120 // I'm (bryce) aware of, but it will probably break site compat, so be really
    121 // sure you want to before doing so.
    122 add_task(async () => {
    123  info(
    124    "Starting same origin iframe, call via JS, *do not* wait for onload test"
    125  );
    126  let iframe = document.createElement("iframe");
    127  let iframeLoadPromise = new Promise(r => {
    128    iframe.onload = r;
    129  });
    130  iframe.src = ""; // No src iframes are same origin.
    131  document.body.appendChild(iframe);
    132  info("iframe appended (we're not waiting for load)");
    133 
    134  try {
    135    iframe.contentWindow.createMediaKeys = createMediaKeys;
    136    let mediaKeys = await iframe.contentWindow.createMediaKeys(
    137      iframe.contentWindow.navigator
    138    );
    139    ok(mediaKeys, "Should get media keys");
    140 
    141    // We await the load to see if they keys persist through the load.
    142    // This could fail if gecko internally associates the keys with the
    143    // about:blank page that is replaced by the load.
    144    await iframeLoadPromise;
    145    ok(mediaKeys, "Media keys should still exist after the load");
    146  } catch (e) {
    147    ok(
    148      false,
    149      `Should not get any errors while trying to get media keys, got ${e}`
    150    );
    151  }
    152 });
    153 
    154 // Different origin iframe, using src attribute, wait for onload
    155 add_task(async () => {
    156  info(
    157    "Starting different origin iframe, using src attribute, wait for onload test"
    158  );
    159  let iframe = document.createElement("iframe");
    160  let iframeLoadPromise = new Promise(r => {
    161    iframe.onload = r;
    162  });
    163  // Make our iframe cross origin (see build/pgo/server-locations.txt for more
    164  // info the url used).
    165  iframe.src =
    166    "https://w3c-test.org:443/tests/dom/media/test/file_eme_createMediaKeys.html";
    167  iframe.allow = "encrypted-media";
    168  document.body.appendChild(iframe);
    169  await iframeLoadPromise;
    170  info("iframe loaded");
    171 
    172  // Setup our handler for when the iframe messages to tell us if it
    173  // created MediaKeys or not.
    174  let iframeMessagePromise = new Promise(r => {
    175    window.onmessage = message => {
    176      is(
    177        message.data,
    178        "successCreatingMediaKeys",
    179        "iframe should have posted us a message saying keys were successfully created"
    180      );
    181      r();
    182    };
    183  });
    184  // Post a message to the iframe to ask it to try and create media keys.
    185  iframe.contentWindow.postMessage("", "*");
    186  // Wait until we've got a message back from our iframe.
    187  await iframeMessagePromise;
    188 });
    189 </script>
    190 </pre>
    191 </body>
    192 </html>