signal-desktop/ts/test-both/challenge_test.ts

307 lines
7.9 KiB
TypeScript
Raw Normal View History

// 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 */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { assert } from 'chai';
import { noop } from 'lodash';
import * as sinon from 'sinon';
import { STORAGE_KEY, ChallengeHandler } from '../challenge';
import type { RegisteredChallengeType } from '../challenge';
import { DAY, SECOND } from '../util/durations';
type CreateHandlerOptions = {
2021-05-07 20:59:46 +00:00
readonly autoSolve?: boolean;
readonly challengeError?: Error;
readonly expireAfter?: number;
readonly onChallengeSolved?: () => void;
readonly onChallengeFailed?: (retryAt?: number) => void;
};
2021-05-08 00:34:50 +00:00
const NOW = Date.now();
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
describe('ChallengeHandler', () => {
const storage = new Map<string, any>();
let challengeStatus = 'idle';
let queuesStarted: Array<string> = [];
2021-05-08 00:34:50 +00:00
beforeEach(function beforeEach() {
storage.clear();
challengeStatus = 'idle';
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();
});
const createChallenge = (
conversationId: string,
options: Partial<RegisteredChallengeType> = {}
): RegisteredChallengeType => {
return {
conversationId,
token: '1',
retryAt: NOW + DEFAULT_RETRY_AFTER,
createdAt: NOW - SECOND,
2022-09-15 00:31:37 +00:00
reason: 'test',
...options,
};
};
const createHandler = async ({
2021-05-07 20:59:46 +00:00
autoSolve = false,
challengeError,
expireAfter,
onChallengeSolved = noop,
onChallengeFailed = noop,
}: CreateHandlerOptions = {}): Promise<ChallengeHandler> => {
const handler = new ChallengeHandler({
expireAfter,
storage: {
get(key: string) {
return storage.get(key);
},
async put(key: string, value: unknown) {
storage.set(key, value);
},
},
startQueue(conversationId: string) {
queuesStarted.push(conversationId);
},
onChallengeSolved,
onChallengeFailed,
requestChallenge(request) {
2021-05-07 20:59:46 +00:00
if (!autoSolve) {
return;
}
setTimeout(() => {
handler.onResponse({
seq: request.seq,
data: { captcha: 'captcha' },
});
2021-05-07 20:59:46 +00:00
}, SOLVE_AFTER);
},
async sendChallengeResponse() {
if (challengeError) {
throw challengeError;
}
},
setChallengeStatus(status) {
challengeStatus = status;
},
});
await handler.load();
await handler.onOnline();
return handler;
};
const isInStorage = (conversationId: string) => {
return (storage.get(STORAGE_KEY) || []).some(
({ conversationId: storageId }: { conversationId: string }) => {
return storageId === conversationId;
}
);
};
it('should automatically start queue after timeout', async function test() {
const handler = await createHandler();
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.conversationId));
assert.equal(challengeStatus, 'required');
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.equal(challengeStatus, 'idle');
assert.isFalse(isInStorage(one.conversationId));
});
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 });
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
assert.equal(challengeStatus, 'required');
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.isFalse(isInStorage(one.conversationId));
assert.equal(challengeStatus, 'idle');
});
it('should send old challenges', async function test() {
const handler = await createHandler();
const challenges = [
createChallenge('1'),
createChallenge('2'),
createChallenge('3'),
];
for (const challenge of challenges) {
await handler.register(challenge);
}
assert.equal(challengeStatus, 'required');
assert.deepEqual(queuesStarted, []);
for (const challenge of challenges) {
assert.isTrue(
isInStorage(challenge.conversationId),
`${challenge.conversationId} should be in storage`
);
}
await handler.onOffline();
// Wait for challenges to mature
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
// Create new handler to load old challenges from storage; it will start up online
await createHandler();
for (const challenge of challenges) {
await handler.unregister(challenge.conversationId);
}
for (const challenge of challenges) {
assert.isFalse(
isInStorage(challenge.conversationId),
`${challenge.conversationId} should not be in storage`
);
}
// The order has to be correct
assert.deepEqual(queuesStarted, ['1', '2', '3']);
assert.equal(challengeStatus, 'idle');
});
it('should send challenge immediately if it is ready', async () => {
const handler = await createHandler();
const one = createChallenge('1', {
retryAt: IMMEDIATE_RETRY,
2021-05-07 20:59:46 +00:00
});
await handler.register(one);
assert.equal(challengeStatus, 'idle');
assert.deepEqual(queuesStarted, [one.conversationId]);
});
it('should not retry expired challenges', async function test() {
const handler = await createHandler();
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.conversationId));
const newHandler = await createHandler({
2021-05-07 20:59:46 +00:00
autoSolve: true,
expireAfter: -1,
});
await handler.unregister(one.conversationId);
challengeStatus = 'idle';
await newHandler.load();
assert.equal(challengeStatus, 'idle');
assert.deepEqual(queuesStarted, []);
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
assert.equal(challengeStatus, 'idle');
assert.deepEqual(queuesStarted, []);
assert.isFalse(isInStorage(one.conversationId));
});
it('should send challenges that matured while we were offline', async function test() {
const handler = await createHandler();
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, []);
assert.equal(challengeStatus, 'required');
await handler.onOffline();
// Let challenges mature
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
assert.isTrue(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, []);
assert.equal(challengeStatus, 'required');
// Go back online
await handler.onOnline();
assert.isFalse(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.equal(challengeStatus, 'idle');
});
2021-05-08 00:34:50 +00:00
it('should trigger onChallengeSolved', async function test() {
const onChallengeSolved = sinon.stub();
const handler = await createHandler({
2021-05-07 20:59:46 +00:00
autoSolve: true,
onChallengeSolved,
});
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
// Let the challenge go through
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
sinon.assert.calledOnce(onChallengeSolved);
});
2021-05-08 00:34:50 +00:00
it('should trigger onChallengeFailed', async function test() {
const onChallengeFailed = sinon.stub();
const handler = await createHandler({
2021-05-07 20:59:46 +00:00
autoSolve: true,
challengeError: new Error('custom failure'),
onChallengeFailed,
});
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
// Let the challenge go through
2021-05-08 00:34:50 +00:00
await this.clock.nextAsync();
sinon.assert.calledOnce(onChallengeFailed);
});
});