Update implementation and test for panic observer

This commit is contained in:
ameba23
2021-05-10 10:07:26 +02:00
parent f998482e93
commit d45b4be453
2 changed files with 37 additions and 3 deletions

View File

@@ -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);

View File

@@ -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<BriarIntegrationTestComponent> {
public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegrationTestComponent> implements RemoteWipeManager.Observer {
private RemoteWipeManager remoteWipeManager0;
private RemoteWipeManager remoteWipeManager1;
@@ -35,6 +35,8 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
private Group g2From0;
private Group g0From2;
private boolean panicCalled = false;
@Before
@Override
public void setUp() throws Exception {
@@ -72,8 +74,10 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
BriarIntegrationTestComponent.Helper.injectEagerSingletons(c2);
}
@Test
public void testRemoteWipe() throws Exception {
remoteWipeManager0.listenForPanic(this);
db0.transaction(false, txn -> {
// TODO assert that we do not already have a wipe setup
// assertNull(socialBackupManager0.getBackupMetadata(txn));
@@ -122,6 +126,8 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
Collection<ConversationMessageHeader> messages2At0 =
getMessages2At0();
assertEquals(1, messages2At0.size());
assertTrue(panicCalled);
}
private Collection<ConversationMessageHeader> getMessages1At0()
@@ -154,4 +160,9 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
assertEquals(msgCount, c1.getMsgCount());
assertEquals(unreadCount, c1.getUnreadCount());
}
@Override
public void onPanic() {
panicCalled = true;
}
}