Fix unit tests for browsers

This commit is contained in:
Tankred Hase 2016-03-23 15:39:51 +08:00
parent e5e76d2eb7
commit 963648c6ea
4 changed files with 22 additions and 17 deletions

View File

@ -107,8 +107,7 @@ Message.prototype.decrypt = function(privateKey, sessionKey, password) {
var symEncryptedPacket = symEncryptedPacketlist[0];
return symEncryptedPacket.decrypt(keyObj.algorithm, keyObj.data).then(() => {
var resultMsg = new Message(symEncryptedPacket.packets);
// remove packets after decryption
symEncryptedPacket.packets = new packet.List();
symEncryptedPacket.packets = new packet.List(); // remove packets after decryption
return resultMsg;
});
}
@ -224,7 +223,6 @@ Message.prototype.encrypt = function(keys, passwords) {
var sessionKey = crypto.generateSessionKey(enums.read(enums.symmetric, symAlgo));
var msg = encryptSessionKey(sessionKey, enums.read(enums.symmetric, symAlgo), keys, passwords);
var packetlist = msg.packets;
var symEncryptedPacket;
if (config.aead_protect) {
@ -237,9 +235,8 @@ Message.prototype.encrypt = function(keys, passwords) {
symEncryptedPacket.packets = this.packets;
return symEncryptedPacket.encrypt(enums.read(enums.symmetric, symAlgo), sessionKey).then(() => {
packetlist.push(symEncryptedPacket);
// remove packets after encryption
symEncryptedPacket.packets = new packet.List();
msg.packets.push(symEncryptedPacket);
symEncryptedPacket.packets = new packet.List(); // remove packets after encryption
return msg;
});
};

View File

@ -659,10 +659,14 @@ var pgp_desktop_priv =
it('Generated key is not unlocked by default', function(done) {
var opt = {numBits: 512, userIds: 'test <a@b.com>', passphrase: '123'};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKey(opt).then(function(key) {
var msg = openpgp.message.fromText('hello').encrypt([key.key]);
msg = msg.decrypt.bind(msg, key.key);
expect(msg).to.throw('Private key is not decrypted.');
var key;
openpgp.generateKey(opt).then(function(newKey) {
key = newKey;
return openpgp.message.fromText('hello').encrypt([key.key]);
}).then(function(msg) {
return msg.decrypt(key.key);
}).catch(function(err) {
expect(err.message).to.equal('Private key is not decrypted.');
done();
});
});

View File

@ -422,7 +422,7 @@ describe('OpenPGP.js public api tests', function() {
});
describe('encrypt, decrypt, sign, verify - integration tests', function() {
var privateKey, publicKey, zeroCopyVal;
var privateKey, publicKey, zeroCopyVal, useNativeVal;
beforeEach(function() {
publicKey = openpgp.key.readArmored(pub_key);
@ -432,10 +432,12 @@ describe('OpenPGP.js public api tests', function() {
expect(privateKey.keys).to.have.length(1);
expect(privateKey.err).to.not.exist;
zeroCopyVal = openpgp.config.zeroCopy;
useNativeVal = openpgp.config.useNative;
});
afterEach(function() {
openpgp.config.zeroCopy = zeroCopyVal;
openpgp.config.useNative = useNativeVal;
});
it('Decrypting key with wrong passphrase returns false', function () {
@ -876,6 +878,7 @@ describe('OpenPGP.js public api tests', function() {
it('should encrypt and decrypt with binary data and transferable objects', function(done) {
openpgp.config.zeroCopy = true; // activate transferable objects
openpgp.config.useNative = false; // use asm.js fallback with web worker, not native crypto
var encOpt = {
data: new Uint8Array([0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01]),
passwords: password1,

View File

@ -304,12 +304,13 @@ describe("Signature", function() {
var msg = openpgp.message.readArmored(msg_arm1);
priv_key_gnupg_ext.subKeys[0].subKey.decrypt("abcd");
msg = msg.decrypt(priv_key_gnupg_ext);
var verified = msg.verify([pub_key]);
expect(verified).to.exist;
expect(verified).to.have.length(1);
expect(verified[0].valid).to.be.true;
done();
msg.decrypt(priv_key_gnupg_ext).then(function(msg) {
var verified = msg.verify([pub_key]);
expect(verified).to.exist;
expect(verified).to.have.length(1);
expect(verified[0].valid).to.be.true;
done();
});
});
it('Verify V4 signature. Hash: SHA1. PK: RSA. Signature Type: 0x00 (binary document)', function(done) {