summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_mediarecorder_principals.html
blob: 00dcaa5a5be22afbc506e1f0e708e21da4c016f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=489415
-->
<head>
  <title>Test for MediaRecorder Reaction to Principal Change</title>
  <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<div>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1018299">Test for MediaRecorder Principal Handling</a>
</div>

<pre id="test">
<script type="text/javascript">
SimpleTest.waitForExplicitFinish();

let throwOutside = e => setTimeout(() => { throw e; });

// Loading data from a resource that changes origins while streaming should
// be detected by the media cache and result in a null principal so that the
// MediaRecorder usages below fail.

// This test relies on midflight-redirect.sjs returning the the first quarter of
// the resource as a byte range response, and then hanging up, and when Firefox
// requests the remainder midflight-redirect.sjs serves a redirect to another origin.

async function testPrincipals(resource) {
  if (!resource) {
    todo(false, "No types supported");
    return;
  }
  await testPrincipals1(resource);
  await testPrincipals2(resource);
}

function makeVideo() {
  let video = document.createElement("video");
  video.preload = "metadata";
  video.controls = true;
  document.body.appendChild(video);
  return video;
}

// First test: Load file from same-origin first, then get redirected to
// another origin before attempting to record stream.
async function testPrincipals1(resource) {
  let video = makeVideo();
  video.src =
      "http://mochi.test:8888/tests/dom/media/test/midflight-redirect.sjs" +
      "?resource=" + resource.name + "&type=" + resource.type;

  let errorBarrier = once(video, "error");
  // Wait for the video to load to metadata. We can then start capturing.
  // Must also handle the download bursting and hitting the error before we
  // reach loadedmetadata. Normally we reach loadedmetadata first, but
  // rarely we hit the redirect first.
  await Promise.race([once(video, "loadedmetadata"), errorBarrier]);

  let rec = new MediaRecorder(video.mozCaptureStreamUntilEnded());
  video.play();

  // Wait until we hit a playback error. This means our download has hit the redirect.
  await errorBarrier;

  // Try to record, it should be blocked with a security error.
  try {
    rec.start();
    ok(false, "mediaRecorder.start() must throw SecurityError, but didn't throw at all");
  } catch (ex) {
    is(ex.name, "SecurityError", "mediaRecorder.start() must throw SecurityError");
  }
  removeNodeAndSource(video);
}

// Second test: Load file from same-origin first, but record ASAP, before
// getting redirected to another origin.
async function testPrincipals2(resource) {
  let video = makeVideo();
  video.src =
      "http://mochi.test:8888/tests/dom/media/test/midflight-redirect.sjs" +
      "?resource=" + resource.name + "&type=" + resource.type;

  // Wait for the video to load to metadata. We can then start capturing.
  // Must also handle the download bursting and hitting the error before we
  // reach loadedmetadata. Normally we reach loadedmetadata first, but
  // rarely we hit the redirect first.
  await Promise.race([once(video, "loadedmetadata"), once(video, "error")]);

  let ended = false;
  once(video, "ended", () => ended = true);

  // Start capturing. It should work.
  let rec;
  let errorBarrier;
  try {
    rec = new MediaRecorder(video.mozCaptureStreamUntilEnded());
    errorBarrier = nextEvent(rec, "error");
    rec.start();
    ok(true, "mediaRecorder.start() should not throw here, and didn't");
  } catch (ex) {
    ok(false, "mediaRecorder.start() unexpectedly threw " + ex.name + " (" + ex.message + ")");
  }

  // Play the video, this should result in a SecurityError on the recorder.
  let hasStopped = once(rec, "stop");
  video.play();
  let error = (await errorBarrier).error;
  is(error.name, "SecurityError", "mediaRecorder.onerror must fire SecurityError");
  ok(error.stack.includes('test_mediarecorder_principals.html'),
    'Events fired from onerror should include an error with a stack trace indicating ' +
    'an error in this test');
  is(ended, false, "Playback should not have reached end");
  await hasStopped;
  is(ended, false, "Playback should definitely not have reached end");

  removeNodeAndSource(video);
}

testPrincipals({ name:"pixel_aspect_ratio.mp4", type:"video/mp4", duration:28 })
  .catch(e => throwOutside(e))
  .then(() => SimpleTest.finish());

</script>
</pre>

</body>
</html>