From 20422edf788c6fe5f16b6bead19b1f522f4d289a Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Mon, 21 May 2018 16:26:11 -0300 Subject: [PATCH] Introduction: Reset session information for removed introducees --- .../IntroducerProtocolEngine.java | 22 ++++++++- .../briar/introduction/IntroducerSession.java | 2 +- .../introduction/IntroductionManagerImpl.java | 3 +- .../IntroductionIntegrationTest.java | 47 +++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerProtocolEngine.java b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerProtocolEngine.java index a346a0bf32..c159697adc 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerProtocolEngine.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerProtocolEngine.java @@ -92,9 +92,27 @@ class IntroducerProtocolEngine throw new UnsupportedOperationException(); // Invalid in this role } - IntroducerSession onAbortAction(Transaction txn, IntroducerSession s) + IntroducerSession onIntroduceeRemoved(Transaction txn, + Introducee remainingIntroducee, IntroducerSession session) throws DbException { - return abort(txn, s); + // abort session + IntroducerSession s = abort(txn, session); + // reset information for introducee that was removed + Introducee introduceeA, introduceeB; + if (remainingIntroducee.author.equals(s.getIntroduceeA().author)) { + introduceeA = s.getIntroduceeA(); + introduceeB = + new Introducee(s.getSessionId(), s.getIntroduceeB().groupId, + s.getIntroduceeB().author); + } else if (remainingIntroducee.author + .equals(s.getIntroduceeB().author)) { + introduceeA = + new Introducee(s.getSessionId(), s.getIntroduceeA().groupId, + s.getIntroduceeA().author); + introduceeB = s.getIntroduceeB(); + } else throw new DbException(); + return new IntroducerSession(s.getSessionId(), s.getState(), + s.getRequestTimestamp(), introduceeA, introduceeB); } @Override diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerSession.java b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerSession.java index c26eb26d98..3c50621eb7 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerSession.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroducerSession.java @@ -79,7 +79,7 @@ class IntroducerSession extends Session<IntroducerState> { i.lastLocalMessageId, remoteMessageId); } - private Introducee(SessionId sessionId, GroupId groupId, + Introducee(SessionId sessionId, GroupId groupId, Author author) { this(sessionId, groupId, author, -1, null, null); } diff --git a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionManagerImpl.java index fd39a865fe..64fc1b40d4 100644 --- a/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/introduction/IntroductionManagerImpl.java @@ -555,7 +555,8 @@ class IntroductionManagerImpl extends ConversationClientImpl IntroducerSession s, MessageId storageId, Introducee i, LocalAuthor localAuthor) throws DbException { if (db.containsContact(txn, i.author.getId(), localAuthor.getId())) { - IntroducerSession session = introducerEngine.onAbortAction(txn, s); + IntroducerSession session = + introducerEngine.onIntroduceeRemoved(txn, i, s); storeSession(txn, storageId, session); } else { db.removeMessage(txn, storageId); diff --git a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java index 875825288e..b3161af64d 100644 --- a/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/introduction/IntroductionIntegrationTest.java @@ -44,6 +44,7 @@ import java.util.concurrent.TimeoutException; import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH; import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID; import static org.briarproject.bramble.test.TestUtils.getRandomBytes; +import static org.briarproject.bramble.test.TestUtils.getSecretKey; import static org.briarproject.bramble.test.TestUtils.getTransportProperties; import static org.briarproject.bramble.test.TestUtils.getTransportPropertiesMap; import static org.briarproject.briar.api.introduction.IntroductionManager.CLIENT_ID; @@ -918,6 +919,51 @@ public class IntroductionIntegrationTest .getMessageMetadataAsDictionary(group0.getId()).size()); } + @Test + public void testIntroductionAfterReAddingContacts() throws Exception { + // make introduction + long time = clock.currentTimeMillis(); + introductionManager0 + .makeIntroduction(contact1From0, contact2From0, null, time); + + // 0 and 1 remove and re-add each other + contactManager0.removeContact(contactId1From0); + contactManager1.removeContact(contactId0From1); + contactId1From0 = contactManager0 + .addContact(author1, author0.getId(), getSecretKey(), + clock.currentTimeMillis(), true, true, true); + contact1From0 = contactManager0.getContact(contactId1From0); + contactId0From1 = contactManager1 + .addContact(author0, author1.getId(), getSecretKey(), + clock.currentTimeMillis(), true, true, true); + contact0From1 = contactManager1.getContact(contactId0From1); + + // Sync initial client versioning updates and transport properties + sync0To1(1, true); + sync1To0(1, true); + sync0To1(2, true); + sync1To0(1, true); + + // a new introduction should be possible + assertTrue(introductionManager0 + .canIntroduce(contact1From0, contact2From0)); + + // listen to events, so we don't miss new request + addListeners(true, true); + + // make new introduction + time = clock.currentTimeMillis(); + introductionManager0 + .makeIntroduction(contact1From0, contact2From0, null, time); + + // introduction should sync and not be INVALID or PENDING + sync0To1(1, true); + + // assert that new request was received + eventWaiter.await(TIMEOUT, 1); + assertTrue(listener1.requestReceived); + } + private void testModifiedResponse(StateVisitor visitor) throws Exception { addListeners(true, true); @@ -1099,6 +1145,7 @@ public class IntroductionIntegrationTest protected volatile boolean aborted = false; protected volatile Event latestEvent; + @SuppressWarnings("WeakerAccess") IntroductionResponse getResponse() { assertTrue( latestEvent instanceof IntroductionResponseReceivedEvent); -- GitLab