Created TransportPropertyManager facade.

This commit is contained in:
akwizgran
2015-12-18 16:39:19 +00:00
parent 7c869a6ebd
commit 805bf39ec8
19 changed files with 342 additions and 163 deletions

View File

@@ -10,7 +10,7 @@ import org.briarproject.TestSystemModule;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.Event;
@@ -19,11 +19,13 @@ import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageVerifier;
import org.briarproject.api.sync.MessagingSession;
import org.briarproject.api.sync.PacketReader;
@@ -34,11 +36,13 @@ import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
import org.briarproject.api.transport.TransportKeys;
import org.briarproject.contact.ContactModule;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.data.DataModule;
import org.briarproject.db.DatabaseModule;
import org.briarproject.event.EventModule;
import org.briarproject.identity.IdentityModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.plugins.ImmediateExecutor;
import org.briarproject.transport.TransportModule;
import org.junit.After;
@@ -53,8 +57,6 @@ import java.io.OutputStream;
import java.util.Collections;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.sync.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -64,8 +66,6 @@ import static org.junit.Assert.assertTrue;
public class SimplexMessagingIntegrationTest extends BriarTestCase {
private static final int MAX_LATENCY = 2 * 60 * 1000; // 2 minutes
private static final long ROTATION_PERIOD_LENGTH =
MAX_LATENCY + MAX_CLOCK_DIFFERENCE;
private final File testDir = TestUtils.getTestDirectory();
private final File aliceDir = new File(testDir, "alice");
@@ -86,9 +86,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private Injector createInjector(File dir) {
return Guice.createInjector(new TestDatabaseModule(dir),
new TestLifecycleModule(), new TestSystemModule(),
new CryptoModule(), new DatabaseModule(), new EventModule(),
new SyncModule(), new DataModule(),
new TransportModule());
new ContactModule(), new CryptoModule(), new DatabaseModule(),
new DataModule(), new EventModule(), new IdentityModule(),
new SyncModule(), new MessagingModule(), new TransportModule());
}
@Test
@@ -100,40 +100,41 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Open Alice's database
DatabaseComponent db = alice.getInstance(DatabaseComponent.class);
assertFalse(db.open());
// Add the transport
db.addTransport(transportId, MAX_LATENCY);
// Start Alice's key manager
KeyManager keyManager = alice.getInstance(KeyManager.class);
keyManager.start();
// Add a local pseudonym for Alice
// Add an identity for Alice
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
db.addLocalAuthor(aliceAuthor);
IdentityManager identityManager =
alice.getInstance(IdentityManager.class);
identityManager.addLocalAuthor(aliceAuthor);
// Add Bob as a contact
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
Author bobAuthor = new Author(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = db.addContact(bobAuthor, aliceId);
// Add the inbox group
GroupFactory gf = alice.getInstance(GroupFactory.class);
Group group = gf.createGroup("Group", new byte[GROUP_SALT_LENGTH]);
db.addGroup(group);
db.setInboxGroup(contactId, group);
// Add the transport
db.addTransport(transportId, MAX_LATENCY);
ContactManager contactManager = alice.getInstance(ContactManager.class);
ContactId contactId = contactManager.addContact(bobAuthor, aliceId);
// Create the private conversation
MessagingManager messagingManager =
alice.getInstance(MessagingManager.class);
messagingManager.addContact(contactId, master);
// Derive and store the transport keys
long rotationPeriod = timestamp / ROTATION_PERIOD_LENGTH;
CryptoComponent crypto = alice.getInstance(CryptoComponent.class);
TransportKeys keys = crypto.deriveTransportKeys(transportId, master,
rotationPeriod, true);
db.addTransportKeys(contactId, keys);
keyManager.contactAdded(contactId, Collections.singletonList(keys));
keyManager.addContact(contactId, Collections.singletonList(transportId),
master, timestamp, true);
// Send Bob a message
String contentType = "text/plain";
byte[] body = "Hi Bob!".getBytes("UTF-8");
MessageFactory messageFactory = alice.getInstance(MessageFactory.class);
Message message = messageFactory.createAnonymousMessage(null, group,
contentType, timestamp, body);
db.addLocalMessage(message);
PrivateMessageFactory messageFactory =
alice.getInstance(PrivateMessageFactory.class);
GroupId groupId = messagingManager.getConversationId(contactId);
PrivateConversation conversation =
messagingManager.getConversation(groupId);
Message message = messageFactory.createPrivateMessage(null,
conversation, "text/plain", timestamp, body);
messagingManager.addLocalMessage(message);
// Get a stream context
StreamContext ctx = keyManager.getStreamContext(contactId, transportId);
assertNotNull(ctx);
@@ -143,13 +144,13 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
alice.getInstance(StreamWriterFactory.class);
OutputStream streamWriter =
streamWriterFactory.createStreamWriter(out, ctx);
// Create an outgoing messaging session
// Create an outgoing sync session
EventBus eventBus = alice.getInstance(EventBus.class);
PacketWriterFactory packetWriterFactory =
alice.getInstance(PacketWriterFactory.class);
PacketWriter packetWriter = packetWriterFactory.createPacketWriter(
streamWriter);
MessagingSession session = new org.briarproject.sync.SimplexOutgoingSession(db,
MessagingSession session = new SimplexOutgoingSession(db,
new ImmediateExecutor(), eventBus, contactId, transportId,
MAX_LATENCY, packetWriter);
// Write whatever needs to be written
@@ -166,33 +167,31 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
// Open Bob's database
DatabaseComponent db = bob.getInstance(DatabaseComponent.class);
assertFalse(db.open());
// Add the transport
db.addTransport(transportId, MAX_LATENCY);
// Start Bob's key manager
KeyManager keyManager = bob.getInstance(KeyManager.class);
keyManager.start();
// Add a local pseudonym for Bob
// Add an identity for Bob
AuthorId bobId = new AuthorId(TestUtils.getRandomId());
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[100], 1234);
db.addLocalAuthor(bobAuthor);
IdentityManager identityManager =
bob.getInstance(IdentityManager.class);
identityManager.addLocalAuthor(bobAuthor);
// Add Alice as a contact
AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
Author aliceAuthor = new Author(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = db.addContact(aliceAuthor, bobId);
// Add the inbox group
GroupFactory gf = bob.getInstance(GroupFactory.class);
Group group = gf.createGroup("Group", new byte[GROUP_SALT_LENGTH]);
db.addGroup(group);
db.setInboxGroup(contactId, group);
// Add the transport
db.addTransport(transportId, MAX_LATENCY);
ContactManager contactManager = bob.getInstance(ContactManager.class);
ContactId contactId = contactManager.addContact(aliceAuthor, bobId);
// Create the private conversation
MessagingManager messagingManager =
bob.getInstance(MessagingManager.class);
messagingManager.addContact(contactId, master);
// Derive and store the transport keys
long rotationPeriod = timestamp / ROTATION_PERIOD_LENGTH;
CryptoComponent crypto = bob.getInstance(CryptoComponent.class);
TransportKeys keys = crypto.deriveTransportKeys(transportId, master,
rotationPeriod, false);
db.addTransportKeys(contactId, keys);
keyManager.contactAdded(contactId, Collections.singletonList(keys));
keyManager.addContact(contactId, Collections.singletonList(transportId),
master, timestamp, false);
// Set up an event listener
MessageListener listener = new MessageListener();
bob.getInstance(EventBus.class).addListener(listener);
@@ -201,14 +200,14 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH];
int read = in.read(tag);
assertEquals(tag.length, read);
StreamContext ctx = keyManager.recogniseTag(transportId, tag);
StreamContext ctx = keyManager.getStreamContext(transportId, tag);
assertNotNull(ctx);
// Create a stream reader
StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class);
InputStream streamReader =
streamReaderFactory.createStreamReader(in, ctx);
// Create an incoming messaging session
// Create an incoming sync session
EventBus eventBus = bob.getInstance(EventBus.class);
MessageVerifier messageVerifier =
bob.getInstance(MessageVerifier.class);
@@ -216,7 +215,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
bob.getInstance(PacketReaderFactory.class);
PacketReader packetReader = packetReaderFactory.createPacketReader(
streamReader);
MessagingSession session = new org.briarproject.sync.IncomingSession(db,
MessagingSession session = new IncomingSession(db,
new ImmediateExecutor(), new ImmediateExecutor(), eventBus,
messageVerifier, contactId, transportId, packetReader);
// No messages should have been added yet