Unit tests for web worker. Only active for browsers.

This commit is contained in:
Thomas Oberndörfer 2014-01-13 15:42:03 +01:00
parent 3612fc12dc
commit 17c6f90113
6 changed files with 160 additions and 10 deletions

View File

@ -23,6 +23,11 @@ module.exports = function(grunt) {
external: [ 'crypto', 'node-localstorage' ]
}
},
worker: {
files: {
'dist/openpgp.worker.js': [ './src/worker/worker.js' ]
}
},
unittests: {
files: {
'test/lib/unittests-bundle.js': []

View File

@ -28,9 +28,10 @@ function AsyncProxy(path) {
}
AsyncProxy.prototype.onMessage = function(event) {
switch (event.data.event) {
var msg = event.data;
switch (msg.event) {
case 'method-return':
this.tasks.shift()(event.data.err, event.data.data);
this.tasks.shift()(msg.err ? new Error(msg.err) : null, msg.data);
break;
default:
throw new Error('Unknown Worker Event.');

View File

@ -15,7 +15,7 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var window = {}; // to make UMD bundles work
window = {}; // to make UMD bundles work
importScripts('openpgp.js');
@ -33,7 +33,7 @@ onmessage = function (event) {
msg.keys = msg.keys.map(packetlistCloneToKey);
data = window.openpgp.encryptMessage(msg.keys, msg.text);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -43,7 +43,7 @@ onmessage = function (event) {
msg.privateKey = packetlistCloneToKey(msg.privateKey);
data = window.openpgp.signAndEncryptMessage(msg.publicKeys, msg.privateKey, msg.text);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -53,7 +53,7 @@ onmessage = function (event) {
msg.message = packetlistCloneToMessage(msg.message.packets);
data = window.openpgp.decryptMessage(msg.privateKey, msg.message);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -64,7 +64,7 @@ onmessage = function (event) {
msg.message = packetlistCloneToMessage(msg.message.packets);
data = window.openpgp.decryptAndVerifyMessage(msg.privateKey, msg.publicKeys, msg.message);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -73,7 +73,7 @@ onmessage = function (event) {
msg.privateKeys = msg.privateKeys.map(packetlistCloneToKey);
data = window.openpgp.signClearMessage(msg.privateKeys, msg.text);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -84,7 +84,7 @@ onmessage = function (event) {
msg.message = new window.openpgp.cleartext.CleartextMessage(msg.message.text, packetlist);
data = window.openpgp.verifyClearSignedMessage(msg.publicKeys, msg.message);
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;
@ -93,7 +93,7 @@ onmessage = function (event) {
data = window.openpgp.generateKeyPair(msg.keyType, msg.numBits, msg.userId, msg.passphrase);
data.key = data.key.toPacketlist();
} catch (e) {
err = e;
err = e.message;
}
postMessage({event: 'method-return', data: data, err: err});
break;

View File

@ -1,4 +1,7 @@
describe('Unit Tests', function () {
require('./general');
require('./crypto');
if (typeof window !== 'undefined') {
require('./worker');
}
});

137
test/worker/api.js Normal file
View File

@ -0,0 +1,137 @@
'use strict';
var openpgp = typeof window != 'undefined' && window.openpgp ? window.openpgp : require('../../src/index');
var chai = require('chai'),
expect = chai.expect;
describe('High level API', function() {
var proxy;
this.timeout(50000);
var pub_key =
['-----BEGIN PGP PUBLIC KEY BLOCK-----',
'Version: GnuPG v2.0.19 (GNU/Linux)',
'Type: RSA/RSA',
'',
'mI0EUmEvTgEEANyWtQQMOybQ9JltDqmaX0WnNPJeLILIM36sw6zL0nfTQ5zXSS3+',
'fIF6P29lJFxpblWk02PSID5zX/DYU9/zjM2xPO8Oa4xo0cVTOTLj++Ri5mtr//f5',
'GLsIXxFrBJhD/ghFsL3Op0GXOeLJ9A5bsOn8th7x6JucNKuaRB6bQbSPABEBAAG0',
'JFRlc3QgTWNUZXN0aW5ndG9uIDx0ZXN0QGV4YW1wbGUuY29tPoi5BBMBAgAjBQJS',
'YS9OAhsvBwsJCAcDAgEGFQgCCQoLBBYCAwECHgECF4AACgkQSmNhOk1uQJQwDAP6',
'AgrTyqkRlJVqz2pb46TfbDM2TDF7o9CBnBzIGoxBhlRwpqALz7z2kxBDmwpQa+ki',
'Bq3jZN/UosY9y8bhwMAlnrDY9jP1gdCo+H0sD48CdXybblNwaYpwqC8VSpDdTndf',
'9j2wE/weihGp/DAdy/2kyBCaiOY1sjhUfJ1GogF49rC4jQRSYS9OAQQA6R/PtBFa',
'JaT4jq10yqASk4sqwVMsc6HcifM5lSdxzExFP74naUMMyEsKHP53QxTF0Grqusag',
'Qg/ZtgT0CN1HUM152y7ACOdp1giKjpMzOTQClqCoclyvWOFB+L/SwGEIJf7LSCEr',
'woBuJifJc8xAVr0XX0JthoW+uP91eTQ3XpsAEQEAAYkBPQQYAQIACQUCUmEvTgIb',
'LgCoCRBKY2E6TW5AlJ0gBBkBAgAGBQJSYS9OAAoJEOCE90RsICyXuqIEANmmiRCA',
'SF7YK7PvFkieJNwzeK0V3F2lGX+uu6Y3Q/Zxdtwc4xR+me/CSBmsURyXTO29OWhP',
'GLszPH9zSJU9BdDi6v0yNprmFPX/1Ng0Abn/sCkwetvjxC1YIvTLFwtUL/7v6NS2',
'bZpsUxRTg9+cSrMWWSNjiY9qUKajm1tuzPDZXAUEAMNmAN3xXN/Kjyvj2OK2ck0X',
'W748sl/tc3qiKPMJ+0AkMF7Pjhmh9nxqE9+QCEl7qinFqqBLjuzgUhBU4QlwX1GD',
'AtNTq6ihLMD5v1d82ZC7tNatdlDMGWnIdvEMCv2GZcuIqDQ9rXWs49e7tq1NncLY',
'hz3tYjKhoFTKEIq3y3Pp',
'=h/aX',
'-----END PGP PUBLIC KEY BLOCK-----'].join('\n');
var priv_key =
['-----BEGIN PGP PRIVATE KEY BLOCK-----',
'Version: GnuPG v2.0.19 (GNU/Linux)',
'Type: RSA/RSA',
'Pwd: hello world',
'',
'lQH+BFJhL04BBADclrUEDDsm0PSZbQ6pml9FpzTyXiyCyDN+rMOsy9J300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-----END PGP PRIVATE KEY BLOCK-----'].join('\n');
var plaintext = 'short message\nnext line\n한국어/조선말';
var pubKey, privKey;
it('Test initialization', function (done) {
pubKey = openpgp.key.readArmored(pub_key).keys[0];
expect(pubKey).to.exist;
privKey = openpgp.key.readArmored(priv_key).keys[0];
expect(privKey).to.exist;
done();
});
it('Initialize AsyncProxy', function (done) {
proxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js');
expect(proxy).to.exist;
done();
});
it('Generate key', function (done) {
proxy.generateKeyPair(3, 1024, 'Test McTestington <test@example.com>', 'hello world', function(err, data) {
expect(err).to.not.exist;
expect(data).to.exist;
expect(data.publicKeyArmored).to.match(/^-----BEGIN PGP PUBLIC/);
expect(data.privateKeyArmored).to.match(/^-----BEGIN PGP PRIVATE/);
expect(data.key).to.be.an.instanceof(openpgp.key.Key);
done();
});
});
it('Encrypt and decrypt text', function (done) {
proxy.encryptMessage([pubKey], plaintext, function(err, data) {
expect(err).to.not.exist;
expect(data).to.exist;
expect(data).to.match(/^-----BEGIN PGP MESSAGE/);
var msg = openpgp.message.readArmored(data);
expect(msg).to.be.an.instanceof(openpgp.message.Message);
proxy.decryptMessage(privKey, msg, function(err, data) {
expect(data).to.not.exist;
expect(err).to.exist;
expect(err.message).to.equal('Private key is not decrypted.');
privKey.decrypt('hello world');
proxy.decryptMessage(privKey, msg, function(err, data) {
expect(err).to.not.exist;
expect(data).to.exist;
expect(data).to.equal(plaintext);
done();
});
});
});
});
});

4
test/worker/index.js Normal file
View File

@ -0,0 +1,4 @@
describe('Web Worker', function () {
require('./api.js');
});