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

86 lines
2.2 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';
import { v4 as uuid } from 'uuid';
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 { encryptProfileData } from '../../util/encryptProfileData';
describe('encryptProfileData', () => {
it('encrypts and decrypts properly', async () => {
2021-09-24 00:49:05 +00:00
const keyBuffer = getRandomBytes(32);
2021-07-19 19:26:06 +00:00
const conversation = {
aboutEmoji: '🐢',
aboutText: 'I like turtles',
familyName: 'Kid',
firstName: 'Zombie',
2021-09-24 00:49:05 +00:00
profileKey: Bytes.toBase64(keyBuffer),
2021-07-19 19:26:06 +00:00
uuid: uuid(),
// To satisfy TS
acceptedMessageRequest: true,
id: '',
isMe: true,
sharedGroupNames: [],
title: '',
type: 'direct' as const,
};
const [encrypted] = await encryptProfileData(conversation);
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);
}
});
});