Update test

This commit is contained in:
ameba23
2021-05-07 11:01:35 +02:00
parent e97114e322
commit cfc705ca41
2 changed files with 67 additions and 12 deletions

View File

@@ -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<MessageId, BdfDictionary> 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<MessageId, BdfDictionary> results =
clientHelper.getMessageMetadataAsDictionary(txn, g, query);
// if (results.size() > 1) throw new DbException();
if (results.isEmpty()) return null;
Map.Entry<MessageId, BdfDictionary> e =
results.entrySet().iterator().next();
return new Pair<>(e.getKey(), e.getValue());
}
}

View File

@@ -84,20 +84,44 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
// Sync the setup messages to the contacts
sync0To1(1, true);
sync0To2(1, true);
// TODO check for the headers
// The setup message from 0 should have arrived at 1
Collection<ConversationMessageHeader> messages0At1 =
getMessages0At1();
assertEquals(1, messages0At1.size());
System.out.println("mmmm" + messages0At1.size());
Collection<ConversationMessageHeader> 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<ConversationMessageHeader> messages1At0 =
getMessages1At0();
assertEquals(1, messages1At0.size());
Collection<ConversationMessageHeader> messages2At0 =
getMessages2At0();
assertEquals(1, messages2At0.size());
}
private Collection<ConversationMessageHeader> getMessages1At0()