signal-desktop/ts/test-electron/util/encryptProfileData_test.ts

114 lines
3 KiB
TypeScript
Raw Normal View History

2021-07-19 19:26:06 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
2021-09-24 00:49:05 +00:00
import * as Bytes from '../../Bytes';
2021-07-19 19:26:06 +00:00
import {
trimForDisplay,
2021-09-24 00:49:05 +00:00
getRandomBytes,
decryptProfileName,
decryptProfile,
2021-07-19 19:26:06 +00:00
} from '../../Crypto';
import type { ConversationType } from '../../state/ducks/conversations';
2021-10-26 22:59:08 +00:00
import { UUID } from '../../types/UUID';
2021-07-19 19:26:06 +00:00
import { encryptProfileData } from '../../util/encryptProfileData';
describe('encryptProfileData', () => {
let keyBuffer: Uint8Array;
let conversation: ConversationType;
beforeEach(() => {
keyBuffer = getRandomBytes(32);
conversation = {
2021-07-19 19:26:06 +00:00
aboutEmoji: '🐢',
aboutText: 'I like turtles',
familyName: 'Kid',
firstName: 'Zombie',
2021-09-24 00:49:05 +00:00
profileKey: Bytes.toBase64(keyBuffer),
2021-10-26 22:59:08 +00:00
uuid: UUID.generate().toString(),
2021-07-19 19:26:06 +00:00
// To satisfy TS
acceptedMessageRequest: true,
2021-11-02 23:01:13 +00:00
badges: [],
2021-07-19 19:26:06 +00:00
id: '',
isMe: true,
sharedGroupNames: [],
title: '',
type: 'direct' as const,
};
});
2021-07-19 19:26:06 +00:00
it('encrypts and decrypts properly', async () => {
const [encrypted] = await encryptProfileData(conversation, {
oldAvatar: undefined,
newAvatar: undefined,
});
2021-07-19 19:26:06 +00:00
assert.isDefined(encrypted.version);
assert.isDefined(encrypted.name);
assert.isDefined(encrypted.commitment);
2021-09-24 00:49:05 +00:00
const decryptedProfileNameBytes = decryptProfileName(
2021-07-19 19:26:06 +00:00
encrypted.name,
keyBuffer
);
assert.equal(
2021-09-24 00:49:05 +00:00
Bytes.toString(decryptedProfileNameBytes.given),
2021-07-19 19:26:06 +00:00
conversation.firstName
);
if (decryptedProfileNameBytes.family) {
assert.equal(
2021-09-24 00:49:05 +00:00
Bytes.toString(decryptedProfileNameBytes.family),
2021-07-19 19:26:06 +00:00
conversation.familyName
);
} else {
assert.isDefined(decryptedProfileNameBytes.family);
}
if (encrypted.about) {
2021-09-24 00:49:05 +00:00
const decryptedAboutBytes = decryptProfile(
Bytes.fromBase64(encrypted.about),
2021-07-19 19:26:06 +00:00
keyBuffer
);
assert.equal(
2021-09-24 00:49:05 +00:00
Bytes.toString(trimForDisplay(decryptedAboutBytes)),
2021-07-19 19:26:06 +00:00
conversation.aboutText
);
} else {
assert.isDefined(encrypted.about);
}
if (encrypted.aboutEmoji) {
2021-09-24 00:49:05 +00:00
const decryptedAboutEmojiBytes = await decryptProfile(
Bytes.fromBase64(encrypted.aboutEmoji),
2021-07-19 19:26:06 +00:00
keyBuffer
);
assert.equal(
2021-09-24 00:49:05 +00:00
Bytes.toString(trimForDisplay(decryptedAboutEmojiBytes)),
2021-07-19 19:26:06 +00:00
conversation.aboutEmoji
);
} else {
assert.isDefined(encrypted.aboutEmoji);
}
});
it('sets sameAvatar to true when avatars are the same', async () => {
const [encrypted] = await encryptProfileData(conversation, {
oldAvatar: new Uint8Array([1, 2, 3]),
newAvatar: new Uint8Array([1, 2, 3]),
});
assert.isTrue(encrypted.sameAvatar);
});
it('sets sameAvatar to false when avatars are different', async () => {
const [encrypted] = await encryptProfileData(conversation, {
oldAvatar: new Uint8Array([1, 2, 3]),
newAvatar: new Uint8Array([4, 5, 6, 7]),
});
assert.isFalse(encrypted.sameAvatar);
});
2021-07-19 19:26:06 +00:00
});