tor-browser

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

RTCDtlsTransport-getRemoteCertificates.html (3302B)


      1 <!doctype html>
      2 <meta charset="utf-8">
      3 <title>RTCDtlsTransport.prototype.getRemoteCertificates</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="RTCPeerConnection-helper.js"></script>
      7 <script>
      8  'use strict';
      9 
     10  // The following helper functions are called from RTCPeerConnection-helper.js:
     11  //   exchangeIceCandidates
     12  //   exchangeOfferAnswer
     13 
     14  /*
     15    5.5.  RTCDtlsTransport Interface
     16      interface RTCDtlsTransport : EventTarget {
     17        readonly attribute RTCDtlsTransportState state;
     18        sequence<ArrayBuffer> getRemoteCertificates();
     19                 attribute EventHandler          onstatechange;
     20                 attribute EventHandler          onerror;
     21        ...
     22      };
     23 
     24      enum RTCDtlsTransportState {
     25        "new",
     26        "connecting",
     27        "connected",
     28        "closed",
     29        "failed"
     30      };
     31 
     32      getRemoteCertificates
     33        Returns the certificate chain in use by the remote side, with each certificate
     34        encoded in binary Distinguished Encoding Rules (DER) [X690].
     35        getRemoteCertificates() will return an empty list prior to selection of the
     36        remote certificate, which will be completed by the time RTCDtlsTransportState
     37        transitions to "connected".
     38   */
     39  async_test(t => {
     40    const pc1 = new RTCPeerConnection();
     41    t.add_cleanup(() => pc1.close());
     42    const pc2 = new RTCPeerConnection();
     43    t.add_cleanup(() => pc2.close());
     44 
     45    pc1.addTrack(trackFactories.audio());
     46    exchangeIceCandidates(pc1, pc2);
     47 
     48    exchangeOfferAnswer(pc1, pc2)
     49    .then(t.step_func(() => {
     50      const dtlsTransport1 = pc1.getSenders()[0].transport;
     51      const dtlsTransport2 = pc2.getReceivers()[0].transport;
     52 
     53      const testedTransports = new Set();
     54 
     55      // Callback function that test the respective DTLS transports
     56      // when they become connected.
     57      const onConnected = t.step_func(dtlsTransport => {
     58        const certs = dtlsTransport.getRemoteCertificates();
     59 
     60        assert_greater_than(certs.length, 0,
     61          'Expect DTLS transport to have at least one remote certificate when connected');
     62 
     63        for(const cert of certs) {
     64          assert_true(cert instanceof ArrayBuffer,
     65            'Expect certificate elements be instance of ArrayBuffer');
     66        }
     67 
     68        testedTransports.add(dtlsTransport);
     69 
     70        // End the test if both dtlsTransports are tested.
     71        if(testedTransports.has(dtlsTransport1) && testedTransports.has(dtlsTransport2)) {
     72          t.done();
     73        }
     74      })
     75 
     76      for(const dtlsTransport of [dtlsTransport1, dtlsTransport2]) {
     77        if(dtlsTransport.state === 'connected') {
     78          onConnected(dtlsTransport);
     79        } else {
     80          assert_array_equals(dtlsTransport.getRemoteCertificates(), [],
     81            'Expect DTLS certificates be initially empty until become connected');
     82 
     83          dtlsTransport.addEventListener('statechange', t.step_func(() => {
     84            if(dtlsTransport.state === 'connected') {
     85              onConnected(dtlsTransport);
     86            }
     87          }));
     88 
     89          dtlsTransport.addEventListener('error', t.step_func(err => {
     90            assert_unreached(`Unexpected error during DTLS handshake: ${err}`);
     91          }));
     92        }
     93      }
     94    }));
     95  });
     96 
     97 </script>