WIP: Replace session states in the Engine classes

This commit is contained in:
Santiago Torres
2016-05-24 01:41:35 -04:00
parent 89ab4dd04e
commit 62527a62c1
13 changed files with 1584 additions and 513 deletions

View File

@@ -36,34 +36,31 @@ import org.junit.Test;
import java.security.SecureRandom;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.introduction.IntroduceeProtocolState.AWAIT_LOCAL_RESPONSE;
import static org.briarproject.api.introduction.IntroduceeProtocolState.AWAIT_REQUEST;
import static org.briarproject.api.introduction.IntroduceeProtocolState.AWAIT_RESPONSES;
import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT;
import static org.briarproject.api.introduction.IntroductionConstants.ANSWERED;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.EXISTS;
import static org.briarproject.api.introduction.IntroductionConstants.E_PUBLIC_KEY;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID;
import static org.briarproject.api.introduction.IntroductionConstants.INTRODUCER;
import static org.briarproject.api.introduction.IntroductionConstants.LOCAL_AUTHOR_ID;
import static org.briarproject.api.introduction.IntroductionConstants.MAC_LENGTH;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_ID;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
import static org.briarproject.api.introduction.IntroductionConstants.NOT_OUR_RESPONSE;
import static org.briarproject.api.introduction.IntroductionConstants.PUBLIC_KEY;
import static org.briarproject.api.introduction.IntroductionConstants.REMOTE_AUTHOR_ID;
import static org.briarproject.api.introduction.IntroductionConstants.REMOTE_AUTHOR_IS_US;
import static org.briarproject.api.introduction.IntroductionConstants.ROLE;
import static org.briarproject.api.introduction.IntroductionConstants.ROLE_INTRODUCEE;
import static org.briarproject.api.introduction.IntroductionConstants.SESSION_ID;
import static org.briarproject.api.introduction.IntroductionConstants.STATE;
import static org.briarproject.api.introduction.IntroductionConstants.STORAGE_ID;
import static org.briarproject.api.introduction.IntroductionConstants.TIME;
import static org.briarproject.api.introduction.IntroductionConstants.TRANSPORT;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.introduction.IntroduceeSessionState.fromBdfDictionary;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class IntroduceeManagerTest extends BriarTestCase {
@@ -171,12 +168,18 @@ public class IntroduceeManagerTest extends BriarTestCase {
msg.put(NAME, introducee2.getAuthor().getName());
msg.put(PUBLIC_KEY, introducee2.getAuthor().getPublicKey());
final BdfDictionary state =
initializeSessionState(txn, introductionGroup1.getId(), msg);
final IntroduceeSessionState state =
initializeSessionState(txn, sessionId,
introductionGroup1.getId(), msg);
final BdfDictionary statedict = state.toBdfDictionary();
statedict.put(STATE, AWAIT_RESPONSES.getValue());
statedict.put(SESSION_ID, sessionId);
statedict.put(PUBLIC_KEY, msg.getRaw(PUBLIC_KEY));
context.checking(new Expectations() {{
oneOf(clientHelper).mergeMessageMetadata(txn,
localStateMessage.getId(), state);
localStateMessage.getId(), statedict);
}});
introduceeManager.incomingMessage(txn, state, msg);
@@ -199,19 +202,31 @@ public class IntroduceeManagerTest extends BriarTestCase {
msg.put(NAME, introducee2.getAuthor().getName());
msg.put(PUBLIC_KEY, introducee2.getAuthor().getPublicKey());
final BdfDictionary state =
initializeSessionState(txn, introductionGroup1.getId(), msg);
state.put(STATE, IntroduceeProtocolState.AWAIT_RESPONSES.ordinal());
final IntroduceeSessionState state =
initializeSessionState(txn, sessionId,
introductionGroup1.getId(), msg);
state.setTheirTime(time);
state.setTransport(new BdfDictionary());
final BdfDictionary statedict = state.toBdfDictionary();
state.setState(AWAIT_RESPONSES);
statedict.put(STATE, AWAIT_LOCAL_RESPONSE.getValue());
statedict.put(ACCEPT, true);
statedict.put(E_PUBLIC_KEY,
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
statedict.put(NOT_OUR_RESPONSE, message1.getId().getBytes());
// turn request message into a response
msg.put(ACCEPT, true);
msg.put(TIME, time);
msg.put(E_PUBLIC_KEY, TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
msg.put(E_PUBLIC_KEY, statedict.getRaw(E_PUBLIC_KEY));
msg.put(TRANSPORT, new BdfDictionary());
context.checking(new Expectations() {{
oneOf(clientHelper).mergeMessageMetadata(txn,
localStateMessage.getId(), state);
localStateMessage.getId(), statedict);
}});
introduceeManager.incomingMessage(txn, state, msg);
@@ -221,9 +236,69 @@ public class IntroduceeManagerTest extends BriarTestCase {
assertFalse(txn.isComplete());
}
private BdfDictionary initializeSessionState(final Transaction txn,
final GroupId groupId, final BdfDictionary msg)
@Test
public void testInitialSerialization() throws DbException, FormatException {
IntroduceeSessionState state = initializeDefaultSessionState();
BdfDictionary statedict = state.toBdfDictionary();
assertEquals(statedict, fromBdfDictionary(statedict).toBdfDictionary());
// assertEquals(state, fromBdfDictionary(statedict));
}
@Test
public void testFullSerialization() throws DbException, FormatException {
IntroduceeSessionState state = initializeDefaultSessionState();
state.setOurMac(TestUtils.getRandomBytes(42));
// TODO use ALL setters here to make the test cover everything
state.setState(IntroduceeProtocolState.AWAIT_ACK);
state.setAccept(true);
state.setAnswered(true);
state.setOurTime(-1L);
state.setTheirTime(-2L);
state.setMessage("");
state.setEPublicKey(TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
state.setTransport(new BdfDictionary());
state.setContactExists(false);
state.setRemoteAuthorId(new AuthorId(TestUtils.getRandomId()));
state.setRemoteAuthorIsUs(false);
state.setOtherResponseId(TestUtils.getRandomId());
state.setTask(-1);
state.setName(TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH));
state.setOurPublicKey(TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
state.setOurPrivateKey(TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
state.setIntroducedPublicKey(TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
state.setIntroducedName(TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH));
state.setMac(TestUtils.getRandomBytes(MAC_LENGTH));
state.setSignature(TestUtils.getRandomBytes(MAX_SIGNATURE_LENGTH));
state.setOurSignature(TestUtils.getRandomBytes(MAX_SIGNATURE_LENGTH));
state.setOurTransport(new BdfDictionary());
state.setNonce(TestUtils.getRandomBytes(32));
state.setMacKey(TestUtils.getRandomBytes(32));
BdfDictionary statedict = state.toBdfDictionary();
assertEquals(statedict, fromBdfDictionary(statedict).toBdfDictionary());
}
private IntroduceeSessionState initializeDefaultSessionState()
throws DbException, FormatException {
final BdfDictionary msg = new BdfDictionary();
msg.put(TYPE, TYPE_REQUEST);
msg.put(GROUP_ID, introductionGroup1.getId());
msg.put(SESSION_ID, sessionId);
msg.put(MESSAGE_ID, message1.getId());
msg.put(MESSAGE_TIME, time);
msg.put(NAME, introducee2.getAuthor().getName());
msg.put(PUBLIC_KEY, introducee2.getAuthor().getPublicKey());
msg.put(TRANSPORT, new BdfDictionary());
return initializeSessionState(txn, sessionId,
introductionGroup1.getId(), msg);
}
private IntroduceeSessionState initializeSessionState(final Transaction txn,
final SessionId sessionId, final GroupId groupId,
final BdfDictionary msg) throws DbException, FormatException {
final SecureRandom secureRandom = context.mock(SecureRandom.class);
final Bytes salt = new Bytes(new byte[64]);
@@ -231,19 +306,17 @@ public class IntroduceeManagerTest extends BriarTestCase {
new BdfEntry(CONTACT, introducee1.getId().getInt())
);
final boolean contactExists = true;
final BdfDictionary state = new BdfDictionary();
state.put(STORAGE_ID, localStateMessage.getId());
state.put(STATE, AWAIT_REQUEST.getValue());
state.put(ROLE, ROLE_INTRODUCEE);
state.put(GROUP_ID, groupId);
state.put(INTRODUCER, introducer.getAuthor().getName());
state.put(CONTACT_ID_1, introducer.getId().getInt());
state.put(LOCAL_AUTHOR_ID, introducer.getLocalAuthorId().getBytes());
state.put(NOT_OUR_RESPONSE, localStateMessage.getId());
state.put(ANSWERED, false);
state.put(EXISTS, true);
state.put(REMOTE_AUTHOR_ID, introducee2.getAuthor().getId());
state.put(REMOTE_AUTHOR_IS_US, false);
final IntroduceeSessionState state = new IntroduceeSessionState(
localStateMessage.getId(),
sessionId, groupId, introducer.getId(),
introducer.getAuthor().getId(), introducer.getAuthor().getName(),
introducer.getLocalAuthorId(), AWAIT_REQUEST);
state.setName(msg.getString(NAME));
state.setContactExists(true);
state.setRemoteAuthorIsUs(false);
state.setRemoteAuthorId(introducee2.getAuthor().getId());
final BdfDictionary statedict = state.toBdfDictionary();
context.checking(new Expectations() {{
oneOf(clock).currentTimeMillis();
@@ -279,10 +352,12 @@ public class IntroduceeManagerTest extends BriarTestCase {
// store session state
oneOf(clientHelper)
.addLocalMessage(txn, localStateMessage, state, false);
.addLocalMessage(txn, localStateMessage, statedict,
false);
}});
BdfDictionary result = introduceeManager.initialize(txn, groupId, msg);
IntroduceeSessionState result = introduceeManager.initialize(txn,
sessionId, groupId, msg);
context.assertIsSatisfied();
return result;

View File

@@ -19,6 +19,7 @@ import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.system.Clock;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -49,27 +50,30 @@ import static org.briarproject.api.introduction.IntroductionConstants.STATE;
import static org.briarproject.api.introduction.IntroductionConstants.STORAGE_ID;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class IntroducerManagerTest extends BriarTestCase {
final Mockery context;
final IntroducerManager introducerManager;
final CryptoComponent cryptoComponent;
final ClientHelper clientHelper;
final IntroductionGroupFactory introductionGroupFactory;
final MessageSender messageSender;
final Clock clock;
final Contact introducee1;
final Contact introducee2;
final Group localGroup0;
final Group introductionGroup1;
final Group introductionGroup2;
private final Mockery context;
private final IntroducerManager introducerManager;
private final CryptoComponent cryptoComponent;
private final ClientHelper clientHelper;
private final IntroductionGroupFactory introductionGroupFactory;
private final MessageSender messageSender;
private final SecureRandom secureRandom;
private final Clock clock;
private final Contact introducee1;
private final Contact introducee2;
private final Group localGroup0;
private final Group introductionGroup1;
private final Group introductionGroup2;
public IntroducerManagerTest() {
context = new Mockery();
context.setImposteriser(ClassImposteriser.INSTANCE);
messageSender = context.mock(MessageSender.class);
secureRandom = context.mock(SecureRandom.class);
cryptoComponent = context.mock(CryptoComponent.class);
clientHelper = context.mock(ClientHelper.class);
clock = context.mock(Clock.class);
@@ -107,48 +111,109 @@ public class IntroducerManagerTest extends BriarTestCase {
}
@Test
public void testMakeIntroduction() throws DbException, FormatException {
public void testInitializeSessionState()
throws DbException, FormatException {
final Transaction txn = new Transaction(null, false);
final long time = 42L;
context.setImposteriser(ClassImposteriser.INSTANCE);
final SecureRandom secureRandom = context.mock(SecureRandom.class);
final Bytes salt = new Bytes(new byte[64]);
final Message msg = new Message(new MessageId(TestUtils.getRandomId()),
localGroup0.getId(), time, TestUtils.getRandomBytes(64));
final BdfDictionary state = new BdfDictionary();
state.put(SESSION_ID, msg.getId());
state.put(STORAGE_ID, msg.getId());
state.put(STATE, PREPARE_REQUESTS.getValue());
state.put(ROLE, ROLE_INTRODUCER);
state.put(GROUP_ID_1, introductionGroup1.getId());
state.put(GROUP_ID_2, introductionGroup2.getId());
state.put(CONTACT_1, introducee1.getAuthor().getName());
state.put(CONTACT_2, introducee2.getAuthor().getName());
state.put(CONTACT_ID_1, introducee1.getId().getInt());
state.put(CONTACT_ID_2, introducee2.getId().getInt());
state.put(AUTHOR_ID_1, introducee1.getAuthor().getId());
state.put(AUTHOR_ID_2, introducee2.getAuthor().getId());
final BdfDictionary state2 = (BdfDictionary) state.clone();
state2.put(STATE, AWAIT_RESPONSES.getValue());
final IntroducerSessionState state =
getState(msg, introducee1, introducee2);
checkInitialisation(time, salt, msg, txn, state);
IntroducerSessionState result = introducerManager.initialize(txn,
introducee1, introducee2);
assertEquals(state.toBdfDictionary(), result.toBdfDictionary());
context.assertIsSatisfied();
}
@Test
public void testMakeIntroduction() throws DbException, FormatException {
final Transaction txn = new Transaction(null, false);
final long time = 42L;
final Bytes salt = new Bytes(new byte[64]);
final Message msg = new Message(new MessageId(TestUtils.getRandomId()),
localGroup0.getId(), time, TestUtils.getRandomBytes(64));
final IntroducerSessionState state =
getState(msg, introducee1, introducee2);
checkInitialisation(time, salt, msg, txn, state);
final IntroducerSessionState state2 = state;
state2.setState(AWAIT_RESPONSES);
final BdfDictionary msg1 = new BdfDictionary();
msg1.put(TYPE, TYPE_REQUEST);
msg1.put(SESSION_ID, state.getRaw(SESSION_ID));
msg1.put(GROUP_ID, state.getRaw(GROUP_ID_1));
msg1.put(NAME, state.getString(CONTACT_2));
msg1.put(SESSION_ID, state.getSessionId().getBytes());
msg1.put(GROUP_ID, state.getGroup1Id().getBytes());
msg1.put(NAME, state.getContact2Name());
msg1.put(PUBLIC_KEY, introducee2.getAuthor().getPublicKey());
final BdfDictionary msg1send = (BdfDictionary) msg1.clone();
msg1send.put(MESSAGE_TIME, time);
final BdfDictionary msg2 = new BdfDictionary();
msg2.put(TYPE, TYPE_REQUEST);
msg2.put(SESSION_ID, state.getRaw(SESSION_ID));
msg2.put(GROUP_ID, state.getRaw(GROUP_ID_2));
msg2.put(NAME, state.getString(CONTACT_1));
msg2.put(SESSION_ID, state.getSessionId().getBytes());
msg2.put(GROUP_ID, state.getGroup2Id().getBytes());
msg2.put(NAME, state.getContact1Name());
msg2.put(PUBLIC_KEY, introducee1.getAuthor().getPublicKey());
final BdfDictionary msg2send = (BdfDictionary) msg2.clone();
msg2send.put(MESSAGE_TIME, time);
context.checking(new Expectations() {{
// send message
oneOf(clientHelper).mergeMessageMetadata(txn, msg.getId(),
state2.toBdfDictionary());
oneOf(messageSender).sendMessage(txn, msg1send);
oneOf(messageSender).sendMessage(txn, msg2send);
}});
introducerManager
.makeIntroduction(txn, introducee1, introducee2, null, time);
context.assertIsSatisfied();
assertFalse(txn.isComplete());
}
private IntroducerSessionState getState(Message msg, Contact c1, Contact c2)
throws FormatException {
final BdfDictionary d = new BdfDictionary();
d.put(SESSION_ID, new SessionId(msg.getId().getBytes()));
d.put(STORAGE_ID, msg.getId());
d.put(STATE, PREPARE_REQUESTS.getValue());
d.put(ROLE, ROLE_INTRODUCER);
d.put(GROUP_ID_1, introductionGroup1.getId());
d.put(GROUP_ID_2, introductionGroup2.getId());
d.put(CONTACT_1, introducee1.getAuthor().getName());
d.put(CONTACT_2, introducee2.getAuthor().getName());
d.put(CONTACT_ID_1, introducee1.getId().getInt());
d.put(CONTACT_ID_2, introducee2.getId().getInt());
d.put(AUTHOR_ID_1, introducee1.getAuthor().getId());
d.put(AUTHOR_ID_2, introducee2.getAuthor().getId());
IntroducerSessionState state =
IntroducerSessionState.fromBdfDictionary(d);
assertEquals(d, state.toBdfDictionary());
return state;
}
private void checkInitialisation(final long time, final Bytes salt,
final Message msg, final Transaction txn,
final IntroducerSessionState state)
throws FormatException, DbException {
context.checking(new Expectations() {{
// initialize and store session state
oneOf(clock).currentTimeMillis();
@@ -167,20 +232,14 @@ public class IntroducerManagerTest extends BriarTestCase {
oneOf(introductionGroupFactory)
.createIntroductionGroup(introducee2);
will(returnValue(introductionGroup2));
oneOf(clientHelper).addLocalMessage(txn, msg, state, false);
oneOf(clientHelper).addLocalMessage(txn, msg,
state.toBdfDictionary(), false);
// send message
oneOf(clientHelper).mergeMessageMetadata(txn, msg.getId(), state2);
oneOf(messageSender).sendMessage(txn, msg1send);
oneOf(messageSender).sendMessage(txn, msg2send);
//// oneOf(clientHelper).mergeMessageMetadata(txn, msg.getId(), state2);
//// oneOf(messageSender).sendMessage(txn, msg1send);
//// oneOf(messageSender).sendMessage(txn, msg2send);
}});
introducerManager
.makeIntroduction(txn, introducee1, introducee2, null, time);
context.assertIsSatisfied();
assertFalse(txn.isComplete());
}
private ClientId getClientId() {

View File

@@ -6,6 +6,7 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.MessageQueueManager;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
@@ -18,7 +19,7 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.introduction.IntroducerProtocolState;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
@@ -37,39 +38,58 @@ import java.util.Map;
import static junit.framework.TestCase.assertTrue;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.introduction.IntroduceeProtocolState.AWAIT_REQUEST;
import static org.briarproject.api.introduction.IntroductionConstants.AUTHOR_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.AUTHOR_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_1;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_2;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.CONTACT_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.LOCAL_AUTHOR_ID;
import static org.briarproject.api.introduction.IntroductionConstants.REMOTE_AUTHOR_IS_US;
import static org.briarproject.api.introduction.IntroductionConstants.ROLE;
import static org.briarproject.api.introduction.IntroductionConstants.ROLE_INTRODUCEE;
import static org.briarproject.api.introduction.IntroductionConstants.ROLE_INTRODUCER;
import static org.briarproject.api.introduction.IntroductionConstants.SESSION_ID;
import static org.briarproject.api.introduction.IntroductionConstants.STORAGE_ID;
import static org.briarproject.api.introduction.IntroductionConstants.STATE;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE;
import static org.briarproject.api.introduction.IntroductionConstants.TIME;
import static org.briarproject.api.introduction.IntroductionConstants.OUR_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
import static org.briarproject.api.introduction.IntroductionConstants.INTRODUCER;
import static org.briarproject.api.introduction.IntroductionConstants.EXISTS;
import static org.briarproject.api.introduction.IntroductionConstants.NO_TASK;
import static org.briarproject.api.introduction.IntroductionConstants.TASK;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.junit.Assert.assertFalse;
public class IntroductionManagerImplTest extends BriarTestCase {
final Mockery context;
final IntroductionManagerImpl introductionManager;
final IntroducerManager introducerManager;
final IntroduceeManager introduceeManager;
final DatabaseComponent db;
final PrivateGroupFactory privateGroupFactory;
final ClientHelper clientHelper;
final MetadataEncoder metadataEncoder;
final MessageQueueManager messageQueueManager;
final IntroductionGroupFactory introductionGroupFactory;
final Clock clock;
final SessionId sessionId = new SessionId(TestUtils.getRandomId());
final long time = 42L;
final Contact introducee1;
final Contact introducee2;
final Group localGroup0;
final Group introductionGroup1;
final Group introductionGroup2;
final Message message1;
Transaction txn;
private final Mockery context;
private final IntroductionManagerImpl introductionManager;
private final IntroducerManager introducerManager;
private final IntroduceeManager introduceeManager;
private final DatabaseComponent db;
private final PrivateGroupFactory privateGroupFactory;
private final ClientHelper clientHelper;
private final MetadataEncoder metadataEncoder;
private final MessageQueueManager messageQueueManager;
private final IntroductionGroupFactory introductionGroupFactory;
private final Clock clock;
private final SessionId sessionId = new SessionId(TestUtils.getRandomId());
private final long time = 42L;
private final Contact introducee1;
private final Contact introducee2;
private final Group localGroup0;
private final Group introductionGroup1;
private final Group introductionGroup2;
private final Message message1;
private Transaction txn;
public IntroductionManagerImplTest() {
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
@@ -145,14 +165,33 @@ public class IntroductionManagerImplTest extends BriarTestCase {
assertTrue(txn.isComplete());
}
@Test
@Test
public void testAcceptIntroduction() throws DbException, FormatException {
final BdfDictionary state = BdfDictionary.of(
new BdfEntry(ROLE, ROLE_INTRODUCEE),
new BdfEntry(GROUP_ID_1, introductionGroup1.getId()),
new BdfEntry(GROUP_ID_2, introductionGroup2.getId())
new BdfEntry(GROUP_ID_2, introductionGroup2.getId()),
new BdfEntry(SESSION_ID, sessionId),
new BdfEntry(STORAGE_ID, sessionId),
new BdfEntry(AUTHOR_ID_1,introducee1.getAuthor().getId()),
new BdfEntry(CONTACT_1, introducee1.getAuthor().getName()),
new BdfEntry(CONTACT_ID_1, introducee1.getId().getInt()),
new BdfEntry(AUTHOR_ID_2,introducee2.getAuthor().getId() ),
new BdfEntry(CONTACT_2, introducee2.getAuthor().getName()),
new BdfEntry(CONTACT_ID_2, introducee2.getId().getInt()),
new BdfEntry(STATE, AWAIT_REQUEST.getValue()),
new BdfEntry(TIME, time),
new BdfEntry(OUR_TIME, time),
new BdfEntry(NAME, introducee1.getAuthor().getName()),
new BdfEntry(LOCAL_AUTHOR_ID, introducee1.getLocalAuthorId()),
new BdfEntry(INTRODUCER, introducee1.getAuthor().getName()),
new BdfEntry(EXISTS, false),
new BdfEntry(TASK, NO_TASK),
new BdfEntry(REMOTE_AUTHOR_IS_US, false)
);
txn = new Transaction(null, false);
context.checking(new Expectations() {{
oneOf(db).startTransaction(false);
will(returnValue(txn));
@@ -162,7 +201,8 @@ public class IntroductionManagerImplTest extends BriarTestCase {
will(returnValue(introductionGroup1));
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
will(returnValue(state));
oneOf(introduceeManager).acceptIntroduction(txn, state, time);
oneOf(introduceeManager).acceptIntroduction(with(equal(txn)),
with(any(IntroduceeSessionState.class)), with(equal(time)));
oneOf(db).endTransaction(txn);
}});
@@ -176,8 +216,26 @@ public class IntroductionManagerImplTest extends BriarTestCase {
@Test
public void testDeclineIntroduction() throws DbException, FormatException {
final BdfDictionary state = BdfDictionary.of(
new BdfEntry(ROLE, ROLE_INTRODUCEE),
new BdfEntry(GROUP_ID_1, introductionGroup1.getId()),
new BdfEntry(GROUP_ID_2, introductionGroup2.getId())
new BdfEntry(GROUP_ID_2, introductionGroup2.getId()),
new BdfEntry(SESSION_ID, sessionId),
new BdfEntry(STORAGE_ID, sessionId),
new BdfEntry(AUTHOR_ID_1,introducee1.getAuthor().getId()),
new BdfEntry(CONTACT_1, introducee1.getAuthor().getName()),
new BdfEntry(CONTACT_ID_1, introducee1.getId().getInt()),
new BdfEntry(AUTHOR_ID_2,introducee2.getAuthor().getId() ),
new BdfEntry(CONTACT_2, introducee2.getAuthor().getName()),
new BdfEntry(CONTACT_ID_2, introducee2.getId().getInt()),
new BdfEntry(STATE, AWAIT_REQUEST.getValue()),
new BdfEntry(TIME, time),
new BdfEntry(OUR_TIME, time),
new BdfEntry(NAME, introducee1.getAuthor().getName()),
new BdfEntry(LOCAL_AUTHOR_ID, introducee1.getLocalAuthorId()),
new BdfEntry(INTRODUCER, introducee1.getAuthor().getName()),
new BdfEntry(EXISTS, false),
new BdfEntry(TASK, NO_TASK),
new BdfEntry(REMOTE_AUTHOR_IS_US, false)
);
txn = new Transaction(null, false);
@@ -190,7 +248,8 @@ public class IntroductionManagerImplTest extends BriarTestCase {
will(returnValue(introductionGroup1));
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
will(returnValue(state));
oneOf(introduceeManager).declineIntroduction(txn, state, time);
oneOf(introduceeManager).declineIntroduction(with(equal(txn)),
with(any(IntroduceeSessionState.class)), with(equal(time)));
oneOf(db).endTransaction(txn);
}});
@@ -238,19 +297,23 @@ public class IntroductionManagerImplTest extends BriarTestCase {
final BdfDictionary msg = new BdfDictionary();
msg.put(TYPE, TYPE_REQUEST);
final BdfDictionary state = new BdfDictionary();
final IntroduceeSessionState state = initializeIntroduceeSS();
txn = new Transaction(null, false);
final SessionId sessionId = new SessionId(TestUtils.getRandomId());
msg.put(SESSION_ID, sessionId);
context.checking(new Expectations() {{
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
new MessageId(sessionId.getBytes()));
will(returnValue(state.toBdfDictionary()));
oneOf(introduceeManager)
.initialize(txn, introductionGroup1.getId(), msg);
.initialize(txn, sessionId, introductionGroup1.getId(),
msg);
will(returnValue(state));
oneOf(introduceeManager)
.incomingMessage(txn, state, msg);
oneOf(introduceeManager).incomingMessage(txn, state, msg);
}});
introductionManager
.incomingMessage(txn, message1, new BdfList(), msg);
introductionManager.incomingMessage(txn, message1, new BdfList(), msg);
context.assertIsSatisfied();
assertFalse(txn.isComplete());
@@ -265,7 +328,8 @@ public class IntroductionManagerImplTest extends BriarTestCase {
new BdfEntry(SESSION_ID, sessionId)
);
final BdfDictionary state = new BdfDictionary();
final IntroducerSessionState sessionState = initializeIntroducerSS();
final BdfDictionary state = sessionState.toBdfDictionary();
state.put(ROLE, ROLE_INTRODUCER);
state.put(GROUP_ID_1, introductionGroup1.getId());
state.put(GROUP_ID_2, introductionGroup2.getId());
@@ -275,7 +339,8 @@ public class IntroductionManagerImplTest extends BriarTestCase {
context.checking(new Expectations() {{
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
will(returnValue(state));
oneOf(introducerManager).incomingMessage(txn, state, msg);
oneOf(introducerManager).incomingMessage(with(equal(txn)),
with(any(IntroducerSessionState.class)), with(equal(msg)));
}});
introductionManager
@@ -285,5 +350,44 @@ public class IntroductionManagerImplTest extends BriarTestCase {
assertFalse(txn.isComplete());
}
private IntroduceeSessionState initializeIntroduceeSS() {
final ContactId cid = new ContactId(0);
final AuthorId aid = new AuthorId(TestUtils.getRandomId());
Author author = new Author(aid, "Introducer",
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
final Contact introducer = new Contact(cid, author, aid, true, false);
final IntroduceeSessionState state = new IntroduceeSessionState(
new MessageId(TestUtils.getRandomId()),
new SessionId(TestUtils.getRandomId()),
new GroupId(TestUtils.getRandomId()),
introducer.getId(), introducer.getAuthor().getId(),
introducer.getAuthor().getName(), introducer.getLocalAuthorId(),
AWAIT_REQUEST);
state.setContactExists(true);
state.setRemoteAuthorIsUs(false);
state.setRemoteAuthorId(introducee2.getAuthor().getId());
state.setName(introducee2.getAuthor().getName());
return state;
}
private IntroducerSessionState initializeIntroducerSS() {
final ContactId cid = new ContactId(0);
final AuthorId aid = new AuthorId(TestUtils.getRandomId());
Author author = new Author(aid, "Introducer",
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
final Contact introducer = new Contact(cid, author, aid, true, false);
return new IntroducerSessionState(
new MessageId(TestUtils.getRandomId()),
new SessionId(TestUtils.getRandomId()),
new GroupId(TestUtils.getRandomId()),
new GroupId(TestUtils.getRandomId()),
introducer.getId(), introducer.getAuthor().getId(), introducer.getAuthor().getName(),
introducer.getId(), introducer.getAuthor().getId(), introducer.getAuthor().getName(),
IntroducerProtocolState.AWAIT_RESPONSES);
}
}