11package org .cryptomator .cryptofs ;
22
3+ import org .cryptomator .cryptofs .common .BackupHelper ;
34import org .cryptomator .cryptofs .common .Constants ;
45import org .cryptomator .cryptofs .common .FileSystemCapabilityChecker ;
56import org .cryptomator .cryptolib .api .Cryptor ;
@@ -36,6 +37,7 @@ public class CryptoFileSystemsTest {
3637 private final Path pathToVault = mock (Path .class , "vaultPath" );
3738 private final Path normalizedPathToVault = mock (Path .class , "normalizedVaultPath" );
3839 private final Path configFilePath = mock (Path .class , "normalizedVaultPath/vault.cryptomator" );
40+ private final Path configFileBackupPath = mock (Path .class , "normalizedVaultPath/vault.cryptomator.12345678.bkup" );
3941 private final Path dataDirPath = mock (Path .class , "normalizedVaultPath/d" );
4042 private final Path preContenRootPath = mock (Path .class , "normalizedVaultPath/d/AB" );
4143 private final Path contenRootPath = mock (Path .class , "normalizedVaultPath/d/AB/CDEFGHIJKLMNOP" );
@@ -61,6 +63,7 @@ public class CryptoFileSystemsTest {
6163 private MockedStatic <VaultConfig > vaultConficClass ;
6264 private MockedStatic <Files > filesClass ;
6365 private MockedStatic <CryptorProvider > cryptorProviderClass ;
66+ private MockedStatic <BackupHelper > backupHelperClass ;
6467
6568 private final CryptoFileSystems inTest = new CryptoFileSystems (cryptoFileSystemComponentBuilder , capabilityChecker , csprng );
6669
@@ -69,6 +72,7 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
6972 vaultConficClass = Mockito .mockStatic (VaultConfig .class );
7073 filesClass = Mockito .mockStatic (Files .class );
7174 cryptorProviderClass = Mockito .mockStatic (CryptorProvider .class );
75+ backupHelperClass = Mockito .mockStatic (BackupHelper .class );
7276
7377 when (pathToVault .normalize ()).thenReturn (normalizedPathToVault );
7478 when (normalizedPathToVault .resolve ("vault.cryptomator" )).thenReturn (configFilePath );
@@ -77,6 +81,7 @@ public void setup() throws IOException, MasterkeyLoadingFailedException {
7781 filesClass .when (() -> Files .readString (configFilePath , StandardCharsets .US_ASCII )).thenReturn ("jwt-vault-config" );
7882 vaultConficClass .when (() -> VaultConfig .decode ("jwt-vault-config" )).thenReturn (configLoader );
7983 cryptorProviderClass .when (() -> CryptorProvider .forScheme (cipherCombo )).thenReturn (cryptorProvider );
84+ backupHelperClass .when (() -> BackupHelper .attemptBackup (configFilePath )).thenReturn (configFileBackupPath );
8085 when (VaultConfig .decode ("jwt-vault-config" )).thenReturn (configLoader );
8186 when (configLoader .getKeyId ()).thenReturn (URI .create ("test:key" ));
8287 when (keyLoader .loadKey (Mockito .any ())).thenReturn (masterkey );
@@ -105,6 +110,7 @@ public void tearDown() {
105110 vaultConficClass .close ();
106111 filesClass .close ();
107112 cryptorProviderClass .close ();
113+ backupHelperClass .close ();
108114 }
109115
110116 @ Test
@@ -153,6 +159,25 @@ public void testCreateThrowsIOExceptionIfContentRootExistenceCheckFails() {
153159 Assertions .assertThrows (IOException .class , () -> inTest .create (provider , pathToVault , properties ));
154160 }
155161
162+ @ Test
163+ public void testCreateAttemptsBackupOnSuccessfulVerification () throws IOException {
164+ inTest .create (provider , pathToVault , properties );
165+ backupHelperClass .verify (() -> BackupHelper .attemptBackup (configFilePath ));
166+ }
167+
168+ @ Test
169+ public void testCreateWithFailedConfigVerificationMakesNoBackup () throws IOException {
170+ when (configLoader .verify (rawKey , Constants .VAULT_VERSION )).thenThrow (VaultKeyInvalidException .class );
171+ Assertions .assertThrows (VaultKeyInvalidException .class , () -> inTest .create (provider , pathToVault , properties ));
172+ backupHelperClass .verify (() -> BackupHelper .attemptBackup (configFilePath ), Mockito .never ());
173+ }
174+
175+ @ Test
176+ public void testCreateThrowsIOExceptionIfBackupAttemptThrowsOne () throws IOException {
177+ backupHelperClass .when (() -> BackupHelper .attemptBackup (configFilePath )).thenThrow (new IOException ());
178+ Assertions .assertThrows (IOException .class ,() -> inTest .create (provider , pathToVault , properties ));
179+ }
180+
156181 @ Test
157182 public void testGetReturnsFileSystemForPathIfItExists () throws IOException , MasterkeyLoadingFailedException {
158183 CryptoFileSystemImpl fileSystem = inTest .create (provider , pathToVault , properties );
0 commit comments