From 784f55cc5e0c164f231783c014ae779555aa964c Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Mon, 13 May 2024 14:07:37 +0200 Subject: [PATCH] test: add more e2e tests --- package.json | 2 +- test/e2e/decrypt.test.js | 157 +++++++++++++++++++++++++------ test/e2e/private-pgp-2.4-ecc.pem | 15 +++ test/e2e/private-pgp-2.4-rsa.pem | 81 ++++++++++++++++ test/e2e/test-ecc.txt.asc | 8 ++ test/e2e/test-rsa.txt.asc | 14 +++ 6 files changed, 247 insertions(+), 30 deletions(-) create mode 100644 test/e2e/private-pgp-2.4-ecc.pem create mode 100644 test/e2e/private-pgp-2.4-rsa.pem create mode 100644 test/e2e/test-ecc.txt.asc create mode 100644 test/e2e/test-rsa.txt.asc diff --git a/package.json b/package.json index 17f3117c..3193a827 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "build": "make build", "prepack": "make build", "test": "iced3 test/run.iced", - "test:e2e": "node --test test/e2e/" + "test:e2e": "node --enable-source-maps --test test/e2e/" }, "publishConfig": { "access": "public", diff --git a/test/e2e/decrypt.test.js b/test/e2e/decrypt.test.js index aaa5078c..2e90d6ae 100644 --- a/test/e2e/decrypt.test.js +++ b/test/e2e/decrypt.test.js @@ -3,16 +3,14 @@ const fs = require('node:fs/promises'); const assert = require('node:assert'); const kbpgp = require('../..'); -describe('decrypt', () => { - it('works', async () => { +describe('decrypt', async () => { + await it('works', async () => { const key = await fs.readFile(`${__dirname}/private-pgp.pem`, { encoding: 'utf-8', }); const msg = 'wcFMAw+4H7SgaqGOAQ//Yk4RTQoLEhO0TKxN2IUBrCi88ts+CG1SXKeL06sJ2qikN/3n2JYAGGKgkHRICfu5dOnsjyFdLJ1XWUrbsM3XgVWikMbrmzD1Xe7N5DsoZXlt4Wa9pZ+IkZuE6XcKKu9whIJ22ciEwCzFwDmk/CBshdCCVVQ3IYuM6uibEHn/AHQ8K15XhraiSzF6DbJpevs5Cy7b5YHFyE936H25CVnouUQnMPsirpQq3pYeMq/oOtV/m4mfRUUQ7MUxvtrwE4lq4hLjFu5n9rwlcqaFPl7I7BEM++1c9LFpYsP5mTS7hHCZ9wXBqER8fa3fKYx0bK1ihCpjP4zUkR7P/uhWDArXamv7gHX2Kj/Qsbegn7KjTdZlggAmaJl/CuSgCbhySy+E55g3Z1QFajiLRpQ5+RsWFDbbI08YEgzyQ0yNCaRvrkgo7kZ1D95rEGRfY96duOQbjzOEqtvYmFChdemZ2+f9Kh/JH1+X9ynxY/zYe/0p/U7WD3QNTYN18loc4aXiB1adXD5Ka2QfNroLudQBmLaJpJB6wASFfuxddsD5yRnO32NSdRaqIWC1x6ti3ZYJZ2RsNwJExPDzjpQTuMOH2jtpu3q7NHmW3snRKy2YAL2UjI0YdeKIlhc/qLCJt9MRcOxWYvujTMD/yGprhG44qf0jjMkJBu7NjuVIMONujabl9b7SUQGfO/t+3rMuC68bQdCGLlO8gf3hvtD99utzXphi6idjC0HKSW/9KzuMkm+syGmIAYq/0L3EFvpZ38uq7z8KzwFFQHI3sBA34bNEr5zpU5OMWg'; - // This test passes because it does not throw an exception. - assert.strictEqual(1, 1); const pk = await new Promise((resolve, reject) => { kbpgp.KeyManager.import_from_armored_pgp( @@ -34,33 +32,134 @@ describe('decrypt', () => { const ring = new kbpgp.keyring.KeyRing(); ring.add_key_manager(pk); - const startBlock = '-----BEGIN PGP MESSAGE-----\n\n'; - const endBlock = '\n-----END PGP MESSAGE-----'; + let armoredMessage = fixMessage(msg); - let armoredMessage = msg.trim(); - if (!armoredMessage.startsWith(startBlock)) { - armoredMessage = `${startBlock}${armoredMessage}`; - } - if (!armoredMessage.endsWith(endBlock)) { - armoredMessage = `${armoredMessage}${endBlock}`; - } + const data = await new Promise((resolve, reject) => { + kbpgp.unbox( + { + keyfetch: ring, + armored: armoredMessage, + }, + (err, literals) => { + if (err) { + reject(err); + } else { + resolve(literals[0].toString()); + } + }, + ); + }); + + assert.equal(data, '{"o":"abc,def","r":"","v":"123"}'); + }); + + await it('works with pgp 2.4 and ecc', async () => { + const key = await fs.readFile(`${__dirname}/private-pgp-2.4-ecc.pem`, { + encoding: 'utf-8', + }); + + const pk = await new Promise((resolve, reject) => { + kbpgp.KeyManager.import_from_armored_pgp( + { + armored: key.replace(/\n[ \t]+/g, '\n'), + }, + (err, pk) => { + if (err) { + reject(err); + } else { + resolve(pk); + } + }, + ); + }); + + assert.ok(pk); + + const ring = new kbpgp.keyring.KeyRing(); + ring.add_key_manager(pk); + + const msg = await fs.readFile(`${__dirname}/test-ecc.txt.asc`, { + encoding: 'utf-8', + }); const data = await new Promise((resolve, reject) => { - kbpgp.unbox( - { - keyfetch: ring, - armored: armoredMessage, - }, - (err, literals) => { - if (err) { - reject(err); - } else { - resolve(literals[0].toString()); - } - }, - ); - }); - - assert.equal(data, '{"o":"abc,def","r":"","v":"123"}'); + kbpgp.unbox( + { + keyfetch: ring, + armored: msg, + }, + (err, literals) => { + if (err) { + reject(err); + } else { + resolve(literals[0].toString()); + } + }, + ); + }); + + assert.equal(data.trim(), 'test'); + }); + + await it('works with pgp 2.4 and rsa', async () => { + const key = await fs.readFile(`${__dirname}/private-pgp-2.4-rsa.pem`, { + encoding: 'utf-8', + }); + + const pk = await new Promise((resolve, reject) => { + kbpgp.KeyManager.import_from_armored_pgp( + { + armored: key.replace(/\n[ \t]+/g, '\n'), + }, + (err, pk) => { + if (err) { + reject(err); + } else { + resolve(pk); + } + }, + ); + }); + + assert.ok(pk); + + const ring = new kbpgp.keyring.KeyRing(); + ring.add_key_manager(pk); + + const msg = await fs.readFile(`${__dirname}/test-rsa.txt.asc`, { + encoding: 'utf-8', + }); + + const data = await new Promise((resolve, reject) => { + kbpgp.unbox( + { + keyfetch: ring, + armored: msg, + }, + (err, literals) => { + if (err) { + reject(err); + } else { + resolve(literals[0].toString()); + } + }, + ); + }); + + assert.equal(data.trim(), 'test'); }); }); + +function fixMessage(msg) { + const startBlock = '-----BEGIN PGP MESSAGE-----\n\n'; + const endBlock = '\n-----END PGP MESSAGE-----'; + + let armoredMessage = msg.trim(); + if (!armoredMessage.startsWith(startBlock)) { + armoredMessage = `${startBlock}${armoredMessage}`; + } + if (!armoredMessage.endsWith(endBlock)) { + armoredMessage = `${armoredMessage}${endBlock}`; + } + return armoredMessage; +} diff --git a/test/e2e/private-pgp-2.4-ecc.pem b/test/e2e/private-pgp-2.4-ecc.pem new file mode 100644 index 00000000..7ea93202 --- /dev/null +++ b/test/e2e/private-pgp-2.4-ecc.pem @@ -0,0 +1,15 @@ +-----BEGIN PGP PRIVATE KEY BLOCK----- + +lFgEZkHosxYJKwYBBAHaRw8BAQdACDorvXAt65FWfHc6//9cPVsjZIjyh15lJkWH +01pChhMAAQC5Q1iaWv+VukyqmHVnhvQSl5HZ6gMprEc17Jj1/LqzBRGztCFSZW5v +dmF0ZSBUZXN0IDxyZW5vdmF0ZUB0ZXN0LmNvbT6IkAQTFgoAOAIbAwIXgBYhBFAk +o7Sd8lBzvpojYY2H2PbYV2ZTBQJmQgCVBQsJCAcCBhUKCQgLAgQWAgMBAh4FAAoJ +EI2H2PbYV2ZT+boBAJs6O0bkc4MHZIQOfBMoYztQdjCg7QXW8gE1JrQza1+tAQDz +spCMPW3Vw+HPLbF4oWGA/9I8c+tkHVbOh830nvoPBpxdBGZB6LMSCisGAQQBl1UB +BQEBB0Di/1dxEeFhbC9thxGGHFk3rLEwQatU7BpI5oHU+QqdNgMBCAcAAP99h1Zs +8A/78OeSgFBxwwikeACJKxDee318mmDwz6dHmBGliHgEGBYKACAWIQRQJKO0nfJQ +c76aI2GNh9j22FdmUwUCZkHoswIbDAAKCRCNh9j22FdmUy3gAQCId3kAMcUlgM1t +shMpaHnDT7XawogHokJGodarICmolQEAkN8avuZx/8qPqy2BAYKtP0YBFD7fQK9P +hqvy8MDrPgc= +=HTwg +-----END PGP PRIVATE KEY BLOCK----- \ No newline at end of file diff --git a/test/e2e/private-pgp-2.4-rsa.pem b/test/e2e/private-pgp-2.4-rsa.pem new file mode 100644 index 00000000..61cacc87 --- /dev/null +++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hDQEKHZb +C57Sz/ZSUUD/Ru4SOgDKRDmIIPAZXh1C4ktg8A4BETHtxO9jyn91setqDW+RCeO8 +xBt2wQwcW+C97B47IfMp17c3Tcjt7yjxAFiuJqLKdUvPtTmcufBOXugMLCHLDE8S +qK58fcfyDXdsH3uy2jnqq5mwgNu4He1D2muJAbYEGAEIACAWIQRHK2WQXB1GMOQP +CF5gBJX112mscQUCZkH8nwIbDAAKCRBgBJX112mscUFCC/sEaGUlddiQh9Vj2uYY +gsBQXCI5ZfFScv5m9YZs6aTpvuZJnUSJYbXZw6SI4664UXwq7aaql1GZZwSKhgLh +I2xvnnmmJQD2gZaMXaIw3HsX9hPKknYM4syrxUx0DKqXINT97Ob8y3x9aw13NZWd +cpCxHCJq2Usp7vM8kGlLM8cqQSIcy8M8qvFktv/U8ckHM5ebgROA7qUTG28oChFf +Band2p8blPXNe3+BKHbFA+BcGS6Lm9u2Plu0YK89Y7whra9T3xzeuxE3SZz7WpcC +69Hg64HhJj4ZxfAqNTyI9Da1XbBOvX7h501S6QcnBBoQrI0Yp0Y2sOJ0vuKMYTwv +UVUb6tjg266QBjifmR6Tia2jiVVVRnLmUVwA8fewP/WBy9Hl4b+Bx6Mo/GlXAFtU +DkTN9TZuvycGs9C0ii/nkYxaOKzhhMAbF9Qe9PScjXp8WE+ZL5TNjPN7EKobd5Zu +cXap6vnUTA/O+w6LBcDRGJy6UGMxTLosy5Mfo8tiyIUlCNk= +=YlgA +-----END PGP PRIVATE KEY BLOCK----- \ No newline at end of file diff --git a/test/e2e/test-ecc.txt.asc b/test/e2e/test-ecc.txt.asc new file mode 100644 index 00000000..247ccbed --- /dev/null +++ b/test/e2e/test-ecc.txt.asc @@ -0,0 +1,8 @@ +-----BEGIN PGP MESSAGE----- + +hF4DYQZIAg0KuBYSAQdA5NiDH8ye4MUHCKeJDoL7PfCxMfCAOM69tvjAORxI7Ccw +YakY7m8uDpHiy2Qh7mmiq9qjB0jQ4AuIJLeZkXyEPjovDxoIDnCDtk5AErJ2iZH/ +0kcBPxloJCarHSZAmbzHxYVhsMI+Aeu4vkIZ7GXBgrCABMXgbb1cYHSZYRPkIWZx +4ELxhnqDKFSXCgzsRoFzDerg+1+cTwwtGA== +=iVPA +-----END PGP MESSAGE----- diff --git a/test/e2e/test-rsa.txt.asc b/test/e2e/test-rsa.txt.asc new file mode 100644 index 00000000..c8cc1970 --- /dev/null +++ b/test/e2e/test-rsa.txt.asc @@ -0,0 +1,14 @@ +-----BEGIN PGP MESSAGE----- + +hQGMA7GRGhJAkEOcAQwApwgvMZ92PlQP1vD4xqpKMr6mIaJnTv/lsfZBc9ON0riH +Jx9/iBKVqOiZtB7VLLXHhj0rftAjUlXZ5WFMDV8FNeoIUij6wHAWs91lF1wbMCWU +A399O/+4fCeaZdPtMvJFRowzXarowit9a6X7KKZbSkYJrscxge7RAFT2H/PEaFM/ +3xqMH43YRpuOX1f2hx1/ocMzVCo/JnCS+fLyw2KtGaLxXtF+2Cb/aBrJBarpefac +IbqLFANiiJY97SLsA83fi/B8ao4nls61fDtlbHPzOIm8fyy+H89t3z5mHdMww5zu +FXgYd8Bj1NsxB70NxWuHZhrDX3jZltO8f+auzrbGrrY7Vuak0jk78c3cX/JmnaiC +e4+hNzVXHVrTDdmHS9R9s347CtAK9KVgsUfrdOPtdjdCDqMca7enYEDkd1TGY6b5 +Jzjst4p0K1hXVqK/CgrchGk6VgqxwDNyl6PHWsouJRJc950XItI46S+iFRvzxGq0 +cgTmLhZbYreo1CUiCAHh0kcBtMdEJAISribQrx6Mr9BmiAfzuto8k+cxkxu08jQv +jc8irkrbJyOtkkJaP60BGs1lk0S6fXGCOeI3G/EQh8eaSIMBaBovfA== +=WJoj +-----END PGP MESSAGE-----