| Philipp Hancke | b7014f7 | 2017-02-27 13:24:20 | [diff] [blame] | 1 | <!doctype html> |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 2 | <meta charset=utf-8> |
| 3 | <title>RTCPeerConnection.prototype.setRemoteDescription</title> |
| 4 | <script src="/resources/testharness.js"></script> |
| 5 | <script src="/resources/testharnessreport.js"></script> |
| 6 | <script src="RTCPeerConnection-helper.js"></script> |
| 7 | <script> |
| Soares Chen | 0f17643 | 2017-05-04 04:40:47 | [diff] [blame] | 8 | 'use strict'; |
| 9 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 10 | // Test is based on the following editor draft: |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 11 | // https://w3c.github.io/webrtc-pc/archives/20170605/webrtc.html |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 12 | |
| Rick Waldron | 1c5c05d | 2017-06-07 17:54:42 | [diff] [blame] | 13 | // The following helper functions are called from RTCPeerConnection-helper.js: |
| Jan-Ivar Bruaroey | 6d3fda5 | 2018-07-04 13:49:09 | [diff] [blame] | 14 | // assert_session_desc_not_similar() |
| 15 | // assert_session_desc_similar() |
| Rick Waldron | 1c5c05d | 2017-06-07 17:54:42 | [diff] [blame] | 16 | |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 17 | /* |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 18 | 4.3.2. Interface Definition |
| 19 | [Constructor(optional RTCConfiguration configuration)] |
| 20 | interface RTCPeerConnection : EventTarget { |
| 21 | Promise<void> setRemoteDescription( |
| 22 | RTCSessionDescriptionInit description); |
| 23 | |
| 24 | readonly attribute RTCSessionDescription? remoteDescription; |
| 25 | readonly attribute RTCSessionDescription? currentRemoteDescription; |
| 26 | readonly attribute RTCSessionDescription? pendingRemoteDescription; |
| 27 | ... |
| 28 | }; |
| 29 | |
| 30 | 4.6.2. RTCSessionDescription Class |
| 31 | dictionary RTCSessionDescriptionInit { |
| 32 | required RTCSdpType type; |
| 33 | DOMString sdp = ""; |
| 34 | }; |
| 35 | |
| 36 | 4.6.1. RTCSdpType |
| 37 | enum RTCSdpType { |
| 38 | "offer", |
| 39 | "pranswer", |
| 40 | "answer", |
| 41 | "rollback" |
| 42 | }; |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 43 | */ |
| 44 | |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 45 | /* |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 46 | 4.6.1. enum RTCSdpType |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 47 | */ |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 48 | promise_test(async t => { |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 49 | const pc = new RTCPeerConnection(); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 50 | t.add_cleanup(() => pc.close()); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 51 | |
| 52 | // SDP is validated after WebIDL validation |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 53 | try { |
| 54 | await pc.setRemoteDescription({ type: 'bogus', sdp: 'bogus' }); |
| 55 | t.unreached_func("Should have rejected."); |
| 56 | } catch (e) { |
| Stephen McGruer | 2c5c3c4 | 2020-01-23 15:51:07 | [diff] [blame] | 57 | assert_throws_js(TypeError, () => { throw e }); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 58 | } |
| Soares Chen | adb6176 | 2017-08-31 07:51:38 | [diff] [blame] | 59 | }, 'setRemoteDescription with invalid type and invalid SDP should reject with TypeError'); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 60 | |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 61 | promise_test(async t => { |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 62 | const pc = new RTCPeerConnection(); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 63 | t.add_cleanup(() => pc.close()); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 64 | |
| 65 | // SDP is validated after validating type |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 66 | try { |
| 67 | await pc.setRemoteDescription({ type: 'answer', sdp: 'invalid' }); |
| 68 | t.unreached_func("Should have rejected."); |
| 69 | } catch (e) { |
| Stephen McGruer | d510304 | 2020-01-23 21:45:45 | [diff] [blame] | 70 | assert_throws_dom('InvalidStateError', () => { throw e }); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 71 | } |
| Soares Chen | adb6176 | 2017-08-31 07:51:38 | [diff] [blame] | 72 | }, 'setRemoteDescription() with invalid SDP and stable state should reject with InvalidStateError'); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 73 | |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 74 | /* Dedicated signalingstate events test. */ |
| 75 | |
| 76 | promise_test(async t => { |
| 77 | const pc = new RTCPeerConnection(); |
| 78 | const pc2 = new RTCPeerConnection(); |
| 79 | t.add_cleanup(() => pc.close()); |
| 80 | t.add_cleanup(() => pc2.close()); |
| 81 | |
| 82 | let eventCount = 0; |
| 83 | const states = [ |
| Jan-Ivar Bruaroey | 75c1b74 | 2018-05-17 19:47:17 | [diff] [blame] | 84 | 'stable', 'have-local-offer', 'stable', 'have-remote-offer', |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 85 | ]; |
| Jan-Ivar Bruaroey | 75c1b74 | 2018-05-17 19:47:17 | [diff] [blame] | 86 | pc.onsignalingstatechange = t.step_func(() => |
| 87 | assert_equals(pc.signalingState, states[++eventCount])); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 88 | |
| 89 | const assert_state = state => { |
| 90 | assert_equals(state, pc.signalingState); |
| 91 | assert_equals(state, states[eventCount]); |
| 92 | }; |
| 93 | |
| Youenn Fablet | 59e5213 | 2018-11-04 04:28:44 | [diff] [blame] | 94 | const offer = await generateAudioReceiveOnlyOffer(pc); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 95 | assert_state('stable'); |
| 96 | await pc.setLocalDescription(offer); |
| 97 | assert_state('have-local-offer'); |
| 98 | await pc2.setRemoteDescription(offer); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 99 | await exchangeAnswer(pc, pc2); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 100 | assert_state('stable'); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 101 | await exchangeOffer(pc2, pc); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 102 | assert_state('have-remote-offer'); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 103 | }, 'Negotiation should fire signalingsstate events'); |
| 104 | |
| Soares Chen | a01b1e5 | 2017-07-07 09:52:03 | [diff] [blame] | 105 | /* Operations after returning to stable state */ |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 106 | |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 107 | promise_test(async t => { |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 108 | const pc = new RTCPeerConnection(); |
| 109 | const pc2 = new RTCPeerConnection(); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 110 | t.add_cleanup(() => pc.close()); |
| 111 | t.add_cleanup(() => pc2.close()); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 112 | |
| Youenn Fablet | 59e5213 | 2018-11-04 04:28:44 | [diff] [blame] | 113 | const offer1 = await generateAudioReceiveOnlyOffer(pc2); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 114 | await pc2.setLocalDescription(offer1); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 115 | await pc.setRemoteDescription(offer1); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 116 | await exchangeAnswer(pc2, pc); |
| Youenn Fablet | 59e5213 | 2018-11-04 04:28:44 | [diff] [blame] | 117 | const offer2 = await generateVideoReceiveOnlyOffer(pc2); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 118 | await pc2.setLocalDescription(offer2); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 119 | await pc.setRemoteDescription(offer2); |
| Jan-Ivar Bruaroey | 6d3fda5 | 2018-07-04 13:49:09 | [diff] [blame] | 120 | assert_session_desc_not_similar(offer1, offer2); |
| 121 | assert_session_desc_similar(pc.remoteDescription, offer2); |
| 122 | assert_session_desc_similar(pc.currentRemoteDescription, offer1); |
| 123 | assert_session_desc_similar(pc.pendingRemoteDescription, offer2); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 124 | }, 'Calling setRemoteDescription() again after one round of remote-offer/local-answer should succeed'); |
| 125 | |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 126 | promise_test(async t => { |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 127 | const pc = new RTCPeerConnection(); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 128 | const pc2 = new RTCPeerConnection(); |
| 129 | t.add_cleanup(() => pc.close()); |
| 130 | t.add_cleanup(() => pc2.close()); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 131 | |
| Youenn Fablet | 59e5213 | 2018-11-04 04:28:44 | [diff] [blame] | 132 | const offer = await generateAudioReceiveOnlyOffer(pc); |
| Jan-Ivar Bruaroey | 85e0c94 | 2018-05-16 14:38:06 | [diff] [blame] | 133 | await pc.setLocalDescription(offer); |
| 134 | await pc2.setRemoteDescription(offer); |
| 135 | const answer = await pc2.createAnswer(); |
| 136 | await pc2.setLocalDescription(answer); |
| 137 | await pc.setRemoteDescription(answer); |
| Amit Hilbuch | eced296 | 2019-01-07 18:23:30 | [diff] [blame] | 138 | await exchangeOffer(pc2, pc); |
| Jan-Ivar Bruaroey | 1387c7a | 2024-03-11 16:12:28 | [diff] [blame^] | 139 | assert_equals(pc.remoteDescription, pc.pendingRemoteDescription); |
| Jan-Ivar Bruaroey | 6d3fda5 | 2018-07-04 13:49:09 | [diff] [blame] | 140 | assert_session_desc_similar(pc.remoteDescription, offer); |
| 141 | assert_session_desc_similar(pc.currentRemoteDescription, answer); |
| Soares Chen | fda8782 | 2017-06-05 14:00:30 | [diff] [blame] | 142 | }, 'Switching role from offerer to answerer after going back to stable state should succeed'); |
| 143 | |
| Jan-Ivar Bruaroey | 03b02ea | 2021-09-20 15:58:42 | [diff] [blame] | 144 | promise_test(async t => { |
| 145 | const pc = new RTCPeerConnection(); |
| 146 | t.add_cleanup(() => pc.close()); |
| 147 | const offer = await pc.createOffer(); |
| 148 | const p = Promise.race([ |
| 149 | pc.setRemoteDescription(offer), |
| 150 | new Promise(r => t.step_timeout(() => r("timeout"), 200)) |
| 151 | ]); |
| 152 | pc.close(); |
| 153 | assert_equals(await p, "timeout"); |
| 154 | assert_equals(pc.signalingState, "closed", "In closed state"); |
| 155 | }, 'Closing on setRemoteDescription() neither resolves nor rejects'); |
| 156 | |
| 157 | promise_test(async t => { |
| 158 | const pc = new RTCPeerConnection(); |
| 159 | t.add_cleanup(() => pc.close()); |
| 160 | const offer = await pc.createOffer(); |
| 161 | await pc.setLocalDescription(offer); |
| 162 | const p = Promise.race([ |
| 163 | pc.setRemoteDescription(offer), |
| 164 | new Promise(r => t.step_timeout(() => r("timeout"), 200)) |
| 165 | ]); |
| 166 | pc.close(); |
| 167 | assert_equals(await p, "timeout"); |
| 168 | assert_equals(pc.signalingState, "closed", "In closed state"); |
| 169 | }, 'Closing on rollback neither resolves nor rejects'); |
| Soares Chen | 0f17643 | 2017-05-04 04:40:47 | [diff] [blame] | 170 | |
| Philipp Hancke | b7014f7 | 2017-02-27 13:24:20 | [diff] [blame] | 171 | </script> |