summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/protocol/candidate-exchange.https.html
blob: c54f26e6d80dd9056e8ebc11df929b51f69f88eb (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html>
<head>
<title>Candidate exchange</title>
<meta name=timeout content=long>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../RTCPeerConnection-helper.js"></script>
</head>
<body>
<script>

class StateLogger {
  constructor(source, eventname, field) {
    source.addEventListener(eventname, event => {
      this.events.push(source[field]);
    });
    this.events = [source[field]];
  }
}

class IceStateLogger extends StateLogger {
  constructor(source) {
    super(source, 'iceconnectionstatechange', 'iceConnectionState');
  }
}

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1.createDataChannel('datachannel');
  pc1IceStates = new IceStateLogger(pc1);
  pc2IceStates = new IceStateLogger(pc1);
  exchangeIceCandidates(pc1, pc2);
  await exchangeOfferAnswer(pc1, pc2);
  // Note - it's been claimed that this state sometimes jumps straight
  // to "completed". If so, this test should be flaky.
  await waitForIceStateChange(pc1, ['connected']);
  assert_array_equals(pc1IceStates.events, ['new', 'checking', 'connected']);
  assert_array_equals(pc2IceStates.events, ['new', 'checking', 'connected']);
}, 'Two way ICE exchange works');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1IceStates = new IceStateLogger(pc1);
  pc2IceStates = new IceStateLogger(pc1);
  let candidates = [];
  pc1.createDataChannel('datachannel');
  pc1.onicecandidate = e => {
    candidates.push(e.candidate);
  }
  // Candidates from PC2 are not delivered to pc1, so pc1 will use
  // peer-reflexive candidates.
  await exchangeOfferAnswer(pc1, pc2);
  const waiter = waitForIceGatheringState(pc1, ['complete']);
  await waiter;
  for (const candidate of candidates) {
    if (candidate) {
      pc2.addIceCandidate(candidate);
    }
  }
  await Promise.all([waitForIceStateChange(pc1, ['connected', 'completed']),
                     waitForIceStateChange(pc2, ['connected', 'completed'])]);
  const candidate_pair = pc1.sctp.transport.iceTransport.getSelectedCandidatePair();
  assert_equals(candidate_pair.local.type, 'host');
  assert_equals(candidate_pair.remote.type, 'prflx');
  assert_array_equals(pc1IceStates.events, ['new', 'checking', 'connected']);
  assert_array_equals(pc2IceStates.events, ['new', 'checking', 'connected']);
}, 'Adding only caller -> callee candidates gives a connection');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1IceStates = new IceStateLogger(pc1);
  pc2IceStates = new IceStateLogger(pc1);
  let candidates = [];
  pc1.createDataChannel('datachannel');
  pc2.onicecandidate = e => {
    candidates.push(e.candidate);
  }
  // Candidates from pc1 are not delivered to pc2.  so pc2 will use
  // peer-reflexive candidates.
  await exchangeOfferAnswer(pc1, pc2);
  const waiter = waitForIceGatheringState(pc2, ['complete']);
  await waiter;
  for (const candidate of candidates) {
    if (candidate) {
      pc1.addIceCandidate(candidate);
    }
  }
  await Promise.all([waitForIceStateChange(pc1, ['connected', 'completed']),
                     waitForIceStateChange(pc2, ['connected', 'completed'])]);
  const candidate_pair = pc2.sctp.transport.iceTransport.getSelectedCandidatePair();
  assert_equals(candidate_pair.local.type, 'host');
  assert_equals(candidate_pair.remote.type, 'prflx');
  assert_array_equals(pc1IceStates.events, ['new', 'checking', 'connected']);
  assert_array_equals(pc2IceStates.events, ['new', 'checking', 'connected']);
}, 'Adding only callee -> caller candidates gives a connection');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1IceStates = new IceStateLogger(pc1);
  pc2IceStates = new IceStateLogger(pc1);
  let pc2ToPc1Candidates = [];
  pc1.createDataChannel('datachannel');
  pc2.onicecandidate = e => {
    pc2ToPc1Candidates.push(e.candidate);
    // This particular test verifies that candidates work
    // properly if added from the pc2 onicecandidate event.
    if (!e.candidate) {
      for (const candidate of pc2ToPc1Candidates) {
        if (candidate) {
          pc1.addIceCandidate(candidate);
        }
      }
    }
  }
  // Candidates from |pc1| are not delivered to |pc2|. |pc2| will use
  // peer-reflexive candidates.
  await exchangeOfferAnswer(pc1, pc2);
  await Promise.all([waitForIceStateChange(pc1, ['connected', 'completed']),
                     waitForIceStateChange(pc2, ['connected', 'completed'])]);
  const candidate_pair = pc2.sctp.transport.iceTransport.getSelectedCandidatePair();
  assert_equals(candidate_pair.local.type, 'host');
  assert_equals(candidate_pair.remote.type, 'prflx');
  assert_array_equals(pc1IceStates.events, ['new', 'checking', 'connected']);
  assert_array_equals(pc2IceStates.events, ['new', 'checking', 'connected']);
}, 'Adding callee -> caller candidates from end-of-candidates gives a connection');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1IceStates = new IceStateLogger(pc1);
  pc2IceStates = new IceStateLogger(pc1);
  let pc1ToPc2Candidates = [];
  let pc2ToPc1Candidates = [];
  pc1.createDataChannel('datachannel');
  pc1.onicecandidate = e => {
    pc1ToPc2Candidates.push(e.candidate);
  }
  pc2.onicecandidate = e => {
    pc2ToPc1Candidates.push(e.candidate);
  }
  const offer = await pc1.createOffer();
  await Promise.all([pc1.setLocalDescription(offer),
                     pc2.setRemoteDescription(offer)]);
  const answer = await pc2.createAnswer();
  await waitForIceGatheringState(pc1, ['complete']);
  await pc2.setLocalDescription(answer).then(() => {
    for (const candidate of pc1ToPc2Candidates) {
      if (candidate) {
        pc2.addIceCandidate(candidate);
      }
    }
  });
  await waitForIceGatheringState(pc2, ['complete']);
  pc1.setRemoteDescription(answer).then(async () => {
    for (const candidate of pc2ToPc1Candidates) {
      if (candidate) {
        await pc1.addIceCandidate(candidate);
      }
    }
  });
  await Promise.all([waitForIceStateChange(pc1, ['connected', 'completed']),
                     waitForIceStateChange(pc2, ['connected', 'completed'])]);
  const candidate_pair =
        pc1.sctp.transport.iceTransport.getSelectedCandidatePair();
  assert_equals(candidate_pair.local.type, 'host');
  // When we supply remote candidates, we expect a jump to the 'host' candidate,
  // but it might also remain as 'prflx'.
  assert_true(candidate_pair.remote.type == 'host' ||
              candidate_pair.remote.type == 'prflx');
  assert_array_equals(pc1IceStates.events, ['new', 'checking', 'connected']);
  assert_array_equals(pc2IceStates.events, ['new', 'checking', 'connected']);
}, 'Explicit offer/answer exchange gives a connection');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  pc1.createDataChannel('datachannel');
  pc1.onicecandidate = assert_unreached;
  const offer = await pc1.createOffer();
  await pc1.setLocalDescription(offer);
  await new Promise(resolve => {
    pc1.onicecandidate = resolve;
  });
}, 'Candidates always arrive after setLocalDescription(offer) resolves');

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  pc1.createDataChannel('datachannel');
  pc2.onicecandidate = assert_unreached;
  const offer = await pc1.createOffer();
  await pc2.setRemoteDescription(offer);
  await pc2.setLocalDescription(await pc2.createAnswer());
  await new Promise(resolve => {
    pc2.onicecandidate = resolve;
  });
}, 'Candidates always arrive after setLocalDescription(answer) resolves');

</script>
</body>
</html>