summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_local_cert.js
blob: 937c0432b521d204ae35c7e95496894005eedd2a (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const certService = Cc["@mozilla.org/security/local-cert-service;1"]
                    .getService(Ci.nsILocalCertService);

const gNickname = "local-cert-test";

function run_test() {
  // Need profile dir to store the key / cert
  do_get_profile();
  // Ensure PSM is initialized
  Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
  run_next_test();
}

function getOrCreateCert(nickname) {
  return new Promise((resolve, reject) => {
    certService.getOrCreateCert(nickname, {
      handleCert: function(c, rv) {
        if (rv) {
          reject(rv);
          return;
        }
        resolve(c);
      }
    });
  });
}

function removeCert(nickname) {
  return new Promise((resolve, reject) => {
    certService.removeCert(nickname, {
      handleResult: function(rv) {
        if (rv) {
          reject(rv);
          return;
        }
        resolve();
      }
    });
  });
}

add_task(function* () {
  // No master password, so no prompt required here
  ok(!certService.loginPromptRequired);

  let certA = yield getOrCreateCert(gNickname);
  equal(certA.nickname, gNickname);

  // Getting again should give the same cert
  let certB = yield getOrCreateCert(gNickname);
  equal(certB.nickname, gNickname);

  // Should be matching instances
  ok(certA.equals(certB));

  // Check a few expected attributes
  ok(certA.isSelfSigned);
  equal(certA.certType, Ci.nsIX509Cert.USER_CERT);

  // New nickname should give a different cert
  let diffNameCert = yield getOrCreateCert("cool-stuff");
  ok(!diffNameCert.equals(certA));

  // Remove the cert, and get a new one again
  yield removeCert(gNickname);
  let newCert = yield getOrCreateCert(gNickname);
  ok(!newCert.equals(certA));

  // Drop all cert references and GC
  let serial = newCert.serialNumber;
  certA = certB = diffNameCert = newCert = null;
  Cu.forceGC();
  Cu.forceCC();

  // Should still get the same cert back
  let certAfterGC = yield getOrCreateCert(gNickname);
  equal(certAfterGC.serialNumber, serial);
});