summaryrefslogtreecommitdiff
path: root/dom/media/tests/mochitest/identity/test_fingerprints.html
blob: 0fd065af26d9a0bf5fd3ec0c90c5fd0bcd8376be (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
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
  <script class="testbody" type="application/javascript">
'use strict';

// here we call the identity provider directly
function getIdentityAssertion(fpArray) {
  var Cu = SpecialPowers.Cu;
  var rtcid = Cu.import('resource://gre/modules/media/IdpSandbox.jsm');
  var sandbox = new rtcid.IdpSandbox('example.com', 'idp.js', window);
  return sandbox.start()
    .then(idp => SpecialPowers.wrap(idp)
                   .generateAssertion(JSON.stringify({ fingerprint: fpArray }),
                                      'https://example.com'))
    .then(assertion => {
      assertion = SpecialPowers.wrap(assertion);
      var assertionString = btoa(JSON.stringify(assertion));
      sandbox.stop();
      return assertionString;
    });
}

// This takes a real fingerprint and makes some extra bad ones.
function makeFingerprints(algo, digest) {
  var fingerprints = [];
  fingerprints.push({ algorithm: algo, digest: digest });
  for (var i = 0; i < 3; ++i) {
    fingerprints.push({
      algorithm: algo,
      digest: digest.replace(/:./g, ':' + i.toString(16))
    });
  }
  return fingerprints;
}

var fingerprintRegex = /^a=fingerprint:(\S+) (\S+)/m;
var identityRegex = /^a=identity:(\S+)/m;

function fingerprintSdp(fingerprints) {
  return fingerprints.map(fp => 'a=fInGeRpRiNt:' + fp.algorithm +
                                ' ' + fp.digest + '\n').join('');
}

// Firefox only uses a single fingerprint.
// That doesn't mean we have it create SDP that describes two.
// This function synthesizes that SDP and tries to set it.
function testMultipleFingerprints() {
  // this one fails setRemoteDescription if the identity is not good
  var pcStrict = new RTCPeerConnection({ peerIdentity: 'someone@example.com'});
  // this one will be manually tweaked to have two fingerprints
  var pcDouble = new RTCPeerConnection({});

  var offer, match, fingerprints;

  var fail = msg =>
      (e => ok(false, 'error in ' + msg + ': ' +
               (e.message ? (e.message + '\n' + e.stack) : e)));

  navigator.mediaDevices.getUserMedia({ audio: true, fake: true })
    .then(stream => {
      ok(stream, 'Got fake stream');
      pcDouble.addStream(stream);
      return pcDouble.createOffer();
    })
    .then(o => {
      offer = o;
      ok(offer, 'Got offer');

      match = offer.sdp.match(fingerprintRegex);
      if (!match) {
        throw new Error('No fingerprint in offer SDP');
      }
      fingerprints = makeFingerprints(match[1], match[2]);
      return getIdentityAssertion(fingerprints);
    })
    .then(assertion => {
      ok(assertion, 'Should have assertion');

      var sdp = offer.sdp.slice(0, match.index) +
          'a=identity:' + assertion + '\n' +
          fingerprintSdp(fingerprints.slice(1)) +
          offer.sdp.slice(match.index);

      var desc = new RTCSessionDescription({ type: 'offer', sdp: sdp });
      return pcStrict.setRemoteDescription(desc);
    })
    .then(() => {
      ok(true, 'Modified fingerprints were accepted');
    }, error => {
      var e = SpecialPowers.wrap(error);
      ok(false, 'error in test: ' +
         (e.message ? (e.message + '\n' + e.stack) : e));
    })
    .then(() => {
      pcStrict.close();
      pcDouble.close();
      SimpleTest.finish();
    });
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({
  set: [ [ 'media.peerconnection.identity.enabled', true ] ]
}, testMultipleFingerprints);
</script>
  </body>
</html>