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';
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
import { STORAGE_KEY, ChallengeHandler } from '../challenge';
|
|
|
|
import type { RegisteredChallengeType } from '../challenge';
|
|
|
|
import { DAY, SECOND } from '../util/durations';
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
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;
|
2022-03-21 21:19:37 +00:00
|
|
|
readonly onChallengeFailed?: (retryAt?: number) => void;
|
2021-05-06 00:09:29 +00:00
|
|
|
};
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
const NOW = Date.now();
|
2022-03-21 21:19:37 +00:00
|
|
|
const NEVER_RETRY = NOW + DAY;
|
|
|
|
const IMMEDIATE_RETRY = NOW - 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>();
|
|
|
|
let challengeStatus = 'idle';
|
2022-03-21 21:19:37 +00:00
|
|
|
let queuesStarted: Array<string> = [];
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
beforeEach(function beforeEach() {
|
2021-05-06 00:09:29 +00:00
|
|
|
storage.clear();
|
|
|
|
challengeStatus = 'idle';
|
2022-03-21 21:19:37 +00:00
|
|
|
queuesStarted = [];
|
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
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const createChallenge = (
|
|
|
|
conversationId: string,
|
|
|
|
options: Partial<RegisteredChallengeType> = {}
|
|
|
|
): RegisteredChallengeType => {
|
2021-05-06 00:09:29 +00:00
|
|
|
return {
|
2022-03-21 21:19:37 +00:00
|
|
|
conversationId,
|
|
|
|
token: '1',
|
|
|
|
retryAt: NOW + DEFAULT_RETRY_AFTER,
|
|
|
|
createdAt: NOW - SECOND,
|
2022-09-15 00:31:37 +00:00
|
|
|
reason: 'test',
|
2022-03-21 21:19:37 +00:00
|
|
|
...options,
|
2021-05-06 00:09:29 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
startQueue(conversationId: string) {
|
|
|
|
queuesStarted.push(conversationId);
|
|
|
|
},
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
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 sendChallengeResponse() {
|
|
|
|
if (challengeError) {
|
|
|
|
throw challengeError;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setChallengeStatus(status) {
|
|
|
|
challengeStatus = status;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await handler.load();
|
|
|
|
await handler.onOnline();
|
|
|
|
return handler;
|
|
|
|
};
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const isInStorage = (conversationId: string) => {
|
|
|
|
return (storage.get(STORAGE_KEY) || []).some(
|
|
|
|
({ conversationId: storageId }: { conversationId: string }) => {
|
|
|
|
return storageId === conversationId;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
it('should automatically start queue after timeout', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1');
|
2021-05-06 00:09:29 +00:00
|
|
|
await handler.register(one);
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.isTrue(isInStorage(one.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, [one.conversationId]);
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'idle');
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.isFalse(isInStorage(one.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1', {
|
|
|
|
retryAt: NEVER_RETRY,
|
|
|
|
});
|
2021-05-06 00:09:29 +00:00
|
|
|
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
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, [one.conversationId]);
|
|
|
|
assert.isFalse(isInStorage(one.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
it('should send old challenges', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const challenges = [
|
|
|
|
createChallenge('1'),
|
|
|
|
createChallenge('2'),
|
|
|
|
createChallenge('3'),
|
2021-05-06 00:09:29 +00:00
|
|
|
];
|
2022-03-21 21:19:37 +00:00
|
|
|
for (const challenge of challenges) {
|
|
|
|
await handler.register(challenge);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'required');
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, []);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
for (const challenge of challenges) {
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.isTrue(
|
2022-03-21 21:19:37 +00:00
|
|
|
isInStorage(challenge.conversationId),
|
|
|
|
`${challenge.conversationId} should be in storage`
|
2021-05-06 00:09:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await handler.onOffline();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
// Wait for challenges to mature
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
// Create new handler to load old challenges from storage; it will start up online
|
2021-05-06 00:09:29 +00:00
|
|
|
await createHandler();
|
2022-03-21 21:19:37 +00:00
|
|
|
|
|
|
|
for (const challenge of challenges) {
|
|
|
|
await handler.unregister(challenge.conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
for (const challenge of challenges) {
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.isFalse(
|
2022-03-21 21:19:37 +00:00
|
|
|
isInStorage(challenge.conversationId),
|
|
|
|
`${challenge.conversationId} should not be in storage`
|
2021-05-06 00:09:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The order has to be correct
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, ['1', '2', '3']);
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
it('should send challenge immediately if it is ready', async () => {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1', {
|
|
|
|
retryAt: IMMEDIATE_RETRY,
|
2021-05-07 20:59:46 +00:00
|
|
|
});
|
2021-05-06 00:09:29 +00:00
|
|
|
await handler.register(one);
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, [one.conversationId]);
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
it('should not retry expired challenges', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1');
|
|
|
|
await handler.register(one);
|
|
|
|
assert.isTrue(isInStorage(one.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
const newHandler = await createHandler({
|
2021-05-07 20:59:46 +00:00
|
|
|
autoSolve: true,
|
2021-05-06 00:09:29 +00:00
|
|
|
expireAfter: -1,
|
|
|
|
});
|
2022-03-21 21:19:37 +00:00
|
|
|
await handler.unregister(one.conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
challengeStatus = 'idle';
|
|
|
|
await newHandler.load();
|
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, []);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
assert.equal(challengeStatus, 'idle');
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.deepEqual(queuesStarted, []);
|
|
|
|
assert.isFalse(isInStorage(one.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
it('should send challenges that matured while we were offline', async function test() {
|
2021-05-06 00:09:29 +00:00
|
|
|
const handler = await createHandler();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1');
|
2021-05-06 00:09:29 +00:00
|
|
|
await handler.register(one);
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.isTrue(isInStorage(one.conversationId));
|
|
|
|
assert.deepEqual(queuesStarted, []);
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
|
|
|
await handler.onOffline();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
// Let challenges mature
|
2021-05-08 00:34:50 +00:00
|
|
|
await this.clock.nextAsync();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.isTrue(isInStorage(one.conversationId));
|
|
|
|
assert.deepEqual(queuesStarted, []);
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'required');
|
|
|
|
|
|
|
|
// Go back online
|
|
|
|
await handler.onOnline();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
assert.isFalse(isInStorage(one.conversationId));
|
|
|
|
assert.deepEqual(queuesStarted, [one.conversationId]);
|
2021-05-06 00:09:29 +00:00
|
|
|
assert.equal(challengeStatus, 'idle');
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1', {
|
|
|
|
retryAt: NEVER_RETRY,
|
|
|
|
});
|
2021-05-06 00:09:29 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const one = createChallenge('1', {
|
|
|
|
retryAt: NEVER_RETRY,
|
|
|
|
});
|
2021-05-06 00:09:29 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|