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

461 lines
14 KiB
TypeScript
Raw Normal View History

// 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 { ServiceIdKind, Proto, StorageState } from '@signalapp/mock-server';
import type { PrimaryDevice } from '@signalapp/mock-server';
import createDebug from 'debug';
import Long from 'long';
import * as durations from '../../util/durations';
import { uuidToBytes } from '../../util/uuidToBytes';
2024-01-29 22:37:26 +00:00
import { generateConfigMatrix } from '../../util/generateConfigMatrix';
2023-09-27 23:14:55 +00:00
import { toUntaggedPni } from '../../types/ServiceId';
import { MY_STORY_ID } from '../../types/Stories';
import { Bootstrap } from '../bootstrap';
import type { App } from '../bootstrap';
export const debug = createDebug('mock:test:merge');
const IdentifierType = Proto.ManifestRecord.Identifier.Type;
describe('pnp/merge', function (this: Mocha.Suite) {
this.timeout(durations.MINUTE);
let bootstrap: Bootstrap;
let app: App;
let pniContact: PrimaryDevice;
let pniIdentityKey: Uint8Array;
let aciIdentityKey: Uint8Array;
beforeEach(async () => {
bootstrap = new Bootstrap({ contactCount: 0 });
await bootstrap.init();
const { server, phone } = bootstrap;
pniContact = await server.createPrimaryDevice({
profileName: 'ACI Contact',
});
2023-08-16 20:54:39 +00:00
pniIdentityKey = pniContact.getPublicKey(ServiceIdKind.PNI).serialize();
aciIdentityKey = pniContact.publicKey.serialize();
let state = StorageState.getEmpty();
state = state.updateAccount({
profileKey: phone.profileKey.serialize(),
e164: phone.device.number,
});
state = state.addContact(
pniContact,
{
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
identityKey: pniIdentityKey,
serviceE164: pniContact.device.number,
givenName: 'PNI Contact',
},
2023-08-16 20:54:39 +00:00
ServiceIdKind.PNI
);
state = state.addContact(pniContact, {
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
serviceE164: undefined,
identityKey: aciIdentityKey,
givenName: pniContact.profileName,
});
// Put both contacts in left pane
2023-08-16 20:54:39 +00:00
state = state.pin(pniContact, ServiceIdKind.PNI);
state = state.pin(pniContact, ServiceIdKind.ACI);
// Add my story
state = state.addRecord({
type: IdentifierType.STORY_DISTRIBUTION_LIST,
record: {
storyDistributionList: {
allowsReplies: true,
identifier: uuidToBytes(MY_STORY_ID),
isBlockList: true,
name: MY_STORY_ID,
2023-08-16 20:54:39 +00:00
recipientServiceIds: [],
},
},
});
await phone.setStorageState(state);
app = await bootstrap.link();
});
afterEach(async function (this: Mocha.Context) {
await bootstrap.maybeSaveLogs(this.currentTest, app);
await app.close();
await bootstrap.teardown();
});
2024-01-29 22:37:26 +00:00
const matrix = generateConfigMatrix({
finalContact: [ServiceIdKind.ACI, ServiceIdKind.PNI],
withPNIMessage: [false, true],
pniSignatureVerified: [false, true],
});
for (const { finalContact, withPNIMessage, pniSignatureVerified } of matrix) {
const testName =
'happens via storage service, ' +
`${withPNIMessage ? 'with' : 'without'} pni message ` +
`${pniSignatureVerified ? 'with' : 'without'} pniSignatureVerified ` +
`(${finalContact})`;
2024-01-29 22:37:26 +00:00
// eslint-disable-next-line no-loop-func
it(testName, async () => {
const { phone } = bootstrap;
2024-01-29 22:37:26 +00:00
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
2024-01-29 22:37:26 +00:00
debug('opening conversation with the aci contact');
await leftPane
.locator(`[data-testid="${pniContact.device.aci}"]`)
.click();
2023-01-10 18:57:39 +00:00
2024-01-29 22:37:26 +00:00
await window.locator('.module-conversation-hero').waitFor();
2023-01-10 18:57:39 +00:00
2024-01-29 22:37:26 +00:00
debug('Send message to ACI');
{
const compositionInput = await app.waitForEnabledComposer();
2023-01-10 18:57:39 +00:00
2024-01-29 22:37:26 +00:00
await compositionInput.type('Hello ACI');
await compositionInput.press('Enter');
}
2023-01-10 18:57:39 +00:00
2024-01-29 22:37:26 +00:00
debug('opening conversation with the pni contact');
await leftPane
.locator('.module-conversation-list__item--contact-or-conversation')
.first()
.click();
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
await window.locator('.module-conversation-hero').waitFor();
2023-01-10 18:57:39 +00:00
2024-01-29 22:37:26 +00:00
debug('Verify starting state');
{
// No messages
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 0, 'message count');
// No notifications
const notifications = window.locator('.SystemMessage');
assert.strictEqual(
await notifications.count(),
0,
'notification count'
);
}
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
if (withPNIMessage) {
debug('Send message to PNI');
const compositionInput = await app.waitForEnabledComposer();
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
await compositionInput.type('Hello PNI');
await compositionInput.press('Enter');
}
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
if (finalContact === ServiceIdKind.ACI) {
debug('switching back to ACI conversation');
await leftPane
.locator(`[data-testid="${pniContact.device.aci}"]`)
.click();
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
await window.locator('.module-conversation-hero').waitFor();
}
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
debug(
'removing both contacts from storage service, adding one combined contact'
);
{
const state = await phone.expectStorageState('consistency check');
await phone.setStorageState(
state.mergeContact(pniContact, {
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
identityKey: pniContact.publicKey.serialize(),
profileKey: pniContact.profileKey.serialize(),
pniSignatureVerified,
})
2023-01-10 18:57:39 +00:00
);
2024-01-29 22:37:26 +00:00
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
await app.waitForManifestVersion(state.version);
}
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
debug('Verify final state');
{
// Should have both PNI and ACI messages
await window.locator('.module-message__text >> "Hello ACI"').waitFor();
if (withPNIMessage) {
2023-03-24 00:52:46 +00:00
await window
2024-01-29 22:37:26 +00:00
.locator('.module-message__text >> "Hello PNI"')
2023-03-24 00:52:46 +00:00
.waitFor();
2024-01-29 22:37:26 +00:00
}
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
const messages = window.locator('.module-message__text');
assert.strictEqual(
await messages.count(),
withPNIMessage ? 2 : 1,
'message count'
);
2023-03-24 00:52:46 +00:00
2024-01-29 22:37:26 +00:00
// One notification - the merge
const notifications = window.locator('.SystemMessage');
assert.strictEqual(
await notifications.count(),
withPNIMessage && !pniSignatureVerified ? 1 : 0,
'notification count'
);
if (withPNIMessage && !pniSignatureVerified) {
const first = await notifications.first();
assert.match(
await first.innerText(),
/Your message history with ACI Contact and their number .* has been merged./
);
2023-03-24 00:52:46 +00:00
}
2024-01-29 22:37:26 +00:00
}
});
2023-01-10 18:57:39 +00:00
}
2024-01-31 17:51:09 +00:00
for (const withPniContact of [false, true]) {
const testName =
'accepts storage service contact splitting ' +
`${withPniContact ? 'with PNI contact' : 'without PNI contact'}`;
2024-01-31 17:51:09 +00:00
// eslint-disable-next-line no-loop-func
it(testName, async () => {
const { phone } = bootstrap;
debug(
'removing both contacts from storage service, adding one combined contact'
);
2024-01-31 17:51:09 +00:00
{
const state = await phone.expectStorageState('consistency check');
await phone.setStorageState(
state.mergeContact(pniContact, {
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
identityKey: pniContact.publicKey.serialize(),
profileKey: pniContact.profileKey.serialize(),
})
);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
}
2024-01-31 17:51:09 +00:00
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
2024-01-31 17:51:09 +00:00
debug('opening conversation with the merged contact');
await leftPane
.locator(
`[data-testid="${pniContact.device.aci}"] >> ` +
`"${pniContact.profileName}"`
)
.click();
2024-01-31 17:51:09 +00:00
await window.locator('.module-conversation-hero').waitFor();
2024-01-31 17:51:09 +00:00
debug('Send message to merged contact');
{
const compositionInput = await app.waitForEnabledComposer();
2024-01-31 17:51:09 +00:00
await compositionInput.type('Hello merged');
await compositionInput.press('Enter');
}
2024-01-31 17:51:09 +00:00
debug('Split contact and mark ACI as unregistered');
{
let state = await phone.expectStorageState('consistency check');
2024-01-31 17:51:09 +00:00
state = state.updateContact(pniContact, {
pni: undefined,
serviceE164: undefined,
unregisteredAtTimestamp: Long.fromNumber(bootstrap.getTimestamp()),
});
2024-01-31 17:51:09 +00:00
if (withPniContact) {
state = state.addContact(
pniContact,
{
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
2024-01-31 17:51:09 +00:00
identityKey: pniIdentityKey,
2024-01-31 17:51:09 +00:00
serviceE164: pniContact.device.number,
givenName: 'PNI Contact',
},
ServiceIdKind.PNI
);
}
2024-01-31 17:51:09 +00:00
state = state.pin(pniContact, ServiceIdKind.PNI);
2024-01-31 17:51:09 +00:00
await phone.setStorageState(state);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
}
2024-01-31 17:51:09 +00:00
debug('Wait for pni contact to appear');
await leftPane
.locator(`[data-testid="${pniContact.device.pni}"]`)
.waitFor();
2024-01-31 17:51:09 +00:00
debug('Verify that the message is in the ACI conversation');
{
// Should have both PNI and ACI messages
await window
.locator('.module-message__text >> "Hello merged"')
.waitFor();
2024-01-31 17:51:09 +00:00
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 1, 'message count');
}
2024-01-31 17:51:09 +00:00
debug('Open PNI conversation');
await leftPane
.locator(`[data-testid="${pniContact.device.pni}"]`)
.click();
2024-01-31 17:51:09 +00:00
debug('Verify absence of messages in the PNI conversation');
{
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 0, 'message count');
}
});
}
it('splits contact when ACI becomes unregistered', async () => {
const { phone, server } = bootstrap;
debug(
'removing both contacts from storage service, adding one combined contact'
);
{
const state = await phone.expectStorageState('consistency check');
await phone.setStorageState(
state.mergeContact(pniContact, {
identityState: Proto.ContactRecord.IdentityState.DEFAULT,
whitelisted: true,
identityKey: pniContact.publicKey.serialize(),
profileKey: pniContact.profileKey.serialize(),
})
);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
}
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
debug('opening conversation with the merged contact');
await leftPane
.locator(
2023-08-16 20:54:39 +00:00
`[data-testid="${pniContact.device.aci}"] >> ` +
`"${pniContact.profileName}"`
)
.click();
await window.locator('.module-conversation-hero').waitFor();
debug('Unregistering ACI');
server.unregister(pniContact);
const state = await phone.expectStorageState('initial state');
debug('Send message to merged contact');
{
const compositionInput = await app.waitForEnabledComposer();
await compositionInput.type('Hello merged');
await compositionInput.press('Enter');
}
debug('Verify that contact is split in storage service');
{
const newState = await phone.waitForStorageState({
after: state,
});
const { added, removed } = newState.diff(state);
assert.strictEqual(added.length, 2, 'only two records must be added');
assert.strictEqual(removed.length, 1, 'only one record must be removed');
let pniContacts = 0;
let aciContacts = 0;
for (const { contact } of added) {
if (!contact) {
throw new Error('Invalid record');
}
2023-08-16 20:54:39 +00:00
const { aci, serviceE164, pni } = contact;
if (aci === pniContact.device.aci) {
aciContacts += 1;
assert.strictEqual(pni, '');
assert.strictEqual(serviceE164, '');
2023-09-27 23:14:55 +00:00
} else if (pni === toUntaggedPni(pniContact.device.pni)) {
pniContacts += 1;
2023-08-16 20:54:39 +00:00
assert.strictEqual(aci, '');
assert.strictEqual(serviceE164, pniContact.device.number);
}
}
assert.strictEqual(aciContacts, 1);
assert.strictEqual(pniContacts, 1);
2023-09-27 23:14:55 +00:00
assert.strictEqual(
removed[0].contact?.pni,
toUntaggedPni(pniContact.device.pni)
);
2023-08-16 20:54:39 +00:00
assert.strictEqual(removed[0].contact?.aci, pniContact.device.aci);
// Pin PNI so that it appears in the left pane
2023-08-16 20:54:39 +00:00
const updated = newState.pin(pniContact, ServiceIdKind.PNI);
await phone.setStorageState(updated);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
}
debug('Verify that the message is in the ACI conversation');
{
// Should have both PNI and ACI messages
await window.locator('.module-message__text >> "Hello merged"').waitFor();
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 1, 'message count');
}
debug('Open PNI conversation');
await leftPane.locator(`[data-testid="${pniContact.device.pni}"]`).click();
2023-02-08 17:14:59 +00:00
debug('Wait for ACI conversation to go away');
await window
.locator(`.module-conversation-hero >> ${pniContact.profileName}`)
.waitFor({
state: 'hidden',
});
debug('Verify absence of messages in the PNI conversation');
{
const messages = window.locator('.module-message__text');
assert.strictEqual(await messages.count(), 0, 'message count');
}
});
});