tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_dataChannel_id.html (2686B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <script type="application/javascript" src="pc.js"></script>
      5  <script type="application/javascript" src="iceTestUtils.js"></script>
      6 </head>
      7 <body>
      8 <pre id="test">
      9 <script type="application/javascript">
     10  createHTML({
     11    bug: "",
     12    title: ""
     13  });
     14 
     15  const tests = [
     16    async function checkIdReuseAfterClose() {
     17      // Takes the DTLS server role
     18      const pc1 = new RTCPeerConnection();
     19      // Takes the DTLS client role
     20      const pc2 = new RTCPeerConnection();
     21 
     22      info("Creating initial channels");
     23      const dc1pc1 = pc1.createDataChannel('');
     24      const dc1open = new Promise(r => dc1pc1.onopen = r);
     25      const dc2created = new Promise(r => pc2.ondatachannel = r);
     26 
     27      await connect(pc1, pc2, 2000, "Initial connection");
     28      const dc1pc2 = (await dc2created).channel;
     29      await dc1open;
     30 
     31      info("Checking initial ids");
     32      // By default, offerer (pc1) is in DTLS server role
     33      is(dc1pc1.id % 2, 1,
     34        `Channel created by the DTLS server role must be odd (was ${dc1pc1.id})`);
     35      is(dc1pc2.id, dc1pc1.id, 'Both pcs should have the same id for the first channel');
     36      const dc2pc2 = pc2.createDataChannel('another');
     37      const dc2pc1 = (await new Promise(r => pc1.ondatachannel = r)).channel;
     38 
     39      is(dc2pc2.id % 2, 0,
     40        `Channel created by the DTLS client role must be even (was ${dc2pc2.id})`);
     41      is(dc2pc1.id, dc2pc2.id, 'Both pcs should have the same id for the second channel');
     42 
     43      const id1 = dc1pc1.id;
     44      const id2 = dc2pc1.id;
     45 
     46      // Close from pc1
     47      dc1pc1.close();
     48      dc2pc1.close();
     49 
     50      info("Waiting for close events");
     51      await Promise.all([new Promise(r => dc1pc2.onclose = r), new Promise(r => dc2pc2.onclose = r), new Promise(r => dc1pc1.onclose = r), new Promise(r => dc2pc1.onclose = r)]);
     52 
     53      // Unfortunately, there's no way to detect that the stream resets are done.
     54      // This is why this is a mochitest and not a wpt.
     55      await new Promise(r => setTimeout(r, 1000));
     56 
     57      info("Creating new channels");
     58      const dc3pc1 = pc1.createDataChannel('again', {negotiated: true, id: id1});
     59      await new Promise(r => dc3pc1.onopen = r);
     60      is(dc3pc1.id, id1, 'id should be reused');
     61 
     62      const dc4pc2 = pc2.createDataChannel('yet again', {negotiated: true, id: id2});
     63      await new Promise(r => dc4pc2.onopen = r);
     64      is(dc4pc2.id, id2, 'id should be reused');
     65      pc1.close();
     66      pc2.close();
     67 }
     68  ];
     69 
     70  runNetworkTest(async () => {
     71    for (const test of tests) {
     72      info(`Running test: ${test.name}`);
     73      await test();
     74      info(`Done running test: ${test.name}`);
     75    }
     76  });
     77 
     78 </script>
     79 </pre>
     80 </body>
     81 </html>