js: clean up test extension

--

Tests will now run with one instance of gpgmejs each block,
which reduces overhead. Readability is (hopefully) improved),
some negative tests are added.

There is still a performance problem in base64 encoding/decoding,
which causes some tests to fail due to time out.
This commit is contained in:
Maximilian Krambach 2018-07-27 11:20:33 +02:00
parent 4b343c4e33
commit b18b96fb36
7 changed files with 425 additions and 461 deletions

View File

@ -22,18 +22,25 @@
* Raimund Renkert <rrenkert@intevation.de> * Raimund Renkert <rrenkert@intevation.de>
*/ */
/* global describe, it, expect, Gpgmejs, ImportablePublicKey, inputvalues */ /* global describe, it, expect, before, afterEach, Gpgmejs*/
/* global ImportablePublicKey, inputvalues */
describe('Key importing', function () { describe('Key importing', function () {
it('Prepare test Key (deleting it from gnupg, if present)', function(done){ const fpr = ImportablePublicKey.fingerprint;
let prm = Gpgmejs.init(); const pubKey = ImportablePublicKey.key;
prm.then(function (context) { const changedKey = ImportablePublicKey.keyChangedUserId;
expect(context.Keyring.getKeys).to.be.a('function');
context.Keyring.getKeys(ImportablePublicKey.fingerprint).then( let context = null;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
context.Keyring.getKeys(fpr).then(
function(result){ function(result){
if (result.length === 1) { if (result.length === 1) {
result[0].delete().then(function(result){ result[0].delete().then(function(){
expect(result).to.be.true; done();
},function(){
done(); done();
}); });
} else { } else {
@ -43,76 +50,79 @@ describe('Key importing', function () {
}); });
}); });
it('importing, updating, then deleting public Key', function (done) { afterEach(function(done){
//This test runs in one large step, to ensure the proper state of the // delete the test key if still present
// key in all stages. context.Keyring.getKeys(fpr).then(
let prm = Gpgmejs.init();
prm.then(function (context) {
context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
function(result){ function(result){
if (result.length === 1) {
result[0].delete().then(function(){
done();
},function(){
done();
});
} else {
done();
}
});
});
it('Importing Key', function (done) {
context.Keyring.getKeys(fpr).then(function(result){
expect(result).to.be.an('array'); expect(result).to.be.an('array');
expect(result.length).to.equal(0); expect(result.length).to.equal(0);
context.Keyring.importKey(ImportablePublicKey.key, true) context.Keyring.importKey(pubKey).then(function(result){
expect(result[0]).to.not.be.undefined;
expect(result[0].key).to.be.an('object');
expect(result[0].key.fingerprint).to.equal(fpr);
expect(result[0].status).to.equal('newkey');
done();
});
});
});
it('Updating Key', function(done){
context.Keyring.importKey(pubKey)
.then(function(result){ .then(function(result){
expect(result.Keys[0]).to.not.be.undefined; expect(result[0].key).to.not.be.undefined;
expect(result.Keys[0].key).to.be.an('object'); expect(result[0].status).to.equal('newkey');
expect(result.Keys[0].key.fingerprint).to.equal( context.Keyring.importKey(changedKey).then(function(res){
ImportablePublicKey.fingerprint); expect(res[0].key).to.be.an('object');
expect(result.Keys[0].status).to.equal('newkey'); expect(res[0].key.fingerprint).to.equal(fpr);
context.Keyring.importKey( expect(res[0].status).to.equal('change');
ImportablePublicKey.keyChangedUserId,true) expect(res[0].changes.userId).to.be.true;
.then(function(res){ expect(res[0].changes.subkey).to.be.false;
expect(res.Keys[0]).to.not.be.undefined; expect(res[0].changes.signature).to.be.true;
expect(res.Keys[0].key).to.be.an('object'); done();
expect(res.Keys[0].key.fingerprint).to.equal( });
ImportablePublicKey.fingerprint); });
expect(res.Keys[0].status).to.equal( });
'change');
expect( it('Deleting Key', function(done) {
res.Keys[0].changes.userId).to.be.true; context.Keyring.importKey(pubKey).then(function(result){
expect( expect(result[0].key).to.be.an('object');
res.Keys[0].changes.subkey).to.be.false; expect(result[0].key.fingerprint).to.equal(fpr);
expect( result[0].key.delete().then(function(result){
res.Keys[0].changes.signature).to.be.true;
res.Keys[0].key.delete().then(function(result){
expect(result).to.be.true; expect(result).to.be.true;
done(); done();
}); });
}); });
}); });
});
});
});
it('Import result feedback', function(done){ it('Import result feedback', function(done){
let prm = Gpgmejs.init(); context.Keyring.importKey(pubKey, true).then(function(result){
prm.then(function (context) {
context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
function(result){
expect(result).to.be.an('array');
expect(result.length).to.equal(0);
context.Keyring.importKey(ImportablePublicKey.key, true)
.then(function(result){
expect(result).to.be.an('object'); expect(result).to.be.an('object');
expect(result.Keys[0]).to.be.an('object'); expect(result.Keys[0]).to.be.an('object');
expect(result.Keys[0].key.fingerprint).to.equal( expect(result.Keys[0].key.fingerprint).to.equal(fpr);
ImportablePublicKey.fingerprint);
expect(result.Keys[0].status).to.equal('newkey'); expect(result.Keys[0].status).to.equal('newkey');
result.Keys[0].key.getArmor().then(function(armor){ result.Keys[0].key.getArmor().then(function(armor){
expect(armor).to.be.a('string'); expect(armor).to.be.a('string');
result.Keys[0].key.delete().then(function(){
done(); done();
}); });
}); });
}); });
});
});
});
it('exporting armored Key with getKeysArmored', function (done) { it('exporting armored Key with getKeysArmored', function (done) {
let prm = Gpgmejs.init(); context.Keyring.importKey(pubKey).then(function(){
const fpr = inputvalues.encrypt.good.fingerprint;
prm.then(function (context) {
context.Keyring.getKeysArmored(fpr).then(function(result){ context.Keyring.getKeysArmored(fpr).then(function(result){
expect(result).to.be.an('object'); expect(result).to.be.an('object');
expect(result.armored).to.be.a('string'); expect(result.armored).to.be.a('string');
@ -121,18 +131,15 @@ describe('Key importing', function () {
}); });
}); });
}); });
it('exporting armored Key (including secret fingerprints) with '
+ 'getKeysArmored', function (done) { it('Exporting Key (including secret fingerprints)', function (done) {
let prm = Gpgmejs.init(); const key_secret = inputvalues.encrypt.good.fingerprint;
const fpr = inputvalues.encrypt.good.fingerprint; context.Keyring.getKeysArmored(key_secret, true).then(function(result){
prm.then(function (context) {
context.Keyring.getKeysArmored(fpr, true).then(function(result){
expect(result).to.be.an('object'); expect(result).to.be.an('object');
expect(result.armored).to.be.a('string'); expect(result.armored).to.be.a('string');
expect(result.secret_fprs).to.be.an('array'); expect(result.secret_fprs).to.be.an('array');
expect(result.secret_fprs[0]).to.equal(fpr); expect(result.secret_fprs[0]).to.equal(key_secret);
done(); done();
}); });
}); });
}); });
});

View File

@ -21,21 +21,29 @@
* Maximilian Krambach <mkrambach@intevation.de> * Maximilian Krambach <mkrambach@intevation.de>
*/ */
/* global describe, it, expect, Gpgmejs */ /* global describe, it, expect, before, Gpgmejs */
/* global inputvalues, encryptedData, bigString, bigBoringString */ /* global inputvalues, encryptedData, bigString, bigBoringString */
describe('Encryption and Decryption', function () { describe('Encryption and Decryption', function () {
let context = null;
let good_fpr = inputvalues.encrypt.good.fingerprint;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
done();
});
});
it('Successful encrypt and decrypt simple string', function (done) { it('Successful encrypt and decrypt simple string', function (done) {
let prm = Gpgmejs.init(); let data = inputvalues.encrypt.good.data;
prm.then(function (context) { context.encrypt(data, good_fpr).then(function (answer) {
context.encrypt(
inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE'); expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE'); expect(answer.data).to.include('END PGP MESSAGE');
context.decrypt(answer.data).then(function (result) { context.decrypt(answer.data).then(function (result) {
expect(result).to.not.be.empty; expect(result).to.not.be.empty;
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
@ -45,115 +53,57 @@ describe('Encryption and Decryption', function () {
}); });
}); });
}); });
});
it('Decrypt simple non-ascii', function (done) { it('Decrypt simple non-ascii', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
let data = encryptedData; let data = encryptedData;
context.decrypt(data).then( context.decrypt(data).then(function (result) {
function (result) {
expect(result).to.not.be.empty; expect(result).to.not.be.empty;
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
expect(result.data).to.equal( expect(result.data).to.equal(
'¡Äußerste µ€ før ñoquis@hóme! Добрый день\n'); '¡Äußerste µ€ før ñoquis@hóme! Добрый день\n');
done(); done();
}); });
});
}).timeout(3000); }).timeout(3000);
it('Roundtrip does not destroy trailing whitespace', it('Trailing whitespace and different line endings', function (done) {
function (done) { const data = 'Keks. \rKeks \n Keks \r\n';
let prm = Gpgmejs.init(); context.encrypt(data, good_fpr).then(function (answer) {
prm.then(function (context) {
let data = 'Keks. \rKeks \n Keks \r\n';
context.encrypt(data,
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include( expect(answer.data).to.include('BEGIN PGP MESSAGE');
'BEGIN PGP MESSAGE'); expect(answer.data).to.include('END PGP MESSAGE');
expect(answer.data).to.include(
'END PGP MESSAGE'); context.decrypt(answer.data).then(function (result) {
context.decrypt(answer.data).then(
function (result) {
expect(result).to.not.be.empty; expect(result).to.not.be.empty;
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
expect(result.data).to.equal(data); expect(result.data).to.equal(data);
done(); done();
});
}); });
}); });
}).timeout(5000); }).timeout(5000);
for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
(j + 1) + '/'
+ inputvalues.encrypt.good.data_nonascii_32.length + ')',
function (done) {
let input = inputvalues.encrypt.good.data_nonascii_32[j];
expect(input).to.have.length(32);
let prm = Gpgmejs.init();
prm.then(function (context) {
let data = '';
for (let i=0; i < 34 * 1024; i++){
data += input;
}
context.encrypt(data,
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
'END PGP MESSAGE');
context.decrypt(answer.data).then(
function (result) {
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(data);
done();
});
});
});
}).timeout(3000);
}
it('Random data, as string', function (done) { it('Random data, as string', function (done) {
let data = bigString(1000); let data = bigString(1000);
let prm = Gpgmejs.init(); context.encrypt(data, good_fpr).then(function (answer) {
prm.then(function (context) {
context.encrypt(data,
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include( expect(answer.data).to.include(
'BEGIN PGP MESSAGE'); 'BEGIN PGP MESSAGE');
expect(answer.data).to.include( expect(answer.data).to.include(
'END PGP MESSAGE'); 'END PGP MESSAGE');
context.decrypt(answer.data).then( context.decrypt(answer.data).then(function (result) {
function (result) {
expect(result).to.not.be.empty; expect(result).to.not.be.empty;
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
expect(result.data).to.equal(data); expect(result.data).to.equal(data);
done(); done();
}); });
}); });
});
}).timeout(3000); }).timeout(3000);
it('Data, input as base64', function (done) { it('Data, input as base64', function (done) {
let data = inputvalues.encrypt.good.data; let data = inputvalues.encrypt.good.data;
let b64data = btoa(data); let b64data = btoa(data);
let prm = Gpgmejs.init(); context.encrypt(b64data, good_fpr, true).then(function (answer) {
prm.then(function (context) {
context.encrypt(b64data,
inputvalues.encrypt.good.fingerprint, true).then(
function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include( expect(answer.data).to.include(
@ -168,17 +118,12 @@ describe('Encryption and Decryption', function () {
done(); done();
}); });
}); });
});
}).timeout(3000); }).timeout(3000);
it('Random data, input as base64', function (done) { it('Random data, input as base64', function (done) {
let data = bigBoringString(0.001); let data = bigBoringString(0.001);
let b64data = btoa(data); let b64data = btoa(data);
let prm = Gpgmejs.init(); context.encrypt(b64data, good_fpr, true).then(function (answer) {
prm.then(function (context) {
context.encrypt(b64data,
inputvalues.encrypt.good.fingerprint, true).then(
function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include( expect(answer.data).to.include(
@ -193,7 +138,33 @@ describe('Encryption and Decryption', function () {
done(); done();
}); });
}); });
});
}).timeout(3000); }).timeout(3000);
for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
(j + 1) + '/'
+ inputvalues.encrypt.good.data_nonascii_32.length + ')',
function (done) {
let input = inputvalues.encrypt.good.data_nonascii_32[j];
expect(input).to.have.length(32);
let data = '';
for (let i=0; i < 34 * 1024; i++){
data += input;
}
context.encrypt(data,good_fpr).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
'END PGP MESSAGE');
context.decrypt(answer.data).then(function (result) {
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(data);
done();
});
});
}).timeout(5000);
}
}); });

View File

@ -21,16 +21,23 @@
* Maximilian Krambach <mkrambach@intevation.de> * Maximilian Krambach <mkrambach@intevation.de>
*/ */
/* global describe, it, expect, Gpgmejs */ /* global describe, it, expect, before, Gpgmejs */
/* global inputvalues, fixedLengthString */ /* global inputvalues, fixedLengthString */
describe('Encryption', function () { describe('Encryption', function () {
let context = null;
const good_fpr = inputvalues.encrypt.good.fingerprint;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
done();
});
});
it('Successful encrypt', function (done) { it('Successful encrypt', function (done) {
let prm = Gpgmejs.init(); const data = inputvalues.encrypt.good.data;
prm.then(function (context) { context.encrypt(data, good_fpr).then(function (answer) {
context.encrypt(
inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE'); expect(answer.data).to.include('BEGIN PGP MESSAGE');
@ -38,62 +45,24 @@ describe('Encryption', function () {
done(); done();
}); });
}); });
});
it('Successful encrypt 5 MB', function (done) { const sizes = [5,20,50];
let prm = Gpgmejs.init(); for (let i=0; i < sizes.length; i++) {
let data = fixedLengthString(5); it('Successful encrypt a ' + sizes[i] + 'MB message', function (done) {
prm.then(function (context) { const data = fixedLengthString(sizes[i]);
context.encrypt( context.encrypt(data, good_fpr).then(function (answer) {
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE'); expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE'); expect(answer.data).to.include('END PGP MESSAGE');
done(); done();
}); });
});
}).timeout(10000);
it('Successful encrypt 20 MB', function (done) {
let prm = Gpgmejs.init();
let data = fixedLengthString(20);
prm.then(function (context) {
context.encrypt(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
}).timeout(20000);
it('Successful encrypt 50 MB', function (done) {
let prm = Gpgmejs.init();
let data = fixedLengthString(50);
prm.then(function (context) {
context.encrypt(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
}).timeout(20000); }).timeout(20000);
}
it('Sending encryption without keys fails', function (done) { it('Sending encryption without keys fails', function (done) {
let prm = Gpgmejs.init(); const data = inputvalues.encrypt.good.data;
prm.then(function (context) { context.encrypt(data,null).then(function (answer) {
context.encrypt(
inputvalues.encrypt.good.data,
null).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function(error){ }, function(error){
expect(error).to.be.an('Error'); expect(error).to.be.an('Error');
@ -101,13 +70,9 @@ describe('Encryption', function () {
done(); done();
}); });
}); });
});
it('Sending encryption without data fails', function (done) { it('Sending encryption without data fails', function (done) {
let prm = Gpgmejs.init(); context.encrypt(null, good_fpr).then(function (answer) {
prm.then(function (context) {
context.encrypt(
null, inputvalues.encrypt.good.keyid).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function (error) { }, function (error) {
expect(error).to.be.an.instanceof(Error); expect(error).to.be.an.instanceof(Error);
@ -115,14 +80,11 @@ describe('Encryption', function () {
done(); done();
}); });
}); });
});
it('Sending encryption with non existing keys fails', function (done) { it('Sending encryption with non existing keys fails', function (done) {
let prm = Gpgmejs.init(); const data = inputvalues.encrypt.good.data;
prm.then(function (context) { const bad_fpr = inputvalues.encrypt.bad.fingerprint;
context.encrypt( context.encrypt(data, bad_fpr).then(function (answer) {
inputvalues.encrypt.good.data,
inputvalues.encrypt.bad.fingerprint).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function(error){ }, function(error){
expect(error).to.be.an('Error'); expect(error).to.be.an('Error');
@ -130,15 +92,11 @@ describe('Encryption', function () {
expect(error.code).to.equal('GNUPG_ERROR'); expect(error.code).to.equal('GNUPG_ERROR');
done(); done();
}); });
});
}).timeout(5000); }).timeout(5000);
it('Overly large message ( > 64MB) is rejected', function (done) { it('Overly large message ( > 64MB) is rejected', function (done) {
let prm = Gpgmejs.init(); const data = fixedLengthString(65);
prm.then(function (context) { context.encrypt(data, good_fpr).then(function (answer) {
context.encrypt(
fixedLengthString(65),
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function(error){ }, function(error){
expect(error).to.be.an.instanceof(Error); expect(error).to.be.an.instanceof(Error);
@ -149,7 +107,6 @@ describe('Encryption', function () {
// this library // this library
done(); done();
}); });
});
}).timeout(8000); }).timeout(8000);
// TODO check different valid parameter // TODO check different valid parameter

View File

@ -53,6 +53,39 @@ const inputvalues = {// eslint-disable-line no-unused-vars
// bogus fingerprint) // bogus fingerprint)
fingerprint: 'CDC3A2B2860625CCBFC5AAAAAC6D1B604967FC4A' fingerprint: 'CDC3A2B2860625CCBFC5AAAAAC6D1B604967FC4A'
} }
},
signedMessage: {
good: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
'Hash: SHA256\n' +
'\n' +
'Matschige Münsteraner Marshmallows\n' +
'-----BEGIN PGP SIGNATURE-----\n' +
'\n' +
'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
'=ioB6\n' +
'-----END PGP SIGNATURE-----\n',
bad: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
'Hash: SHA256\n' +
'\n' +
'Matschige Münchener Marshmallows\n' +
'-----BEGIN PGP SIGNATURE-----\n' +
'\n' +
'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
'=ioB6\n' +
'-----END PGP SIGNATURE-----\n',
} }
}; };
@ -83,7 +116,7 @@ function bigUint8(megabytes){// eslint-disable-line no-unused-vars
let maxlength = 1024 * 1024 * megabytes; let maxlength = 1024 * 1024 * megabytes;
let uint = new Uint8Array(maxlength); let uint = new Uint8Array(maxlength);
for (let i= 0; i < maxlength; i++){ for (let i= 0; i < maxlength; i++){
uint[i] = Math.random() * Math.floor(256); uint[i] = Math.floor(Math.random() * 256);
} }
return uint; return uint;
} }
@ -125,6 +158,27 @@ function slightlyLessBoringString(megabytes, set){
return string.join(''); return string.join('');
} }
// Take a gpg looking string and destroy it a bit by changing random values
// eslint-disable-next-line no-unused-vars
function destroylegitimateGpg(string, mutations=5){
const allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/\n';
for (let i=0; i < mutations.length; i++){
// leave the first and last 35 chars (header/footer) intact
let position = Math.floor(Math.random() *(string.length - 70)) + 35;
let str0 = string.substring(0,position - 1);
let str1 = string.substring(position, position + 1);
let str2 = string.substring(position +1);
let success = false;
while (!success){
let newchar = Math.floor(Math.random() * allowed.length);
if (newchar !== str1){
string = str0 + newchar + str2;
success = true;
}
}
}
}
// Data encrypted with testKey // Data encrypted with testKey
const encryptedData =// eslint-disable-line no-unused-vars const encryptedData =// eslint-disable-line no-unused-vars
'-----BEGIN PGP MESSAGE-----\n' + '-----BEGIN PGP MESSAGE-----\n' +

View File

@ -20,61 +20,37 @@
* Author(s): * Author(s):
* Maximilian Krambach <mkrambach@intevation.de> * Maximilian Krambach <mkrambach@intevation.de>
*/ */
/* global describe, it, expect, Gpgmejs */ /* global describe, it, before, expect, Gpgmejs */
/* global bigString, inputvalues */ /* global bigString, inputvalues */
describe('Long running Encryption/Decryption', function () { describe('Long running Encryption/Decryption', function () {
let context = null;
const good_fpr = inputvalues.encrypt.good.fingerprint;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
done();
});
});
for (let i=0; i < 101; i++) { for (let i=0; i < 101; i++) {
it('Successful encrypt/decrypt completely random data ' + it('Successful encrypt/decrypt completely random data '
(i+1) + '/100', function (done) { + (i+1) + '/100', function (done) {
let prm = Gpgmejs.init(); const data = bigString(2*1024*1024);
let data = bigString(2*1024*1024); context.encrypt(data,good_fpr).then(function (answer){
prm.then(function (context) {
context.encrypt(data,
inputvalues.encrypt.good.fingerprint).then(
function (answer){
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include( expect(answer.data).to.include('BEGIN PGP MESSAGE');
'BEGIN PGP MESSAGE'); expect(answer.data).to.include('END PGP MESSAGE');
expect(answer.data).to.include( context.decrypt(answer.data).then(function(result){
'END PGP MESSAGE');
context.decrypt(answer.data).then(
function(result){
expect(result).to.not.be.empty; expect(result).to.not.be.empty;
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
/*
if (result.data.length !== data.length) {
console.log('diff: ' +
(result.data.length - data.length));
for (let i=0; i < result.data.length; i++){
if (result.data[i] !== data[i]){
console.log('position: ' + i);
console.log('result : ' +
result.data.charCodeAt(i) +
result.data[i-2] +
result.data[i-1] +
result.data[i] +
result.data[i+1] +
result.data[i+2]);
console.log('original: ' +
data.charCodeAt(i) +
data[i-2] +
data[i-1] +
data[i] +
data[i+1] +
data[i+2]);
break;
}
}
}
*/
expect(result.data).to.equal(data); expect(result.data).to.equal(data);
done(); done();
}); });
}); });
}); }).timeout(15000);
}).timeout(8000);
} }
}); });

View File

@ -21,17 +21,24 @@
* Maximilian Krambach <mkrambach@intevation.de> * Maximilian Krambach <mkrambach@intevation.de>
*/ */
/* global describe, it, expect, Gpgmejs */ /* global describe, it, expect, before, Gpgmejs */
/* global bigString, inputvalues */ /* global bigString, inputvalues */
describe('Signing', function () { describe('Signing', function () {
let context = null;
const good_fpr = inputvalues.encrypt.good.fingerprint;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
done();
});
});
it('Sign a message', function (done) { it('Sign a message', function (done) {
let prm = Gpgmejs.init(); const data = bigString(100);
prm.then(function (context) { context.sign(data, good_fpr).then(function (answer) {
let data = bigString(100);
context.sign(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP SIGNATURE'); expect(answer.data).to.include('BEGIN PGP SIGNATURE');
@ -40,16 +47,10 @@ describe('Signing', function () {
done(); done();
}); });
}); });
});
it('Detached sign a message', function (done) { it('Detached sign a message', function (done) {
let prm = Gpgmejs.init(); const data = bigString(100);
prm.then(function (context) { context.sign(data,good_fpr, 'detached').then(function (answer) {
let data = bigString(100);
context.sign(
data,
inputvalues.encrypt.good.fingerprint,
'detached'
).then(function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string'); expect(answer.data).to.be.a('string');
expect(answer.data).to.include(data); expect(answer.data).to.include(data);
@ -58,6 +59,5 @@ describe('Signing', function () {
done(); done();
}); });
}); });
});
}); });

View File

@ -21,50 +21,50 @@
* Maximilian Krambach <mkrambach@intevation.de> * Maximilian Krambach <mkrambach@intevation.de>
*/ */
/* global describe, it, expect, bigString, inputvalues, Gpgmejs */ /* global describe, it, expect, before, bigString, inputvalues, Gpgmejs */
let verifyData = {
signedMessage: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
'Hash: SHA256\n' +
'\n' +
'Matschige Münsteraner Marshmallows\n' +
'-----BEGIN PGP SIGNATURE-----\n' +
'\n' +
'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
'=ioB6\n' +
'-----END PGP SIGNATURE-----\n'
};
describe('Verify data', function () {
describe('Verifying data', function () {
let context = null;
before(function(done){
const prm = Gpgmejs.init();
prm.then(function(gpgmejs){
context = gpgmejs;
done();
});
});
it('Successful verify message', function (done) { it('Successful verify message', function (done) {
let message = verifyData.signedMessage; const message = inputvalues.signedMessage.good;
let prm = Gpgmejs.init();
prm.then(function (context) {
context.verify(message).then(function(result){ context.verify(message).then(function(result){
expect(result.data).to.be.a('string'); expect(result.data).to.be.a('string');
expect(result.all_valid).to.be.true; expect(result.all_valid).to.be.true;
expect(result.count).to.equal(1); expect(result.count).to.equal(1);
expect(result.signatures.good).to.be.an('array'); expect(result.signatures.good).to.be.an('array');
expect(result.signatures.good.length).to.equal(1); expect(result.signatures.good.length).to.equal(1);
expect(result.signatures.good[0].fingerprint) expect(result.signatures.good[0].fingerprint).to.be.a('string');
.to.be.a('string');
expect(result.signatures.good[0].valid).to.be.true; expect(result.signatures.good[0].valid).to.be.true;
done(); done();
}); });
}); });
it('Successfully recognize changed cleartext', function (done) {
const message = inputvalues.signedMessage.bad;
context.verify(message).then(function(result){
expect(result.data).to.be.a('string');
expect(result.all_valid).to.be.false;
expect(result.count).to.equal(1);
expect(result.signatures.bad).to.be.an('array');
expect(result.signatures.bad.length).to.equal(1);
expect(result.signatures.bad[0].fingerprint).to.be.a('string');
expect(result.signatures.bad[0].valid).to.be.false;
done();
});
}); });
it('Encrypt-Sign-Verify random message', function (done) { it('Encrypt-Sign-Verify random message', function (done) {
let message = bigString(2000); const message = bigString(2000);
let fpr = inputvalues.encrypt.good.fingerprint; let fpr = inputvalues.encrypt.good.fingerprint;
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(message, fpr).then(function(message_enc){ context.encrypt(message, fpr).then(function(message_enc){
context.sign(message_enc.data, fpr).then(function(message_encsign){ context.sign(message_enc.data, fpr).then(function(message_encsign){
context.verify(message_encsign.data).then(function(result){ context.verify(message_encsign.data).then(function(result){
@ -74,8 +74,8 @@ describe('Verify data', function () {
expect(result.count).to.equal(1); expect(result.count).to.equal(1);
expect(result.signatures.good).to.be.an('array'); expect(result.signatures.good).to.be.an('array');
expect(result.signatures.good.length).to.equal(1); expect(result.signatures.good.length).to.equal(1);
expect(result.signatures.good[0].fingerprint) expect(
.to.equal(fpr); result.signatures.good[0].fingerprint).to.equal(fpr);
expect(result.signatures.good[0].valid).to.be.true; expect(result.signatures.good[0].valid).to.be.true;
done(); done();
}); });
@ -83,4 +83,3 @@ describe('Verify data', function () {
}); });
}); });
}); });
});