Skip to content

Commit

Permalink
Add testsuite for decryptWithSharedSecret
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasALLOIN committed Jun 2, 2022
1 parent a13790b commit fc62c88
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions src/encryption.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
decrypt,
decryptSafely,
decryptSafely, decryptWithSharedSecret,
encrypt,
encryptSafely,
encryptSafely, EthEncryptedData,
getEncryptionPublicKey,
} from './encryption';

Expand Down Expand Up @@ -73,6 +73,86 @@ describe('encryption', function () {
expect(plaintext).toBe(secretMessage);
});

// shared secret decryption test
it('bob decrypts message that Alice sent to him with his shared secret', function () {
const encryptedDataWithHW: EthEncryptedData = {
version: 'x25519-xsalsa20-poly1305',
nonce: 'QjBaLWLlYeIDUcMjqUpDkIxoaBIck/lh',
ephemPublicKey: '/uwH3xIXDBG8ARritky4dSh9DXFNo1Jw2lSgq+Prdx0=',
ciphertext: 'pT7dEopOHWZgFQZ0cK2ia/9Ewz03xq6db/vU8glwg+deI4WiyP2lTY0s',
};
const sharedSecret = 'Pplxc07fb6IiXAmtDJ9ebL4KRGXF9qeic0ZmktOdHCk=';

const result = decryptWithSharedSecret({
encryptedData: encryptedDataWithHW,
sharedSecret,
});
expect(result).toBe(secretMessage);
});

it('bob decrypts invalid message that Alice sent to him with his shared secret', function () {
const encryptedDataWithHW: EthEncryptedData = {
version: 'x25519-xsalsa20-poly1305',
nonce: 'QjBaLWLlYeIDUcMjqUpDkIxoaBIck/lh',
ephemPublicKey: '/uwH3xIXDBG8ARritky4dSh9DXFNo1Jw2lSgq+Prdx0=',
ciphertext: 'pT7dEopOHWZgFQZ0cK2ia/9Ewz03xq6db/vU8glgg+deI4WiyP2lTY0s',
};
const sharedSecret = 'Pplxc07fb6IiXAmtDJ9ebL4KRGXF9qeic0ZmktOdHCk=';

expect(() =>
decryptWithSharedSecret({
encryptedData: encryptedDataWithHW,
sharedSecret,
}),
).toThrow('Decryption failed.');
});

it('bob decrypts unknown version message that Alice sent to him with his shared secret', function () {
const encryptedDataWithHW: EthEncryptedData = {
version: 'x25519-xsalsa21-poly1305',
nonce: 'QjBaLWLlYeIDUcMjqUpDkIxoaBIck/lh',
ephemPublicKey: '/uwH3xIXDBG8ARritky4dSh9DXFNo1Jw2lSgq+Prdx0=',
ciphertext: 'pT7dEopOHWZgFQZ0cK2ia/9Ewz03xq6db/vU8glgg+deI4WiyP2lTY0s',
};
const sharedSecret = 'Pplxc07fb6IiXAmtDJ9ebL4KRGXF9qeic0ZmktOdHCk=';

expect(() =>
decryptWithSharedSecret({
encryptedData: encryptedDataWithHW,
sharedSecret,
}),
).toThrow('Encryption type/version not supported.');
});

it('bob decrypts null encrypted that Alice sent to him with his shared secret', function () {
const encryptedDataWithHW: EthEncryptedData = null;
const sharedSecret = 'Pplxc07fb6IiXAmtDJ9ebL4KRGXF9qeic0ZmktOdHCk=';

expect(() =>
decryptWithSharedSecret({
encryptedData: encryptedDataWithHW,
sharedSecret,
}),
).toThrow('Missing encryptedData parameter');
});

it('bob decrypts message that Alice sent to him with null shared secret', function () {
const encryptedDataWithHW: EthEncryptedData = {
version: 'x25519-xsalsa21-poly1305',
nonce: 'QjBaLWLlYeIDUcMjqUpDkIxoaBIck/lh',
ephemPublicKey: '/uwH3xIXDBG8ARritky4dSh9DXFNo1Jw2lSgq+Prdx0=',
ciphertext: 'pT7dEopOHWZgFQZ0cK2ia/9Ewz03xq6db/vU8glgg+deI4WiyP2lTY0s',
};
const sharedSecret = null;

expect(() =>
decryptWithSharedSecret({
encryptedData: encryptedDataWithHW,
sharedSecret,
}),
).toThrow('Missing sharedSecret parameter');
});

// decryption test
it('bob decrypts message that Alice sent to him', function () {
const result = decrypt({
Expand Down

0 comments on commit fc62c88

Please sign in to comment.