test_getUserMedia_permission.html (3463B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script src="mediaStreamPlayback.js"></script> 5 </head> 6 <body> 7 <pre id="test"> 8 <script type="application/javascript"> 9 createHTML({ title: "Test getUserMedia in iframes", bug: "1371741" }); 10 /** 11 Tests covering enumerateDevices API and deviceId constraint. Exercise code. 12 */ 13 14 // Call gUM in iframe. 15 async function iframeGum(dict, iframe = document.createElement("iframe")) { 16 Object.assign(iframe, dict); 17 if (dict.src) { 18 info(`<iframe src="${dict.src}" sandbox="${dict.sandbox}">`); 19 } else { 20 info(`<iframe srcdoc sandbox="${dict.sandbox}">`); 21 } 22 document.documentElement.appendChild(iframe); 23 24 const once = (t, msg) => new Promise(r => t.addEventListener(msg, r, { once: true })); 25 const haveMessage = once(window, "message"); 26 await new Promise(resolve => iframe.onload = resolve); 27 return (await haveMessage).data; 28 }; 29 30 runTest(async () => { 31 const path = "/tests/dom/media/webrtc/tests/mochitests/test_getUserMedia_permission_iframe.html"; 32 33 async function sourceFn() { 34 try { 35 const gUM = c => navigator.mediaDevices.getUserMedia(c); 36 let message; 37 let stream; 38 try { 39 stream = await gUM({ video: true }); 40 message = 'success'; 41 } catch(e) { 42 message = e.name; 43 } 44 parent.postMessage(message, 'https://example.com:443'); 45 46 if (message == "success") { 47 stream.getTracks().forEach(track => track.stop()); 48 } 49 } catch (e) { 50 setTimeout(() => { throw e; }); 51 } 52 } 53 54 const source = `<html\><script\>(${sourceFn.toString()})()</script\></html\>`; 55 56 // Test gUM in sandboxed vs. regular iframe. 57 58 for (const origin of [window.location.origin, "https://test1.example.com"]) { 59 const src = origin + path; 60 is(await iframeGum({ src, sandbox: "allow-scripts" }), 61 "NotAllowedError", "gUM fails in sandboxed iframe " + origin); 62 } 63 is(await iframeGum({ 64 src: path, 65 sandbox: "allow-scripts allow-same-origin", 66 }), 67 "success", "gUM works in regular same-origin iframe"); 68 is(await iframeGum({ 69 src: `https://test1.example.com${path}`, 70 sandbox: "allow-scripts allow-same-origin", 71 }), 72 "NotAllowedError", "gUM fails in regular cross-origin iframe"); 73 74 // Test gUM in sandboxed vs regular srcdoc iframe 75 76 const iframeSrcdoc = document.createElement("iframe"); 77 iframeSrcdoc.srcdoc = source; 78 is(await iframeGum({ sandbox: "allow-scripts" }, iframeSrcdoc), 79 "NotAllowedError", "gUM fails in sandboxed srcdoc iframe"); 80 is(await iframeGum({ sandbox: "allow-scripts allow-same-origin" }, iframeSrcdoc), 81 "success", "gUM works in regular srcdoc iframe"); 82 83 // Test gUM in sandboxed vs regular blob iframe 84 85 const blob = new Blob([source], {type : "text/html"}); 86 let src = URL.createObjectURL(blob); 87 is(await iframeGum({ src, sandbox: "allow-scripts" }), 88 "NotAllowedError", "gUM fails in sandboxed blob iframe"); 89 is(await iframeGum({ src, sandbox: "allow-scripts allow-same-origin"}), 90 "success", "gUM works in regular blob iframe"); 91 URL.revokeObjectURL(src); 92 93 // data iframes always have null-principals 94 95 src = `data:text/html;base64,${btoa(source)}`; 96 is(await iframeGum({ src, sandbox: "allow-scripts" }), 97 "NotAllowedError", "gUM fails in sandboxed data iframe"); 98 is(await iframeGum({ src, sandbox: "allow-scripts allow-same-origin"}), 99 "NotAllowedError", "gUM fails in regular data iframe"); 100 }); 101 </script> 102 </pre> 103 </body> 104 </html>