DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d8847129d134)

VCS Links

Line Code
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test UDPSocket API</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<iframe id="iframe"></iframe>
<pre id="test">
<script type="application/javascript;version=1.8">
'use strict';
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

const HELLO_WORLD = 'hlo wrld. ';
const DATA_ARRAY = [0, 255, 254, 0, 1, 2, 3, 0, 255, 255, 254, 0];
const DATA_ARRAY_BUFFER = new ArrayBuffer(DATA_ARRAY.length);
const TYPED_DATA_ARRAY = new Uint8Array(DATA_ARRAY_BUFFER);
const BIG_ARRAY = new Array(4096);
const BIG_ARRAY_BUFFER = new ArrayBuffer(BIG_ARRAY.length);
const BIG_TYPED_ARRAY = new Uint8Array(BIG_ARRAY_BUFFER);

for (let i = 0; i < BIG_ARRAY.length; i++) {
  BIG_ARRAY[i] = Math.floor(Math.random() * 256);
}

TYPED_DATA_ARRAY.set(DATA_ARRAY);
BIG_TYPED_ARRAY.set(BIG_ARRAY);

function is_same_buffer(recv_data, expect_data) {
  let recv_dataview = new Uint8Array(recv_data);
  let expected_dataview = new Uint8Array(expect_data);

  if (recv_dataview.length !== expected_dataview.length) {
    return false;
  }

  for (let i = 0; i < recv_dataview.length; i++) {
    if (recv_dataview[i] != expected_dataview[i]) {
      info('discover byte differenct at ' + i);
      return false;
    }
  }
  return true;
}

function testOpen() {
  info('test for creating an UDP Socket');
  let socket = new UDPSocket();
  is(socket.localPort, null, 'expect no local port before socket opened');
  is(socket.localAddress, null, 'expect no local address before socket opened');
  is(socket.remotePort, null, 'expected no default remote port');
  is(socket.remoteAddress, null, 'expected no default remote address');
  is(socket.readyState, 'opening', 'expected ready state = opening');
  is(socket.loopback, false, 'expected no loopback');
  is(socket.addressReuse, true, 'expect to reuse address');

  return socket.opened.then(function() {
    ok(true, 'expect openedPromise to be resolved after successful socket binding');
    ok(!(socket.localPort === 0), 'expect allocated a local port');
    is(socket.localAddress, '0.0.0.0', 'expect assigned to default address');
    is(socket.readyState, 'open', 'expected ready state = open');

    return socket;
  });
}

function testSendString(socket) {
  info('test for sending string data');

  socket.send(HELLO_WORLD, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    socket.addEventListener('message', function recv_callback(msg) {
      socket.removeEventListener('message', recv_callback);
      let recvData= String.fromCharCode.apply(null, new Uint8Array(msg.data));
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      is(recvData, HELLO_WORLD, 'expected same string data');
      resolve(socket);
    });
  });
}

function testSendArrayBuffer(socket) {
  info('test for sending ArrayBuffer');

  socket.send(DATA_ARRAY_BUFFER, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    socket.addEventListener('message', function recv_callback(msg) {
      socket.removeEventListener('message', recv_callback);
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      ok(is_same_buffer(msg.data, DATA_ARRAY_BUFFER), 'expected same buffer data');
      resolve(socket);
    });
  });
}

function testSendArrayBufferView(socket) {
  info('test for sending ArrayBufferView');

  socket.send(TYPED_DATA_ARRAY, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    socket.addEventListener('message', function recv_callback(msg) {
      socket.removeEventListener('message', recv_callback);
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      ok(is_same_buffer(msg.data, TYPED_DATA_ARRAY), 'expected same buffer data');
      resolve(socket);
    });
  });
}

function testSendBlob(socket) {
  info('test for sending Blob');

  let blob = new Blob([HELLO_WORLD], {type : 'text/plain'});
  socket.send(blob, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    socket.addEventListener('message', function recv_callback(msg) {
      socket.removeEventListener('message', recv_callback);
      let recvData= String.fromCharCode.apply(null, new Uint8Array(msg.data));
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      is(recvData, HELLO_WORLD, 'expected same string data');
      resolve(socket);
    });
  });
}

function testSendBigArray(socket) {
  info('test for sending Big ArrayBuffer');

  socket.send(BIG_TYPED_ARRAY, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    let byteReceived = 0;
    socket.addEventListener('message', function recv_callback(msg) {
      let byteBegin = byteReceived;
      byteReceived += msg.data.byteLength;
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      ok(is_same_buffer(msg.data, BIG_TYPED_ARRAY.subarray(byteBegin, byteReceived)), 'expected same buffer data [' + byteBegin+ '-' + byteReceived + ']');
      if (byteReceived >= BIG_TYPED_ARRAY.length) {
        socket.removeEventListener('message', recv_callback);
        resolve(socket);
      }
    });
  });
}

function testSendBigBlob(socket) {
  info('test for sending Big Blob');

  let blob = new Blob([BIG_TYPED_ARRAY]);
  socket.send(blob, '127.0.0.1', socket.localPort);

  return new Promise(function(resolve, reject) {
    let byteReceived = 0;
    socket.addEventListener('message', function recv_callback(msg) {
      let byteBegin = byteReceived;
      byteReceived += msg.data.byteLength;
      is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
      ok(is_same_buffer(msg.data, BIG_TYPED_ARRAY.subarray(byteBegin, byteReceived)), 'expected same buffer data [' + byteBegin+ '-' + byteReceived + ']');
      if (byteReceived >= BIG_TYPED_ARRAY.length) {
        socket.removeEventListener('message', recv_callback);
        resolve(socket);
      }
    });
  });
}

function testUDPOptions(socket) {
  info('test for UDP init options');

  let remoteSocket = new UDPSocket({addressReuse: false,
                                    loopback: true,
                                    localAddress: '127.0.0.1',
                                    remoteAddress: '127.0.0.1',
                                    remotePort: socket.localPort});
  is(remoteSocket.localAddress, '127.0.0.1', 'expected local address');
  is(remoteSocket.remoteAddress, '127.0.0.1', 'expected remote address');
  is(remoteSocket.remotePort, socket.localPort, 'expected remote port');
  is(remoteSocket.addressReuse, false, 'expected address not reusable');
  is(remoteSocket.loopback, true, 'expected loopback mode is on');

  return remoteSocket.opened.then(function() {
    remoteSocket.send(HELLO_WORLD);
    return new Promise(function(resolve, reject) {
      socket.addEventListener('message', function recv_callback(msg) {
        socket.removeEventListener('message', recv_callback);
        let recvData= String.fromCharCode.apply(null, new Uint8Array(msg.data));
        is(msg.remotePort, remoteSocket.localPort, 'expected packet from ' + remoteSocket.localPort);
        is(recvData, HELLO_WORLD, 'expected same string data');
        resolve(socket);
      });
    });
  });
}

function testClose(socket) {
  info('test for close');

  socket.close();
  is(socket.readyState, 'closed', 'expect ready state to be "closed"');
  try {
    socket.send(HELLO_WORLD, '127.0.0.1', socket.localPort);
    ok(false, 'unexpect to send successfully');
  } catch (e) {
    ok(true, 'expected send fail after socket closed');
  }

  return socket.closed.then(function() {
    ok(true, 'expected closedPromise is resolved after socket.close()');
  });
}

function testMulticast() {
  info('test for multicast');

  let socket = new UDPSocket({loopback: true});

  const MCAST_ADDRESS = '224.0.0.255';
  socket.joinMulticastGroup(MCAST_ADDRESS);

  return socket.opened.then(function() {
    socket.send(HELLO_WORLD, MCAST_ADDRESS, socket.localPort);

    return new Promise(function(resolve, reject) {
      socket.addEventListener('message', function recv_callback(msg) {
        socket.removeEventListener('message', recv_callback);
        let recvData= String.fromCharCode.apply(null, new Uint8Array(msg.data));
        is(msg.remotePort, socket.localPort, 'expected packet from ' + socket.localPort);
        is(recvData, HELLO_WORLD, 'expected same string data');
        socket.leaveMulticastGroup(MCAST_ADDRESS);
        resolve();
      });
    });
  });
}

function testInvalidUDPOptions() {
  info('test for invalid UDPOptions');
  try {
    let socket = new UDPSocket({localAddress: 'not-a-valid-address'});
    ok(false, 'should not create an UDPSocket with an invalid localAddress');
  } catch (e) {
    is(e.name, 'InvalidAccessError', 'expected InvalidAccessError will be thrown if localAddress is not a valid IPv4/6 address');
  }

  try {
    let socket = new UDPSocket({localPort: 0});
    ok(false, 'should not create an UDPSocket with an invalid localPort');
  } catch (e) {
    is(e.name, 'InvalidAccessError', 'expected InvalidAccessError will be thrown if localPort is not a valid port number');
  }

  try {
    let socket = new UDPSocket({remotePort: 0});
    ok(false, 'should not create an UDPSocket with an invalid remotePort');
  } catch (e) {
    is(e.name, 'InvalidAccessError', 'expected InvalidAccessError will be thrown if localPort is not a valid port number');
  }
}

function testOpenFailed() {
  info('test for falied on open');

  //according to RFC5737, address block 192.0.2.0/24 should not be used in both local and public contexts
  let socket = new UDPSocket({localAddress: '192.0.2.0'});

  return socket.opened.then(function() {
    ok(false, 'should not resolve openedPromise while fail to bind socket');
    socket.close();
  }).catch(function(reason) {
    is(reason.name, 'NetworkError', 'expected openedPromise to be rejected while fail to bind socket');
  });
}

function testSendBeforeOpen() {
  info('test for send before open');

  let socket = new UDPSocket();

  try {
    socket.send(HELLO_WORLD, '127.0.0.1', 9);
    ok(false, 'unexpect to send successfully');
  } catch (e) {
    ok(true, 'expected send fail before openedPromise is resolved');
  }

  return socket.opened.then(function() {
    socket.close();
  });
}

function testCloseBeforeOpened() {
  info('test for close socket before opened');

  let socket = new UDPSocket();
  socket.opened.then(function() {
    ok(false, 'should not resolve openedPromise if it has already been closed');
  }).catch(function(reason) {
    is(reason.name, 'AbortError', 'expected openedPromise to be rejected while socket is closed during opening');
  });

  return socket.close().then(function() {
    ok(true, 'expected closedPromise to be resolved');
  }).then(socket.opened);
}

function testOpenWithoutClose() {
  info('test for open without close');

  let closed = [];
  for (let i = 0; i < 50; i++) {
    let socket = new UDPSocket();
    closed.push(socket.closed);
  }

  SpecialPowers.gc();
  info('all unrefereced socket should be closed right after GC');

  return Promise.all(closed);
}

function testBFCache() {
  info('test for bfcache behavior');

  let socket = new UDPSocket();

  return socket.opened.then(function() {
    let iframe = document.getElementById('iframe');
    SpecialPowers.wrap(iframe).mozbrowser = true;
    iframe.src = 'file_udpsocket_iframe.html?' + socket.localPort;

    return new Promise(function(resolve, reject) {
      socket.addEventListener('message', function recv_callback(msg) {
        socket.removeEventListener('message', recv_callback);
        iframe.src = 'about:blank';
        iframe.addEventListener('load', function onload() {
          iframe.removeEventListener('load', onload);
          socket.send(HELLO_WORLD, '127.0.0.1', msg.remotePort);

          function recv_again_callback(msg) {
            socket.removeEventListener('message', recv_again_callback);
            ok(false, 'should not receive packet after page unload');
          }

          socket.addEventListener('message', recv_again_callback);

          let timeout = setTimeout(function() {
            socket.removeEventListener('message', recv_again_callback);
            socket.close();
            resolve();
          }, 5000);
        });
      });
    });
  });
}

function runTest() {
  testOpen()
  .then(testSendString)
  .then(testSendArrayBuffer)
  .then(testSendArrayBufferView)
  .then(testSendBlob)
  .then(testSendBigArray)
  .then(testSendBigBlob)
  .then(testUDPOptions)
  .then(testClose)
  .then(testMulticast)
  .then(testInvalidUDPOptions)
  .then(testOpenFailed)
  .then(testSendBeforeOpen)
  .then(testCloseBeforeOpened)
  .then(testOpenWithoutClose)
  .then(testBFCache)
  .then(function() {
    info('test finished');
    SimpleTest.finish();
  })
  .catch(function(err) {
    ok(false, 'test failed due to: ' + err);
    SimpleTest.finish();
  });
}

window.addEventListener('load', function () {
  SpecialPowers.pushPermissions([
    {type: 'udp-socket', allow: true, context: document}], function() {
    SpecialPowers.pushPrefEnv({
      'set': [
        ['dom.udpsocket.enabled', true],
        ['browser.sessionhistory.max_total_viewers', 10]
      ]
    }, runTest);
  });
});

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