2021-05-06 00:09:29 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2021-08-26 18:18:00 +00:00
|
|
|
/* eslint-disable no-await-in-loop */
|
2021-05-06 00:09:29 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
import * as sinon from 'sinon';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MinimalMessage } from '../challenge';
|
|
|
|
import { ChallengeHandler } from '../challenge';
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
type CreateMessageOptions = {
|
|
|
|
readonly sentAt?: number;
|
|
|
|
readonly retryAfter?: number;
|
|
|
|
readonly isNormalBubble?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CreateHandlerOptions = {
|
2021-05-07 20:59:46 +00:00
|
|
|
readonly autoSolve?: boolean;
|
2021-05-06 00:09:29 +00:00
|
|
|
readonly challengeError?: Error;
|
|
|
|
readonly expireAfter?: number;
|
|
|
|
readonly onChallengeSolved?: () => void;
|
|
|
|
readonly onChallengeFailed?: (retryAfter?: number) => void;
|
|
|
|
};
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
const NOW = Date.now();
|
2021-05-07 20:59:46 +00:00
|
|
|
const ONE_DAY = 24 * 3600 * 1000;
|
2021-05-08 00:34:50 +00:00
|
|
|
const NEVER_RETRY = NOW + ONE_DAY;
|
|
|
|
const IMMEDIATE_RETRY = NOW - ONE_DAY;
|
2021-05-07 20:59:46 +00:00
|
|
|
|
|
|
|
// Various timeouts in milliseconds
|
|
|
|
const DEFAULT_RETRY_AFTER = 25;
|
|
|
|
const SOLVE_AFTER = 5;
|
2021-05-07 16:10:40 +00:00
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
describe('ChallengeHandler', () => {
|
|
|
|
const storage = new Map<string, any>();
|
|
|
|
const messageStorage = new Map<string, MinimalMessage>();
|
|
|
|
let challengeStatus = 'idle';
|
|
|
|
let sent: Array<string> = [];
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
beforeEach(function beforeEach() {
|
2021-05-06 00:09:29 +00:00
|
|
|
storage.clear();
|
|
|
|
messageStorage.clear();
|
|
|
|
challengeStatus = 'idle';
|
|
|
|
sent = [];
|
2021-05-08 00:34:50 +00:00
|
|
|
|
|
|
|
this.sandbox = sinon.createSandbox();
|
|
|
|
this.clock = this.sandbox.useFakeTimers({
|
|
|
|
now: NOW,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function afterEach() {
|
|
|
|
this.sandbox.restore();
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const createMessage = (
|
|
|
|
id: string,
|
|
|
|
options: CreateMessageOptions = {}
|
|
|
|
): MinimalMessage => {
|
|
|
|
const {
|
|
|
|
sentAt = 0,
|
|
|
|
isNormalBubble = true,
|
2021-05-08 00:34:50 +00:00
|
|
|
retryAfter = NOW + DEFAULT_RETRY_AFTER,
|
2021-05-06 00:09:29 +00:00
|
|
|
} = options;
|
|
|
|
|
|
|
|
const testLocalSent = sent;
|
|
|
|
|
|
|
|
const events = new Map<string, () => void>();
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
idForLogging: () => id,
|
|
|
|
isNormalBubble() {
|
|
|
|
return isNormalBubble;
|
|
|
|
},
|
|
|
|
getLastChallengeError() {
|
|
|
|
return {
|
|
|
|
name: 'Ignored',
|
|
|
|
message: 'Ignored',
|
|
|
|
retryAfter,
|
|
|
|
data: { token: 'token', options: ['recaptcha'] },
|
|
|
|
};
|
|
|
|
},
|
|
|
|
get(name) {
|
|
|
|
assert.equal(name, 'sent_at');
|
|
|
|
return sentAt;
|
|
|
|
},
|
|
|
|
on(name, handler) {
|
|
|
|
if (events.get(name)) {
|
|
|
|
throw new Error('Duplicate event');
|
|
|
|
}
|
|
|
|
events.set(name, handler);
|
|
|
|
},
|
|
|
|
off(name, handler) {
|
|
|
|
assert.equal(events.get(name), handler);
|
|
|
|
events.delete(name);
|
|
|
|
},
|
|
|
|
async retrySend() {
|
|
|
|
const handler = events.get('sent');
|
|
|
|
if (!handler) {
|
|
|
|
throw new Error('Expected handler');
|
|
|
|
}
|
|
|
|
handler();
|
|
|
|
testLocalSent.push(this.id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createHandler = async ({
|
2021-05-07 20:59:46 +00:00
|
|
|
autoSolve = false,
|
2021-05-06 00:09:29 +00:00
|
|
|
challengeError,
|
|
|
|
expireAfter,
|
|
|
|
onChallengeSolved = noop,
|
|
|
|
onChallengeFailed = noop,
|
|
|
|
}: CreateHandlerOptions = {}): Promise<ChallengeHandler> => {
|
|
|
|
const handler = new ChallengeHandler({
|
|
|
|
expireAfter,
|
|
|
|
|
|
|
|
storage: {
|
2021-06-15 00:09:37 +00:00
|
|
|
get(key: string) {
|
2021-05-06 00:09:29 +00:00
|
|
|
return storage.get(key);
|
|
|
|
},
|
2021-06-15 00:09:37 +00:00
|
|
|
async put(key: string, value: unknown) {
|
2021-05-06 00:09:29 +00:00
|
|
|
storage.set(key, value);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
onChallengeSolved,
|
|
|
|
onChallengeFailed,
|
|
|
|
|
|
|
|
requestChallenge(request) {
|
2021-05-07 20:59:46 +00:00
|
|
|
if (!autoSolve) {
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
handler.onResponse({
|
|
|
|
seq: request.seq,
|
|
|
|
data: { captcha: 'captcha' },
|
|
|
|
});
|
2021-05-07 20:59:46 +00:00
|
|
|
}, SOLVE_AFTER);
|
2021-05-06 00:09:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getMessageById(messageId) {
|
|
|
|
return messageStorage.get(messageId);
|
|
|
|
},
|
|
|
|
|
|
|
|
async sendChallengeResponse() {
|
|
|
|
if (challengeError) {
|
|
|
|
throw challengeError;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setChallengeStatus(status) {
|
|
|
|
challengeStatus = status;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await handler.load();
|
|
|
|
await handler.onOnline();
|
|
|
|
return handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isInStorage = (messageId: string) => {
|
|
|
|
return (storage.get('challenge:retry-message-ids') || []).some(
|
|
|
|
({ messageId: storageId }: { messageId: string }) => {
|
|
|
|
return storageId === messageId;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should automatically retry after timeout', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
|
|
|
const one = createMessage('1');
|
|
|
|
messageStorage.set('1', one);
|
|
|
|
|
|
|
|
await handler.register(one);
|
|
|
|
assert.isTrue(isInStorage(one.id));
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.deepEqual(sent, ['1']);
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
assert.isFalse(isInStorage(one.id));
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should send challenge response', async function test() {
|
2021-05-07 20:59:46 +00:00
|
|
|
const handler = await createHandler({ autoSolve: true });
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-05-07 16:10:40 +00:00
|
|
|
const one = createMessage('1', { retryAfter: NEVER_RETRY });
|
2021-05-06 00:09:29 +00:00
|
|
|
messageStorage.set('1', one);
|
|
|
|
|
|
|
|
await handler.register(one);
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.deepEqual(sent, ['1']);
|
|
|
|
assert.isFalse(isInStorage(one.id));
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should send old messages', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
|
|
|
// Put messages in reverse order to validate that the send order is correct
|
|
|
|
const messages = [
|
2021-05-07 20:59:46 +00:00
|
|
|
createMessage('3', { sentAt: 3 }),
|
|
|
|
createMessage('2', { sentAt: 2 }),
|
|
|
|
createMessage('1', { sentAt: 1 }),
|
2021-05-06 00:09:29 +00:00
|
|
|
];
|
|
|
|
for (const message of messages) {
|
|
|
|
messageStorage.set(message.id, message);
|
|
|
|
await handler.register(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
assert.isTrue(
|
|
|
|
isInStorage(message.id),
|
|
|
|
`${message.id} should be in storage`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await handler.onOffline();
|
|
|
|
|
|
|
|
// Wait for messages to mature
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
// Create new handler to load old messages from storage
|
|
|
|
await createHandler();
|
|
|
|
for (const message of messages) {
|
|
|
|
await handler.unregister(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
assert.isFalse(
|
|
|
|
isInStorage(message.id),
|
|
|
|
`${message.id} should not be in storage`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The order has to be correct
|
|
|
|
assert.deepEqual(sent, ['1', '2', '3']);
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should send message immediately if it is ready', async () => {
|
|
|
|
const handler = await createHandler();
|
|
|
|
|
2021-05-07 20:59:46 +00:00
|
|
|
const one = createMessage('1', { retryAfter: IMMEDIATE_RETRY });
|
2021-05-06 00:09:29 +00:00
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
assert.deepEqual(sent, ['1']);
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should not change challenge status on non-bubble messages', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2021-05-07 20:59:46 +00:00
|
|
|
const one = createMessage('1', {
|
|
|
|
isNormalBubble: false,
|
|
|
|
});
|
2021-05-06 00:09:29 +00:00
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.deepEqual(sent, ['1']);
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should not retry expired messages', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
|
|
|
const bubble = createMessage('1');
|
|
|
|
messageStorage.set('1', bubble);
|
|
|
|
await handler.register(bubble);
|
|
|
|
assert.isTrue(isInStorage(bubble.id));
|
|
|
|
|
|
|
|
const newHandler = await createHandler({
|
2021-05-07 20:59:46 +00:00
|
|
|
autoSolve: true,
|
2021-05-06 00:09:29 +00:00
|
|
|
expireAfter: -1,
|
|
|
|
});
|
|
|
|
await handler.unregister(bubble);
|
|
|
|
|
|
|
|
challengeStatus = 'idle';
|
|
|
|
await newHandler.load();
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.isFalse(isInStorage(bubble.id));
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should send messages that matured while we were offline', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
|
|
|
const one = createMessage('1');
|
|
|
|
messageStorage.set('1', one);
|
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
assert.isTrue(isInStorage(one.id));
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
|
|
|
await handler.onOffline();
|
|
|
|
|
|
|
|
// Let messages mature
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.isTrue(isInStorage(one.id));
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
|
|
|
// Go back online
|
|
|
|
await handler.onOnline();
|
|
|
|
|
|
|
|
assert.isFalse(isInStorage(one.id));
|
|
|
|
assert.deepEqual(sent, [one.id]);
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should not retry more than 5 times', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2021-05-07 22:36:28 +00:00
|
|
|
const one = createMessage('1', { retryAfter: IMMEDIATE_RETRY });
|
|
|
|
const retrySend = sinon.stub(one, 'retrySend');
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
messageStorage.set('1', one);
|
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
assert.isTrue(isInStorage(one.id));
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
// Wait more than 5 times
|
|
|
|
for (let i = 0; i < 6; i += 1) {
|
|
|
|
await this.clock.nextAsync();
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.isTrue(isInStorage(one.id));
|
|
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
|
|
|
sinon.assert.callCount(retrySend, 5);
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should trigger onChallengeSolved', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const onChallengeSolved = sinon.stub();
|
|
|
|
|
|
|
|
const handler = await createHandler({
|
2021-05-07 20:59:46 +00:00
|
|
|
autoSolve: true,
|
2021-05-06 00:09:29 +00:00
|
|
|
onChallengeSolved,
|
|
|
|
});
|
|
|
|
|
2021-05-07 20:59:46 +00:00
|
|
|
const one = createMessage('1', { retryAfter: NEVER_RETRY });
|
2021-05-06 00:09:29 +00:00
|
|
|
messageStorage.set('1', one);
|
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
// Let the challenge go through
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
sinon.assert.calledOnce(onChallengeSolved);
|
|
|
|
});
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
it('should trigger onChallengeFailed', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const onChallengeFailed = sinon.stub();
|
|
|
|
|
|
|
|
const handler = await createHandler({
|
2021-05-07 20:59:46 +00:00
|
|
|
autoSolve: true,
|
2021-05-06 00:09:29 +00:00
|
|
|
challengeError: new Error('custom failure'),
|
|
|
|
onChallengeFailed,
|
|
|
|
});
|
|
|
|
|
2021-05-07 20:59:46 +00:00
|
|
|
const one = createMessage('1', { retryAfter: NEVER_RETRY });
|
2021-05-06 00:09:29 +00:00
|
|
|
messageStorage.set('1', one);
|
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
// Let the challenge go through
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
sinon.assert.calledOnce(onChallengeFailed);
|
|
|
|
});
|
|
|
|
});
|