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/unit/crypto/MessageEncrypter_test.ts

149 lines
4.3 KiB
TypeScript

5 years ago
import { expect } from 'chai';
import * as crypto from 'crypto';
import * as sinon from 'sinon';
import { MessageEncrypter } from '../../../../session/crypto';
import { EncryptionType } from '../../../../session/types/EncryptionType';
import { Stubs, TestUtils } from '../../../test-utils';
import { UserUtil } from '../../../../util';
import { SignalService } from '../../../../protobuf';
5 years ago
import * as Ratchet from '../../../../session/medium_group/ratchet';
5 years ago
// tslint:disable-next-line: max-func-body-length
5 years ago
describe('MessageEncrypter', () => {
const sandbox = sinon.createSandbox();
5 years ago
const ourNumber = '0123456789abcdef';
5 years ago
beforeEach(() => {
TestUtils.stubWindow('libsignal', {
SignalProtocolAddress: sandbox.stub(),
5 years ago
SessionCipher: Stubs.SessionCipherStub,
} as any);
TestUtils.stubWindow('textsecure', {
storage: {
protocol: sandbox.stub(),
},
});
TestUtils.stubWindow('Signal', {
5 years ago
Metadata: {
SecretSessionCipher: Stubs.SecretSessionCipherStub,
},
});
TestUtils.stubWindow('libloki', {
5 years ago
crypto: {
FallBackSessionCipher: Stubs.FallBackSessionCipherStub,
5 years ago
encryptForPubkey: sinon.fake.returns(''),
} as any,
5 years ago
});
sandbox.stub(UserUtil, 'getCurrentDevicePubKey').resolves(ourNumber);
5 years ago
});
afterEach(() => {
sandbox.restore();
TestUtils.restoreStubs();
5 years ago
});
describe('EncryptionType', () => {
describe('MediumGroup', () => {
5 years ago
it('should return a MEDIUM_GROUP_CIPHERTEXT envelope type', async () => {
5 years ago
const data = crypto.randomBytes(10);
5 years ago
5 years ago
sandbox
.stub(Ratchet, 'encryptWithSenderKey')
.resolves({ ciphertext: '' });
5 years ago
const result = await MessageEncrypter.encrypt(
TestUtils.generateFakePubKey(),
5 years ago
data,
EncryptionType.MediumGroup
);
5 years ago
expect(result.envelopeType).to.deep.equal(
SignalService.Envelope.Type.MEDIUM_GROUP_CIPHERTEXT
5 years ago
);
5 years ago
});
});
describe('SessionRequest', () => {
5 years ago
it('should call FallbackSessionCipher encrypt', async () => {
const data = crypto.randomBytes(10);
const spy = sandbox.spy(
Stubs.FallBackSessionCipherStub.prototype,
'encrypt'
);
5 years ago
await MessageEncrypter.encrypt(
TestUtils.generateFakePubKey(),
data,
EncryptionType.Fallback
);
5 years ago
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'
);
5 years ago
await MessageEncrypter.encrypt(
TestUtils.generateFakePubKey(),
data,
EncryptionType.Fallback
);
5 years ago
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(
TestUtils.generateFakePubKey(),
5 years ago
data,
EncryptionType.Fallback
5 years ago
);
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.Fallback, EncryptionType.Signal];
5 years ago
for (const type of types) {
const spy = sandbox.spy(
Stubs.SecretSessionCipherStub.prototype,
'encrypt'
);
const user = TestUtils.generateFakePubKey();
await MessageEncrypter.encrypt(user, crypto.randomBytes(10), type);
5 years ago
const args = spy.args[0];
const [device, certificate] = args;
const expectedCertificate = SignalService.SenderCertificate.create({
sender: ourNumber,
senderDevice: 1,
});
expect(device).to.equal(user.key);
5 years ago
expect(certificate.toJSON()).to.deep.equal(
expectedCertificate.toJSON()
);
spy.restore();
}
5 years ago
});
});
});