File: RTCPeerConnection-createDataChannel.html

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (616 lines) | stat: -rw-r--r-- 24,778 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!doctype html>
<meta charset=utf-8>
<title>RTCPeerConnection.prototype.createDataChannel</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="RTCPeerConnection-helper.js"></script>
<script>
'use strict';
// Test is based on the following revision:
// https://rawgit.com/w3c/webrtc-pc/1cc5bfc3ff18741033d804c4a71f7891242fb5b3/webrtc.html
/*
  6.1.  RTCPeerConnection Interface Extensions
    partial interface RTCPeerConnection {
        [...]
        RTCDataChannel createDataChannel(USVString label,
                                         optional RTCDataChannelInit dataChannelDict);
        [...]
    };
  6.2.  RTCDataChannel
    interface RTCDataChannel : EventTarget {
        readonly attribute USVString           label;
        readonly attribute boolean             ordered;
        readonly attribute unsigned short?     maxPacketLifeTime;
        readonly attribute unsigned short?     maxRetransmits;
        readonly attribute USVString           protocol;
        readonly attribute boolean             negotiated;
        readonly attribute unsigned short?     id;
        readonly attribute RTCPriorityType     priority;
        readonly attribute RTCDataChannelState readyState;
        readonly attribute unsigned long       bufferedAmount;
                 attribute unsigned long       bufferedAmountLowThreshold;
        [...]
                 attribute DOMString           binaryType;
        [...]
    };
    dictionary RTCDataChannelInit {
        boolean         ordered = true;
        unsigned short  maxPacketLifeTime;
        unsigned short  maxRetransmits;
        USVString       protocol = "";
        boolean         negotiated = false;
        [EnforceRange]
        unsigned short  id;
        RTCPriorityType priority = "low";
    };
  4.9.1.  RTCPriorityType Enum
    enum RTCPriorityType {
      "very-low",
      "low",
      "medium",
      "high"
    };
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_equals(pc.createDataChannel.length, 1);
  assert_throws(new TypeError(), () => pc.createDataChannel());
}, 'createDataChannel with no argument should throw TypeError');
/*
  6.2.  createDataChannel
    2.  If connection's [[isClosed]] slot is true, throw an InvalidStateError.
 */
test((t) => {
  const pc = new RTCPeerConnection();
  pc.close();
  assert_equals(pc.signalingState, 'closed', 'signaling state');
  assert_throws('InvalidStateError', () => pc.createDataChannel(''));
}, 'createDataChannel with closed connection should throw InvalidStateError');
/*
  6.1.  createDataChannel
    4.  Let channel have a [[DataChannelLabel]] internal slot initialized to the value of the
        first argument.
    6.  Let options be the second argument.
    7.  Let channel have an [[MaxPacketLifeTime]] internal slot initialized to
        option's maxPacketLifeTime member, if present, otherwise null.
    8.  Let channel have a [[ReadyState]] internal slot initialized to "connecting".
    9.  Let channel have a [[BufferedAmount]] internal slot initialized to 0.
    10. Let channel have an [[MaxRetransmits]] internal slot initialized to
        option's maxRetransmits member, if present, otherwise null.
    11. Let channel have an [[Ordered]] internal slot initialized to option's
        ordered member.
    12. Let channel have a [[DataChannelProtocol]] internal slot initialized to option's
        protocol member.
    14. Let channel have a [[Negotiated]] internal slot initialized to option's negotiated
        member.
    15. Let channel have an [[DataChannelId]] internal slot initialized to option's id
        member, if it is present and [[Negotiated]] is true, otherwise null.
    17. Let channel have an [[DataChannelPriority]] internal slot initialized to option's
        priority member.
    21. If the [[DataChannelId]] slot is null (due to no ID being passed into
        createDataChannel, or [[Negotiated]] being false), and the DTLS role of the SCTP
        transport has already been negotiated, then initialize [[DataChannelId]] to a value
        generated by the user agent, according to [RTCWEB-DATA-PROTOCOL], and skip
        to the next step. If no available ID could be generated, or if the value of the
        [[DataChannelId]] slot is being used by an existing RTCDataChannel, throw an
        OperationError exception.
        Note
        If the [[DataChannelId]] slot is null after this step, it will be populated once
        the DTLS role is determined during the process of setting an RTCSessionDescription.
    22. If channel is the first RTCDataChannel created on connection, update the
        negotiation-needed flag for connection.
  6.2.  RTCDataChannel
    A RTCDataChannel, created with createDataChannel or dispatched via a
    RTCDataChannelEvent, MUST initially be in the connecting state
    bufferedAmountLowThreshold
      [...] The bufferedAmountLowThreshold is initially zero on each new RTCDataChannel,
      but the application may change its value at any time.
    binaryType
      [...] When a RTCDataChannel object is created, the binaryType attribute MUST
      be initialized to the string "blob".
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('');
  assert_true(dc instanceof RTCDataChannel, 'is RTCDataChannel');
  assert_equals(dc.label, '');
  assert_equals(dc.ordered, true);
  assert_equals(dc.maxPacketLifeTime, null);
  assert_equals(dc.maxRetransmits, null);
  assert_equals(dc.protocol, '');
  assert_equals(dc.negotiated, false);
  // Since no offer/answer exchange has occurred yet, the DTLS role is unknown
  // and so the ID should be null.
  assert_equals(dc.id, null);
  assert_equals(dc.priority, 'low');
  assert_equals(dc.readyState, 'connecting');
  assert_equals(dc.bufferedAmount, 0);
  assert_equals(dc.bufferedAmountLowThreshold, 0);
  assert_equals(dc.binaryType, 'blob');
}, 'createDataChannel attribute default values');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('test', {
    ordered: false,
    maxPacketLifeTime: null,
    maxRetransmits: 1,
    protocol: 'custom',
    negotiated: true,
    id: 3,
    priority: 'high'
  });
  assert_true(dc instanceof RTCDataChannel, 'is RTCDataChannel');
  assert_equals(dc.label, 'test');
  assert_equals(dc.ordered, false);
  assert_equals(dc.maxPacketLifeTime, null);
  assert_equals(dc.maxRetransmits, 1);
  assert_equals(dc.protocol, 'custom');
  assert_equals(dc.negotiated, true);
  assert_equals(dc.id, 3);
  assert_equals(dc.priority, 'high');
  assert_equals(dc.readyState, 'connecting');
  assert_equals(dc.bufferedAmount, 0);
  assert_equals(dc.bufferedAmountLowThreshold, 0);
  assert_equals(dc.binaryType, 'blob');
}, 'createDataChannel with provided parameters should initialize attributes to provided values');
/*
  6.2.  createDataChannel
    4.  Let channel have a [[DataChannelLabel]] internal slot initialized to the value of the
        first argument.
  [ECMA262] 7.1.12. ToString(argument)
    undefined -> "undefined"
    null -> "null"
  [WebIDL] 3.10.15. Convert a DOMString to a sequence of Unicode scalar values
 */
const labels = [
  ['"foo"', 'foo', 'foo'],
  ['null', null, 'null'],
  ['undefined', undefined, 'undefined'],
  ['lone surrogate', '\uD800', '\uFFFD'],
];
for (const [description, label, expected] of labels) {
  test((t) => {
    const pc = new RTCPeerConnection;
    t.add_cleanup(() => pc.close());
    const dc = pc.createDataChannel(label);
    assert_equals(dc.label, expected);
  }, `createDataChannel with label ${description} should succeed`);
}
/*
  6.2.  RTCDataChannel
    createDataChannel
      11. Let channel have an [[Ordered]] internal slot initialized to option's
          ordered member.
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', { ordered: false });
  assert_equals(dc.ordered, false);
}, 'createDataChannel with ordered false should succeed');
// true as the default value of a boolean is confusing because null is converted
// to false while undefined is converted to true.
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc1 = pc.createDataChannel('', { ordered: null });
  assert_equals(dc1.ordered, false);
  const dc2 = pc.createDataChannel('', { ordered: undefined });
  assert_equals(dc2.ordered, true);
}, 'createDataChannel with ordered null/undefined should succeed');
/*
  6.2.  RTCDataChannel
    createDataChannel
      7.  Let channel have an [[MaxPacketLifeTime]] internal slot initialized to
          option's maxPacketLifeTime member, if present, otherwise null.
 */
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', { maxPacketLifeTime: 0 });
  assert_equals(dc.maxPacketLifeTime, 0);
}, 'createDataChannel with maxPacketLifeTime 0 should succeed');
/*
  6.2.  RTCDataChannel
    createDataChannel
      10. Let channel have an [[MaxRetransmits]] internal slot initialized to
          option's maxRetransmits member, if present, otherwise null.
 */
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', { maxRetransmits: 0 });
  assert_equals(dc.maxRetransmits, 0);
}, 'createDataChannel with maxRetransmits 0 should succeed');
/*
  6.2.  createDataChannel
    18. If both [[MaxPacketLifeTime]] and [[MaxRetransmits]] attributes are set (not null),
        throw a TypeError.
 */
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  pc.createDataChannel('', {
    maxPacketLifeTime: null,
    maxRetransmits: null
  });
}, 'createDataChannel with both maxPacketLifeTime and maxRetransmits null should succeed');
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () => pc.createDataChannel('', {
    maxPacketLifeTime: 0,
    maxRetransmits: 0
  }));
  assert_throws(new TypeError(), () => pc.createDataChannel('', {
    maxPacketLifeTime: 42,
    maxRetransmits: 42
  }));
}, 'createDataChannel with both maxPacketLifeTime and maxRetransmits should throw TypeError');
/*
  6.2.  RTCDataChannel
    createDataChannel
      12. Let channel have a [[DataChannelProtocol]] internal slot initialized to option's
          protocol member.
 */
const protocols = [
  ['"foo"', 'foo', 'foo'],
  ['null', null, 'null'],
  ['undefined', undefined, ''],
  ['lone surrogate', '\uD800', '\uFFFD'],
];
for (const [description, protocol, expected] of protocols) {
  test((t) => {
    const pc = new RTCPeerConnection;
    t.add_cleanup(() => pc.close());
    const dc = pc.createDataChannel('', { protocol });
    assert_equals(dc.protocol, expected);
  }, `createDataChannel with protocol ${description} should succeed`);
}
/*
  6.2.  RTCDataChannel
    createDataChannel
      20. If [[DataChannelId]] is equal to 65535, which is greater than the maximum allowed
          ID of 65534 but still qualifies as an unsigned short, throw a TypeError.
 */
for (const id of [0, 1, 65534, 65535]) {
  test((t) => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    const dc = pc.createDataChannel('', { id });
    assert_equals(dc.id, null);
  }, `createDataChannel with id ${id} and negotiated not set should succeed, but not set the channel's id`);
}
for (const id of [0, 1, 65534]) {
  test((t) => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    const dc = pc.createDataChannel('', { negotiated: true, id });
    assert_equals(dc.id, id);
  }, `createDataChannel with id ${id} and negotiated true should succeed, and set the channel's id`);
}
for (const id of [-1, 65536]) {
  test((t) => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    assert_throws(new TypeError(), () => pc.createDataChannel('', { id }));
  }, `createDataChannel with id ${id} and negotiated not set should throw TypeError`);
}
for (const id of [-1, 65535, 65536]) {
  test((t) => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    assert_throws(new TypeError(), () => pc.createDataChannel('', { negotiated: true, id }));
  }, `createDataChannel with id ${id} and negotiated true should throw TypeError`);
}
/*
  6.2.  RTCDataChannel
    createDataChannel
      17. Let channel have an [[DataChannelPriority]] internal slot initialized to option's
          priority member.
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', { priority: 'high' });
  assert_equals(dc.priority, 'high');
}, 'createDataChannel with priority "high" should succeed');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(),
    () => pc.createDataChannel('', { priority: 'invalid' }));
}, 'createDataChannel with invalid priority should throw TypeError');
/*
  6.2.  createDataChannel
    5.  If [[DataChannelLabel]] is longer than 65535 bytes, throw a TypeError.
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('l'.repeat(65536)));
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('l'.repeat(65536), {
      negotiated: true,
      id: 42
    }));
}, 'createDataChannel with too long label should throw TypeError');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('\u00b5'.repeat(32768)));
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('\u00b5'.repeat(32768), {
      negotiated: true,
      id: 42
    }));
}, 'createDataChannel with too long label (2 byte unicode) should throw TypeError');
/*
  6.2.  label
        [...] Scripts are allowed to create multiple RTCDataChannel objects with the same label.
        [...]
 */
test(t => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const label = 'test';
  pc.createDataChannel(label);
  pc.createDataChannel(label);
}, 'createDataChannel with same label used twice should not throw');
/*
  6.2.  createDataChannel
    13. If [[DataChannelProtocol]] is longer than 65535 bytes long, throw a TypeError.
 */
test(() => {
  const pc = new RTCPeerConnection;
  const channel = pc.createDataChannel('', { negotiated: true });
  assert_equals(channel.negotiated, true);
}, 'createDataChannel with negotiated true should succeed');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('', {
      protocol: 'p'.repeat(65536)
    }));
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('', {
      protocol: 'p'.repeat(65536),
      negotiated: true,
      id: 42
    }));
}, 'createDataChannel with too long protocol should throw TypeError');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('', {
      protocol: '\u00b6'.repeat(32768)
    }));
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('', {
      protocol: '\u00b6'.repeat(32768),
      negotiated: true,
      id: 42
    }));
}, 'createDataChannel with too long protocol (2 byte unicode) should throw TypeError');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const label = 'l'.repeat(65535);
  const protocol = 'p'.repeat(65535);
  const dc = pc.createDataChannel(label, {
    protocol: protocol
  });
  assert_equals(dc.label, label);
  assert_equals(dc.protocol, protocol);
}, 'createDataChannel with maximum length label and protocol should succeed');
/*
  6.2   createDataChannel
    15. Let channel have an [[DataChannelId]] internal slot initialized to option's id member,
        if it is present and [[Negotiated]] is true, otherwise null.
        NOTE
        This means the id member will be ignored if the data channel is negotiated in-band; this
        is intentional. Data channels negotiated in-band should have IDs selected based on the
        DTLS role, as specified in [RTCWEB-DATA-PROTOCOL].
 */
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', {
    negotiated: false,
  });
  assert_equals(dc.negotiated, false, 'Expect dc.negotiated to be false');
}, 'createDataChannel with negotiated false should succeed');
test((t) => {
  const pc = new RTCPeerConnection;
  t.add_cleanup(() => pc.close());
  const dc = pc.createDataChannel('', {
    negotiated: false,
    id: 42
  });
  assert_equals(dc.negotiated, false, 'Expect dc.negotiated to be false');
  assert_equals(dc.id, null, 'Expect dc.id to be ignored (null)');
}, 'createDataChannel with negotiated false and id 42 should ignore the id');
/*
  6.2.  createDataChannel
    16. If [[Negotiated]] is true and [[DataChannelId]] is null, throw a TypeError.
 */
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('test', {
      negotiated: true
    }));
}, 'createDataChannel with negotiated true and id not defined should throw TypeError');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  assert_throws(new TypeError(), () =>
    pc.createDataChannel('test', {
      negotiated: true,
      id: null
    }));
}, 'createDataChannel with negotiated true and id null should throw TypeError');
/*
  4.4.1.6.  Set the RTCSessionSessionDescription
    2.2.6.  If description is of type "answer" or "pranswer", then run the
            following steps:
      3.    If description negotiates the DTLS role of the SCTP transport, and there is an
            RTCDataChannel with a null id, then generate an ID according to
            [RTCWEB-DATA-PROTOCOL]. [...]
  6.1.  createDataChannel
    21. If the [[DataChannelId]] slot is null (due to no ID being passed into
        createDataChannel, or [[Negotiated]] being false), and the DTLS role of the SCTP
        transport has already been negotiated, then initialize [[DataChannelId]] to a value
        generated by the user agent, according to [RTCWEB-DATA-PROTOCOL], and skip
        to the next step. If no available ID could be generated, or if the value of the
        [[DataChannelId]] slot is being used by an existing RTCDataChannel, throw an
        OperationError exception.
        Note
        If the [[DataChannelId]] slot is null after this step, it will be populated once
        the DTLS role is determined during the process of setting an RTCSessionDescription.
 */
promise_test(async (t) => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const negotiatedDc = pc1.createDataChannel('negotiated-channel', {
    negotiated: true,
    id: 42,
  });
  assert_equals(negotiatedDc.id, 42, 'Expect negotiatedDc.id to be 42');
  const dc1 = pc1.createDataChannel('channel');
  assert_equals(dc1.id, null, 'Expect initial id to be null');
  const offer = await pc1.createOffer();
  await Promise.all([pc1.setLocalDescription(offer), pc2.setRemoteDescription(offer)]);
  const answer = await pc2.createAnswer();
  await pc1.setRemoteDescription(answer);
  assert_not_equals(dc1.id, null,
    'Expect dc1.id to be assigned after remote description has been set');
  assert_greater_than_equal(dc1.id, 0,
    'Expect dc1.id to be set to valid unsigned short');
  assert_less_than(dc1.id, 65535,
    'Expect dc1.id to be set to valid unsigned short');
  const dc2 = pc1.createDataChannel('channel');
  assert_not_equals(dc2.id, null,
    'Expect dc2.id to be assigned after remote description has been set');
  assert_greater_than_equal(dc2.id, 0,
    'Expect dc2.id to be set to valid unsigned short');
  assert_less_than(dc2.id, 65535,
    'Expect dc2.id to be set to valid unsigned short');
  assert_not_equals(dc2, dc1,
    'Expect channels created from same label to be different');
  assert_equals(dc2.label, dc1.label,
    'Expect different channels can have the same label but different id');
  assert_not_equals(dc2.id, dc1.id,
    'Expect different channels can have the same label but different id');
  assert_equals(negotiatedDc.id, 42,
    'Expect negotiatedDc.id to be 42 after remote description has been set');
}, 'Channels created (after setRemoteDescription) should have id assigned');
test((t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());
  const dc1 = pc.createDataChannel('channel-1', {
    negotiated: true,
    id: 42,
  });
  assert_equals(dc1.id, 42,
    'Expect dc1.id to be 42');
  const dc2 = pc.createDataChannel('channel-2', {
    negotiated: true,
    id: 43,
  });
  assert_equals(dc2.id, 43,
    'Expect dc2.id to be 43');
  assert_throws('OperationError', () =>
    pc.createDataChannel('channel-3', {
      negotiated: true,
      id: 42,
    }));
}, 'Reusing a data channel id that is in use should throw OperationError');
// We've seen implementations behaving differently before and after the connection has been
// established.
promise_test(async (t) => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const dc1 = pc1.createDataChannel('channel-1', {
    negotiated: true,
    id: 42,
  });
  assert_equals(dc1.id, 42, 'Expect dc1.id to be 42');
  const dc2 = pc1.createDataChannel('channel-2', {
    negotiated: true,
    id: 43,
  });
  assert_equals(dc2.id, 43, 'Expect dc2.id to be 43');
  const offer = await pc1.createOffer();
  await Promise.all([pc1.setLocalDescription(offer), pc2.setRemoteDescription(offer)]);
  const answer = await pc2.createAnswer();
  await pc1.setRemoteDescription(answer);
  assert_equals(dc1.id, 42, 'Expect dc1.id to be 42');
  assert_equals(dc2.id, 43, 'Expect dc2.id to be 43');
  assert_throws('OperationError', () =>
    pc1.createDataChannel('channel-3', {
      negotiated: true,
      id: 42,
    }));
}, 'Reusing a data channel id that is in use (after setRemoteDescription) should throw ' +
   'OperationError');
promise_test(async (t) => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  const dc1 = pc1.createDataChannel('channel-1');
  const offer = await pc1.createOffer();
  await Promise.all([pc1.setLocalDescription(offer), pc2.setRemoteDescription(offer)]);
  const answer = await pc2.createAnswer();
  await pc1.setRemoteDescription(answer);
  assert_not_equals(dc1.id, null,
    'Expect dc1.id to be assigned after remote description has been set');
  assert_throws('OperationError', () =>
    pc1.createDataChannel('channel-2', {
      negotiated: true,
      id: dc1.id,
    }));
}, 'Reusing a data channel id that is in use (after setRemoteDescription, negotiated via DCEP) ' +
  'should throw OperationError');
// Based on https://bugzilla.mozilla.org/show_bug.cgi?id=1441723
promise_test(async (t) => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());
  await createDataChannelPair(pc1, pc2);
  const dc = pc1.createDataChannel('');
  assert_equals(dc.readyState, 'connecting', 'Channel should be in the connecting state');
}, 'New data channel should be in the connecting state after creation (after connection ' +
  'establishment)');
/*
  Untestable
    6.1.  createDataChannel
      19. If a setting, either [[MaxPacketLifeTime]] or [[MaxRetransmits]], has been set to
          indicate unreliable mode, and that value exceeds the maximum value supported
          by the user agent, the value MUST be set to the user agents maximum value.
      23. Return channel and continue the following steps in parallel.
      24. Create channel's associated underlying data transport and configure
          it according to the relevant properties of channel.
  Tested in RTCPeerConnection-onnegotiationneeded.html
    22. If channel is the first RTCDataChannel created on connection, update the
        negotiation-needed flag for connection.
  Tested in RTCDataChannel-id.html
    - Odd/even rules for '.id'
  Tested in RTCDataChannel-dcep.html
    - Transmission of '.label' and further options
*/
</script>