summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/dataChannel.js
blob: eac52f96ab58b6d449c1899f95964490b2940aac (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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Returns the contents of a blob as text
 *
 * @param {Blob} blob
          The blob to retrieve the contents from
 */
function getBlobContent(blob) {
  return new Promise(resolve => {
    var reader = new FileReader();
    // Listen for 'onloadend' which will always be called after a success or failure
    reader.onloadend = event => resolve(event.target.result);
    reader.readAsText(blob);
  });
}

var commandsCreateDataChannel = [
  function PC_REMOTE_EXPECT_DATA_CHANNEL(test) {
    test.pcRemote.expectDataChannel();
  },

  function PC_LOCAL_CREATE_DATA_CHANNEL(test) {
    var channel = test.pcLocal.createDataChannel({});
    is(channel.binaryType, "blob", channel + " is of binary type 'blob'");

    is(
      test.pcLocal.signalingState,
      STABLE,
      "Create datachannel does not change signaling state"
    );
    return test.pcLocal.observedNegotiationNeeded;
  },
];

var commandsWaitForDataChannel = [
  function PC_LOCAL_VERIFY_DATA_CHANNEL_STATE(test) {
    return test.pcLocal.dataChannels[0].opened;
  },

  function PC_REMOTE_VERIFY_DATA_CHANNEL_STATE(test) {
    return test.pcRemote.nextDataChannel.then(channel => channel.opened);
  },
];

var commandsCheckDataChannel = [
  function SEND_MESSAGE(test) {
    var message = "Lorem ipsum dolor sit amet";

    info("Sending message:" + message);
    return test.send(message).then(result => {
      is(
        result.data,
        message,
        "Message correctly transmitted from pcLocal to pcRemote."
      );
    });
  },

  function SEND_BLOB(test) {
    var contents = "At vero eos et accusam et justo duo dolores et ea rebum.";
    var blob = new Blob([contents], { type: "text/plain" });

    info("Sending blob");
    return test
      .send(blob)
      .then(result => {
        ok(result.data instanceof Blob, "Received data is of instance Blob");
        is(result.data.size, blob.size, "Received data has the correct size.");

        return getBlobContent(result.data);
      })
      .then(recv_contents =>
        is(recv_contents, contents, "Received data has the correct content.")
      );
  },

  function CREATE_SECOND_DATA_CHANNEL(test) {
    return test.createDataChannel({}).then(result => {
      is(
        result.remote.binaryType,
        "blob",
        "remote data channel is of binary type 'blob'"
      );
    });
  },

  function SEND_MESSAGE_THROUGH_LAST_OPENED_CHANNEL(test) {
    var channels = test.pcRemote.dataChannels;
    var message = "I am the Omega";

    info("Sending message:" + message);
    return test.send(message).then(result => {
      is(
        channels.indexOf(result.channel),
        channels.length - 1,
        "Last channel used"
      );
      is(result.data, message, "Received message has the correct content.");
    });
  },

  function SEND_MESSAGE_THROUGH_FIRST_CHANNEL(test) {
    var message = "Message through 1st channel";
    var options = {
      sourceChannel: test.pcLocal.dataChannels[0],
      targetChannel: test.pcRemote.dataChannels[0],
    };

    info("Sending message:" + message);
    return test.send(message, options).then(result => {
      is(
        test.pcRemote.dataChannels.indexOf(result.channel),
        0,
        "1st channel used"
      );
      is(result.data, message, "Received message has the correct content.");
    });
  },

  function SEND_MESSAGE_BACK_THROUGH_FIRST_CHANNEL(test) {
    var message = "Return a message also through 1st channel";
    var options = {
      sourceChannel: test.pcRemote.dataChannels[0],
      targetChannel: test.pcLocal.dataChannels[0],
    };

    info("Sending message:" + message);
    return test.send(message, options).then(result => {
      is(
        test.pcLocal.dataChannels.indexOf(result.channel),
        0,
        "1st channel used"
      );
      is(result.data, message, "Return message has the correct content.");
    });
  },

  function CREATE_NEGOTIATED_DATA_CHANNEL_MAX_RETRANSMITS(test) {
    var options = {
      negotiated: true,
      id: 5,
      protocol: "foo/bar",
      ordered: false,
      maxRetransmits: 500,
    };
    return test.createDataChannel(options).then(result => {
      is(
        result.local.binaryType,
        "blob",
        result.remote + " is of binary type 'blob'"
      );
      is(
        result.local.id,
        options.id,
        result.local + " id is:" + result.local.id
      );
      is(
        result.local.protocol,
        options.protocol,
        result.local + " protocol is:" + result.local.protocol
      );
      is(
        result.local.reliable,
        false,
        result.local + " reliable is:" + result.local.reliable
      );
      is(
        result.local.ordered,
        options.ordered,
        result.local + " ordered is:" + result.local.ordered
      );
      is(
        result.local.maxRetransmits,
        options.maxRetransmits,
        result.local + " maxRetransmits is:" + result.local.maxRetransmits
      );
      is(
        result.local.maxPacketLifeTime,
        null,
        result.local + " maxPacketLifeTime is:" + result.local.maxPacketLifeTime
      );

      is(
        result.remote.binaryType,
        "blob",
        result.remote + " is of binary type 'blob'"
      );
      is(
        result.remote.id,
        options.id,
        result.remote + " id is:" + result.remote.id
      );
      is(
        result.remote.protocol,
        options.protocol,
        result.remote + " protocol is:" + result.remote.protocol
      );
      is(
        result.remote.reliable,
        false,
        result.remote + " reliable is:" + result.remote.reliable
      );
      is(
        result.remote.ordered,
        options.ordered,
        result.remote + " ordered is:" + result.remote.ordered
      );
      is(
        result.remote.maxRetransmits,
        options.maxRetransmits,
        result.remote + " maxRetransmits is:" + result.remote.maxRetransmits
      );
      is(
        result.remote.maxPacketLifeTime,
        null,
        result.remote +
          " maxPacketLifeTime is:" +
          result.remote.maxPacketLifeTime
      );
    });
  },

  function SEND_MESSAGE_THROUGH_LAST_OPENED_CHANNEL2(test) {
    var channels = test.pcRemote.dataChannels;
    var message = "I am the walrus; Goo goo g'joob";

    info("Sending message:" + message);
    return test.send(message).then(result => {
      is(
        channels.indexOf(result.channel),
        channels.length - 1,
        "Last channel used"
      );
      is(result.data, message, "Received message has the correct content.");
    });
  },

  function CREATE_NEGOTIATED_DATA_CHANNEL_MAX_PACKET_LIFE_TIME(test) {
    var options = {
      ordered: false,
      maxPacketLifeTime: 10,
    };
    return test.createDataChannel(options).then(result => {
      is(
        result.local.binaryType,
        "blob",
        result.local + " is of binary type 'blob'"
      );
      is(
        result.local.protocol,
        "",
        result.local + " protocol is:" + result.local.protocol
      );
      is(
        result.local.reliable,
        false,
        result.local + " reliable is:" + result.local.reliable
      );
      is(
        result.local.ordered,
        options.ordered,
        result.local + " ordered is:" + result.local.ordered
      );
      is(
        result.local.maxRetransmits,
        null,
        result.local + " maxRetransmits is:" + result.local.maxRetransmits
      );
      is(
        result.local.maxPacketLifeTime,
        options.maxPacketLifeTime,
        result.local + " maxPacketLifeTime is:" + result.local.maxPacketLifeTime
      );

      is(
        result.remote.binaryType,
        "blob",
        result.remote + " is of binary type 'blob'"
      );
      is(
        result.remote.protocol,
        "",
        result.remote + " protocol is:" + result.remote.protocol
      );
      is(
        result.remote.reliable,
        false,
        result.remote + " reliable is:" + result.remote.reliable
      );
      is(
        result.remote.ordered,
        options.ordered,
        result.remote + " ordered is:" + result.remote.ordered
      );
      is(
        result.remote.maxRetransmits,
        null,
        result.remote + " maxRetransmits is:" + result.remote.maxRetransmits
      );
      is(
        result.remote.maxPacketLifeTime,
        options.maxPacketLifeTime,
        result.remote +
          " maxPacketLifeTime is:" +
          result.remote.maxPacketLifeTime
      );
    });
  },

  function SEND_MESSAGE_THROUGH_LAST_OPENED_CHANNEL3(test) {
    var channels = test.pcRemote.dataChannels;
    var message = "Nice to see you working maxPacketLifeTime";

    info("Sending message:" + message);
    return test.send(message).then(result => {
      is(
        channels.indexOf(result.channel),
        channels.length - 1,
        "Last channel used"
      );
      is(result.data, message, "Received message has the correct content.");
    });
  },
];

var commandsCheckLargeXfer = [
  function SEND_BIG_BUFFER(test) {
    var size = 2 * 1024 * 1024; // SCTP internal buffer is now 1MB, so use 2MB to ensure the buffer gets full
    var buffer = new ArrayBuffer(size);
    // note: type received is always blob for binary data
    var options = {};
    options.bufferedAmountLowThreshold = 64 * 1024;
    info("Sending arraybuffer");
    return test.send(buffer, options).then(result => {
      ok(result.data instanceof Blob, "Received data is of instance Blob");
      is(result.data.size, size, "Received data has the correct size.");
    });
  },
];

function addInitialDataChannel(chain) {
  chain.insertBefore("PC_LOCAL_CREATE_OFFER", commandsCreateDataChannel);
  chain.insertBefore(
    "PC_LOCAL_WAIT_FOR_MEDIA_FLOW",
    commandsWaitForDataChannel
  );
  chain.removeAfter("PC_REMOTE_CHECK_ICE_CONNECTIONS");
  chain.append(commandsCheckDataChannel);
}