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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixEvent } from 'matrix-js-sdk';
|
|
|
|
|
2021-10-23 01:23:32 +03:00
|
|
|
import { DecryptionFailure, DecryptionFailureTracker } from '../src/DecryptionFailureTracker';
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
class MockDecryptionError extends Error {
|
|
|
|
constructor(code) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.code = code || 'MOCK_DECRYPTION_ERROR';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:33:07 +03:00
|
|
|
function createFailedDecryptionEvent() {
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
event_id: "event-id-" + Math.random().toString(16).slice(2),
|
|
|
|
});
|
2021-06-17 17:23:29 +03:00
|
|
|
event.setClearData(event.badEncryptedMessage(":("));
|
2018-06-15 15:33:07 +03:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:29 +03:00
|
|
|
describe('DecryptionFailureTracker', function() {
|
2018-06-15 15:33:07 +03:00
|
|
|
it('tracks a failed decryption', function(done) {
|
|
|
|
const failedDecryptionEvent = createFailedDecryptionEvent();
|
2018-06-28 17:03:47 +03:00
|
|
|
|
|
|
|
let count = 0;
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
const err = new MockDecryptionError();
|
|
|
|
tracker.eventDecrypted(failedDecryptionEvent, err);
|
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
|
|
|
|
2018-12-25 05:13:09 +03:00
|
|
|
expect(count).not.toBe(0, 'should track a failure for an event that failed decryption');
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not track a failed decryption where the event is subsequently successfully decrypted', (done) => {
|
|
|
|
const decryptedEvent = createFailedDecryptionEvent();
|
2018-06-28 17:03:47 +03:00
|
|
|
const tracker = new DecryptionFailureTracker((total) => {
|
2018-06-15 15:33:07 +03:00
|
|
|
expect(true).toBe(false, 'should not track an event that has since been decrypted correctly');
|
2021-12-06 13:43:42 +03:00
|
|
|
}, () => "UnknownError");
|
2018-06-15 15:33:07 +03:00
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
const err = new MockDecryptionError();
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
// Indicate successful decryption: clear data can be anything where the msgtype is not m.bad.encrypted
|
2021-06-17 17:23:29 +03:00
|
|
|
decryptedEvent.setClearData({});
|
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
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only tracks a single failure per event, despite multiple failed decryptions for multiple events', (done) => {
|
|
|
|
const decryptedEvent = createFailedDecryptionEvent();
|
|
|
|
const decryptedEvent2 = createFailedDecryptionEvent();
|
|
|
|
|
|
|
|
let count = 0;
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
|
2018-06-15 15:33:07 +03:00
|
|
|
|
|
|
|
// Arbitrary number of failed decryptions for both events
|
2018-07-05 15:54:44 +03:00
|
|
|
const err = new MockDecryptionError();
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2, err);
|
|
|
|
tracker.eventDecrypted(decryptedEvent2, err);
|
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
|
|
|
|
|
|
|
expect(count).toBe(2, count + ' failures tracked, should only track a single failure per event');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-06-15 16:48:20 +03:00
|
|
|
it('should not track a failure for an event that was tracked previously', (done) => {
|
|
|
|
const decryptedEvent = createFailedDecryptionEvent();
|
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
let count = 0;
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
|
2018-06-15 16:48:20 +03:00
|
|
|
|
|
|
|
// Indicate decryption
|
2018-07-05 15:54:44 +03:00
|
|
|
const err = new MockDecryptionError();
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
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
|
2018-07-05 15:54:44 +03:00
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
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
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
expect(count).toBe(1, 'should only track a single failure per event');
|
2018-06-15 16:48:20 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-06-15 17:26:53 +03:00
|
|
|
|
2018-06-15 19:08:11 +03:00
|
|
|
xit('should not track a failure for an event that was tracked in a previous session', (done) => {
|
2018-06-15 17:26:53 +03:00
|
|
|
// This test uses localStorage, clear it beforehand
|
|
|
|
localStorage.clear();
|
|
|
|
|
|
|
|
const decryptedEvent = createFailedDecryptionEvent();
|
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
let count = 0;
|
2021-12-06 13:43:42 +03:00
|
|
|
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
|
2018-06-15 17:26:53 +03:00
|
|
|
|
|
|
|
// Indicate decryption
|
2018-07-05 15:54:44 +03:00
|
|
|
const err = new MockDecryptionError();
|
|
|
|
tracker.eventDecrypted(decryptedEvent, err);
|
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
|
2021-12-06 13:43:42 +03:00
|
|
|
const secondTracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
|
2018-06-15 19:08:11 +03:00
|
|
|
|
|
|
|
//secondTracker.loadTrackedEventHashMap();
|
|
|
|
|
2018-07-05 15:54:44 +03:00
|
|
|
secondTracker.eventDecrypted(decryptedEvent, err);
|
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
|
|
|
|
2018-06-28 17:03:47 +03:00
|
|
|
expect(count).toBe(1, count + ' failures tracked, should only track a single failure per event');
|
2018-06-15 17:26:53 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-07-05 15:54:44 +03:00
|
|
|
|
|
|
|
it('should count different error codes separately for multiple failures with different error codes', () => {
|
|
|
|
const counts = {};
|
|
|
|
const tracker = new DecryptionFailureTracker(
|
|
|
|
(total, errorCode) => counts[errorCode] = (counts[errorCode] || 0) + total,
|
2021-12-06 13:43:42 +03:00
|
|
|
(error) => error === "UnknownError" ? "UnknownError" : "OlmKeysNotSentError",
|
2018-07-05 15:54:44 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
|
2021-12-06 13:43:42 +03:00
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id1', 'UnknownError'));
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'OlmKeysNotSentError'));
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'OlmKeysNotSentError'));
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id3', 'OlmKeysNotSentError'));
|
2018-07-05 15:54:44 +03:00
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2021-12-06 13:43:42 +03:00
|
|
|
expect(counts['UnknownError']).toBe(1, 'should track one UnknownError');
|
|
|
|
expect(counts['OlmKeysNotSentError']).toBe(2, 'should track two OlmKeysNotSentError');
|
2018-07-05 15:54:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should map error codes correctly', () => {
|
|
|
|
const counts = {};
|
|
|
|
const tracker = new DecryptionFailureTracker(
|
|
|
|
(total, errorCode) => counts[errorCode] = (counts[errorCode] || 0) + total,
|
2021-12-06 13:43:42 +03:00
|
|
|
(errorCode) => 'OlmUnspecifiedError',
|
2018-07-05 15:54:44 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id1', 'ERROR_CODE_1'));
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'ERROR_CODE_2'));
|
|
|
|
tracker.addDecryptionFailure(new DecryptionFailure('$event_id3', 'ERROR_CODE_3'));
|
|
|
|
|
|
|
|
// Pretend "now" is Infinity
|
|
|
|
tracker.checkFailures(Infinity);
|
|
|
|
|
|
|
|
tracker.trackFailures();
|
|
|
|
|
2021-12-06 13:43:42 +03:00
|
|
|
expect(counts['OlmUnspecifiedError'])
|
|
|
|
.toBe(3, 'should track three OlmUnspecifiedError, got ' + counts['OlmUnspecifiedError']);
|
2018-07-05 15:54:44 +03:00
|
|
|
});
|
2018-06-15 15:33:07 +03:00
|
|
|
});
|