Addressed review comments.

This commit is contained in:
akwizgran
2016-11-29 11:36:12 +00:00
parent b0b932a01c
commit 90124e00ca

View File

@@ -36,7 +36,7 @@ import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser; import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@@ -46,6 +46,8 @@ import static junit.framework.TestCase.fail;
import static org.briarproject.TestUtils.getRandomBytes; import static org.briarproject.TestUtils.getRandomBytes;
import static org.briarproject.TestUtils.getRandomId; import static org.briarproject.TestUtils.getRandomId;
import static org.briarproject.TestUtils.getRandomString; import static org.briarproject.TestUtils.getRandomString;
import static org.briarproject.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH;
import static org.briarproject.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID; import static org.briarproject.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID;
import static org.briarproject.api.sync.Group.Visibility.SHARED; import static org.briarproject.api.sync.Group.Visibility.SHARED;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH; import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
@@ -60,7 +62,6 @@ import static org.junit.Assert.assertTrue;
public class GroupInvitationManagerImplTest extends BriarMockTestCase { public class GroupInvitationManagerImplTest extends BriarMockTestCase {
private final GroupInvitationManagerImpl groupInvitationManager;
private final DatabaseComponent db = context.mock(DatabaseComponent.class); private final DatabaseComponent db = context.mock(DatabaseComponent.class);
private final ClientHelper clientHelper = context.mock(ClientHelper.class); private final ClientHelper clientHelper = context.mock(ClientHelper.class);
private final ContactGroupFactory contactGroupFactory = private final ContactGroupFactory contactGroupFactory =
@@ -77,12 +78,17 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
context.mock(SessionEncoder.class); context.mock(SessionEncoder.class);
private final ProtocolEngineFactory engineFactory = private final ProtocolEngineFactory engineFactory =
context.mock(ProtocolEngineFactory.class); context.mock(ProtocolEngineFactory.class);
private final CreatorProtocolEngine creatorEngine; private final CreatorProtocolEngine creatorEngine;
private final InviteeProtocolEngine inviteeEngine; private final InviteeProtocolEngine inviteeEngine;
private final PeerProtocolEngine peerEngine; private final PeerProtocolEngine peerEngine;
private final CreatorSession creatorSession; private final CreatorSession creatorSession;
private final InviteeSession inviteeSession; private final InviteeSession inviteeSession;
private final PeerSession peerSession; private final PeerSession peerSession;
private final MessageMetadata messageMetadata;
private final GroupInvitationManagerImpl groupInvitationManager;
private final Group localGroup = private final Group localGroup =
new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5)); new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
private final Transaction txn = new Transaction(null, false); private final Transaction txn = new Transaction(null, false);
@@ -97,14 +103,23 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5)); new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
private final Group privateGroup = private final Group privateGroup =
new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5)); new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
private final BdfDictionary meta = BdfDictionary.of(new BdfEntry("f", "o")); private final BdfDictionary meta = BdfDictionary.of(new BdfEntry("m", "e"));
private final Message message = private final Message message =
new Message(new MessageId(getRandomId()), contactGroup.getId(), new Message(new MessageId(getRandomId()), contactGroup.getId(),
0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1)); 0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
private final BdfList body = new BdfList(); private final BdfList body = BdfList.of("body");
private final SessionId sessionId =
new SessionId(privateGroup.getId().getBytes());
private final Message storageMessage = private final Message storageMessage =
new Message(new MessageId(getRandomId()), contactGroup.getId(), new Message(new MessageId(getRandomId()), contactGroup.getId(),
0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1)); 0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
private final BdfDictionary bdfSession =
BdfDictionary.of(new BdfEntry("f", "o"));
private final Map<MessageId, BdfDictionary> oneResult =
Collections.singletonMap(storageMessage.getId(), bdfSession);
private final Map<MessageId, BdfDictionary> noResults =
Collections.emptyMap();
public GroupInvitationManagerImplTest() { public GroupInvitationManagerImplTest() {
context.setImposteriser(ClassImposteriser.INSTANCE); context.setImposteriser(ClassImposteriser.INSTANCE);
@@ -116,6 +131,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
inviteeSession = context.mock(InviteeSession.class); inviteeSession = context.mock(InviteeSession.class);
peerSession = context.mock(PeerSession.class); peerSession = context.mock(PeerSession.class);
messageMetadata = context.mock(MessageMetadata.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(engineFactory).createCreatorEngine(); oneOf(engineFactory).createCreatorEngine();
will(returnValue(creatorEngine)); will(returnValue(creatorEngine));
@@ -179,11 +196,11 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, c); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, c);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
}}); }});
expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(), expectGetSession(noResults, new SessionId(g.getBytes()),
new SessionId(g.getBytes())); contactGroup.getId());
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(peerEngine).onMemberAddedAction(with(equal(txn)), oneOf(peerEngine).onMemberAddedAction(with(txn),
with(any(PeerSession.class))); with(any(PeerSession.class)));
will(returnValue(peerSession)); will(returnValue(peerSession));
}}); }});
@@ -210,16 +227,16 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
}}); }});
} }
private void expectGetSession(final Map<MessageId, BdfDictionary> result, private void expectGetSession(final Map<MessageId, BdfDictionary> results,
final SessionId sessionId) throws Exception { final SessionId sessionId, final GroupId contactGroupId)
throws Exception {
final BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u")); final BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u"));
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(sessionParser).getSessionQuery(sessionId); oneOf(sessionParser).getSessionQuery(sessionId);
will(returnValue(query)); will(returnValue(query));
oneOf(clientHelper) oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
.getMessageMetadataAsDictionary(txn, contactGroup.getId(), contactGroupId, query);
query); will(returnValue(results));
will(returnValue(result));
}}); }});
} }
@@ -289,45 +306,39 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
private void expectFirstIncomingMessage(Role role, MessageType type) private void expectFirstIncomingMessage(Role role, MessageType type)
throws Exception { throws Exception {
expectIncomingMessage(role, type, expectParseMessageMetadata();
Collections.<MessageId, BdfDictionary>emptyMap()); expectGetSession(noResults, sessionId, contactGroup.getId());
Session session = expectHandleFirstMessage(role, messageMetadata, type);
if (session != null) {
expectCreateStorageId();
expectStoreSession(session, storageMessage.getId());
}
} }
private void expectIncomingMessage(Role role, MessageType type) private void expectParseMessageMetadata() throws Exception {
throws Exception {
BdfDictionary state = BdfDictionary.of(new BdfEntry("state", "test"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
expectIncomingMessage(role, type, states);
}
private void expectIncomingMessage(final Role role,
final MessageType type, final Map<MessageId, BdfDictionary> states)
throws Exception {
final MessageMetadata messageMetadata =
context.mock(MessageMetadata.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).parseMetadata(meta); oneOf(messageParser).parseMetadata(meta);
will(returnValue(messageMetadata)); will(returnValue(messageMetadata));
oneOf(messageMetadata).getPrivateGroupId(); oneOf(messageMetadata).getPrivateGroupId();
will(returnValue(privateGroup.getId())); will(returnValue(privateGroup.getId()));
}}); }});
expectGetSession(states,
new SessionId(privateGroup.getId().getBytes()));
Session session; }
if (states.isEmpty()) {
session = expectHandleFirstMessage(role, messageMetadata, type); private void expectIncomingMessage(Role role, MessageType type)
if (session != null) { throws Exception {
expectCreateStorageId(); BdfDictionary bdfSession = BdfDictionary.of(new BdfEntry("f", "o"));
expectStoreSession(session, storageMessage.getId()); expectIncomingMessageWithSession(role, type, bdfSession);
} }
} else {
assertEquals(1, states.size()); private void expectIncomingMessageWithSession(final Role role,
session = expectHandleMessage(role, messageMetadata, final MessageType type, final BdfDictionary bdfSession)
states.values().iterator().next(), type); throws Exception {
expectStoreSession(session, storageMessage.getId()); expectParseMessageMetadata();
} expectGetSession(oneResult, sessionId, contactGroup.getId());
Session session = expectHandleMessage(role, messageMetadata, bdfSession,
type);
expectStoreSession(session, storageMessage.getId());
} }
@Nullable @Nullable
@@ -353,7 +364,7 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
engine = peerEngine; engine = peerEngine;
session = peerSession; session = peerSession;
} else { } else {
throw new RuntimeException(); throw new AssertionError();
} }
expectIndividualMessage(type, engine, session); expectIndividualMessage(type, engine, session);
return session; return session;
@@ -394,8 +405,7 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
expectIndividualMessage(type, peerEngine, peerSession); expectIndividualMessage(type, peerEngine, peerSession);
return peerSession; return peerSession;
} else { } else {
fail(); throw new AssertionError();
throw new RuntimeException();
} }
} }
@@ -407,9 +417,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).parseInviteMessage(message, body); oneOf(messageParser).parseInviteMessage(message, body);
will(returnValue(msg)); will(returnValue(msg));
oneOf(engine).onInviteMessage(with(equal(txn)), oneOf(engine).onInviteMessage(with(txn),
with(AbstractExpectations.<S>anything()), with(AbstractExpectations.<S>anything()), with(msg));
with(equal(msg)));
will(returnValue(session)); will(returnValue(session));
}}); }});
} else if (type == JOIN) { } else if (type == JOIN) {
@@ -417,9 +426,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).parseJoinMessage(message, body); oneOf(messageParser).parseJoinMessage(message, body);
will(returnValue(msg)); will(returnValue(msg));
oneOf(engine).onJoinMessage(with(equal(txn)), oneOf(engine).onJoinMessage(with(txn),
with(AbstractExpectations.<S>anything()), with(AbstractExpectations.<S>anything()), with(msg));
with(equal(msg)));
will(returnValue(session)); will(returnValue(session));
}}); }});
} else if (type == LEAVE) { } else if (type == LEAVE) {
@@ -427,9 +435,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).parseLeaveMessage(message, body); oneOf(messageParser).parseLeaveMessage(message, body);
will(returnValue(msg)); will(returnValue(msg));
oneOf(engine).onLeaveMessage(with(equal(txn)), oneOf(engine).onLeaveMessage(with(txn),
with(AbstractExpectations.<S>anything()), with(AbstractExpectations.<S>anything()), with(msg));
with(equal(msg)));
will(returnValue(session)); will(returnValue(session));
}}); }});
} else if (type == ABORT) { } else if (type == ABORT) {
@@ -437,9 +444,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).parseAbortMessage(message, body); oneOf(messageParser).parseAbortMessage(message, body);
will(returnValue(msg)); will(returnValue(msg));
oneOf(engine).onAbortMessage(with(equal(txn)), oneOf(engine).onAbortMessage(with(txn),
with(AbstractExpectations.<S>anything()), with(AbstractExpectations.<S>anything()), with(msg));
with(equal(msg)));
will(returnValue(session)); will(returnValue(session));
}}); }});
} else { } else {
@@ -451,11 +457,9 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
public void testSendFirstInvitation() throws Exception { public void testSendFirstInvitation() throws Exception {
final String msg = "Invitation text for first invitation"; final String msg = "Invitation text for first invitation";
final long time = 42L; final long time = 42L;
final byte[] signature = TestUtils.getRandomBytes(42); final byte[] signature = getRandomBytes(42);
Map<MessageId, BdfDictionary> states = Collections.emptyMap();
expectGetSession(states, expectGetSession(noResults, sessionId, contactGroup.getId());
new SessionId(privateGroup.getId().getBytes()));
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(txn)); will(returnValue(txn));
@@ -466,9 +470,9 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
}}); }});
expectCreateStorageId(); expectCreateStorageId();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(creatorEngine).onInviteAction(with(same(txn)), oneOf(creatorEngine).onInviteAction(with(txn),
with(any(CreatorSession.class)), with(equal(msg)), with(any(CreatorSession.class)), with(msg), with(time),
with(equal(time)), with(equal(signature))); with(signature));
will(returnValue(creatorSession)); will(returnValue(creatorSession));
}}); }});
expectStoreSession(creatorSession, storageMessage.getId()); expectStoreSession(creatorSession, storageMessage.getId());
@@ -484,14 +488,9 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
public void testSendSubsequentInvitation() throws Exception { public void testSendSubsequentInvitation() throws Exception {
final String msg = "Invitation text for subsequent invitation"; final String msg = "Invitation text for subsequent invitation";
final long time = 43L; final long time = 43L;
final byte[] signature = TestUtils.getRandomBytes(43); final byte[] signature = getRandomBytes(43);
final BdfDictionary state =
BdfDictionary.of(new BdfEntry("state", "test"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
expectGetSession(states, expectGetSession(oneResult, sessionId, contactGroup.getId());
new SessionId(privateGroup.getId().getBytes()));
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(txn)); will(returnValue(txn));
@@ -500,11 +499,11 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(sessionParser) oneOf(sessionParser)
.parseCreatorSession(contactGroup.getId(), state); .parseCreatorSession(contactGroup.getId(), bdfSession);
will(returnValue(creatorSession)); will(returnValue(creatorSession));
oneOf(creatorEngine).onInviteAction(with(same(txn)), oneOf(creatorEngine).onInviteAction(with(txn),
with(any(CreatorSession.class)), with(equal(msg)), with(any(CreatorSession.class)), with(msg), with(time),
with(equal(time)), with(equal(signature))); with(signature));
will(returnValue(creatorSession)); will(returnValue(creatorSession));
}}); }});
expectStoreSession(creatorSession, storageMessage.getId()); expectStoreSession(creatorSession, storageMessage.getId());
@@ -529,59 +528,48 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(db).endTransaction(txn); oneOf(db).endTransaction(txn);
}}); }});
expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(), expectGetSession(noResults, sessionId, contactGroup.getId());
sessionId);
groupInvitationManager.respondToInvitation(contactId, sessionId, true); groupInvitationManager.respondToInvitation(contactId, sessionId, true);
} }
@Test @Test
public void testAcceptInvitationWithSession() throws Exception { public void testAcceptInvitationWithSession() throws Exception {
final boolean accept = true; expectRespondToInvitation(sessionId, true);
SessionId sessionId = new SessionId(getRandomId());
BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
expectRespondToInvitation(states, sessionId, accept);
groupInvitationManager groupInvitationManager
.respondToInvitation(contactId, sessionId, accept); .respondToInvitation(contactId, sessionId, true);
} }
@Test @Test
public void testDeclineInvitationWithSession() throws Exception { public void testDeclineInvitationWithSession() throws Exception {
final boolean accept = false; expectRespondToInvitation(sessionId, false);
SessionId sessionId = new SessionId(getRandomId());
BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
expectRespondToInvitation(states, sessionId, accept);
groupInvitationManager groupInvitationManager
.respondToInvitation(contactId, sessionId, accept); .respondToInvitation(contactId, sessionId, false);
} }
@Test @Test
public void testRespondToInvitationWithGroupId() throws Exception { public void testAcceptInvitationWithGroupId() throws Exception {
final boolean accept = true; PrivateGroup pg = new PrivateGroup(privateGroup,
final PrivateGroup g = context.mock(PrivateGroup.class); getRandomString(MAX_GROUP_NAME_LENGTH), author,
SessionId sessionId = new SessionId(privateGroup.getId().getBytes()); getRandomBytes(GROUP_SALT_LENGTH));
BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
context.checking(new Expectations() {{ expectRespondToInvitation(sessionId, true);
oneOf(g).getId(); groupInvitationManager.respondToInvitation(contactId, pg, true);
will(returnValue(privateGroup.getId()));
}});
expectRespondToInvitation(states, sessionId, accept);
groupInvitationManager.respondToInvitation(contactId, g, accept);
} }
private void expectRespondToInvitation( @Test
final Map<MessageId, BdfDictionary> states, public void testDeclineInvitationWithGroupId() throws Exception {
final SessionId sessionId, final boolean accept) throws Exception { PrivateGroup pg = new PrivateGroup(privateGroup,
expectGetSession(states, sessionId); getRandomString(MAX_GROUP_NAME_LENGTH), author,
getRandomBytes(GROUP_SALT_LENGTH));
expectRespondToInvitation(sessionId, false);
groupInvitationManager.respondToInvitation(contactId, pg, false);
}
private void expectRespondToInvitation(final SessionId sessionId,
final boolean accept) throws Exception {
expectGetSession(oneResult, sessionId, contactGroup.getId());
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(txn)); will(returnValue(txn));
@@ -589,15 +577,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(contact)); will(returnValue(contact));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
}});
if (states.isEmpty()) return;
assertEquals(1, states.size());
final BdfDictionary state = states.values().iterator().next();
context.checking(new Expectations() {{
oneOf(sessionParser) oneOf(sessionParser)
.parseInviteeSession(contactGroup.getId(), state); .parseInviteeSession(contactGroup.getId(), bdfSession);
will(returnValue(inviteeSession)); will(returnValue(inviteeSession));
if (accept) oneOf(inviteeEngine).onJoinAction(txn, inviteeSession); if (accept) oneOf(inviteeEngine).onJoinAction(txn, inviteeSession);
else oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession); else oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession);
@@ -610,11 +591,6 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
@Test @Test
public void testRevealRelationship() throws Exception { public void testRevealRelationship() throws Exception {
SessionId sessionId = new SessionId(privateGroup.getId().getBytes());
final BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
Map<MessageId, BdfDictionary> states =
Collections.singletonMap(storageMessage.getId(), state);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(txn)); will(returnValue(txn));
@@ -622,14 +598,15 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(contact)); will(returnValue(contact));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(sessionParser).parsePeerSession(contactGroup.getId(), state); oneOf(sessionParser)
.parsePeerSession(contactGroup.getId(), bdfSession);
will(returnValue(peerSession)); will(returnValue(peerSession));
oneOf(peerEngine).onJoinAction(txn, peerSession); oneOf(peerEngine).onJoinAction(txn, peerSession);
will(returnValue(peerSession)); will(returnValue(peerSession));
oneOf(db).commitTransaction(txn); oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn); oneOf(db).endTransaction(txn);
}}); }});
expectGetSession(states, sessionId); expectGetSession(oneResult, sessionId, contactGroup.getId());
expectStoreSession(peerSession, storageMessage.getId()); expectStoreSession(peerSession, storageMessage.getId());
groupInvitationManager groupInvitationManager
@@ -638,8 +615,6 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testRevealRelationshipWithoutSession() throws Exception { public void testRevealRelationshipWithoutSession() throws Exception {
SessionId sessionId = new SessionId(privateGroup.getId().getBytes());
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(txn)); will(returnValue(txn));
@@ -649,8 +624,7 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(db).endTransaction(txn); oneOf(db).endTransaction(txn);
}}); }});
expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(), expectGetSession(noResults, sessionId, contactGroup.getId());
sessionId);
groupInvitationManager groupInvitationManager
.revealRelationship(contactId, privateGroup.getId()); .revealRelationship(contactId, privateGroup.getId());
@@ -658,20 +632,19 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
@Test @Test
public void testGetInvitationMessages() throws Exception { public void testGetInvitationMessages() throws Exception {
final BdfDictionary query = new BdfDictionary(); final BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u"));
final BdfDictionary d1 = BdfDictionary.of(new BdfEntry("m1", "d")); final MessageId messageId2 = new MessageId(TestUtils.getRandomId());
final BdfDictionary d2 = BdfDictionary.of(new BdfEntry("m2", "d")); final BdfDictionary meta2 = BdfDictionary.of(new BdfEntry("m2", "e"));
final Map<MessageId, BdfDictionary> results = new HashMap<>(); final Map<MessageId, BdfDictionary> results = new HashMap<>();
results.put(message.getId(), d1); results.put(message.getId(), meta);
results.put(storageMessage.getId(), d2); results.put(messageId2, meta2);
long time1 = 1L, time2 = 2L; long time1 = 1L, time2 = 2L;
final MessageMetadata meta1 = final MessageMetadata messageMetadata1 =
new MessageMetadata(INVITE, privateGroup.getId(), time1, true, new MessageMetadata(INVITE, privateGroup.getId(), time1, true,
true, true, true); true, true, true);
final MessageMetadata meta2 = final MessageMetadata messageMetadata2 =
new MessageMetadata(JOIN, privateGroup.getId(), time2, true, new MessageMetadata(JOIN, privateGroup.getId(), time2, true,
true, true, true); true, true, true);
final BdfList list1 = BdfList.of(1);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).startTransaction(true); oneOf(db).startTransaction(true);
@@ -682,23 +655,22 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(messageParser).getMessagesVisibleInUiQuery(); oneOf(messageParser).getMessagesVisibleInUiQuery();
will(returnValue(query)); will(returnValue(query));
oneOf(clientHelper) oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
.getMessageMetadataAsDictionary(txn, contactGroup.getId(), contactGroup.getId(), query);
query);
will(returnValue(results)); will(returnValue(results));
// first message // first message
oneOf(messageParser).parseMetadata(d1); oneOf(messageParser).parseMetadata(meta);
will(returnValue(meta1)); will(returnValue(messageMetadata1));
oneOf(db).getMessageStatus(txn, contactId, message.getId()); oneOf(db).getMessageStatus(txn, contactId, message.getId());
oneOf(clientHelper).getMessage(txn, message.getId()); oneOf(clientHelper).getMessage(txn, message.getId());
will(returnValue(message)); will(returnValue(message));
oneOf(clientHelper).toList(message); oneOf(clientHelper).toList(message);
will(returnValue(list1)); will(returnValue(body));
oneOf(messageParser).parseInviteMessage(message, list1); oneOf(messageParser).parseInviteMessage(message, body);
// second message // second message
oneOf(messageParser).parseMetadata(d2); oneOf(messageParser).parseMetadata(meta2);
will(returnValue(meta2)); will(returnValue(messageMetadata2));
oneOf(db).getMessageStatus(txn, contactId, storageMessage.getId()); oneOf(db).getMessageStatus(txn, contactId, messageId2);
// end transaction // end transaction
oneOf(db).commitTransaction(txn); oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn); oneOf(db).endTransaction(txn);
@@ -713,33 +685,39 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
if (m.getId().equals(message.getId())) { if (m.getId().equals(message.getId())) {
assertTrue(m instanceof GroupInvitationRequest); assertTrue(m instanceof GroupInvitationRequest);
assertEquals(time1, m.getTimestamp()); assertEquals(time1, m.getTimestamp());
} else if (m.getId().equals(storageMessage.getId())) { } else if (m.getId().equals(messageId2)) {
assertTrue(m instanceof GroupInvitationResponse); assertTrue(m instanceof GroupInvitationResponse);
assertEquals(time2, m.getTimestamp()); assertEquals(time2, m.getTimestamp());
} else {
throw new AssertionError();
} }
} }
} }
@Test @Test
public void testGetInvitations() throws Exception { public void testGetInvitations() throws Exception {
final BdfDictionary query = new BdfDictionary(); final BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u"));
BdfDictionary d1 = BdfDictionary.of(new BdfEntry("m1", "d")); final MessageId messageId2 = new MessageId(TestUtils.getRandomId());
BdfDictionary d2 = BdfDictionary.of(new BdfEntry("m2", "d")); final BdfDictionary meta2 = BdfDictionary.of(new BdfEntry("m2", "e"));
final Map<MessageId, BdfDictionary> results = new HashMap<>(); final Map<MessageId, BdfDictionary> results = new HashMap<>();
results.put(message.getId(), d1); results.put(message.getId(), meta);
results.put(storageMessage.getId(), d2); results.put(messageId2, meta2);
final BdfList list1 = BdfList.of(1); final Message message2 = new Message(messageId2, contactGroup.getId(),
final BdfList list2 = BdfList.of(2); 0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
long time1 = 1L, time2 = 2L; long time1 = 1L, time2 = 2L;
final InviteMessage m1 = final String groupName = getRandomString(MAX_GROUP_NAME_LENGTH);
final byte[] salt = getRandomBytes(GROUP_SALT_LENGTH);
final InviteMessage inviteMessage1 =
new InviteMessage(message.getId(), contactGroup.getId(), new InviteMessage(message.getId(), contactGroup.getId(),
privateGroup.getId(), time1, "test", author, privateGroup.getId(), time1, groupName, author, salt,
getRandomBytes(5), null, getRandomBytes(5)); null, getRandomBytes(5));
final InviteMessage m2 = final InviteMessage inviteMessage2 =
new InviteMessage(message.getId(), contactGroup.getId(), new InviteMessage(message.getId(), contactGroup.getId(),
privateGroup.getId(), time2, "test", author, privateGroup.getId(), time2, groupName, author, salt,
getRandomBytes(5), null, getRandomBytes(5)); null, getRandomBytes(5));
final PrivateGroup g = context.mock(PrivateGroup.class); final PrivateGroup pg = new PrivateGroup(privateGroup, groupName,
author, salt);
final BdfList body2 = BdfList.of("body2");
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(messageParser).getInvitesAvailableToAnswerQuery(); oneOf(messageParser).getInvitesAvailableToAnswerQuery();
@@ -750,32 +728,29 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
will(returnValue(Collections.singletonList(contact))); will(returnValue(Collections.singletonList(contact)));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(clientHelper) oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
.getMessageMetadataAsDictionary(txn, contactGroup.getId(), contactGroup.getId(), query);
query);
will(returnValue(results)); will(returnValue(results));
// message 1 // message 1
oneOf(clientHelper).getMessage(txn, message.getId()); oneOf(clientHelper).getMessage(txn, message.getId());
will(returnValue(message)); will(returnValue(message));
oneOf(clientHelper).toList(message); oneOf(clientHelper).toList(message);
will(returnValue(list1)); will(returnValue(body));
oneOf(messageParser).parseInviteMessage(message, list1); oneOf(messageParser).parseInviteMessage(message, body);
will(returnValue(m1)); will(returnValue(inviteMessage1));
oneOf(privateGroupFactory) oneOf(privateGroupFactory).createPrivateGroup(groupName, author,
.createPrivateGroup(m1.getGroupName(), m1.getCreator(), salt);
m1.getSalt()); will(returnValue(pg));
will(returnValue(g));
// message 2 // message 2
oneOf(clientHelper).getMessage(txn, storageMessage.getId()); oneOf(clientHelper).getMessage(txn, messageId2);
will(returnValue(storageMessage)); will(returnValue(message2));
oneOf(clientHelper).toList(storageMessage); oneOf(clientHelper).toList(message2);
will(returnValue(list2)); will(returnValue(body2));
oneOf(messageParser).parseInviteMessage(storageMessage, list2); oneOf(messageParser).parseInviteMessage(message2, body2);
will(returnValue(m2)); will(returnValue(inviteMessage2));
oneOf(privateGroupFactory) oneOf(privateGroupFactory).createPrivateGroup(groupName, author,
.createPrivateGroup(m2.getGroupName(), m2.getCreator(), salt);
m2.getSalt()); will(returnValue(pg));
will(returnValue(g));
// end transaction // end transaction
oneOf(db).commitTransaction(txn); oneOf(db).commitTransaction(txn);
oneOf(db).endTransaction(txn); oneOf(db).endTransaction(txn);
@@ -787,6 +762,8 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
for (GroupInvitationItem i : items) { for (GroupInvitationItem i : items) {
assertEquals(contact, i.getCreator()); assertEquals(contact, i.getCreator());
assertEquals(author, i.getCreator().getAuthor()); assertEquals(author, i.getCreator().getAuthor());
assertEquals(privateGroup.getId(), i.getId());
assertEquals(groupName, i.getName());
} }
} }
@@ -822,20 +799,14 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
private void expectIsInvitationAllowed(final CreatorState state) private void expectIsInvitationAllowed(final CreatorState state)
throws Exception { throws Exception {
final SessionId sessionId = expectGetSession(oneResult, sessionId, contactGroup.getId());
new SessionId(privateGroup.getId().getBytes());
final BdfDictionary meta = BdfDictionary.of(new BdfEntry("m", "d"));
final Map<MessageId, BdfDictionary> results = new HashMap<>();
results.put(message.getId(), meta);
expectGetSession(results, sessionId);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(db).startTransaction(true); oneOf(db).startTransaction(true);
will(returnValue(txn)); will(returnValue(txn));
oneOf(sessionParser) oneOf(sessionParser)
.parseCreatorSession(contactGroup.getId(), meta); .parseCreatorSession(contactGroup.getId(), bdfSession);
will(returnValue(creatorSession)); will(returnValue(creatorSession));
oneOf(creatorSession).getState(); oneOf(creatorSession).getState();
will(returnValue(state)); will(returnValue(state));
@@ -856,68 +827,70 @@ public class GroupInvitationManagerImplTest extends BriarMockTestCase {
@Test @Test
public void testRemovingGroupEndsSessions() throws Exception { public void testRemovingGroupEndsSessions() throws Exception {
final Contact contact2 = final Contact contact2 = new Contact(new ContactId(2), author,
new Contact(new ContactId(2), author, author.getId(), true, author.getId(), true, true);
true); final Contact contact3 = new Contact(new ContactId(3), author,
final Contact contact3 = author.getId(), true, true);
new Contact(new ContactId(3), author, author.getId(), true, final Collection<Contact> contacts =
true); Arrays.asList(contact, contact2, contact3);
final Collection<Contact> contacts = new ArrayList<>();
contacts.add(contact); final Group contactGroup2 = new Group(new GroupId(getRandomId()),
contacts.add(contact2); CLIENT_ID, getRandomBytes(5));
contacts.add(contact3); final Group contactGroup3 = new Group(new GroupId(getRandomId()),
final MessageId mId2 = new MessageId(getRandomId()); CLIENT_ID, getRandomBytes(5));
final MessageId mId3 = new MessageId(getRandomId());
final BdfDictionary meta1 = BdfDictionary.of(new BdfEntry("m1", "d")); final MessageId storageId2 = new MessageId(getRandomId());
final BdfDictionary meta2 = BdfDictionary.of(new BdfEntry("m2", "d")); final MessageId storageId3 = new MessageId(getRandomId());
final BdfDictionary meta3 = BdfDictionary.of(new BdfEntry("m3", "d")); final BdfDictionary bdfSession2 =
BdfDictionary.of(new BdfEntry("f2", "o"));
final BdfDictionary bdfSession3 =
BdfDictionary.of(new BdfEntry("f3", "o"));
expectGetSession(oneResult, sessionId, contactGroup.getId());
expectGetSession(Collections.singletonMap(storageId2, bdfSession2),
sessionId, contactGroup2.getId());
expectGetSession(Collections.singletonMap(storageId3, bdfSession3),
sessionId, contactGroup3.getId());
expectGetSession(
Collections.singletonMap(storageMessage.getId(), meta1),
new SessionId(privateGroup.getId().getBytes()));
expectGetSession(
Collections.singletonMap(mId2, meta2),
new SessionId(privateGroup.getId().getBytes()));
expectGetSession(
Collections.singletonMap(mId3, meta3),
new SessionId(privateGroup.getId().getBytes()));
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(db).getContacts(txn); oneOf(db).getContacts(txn);
will(returnValue(contacts)); will(returnValue(contacts));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup)); will(returnValue(contactGroup));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact2); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact2);
will(returnValue(contactGroup)); will(returnValue(contactGroup2));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact3); oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact3);
will(returnValue(contactGroup)); will(returnValue(contactGroup3));
// session 1 // session 1
oneOf(sessionParser).getRole(meta1); oneOf(sessionParser).getRole(bdfSession);
will(returnValue(Role.CREATOR)); will(returnValue(Role.CREATOR));
oneOf(sessionParser) oneOf(sessionParser)
.parseCreatorSession(contactGroup.getId(), meta1); .parseCreatorSession(contactGroup.getId(), bdfSession);
will(returnValue(creatorSession)); will(returnValue(creatorSession));
oneOf(creatorEngine).onLeaveAction(txn, creatorSession); oneOf(creatorEngine).onLeaveAction(txn, creatorSession);
will(returnValue(creatorSession)); will(returnValue(creatorSession));
// session 2 // session 2
oneOf(sessionParser).getRole(meta2); oneOf(sessionParser).getRole(bdfSession2);
will(returnValue(Role.INVITEE)); will(returnValue(Role.INVITEE));
oneOf(sessionParser) oneOf(sessionParser)
.parseInviteeSession(contactGroup.getId(), meta2); .parseInviteeSession(contactGroup2.getId(), bdfSession2);
will(returnValue(inviteeSession)); will(returnValue(inviteeSession));
oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession); oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession);
will(returnValue(inviteeSession)); will(returnValue(inviteeSession));
// session 3 // session 3
oneOf(sessionParser).getRole(meta3); oneOf(sessionParser).getRole(bdfSession3);
will(returnValue(Role.PEER)); will(returnValue(Role.PEER));
oneOf(sessionParser) oneOf(sessionParser)
.parsePeerSession(contactGroup.getId(), meta3); .parsePeerSession(contactGroup3.getId(), bdfSession3);
will(returnValue(peerSession)); will(returnValue(peerSession));
oneOf(peerEngine).onLeaveAction(txn, peerSession); oneOf(peerEngine).onLeaveAction(txn, peerSession);
will(returnValue(peerSession)); will(returnValue(peerSession));
}}); }});
expectStoreSession(creatorSession, storageMessage.getId()); expectStoreSession(creatorSession, storageMessage.getId());
expectStoreSession(inviteeSession, mId2); expectStoreSession(inviteeSession, storageId2);
expectStoreSession(peerSession, mId3); expectStoreSession(peerSession, storageId3);
groupInvitationManager.removingGroup(txn, privateGroup.getId()); groupInvitationManager.removingGroup(txn, privateGroup.getId());
} }