From ff214eaa4ab29e587236f2f70779c1c1eeea7895 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Sun, 24 Dec 2017 22:29:32 +0100 Subject: [PATCH] Add more assertions to DatabaseManager --- .../main/java/me/impy/aegis/MainActivity.java | 12 +++---- .../me/impy/aegis/db/DatabaseManager.java | 35 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index 4adff153..0306ecb3 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -91,14 +91,14 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis touchHelper.attachToRecyclerView(rvKeyProfiles); rvKeyProfiles.setAdapter(_keyProfileAdapter); - if (!_app.isRunning() && !_db.isUnlocked()) { + if (!_app.isRunning() && _db.isLocked()) { if (!_app.getPreferences().getBoolean("pref_intro", false)) { Intent intro = new Intent(this, IntroActivity.class); startActivityForResult(intro, CODE_DO_INTRO); } else { try { _db.load(); - if (!_db.isUnlocked()) { + if (_db.isLocked()) { startAuthActivity(); } } catch (FileNotFoundException e) { @@ -114,7 +114,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis } } - if (_db.isUnlocked()) { + if (!_db.isLocked()) { loadKeyProfiles(); } } @@ -346,7 +346,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis MasterKey key = (MasterKey) data.getSerializableExtra("key"); try { _db.load(); - if (!_db.isUnlocked()) { + if (_db.isLocked()) { _db.unlock(key); } } catch (Exception e) { @@ -377,7 +377,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis private void doShortcutActions() { Intent intent = getIntent(); String mode = intent.getStringExtra("Action"); - if (mode == null || !_db.isUnlocked()) { + if (mode == null || _db.isLocked()) { return; } @@ -556,7 +556,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis private void updateLockIcon() { // hide the lock icon if the database is not unlocked - if (_menu != null && _db.isUnlocked()) { + if (_menu != null && !_db.isLocked()) { MenuItem item = _menu.findItem(R.id.action_lock); item.setVisible(_db.getFile().isEncrypted()); } diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseManager.java b/app/src/main/java/me/impy/aegis/db/DatabaseManager.java index 85f7e7d0..4540c1c2 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseManager.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseManager.java @@ -30,6 +30,8 @@ public class DatabaseManager { } public void load() throws Exception { + assertState(true, false); + byte[] fileBytes; FileInputStream file = null; @@ -58,14 +60,14 @@ public class DatabaseManager { } public void lock() throws Exception { - assertUnlocked(); + assertState(false, true); // TODO: properly clear everything _key = null; _db = null; } public void unlock(MasterKey key) throws Exception { - assertLoaded(); + assertState(true, true); byte[] encrypted = _file.getContent(); CryptParameters params = _file.getCryptParameters(); CryptResult result = key.decrypt(encrypted, params); @@ -90,7 +92,7 @@ public class DatabaseManager { } public void save() throws Exception { - assertUnlocked(); + assertState(false, true); byte[] dbBytes = _db.serialize(); if (!_file.isEncrypted()) { _file.setContent(dbBytes); @@ -103,7 +105,7 @@ public class DatabaseManager { } public String export(boolean encrypt) throws Exception { - assertUnlocked(); + assertState(false, true); byte[] bytes = _db.serialize(); encrypt = encrypt && getFile().isEncrypted(); if (encrypt) { @@ -136,22 +138,22 @@ public class DatabaseManager { } public void addKey(DatabaseEntry entry) throws Exception { - assertUnlocked(); + assertState(false, true); _db.addKey(entry); } public void removeKey(DatabaseEntry entry) throws Exception { - assertUnlocked(); + assertState(false, true); _db.removeKey(entry); } public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception { - assertUnlocked(); + assertState(false, true); _db.swapKeys(entry1, entry2); } public List getKeys() throws Exception { - assertUnlocked(); + assertState(false, true); return _db.getKeys(); } @@ -163,20 +165,21 @@ public class DatabaseManager { return _file != null; } - public boolean isUnlocked() { - return _db != null; + public boolean isLocked() { + return _db == null; } - private void assertLoaded() throws Exception { - if (!isLoaded()) { + private void assertState(boolean locked, boolean loaded) throws Exception { + if (isLoaded() && !loaded) { throw new Exception("database file has not been loaded yet"); + } else if (!isLoaded() && loaded) { + throw new Exception("database file has is already been loaded"); } - } - private void assertUnlocked() throws Exception { - assertLoaded(); - if (!isUnlocked()) { + if (isLocked() && !locked) { throw new Exception("database file has not been unlocked yet"); + } else if (!isLocked() && locked) { + throw new Exception("database file has is already been unlocked"); } } }