summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/RTCSctpTransport-maxMessageSize.html
blob: 99767611509197a3d1037957551fea397ecad21e (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
<!doctype html>
<meta charset=utf-8>
<title>RTCSctpTransport.prototype.maxMessageSize</title>
<link rel="help" href="https://w3c.github.io/webrtc-pc/#rtcsctptransport-interface">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="RTCPeerConnection-helper.js"></script>
<script>
'use strict';

// This test has an assert_unreached() that requires that the variable
// canSendSize (initiated below) must be 0 or greater than 2. The reason
// is that we need two non-zero values for testing the following two cases:
//
// * if remote MMS `1` < canSendSize it should result in `1`.
// * renegotiation of the above case with remoteMMS `2` should result in `2`.
//
// This is a bit unfortunate but shouldn't have any practical impact.

// Helper class to read SDP attributes and generate SDPs with modified attribute values
class SDPAttributeHelper {
  constructor(attrName, valueRegExpStr) {
    this.attrName = attrName;
    this.re = new RegExp(`^a=${attrName}:(${valueRegExpStr})\\r\\n`, 'm');
  }

  getValue(sdp) {
    const matches = sdp.match(this.re);
    return matches ? matches[1] : null;
  }

  sdpWithValue(sdp, value) {
    const matches = sdp.match(this.re);
    const sdpParts = sdp.split(matches[0]);
    const attributeLine = arguments.length > 1 ? `a=${this.attrName}:${value}\r\n` : '';
    return `${sdpParts[0]}${attributeLine}${sdpParts[1]}`;
  }

  sdpWithoutAttribute(sdp) {
    return this.sdpWithValue(sdp);
  }
}

const mmsAttributeHelper = new SDPAttributeHelper('max-message-size', '\\d+');
let canSendSize = null;
const remoteSize1 = 1;
const remoteSize2 = 2;

promise_test(async (t) => {
  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());

  assert_equals(pc.sctp, null, 'RTCSctpTransport must be null');

  let offer = await generateDataChannelOffer(pc);
  assert_not_equals(mmsAttributeHelper.getValue(offer.sdp), null,
    'SDP should have max-message-size attribute');
  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, 0) };
  await pc.setRemoteDescription(offer);
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  canSendSize = pc.sctp.maxMessageSize === Number.POSITIVE_INFINITY ? 0 : pc.sctp.maxMessageSize;
  if (canSendSize !== 0 && canSendSize < remoteSize2) {
    assert_unreached(
      'This test needs canSendSize to be 0 or > 2 for further "below" and "above" tests');
  }
}, 'Determine the local side send limitation (canSendSize) by offering a max-message-size of 0');

promise_test(async (t) => {
  assert_not_equals(canSendSize, null, 'canSendSize needs to be determined');

  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());

  assert_equals(pc.sctp, null, 'RTCSctpTransport must be null');

  let offer = await generateDataChannelOffer(pc);
  assert_not_equals(mmsAttributeHelper.getValue(offer.sdp), null,
    'SDP should have max-message-size attribute');

  // Remove the max-message-size SDP attribute
  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithoutAttribute(offer.sdp) };
  await pc.setRemoteDescription(offer);
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  // Test outcome depends on canSendSize value
  if (canSendSize !== 0) {
    assert_equals(pc.sctp.maxMessageSize, Math.min(65536, canSendSize),
      'Missing SDP attribute and a non-zero canSendSize should give an maxMessageSize of min(65536, canSendSize)');
  } else {
    assert_equals(pc.sctp.maxMessageSize, 65536,
      'Missing SDP attribute and a canSendSize of 0 should give an maxMessageSize of 65536');
  }
}, 'Remote offer SDP missing max-message-size attribute');

promise_test(async (t) => {
  assert_not_equals(canSendSize, null, 'canSendSize needs to be determined');

  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());

  assert_equals(pc.sctp, null, 'RTCSctpTransport must be null');

  let offer = await generateDataChannelOffer(pc);
  assert_not_equals(mmsAttributeHelper.getValue(offer.sdp), null,
    'SDP should have max-message-size attribute');

  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, remoteSize1) };
  await pc.setRemoteDescription(offer);
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  assert_equals(pc.sctp.maxMessageSize, remoteSize1,
    'maxMessageSize should be the value provided by the remote peer (as long as it is less than canSendSize)');
}, 'max-message-size with a (non-zero) value provided by the remote peer');

promise_test(async (t) => {
  assert_not_equals(canSendSize, null, 'canSendSize needs to be determined');

  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());

  assert_equals(pc.sctp, null, 'RTCSctpTransport must be null');

  let offer = await generateDataChannelOffer(pc);
  assert_not_equals(mmsAttributeHelper.getValue(offer.sdp), null,
    'SDP should have max-message-size attribute');

  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, remoteSize1) };
  await pc.setRemoteDescription(offer);
  let answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  assert_equals(pc.sctp.maxMessageSize, remoteSize1,
    'maxMessageSize should be the value provided by the remote peer (as long as it is less than canSendSize)');

  // Start new O/A exchange that updates max-message-size to remoteSize2
  offer = await pc.createOffer();
  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, remoteSize2)};
  await pc.setRemoteDescription(offer);
  answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  assert_equals(pc.sctp.maxMessageSize, remoteSize2,
    'maxMessageSize should be the new value provided by the remote peer (as long as it is less than canSendSize)');

  // Start new O/A exchange that updates max-message-size to zero
  offer = await pc.createOffer();
  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, 0)};
  await pc.setRemoteDescription(offer);
  answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  assert_equals(pc.sctp.maxMessageSize, canSendSize,
    'maxMessageSize should be canSendSize');

  // Start new O/A exchange that updates max-message-size to remoteSize1 again
  offer = await pc.createOffer();
  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, remoteSize1)};
  await pc.setRemoteDescription(offer);
  answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  assert_equals(pc.sctp.maxMessageSize, remoteSize1,
    'maxMessageSize should be the new value provided by the remote peer (as long as it is less than canSendSize)');
}, 'Renegotiate max-message-size with various values provided by the remote peer');

promise_test(async (t) => {
  assert_not_equals(canSendSize, null, 'canSendSize needs to be determined');

  const pc = new RTCPeerConnection();
  t.add_cleanup(() => pc.close());

  assert_equals(pc.sctp, null, 'RTCSctpTransport must be null');
  const largerThanCanSendSize = canSendSize === 0 ? 0 : canSendSize + 1;

  let offer = await generateDataChannelOffer(pc);
  assert_not_equals(mmsAttributeHelper.getValue(offer.sdp), null,
    'SDP should have max-message-size attribute');

  offer = { type: 'offer', sdp: mmsAttributeHelper.sdpWithValue(offer.sdp, largerThanCanSendSize) };
  await pc.setRemoteDescription(offer);
  const answer = await pc.createAnswer();
  await pc.setLocalDescription(answer);

  assert_not_equals(pc.sctp, null, 'RTCSctpTransport must be available');
  // Test outcome depends on canSendSize value
  if (canSendSize !== 0) {
    assert_equals(pc.sctp.maxMessageSize, canSendSize,
      'A remote value larger than a non-zero canSendSize should limit maxMessageSize to canSendSize');
  } else {
    assert_equals(pc.sctp.maxMessageSize, Number.POSITIVE_INFINITY,
      'A remote value of zero and canSendSize zero should result in "infinity"');
  }
}, 'max-message-size with a (non-zero) value larger than canSendSize provided by the remote peer');

</script>