From cfc705ca41abff843d2d37b29f40e2430defe188 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Fri, 7 May 2021 11:01:35 +0200 Subject: [PATCH] Update test --- .../remotewipe/RemoteWipeManagerImpl.java | 45 ++++++++++++++++--- .../remotewipe/RemoteWipeIntegrationTest.java | 34 +++++++++++--- 2 files changed, 67 insertions(+), 12 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 ce556f617..d73ff7e82 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 @@ -1,6 +1,7 @@ package org.briarproject.briar.remotewipe; import org.briarproject.bramble.api.FormatException; +import org.briarproject.bramble.api.Pair; import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.client.ContactGroupFactory; import org.briarproject.bramble.api.contact.Contact; @@ -37,6 +38,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nullable; import javax.inject.Inject; import static org.briarproject.briar.client.MessageTrackerConstants.MSG_KEY_READ; @@ -108,15 +110,15 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl System.out.println("Incoming message called"); MessageType type = MessageType.fromValue(body.getLong(0).intValue()); if (type == SETUP) { - - messageTracker.trackIncomingMessage(txn, m); -// message.getGroupId turn into contactid -// txn.attach event + // message.getGroupId turn into contactid + // txn.attach event } else if (type == WIPE) { - ContactId contactId = getContactId(txn, m.getGroupId()); - // check if contact is in list of wipers + // Check if contact is in list of wipers + if (findMessage(txn, m.getGroupId(), SETUP, true) != null) { + System.out.println("Got a wipe message from a wiper"); + } // if so, increment counter // check if counter = threshold } @@ -162,10 +164,13 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl public void wipe(Transaction txn, Contact contact) throws DbException, FormatException { - // TODO check that we have a SETUP message from contact + // Check that we have a SETUP message from contact + if (!amWiper(txn, contact.getId())) throw new DbException(); + Group group = getContactGroup(contact); GroupId g = group.getId(); if (!db.containsGroup(txn, g)) db.addGroup(txn, group); + long timestamp = clock.currentTimeMillis(); byte[] body = messageEncoder.encodeWipeMessage(); @@ -181,6 +186,16 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl messageTracker.trackOutgoingMessage(txn, m); } + public boolean amWiper(Transaction txn, ContactId contactId) { + try { + GroupId groupId = getContactGroup(db.getContact(txn, contactId)).getId(); + return findMessage(txn, groupId, SETUP, false) != null; + } catch (DbException e) { + return false; + } catch (FormatException e) { + return false; + } + } @Override public Group getContactGroup(Contact c) { @@ -302,4 +317,20 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl public void removingContact(Transaction txn, Contact c) throws DbException { db.removeGroup(txn, getContactGroup(c)); } + + @Nullable + private Pair findMessage(Transaction txn, + GroupId g, MessageType type, boolean local) + throws DbException, FormatException { + BdfDictionary query = BdfDictionary.of( + new BdfEntry(MSG_KEY_MESSAGE_TYPE, type.getValue()), + new BdfEntry(MSG_KEY_LOCAL, local)); + Map results = + clientHelper.getMessageMetadataAsDictionary(txn, g, query); +// if (results.size() > 1) throw new DbException(); + if (results.isEmpty()) return null; + Map.Entry e = + results.entrySet().iterator().next(); + return new Pair<>(e.getKey(), e.getValue()); + } } 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 39a788e83..51ee84a97 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 @@ -84,20 +84,44 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest messages0At1 = getMessages0At1(); assertEquals(1, messages0At1.size()); - System.out.println("mmmm" + messages0At1.size()); + + Collection messages0At2 = + getMessages0At2(); + assertEquals(1, messages0At2.size()); + for (ConversationMessageHeader h : messages0At1) { assertTrue(h instanceof RemoteWipeMessageHeader); RemoteWipeMessageHeader r = (RemoteWipeMessageHeader) h; assertFalse(r.isLocal()); } -// db1.transaction(false, txn -> { -// assertTrue(socialBackupManager1.amCustodian(txn, contactId0From1)); -// }); + + // The wipers check that they are now wipers + db1.transaction(false, txn -> { + assertTrue(remoteWipeManager1.amWiper(txn, contactId0From1)); + remoteWipeManager1.wipe(txn, contact0From1); + }); + + db2.transaction(false, txn -> { + assertTrue(remoteWipeManager2.amWiper(txn, contactId0From2)); + remoteWipeManager2.wipe(txn, contact0From2); + }); + + // Sync the wipe messages to the wipee + sync1To0(1, true); + sync2To0(1, true); + + Collection messages1At0 = + getMessages1At0(); + assertEquals(1, messages1At0.size()); + + Collection messages2At0 = + getMessages2At0(); + assertEquals(1, messages2At0.size()); } private Collection getMessages1At0()