Only allow new introductions in START state

When the user attempts an introduction, instead of the introduction
message input field, an explanatory text will be shown and the
introduction can not be made until the last one has been finished.
This commit is contained in:
Torsten Grote
2018-04-25 12:05:15 -03:00
parent 94a6137a42
commit b291fcd2cd
6 changed files with 88 additions and 13 deletions

View File

@@ -47,6 +47,7 @@ import javax.inject.Inject;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.briar.api.introduction.Role.INTRODUCEE;
import static org.briarproject.briar.api.introduction.Role.INTRODUCER;
import static org.briarproject.briar.introduction.IntroducerState.START;
import static org.briarproject.briar.introduction.IntroductionConstants.GROUP_KEY_CONTACT_ID;
import static org.briarproject.briar.introduction.MessageType.ABORT;
import static org.briarproject.briar.introduction.MessageType.ACCEPT;
@@ -267,6 +268,28 @@ class IntroductionManagerImpl extends ConversationClientImpl
}
}
@Override
public boolean canIntroduce(Contact c1, Contact c2) throws DbException {
Transaction txn = db.startTransaction(true);
try {
// Look up the session, if there is one
Author introducer = identityManager.getLocalAuthor(txn);
SessionId sessionId =
crypto.getSessionId(introducer, c1.getAuthor(),
c2.getAuthor());
StoredSession ss = getSession(txn, sessionId);
if (ss == null) return true;
IntroducerSession session =
sessionParser.parseIntroducerSession(ss.bdfSession);
if (session.getState() == START) return true;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return false;
}
@Override
public void makeIntroduction(Contact c1, Contact c2, @Nullable String msg,
long timestamp) throws DbException {
@@ -398,12 +421,11 @@ class IntroductionManagerImpl extends ConversationClientImpl
Role role = sessionParser.getRole(bdfSession);
SessionId sessionId;
Author author;
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);
if (role == INTRODUCER) {
IntroducerSession session =
sessionParser.parseIntroducerSession(bdfSession);
sessionId = session.getSessionId();
if (localAuthor.equals(session.getIntroducee1().author)) {
if (contactGroupId.equals(session.getIntroducee1().groupId)) {
author = session.getIntroducee2().author;
} else {
author = session.getIntroducee1().author;
@@ -419,6 +441,7 @@ class IntroductionManagerImpl extends ConversationClientImpl
if (msg == null || body == null) throw new AssertionError();
RequestMessage rm = messageParser.parseRequestMessage(msg, body);
String message = rm.getMessage();
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);
boolean contactExists = contactManager
.contactExists(txn, rm.getAuthor().getId(),
localAuthor.getId());

View File

@@ -23,6 +23,7 @@ import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.test.TestDatabaseModule;
import org.briarproject.briar.api.client.ProtocolStateException;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.introduction.IntroductionManager;
import org.briarproject.briar.api.introduction.IntroductionMessage;
@@ -446,6 +447,26 @@ public class IntroductionIntegrationTest
assertFalse(listener2.aborted);
}
@Test(expected = ProtocolStateException.class)
public void testDoubleIntroduction() throws Exception {
// we can make an introduction
assertTrue(introductionManager0
.canIntroduce(contact1From0, contact2From0));
// make the introduction
long time = clock.currentTimeMillis();
introductionManager0
.makeIntroduction(contact1From0, contact2From0, null, time);
// no more introduction allowed while the existing one is in progress
assertFalse(introductionManager0
.canIntroduce(contact1From0, contact2From0));
// try it anyway and fail
introductionManager0
.makeIntroduction(contact1From0, contact2From0, null, time);
}
@Test
public void testIntroducerRemovedCleanup() throws Exception {
addListeners(true, true);