summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/test/unit/rnp/test_alias.js
blob: cc30b52a8aa2b840c2ed6322aa77c94bba5a8fd0 (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
319
320
321
/* 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 OpenPGP encryption alias rules.
 */

"use strict";

const { RNP } = ChromeUtils.import("chrome://openpgp/content/modules/RNP.jsm");
const { EnigmailConstants } = ChromeUtils.import(
  "chrome://openpgp/content/modules/constants.jsm"
);
const { EnigmailKeyRing } = ChromeUtils.import(
  "chrome://openpgp/content/modules/keyRing.jsm"
);
const { EnigmailEncryption } = ChromeUtils.import(
  "chrome://openpgp/content/modules/encryption.jsm"
);
const { OpenPGPAlias } = ChromeUtils.import(
  "chrome://openpgp/content/modules/OpenPGPAlias.jsm"
);

const { OpenPGPTestUtils } = ChromeUtils.import(
  "resource://testing-common/mozmill/OpenPGPTestUtils.jsm"
);

const keyDir = "../../../../../test/browser/openpgp/data/keys";
const mailNewsDir = "../../../../../../mailnews/test/data";

// Alice's key: EB85BB5FA33A75E15E944E63F231550C4F47E38E
// Bob's key:   D1A66E1A23B182C9980F788CFBFCC82A015E7330
// Carol's key: B8F2F6F4BD3AD3F82DC446833099FF1238852B9F

const tests = [
  {
    info: "Should find Alice's key directly",
    filename: undefined,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: null,
  },
  {
    info: "Key absent, no alias defined for address",
    filename: `${mailNewsDir}/alias-1.json`,
    to: "nobody@openpgp.example",
    expectedMissing: true,
    expectedAliasKeys: null,
  },
  {
    info: "File maps Alice's address to Bob's (id) and Carol's (fingerprint) keys",
    filename: `${mailNewsDir}/alias-1.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: [
      "D1A66E1A23B182C9980F788CFBFCC82A015E7330",
      "B8F2F6F4BD3AD3F82DC446833099FF1238852B9F",
    ],
  },
  {
    info: "File maps Alice's address to an absent key",
    filename: `${mailNewsDir}/alias-2.json`,
    to: "alice@openpgp.example",
    expectedMissing: true,
    expectedAliasKeys: null,
  },
  {
    info: "File maps Alice's address to Alice's key (unnecessary alias)",
    filename: `${mailNewsDir}/alias-3.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: ["EB85BB5FA33A75E15E944E63F231550C4F47E38E"],
  },
  {
    info: "File maps an address to several keys, all available",
    filename: `${mailNewsDir}/alias-4.json`,
    to: "nobody@example.com",
    expectedMissing: false,
    expectedAliasKeys: [
      "EB85BB5FA33A75E15E944E63F231550C4F47E38E",
      "D1A66E1A23B182C9980F788CFBFCC82A015E7330",
      "B8F2F6F4BD3AD3F82DC446833099FF1238852B9F",
    ],
  },
  {
    info: "File maps an address to several keys, one not available",
    filename: `${mailNewsDir}/alias-5.json`,
    to: "nobody@example.com",
    expectedMissing: true,
    expectedAliasKeys: null,
  },
  {
    info: "File maps the domain to Carol's key",
    filename: `${mailNewsDir}/alias-6.json`,
    to: "someone@example.com",
    expectedMissing: false,
    expectedAliasKeys: ["B8F2F6F4BD3AD3F82DC446833099FF1238852B9F"],
  },
  {
    info: "Multiple rules, should match domain1 rule",
    filename: `${mailNewsDir}/alias-7.json`,
    to: "someone@domain1.example.com",
    expectedMissing: false,
    expectedAliasKeys: ["EB85BB5FA33A75E15E944E63F231550C4F47E38E"],
  },
  {
    info: "Multiple rules, should match domain2 rule",
    filename: `${mailNewsDir}/alias-7.json`,
    to: "contact@domain2.example.com",
    expectedMissing: false,
    expectedAliasKeys: ["D1A66E1A23B182C9980F788CFBFCC82A015E7330"],
  },
  {
    info: "Multiple rules, should match email contact@domain1 rule",
    filename: `${mailNewsDir}/alias-7.json`,
    to: "contact@domain1.example.com",
    expectedMissing: false,
    expectedAliasKeys: [
      "D1A66E1A23B182C9980F788CFBFCC82A015E7330",
      "EB85BB5FA33A75E15E944E63F231550C4F47E38E",
    ],
  },
  {
    info: "Multiple rules, shouldn't match",
    filename: `${mailNewsDir}/alias-7.json`,
    to: "contact@domain2.example",
    expectedMissing: true,
    expectedAliasKeys: null,
  },
  {
    info: "Mixed case test a",
    filename: `${mailNewsDir}/alias-8.json`,
    to: "a@UPPERDOM.EXAMPLE",
    expectedMissing: false,
    expectedAliasKeys: ["EB85BB5FA33A75E15E944E63F231550C4F47E38E"],
  },
  {
    info: "Mixed case test b",
    filename: `${mailNewsDir}/alias-8.json`,
    to: "b@lowerdom.example",
    expectedMissing: false,
    expectedAliasKeys: ["D1A66E1A23B182C9980F788CFBFCC82A015E7330"],
  },
  {
    info: "Mixed case test c",
    filename: `${mailNewsDir}/alias-8.json`,
    to: "C@MIXed.EXample",
    expectedMissing: false,
    expectedAliasKeys: ["B8F2F6F4BD3AD3F82DC446833099FF1238852B9F"],
  },
  {
    info: "Mixed case test d",
    filename: `${mailNewsDir}/alias-13.json`,
    to: "NAME@DOMAIN.NET",
    expectedMissing: false,
    expectedAliasKeys: ["D1A66E1A23B182C9980F788CFBFCC82A015E7330"],
  },
  {
    info: "Mixed case test e",
    filename: `${mailNewsDir}/alias-14.json`,
    to: "name@domain.net",
    expectedMissing: false,
    expectedAliasKeys: ["D1A66E1A23B182C9980F788CFBFCC82A015E7330"],
  },
  {
    info: "Mixed case test f",
    filename: `${mailNewsDir}/alias-15.json`,
    to: "name@domain.net",
    expectedMissing: false,
    expectedAliasKeys: ["D1A66E1A23B182C9980F788CFBFCC82A015E7330"],
  },
  {
    info: "JSON with bad syntax, should find Alice's key directly",
    filename: `${mailNewsDir}/alias-9.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: null,
    expectException: true,
  },
  {
    info: "JSON with missing keys entry, should find Alice's key directly",
    filename: `${mailNewsDir}/alias-10.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: null,
  },
  {
    info: "JSON with empty keys entry, should find Alice's key directly",
    filename: `${mailNewsDir}/alias-11.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: null,
  },
  {
    info: "JSON with bad type keys entry, should find Alice's key directly",
    filename: `${mailNewsDir}/alias-12.json`,
    to: "alice@openpgp.example",
    expectedMissing: false,
    expectedAliasKeys: null,
  },
];

/**
 * Initialize OpenPGP add testing keys.
 */
add_setup(async function () {
  do_get_profile();

  await OpenPGPTestUtils.initOpenPGP();

  await OpenPGPTestUtils.importPublicKey(
    null,
    do_get_file(`${keyDir}/alice@openpgp.example-0xf231550c4f47e38e-pub.asc`)
  );

  await OpenPGPTestUtils.importPublicKey(
    null,
    do_get_file(`${keyDir}/bob@openpgp.example-0xfbfcc82a015e7330-pub.asc`)
  );

  await OpenPGPTestUtils.importPublicKey(
    null,
    do_get_file(`${keyDir}/carol@example.com-0x3099ff1238852b9f-pub.asc`)
  );
});

add_task(async function testAlias() {
  let aliasFilename = "openpgp-alias-rules.json";
  let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);

  for (let test of tests) {
    if (test.filename) {
      info(`Running alias test with rules from: ${test.filename}`);

      // Copy test file to profile directory (which is a relative path),
      // because load function only works with simple filenames
      // or absolute file URLs.

      let inFile = do_get_file(test.filename);
      inFile.copyTo(profileDir, aliasFilename);

      try {
        await OpenPGPAlias._loadFromFile(aliasFilename);
        Assert.ok(
          !("expectException" in test) || !test.expectException,
          "expected no load exception"
        );
      } catch (ex) {
        console.log(
          "exception when loading alias file " + aliasFilename + " : " + ex
        );
        Assert.ok(
          "expectException" in test && test.expectException,
          "expected load exception"
        );
      }
    } else {
      info(`Running alias test without rules`);
      OpenPGPAlias._clear();
    }
    info(test.info);

    let addresses = [test.to];
    let resultDetails = {};

    let isMissing = await EnigmailKeyRing.getValidKeysForAllRecipients(
      addresses,
      resultDetails
    );

    Assert.ok(
      (isMissing && test.expectedMissing) ||
        (!isMissing && !test.expectedMissing),
      "Should have the expected result from getValidKeysForAllRecipients"
    );

    if (isMissing || test.expectedMissing) {
      continue;
    }

    let errorMsgObj = { value: "" };
    let logFileObj = {};
    let encryptArgs = EnigmailEncryption.getCryptParams(
      "",
      test.to,
      "",
      "SHA256",
      EnigmailConstants.SEND_ENCRYPTED,
      0,
      errorMsgObj,
      logFileObj
    );

    let foundAliasKeys = encryptArgs.aliasKeys.get(test.to.toLowerCase());

    if (!test.expectedAliasKeys) {
      Assert.ok(!foundAliasKeys, "foundAliasKeys should be empty");
    } else {
      Assert.equal(foundAliasKeys.length, test.expectedAliasKeys.length);

      test.expectedAliasKeys.forEach((val, i) => {
        Assert.ok(foundAliasKeys.includes(val));
      });

      let encryptResult = {};
      let encrypted = await RNP.encryptAndOrSign(
        "plaintext",
        encryptArgs,
        encryptResult
      );

      Assert.ok(
        !encryptResult.exitCode,
        "RNP.encryptAndOrSign() should exit ok"
      );

      Assert.ok(encrypted.includes("END PGP MESSAGE"));
    }
  }
});