108 lines
4.1 KiB
JavaScript
108 lines
4.1 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { registerPgpIpcHandlers } = require('../desktop/ipc/pgp.cjs');
|
|
|
|
function registerWith(overrides = {}) {
|
|
const handlers = new Map();
|
|
registerPgpIpcHandlers({
|
|
registerIpcHandler(channel, handler) { handlers.set(channel, handler); },
|
|
BrowserWindow: { fromWebContents: () => ({}) },
|
|
dialog: {
|
|
async showOpenDialog() { return { canceled: true, filePaths: [] }; },
|
|
async showSaveDialog() { return { canceled: true, filePath: '' }; },
|
|
async showMessageBox() { return { response: 0 }; }
|
|
},
|
|
fs: {}, openpgp: {}, pgpKeys: {}, pgpCache: {}, pgpMail: {},
|
|
PgpError: class PgpError extends Error {},
|
|
pgpKeyServers: {}, listServers() { return []; }, MAX_CUSTOM_SERVERS: 10,
|
|
normalizeCustomServer() {}, randomUUID() { return 'test-id'; },
|
|
readAccount() { return null; }, imapClient() {}, folderPath() {}, simpleParser() {}, extractMailContent() {},
|
|
...overrides
|
|
});
|
|
return handlers;
|
|
}
|
|
|
|
test('registers the complete OpenPGP IPC surface through the shared boundary', () => {
|
|
const registered = [];
|
|
registerPgpIpcHandlers({
|
|
registerIpcHandler(channel, handler) { registered.push({ channel, handler }); },
|
|
BrowserWindow: {},
|
|
dialog: {},
|
|
fs: {},
|
|
openpgp: {},
|
|
pgpKeys: {},
|
|
pgpCache: {},
|
|
pgpMail: {},
|
|
PgpError: class PgpError extends Error {},
|
|
pgpKeyServers: {},
|
|
listServers() {},
|
|
MAX_CUSTOM_SERVERS: 10,
|
|
normalizeCustomServer() {},
|
|
randomUUID() {},
|
|
readAccount() {},
|
|
imapClient() {},
|
|
folderPath() {},
|
|
simpleParser() {},
|
|
extractMailContent() {}
|
|
});
|
|
|
|
const channels = registered.map(item => item.channel).sort();
|
|
assert.deepEqual(channels, [
|
|
'pgp:add-contact', 'pgp:add-key-server', 'pgp:change-passphrase', 'pgp:check-send',
|
|
'pgp:contacts', 'pgp:decrypt-message', 'pgp:delete', 'pgp:export-contact',
|
|
'pgp:export-private', 'pgp:export-public', 'pgp:export-revocation', 'pgp:find-key',
|
|
'pgp:forget-passphrases', 'pgp:generate', 'pgp:get-settings', 'pgp:import',
|
|
'pgp:key-servers', 'pgp:list', 'pgp:publish-key', 'pgp:remove-contact',
|
|
'pgp:remove-key-server', 'pgp:set-key-server-enabled', 'pgp:set-settings', 'pgp:set-trust'
|
|
]);
|
|
assert.ok(registered.every(item => typeof item.handler === 'function'));
|
|
});
|
|
|
|
test('native cancellation blocks replacing an existing contact key', async () => {
|
|
const dialogs = [];
|
|
let added = false;
|
|
const handlers = registerWith({
|
|
dialog: {
|
|
async showMessageBox(_window, options) { dialogs.push(options); return { response: 0 }; }
|
|
},
|
|
openpgp: {
|
|
async readKey() {
|
|
return { getFingerprint: () => 'A'.repeat(40), getUserIDs: () => ['Alice <alice@example.test>'] };
|
|
}
|
|
},
|
|
pgpKeys: {
|
|
async listContacts() { return [{ name: 'Alice', emails: ['alice@example.test'], fingerprint: 'B'.repeat(40) }]; },
|
|
async addContact() { added = true; }
|
|
}
|
|
});
|
|
|
|
await assert.rejects(
|
|
handlers.get('pgp:add-contact')({ sender: {} }, { armoredKey: 'new-public-key', source: 'message' }),
|
|
/Key replacement cancelled/
|
|
);
|
|
assert.equal(added, false);
|
|
assert.equal(dialogs.length, 1);
|
|
assert.deepEqual(dialogs[0].buttons, ['Cancel', 'Replace key']);
|
|
assert.match(dialogs[0].detail, /A{4} A{4}/);
|
|
assert.match(dialogs[0].detail, /B{4} B{4}/);
|
|
});
|
|
|
|
test('native confirmation is required before raising contact-key trust', async () => {
|
|
let response = 0;
|
|
let trustChanges = 0;
|
|
const fingerprint = 'C'.repeat(40);
|
|
const handlers = registerWith({
|
|
dialog: { async showMessageBox() { return { response }; } },
|
|
pgpKeys: {
|
|
async listContacts() { return [{ name: 'Carol', emails: ['carol@example.test'], fingerprint }]; },
|
|
async setTrust(id, level) { trustChanges += 1; return { fingerprint: id, trust: level }; }
|
|
}
|
|
});
|
|
|
|
await assert.rejects(handlers.get('pgp:set-trust')({ sender: {} }, fingerprint, 'verified'), /Trust change cancelled/);
|
|
assert.equal(trustChanges, 0);
|
|
response = 1;
|
|
assert.deepEqual(await handlers.get('pgp:set-trust')({ sender: {} }, fingerprint, 'verified'), { fingerprint, trust: 'verified' });
|
|
assert.equal(trustChanges, 1);
|
|
});
|