2022-08-15 21:53:33 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
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 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';
|
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';
|
|
|
|
|
|
|
|
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);
|
2023-09-19 17:02:37 +00:00
|
|
|
this.retries(4);
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
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-08-16 20:54:39 +00:00
|
|
|
identityKey: pniContact.getPublicKey(ServiceIdKind.PNI).serialize(),
|
2022-08-15 21:53:33 +00:00
|
|
|
givenName: 'PNI Contact',
|
|
|
|
},
|
2023-08-16 20:54:39 +00:00
|
|
|
ServiceIdKind.PNI
|
2022-08-15 21:53:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
2023-08-16 20:54:39 +00:00
|
|
|
state = state.pin(pniContact, ServiceIdKind.PNI);
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
// 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,
|
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 after() {
|
2023-07-14 16:53:20 +00:00
|
|
|
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();
|
|
|
|
|
2023-07-26 22:23:32 +00:00
|
|
|
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}`
|
|
|
|
);
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
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('sending a message to our PNI');
|
|
|
|
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('opening conversation with the stranger');
|
|
|
|
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('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();
|
|
|
|
|
2023-07-26 22:23:32 +00:00
|
|
|
const leftPane = window.locator('#LeftPane');
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
const { source, body, serviceIdKind } = await pniContact.waitForMessage();
|
2022-08-15 21:53:33 +00:00
|
|
|
|
|
|
|
assert.strictEqual(source, desktop, 'PNI message has valid source');
|
|
|
|
assert.strictEqual(body, 'Hello PNI', 'PNI message has valid body');
|
|
|
|
assert.strictEqual(
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceIdKind,
|
|
|
|
ServiceIdKind.PNI,
|
2022-08-15 21:53:33 +00:00
|
|
|
'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-08-16 20:54:39 +00:00
|
|
|
.locator(`[data-testid="${pniContact.toContact().aci}"]`)
|
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');
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
const { source, body, serviceIdKind } = await pniContact.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');
|
2023-01-12 22:18:08 +00:00
|
|
|
|
2022-08-15 21:53:33 +00:00
|
|
|
{
|
|
|
|
const newState = await phone.waitForStorageState({
|
|
|
|
after: state,
|
|
|
|
});
|
|
|
|
|
2023-09-27 23:14:55 +00:00
|
|
|
const pniRecord = newState.getContact(pniContact, ServiceIdKind.PNI);
|
|
|
|
const aciRecord = newState.getContact(pniContact, 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
|
|
|
|
2023-09-27 23:14:55 +00:00
|
|
|
assert.strictEqual(aciRecord?.aci, pniContact.device.aci);
|
|
|
|
assert.strictEqual(
|
|
|
|
aciRecord?.pni &&
|
|
|
|
isUntaggedPniString(aciRecord?.pni) &&
|
|
|
|
toTaggedPni(aciRecord?.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
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|