From d45b4be4534412db3fc215803065b3a4c26c71d9 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Mon, 10 May 2021 10:07:26 +0200 Subject: [PATCH] Update implementation and test for panic observer --- .../remotewipe/RemoteWipeManagerImpl.java | 27 +++++++++++++++++-- .../remotewipe/RemoteWipeIntegrationTest.java | 13 ++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java index 7a5ed59a7..01d7803d9 100644 --- a/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/remotewipe/RemoteWipeManagerImpl.java @@ -64,6 +64,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl private final ContactManager contactManager; private final MessageEncoder messageEncoder; private final MessageParser messageParser; + private RemoteWipeManager.Observer observer; @Inject protected RemoteWipeManagerImpl( @@ -88,6 +89,11 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl contactGroupFactory.createLocalGroup(CLIENT_ID, MAJOR_VERSION); } + @Override + public void listenForPanic(RemoteWipeManager.Observer observer) { + this.observer = observer; + } + @Override public void onDatabaseOpened(Transaction txn) throws DbException { System.out.println("DATAbase opened"); @@ -128,14 +134,31 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl BdfList receivedWipeMessages = existingMeta.getOptionalList(GROUP_KEY_RECEIVED_WIPE); if (receivedWipeMessages == null) receivedWipeMessages = new BdfList(); - // TODO filter the messages for old ones + + for (int i = 0; i < receivedWipeMessages.size(); i++) { + BdfList receivedWipeMessage = receivedWipeMessages.getList(i); + // If we already have one from this contact, ignore + if (receivedWipeMessage.getLong(0).intValue() == contactId.getInt()) return false; + + // TODO filter the messages for old ones +// long timestamp = receivedWipeMessage.getLong(1); +// if (clock.currentTimeMillis() - timestamp > MAX_MESSAGE_AGE) { +// receivedWipeMessages.remove(i); +// } + } if (receivedWipeMessages.size() + 1 == THRESHOLD) { System.out.println("Threshold reached - panic!"); + if (observer != null) { + observer.onPanic(); + } // we could here clear the metadata to allow us to send // the wipe messages several times when testing } else { - receivedWipeMessages.add(1); // TODO this should be the timestamp + BdfList newReceivedWipeMessage = new BdfList(); + newReceivedWipeMessage.add(contactId.getInt()); + newReceivedWipeMessage.add(1); // TODO this should be the timestamp + receivedWipeMessages.add(newReceivedWipeMessage); BdfDictionary newMeta = new BdfDictionary(); newMeta.put(GROUP_KEY_RECEIVED_WIPE, receivedWipeMessages); clientHelper.mergeGroupMetadata(txn, localGroup.getId(), newMeta); diff --git a/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java index 51ee84a97..9a763ebe8 100644 --- a/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/remotewipe/RemoteWipeIntegrationTest.java @@ -24,7 +24,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -public class RemoteWipeIntegrationTest extends BriarIntegrationTest { +public class RemoteWipeIntegrationTest extends BriarIntegrationTest implements RemoteWipeManager.Observer { private RemoteWipeManager remoteWipeManager0; private RemoteWipeManager remoteWipeManager1; @@ -35,6 +35,8 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest { // TODO assert that we do not already have a wipe setup // assertNull(socialBackupManager0.getBackupMetadata(txn)); @@ -122,6 +126,8 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest messages2At0 = getMessages2At0(); assertEquals(1, messages2At0.size()); + + assertTrue(panicCalled); } private Collection getMessages1At0() @@ -154,4 +160,9 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest