@@ -16,10 +16,18 @@ limitations under the License.
16
16
17
17
import expect from 'expect' ;
18
18
19
- import DecryptionFailureTracker from '../src/DecryptionFailureTracker' ;
19
+ import { DecryptionFailure , DecryptionFailureTracker } from '../src/DecryptionFailureTracker' ;
20
20
21
21
import { MatrixEvent } from 'matrix-js-sdk' ;
22
22
23
+ class MockDecryptionError extends Error {
24
+ constructor ( code ) {
25
+ super ( ) ;
26
+
27
+ this . code = code || 'MOCK_DECRYPTION_ERROR' ;
28
+ }
29
+ }
30
+
23
31
function createFailedDecryptionEvent ( ) {
24
32
const event = new MatrixEvent ( {
25
33
event_id : "event-id-" + Math . random ( ) . toString ( 16 ) . slice ( 2 ) ,
@@ -37,13 +45,14 @@ describe('DecryptionFailureTracker', function() {
37
45
let count = 0 ;
38
46
const tracker = new DecryptionFailureTracker ( ( total ) => count += total ) ;
39
47
40
- tracker . eventDecrypted ( failedDecryptionEvent ) ;
48
+ const err = new MockDecryptionError ( ) ;
49
+ tracker . eventDecrypted ( failedDecryptionEvent , err ) ;
41
50
42
51
// Pretend "now" is Infinity
43
52
tracker . checkFailures ( Infinity ) ;
44
53
45
- // Immediately track the newest failure, if there is one
46
- tracker . trackFailure ( ) ;
54
+ // Immediately track the newest failures
55
+ tracker . trackFailures ( ) ;
47
56
48
57
expect ( count ) . toNotBe ( 0 , 'should track a failure for an event that failed decryption' ) ;
49
58
@@ -56,17 +65,18 @@ describe('DecryptionFailureTracker', function() {
56
65
expect ( true ) . toBe ( false , 'should not track an event that has since been decrypted correctly' ) ;
57
66
} ) ;
58
67
59
- tracker . eventDecrypted ( decryptedEvent ) ;
68
+ const err = new MockDecryptionError ( ) ;
69
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
60
70
61
71
// Indicate successful decryption: clear data can be anything where the msgtype is not m.bad.encrypted
62
72
decryptedEvent . _setClearData ( { } ) ;
63
- tracker . eventDecrypted ( decryptedEvent ) ;
73
+ tracker . eventDecrypted ( decryptedEvent , null ) ;
64
74
65
75
// Pretend "now" is Infinity
66
76
tracker . checkFailures ( Infinity ) ;
67
77
68
- // Immediately track the newest failure, if there is one
69
- tracker . trackFailure ( ) ;
78
+ // Immediately track the newest failures
79
+ tracker . trackFailures ( ) ;
70
80
done ( ) ;
71
81
} ) ;
72
82
@@ -78,23 +88,24 @@ describe('DecryptionFailureTracker', function() {
78
88
const tracker = new DecryptionFailureTracker ( ( total ) => count += total ) ;
79
89
80
90
// Arbitrary number of failed decryptions for both events
81
- tracker . eventDecrypted ( decryptedEvent ) ;
82
- tracker . eventDecrypted ( decryptedEvent ) ;
83
- tracker . eventDecrypted ( decryptedEvent ) ;
84
- tracker . eventDecrypted ( decryptedEvent ) ;
85
- tracker . eventDecrypted ( decryptedEvent ) ;
86
- tracker . eventDecrypted ( decryptedEvent2 ) ;
87
- tracker . eventDecrypted ( decryptedEvent2 ) ;
88
- tracker . eventDecrypted ( decryptedEvent2 ) ;
91
+ const err = new MockDecryptionError ( ) ;
92
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
93
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
94
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
95
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
96
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
97
+ tracker . eventDecrypted ( decryptedEvent2 , err ) ;
98
+ tracker . eventDecrypted ( decryptedEvent2 , err ) ;
99
+ tracker . eventDecrypted ( decryptedEvent2 , err ) ;
89
100
90
101
// Pretend "now" is Infinity
91
102
tracker . checkFailures ( Infinity ) ;
92
103
93
- // Simulated polling of `trackFailure `, an arbitrary number ( > 2 ) times
94
- tracker . trackFailure ( ) ;
95
- tracker . trackFailure ( ) ;
96
- tracker . trackFailure ( ) ;
97
- tracker . trackFailure ( ) ;
104
+ // Simulated polling of `trackFailures `, an arbitrary number ( > 2 ) times
105
+ tracker . trackFailures ( ) ;
106
+ tracker . trackFailures ( ) ;
107
+ tracker . trackFailures ( ) ;
108
+ tracker . trackFailures ( ) ;
98
109
99
110
expect ( count ) . toBe ( 2 , count + ' failures tracked, should only track a single failure per event' ) ;
100
111
@@ -108,17 +119,18 @@ describe('DecryptionFailureTracker', function() {
108
119
const tracker = new DecryptionFailureTracker ( ( total ) => count += total ) ;
109
120
110
121
// Indicate decryption
111
- tracker . eventDecrypted ( decryptedEvent ) ;
122
+ const err = new MockDecryptionError ( ) ;
123
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
112
124
113
125
// Pretend "now" is Infinity
114
126
tracker . checkFailures ( Infinity ) ;
115
127
116
- tracker . trackFailure ( ) ;
128
+ tracker . trackFailures ( ) ;
117
129
118
130
// Indicate a second decryption, after having tracked the failure
119
- tracker . eventDecrypted ( decryptedEvent ) ;
131
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
120
132
121
- tracker . trackFailure ( ) ;
133
+ tracker . trackFailures ( ) ;
122
134
123
135
expect ( count ) . toBe ( 1 , 'should only track a single failure per event' ) ;
124
136
@@ -135,25 +147,68 @@ describe('DecryptionFailureTracker', function() {
135
147
const tracker = new DecryptionFailureTracker ( ( total ) => count += total ) ;
136
148
137
149
// Indicate decryption
138
- tracker . eventDecrypted ( decryptedEvent ) ;
150
+ const err = new MockDecryptionError ( ) ;
151
+ tracker . eventDecrypted ( decryptedEvent , err ) ;
139
152
140
153
// Pretend "now" is Infinity
141
154
// NB: This saves to localStorage specific to DFT
142
155
tracker . checkFailures ( Infinity ) ;
143
156
144
- tracker . trackFailure ( ) ;
157
+ tracker . trackFailures ( ) ;
145
158
146
159
// Simulate the browser refreshing by destroying tracker and creating a new tracker
147
160
const secondTracker = new DecryptionFailureTracker ( ( total ) => count += total ) ;
148
161
149
162
//secondTracker.loadTrackedEventHashMap();
150
163
151
- secondTracker . eventDecrypted ( decryptedEvent ) ;
164
+ secondTracker . eventDecrypted ( decryptedEvent , err ) ;
152
165
secondTracker . checkFailures ( Infinity ) ;
153
- secondTracker . trackFailure ( ) ;
166
+ secondTracker . trackFailures ( ) ;
154
167
155
168
expect ( count ) . toBe ( 1 , count + ' failures tracked, should only track a single failure per event' ) ;
156
169
157
170
done ( ) ;
158
171
} ) ;
172
+
173
+ it ( 'should count different error codes separately for multiple failures with different error codes' , ( ) => {
174
+ const counts = { } ;
175
+ const tracker = new DecryptionFailureTracker (
176
+ ( total , errorCode ) => counts [ errorCode ] = ( counts [ errorCode ] || 0 ) + total ,
177
+ ) ;
178
+
179
+ // One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
180
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id1' , 'ERROR_CODE_1' ) ) ;
181
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id2' , 'ERROR_CODE_2' ) ) ;
182
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id2' , 'ERROR_CODE_2' ) ) ;
183
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id3' , 'ERROR_CODE_2' ) ) ;
184
+
185
+ // Pretend "now" is Infinity
186
+ tracker . checkFailures ( Infinity ) ;
187
+
188
+ tracker . trackFailures ( ) ;
189
+
190
+ expect ( counts [ 'ERROR_CODE_1' ] ) . toBe ( 1 , 'should track one ERROR_CODE_1' ) ;
191
+ expect ( counts [ 'ERROR_CODE_2' ] ) . toBe ( 2 , 'should track two ERROR_CODE_2' ) ;
192
+ } ) ;
193
+
194
+ it ( 'should map error codes correctly' , ( ) => {
195
+ const counts = { } ;
196
+ const tracker = new DecryptionFailureTracker (
197
+ ( total , errorCode ) => counts [ errorCode ] = ( counts [ errorCode ] || 0 ) + total ,
198
+ ( errorCode ) => 'MY_NEW_ERROR_CODE' ,
199
+ ) ;
200
+
201
+ // One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
202
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id1' , 'ERROR_CODE_1' ) ) ;
203
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id2' , 'ERROR_CODE_2' ) ) ;
204
+ tracker . addDecryptionFailure ( new DecryptionFailure ( '$event_id3' , 'ERROR_CODE_3' ) ) ;
205
+
206
+ // Pretend "now" is Infinity
207
+ tracker . checkFailures ( Infinity ) ;
208
+
209
+ tracker . trackFailures ( ) ;
210
+
211
+ expect ( counts [ 'MY_NEW_ERROR_CODE' ] )
212
+ . toBe ( 3 , 'should track three MY_NEW_ERROR_CODE, got ' + counts [ 'MY_NEW_ERROR_CODE' ] ) ;
213
+ } ) ;
159
214
} ) ;
0 commit comments