signal-desktop/ts/test-mock/pnp/pni_signature_test.ts

432 lines
13 KiB
TypeScript
Raw Normal View History

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 Long from 'long';
2023-08-16 20:54:39 +00:00
import { Pni } from '@signalapp/libsignal-client';
2022-08-15 21:53:33 +00:00
import {
2023-08-16 20:54:39 +00:00
ServiceIdKind,
2022-08-15 21:53:33 +00:00
Proto,
ReceiptType,
StorageState,
} from '@signalapp/mock-server';
import createDebug from 'debug';
import * as durations from '../../util/durations';
import { uuidToBytes } from '../../util/uuidToBytes';
import { MY_STORY_ID } from '../../types/Stories';
2023-09-27 23:14:55 +00:00
import { isUntaggedPniString, toTaggedPni } from '../../types/ServiceId';
2022-08-15 21:53:33 +00:00
import { Bootstrap } from '../bootstrap';
import type { App } from '../bootstrap';
2023-12-15 18:33:02 +00:00
import {
DELETE_SENT_PROTO_BATCHER_WAIT_MS,
RECEIPT_BATCHER_WAIT_MS,
} from '../../types/Receipt';
import { sleep } from '../../util/sleep';
2024-04-03 17:17:39 +00:00
import { expectSystemMessages, typeIntoInput } from '../helpers';
2022-08-15 21:53:33 +00:00
export const debug = createDebug('mock:test:pni-signature');
const IdentifierType = Proto.ManifestRecord.Identifier.Type;
describe('pnp/PNI Signature', function (this: Mocha.Suite) {
2022-08-15 21:53:33 +00:00
this.timeout(durations.MINUTE);
let bootstrap: Bootstrap;
let app: App;
beforeEach(async () => {
bootstrap = new Bootstrap({ contactCount: 0 });
2022-08-15 21:53:33 +00:00
await bootstrap.init();
const { phone } = bootstrap;
2022-08-15 21:53:33 +00:00
let state = StorageState.getEmpty();
state = state.updateAccount({
profileKey: phone.profileKey.serialize(),
e164: phone.device.number,
});
// Add my story
state = state.addRecord({
type: IdentifierType.STORY_DISTRIBUTION_LIST,
record: {
storyDistributionList: {
allowsReplies: true,
identifier: uuidToBytes(MY_STORY_ID),
2022-08-15 21:53:33 +00:00
isBlockList: true,
name: MY_STORY_ID,
2023-08-16 20:54:39 +00:00
recipientServiceIds: [],
2022-08-15 21:53:33 +00:00
},
},
});
await phone.setStorageState(state);
app = await bootstrap.link();
});
afterEach(async function (this: Mocha.Context) {
await bootstrap.maybeSaveLogs(this.currentTest, 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;
2023-08-16 20:54:39 +00:00
const ourPniKey = await desktop.getIdentityKey(ServiceIdKind.PNI);
const ourAciKey = await desktop.getIdentityKey(ServiceIdKind.ACI);
2022-08-15 21:53:33 +00:00
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
const conversationStack = window.locator('.Inbox__conversation-stack');
2022-08-15 21:53:33 +00:00
debug('creating a stranger');
const stranger = await server.createPrimaryDevice({
profileName: 'Mysterious Stranger',
});
2023-08-16 20:54:39 +00:00
const ourKey = await desktop.popSingleUseKey(ServiceIdKind.PNI);
await stranger.addSingleUseKey(desktop, ourKey, ServiceIdKind.PNI);
2022-08-15 21:53:33 +00:00
const checkPniSignature = (
message: Proto.IPniSignatureMessage | null | undefined,
source: string
) => {
if (!message) {
throw new Error(
`Missing expected pni signature message from ${source}`
);
}
2023-08-16 20:54:39 +00:00
if (!message.pni) {
throw new Error(
`Missing expected pni on pni signature message from ${source}`
);
}
2022-08-15 21:53:33 +00:00
assert.deepEqual(
2023-09-14 20:39:51 +00:00
Pni.fromUuidBytes(Buffer.from(message.pni)).getServiceIdString(),
2023-08-16 20:54:39 +00:00
desktop.pni,
2022-08-15 21:53:33 +00:00
`Incorrect pni in pni signature message from ${source}`
);
const isValid = ourPniKey.verifyAlternateIdentity(
ourAciKey,
2022-08-15 21:53:33 +00:00
Buffer.from(message.signature ?? [])
);
assert.isTrue(isValid, `Invalid pni signature from ${source}`);
};
debug('Send a message to our PNI');
2022-08-15 21:53:33 +00:00
await stranger.sendText(desktop, 'A message to PNI', {
2023-08-16 20:54:39 +00:00
serviceIdKind: ServiceIdKind.PNI,
2022-08-15 21:53:33 +00:00
withProfileKey: true,
timestamp: bootstrap.getTimestamp(),
});
debug('Open conversation with the stranger');
2022-08-15 21:53:33 +00:00
await leftPane
2023-08-16 20:54:39 +00:00
.locator(`[data-testid="${stranger.toContact().aci}"]`)
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('Wait for a pniSignatureMessage');
2022-08-15 21:53:33 +00:00
{
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');
{
const compositionInput = await app.waitForEnabledComposer();
2022-08-15 21:53:33 +00:00
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'first');
await compositionInput.press('Enter');
}
debug('Wait for the first message with pni signature');
2022-08-15 21:53:33 +00:00
{
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('Send unencrypted receipt', receiptTimestamp);
2022-08-15 21:53:33 +00:00
await stranger.sendUnencryptedReceipt(desktop, {
messageTimestamp: dataMessage.timestamp?.toNumber() ?? 0,
timestamp: receiptTimestamp,
});
}
debug('Enter second message text');
{
const compositionInput = await app.waitForEnabledComposer();
2022-08-15 21:53:33 +00:00
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'second');
await compositionInput.press('Enter');
}
debug('Wait for the second message with pni signature');
2022-08-15 21:53:33 +00:00
{
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('Send encrypted receipt', receiptTimestamp);
2022-08-15 21:53:33 +00:00
await stranger.sendReceipt(desktop, {
type: ReceiptType.Delivery,
messageTimestamps: [dataMessage.timestamp?.toNumber() ?? 0],
timestamp: receiptTimestamp,
});
2023-12-15 18:33:02 +00:00
// Wait for receipts to be batched and processed (+ buffer)
await sleep(
RECEIPT_BATCHER_WAIT_MS + DELETE_SENT_PROTO_BATCHER_WAIT_MS + 20
);
2022-08-15 21:53:33 +00:00
}
2023-12-15 18:33:02 +00:00
2022-08-15 21:53:33 +00:00
debug('Enter third message text');
{
const compositionInput = await app.waitForEnabledComposer();
2022-08-15 21:53:33 +00:00
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'third');
await compositionInput.press('Enter');
}
debug('Wait for the third message without pni signature');
2022-08-15 21:53:33 +00:00
{
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'
);
}
debug('Verify final state');
{
// One incoming, three outgoing
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 4, 'message count');
2024-03-12 16:29:31 +00:00
await expectSystemMessages(window, ['You accepted the message request']);
}
2022-08-15 21:53:33 +00:00
});
it('should be received by Desktop and trigger contact merge', async () => {
const { desktop, phone, server } = bootstrap;
2022-08-15 21:53:33 +00:00
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
2022-08-15 21:53:33 +00:00
debug('Capture storage service state before messaging');
let state = await phone.expectStorageState('state before messaging');
debug('Create stranger');
const STRANGER_NAME = 'Mysterious Stranger';
const stranger = await server.createPrimaryDevice({
profileName: STRANGER_NAME,
});
2022-08-15 21:53:33 +00:00
debug('Send a PNI sync message');
const timestamp = bootstrap.getTimestamp();
const destinationServiceId = stranger.device.pni;
const destination = stranger.device.number;
const destinationPniIdentityKey = await stranger.device.getIdentityKey(
ServiceIdKind.PNI
);
const originalDataMessage = {
body: 'Hello PNI',
timestamp: Long.fromNumber(timestamp),
};
const content = {
syncMessage: {
sent: {
destinationServiceId,
destination,
timestamp: Long.fromNumber(timestamp),
message: originalDataMessage,
unidentifiedStatus: [
{
destinationServiceId,
destination,
destinationPniIdentityKey: destinationPniIdentityKey.serialize(),
},
],
},
},
};
const sendOptions = {
timestamp,
};
await phone.sendRaw(desktop, content, sendOptions);
debug('Wait for updated storage service state with PNI contact');
{
const newState = await phone.waitForStorageState({
after: state,
});
2022-08-15 21:53:33 +00:00
const aciRecord = newState.getContact(stranger, ServiceIdKind.ACI);
assert.isUndefined(aciRecord, 'ACI contact must not be created');
const pniRecord = newState.getContact(stranger, ServiceIdKind.PNI);
assert.deepEqual(
pniRecord?.identityKey,
destinationPniIdentityKey.serialize(),
'PNI contact must have correct identity key'
);
state = newState;
}
2022-08-15 21:53:33 +00:00
debug('Open conversation with the pni contact');
const contactElem = leftPane.locator(
`[data-testid="${stranger.device.pni}"]`
);
await contactElem.click();
2022-08-15 21:53:33 +00:00
debug('Verify that left pane shows phone number');
{
const strangerName = await contactElem
.locator('.module-contact-name')
.first()
.innerText();
assert.equal(
strangerName.slice(-4),
destination?.slice(-4),
'no profile, just phone number'
2022-08-15 21:53:33 +00:00
);
}
debug('Verify that we are in MR state');
const conversationStack = window.locator('.Inbox__conversation-stack');
await conversationStack
.locator('.module-message-request-actions button >> "Continue"')
.waitFor();
2022-08-15 21:53:33 +00:00
debug('Clear message request state on phone');
{
const newState = state.updateContact(
stranger,
{
whitelisted: true,
},
ServiceIdKind.PNI
);
await phone.setStorageState(newState, state);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
state = newState;
}
2022-08-15 21:53:33 +00:00
debug('Wait for MR state to disappear');
await conversationStack
.locator('.module-message-request-actions button >> "Continue"')
.waitFor({ state: 'hidden' });
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 stranger.addSingleUseKey(desktop, ourKey);
2022-08-15 21:53:33 +00:00
await stranger.sendText(desktop, 'Hello Desktop!', {
2022-08-15 21:53:33 +00:00
timestamp: bootstrap.getTimestamp(),
withPniSignature: true,
2024-03-06 23:59:51 +00:00
withProfileKey: true,
2022-08-15 21:53:33 +00:00
});
debug('Wait for merge to happen');
await leftPane
.locator(`[data-testid="${stranger.toContact().aci}"]`)
2022-08-15 21:53:33 +00:00
.waitFor();
{
debug('Wait for composition input to clear');
const compositionInput = await app.waitForEnabledComposer();
2022-08-15 21:53:33 +00:00
debug('Enter an ACI message text');
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'Hello ACI');
await compositionInput.press('Enter');
}
2022-08-15 21:53:33 +00:00
debug('Wait for a ACI message');
2022-08-15 21:53:33 +00:00
{
const { source, body, serviceIdKind } = await stranger.waitForMessage();
2022-08-15 21:53:33 +00:00
assert.strictEqual(source, desktop, 'ACI message has valid source');
assert.strictEqual(body, 'Hello ACI', 'ACI message has valid body');
assert.strictEqual(
2023-08-16 20:54:39 +00:00
serviceIdKind,
ServiceIdKind.ACI,
2022-08-15 21:53:33 +00:00
'ACI message has valid destination'
);
}
debug('Verify final state');
2022-08-15 21:53:33 +00:00
{
const newState = await phone.waitForStorageState({
after: state,
});
const pniRecord = newState.getContact(stranger, ServiceIdKind.PNI);
const aciRecord = newState.getContact(stranger, ServiceIdKind.ACI);
2023-08-16 20:54:39 +00:00
assert.strictEqual(
2023-09-27 23:14:55 +00:00
aciRecord,
pniRecord,
2023-08-16 20:54:39 +00:00
'ACI Contact must be the same as PNI Contact storage service'
2022-08-15 21:53:33 +00:00
);
2023-09-27 23:14:55 +00:00
assert(aciRecord, 'ACI Contact must be in storage service');
2022-08-15 21:53:33 +00:00
assert.strictEqual(aciRecord?.aci, stranger.device.aci);
2023-09-27 23:14:55 +00:00
assert.strictEqual(
aciRecord?.pni &&
isUntaggedPniString(aciRecord?.pni) &&
toTaggedPni(aciRecord?.pni),
stranger.device.pni
2023-09-27 23:14:55 +00:00
);
2024-01-29 22:37:26 +00:00
assert.strictEqual(aciRecord?.pniSignatureVerified, true);
// Two outgoing, one incoming
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 3, 'messages');
2024-03-06 23:59:51 +00:00
// Title transition notification
await expectSystemMessages(window, [/You started this chat with/]);
2023-05-24 22:05:04 +00:00
assert.isEmpty(await phone.getOrphanedStorageKeys());
2022-08-15 21:53:33 +00:00
}
});
});