Commit 1be400eb authored by Ernir Erlingsson's avatar Ernir Erlingsson

Switched Roboguice/Guice out for Dagger 2

parent e5d70381
bin
gen
build
local.properties
.settings
apply plugin: 'com.android.library'
sourceCompatibility = 1.6
targetCompatibility = 1.6
apply plugin: 'witness'
apply plugin: 'com.neenbedankt.android-apt'
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':briar-api')
compile project(':briar-core')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
testApt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
compile 'cglib:cglib-nodep:3.1'
testCompile project(':briar-tests')
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/ernir/dev/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
package com.ymirmobile.briar_android_tests;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
\ No newline at end of file
<manifest package="org.briarproject"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>
</application>
</manifest>
<resources>
<string name="app_name">briar-android-tests</string>
</resources>
package org.briarproject;
import com.google.inject.Guice;
import com.google.inject.Injector;
package org.briarproject.protocol;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.SecretKey;
......@@ -22,14 +21,15 @@ import org.briarproject.api.sync.Request;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.data.DataModule;
import org.briarproject.db.DatabaseModule;
import org.briarproject.event.EventModule;
import org.briarproject.sync.SyncModule;
import org.briarproject.transport.TransportModule;
import org.junit.Test;
import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
......@@ -37,48 +37,43 @@ import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import javax.inject.Inject;
public class ProtocolIntegrationTest extends BriarTestCase {
private final StreamReaderFactory streamReaderFactory;
private final StreamWriterFactory streamWriterFactory;
private final PacketReaderFactory packetReaderFactory;
private final PacketWriterFactory packetWriterFactory;
@Inject
StreamReaderFactory streamReaderFactory;
@Inject
StreamWriterFactory streamWriterFactory;
@Inject
PacketReaderFactory packetReaderFactory;
@Inject
PacketWriterFactory packetWriterFactory;
private final ContactId contactId;
private final TransportId transportId;
private final SecretKey tagKey, headerKey;
private final Message message, message1;
private final Collection<MessageId> messageIds;
private final ProtocolTestComponent component;
public ProtocolIntegrationTest() throws Exception {
Injector i = Guice.createInjector(new TestDatabaseModule(),
new TestLifecycleModule(), new TestSystemModule(),
new CryptoModule(), new DatabaseModule(), new EventModule(),
new SyncModule(), new DataModule(),
new TransportModule());
streamReaderFactory = i.getInstance(StreamReaderFactory.class);
streamWriterFactory = i.getInstance(StreamWriterFactory.class);
packetReaderFactory = i.getInstance(PacketReaderFactory.class);
packetWriterFactory = i.getInstance(PacketWriterFactory.class);
component = DaggerProtocolTestComponent.builder().build();
component.inject(this);
contactId = new ContactId(234);
transportId = new TransportId("id");
// Create the transport keys
tagKey = TestUtils.createSecretKey();
headerKey = TestUtils.createSecretKey();
// Create a group
GroupFactory groupFactory = i.getInstance(GroupFactory.class);
GroupFactory groupFactory = component.getGroupFactory();
ClientId clientId = new ClientId(TestUtils.getRandomId());
byte[] descriptor = new byte[MAX_GROUP_DESCRIPTOR_LENGTH];
Group group = groupFactory.createGroup(clientId, descriptor);
// Add two messages to the group
MessageFactory messageFactory = i.getInstance(MessageFactory.class);
MessageFactory messageFactory = component.getMessageFactory();
long timestamp = System.currentTimeMillis();
String messageBody = "Hello world";
message = messageFactory.createMessage(group.getId(), timestamp,
......@@ -159,4 +154,5 @@ public class ProtocolIntegrationTest extends BriarTestCase {
assertEquals(m1.getTimestamp(), m2.getTimestamp());
assertArrayEquals(m1.getRaw(), m2.getRaw());
}
}
package org.briarproject.protocol;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestSystemModule;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.data.DataModule;
import org.briarproject.db.DatabaseModule;
import org.briarproject.event.EventModule;
import org.briarproject.sync.SyncModule;
import org.briarproject.transport.TransportModule;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {TestDatabaseModule.class, TestSystemModule.class,
CryptoModule.class, DatabaseModule.class, EventModule.class,
SyncModule.class, DataModule.class, TransportModule.class})
public interface ProtocolTestComponent {
void inject(ProtocolIntegrationTest testCase);
GroupFactory getGroupFactory();
MessageFactory getMessageFactory();
}
package org.briarproject.sync;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestLifecycleModule;
import org.briarproject.TestSystemModule;
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.forum.ForumModule;
import org.briarproject.identity.IdentityModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.transport.TransportModule;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {TestDatabaseModule.class, TestLifecycleModule.class,
TestSystemModule.class, ContactModule.class, CryptoModule.class,
DatabaseModule.class, EventModule.class, SyncModule.class,
DataModule.class, TransportModule.class, ForumModule.class,
IdentityModule.class, MessagingModule.class})
public interface ConstantsComponent {
void inject(ConstantsTest testCase);
}
package org.briarproject.sync;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.briarproject.BriarTestCase;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestLifecycleModule;
import org.briarproject.TestSystemModule;
import org.briarproject.TestUtils;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.CryptoComponent;
......@@ -21,22 +15,6 @@ import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.messaging.MessagingConstants;
import org.briarproject.api.messaging.PrivateMessage;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.briarproject.clients.ClientsModule;
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.forum.ForumModule;
import org.briarproject.identity.IdentityModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.transport.TransportModule;
import org.junit.Test;
import java.util.Random;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
......@@ -45,26 +23,32 @@ import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESS
import static org.briarproject.api.sync.SyncConstants.MAX_PACKET_PAYLOAD_LENGTH;
import static org.junit.Assert.assertTrue;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.junit.Test;
import java.util.Random;
import javax.inject.Inject;
public class ConstantsTest extends BriarTestCase {
// TODO: Break this up into tests that are relevant for each package
@Inject
CryptoComponent crypto;
@Inject
AuthorFactory authorFactory;
@Inject
PrivateMessageFactory privateMessageFactory;
@Inject
ForumPostFactory forumPostFactory;
private final CryptoComponent crypto;
private final AuthorFactory authorFactory;
private final PrivateMessageFactory privateMessageFactory;
private final ForumPostFactory forumPostFactory;
private final ConstantsComponent component;
public ConstantsTest() throws Exception {
Injector i = Guice.createInjector(new TestDatabaseModule(),
new TestLifecycleModule(), new TestSystemModule(),
new ClientsModule(), new ContactModule(), new CryptoModule(),
new DatabaseModule(), new DataModule(), new EventModule(),
new ForumModule(), new IdentityModule(), new MessagingModule(),
new SyncModule(), new TransportModule());
crypto = i.getInstance(CryptoComponent.class);
authorFactory = i.getInstance(AuthorFactory.class);
privateMessageFactory = i.getInstance(PrivateMessageFactory.class);
forumPostFactory = i.getInstance(ForumPostFactory.class);
component = DaggerConstantsComponent.builder().build();
component.inject(this);
}
@Test
......
package org.briarproject.sync;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestSystemModule;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.PacketReaderFactory;
import org.briarproject.api.sync.PacketWriterFactory;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
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.lifecycle.LifecycleModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.transport.TransportModule;
import javax.inject.Singleton;
import dagger.Component;
/**
* Created by Ernir Erlingsson (ernir@ymirmobile.com) on 3.3.2016.
*/
@Singleton
@Component(modules = {TestDatabaseModule.class, TestSystemModule.class,
LifecycleModule.class, ContactModule.class, CryptoModule.class,
DatabaseModule.class, EventModule.class, SyncModule.class,
DataModule.class, TransportModule.class, IdentityModule.class,
MessagingModule.class})
public interface SimplexMessagingComponent {
void inject(SimplexMessagingIntegrationTest testCase);
LifecycleManager getLifeCycleManager();
DatabaseComponent getDatabaseComponent();
IdentityManager getIdentityManager();
ContactManager getContactManager();
MessagingManager getMessagingManager();
KeyManager getKeyManager();
PrivateMessageFactory getPrivateMessageFactory();
PacketWriterFactory getPacketWriterFactory();
EventBus getEventBus();
StreamWriterFactory getStreamWriterFactory();
StreamReaderFactory getStreamReaderFactory();
PacketReaderFactory getPacketReaderFactory();
}
package org.briarproject.sync;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.briarproject.BriarTestCase;
import org.briarproject.ImmediateExecutor;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestSystemModule;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.db.StorageStatus;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
......@@ -36,33 +31,24 @@ 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.clients.ClientsModule;
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.lifecycle.LifecycleModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.transport.TransportModule;
import org.briarproject.plugins.ImmediateExecutor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class SimplexMessagingIntegrationTest extends BriarTestCase {
private static final int MAX_LATENCY = 2 * 60 * 1000; // 2 minutes
......@@ -76,22 +62,16 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private final AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
private final AuthorId bobId = new AuthorId(TestUtils.getRandomId());
private Injector alice, bob;
// private Injector alice, bob;
private SimplexMessagingComponent alice, bob;
@Before
public void setUp() {
assertTrue(testDir.mkdirs());
alice = createInjector(aliceDir);
bob = createInjector(bobDir);
}
private Injector createInjector(File dir) {
return Guice.createInjector(new TestDatabaseModule(dir),
new TestSystemModule(), new ClientsModule(),
new ContactModule(), new CryptoModule(), new DatabaseModule(),
new DataModule(), new EventModule(), new IdentityModule(),
new LifecycleModule(), new MessagingModule(), new SyncModule(),
new TransportModule());
alice = DaggerSimplexMessagingComponent.builder()
.testDatabaseModule(new TestDatabaseModule(aliceDir)).build();
bob = DaggerSimplexMessagingComponent.builder()
.testDatabaseModule(new TestDatabaseModule(bobDir)).build();
}
@Test
......@@ -101,43 +81,34 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private byte[] write() throws Exception {
// Instantiate Alice's services
LifecycleManager lifecycleManager =
alice.getInstance(LifecycleManager.class);
DatabaseComponent db = alice.getInstance(DatabaseComponent.class);
IdentityManager identityManager =
alice.getInstance(IdentityManager.class);
ContactManager contactManager = alice.getInstance(ContactManager.class);
MessagingManager messagingManager =
alice.getInstance(MessagingManager.class);
KeyManager keyManager = alice.getInstance(KeyManager.class);
PrivateMessageFactory privateMessageFactory =
alice.getInstance(PrivateMessageFactory.class);
PacketWriterFactory packetWriterFactory =
alice.getInstance(PacketWriterFactory.class);
EventBus eventBus = alice.getInstance(EventBus.class);
StreamWriterFactory streamWriterFactory =
alice.getInstance(StreamWriterFactory.class);
LifecycleManager lifecycleManager = alice.getLifeCycleManager();
DatabaseComponent db = alice.getDatabaseComponent();
IdentityManager identityManager = alice.getIdentityManager();
ContactManager contactManager = alice.getContactManager();
MessagingManager messagingManager = alice.getMessagingManager();
KeyManager keyManager = alice.getKeyManager();
PrivateMessageFactory privateMessageFactory = alice.getPrivateMessageFactory();
PacketWriterFactory packetWriterFactory = alice.getPacketWriterFactory();
EventBus eventBus = alice.getEventBus();
StreamWriterFactory streamWriterFactory = alice.getStreamWriterFactory();
// Start the lifecycle manager
lifecycleManager.startServices();
lifecycleManager.waitForStartup();
// Add a transport
Transaction txn = db.startTransaction();
try {
db.addTransport(txn, transportId, MAX_LATENCY);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
db.addTransport(transportId, MAX_LATENCY);
// Add an identity for Alice
LocalAuthor aliceAuthor = new LocalAuthor(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
StorageStatus.ADDING);
identityManager.addLocalAuthor(aliceAuthor);
// Add Bob as a contact
Author bobAuthor = new Author(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = contactManager.addContact(bobAuthor, aliceId,
master, timestamp, true, true);
ContactId contactId = contactManager.addContact(bobAuthor, aliceId);
// Derive and store the transport keys
keyManager.addContact(contactId, master, timestamp, true);
// Send Bob a message
GroupId groupId = messagingManager.getConversationId(contactId);
......@@ -172,45 +143,37 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private void read(byte[] stream) throws Exception {
// Instantiate Bob's services
LifecycleManager lifecycleManager =
bob.getInstance(LifecycleManager.class);
DatabaseComponent db = bob.getInstance(DatabaseComponent.class);
IdentityManager identityManager =
bob.getInstance(IdentityManager.class);
ContactManager contactManager = bob.getInstance(ContactManager.class);
KeyManager keyManager = bob.getInstance(KeyManager.class);
StreamReaderFactory streamReaderFactory =
bob.getInstance(StreamReaderFactory.class);
PacketReaderFactory packetReaderFactory =
bob.getInstance(PacketReaderFactory.class);
EventBus eventBus = bob.getInstance(EventBus.class);
LifecycleManager lifecycleManager = bob.getLifeCycleManager();
DatabaseComponent db = bob.getDatabaseComponent();
IdentityManager identityManager = bob.getIdentityManager();
ContactManager contactManager = bob.getContactManager();
KeyManager keyManager = bob.getKeyManager();
StreamReaderFactory streamReaderFactory = bob.getStreamReaderFactory();
PacketReaderFactory packetReaderFactory = bob.getPacketReaderFactory();
EventBus eventBus = bob.getEventBus();
// Bob needs a MessagingManager even though we're not using it directly
bob.getInstance(MessagingManager.class);
bob.getMessagingManager();
// Start the lifecyle manager
lifecycleManager.startServices();
lifecycleManager.waitForStartup();
// Add a transport
Transaction txn = db.startTransaction();
try {
db.addTransport(txn, transportId, MAX_LATENCY);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
db.addTransport(transportId, MAX_LATENCY);
// Add an identity for Bob
LocalAuthor bobAuthor = new LocalAuthor(bobId, "Bob",
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp,
StorageStatus.ADDING);
identityManager.addLocalAuthor(bobAuthor);
// Add Alice as a contact
Author aliceAuthor = new Author(aliceId, "Alice",
new byte[MAX_PUBLIC_KEY_LENGTH]);
ContactId contactId = contactManager.addContact(aliceAuthor, bobId,
master, timestamp, false, true);
ContactId contactId = contactManager.addContact(aliceAuthor, bobId);
// Derive and store the transport keys
keyManager.addContact(contactId, master, timestamp, false);
// Set up an event listener
MessageListener listener = new MessageListener();
bob.getInstance(EventBus.class).addListener(listener);
bob.getEventBus().addListener(listener);
// Read and recognise the tag
ByteArrayInputStream in = new ByteArrayInputStream(stream);
byte[] tag = new byte[TAG_LENGTH];
......
apply plugin: 'com.android.application'
apply plugin: 'witness'
apply plugin: 'com.neenbedankt.android-apt'
repositories {
jcenter()
......@@ -27,10 +28,13 @@ dependencies {
exclude module: 'support-v4'
exclude module: 'recyclerview-v7'
}
compile "org.roboguice:roboguice:2.0"
compile "info.guardianproject.panic:panic:0.5"
compile "info.guardianproject.trustedintents:trustedintents:0.2"
compile "de.hdodenhof:circleimageview:2.0.0"
apt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
compile 'cglib:cglib-nodep:3.1'
}
dependencyVerification {
......@@ -42,7 +46,6 @@ dependencyVerification {
'com.android.support:design:41a9cd75ca78f25df5f573db7cedf8bb66beae00c330943923ba9f3e2051736d',
'com.android.support:support-annotations:f347a35b9748a4103b39a6714a77e2100f488d623fd6268e259c177b200e9d82',