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

78 lines
2.2 KiB
TypeScript

5 years ago
import { expect } from 'chai';
import { ImportMock, MockManager } 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
describe('MessageEncrypter', () => {
const sandbox = sinon.createSandbox();
5 years ago
5 years ago
let sessionCipherStub: MockManager<Stubs.SessionCipherBasicStub>;
5 years ago
beforeEach(() => {
5 years ago
sessionCipherStub = ImportMock.mockClass(Stubs, 'SessionCipherBasicStub');
ImportMock.mockOther(window, 'libsignal', {
SignalProtocolAddress: sandbox.stub(),
5 years ago
SessionCipher: Stubs.SessionCipherBasicStub,
} as any);
ImportMock.mockOther(window, 'textsecure', {
storage: {
protocol: sandbox.stub(),
},
});
5 years ago
ImportMock.mockFunction(UserUtil, 'getCurrentDevicePubKey', '1');
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', () => {
it('should call FallbackSessionCipher', async () => {
});
it('should pass the padded message body to encrypt', async () => {
});
});
*/
5 years ago
describe('Signal', () => {
it('should call SessionCipher encrypt', async () => {
const data = crypto.randomBytes(10);
const stub = sessionCipherStub.mock('encrypt').resolves({
type: 1,
body: 'body',
});
await MessageEncrypter.encrypt('1', data, EncryptionType.Signal);
5 years ago
expect(stub.called).to.equal(
true,
'SessionCipher.encrypt should be called.'
);
5 years ago
});
5 years ago
it('should pass the padded message body to encrypt', async () => {});
5 years ago
});
});
});