2022-08-15 21:53:33 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import {
|
|
|
|
UUIDKind,
|
|
|
|
Proto,
|
|
|
|
ReceiptType,
|
|
|
|
StorageState,
|
|
|
|
} from '@signalapp/mock-server';
|
|
|
|
import type { PrimaryDevice } from '@signalapp/mock-server';
|
|
|
|
import createDebug from 'debug';
|
|
|
|
|
|
|
|
import * as durations from '../../util/durations';
|
|
|
|
import { uuidToBytes } from '../../util/uuidToBytes';
|
2022-11-10 15:51:39 +00:00
|
|
|
import { MY_STORY_ID } from '../../types/Stories';
|
2022-08-15 21:53:33 +00:00
|
|
|
import { Bootstrap } from '../bootstrap';
|
|
|
|
import type { App } from '../bootstrap';
|
|
|
|
|
|
|
|
export const debug = createDebug('mock:test:pni-signature');
|
|
|
|
|
|
|
|
const IdentifierType = Proto.ManifestRecord.Identifier.Type;
|
|
|
|
|
2022-09-21 16:18:48 +00:00
|
|
|
describe('pnp/PNI Signature', function needsName() {
|
2022-08-15 21:53:33 +00:00
|
|
|
this.timeout(durations.MINUTE);
|
|
|
|
|
|
|
|
let bootstrap: Bootstrap;
|
|
|
|
let app: App;
|
|
|
|
let pniContact: PrimaryDevice;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
bootstrap = new Bootstrap();
|
|
|
|
await bootstrap.init();
|
|
|
|
|
|
|
|
const { server, phone } = bootstrap;
|
|
|
|
|
|
|
|
pniContact = await server.createPrimaryDevice({
|
|
|
|
profileName: 'ACI Contact',
|
|
|
|
});
|
|
|
|
|
|
|
|
let state = StorageState.getEmpty();
|
|
|
|
|
|
|
|
state = state.updateAccount({
|
|
|
|
profileKey: phone.profileKey.serialize(),
|
|
|
|
e164: phone.device.number,
|
|
|
|
});
|
|
|
|
|
|
|
|
state = state.addContact(
|
|
|
|
pniContact,
|
|
|
|
{
|
|
|
|
whitelisted: true,
|
|
|
|
serviceE164: pniContact.device.number,
|
2023-02-08 01:55:07 +00:00
|
|
|
identityKey: pniContact.getPublicKey(UUIDKind.PNI).serialize(),
|
2022-08-15 21:53:33 +00:00
|
|
|
givenName: 'PNI Contact',
|
|
|
|
},
|
|
|
|
UUIDKind.PNI
|
|
|
|
);
|
|
|
|
|
|
|
|
state = state.addContact(pniContact, {
|
|
|
|
whitelisted: true,
|
|
|
|
serviceE164: undefined,
|
|
|
|
identityKey: pniContact.publicKey.serialize(),
|
|
|
|
profileKey: pniContact.profileKey.serialize(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Just to make PNI Contact visible in the left pane
|
|
|
|
state = state.pin(pniContact, UUIDKind.PNI);
|
|
|
|
|
|
|
|
// Add my story
|
|
|
|
state = state.addRecord({
|
|
|
|
type: IdentifierType.STORY_DISTRIBUTION_LIST,
|
|
|
|
record: {
|
|
|
|
storyDistributionList: {
|
|
|
|
allowsReplies: true,
|
2022-11-10 15:51:39 +00:00
|
|
|
identifier: uuidToBytes(MY_STORY_ID),
|
2022-08-15 21:53:33 +00:00
|
|
|
isBlockList: true,
|
2022-11-10 15:51:39 +00:00
|
|
|
name: MY_STORY_ID,
|
2022-08-15 21:53:33 +00:00
|
|
|
recipientUuids: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await phone.setStorageState(state);
|
|
|
|
|
|
|
|
app = await bootstrap.link();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function after() {
|
|
|
|
if (this.currentTest?.state !== 'passed') {
|
2022-09-01 16:35:44 +00:00
|
|
|
await bootstrap.saveLogs(app);
|
2022-08-15 21:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await app.close();
|
|
|
|
await bootstrap.teardown();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be sent by Desktop until encrypted delivery receipt', async () => {
|
|
|
|
const { server, desktop } = bootstrap;
|
|
|
|
|
|
|
|
const ourPNIKey = await desktop.getIdentityKey(UUIDKind.PNI);
|
|
|
|
const ourACIKey = await desktop.getIdentityKey(UUIDKind.ACI);
|
|
|
|
|
|
|
|
const window = await app.getWindow();
|
|
|
|
|
|
|
|
const leftPane = window.locator('.left-pane-wrapper');
|
|
|
|
const conversationStack = window.locator('.conversation-stack');
|
|
|
|
|
|
|
|
debug('creating a stranger');
|
|
|
|
const stranger = await server.createPrimaryDevice({
|
|
|
|
profileName: 'Mysterious Stranger',
|
|
|
|
});
|
|
|
|
|
|
|
|
const ourKey = await desktop.popSingleUseKey(UUIDKind.PNI);
|
|
|
|
await stranger.addSingleUseKey(desktop, ourKey, UUIDKind.PNI);
|
|
|
|
|
|
|
|
const checkPniSignature = (
|
|
|
|
message: Proto.IPniSignatureMessage | null | undefined,
|
|
|
|
source: string
|
|
|
|
) => {
|
|
|
|
if (!message) {
|
|
|
|
throw new Error(
|
|
|
|
`Missing expected pni signature message from ${source}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
message.pni,
|
|
|
|
uuidToBytes(desktop.pni),
|
|
|
|
`Incorrect pni in pni signature message from ${source}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const isValid = ourPNIKey.verifyAlternateIdentity(
|
|
|
|
ourACIKey,
|
|
|
|
Buffer.from(message.signature ?? [])
|
|
|
|
);
|
|
|
|
assert.isTrue(isValid, `Invalid pni signature from ${source}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
debug('sending a message to our PNI');
|
|
|
|
await stranger.sendText(desktop, 'A message to PNI', {
|
|
|
|
uuidKind: UUIDKind.PNI,
|
|
|
|
withProfileKey: true,
|
|
|
|
timestamp: bootstrap.getTimestamp(),
|
|
|
|
});
|
|
|
|
|
|
|
|
debug('opening conversation with the stranger');
|
|
|
|
await leftPane
|
2023-01-13 00:24:59 +00:00
|
|
|
.locator(`[data-testid="${stranger.toContact().uuid}"]`)
|
2022-08-15 21:53:33 +00:00
|
|
|
.click();
|
|
|
|
|
|
|
|
debug('Accept conversation from a stranger');
|
|
|
|
await conversationStack
|
|
|
|
.locator('.module-message-request-actions button >> "Accept"')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
debug('Waiting for a pniSignatureMessage');
|
|
|
|
{
|
|
|
|
const { source, content } = await stranger.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(source, desktop, 'initial message has valid source');
|
|
|
|
checkPniSignature(content.pniSignatureMessage, 'initial message');
|
|
|
|
}
|
|
|
|
debug('Enter first message text');
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
await compositionInput.type('first');
|
|
|
|
await compositionInput.press('Enter');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
debug('Waiting for the first message with pni signature');
|
|
|
|
{
|
|
|
|
const { source, content, body, dataMessage } =
|
|
|
|
await stranger.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
source,
|
|
|
|
desktop,
|
|
|
|
'first message must have valid source'
|
|
|
|
);
|
|
|
|
assert.strictEqual(body, 'first', 'first message must have valid body');
|
|
|
|
checkPniSignature(content.pniSignatureMessage, 'first message');
|
|
|
|
|
|
|
|
const receiptTimestamp = bootstrap.getTimestamp();
|
|
|
|
debug('Sending unencrypted receipt', receiptTimestamp);
|
|
|
|
|
|
|
|
await stranger.sendUnencryptedReceipt(desktop, {
|
|
|
|
messageTimestamp: dataMessage.timestamp?.toNumber() ?? 0,
|
|
|
|
timestamp: receiptTimestamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
debug('Enter second message text');
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
await compositionInput.type('second');
|
|
|
|
await compositionInput.press('Enter');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
debug('Waiting for the second message with pni signature');
|
|
|
|
{
|
|
|
|
const { source, content, body, dataMessage } =
|
|
|
|
await stranger.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
source,
|
|
|
|
desktop,
|
|
|
|
'second message must have valid source'
|
|
|
|
);
|
|
|
|
assert.strictEqual(body, 'second', 'second message must have valid body');
|
|
|
|
checkPniSignature(content.pniSignatureMessage, 'second message');
|
|
|
|
|
|
|
|
const receiptTimestamp = bootstrap.getTimestamp();
|
|
|
|
debug('Sending encrypted receipt', receiptTimestamp);
|
|
|
|
|
|
|
|
await stranger.sendReceipt(desktop, {
|
|
|
|
type: ReceiptType.Delivery,
|
|
|
|
messageTimestamps: [dataMessage.timestamp?.toNumber() ?? 0],
|
|
|
|
timestamp: receiptTimestamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
debug('Enter third message text');
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
await compositionInput.type('third');
|
|
|
|
await compositionInput.press('Enter');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
debug('Waiting for the third message without pni signature');
|
|
|
|
{
|
|
|
|
const { source, content, body } = await stranger.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
source,
|
|
|
|
desktop,
|
|
|
|
'third message must have valid source'
|
|
|
|
);
|
|
|
|
assert.strictEqual(body, 'third', 'third message must have valid body');
|
|
|
|
assert(
|
|
|
|
!content.pniSignatureMessage,
|
|
|
|
'third message must not have pni signature message'
|
|
|
|
);
|
|
|
|
}
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
debug('Verify final state');
|
|
|
|
{
|
|
|
|
// One incoming, three outgoing
|
|
|
|
const messages = window.locator('.module-message__text');
|
|
|
|
assert.strictEqual(await messages.count(), 4, 'message count');
|
|
|
|
|
|
|
|
// No notifications
|
|
|
|
const notifications = window.locator('.SystemMessage');
|
|
|
|
assert.strictEqual(await notifications.count(), 0, 'notification count');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be received by Desktop and trigger contact merge', async () => {
|
|
|
|
const { desktop, phone } = bootstrap;
|
|
|
|
|
|
|
|
const window = await app.getWindow();
|
|
|
|
|
|
|
|
const leftPane = window.locator('.left-pane-wrapper');
|
|
|
|
|
|
|
|
debug('opening conversation with the pni contact');
|
|
|
|
await leftPane
|
2023-01-13 00:24:59 +00:00
|
|
|
.locator('.module-conversation-list__item--contact-or-conversation')
|
|
|
|
.first()
|
2022-08-15 21:53:33 +00:00
|
|
|
.click();
|
|
|
|
|
|
|
|
debug('Enter a PNI message text');
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
await compositionInput.type('Hello PNI');
|
|
|
|
await compositionInput.press('Enter');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
debug('Waiting for a PNI message');
|
|
|
|
{
|
|
|
|
const { source, body, uuidKind } = await pniContact.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(source, desktop, 'PNI message has valid source');
|
|
|
|
assert.strictEqual(body, 'Hello PNI', 'PNI message has valid body');
|
|
|
|
assert.strictEqual(
|
|
|
|
uuidKind,
|
|
|
|
UUIDKind.PNI,
|
|
|
|
'PNI message has valid destination'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('Capture storage service state before merging');
|
|
|
|
const state = await phone.expectStorageState('state before merge');
|
|
|
|
|
|
|
|
debug('Enter a draft text without hitting enter');
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
|
|
|
|
|
|
|
await compositionInput.type('Draft text');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
debug('Send back the response with profile key and pni signature');
|
|
|
|
|
|
|
|
const ourKey = await desktop.popSingleUseKey();
|
|
|
|
await pniContact.addSingleUseKey(desktop, ourKey);
|
|
|
|
|
|
|
|
await pniContact.sendText(desktop, 'Hello Desktop!', {
|
|
|
|
timestamp: bootstrap.getTimestamp(),
|
|
|
|
withPniSignature: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
debug('Wait for merge to happen');
|
|
|
|
await leftPane
|
2023-01-13 00:24:59 +00:00
|
|
|
.locator(`[data-testid="${pniContact.toContact().uuid}"]`)
|
2022-08-15 21:53:33 +00:00
|
|
|
.waitFor();
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
{
|
|
|
|
debug('Wait for composition input to clear');
|
|
|
|
const compositionInput = await app.waitForEnabledComposer();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
debug('Enter an ACI message text');
|
|
|
|
await compositionInput.type('Hello ACI');
|
|
|
|
await compositionInput.press('Enter');
|
|
|
|
}
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
debug('Waiting for a ACI message');
|
|
|
|
{
|
|
|
|
const { source, body, uuidKind } = await pniContact.waitForMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(source, desktop, 'ACI message has valid source');
|
|
|
|
assert.strictEqual(body, 'Hello ACI', 'ACI message has valid body');
|
|
|
|
assert.strictEqual(
|
|
|
|
uuidKind,
|
|
|
|
UUIDKind.ACI,
|
|
|
|
'ACI message has valid destination'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('Verify final state');
|
2023-01-12 22:18:08 +00:00
|
|
|
|
2022-08-15 21:53:33 +00:00
|
|
|
{
|
|
|
|
const newState = await phone.waitForStorageState({
|
|
|
|
after: state,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isUndefined(
|
|
|
|
newState.getContact(pniContact, UUIDKind.PNI),
|
|
|
|
'PNI Contact must be removed from storage service'
|
|
|
|
);
|
|
|
|
|
|
|
|
const aci = newState.getContact(pniContact, UUIDKind.ACI);
|
|
|
|
assert(aci, 'ACI Contact must be in storage service');
|
|
|
|
|
|
|
|
assert.strictEqual(aci?.serviceUuid, pniContact.device.uuid);
|
|
|
|
assert.strictEqual(aci?.pni, pniContact.device.pni);
|
2022-12-05 22:46:54 +00:00
|
|
|
|
|
|
|
// Two outgoing, one incoming
|
|
|
|
const messages = window.locator('.module-message__text');
|
|
|
|
assert.strictEqual(await messages.count(), 3, 'messages');
|
|
|
|
|
2023-02-08 01:55:07 +00:00
|
|
|
// No notifications
|
2022-12-05 22:46:54 +00:00
|
|
|
const notifications = window.locator('.SystemMessage');
|
2023-02-08 01:55:07 +00:00
|
|
|
assert.strictEqual(await notifications.count(), 0, 'notifications');
|
2023-05-24 22:05:04 +00:00
|
|
|
|
|
|
|
assert.isEmpty(await phone.getOrphanedStorageKeys());
|
2022-08-15 21:53:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|