summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/openpgp/browser_viewMessageSecurity.js
blob: 19d4a6865e168af0b30f1996200cca0b2aa6009a (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests for the display of the Message Security popup panel, which displays
 * encryption information for both OpenPGP and S/MIME.
 */

"use strict";

const {
  create_encrypted_smime_message,
  add_message_to_folder,
  be_in_folder,
  get_about_message,
  get_special_folder,
  mc,
  select_click_row,
  press_delete,
  plan_for_message_display,
  wait_for_message_display_completion,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
const {
  get_notification_button,
  wait_for_notification_to_show,
  wait_for_notification_to_stop,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/NotificationBoxHelpers.jsm"
);
const { OpenPGPTestUtils } = ChromeUtils.import(
  "resource://testing-common/mozmill/OpenPGPTestUtils.jsm"
);
const { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);
const { SmimeUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/smimeUtils.jsm"
);

const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

const MSG_TEXT = "Sundays are nothing without callaloo.";

function getMsgBodyTxt(mc) {
  let msgPane = get_about_message(mc.window).getMessagePaneBrowser();
  return msgPane.contentDocument.documentElement.textContent;
}

var aliceAcct;
var aliceIdentity;
var initialKeyIdPref = "";
var gInbox;

/**
 * Set up the base account, identity and keys needed for the tests.
 */
add_setup(async function () {
  SmimeUtils.ensureNSS();
  SmimeUtils.loadCertificateAndKey(
    new FileUtils.File(getTestFilePath("data/smime/Bob.p12")),
    "nss"
  );

  aliceAcct = MailServices.accounts.createAccount();
  aliceAcct.incomingServer = MailServices.accounts.createIncomingServer(
    "alice",
    "openpgp.example",
    "pop3"
  );
  aliceIdentity = MailServices.accounts.createIdentity();
  aliceIdentity.email = "alice@openpgp.example";
  aliceAcct.addIdentity(aliceIdentity);

  // Set up the alice's private key.
  let [id] = await OpenPGPTestUtils.importPrivateKey(
    window,
    new FileUtils.File(
      getTestFilePath(
        "data/keys/alice@openpgp.example-0xf231550c4f47e38e-secret.asc"
      )
    )
  );

  initialKeyIdPref = aliceIdentity.getUnicharAttribute("openpgp_key_id");
  aliceIdentity.setUnicharAttribute("openpgp_key_id", id);

  // Import and accept the public key for Bob, our verified sender.
  await OpenPGPTestUtils.importPublicKey(
    window,
    new FileUtils.File(
      getTestFilePath(
        "data/keys/bob@openpgp.example-0xfbfcc82a015e7330-pub.asc"
      )
    )
  );

  gInbox = await get_special_folder(Ci.nsMsgFolderFlags.Inbox, true);
  await be_in_folder(gInbox);
});

/**
 * Test that the encryption icons and the message security popup properly update
 * when selecting an S/MIME or OpenPGP message with different signature and
 * encryption states.
 */
add_task(async function testSmimeOpenPgpSelection() {
  let smimeFile = new FileUtils.File(
    getTestFilePath("data/smime/alice.env.eml")
  );
  // Fetch a local OpenPGP message.
  let openPgpFile = new FileUtils.File(
    getTestFilePath(
      "data/eml/signed-by-0xfbfcc82a015e7330-encrypted-to-0xf231550c4f47e38e.eml"
    )
  );

  // Add the fetched S/MIME message to the inbox folder.
  let copyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFileMessage(
    smimeFile,
    gInbox,
    null,
    false,
    0,
    "",
    copyListener,
    null
  );
  await copyListener.promise;

  // Add the fetched OpenPGP message to the inbox folder.
  copyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFileMessage(
    openPgpFile,
    gInbox,
    null,
    false,
    0,
    "",
    copyListener,
    null
  );
  await copyListener.promise;

  // Select the second row, which should contain the S/MIME message.
  select_click_row(1);

  let aboutMessage = get_about_message();
  Assert.equal(
    aboutMessage.document
      .getElementById("encryptionTechBtn")
      .querySelector("span").textContent,
    "S/MIME"
  );
  Assert.ok(
    OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "S/MIME message should be decrypted"
  );

  let openpgpprocessed = BrowserTestUtils.waitForEvent(
    aboutMessage.document,
    "openpgpprocessed"
  );
  // Select the first row, which should contain the OpenPGP message.
  select_click_row(0);
  await openpgpprocessed;

  Assert.equal(
    aboutMessage.document
      .getElementById("encryptionTechBtn")
      .querySelector("span").textContent,
    "OpenPGP"
  );

  Assert.ok(getMsgBodyTxt(mc).includes(MSG_TEXT), "message text is in body");
  Assert.ok(
    OpenPGPTestUtils.hasSignedIconState(aboutMessage.document, "verified"),
    "signed verified icon is displayed"
  );
  Assert.ok(
    OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is displayed"
  );

  // Delete the two generated messages.
  press_delete();
  select_click_row(0);
  press_delete();
});

/**
 * Test the notification and repairing of a message corrupted by MS-Exchange.
 */
add_task(async function testBrokenMSExchangeEncryption() {
  // Fetch a broken MS-Exchange encrypted message.
  let brokenFile = new FileUtils.File(
    getTestFilePath("data/eml/alice-broken-exchange.eml")
  );
  let notificationBox = "mail-notification-top";
  let notificationValue = "brokenExchange";

  // Add the broken OpenPGP message to the inbox folder.
  let copyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFileMessage(
    brokenFile,
    gInbox,
    null,
    false,
    0,
    "",
    copyListener,
    null
  );
  await copyListener.promise;

  // Select the first row, which should contain the OpenPGP message.
  select_click_row(0);

  // Assert the "corrupted by MS-Exchange" notification is visible.
  let aboutMessage = get_about_message();
  wait_for_notification_to_show(
    aboutMessage,
    notificationBox,
    notificationValue
  );

  // Click on the "repair" button.
  let repairButton = get_notification_button(
    aboutMessage,
    notificationBox,
    notificationValue,
    {
      popup: null,
    }
  );
  plan_for_message_display(mc);
  EventUtils.synthesizeMouseAtCenter(repairButton, {}, aboutMessage);

  // Wait for the "fixing in progress" notification to go away.
  wait_for_notification_to_stop(
    aboutMessage,
    notificationBox,
    "brokenExchangeProgress"
  );

  // The broken exchange repair process generates a new fixed message body and
  // then copies the new message in the same folder. Therefore, we need to wait
  // for the message to be automatically reloaded and reselected.
  wait_for_message_display_completion(mc, true);

  // Assert that the message was repaired and decrypted.
  await TestUtils.waitForCondition(
    () => OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is displayed"
  );

  // Delete the message.
  press_delete();
}).skip(); // TODO

/**
 * Test the working keyboard shortcut event listener for the message header.
 * Ctrl+Alt+S for Windows and Linux, Control+Cmd+S for macOS.
 */
add_task(async function testMessageSecurityShortcut() {
  // Create an S/MIME message and add it to the inbox folder.
  await add_message_to_folder([gInbox], create_encrypted_smime_message());

  // Select the first row, which should contain the S/MIME message.
  select_click_row(0);

  let aboutMessage = get_about_message();
  Assert.equal(
    aboutMessage.document
      .getElementById("encryptionTechBtn")
      .querySelector("span").textContent,
    "S/MIME"
  );

  let modifiers =
    AppConstants.platform == "macosx"
      ? { accelKey: true, ctrlKey: true }
      : { accelKey: true, altKey: true };

  let popupshown = BrowserTestUtils.waitForEvent(
    aboutMessage.document.getElementById("messageSecurityPanel"),
    "popupshown"
  );

  EventUtils.synthesizeKey("s", modifiers, aboutMessage);

  // The Message Security popup panel should show up.
  await popupshown;

  // Select the row again since the focus moved to the popup panel.
  select_click_row(0);
  // Delete the message.
  press_delete();
}).skip(); // TODO

registerCleanupFunction(async function tearDown() {
  // Reset the OpenPGP key and delete the account.
  MailServices.accounts.removeAccount(aliceAcct, true);
  aliceAcct = null;

  await OpenPGPTestUtils.removeKeyById("0xf231550c4f47e38e", true);
});