@Test public void testStoringDbKeyOverwritesBackup() throws Exception { storeDatabaseKey(keyBackupFile, encryptedKeyHex); assertFalse(keyFile.exists()); assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile)); assertTrue(accountManager.storeEncryptedDatabaseKey( newEncryptedKeyHex)); assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyFile)); assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyBackupFile)); }
protected boolean storeEncryptedDatabaseKey(String hex) { LOG.info("Storing database key in file"); if (databaseConfig.getDatabaseKeyDirectory().mkdirs()) LOG.info("Created database key directory"); if (dbKeyBackupFile.exists() && !dbKeyFile.exists()) { if (dbKeyBackupFile.renameTo(dbKeyFile)) LOG.info("Renamed old backup"); else LOG.warning("Failed to rename old backup"); } try { writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored database key in backup file"); if (dbKeyFile.exists()) { if (dbKeyFile.delete()) LOG.info("Deleted primary file"); else LOG.warning("Failed to delete primary file"); } if (dbKeyBackupFile.renameTo(dbKeyFile)) { LOG.info("Renamed backup file to primary"); } else { LOG.warning("Failed to rename backup file to primary"); return false; } writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored second copy of database key in backup file"); return true; } catch (IOException e) { logException(LOG, WARNING, e); return false; } }
AccountManagerImpl implements AccountManager { protected boolean storeEncryptedDatabaseKey(String hex) { LOG.info("Storing database key in file"); if (databaseConfig.getDatabaseKeyDirectory().mkdirs()) LOG.info("Created database key directory"); if (dbKeyBackupFile.exists() && !dbKeyFile.exists()) { if (dbKeyBackupFile.renameTo(dbKeyFile)) LOG.info("Renamed old backup"); else LOG.warning("Failed to rename old backup"); } try { writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored database key in backup file"); if (dbKeyFile.exists()) { if (dbKeyFile.delete()) LOG.info("Deleted primary file"); else LOG.warning("Failed to delete primary file"); } if (dbKeyBackupFile.renameTo(dbKeyFile)) { LOG.info("Renamed backup file to primary"); } else { LOG.warning("Failed to rename backup file to primary"); return false; } writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored second copy of database key in backup file"); return true; } catch (IOException e) { logException(LOG, WARNING, e); return false; } } }
AccountManagerImpl implements AccountManager { protected boolean storeEncryptedDatabaseKey(String hex) { LOG.info("Storing database key in file"); if (databaseConfig.getDatabaseKeyDirectory().mkdirs()) LOG.info("Created database key directory"); if (dbKeyBackupFile.exists() && !dbKeyFile.exists()) { if (dbKeyBackupFile.renameTo(dbKeyFile)) LOG.info("Renamed old backup"); else LOG.warning("Failed to rename old backup"); } try { writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored database key in backup file"); if (dbKeyFile.exists()) { if (dbKeyFile.delete()) LOG.info("Deleted primary file"); else LOG.warning("Failed to delete primary file"); } if (dbKeyBackupFile.renameTo(dbKeyFile)) { LOG.info("Renamed backup file to primary"); } else { LOG.warning("Failed to rename backup file to primary"); return false; } writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored second copy of database key in backup file"); return true; } catch (IOException e) { logException(LOG, WARNING, e); return false; } } @Inject  AccountManagerImpl(DatabaseConfig databaseConfig, CryptoComponent crypto,
			IdentityManager identityManager); }
AccountManagerImpl implements AccountManager { protected boolean storeEncryptedDatabaseKey(String hex) { LOG.info("Storing database key in file"); if (databaseConfig.getDatabaseKeyDirectory().mkdirs()) LOG.info("Created database key directory"); if (dbKeyBackupFile.exists() && !dbKeyFile.exists()) { if (dbKeyBackupFile.renameTo(dbKeyFile)) LOG.info("Renamed old backup"); else LOG.warning("Failed to rename old backup"); } try { writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored database key in backup file"); if (dbKeyFile.exists()) { if (dbKeyFile.delete()) LOG.info("Deleted primary file"); else LOG.warning("Failed to delete primary file"); } if (dbKeyBackupFile.renameTo(dbKeyFile)) { LOG.info("Renamed backup file to primary"); } else { LOG.warning("Failed to rename backup file to primary"); return false; } writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored second copy of database key in backup file"); return true; } catch (IOException e) { logException(LOG, WARNING, e); return false; } } @Inject  AccountManagerImpl(DatabaseConfig databaseConfig, CryptoComponent crypto,
			IdentityManager identityManager); @Override boolean hasDatabaseKey(); @Override @Nullable SecretKey getDatabaseKey(); @Override boolean accountExists(); @Override boolean createAccount(String name, String password); @Override void deleteAccount(); @Override boolean signIn(String password); @Override boolean changePassword(String oldPassword, String newPassword); }
AccountManagerImpl implements AccountManager { protected boolean storeEncryptedDatabaseKey(String hex) { LOG.info("Storing database key in file"); if (databaseConfig.getDatabaseKeyDirectory().mkdirs()) LOG.info("Created database key directory"); if (dbKeyBackupFile.exists() && !dbKeyFile.exists()) { if (dbKeyBackupFile.renameTo(dbKeyFile)) LOG.info("Renamed old backup"); else LOG.warning("Failed to rename old backup"); } try { writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored database key in backup file"); if (dbKeyFile.exists()) { if (dbKeyFile.delete()) LOG.info("Deleted primary file"); else LOG.warning("Failed to delete primary file"); } if (dbKeyBackupFile.renameTo(dbKeyFile)) { LOG.info("Renamed backup file to primary"); } else { LOG.warning("Failed to rename backup file to primary"); return false; } writeDbKeyToFile(hex, dbKeyBackupFile); LOG.info("Stored second copy of database key in backup file"); return true; } catch (IOException e) { logException(LOG, WARNING, e); return false; } } @Inject  AccountManagerImpl(DatabaseConfig databaseConfig, CryptoComponent crypto,
			IdentityManager identityManager); @Override boolean hasDatabaseKey(); @Override @Nullable SecretKey getDatabaseKey(); @Override boolean accountExists(); @Override boolean createAccount(String name, String password); @Override void deleteAccount(); @Override boolean signIn(String password); @Override boolean changePassword(String oldPassword, String newPassword);  }