summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/test/unit/rnp/test_strip.js
blob: 4260921ef26945cbb3daf534d098c557c0fd02af (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
/* 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 stripping keys.
 */

"use strict";

const { RNP } = ChromeUtils.import("chrome://openpgp/content/modules/RNP.jsm");
const { EnigmailKeyRing } = ChromeUtils.import(
  "chrome://openpgp/content/modules/keyRing.jsm"
);
const { OpenPGPTestUtils } = ChromeUtils.import(
  "resource://testing-common/mozmill/OpenPGPTestUtils.jsm"
);
const { MailStringUtils } = ChromeUtils.import(
  "resource:///modules/MailStringUtils.jsm"
);

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

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

  await OpenPGPTestUtils.initOpenPGP();
});

add_task(async function testStripSignatures() {
  await OpenPGPTestUtils.importPublicKey(
    null,
    do_get_file(`${keyDir}/heisenberg-signed-by-pinkman.asc`)
  );

  let heisenbergFpr = "8E3D32E652A254F05BEA9F66CF3EB4AFCAC29340";
  let foundKeys = await RNP.getKeys(["0x" + heisenbergFpr]);

  Assert.equal(foundKeys.length, 1);

  let sigs = RNP.getKeyObjSignatures(foundKeys[0]);

  // Signatures for one user ID
  Assert.equal(sigs.length, 1);

  // The key in the file has two signatures: one self signature,
  // plus one foreign certification signature.
  Assert.equal(sigs[0].sigList.length, 2);

  let reducedKey = RNP.getMultiplePublicKeys([], ["0x" + heisenbergFpr], []);

  // Delete the key we have previously imported
  await RNP.deleteKey(heisenbergFpr);
  foundKeys = await RNP.getKeys(["0x" + heisenbergFpr]);
  Assert.equal(foundKeys.length, 0);

  // Import the reduced key
  let errorObj = {};
  let fingerPrintObj = {};
  let result = await EnigmailKeyRing.importKeyAsync(
    null,
    false,
    reducedKey,
    false,
    null,
    errorObj,
    fingerPrintObj,
    false,
    [],
    false
  );
  Assert.equal(result, 0);

  foundKeys = await RNP.getKeys(["0x" + heisenbergFpr]);
  Assert.equal(foundKeys.length, 1);

  sigs = RNP.getKeyObjSignatures(foundKeys[0]);

  // The imported stripped key should have only the self signature.
  Assert.equal(sigs[0].sigList.length, 1);
});

add_task(async function testKeyWithUnicodeComment() {
  let keyFile = do_get_file(`${keyDir}/key-with-utf8-comment.asc`);
  let keyBlock = await IOUtils.readUTF8(keyFile.path);

  let errorObj = {};
  let fingerPrintObj = {};
  let result = await EnigmailKeyRing.importKeyAsync(
    null,
    false,
    keyBlock,
    false,
    null,
    errorObj,
    fingerPrintObj,
    false,
    [],
    false
  );
  Assert.equal(result, 0);

  let fpr = "72514F43D0060FC588E80238852C55E6D2AFD7EF";
  let foundKeys = await RNP.getKeys(["0x" + fpr]);

  Assert.equal(foundKeys.length, 1);
});

add_task(async function testBinaryKey() {
  let keyFile = do_get_file(`${keyDir}/key-binary.gpg`);
  let keyData = await IOUtils.read(keyFile.path);
  let keyBlock = MailStringUtils.uint8ArrayToByteString(keyData);

  let errorObj = {};
  let fingerPrintObj = {};
  let result = await EnigmailKeyRing.importKeyAsync(
    null,
    false,
    keyBlock,
    true,
    null,
    errorObj,
    fingerPrintObj,
    false,
    [],
    false
  );
  Assert.equal(result, 0);

  let fpr = "683F775BA2E5F0ADEBB29697A2D1B914F722004E";
  let foundKeys = await RNP.getKeys(["0x" + fpr]);

  Assert.equal(foundKeys.length, 1);
});