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