@@ -61,6 +61,7 @@ import {
61
61
EventShieldReason ,
62
62
ImportRoomKeysOpts ,
63
63
KeyBackupCheck ,
64
+ KeyBackupInfo ,
64
65
VerificationRequest ,
65
66
} from "../../../src/crypto-api" ;
66
67
import * as testData from "../../test-utils/test-data" ;
@@ -72,6 +73,7 @@ import { Curve25519AuthData } from "../../../src/crypto-api/keybackup";
72
73
import encryptAESSecretStorageItem from "../../../src/utils/encryptAESSecretStorageItem.ts" ;
73
74
import { CryptoStore , SecretStorePrivateKeys } from "../../../src/crypto/store/base" ;
74
75
import { CryptoEvent } from "../../../src/crypto-api/index.ts" ;
76
+ import { RustBackupManager } from "../../../src/rust-crypto/backup.ts" ;
75
77
76
78
const TEST_USER = "@alice:example.com" ;
77
79
const TEST_DEVICE_ID = "TEST_DEVICE" ;
@@ -1879,6 +1881,74 @@ describe("RustCrypto", () => {
1879
1881
) ;
1880
1882
} ) ;
1881
1883
} ) ;
1884
+
1885
+ describe ( "resetEncryption" , ( ) => {
1886
+ let secretStorage : ServerSideSecretStorage ;
1887
+ beforeEach ( ( ) => {
1888
+ secretStorage = {
1889
+ setDefaultKeyId : jest . fn ( ) ,
1890
+ hasKey : jest . fn ( ) . mockResolvedValue ( false ) ,
1891
+ getKey : jest . fn ( ) . mockResolvedValue ( null ) ,
1892
+ } as unknown as ServerSideSecretStorage ;
1893
+
1894
+ fetchMock . post ( "path:/_matrix/client/v3/keys/upload" , { one_time_key_counts : { } } ) ;
1895
+ fetchMock . post ( "path:/_matrix/client/v3/keys/signatures/upload" , { } ) ;
1896
+ } ) ;
1897
+
1898
+ it ( "reset should reset 4S, backup and cross-signing" , async ( ) => {
1899
+ // We don't have a key backup
1900
+ fetchMock . get ( "path:/_matrix/client/v3/room_keys/version" , { } ) ;
1901
+
1902
+ const rustCrypto = await makeTestRustCrypto ( makeMatrixHttpApi ( ) , undefined , undefined , secretStorage ) ;
1903
+
1904
+ const authUploadDeviceSigningKeys = jest . fn ( ) ;
1905
+ await rustCrypto . resetEncryption ( authUploadDeviceSigningKeys ) ;
1906
+
1907
+ // The default key id should be deleted
1908
+ expect ( secretStorage . setDefaultKeyId ) . toHaveBeenCalledWith ( null ) ;
1909
+ expect ( await rustCrypto . getActiveSessionBackupVersion ( ) ) . toBeNull ( ) ;
1910
+ // The new cross signing keys should be uploaded
1911
+ expect ( authUploadDeviceSigningKeys ) . toHaveBeenCalledWith ( expect . any ( Function ) ) ;
1912
+ } ) ;
1913
+
1914
+ it ( "key backup should be re-enabled after reset" , async ( ) => {
1915
+ // When we will delete the key backup
1916
+ let backupIsDeleted = false ;
1917
+ fetchMock . delete ( "path:/_matrix/client/v3/room_keys/version/1" , ( ) => {
1918
+ backupIsDeleted = true ;
1919
+ return { } ;
1920
+ } ) ;
1921
+ // If the backup is deleted, we will return an empty object
1922
+ fetchMock . get ( "path:/_matrix/client/v3/room_keys/version" , ( ) => {
1923
+ return backupIsDeleted ? { } : testData . SIGNED_BACKUP_DATA ;
1924
+ } ) ;
1925
+
1926
+ // We consider the key backup as trusted
1927
+ jest . spyOn ( RustBackupManager . prototype , "isKeyBackupTrusted" ) . mockResolvedValue ( {
1928
+ trusted : true ,
1929
+ matchesDecryptionKey : true ,
1930
+ } ) ;
1931
+
1932
+ const rustCrypto = await makeTestRustCrypto ( makeMatrixHttpApi ( ) , undefined , undefined , secretStorage ) ;
1933
+ // We have a key backup
1934
+ expect ( await rustCrypto . getActiveSessionBackupVersion ( ) ) . not . toBeNull ( ) ;
1935
+
1936
+ // A new key backup should be created after the reset
1937
+ let newKeyBackupInfo ! : KeyBackupInfo ;
1938
+ fetchMock . post ( "path:/_matrix/client/v3/room_keys/version" , ( res , options ) => {
1939
+ newKeyBackupInfo = JSON . parse ( options . body as string ) ;
1940
+ return { version : "2" } ;
1941
+ } ) ;
1942
+
1943
+ const authUploadDeviceSigningKeys = jest . fn ( ) ;
1944
+ await rustCrypto . resetEncryption ( authUploadDeviceSigningKeys ) ;
1945
+
1946
+ // A new key backup should be created
1947
+ expect ( newKeyBackupInfo . auth_data ) . toBeTruthy ( ) ;
1948
+ // The new cross signing keys should be uploaded
1949
+ expect ( authUploadDeviceSigningKeys ) . toHaveBeenCalledWith ( expect . any ( Function ) ) ;
1950
+ } ) ;
1951
+ } ) ;
1882
1952
} ) ;
1883
1953
1884
1954
/** Build a MatrixHttpApi instance */
0 commit comments