Fix a crash when importing an entry with an existing UUID

Close #11
This commit is contained in:
Alexander Bakker 2018-09-22 14:12:42 +02:00
parent 300fb05c1f
commit ecbbcfee00
5 changed files with 26 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import java.util.UUID;
import me.impy.aegis.encoding.Base64Exception;
import me.impy.aegis.otp.OtpInfoException;
@ -68,4 +69,8 @@ public class Database {
public List<DatabaseEntry> getEntries() {
return _entries.getList();
}
public DatabaseEntry getEntryByUUID(UUID uuid) {
return _entries.getByUUID(uuid);
}
}

View file

@ -64,6 +64,10 @@ public class DatabaseEntry implements Serializable {
_info = OtpInfo.parseJson(obj.getString("type"), obj.getJSONObject("info"));
}
public void resetUUID() {
_uuid = UUID.randomUUID();
}
public UUID getUUID() {
return _uuid;
}

View file

@ -19,19 +19,19 @@ public class DatabaseEntryList implements Iterable<DatabaseEntry>, Serializable
}
public void add(DatabaseEntry entry) {
if (tryGetByUUID(entry.getUUID()) != null) {
if (getByUUID(entry.getUUID()) != null) {
throw new AssertionError("entry found with the same uuid");
}
_entries.add(entry);
}
public void remove(DatabaseEntry entry) {
entry = getByUUID(entry.getUUID());
entry = mustGetByUUID(entry.getUUID());
_entries.remove(entry);
}
public void replace(DatabaseEntry newEntry) {
DatabaseEntry oldEntry = getByUUID(newEntry.getUUID());
DatabaseEntry oldEntry = mustGetByUUID(newEntry.getUUID());
_entries.set(_entries.indexOf(oldEntry), newEntry);
}
@ -43,7 +43,7 @@ public class DatabaseEntryList implements Iterable<DatabaseEntry>, Serializable
return Collections.unmodifiableList(_entries);
}
private DatabaseEntry tryGetByUUID(UUID uuid) {
public DatabaseEntry getByUUID(UUID uuid) {
for (DatabaseEntry entry : _entries) {
if (entry.getUUID().equals(uuid)) {
return entry;
@ -52,8 +52,8 @@ public class DatabaseEntryList implements Iterable<DatabaseEntry>, Serializable
return null;
}
private DatabaseEntry getByUUID(UUID uuid) {
DatabaseEntry entry = tryGetByUUID(uuid);
private DatabaseEntry mustGetByUUID(UUID uuid) {
DatabaseEntry entry = getByUUID(uuid);
if (entry == null) {
throw new AssertionError("no entry found with the same uuid");
}

View file

@ -11,6 +11,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import me.impy.aegis.BuildConfig;
import me.impy.aegis.crypto.MasterKey;
@ -187,6 +188,11 @@ public class DatabaseManager {
return _db.getEntries();
}
public DatabaseEntry getEntryByUUID(UUID uuid) {
assertState(false, true);
return _db.getEntryByUUID(uuid);
}
public MasterKey getMasterKey() {
assertState(false, true);
return _key;

View file

@ -7,9 +7,7 @@ import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference;
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;
import android.view.Window;
@ -315,6 +313,11 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
private void importDatabase(DatabaseImporter importer) throws DatabaseImporterException {
List<DatabaseEntry> entries = importer.convert();
for (DatabaseEntry entry : entries) {
// temporary: randomize the UUID of duplicate entries and add them anyway
if (_db.getEntryByUUID(entry.getUUID()) != null) {
entry.resetUUID();
}
_db.addEntry(entry);
}