webrtc.js (1974B)
1 // Creates two RTCPeerConnection and tries to connect them. Returns 2 // "allowed" if the connection is permitted, "blocked" if it is 3 // blocked on both sides and "inconsistent" in the event that the 4 // result is not the same on both sides (should never happen). 5 async function tryConnect() { 6 const pc1 = new RTCPeerConnection(); 7 const pc2 = new RTCPeerConnection(); 8 9 // Returns a promise which resolves to a boolean which is true 10 // if and only if pc.iceConnectionState settles in the "failed" 11 // state, and never transitions to any state other than "new" 12 // or "failed." 13 const pcFailed = (pc) => { 14 return new Promise((resolve, _reject) => { 15 pc.oniceconnectionstatechange = (e) => { 16 resolve(pc.iceConnectionState == "failed"); 17 }; 18 }); 19 } 20 pc1Failed = pcFailed(pc1); 21 pc2Failed = pcFailed(pc2); 22 23 // Creating a data channel is necessary to induce negotiation: 24 const channel = pc1.createDataChannel('test'); 25 26 // Usual webrtc signaling dance: 27 pc1.onicecandidate = ({candidate}) => pc2.addIceCandidate(candidate); 28 pc2.onicecandidate = ({candidate}) => pc1.addIceCandidate(candidate); 29 const offer = await pc1.createOffer(); 30 await pc1.setLocalDescription(offer); 31 await pc2.setRemoteDescription(pc1.localDescription); 32 const answer = await pc2.createAnswer(); 33 await pc2.setLocalDescription(answer); 34 await pc1.setRemoteDescription(pc2.localDescription); 35 36 const failed1 = await pc1Failed; 37 const failed2 = await pc2Failed; 38 if(failed1 && failed2) { 39 return 'blocked'; 40 } else if(!failed1 && !failed2) { 41 return 'allowed'; 42 } else { 43 return 'inconsistent'; 44 } 45 } 46 47 async function expectAllow() { 48 promise_test(async () => assert_equals(await tryConnect(), 'allowed')); 49 } 50 51 async function expectBlock() { 52 promise_test(async () => assert_equals(await tryConnect(), 'blocked')); 53 } 54 55 // vim: set ts=4 sw=4 et :