You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/test/session/crypto/MessageEncrypter_test.ts

166 lines
5.1 KiB
TypeScript

5 years ago
import { expect } from 'chai';
5 years ago
import { ImportMock } from 'ts-mock-imports';
5 years ago
import * as crypto from 'crypto';
import * as sinon from 'sinon';
import * as window from '../../../window';
import { MessageEncrypter } from '../../../session/crypto';
import { EncryptionType } from '../../../session/types/EncryptionType';
5 years ago
import { Stubs } from '../../test-utils';
import { UserUtil } from '../../../util';
5 years ago
import { SignalService } from '../../../protobuf';
5 years ago
describe('MessageEncrypter', () => {
const sandbox = sinon.createSandbox();
5 years ago
const ourNumber = 'ourNumber';
5 years ago
beforeEach(() => {
ImportMock.mockOther(window, 'libsignal', {
SignalProtocolAddress: sandbox.stub(),
5 years ago
SessionCipher: Stubs.SessionCipherStub,
} as any);
ImportMock.mockOther(window, 'textsecure', {
storage: {
protocol: sandbox.stub(),
},
});
5 years ago
ImportMock.mockOther(window, 'Signal', {
Metadata: {
SecretSessionCipher: Stubs.SecretSessionCipherStub,
},
});
ImportMock.mockOther(window, 'libloki', {
crypto: {
FallBackSessionCipher: Stubs.FallBackSessionCipherStub,
},
});
ImportMock.mockFunction(UserUtil, 'getCurrentDevicePubKey', ourNumber);
5 years ago
});
afterEach(() => {
sandbox.restore();
ImportMock.restore();
5 years ago
});
describe('EncryptionType', () => {
describe('MediumGroup', () => {
it('should throw an error', async () => {
const data = crypto.randomBytes(10);
5 years ago
const promise = MessageEncrypter.encrypt(
'1',
data,
EncryptionType.MediumGroup
);
await expect(promise).to.be.rejectedWith(
'Encryption is not yet supported'
);
5 years ago
});
});
describe('SessionReset', () => {
5 years ago
it('should call FallbackSessionCipher encrypt', async () => {
const data = crypto.randomBytes(10);
const spy = sandbox.spy(
Stubs.FallBackSessionCipherStub.prototype,
'encrypt'
);
await MessageEncrypter.encrypt('1', data, EncryptionType.SessionReset);
expect(spy.called).to.equal(
true,
'FallbackSessionCipher.encrypt should be called.'
);
5 years ago
});
it('should pass the padded message body to encrypt', async () => {
5 years ago
const data = crypto.randomBytes(10);
const spy = sandbox.spy(
Stubs.FallBackSessionCipherStub.prototype,
'encrypt'
);
await MessageEncrypter.encrypt('1', data, EncryptionType.SessionReset);
const paddedData = MessageEncrypter.padPlainTextBuffer(data);
const firstArgument = new Uint8Array(spy.args[0][0]);
expect(firstArgument).to.deep.equal(paddedData);
});
it('should return an UNIDENTIFIED SENDER envelope type', async () => {
const data = crypto.randomBytes(10);
const result = await MessageEncrypter.encrypt(
'1',
data,
EncryptionType.SessionReset
);
expect(result.envelopeType).to.deep.equal(
SignalService.Envelope.Type.UNIDENTIFIED_SENDER
);
5 years ago
});
});
5 years ago
5 years ago
describe('Signal', () => {
it('should call SessionCipher encrypt', async () => {
const data = crypto.randomBytes(10);
5 years ago
const spy = sandbox.spy(Stubs.SessionCipherStub.prototype, 'encrypt');
await MessageEncrypter.encrypt('1', data, EncryptionType.Signal);
5 years ago
expect(spy.called).to.equal(
5 years ago
true,
'SessionCipher.encrypt should be called.'
);
5 years ago
});
5 years ago
it('should pass the padded message body to encrypt', async () => {
const data = crypto.randomBytes(10);
const spy = sandbox.spy(Stubs.SessionCipherStub.prototype, 'encrypt');
await MessageEncrypter.encrypt('1', data, EncryptionType.Signal);
const paddedData = MessageEncrypter.padPlainTextBuffer(data);
const firstArgument = new Uint8Array(spy.args[0][0]);
expect(firstArgument).to.deep.equal(paddedData);
});
it('should return an UNIDENTIFIED SENDER envelope type', async () => {
const data = crypto.randomBytes(10);
const result = await MessageEncrypter.encrypt(
'1',
data,
EncryptionType.Signal
);
expect(result.envelopeType).to.deep.equal(
SignalService.Envelope.Type.UNIDENTIFIED_SENDER
);
});
});
});
describe('Sealed Sender', () => {
it('should pass the correct values to SecretSessionCipher encrypt', async () => {
const types = [EncryptionType.SessionReset, EncryptionType.Signal];
for (const type of types) {
const spy = sandbox.spy(
Stubs.SecretSessionCipherStub.prototype,
'encrypt'
);
await MessageEncrypter.encrypt('user', crypto.randomBytes(10), type);
const args = spy.args[0];
const [device, certificate] = args;
const expectedCertificate = SignalService.SenderCertificate.create({
sender: ourNumber,
senderDevice: 1,
});
expect(device).to.equal('user');
expect(certificate.toJSON()).to.deep.equal(
expectedCertificate.toJSON()
);
spy.restore();
}
5 years ago
});
});
});