summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_cert_export.js
blob: 48769f84e6b620bc856c802a29ff0419356e1c30 (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
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var MockFilePicker = SpecialPowers.MockFilePicker;

function createTemporarySaveDirectory() {
  var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  saveDir.append("testsavedir");
  if (!saveDir.exists()) {
    info("create testsavedir!");
    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  }
  info("return from createTempSaveDir: " + saveDir.path);
  return saveDir;
}

// Create the folder the certificates will be saved into.
var destDir = createTemporarySaveDirectory();
registerCleanupFunction(function () {
  destDir.remove(true);
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

function stringOrArrayEquals(actual, expected, message) {
  is(
    typeof actual,
    typeof expected,
    "actual, expected should have the same type"
  );
  if (typeof expected == "string") {
    is(actual, expected, message);
  } else {
    is(actual.toString(), expected.toString(), message);
  }
}

var dialogWin;
var exportButton;
var expectedCert;

async function setupTest() {
  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
  let certButton = gBrowser.selectedBrowser.contentDocument.getElementById(
    "viewCertificatesButton"
  );
  certButton.scrollIntoView();
  let certDialogLoaded = promiseLoadSubDialog(
    "chrome://pippki/content/certManager.xhtml"
  );
  certButton.click();
  dialogWin = await certDialogLoaded;
  let doc = dialogWin.document;
  doc.getElementById("certmanagertabs").selectedTab =
    doc.getElementById("ca_tab");
  let treeView = doc.getElementById("ca-tree").view;
  // Select any which cert. Ignore parent rows (ie rows without certs):
  for (let i = 0; i < treeView.rowCount; i++) {
    treeView.selection.select(i);
    dialogWin.getSelectedCerts();
    let certs = dialogWin.selected_certs; // yuck... but this is how the dialog works.
    if (certs && certs.length == 1 && certs[0]) {
      expectedCert = certs[0];
      // OK, we managed to select a cert!
      break;
    }
  }

  exportButton = doc.getElementById("ca_exportButton");
  is(exportButton.disabled, false, "Should enable export button");
}

async function checkCertExportWorks(
  exportType,
  encoding,
  expectedFileContents
) {
  MockFilePicker.displayDirectory = destDir;
  var destFile = destDir.clone();
  MockFilePicker.init(window);
  MockFilePicker.filterIndex = exportType;
  MockFilePicker.showCallback = function (fp) {
    info("showCallback");
    let fileName = fp.defaultString;
    info("fileName: " + fileName);
    destFile.append(fileName);
    MockFilePicker.setFiles([destFile]);
    info("done showCallback");
  };
  let finishedExporting = TestUtils.topicObserved("cert-export-finished");
  exportButton.click();
  await finishedExporting;
  MockFilePicker.cleanup();
  if (destFile && destFile.exists()) {
    let contents;
    if (encoding === "utf-8") {
      contents = await IOUtils.readUTF8(destFile.path);
    } else {
      is(encoding, "", "expected either utf-8 or empty string for encoding");
      contents = await IOUtils.read(destFile.path);
    }
    stringOrArrayEquals(
      contents,
      expectedFileContents,
      "Should have written correct contents"
    );
    destFile.remove(false);
  } else {
    ok(false, "No cert saved!");
  }
}

add_task(setupTest);

add_task(async function checkCertPEMExportWorks() {
  let expectedContents = dialogWin.getPEMString(expectedCert);
  await checkCertExportWorks(0, /* 0 = PEM */ "utf-8", expectedContents);
});

add_task(async function checkCertPEMChainExportWorks() {
  let expectedContents = dialogWin.getPEMString(expectedCert);
  await checkCertExportWorks(
    1, // 1 = PEM chain, but the chain is of length 1
    "utf-8",
    expectedContents
  );
});

add_task(async function checkCertDERExportWorks() {
  let expectedContents = Uint8Array.from(expectedCert.getRawDER());
  await checkCertExportWorks(2, /* 2 = DER */ "", expectedContents);
});

function stringToTypedArray(str) {
  let arr = new Uint8Array(str.length);
  for (let i = 0; i < arr.length; i++) {
    arr[i] = str.charCodeAt(i);
  }
  return arr;
}

add_task(async function checkCertPKCS7ExportWorks() {
  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  let expectedContents = stringToTypedArray(certdb.asPKCS7Blob([expectedCert]));
  await checkCertExportWorks(3, /* 3 = PKCS7 */ "", expectedContents);
});

add_task(async function checkCertPKCS7ChainExportWorks() {
  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  let expectedContents = stringToTypedArray(certdb.asPKCS7Blob([expectedCert]));
  await checkCertExportWorks(
    4, // 4 = PKCS7 chain, but the chain is of length 1
    "",
    expectedContents
  );
});