2018-06-15 15:33:07 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
import { decryptExistingEvent, mkDecryptionFailureMatrixEvent } from "matrix-js-sdk/src/testing";
|
|
|
|
import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
import { DecryptionFailureTracker } from "../src/DecryptionFailureTracker";
|
2021-10-23 01:23:32 +03:00
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
async function createFailedDecryptionEvent(code?: DecryptionFailureCode) {
|
2024-03-22 18:48:29 +03:00
|
|
|
return await mkDecryptionFailureMatrixEvent({
|
|
|
|
roomId: "!room:id",
|
|
|
|
sender: "@alice:example.com",
|
2024-04-17 15:36:01 +03:00
|
|
|
code: code ?? DecryptionFailureCode.UNKNOWN_ERROR,
|
2024-03-22 18:48:29 +03:00
|
|
|
msg: ":(",
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:29 +03:00
|
|
|
describe("DecryptionFailureTracker", function () {
|
2024-03-22 18:48:29 +03:00
|
|
|
it("tracks a failed decryption for a visible event", async function () {
|
|
|
|
const failedDecryptionEvent = await createFailedDecryptionEvent();
|
2018-06-28 17:03:47 +03:00
|
|
|
|
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2021-12-06 13:43:42 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(failedDecryptionEvent);
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(failedDecryptionEvent);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should track a failure for an event that failed decryption
|
|
|
|
expect(count).not.toBe(0);
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("tracks a failed decryption with expected raw error for a visible event", async function () {
|
2024-04-17 15:36:01 +03:00
|
|
|
const failedDecryptionEvent = await createFailedDecryptionEvent(
|
|
|
|
DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX,
|
|
|
|
);
|
2022-05-05 06:46:03 +03:00
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
let reportedRawCode = "";
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2022-05-05 06:46:03 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number, _errCode: string, rawCode: string) => {
|
2022-05-05 06:46:03 +03:00
|
|
|
count += total;
|
|
|
|
reportedRawCode = rawCode;
|
|
|
|
},
|
|
|
|
() => "UnknownError",
|
|
|
|
);
|
|
|
|
|
|
|
|
tracker.addVisibleEvent(failedDecryptionEvent);
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(failedDecryptionEvent);
|
2022-05-05 06:46:03 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should track a failure for an event that failed decryption
|
|
|
|
expect(count).not.toBe(0);
|
|
|
|
|
|
|
|
// Should add the rawCode to the event context
|
2024-04-17 15:36:01 +03:00
|
|
|
expect(reportedRawCode).toBe("OLM_UNKNOWN_MESSAGE_INDEX");
|
2022-05-05 06:46:03 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("tracks a failed decryption for an event that becomes visible later", async function () {
|
|
|
|
const failedDecryptionEvent = await createFailedDecryptionEvent();
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2022-01-19 22:31:43 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2022-01-19 22:31:43 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(failedDecryptionEvent);
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(failedDecryptionEvent);
|
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should track a failure for an event that failed decryption
|
|
|
|
expect(count).not.toBe(0);
|
2022-01-19 22:31:43 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("does not track a failed decryption for an event that never becomes visible", async function () {
|
|
|
|
const failedDecryptionEvent = await createFailedDecryptionEvent();
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2022-01-19 22:31:43 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2022-01-19 22:31:43 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(failedDecryptionEvent);
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should not track a failure for an event that never became visible
|
|
|
|
expect(count).toBe(0);
|
2022-01-19 22:31:43 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("does not track a failed decryption where the event is subsequently successfully decrypted", async () => {
|
|
|
|
const decryptedEvent = await createFailedDecryptionEvent();
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2018-06-28 17:03:47 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(_total: number) => {
|
|
|
|
// should not track an event that has since been decrypted correctly
|
|
|
|
expect(true).toBe(false);
|
2021-12-06 13:43:42 +03:00
|
|
|
},
|
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// Indicate successful decryption.
|
|
|
|
await decryptExistingEvent(decryptedEvent, {
|
|
|
|
plainType: "m.room.message",
|
|
|
|
plainContent: { body: "success" },
|
|
|
|
});
|
2018-07-05 15:54:44 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent, null);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
it(
|
|
|
|
"does not track a failed decryption where the event is subsequently successfully decrypted " +
|
|
|
|
"and later becomes visible",
|
2024-03-22 18:48:29 +03:00
|
|
|
async () => {
|
|
|
|
const decryptedEvent = await createFailedDecryptionEvent();
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2022-01-19 22:31:43 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(_total: number) => {
|
|
|
|
// should not track an event that has since been decrypted correctly
|
|
|
|
expect(true).toBe(false);
|
2022-01-19 22:31:43 +03:00
|
|
|
},
|
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// Indicate successful decryption.
|
|
|
|
await decryptExistingEvent(decryptedEvent, {
|
|
|
|
plainType: "m.room.message",
|
|
|
|
plainContent: { body: "success" },
|
|
|
|
});
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
// Immediately track the newest failures
|
|
|
|
tracker.trackFailures();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("only tracks a single failure per event, despite multiple failed decryptions for multiple events", async () => {
|
|
|
|
const decryptedEvent = await createFailedDecryptionEvent();
|
|
|
|
const decryptedEvent2 = await createFailedDecryptionEvent();
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2021-12-06 13:43:42 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
|
|
|
|
2018-06-15 15:33:07 +03:00
|
|
|
// Arbitrary number of failed decryptions for both events
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent2);
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
// Simulated polling of `trackFailures`, an arbitrary number ( > 2 ) times
|
|
|
|
tracker.trackFailures();
|
|
|
|
tracker.trackFailures();
|
|
|
|
tracker.trackFailures();
|
|
|
|
tracker.trackFailures();
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should only track a single failure per event
|
|
|
|
expect(count).toBe(2);
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("should not track a failure for an event that was tracked previously", async () => {
|
|
|
|
const decryptedEvent = await createFailedDecryptionEvent();
|
2018-06-15 16:48:20 +03:00
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2021-12-06 13:43:42 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 16:48:20 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
|
|
|
|
2018-06-15 16:48:20 +03:00
|
|
|
// Indicate decryption
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2018-06-15 16:48:20 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
tracker.trackFailures();
|
2018-06-15 16:48:20 +03:00
|
|
|
|
|
|
|
// Indicate a second decryption, after having tracked the failure
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2018-06-15 16:48:20 +03:00
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
tracker.trackFailures();
|
2018-06-15 16:48:20 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should only track a single failure per event
|
|
|
|
expect(count).toBe(1);
|
2018-06-15 16:48:20 +03:00
|
|
|
});
|
2018-06-15 17:26:53 +03:00
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it.skip("should not track a failure for an event that was tracked in a previous session", async () => {
|
2018-06-15 17:26:53 +03:00
|
|
|
// This test uses localStorage, clear it beforehand
|
|
|
|
localStorage.clear();
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
const decryptedEvent = await createFailedDecryptionEvent();
|
2018-06-15 17:26:53 +03:00
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
let count = 0;
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2021-12-06 13:43:42 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 17:26:53 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
|
|
|
|
2018-06-15 17:26:53 +03:00
|
|
|
// Indicate decryption
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2018-06-15 17:26:53 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
// NB: This saves to localStorage specific to DFT
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
tracker.trackFailures();
|
2018-06-15 17:26:53 +03:00
|
|
|
|
|
|
|
// Simulate the browser refreshing by destroying tracker and creating a new tracker
|
2024-03-25 22:57:29 +03:00
|
|
|
// @ts-ignore access to private constructor
|
2021-12-06 13:43:42 +03:00
|
|
|
const secondTracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number) => (count += total),
|
2021-12-06 13:43:42 +03:00
|
|
|
() => "UnknownError",
|
|
|
|
);
|
2018-06-15 19:08:11 +03:00
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
secondTracker.addVisibleEvent(decryptedEvent);
|
|
|
|
|
|
|
|
//secondTracker.loadTrackedEvents();
|
2018-06-15 19:08:11 +03:00
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
secondTracker.eventDecrypted(decryptedEvent);
|
2018-06-15 17:26:53 +03:00
|
|
|
secondTracker.checkFailures(Infinity);
|
2018-07-05 15:54:44 +03:00
|
|
|
secondTracker.trackFailures();
|
2018-06-15 17:26:53 +03:00
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should only track a single failure per event
|
|
|
|
expect(count).toBe(1);
|
2018-06-15 17:26:53 +03:00
|
|
|
});
|
2018-07-05 15:54:44 +03:00
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("should count different error codes separately for multiple failures with different error codes", async () => {
|
2024-03-25 22:57:29 +03:00
|
|
|
const counts: Record<string, number> = {};
|
|
|
|
|
|
|
|
// @ts-ignore access to private constructor
|
2018-07-05 15:54:44 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number, errorCode: string) => (counts[errorCode] = (counts[errorCode] || 0) + total),
|
2024-04-17 15:36:01 +03:00
|
|
|
(error: DecryptionFailureCode) =>
|
|
|
|
error === DecryptionFailureCode.UNKNOWN_ERROR ? "UnknownError" : "OlmKeysNotSentError",
|
2018-07-05 15:54:44 +03:00
|
|
|
);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
const decryptedEvent1 = await createFailedDecryptionEvent(DecryptionFailureCode.UNKNOWN_ERROR);
|
|
|
|
const decryptedEvent2 = await createFailedDecryptionEvent(
|
|
|
|
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
|
|
|
|
);
|
|
|
|
const decryptedEvent3 = await createFailedDecryptionEvent(
|
|
|
|
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
|
|
|
|
);
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
tracker.addVisibleEvent(decryptedEvent1);
|
|
|
|
tracker.addVisibleEvent(decryptedEvent2);
|
|
|
|
tracker.addVisibleEvent(decryptedEvent3);
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent1);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
|
|
|
tracker.eventDecrypted(decryptedEvent3);
|
2018-07-05 15:54:44 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2022-01-19 22:31:43 +03:00
|
|
|
//expect(counts['UnknownError']).toBe(1, 'should track one UnknownError');
|
2024-03-25 22:57:29 +03:00
|
|
|
expect(counts["OlmKeysNotSentError"]).toBe(2);
|
2018-07-05 15:54:44 +03:00
|
|
|
});
|
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("should aggregate error codes correctly", async () => {
|
2024-03-25 22:57:29 +03:00
|
|
|
const counts: Record<string, number> = {};
|
|
|
|
|
|
|
|
// @ts-ignore access to private constructor
|
2018-07-05 15:54:44 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number, errorCode: string) => (counts[errorCode] = (counts[errorCode] || 0) + total),
|
|
|
|
(_errorCode: string) => "OlmUnspecifiedError",
|
2018-07-05 15:54:44 +03:00
|
|
|
);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
const decryptedEvent1 = await createFailedDecryptionEvent(
|
|
|
|
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
|
|
|
|
);
|
|
|
|
const decryptedEvent2 = await createFailedDecryptionEvent(DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX);
|
|
|
|
const decryptedEvent3 = await createFailedDecryptionEvent(DecryptionFailureCode.UNKNOWN_ERROR);
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
tracker.addVisibleEvent(decryptedEvent1);
|
|
|
|
tracker.addVisibleEvent(decryptedEvent2);
|
|
|
|
tracker.addVisibleEvent(decryptedEvent3);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent1);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2);
|
|
|
|
tracker.eventDecrypted(decryptedEvent3);
|
2018-07-05 15:54:44 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
expect(counts["OlmUnspecifiedError"]).toBe(3);
|
2018-07-05 15:54:44 +03:00
|
|
|
});
|
2022-01-19 22:31:43 +03:00
|
|
|
|
2024-03-22 18:48:29 +03:00
|
|
|
it("should remap error codes correctly", async () => {
|
2024-03-25 22:57:29 +03:00
|
|
|
const counts: Record<string, number> = {};
|
|
|
|
|
|
|
|
// @ts-ignore access to private constructor
|
2022-01-19 22:31:43 +03:00
|
|
|
const tracker = new DecryptionFailureTracker(
|
2024-03-25 22:57:29 +03:00
|
|
|
(total: number, errorCode: string) => (counts[errorCode] = (counts[errorCode] || 0) + total),
|
|
|
|
(errorCode: string) => Array.from(errorCode).reverse().join(""),
|
2022-01-19 22:31:43 +03:00
|
|
|
);
|
|
|
|
|
2024-04-17 15:36:01 +03:00
|
|
|
const decryptedEvent = await createFailedDecryptionEvent(DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX);
|
2022-01-19 22:31:43 +03:00
|
|
|
tracker.addVisibleEvent(decryptedEvent);
|
2024-04-17 15:36:01 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent);
|
2022-01-19 22:31:43 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2024-03-25 22:57:29 +03:00
|
|
|
// should track remapped error code
|
2024-04-17 15:36:01 +03:00
|
|
|
expect(counts["XEDNI_EGASSEM_NWONKNU_MLO"]).toBe(1);
|
2022-01-19 22:31:43 +03:00
|
|
|
});
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|