summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/openpgp/browser_collectKeys.js
blob: d87df03ba3f392200a80b76441256d211c50c32f (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
313
314
315
316
317
318
/* 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 collecting keys from messages.
 */

"use strict";

const { get_about_message, open_message_from_file } = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
const { close_window } = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);
const { waitForCondition } = ChromeUtils.import(
  "resource://testing-common/mozmill/utils.jsm"
);
const {
  assert_notification_displayed,
  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 { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
const { CollectedKeysDB } = ChromeUtils.import(
  "chrome://openpgp/content/modules/CollectedKeysDB.jsm"
);

var aliceAcct;

/**
 * When testing a scenario that should automatically process the OpenPGP
 * contents (it's not suppressed e.g. because of a partial content),
 * then we need to wait for the automatic processing to complete.
 */
async function openpgpProcessed() {
  let [subject] = await TestUtils.topicObserved(
    "document-element-inserted",
    document => {
      return document.ownerGlobal?.location == "about:message";
    }
  );

  return BrowserTestUtils.waitForEvent(subject, "openpgpprocessed");
}

/**
 * Set up the base account, identity and keys needed for the tests.
 */
add_setup(async function () {
  aliceAcct = MailServices.accounts.createAccount();
  aliceAcct.incomingServer = MailServices.accounts.createIncomingServer(
    "alice",
    "openpgp.example",
    "pop3"
  );
  let aliceIdentity = MailServices.accounts.createIdentity();
  aliceIdentity.email = "alice@openpgp.example";
  aliceAcct.addIdentity(aliceIdentity);

  // Set up the alice's private key.
  // We need one key set up for use. Otherwise we do not process OpenPGP data.
  let [id] = await OpenPGPTestUtils.importPrivateKey(
    window,
    new FileUtils.File(
      getTestFilePath(
        "data/keys/alice@openpgp.example-0xf231550c4f47e38e-secret.asc"
      )
    )
  );
  aliceIdentity.setUnicharAttribute("openpgp_key_id", id);
});

/**
 * Test that an attached key is collected.
 */
add_task(async function testCollectKeyAttachment() {
  let keycollected = BrowserTestUtils.waitForEvent(window, "keycollected");
  let opengpgprocessed = openpgpProcessed();
  let mc = await open_message_from_file(
    new FileUtils.File(
      getTestFilePath(
        "data/eml/unsigned-unencrypted-key-0x1f10171bfb881b1c-attached.eml"
      )
    )
  );
  await opengpgprocessed;
  let aboutMessage = get_about_message(mc.window);

  Assert.ok(
    OpenPGPTestUtils.hasNoSignedIconState(aboutMessage.document),
    "signed icon is not displayed"
  );
  Assert.ok(
    !OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is not displayed"
  );
  await keycollected;

  let db = await CollectedKeysDB.getInstance();
  let keys = await db.findKeysForEmail("jdoe@invalid");
  Assert.equal(keys.length, 1, "should find one key");

  let sources = keys[0].sources;
  Assert.equal(sources.length, 1, "should have one source");
  let source = sources[0];

  Assert.equal(source.type, "attachment");
  Assert.equal(source.uri, "mid:4a735c72-dc19-48ff-4fa5-2c1f65513b27@invalid");
  Assert.equal(source.description, "OpenPGP_0x1F10171BFB881B1C.asc");

  close_window(mc);
});

/**
 * Test that an Autocrypt header key is collected.
 */
add_task(async function testCollectAutocrypt() {
  let keycollected = BrowserTestUtils.waitForEvent(window, "keycollected");
  let opengpgprocessed = openpgpProcessed();
  let mc = await open_message_from_file(
    new FileUtils.File(
      getTestFilePath(
        "data/eml/unsigned-unencrypted-0x3099ff1238852b9f-autocrypt.eml"
      )
    )
  );
  await opengpgprocessed;
  let aboutMessage = get_about_message(mc.window);

  Assert.ok(
    OpenPGPTestUtils.hasNoSignedIconState(aboutMessage.document),
    "signed icon is not displayed"
  );
  Assert.ok(
    !OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is not displayed"
  );
  await keycollected;

  const carolEmail = "carol@example.com";

  let db = await CollectedKeysDB.getInstance();
  let keys = await db.findKeysForEmail(carolEmail);
  Assert.equal(keys.length, 1, "should find one key");

  let sources = keys[0].sources;
  Assert.equal(sources.length, 1, "should have one source");
  let source = sources[0];

  Assert.equal(source.type, "autocrypt");
  Assert.equal(
    source.uri,
    "mid:b3609461-36e8-0371-1b9d-7ce6864ec66d@example.com"
  );
  Assert.equal(source.description, undefined);

  // Clean up to ensure other tests will not find this key
  db.deleteKeysForEmail(carolEmail);
  keys = await db.findKeysForEmail(carolEmail);
  Assert.equal(keys.length, 0, "should find zero keys after cleanup");

  close_window(mc);
});

/**
 * Test that an Autocrypt-Gossip header key is collected.
 */
add_task(async function testCollectAutocryptGossip() {
  let keycollected = BrowserTestUtils.waitForEvent(window, "keycollected");
  let keycollected2 = BrowserTestUtils.waitForEvent(window, "keycollected");
  let keycollected3 = BrowserTestUtils.waitForEvent(window, "keycollected");
  let opengpgprocessed = openpgpProcessed();
  let msgc = await open_message_from_file(
    new FileUtils.File(
      getTestFilePath("data/eml/signed-encrypted-autocrypt-gossip.eml")
    )
  );
  await opengpgprocessed;
  let aboutMessage = get_about_message(msgc.window);

  Assert.ok(
    OpenPGPTestUtils.hasSignedIconState(aboutMessage.document, "unknown"),
    "signed icon is displayed"
  );
  Assert.ok(
    OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is displayed"
  );

  await keycollected;
  await keycollected2;
  await keycollected3;

  const carolEmail = "carol@example.com";

  let db = await CollectedKeysDB.getInstance();
  let keys = await db.findKeysForEmail(carolEmail);
  Assert.equal(keys.length, 1, "should find one key");

  let sources = keys[0].sources;
  Assert.equal(sources.length, 1, "should have one source");
  let source = sources[0];

  Assert.equal(source.type, "autocrypt");
  Assert.equal(
    source.uri,
    "mid:e8690528-d187-4d99-b505-9f3d6a2704ca@openpgp.example"
  );
  Assert.equal(source.description, undefined);

  // Clean up to ensure other tests will not find this key
  db.deleteKeysForEmail(carolEmail);
  keys = await db.findKeysForEmail(carolEmail);
  Assert.equal(keys.length, 0, "should find zero keys after cleanup");

  close_window(msgc);
});

/**
 * Test that we don't collect keys that refer to an email address that
 * isn't one of the message participants, and that we don't collect keys
 * if we already have a personal key for an email address.
 */
add_task(async function testSkipFakeOrUnrelatedKeys() {
  let opengpgprocessed = openpgpProcessed();
  let mc = await open_message_from_file(
    new FileUtils.File(
      getTestFilePath("data/eml/unrelated-and-fake-keys-attached.eml")
    )
  );
  await opengpgprocessed;
  let aboutMessage = get_about_message(mc.window);

  Assert.ok(
    OpenPGPTestUtils.hasNoSignedIconState(aboutMessage.document),
    "signed icon is not displayed"
  );
  Assert.ok(
    !OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is not displayed"
  );

  let db = await CollectedKeysDB.getInstance();

  let keys = await db.findKeysForEmail("alice@openpgp.example");
  Assert.equal(
    keys.length,
    0,
    "the attached key for alice should have been ignored because we have a personal key for that address"
  );

  keys = await db.findKeysForEmail("stranger@example.com");
  Assert.equal(
    keys.length,
    0,
    "the attached key for stranger should have been ignored because stranger isn't a participant of this message"
  );

  let bobEmail = "bob@openpgp.example";
  keys = await db.findKeysForEmail(bobEmail);
  Assert.equal(keys.length, 1, "bob's key should have been collected");

  db.deleteKeysForEmail(bobEmail);
  keys = await db.findKeysForEmail(bobEmail);
  Assert.equal(keys.length, 0, "should find zero keys after cleanup");

  close_window(mc);
});

/**
 * If an email contains two different keys for the same email address,
 * don't import any keys for that email address.
 */
add_task(async function testSkipDuplicateKeys() {
  let opengpgprocessed = openpgpProcessed();
  let mc = await open_message_from_file(
    new FileUtils.File(getTestFilePath("data/eml/eve-duplicate.eml"))
  );
  await opengpgprocessed;
  let aboutMessage = get_about_message(mc.window);

  Assert.ok(
    OpenPGPTestUtils.hasNoSignedIconState(aboutMessage.document),
    "signed icon is not displayed"
  );
  Assert.ok(
    !OpenPGPTestUtils.hasEncryptedIconState(aboutMessage.document, "ok"),
    "encrypted icon is not displayed"
  );

  let db = await CollectedKeysDB.getInstance();

  let keys = await db.findKeysForEmail("eve@example.com");
  Assert.equal(
    keys.length,
    0,
    "the attached keys for eve should have been ignored"
  );

  close_window(mc);
});

registerCleanupFunction(async function tearDown() {
  MailServices.accounts.removeAccount(aliceAcct, true);
  await OpenPGPTestUtils.removeKeyById("0xf231550c4f47e38e", true);
  await CollectedKeysDB.deleteDb();
});