@@ -594,35 +594,48 @@ describe("MatrixRTCSession", () => {
594
594
} ) ;
595
595
596
596
it ( "sends keys when joining" , async ( ) => {
597
- const eventSentPromise = new Promise ( ( resolve ) => {
598
- sendEventMock . mockImplementation ( resolve ) ;
599
- } ) ;
597
+ jest . useFakeTimers ( ) ;
598
+ try {
599
+ const eventSentPromise = new Promise ( ( resolve ) => {
600
+ sendEventMock . mockImplementation ( resolve ) ;
601
+ } ) ;
600
602
601
- sess ! . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
603
+ sess ! . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
602
604
603
- await eventSentPromise ;
605
+ await eventSentPromise ;
604
606
605
- expect ( sendEventMock ) . toHaveBeenCalledWith ( expect . stringMatching ( ".*" ) , "io.element.call.encryption_keys" , {
606
- call_id : "" ,
607
- device_id : "AAAAAAA" ,
608
- keys : [
607
+ expect ( sendEventMock ) . toHaveBeenCalledWith (
608
+ expect . stringMatching ( ".*" ) ,
609
+ "io.element.call.encryption_keys" ,
609
610
{
610
- index : 0 ,
611
- key : expect . stringMatching ( ".*" ) ,
611
+ call_id : "" ,
612
+ device_id : "AAAAAAA" ,
613
+ keys : [
614
+ {
615
+ index : 0 ,
616
+ key : expect . stringMatching ( ".*" ) ,
617
+ } ,
618
+ ] ,
619
+ sent_ts : Date . now ( ) ,
612
620
} ,
613
- ] ,
614
- } ) ;
621
+ ) ;
622
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
623
+ } finally {
624
+ jest . useRealTimers ( ) ;
625
+ }
615
626
} ) ;
616
627
617
628
it ( "does not send key if join called when already joined" , ( ) => {
618
629
sess ! . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
619
630
620
631
expect ( client . sendStateEvent ) . toHaveBeenCalledTimes ( 1 ) ;
621
632
expect ( client . sendEvent ) . toHaveBeenCalledTimes ( 1 ) ;
633
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
622
634
623
635
sess ! . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
624
636
expect ( client . sendStateEvent ) . toHaveBeenCalledTimes ( 1 ) ;
625
637
expect ( client . sendEvent ) . toHaveBeenCalledTimes ( 1 ) ;
638
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
626
639
} ) ;
627
640
628
641
it ( "retries key sends" , async ( ) => {
@@ -651,6 +664,7 @@ describe("MatrixRTCSession", () => {
651
664
await eventSentPromise ;
652
665
653
666
expect ( sendEventMock ) . toHaveBeenCalledTimes ( 2 ) ;
667
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
654
668
} finally {
655
669
jest . useRealTimers ( ) ;
656
670
}
@@ -684,6 +698,7 @@ describe("MatrixRTCSession", () => {
684
698
685
699
sess . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
686
700
await keysSentPromise1 ;
701
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
687
702
688
703
sendEventMock . mockClear ( ) ;
689
704
jest . advanceTimersByTime ( 10000 ) ;
@@ -707,6 +722,7 @@ describe("MatrixRTCSession", () => {
707
722
await keysSentPromise2 ;
708
723
709
724
expect ( sendEventMock ) . toHaveBeenCalled ( ) ;
725
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
710
726
} finally {
711
727
jest . useRealTimers ( ) ;
712
728
}
@@ -747,14 +763,17 @@ describe("MatrixRTCSession", () => {
747
763
key : expect . stringMatching ( ".*" ) ,
748
764
} ,
749
765
] ,
766
+ sent_ts : Date . now ( ) ,
750
767
} ,
751
768
) ;
769
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
752
770
753
771
sendEventMock . mockClear ( ) ;
754
772
755
773
// these should be a no-op:
756
774
sess . onMembershipUpdate ( ) ;
757
775
expect ( sendEventMock ) . toHaveBeenCalledTimes ( 0 ) ;
776
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
758
777
} finally {
759
778
jest . useRealTimers ( ) ;
760
779
}
@@ -796,8 +815,10 @@ describe("MatrixRTCSession", () => {
796
815
key : expect . stringMatching ( ".*" ) ,
797
816
} ,
798
817
] ,
818
+ sent_ts : Date . now ( ) ,
799
819
} ,
800
820
) ;
821
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
801
822
802
823
sendEventMock . mockClear ( ) ;
803
824
@@ -832,8 +853,10 @@ describe("MatrixRTCSession", () => {
832
853
key : expect . stringMatching ( ".*" ) ,
833
854
} ,
834
855
] ,
856
+ sent_ts : Date . now ( ) ,
835
857
} ,
836
858
) ;
859
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
837
860
} finally {
838
861
jest . useRealTimers ( ) ;
839
862
}
@@ -877,8 +900,10 @@ describe("MatrixRTCSession", () => {
877
900
key : expect . stringMatching ( ".*" ) ,
878
901
} ,
879
902
] ,
903
+ sent_ts : Date . now ( ) ,
880
904
} ,
881
905
) ;
906
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
882
907
883
908
sendEventMock . mockClear ( ) ;
884
909
@@ -913,8 +938,10 @@ describe("MatrixRTCSession", () => {
913
938
key : expect . stringMatching ( ".*" ) ,
914
939
} ,
915
940
] ,
941
+ sent_ts : Date . now ( ) ,
916
942
} ,
917
943
) ;
944
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
918
945
} finally {
919
946
jest . useRealTimers ( ) ;
920
947
}
@@ -946,6 +973,8 @@ describe("MatrixRTCSession", () => {
946
973
sess . joinRoomSession ( [ mockFocus ] , mockFocus , { manageMediaKeys : true } ) ;
947
974
const firstKeysPayload = await keysSentPromise1 ;
948
975
expect ( firstKeysPayload . keys ) . toHaveLength ( 1 ) ;
976
+ expect ( firstKeysPayload . keys [ 0 ] . index ) . toEqual ( 0 ) ;
977
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
949
978
950
979
sendEventMock . mockClear ( ) ;
951
980
@@ -962,8 +991,10 @@ describe("MatrixRTCSession", () => {
962
991
963
992
const secondKeysPayload = await keysSentPromise2 ;
964
993
965
- expect ( secondKeysPayload . keys ) . toHaveLength ( 2 ) ;
994
+ expect ( secondKeysPayload . keys ) . toHaveLength ( 1 ) ;
995
+ expect ( secondKeysPayload . keys [ 0 ] . index ) . toEqual ( 1 ) ;
966
996
expect ( onMyEncryptionKeyChanged ) . toHaveBeenCalledTimes ( 2 ) ;
997
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 2 ) ;
967
998
} finally {
968
999
jest . useRealTimers ( ) ;
969
1000
}
@@ -984,6 +1015,7 @@ describe("MatrixRTCSession", () => {
984
1015
await keysSentPromise1 ;
985
1016
986
1017
sendEventMock . mockClear ( ) ;
1018
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
987
1019
988
1020
const onMembershipsChanged = jest . fn ( ) ;
989
1021
sess . on ( MatrixRTCSessionEvent . MembershipsChanged , onMembershipsChanged ) ;
@@ -1002,6 +1034,7 @@ describe("MatrixRTCSession", () => {
1002
1034
} ) ;
1003
1035
1004
1036
expect ( sendEventMock ) . not . toHaveBeenCalled ( ) ;
1037
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysSent ) . toEqual ( 1 ) ;
1005
1038
} finally {
1006
1039
jest . useRealTimers ( ) ;
1007
1040
}
@@ -1167,6 +1200,7 @@ describe("MatrixRTCSession", () => {
1167
1200
const bobKeys = sess . getKeysForParticipant ( "@bob:example.org" , "bobsphone" ) ! ;
1168
1201
expect ( bobKeys ) . toHaveLength ( 1 ) ;
1169
1202
expect ( bobKeys [ 0 ] ) . toEqual ( Buffer . from ( "this is the key" , "utf-8" ) ) ;
1203
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 1 ) ;
1170
1204
} ) ;
1171
1205
1172
1206
it ( "collects keys at non-zero indices" , ( ) => {
@@ -1195,6 +1229,7 @@ describe("MatrixRTCSession", () => {
1195
1229
expect ( bobKeys [ 2 ] ) . toBeFalsy ( ) ;
1196
1230
expect ( bobKeys [ 3 ] ) . toBeFalsy ( ) ;
1197
1231
expect ( bobKeys [ 4 ] ) . toEqual ( Buffer . from ( "this is the key" , "utf-8" ) ) ;
1232
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 1 ) ;
1198
1233
} ) ;
1199
1234
1200
1235
it ( "collects keys by merging" , ( ) => {
@@ -1219,6 +1254,7 @@ describe("MatrixRTCSession", () => {
1219
1254
let bobKeys = sess . getKeysForParticipant ( "@bob:example.org" , "bobsphone" ) ! ;
1220
1255
expect ( bobKeys ) . toHaveLength ( 1 ) ;
1221
1256
expect ( bobKeys [ 0 ] ) . toEqual ( Buffer . from ( "this is the key" , "utf-8" ) ) ;
1257
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 1 ) ;
1222
1258
1223
1259
sess . onCallEncryption ( {
1224
1260
getType : jest . fn ( ) . mockReturnValue ( "io.element.call.encryption_keys" ) ,
@@ -1239,6 +1275,7 @@ describe("MatrixRTCSession", () => {
1239
1275
bobKeys = sess . getKeysForParticipant ( "@bob:example.org" , "bobsphone" ) ! ;
1240
1276
expect ( bobKeys ) . toHaveLength ( 5 ) ;
1241
1277
expect ( bobKeys [ 4 ] ) . toEqual ( Buffer . from ( "this is the key" , "utf-8" ) ) ;
1278
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 2 ) ;
1242
1279
} ) ;
1243
1280
1244
1281
it ( "ignores older keys at same index" , ( ) => {
@@ -1279,6 +1316,7 @@ describe("MatrixRTCSession", () => {
1279
1316
const bobKeys = sess . getKeysForParticipant ( "@bob:example.org" , "bobsphone" ) ! ;
1280
1317
expect ( bobKeys ) . toHaveLength ( 1 ) ;
1281
1318
expect ( bobKeys [ 0 ] ) . toEqual ( Buffer . from ( "newer key" , "utf-8" ) ) ;
1319
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 2 ) ;
1282
1320
} ) ;
1283
1321
1284
1322
it ( "key timestamps are treated as monotonic" , ( ) => {
@@ -1342,5 +1380,73 @@ describe("MatrixRTCSession", () => {
1342
1380
1343
1381
const myKeys = sess . getKeysForParticipant ( client . getUserId ( ) ! , client . getDeviceId ( ) ! ) ! ;
1344
1382
expect ( myKeys ) . toBeFalsy ( ) ;
1383
+ expect ( sess ! . statistics . counters . roomEventEncryptionKeysReceived ) . toEqual ( 0 ) ;
1384
+ } ) ;
1385
+
1386
+ it ( "tracks total age statistics for collected keys" , ( ) => {
1387
+ jest . useFakeTimers ( ) ;
1388
+ try {
1389
+ const mockRoom = makeMockRoom ( [ membershipTemplate ] ) ;
1390
+ sess = MatrixRTCSession . roomSessionForRoom ( client , mockRoom ) ;
1391
+
1392
+ // defaults to getTs()
1393
+ jest . setSystemTime ( 1000 ) ;
1394
+ sess . onCallEncryption ( {
1395
+ getType : jest . fn ( ) . mockReturnValue ( "io.element.call.encryption_keys" ) ,
1396
+ getContent : jest . fn ( ) . mockReturnValue ( {
1397
+ device_id : "bobsphone" ,
1398
+ call_id : "" ,
1399
+ keys : [
1400
+ {
1401
+ index : 0 ,
1402
+ key : "dGhpcyBpcyB0aGUga2V5" ,
1403
+ } ,
1404
+ ] ,
1405
+ } ) ,
1406
+ getSender : jest . fn ( ) . mockReturnValue ( "@bob:example.org" ) ,
1407
+ getTs : jest . fn ( ) . mockReturnValue ( 0 ) ,
1408
+ } as unknown as MatrixEvent ) ;
1409
+ expect ( sess ! . statistics . totals . roomEventEncryptionKeysReceivedTotalAge ) . toEqual ( 1000 ) ;
1410
+
1411
+ jest . setSystemTime ( 2000 ) ;
1412
+ sess . onCallEncryption ( {
1413
+ getType : jest . fn ( ) . mockReturnValue ( "io.element.call.encryption_keys" ) ,
1414
+ getContent : jest . fn ( ) . mockReturnValue ( {
1415
+ device_id : "bobsphone" ,
1416
+ call_id : "" ,
1417
+ keys : [
1418
+ {
1419
+ index : 0 ,
1420
+ key : "dGhpcyBpcyB0aGUga2V5" ,
1421
+ } ,
1422
+ ] ,
1423
+ sent_ts : 0 ,
1424
+ } ) ,
1425
+ getSender : jest . fn ( ) . mockReturnValue ( "@bob:example.org" ) ,
1426
+ getTs : jest . fn ( ) . mockReturnValue ( Date . now ( ) ) ,
1427
+ } as unknown as MatrixEvent ) ;
1428
+ expect ( sess ! . statistics . totals . roomEventEncryptionKeysReceivedTotalAge ) . toEqual ( 3000 ) ;
1429
+
1430
+ jest . setSystemTime ( 3000 ) ;
1431
+ sess . onCallEncryption ( {
1432
+ getType : jest . fn ( ) . mockReturnValue ( "io.element.call.encryption_keys" ) ,
1433
+ getContent : jest . fn ( ) . mockReturnValue ( {
1434
+ device_id : "bobsphone" ,
1435
+ call_id : "" ,
1436
+ keys : [
1437
+ {
1438
+ index : 0 ,
1439
+ key : "dGhpcyBpcyB0aGUga2V5" ,
1440
+ } ,
1441
+ ] ,
1442
+ sent_ts : 1000 ,
1443
+ } ) ,
1444
+ getSender : jest . fn ( ) . mockReturnValue ( "@bob:example.org" ) ,
1445
+ getTs : jest . fn ( ) . mockReturnValue ( Date . now ( ) ) ,
1446
+ } as unknown as MatrixEvent ) ;
1447
+ expect ( sess ! . statistics . totals . roomEventEncryptionKeysReceivedTotalAge ) . toEqual ( 5000 ) ;
1448
+ } finally {
1449
+ jest . useRealTimers ( ) ;
1450
+ }
1345
1451
} ) ;
1346
1452
} ) ;
0 commit comments