Test cleanup. #280

This commit is contained in:
akwizgran
2016-04-05 14:19:10 +01:00
parent dc1adc21ae
commit 205dc66572
33 changed files with 425 additions and 449 deletions

View File

@@ -1,12 +1,8 @@
package org.briarproject.sync;
package org.briarproject;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.KeyPair;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.Signature;
import org.briarproject.api.forum.ForumConstants;
import org.briarproject.api.forum.ForumPost;
import org.briarproject.api.forum.ForumPostFactory;
@@ -15,25 +11,21 @@ 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 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;
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
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 {
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;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MAX_PACKET_PAYLOAD_LENGTH;
import static org.junit.Assert.assertTrue;
public class MessageSizeIntegrationTest extends BriarTestCase {
// TODO: Break this up into tests that are relevant for each package
@Inject
CryptoComponent crypto;
@Inject
@@ -43,52 +35,19 @@ public class ConstantsTest extends BriarTestCase {
@Inject
ForumPostFactory forumPostFactory;
private final ConstantsComponent component;
public ConstantsTest() throws Exception {
component = DaggerConstantsComponent.builder().build();
public MessageSizeIntegrationTest() throws Exception {
MessageSizeIntegrationTestComponent component =
DaggerMessageSizeIntegrationTestComponent.builder().build();
component.inject(this);
}
@Test
public void testAgreementPublicKeys() throws Exception {
// Generate 10 agreement key pairs
for (int i = 0; i < 10; i++) {
KeyPair keyPair = crypto.generateSignatureKeyPair();
// Check the length of the public key
byte[] publicKey = keyPair.getPublic().getEncoded();
assertTrue(publicKey.length <= MAX_PUBLIC_KEY_LENGTH);
}
}
@Test
public void testSignaturePublicKeys() throws Exception {
Random random = new Random();
Signature sig = crypto.getSignature();
// Generate 10 signature key pairs
for (int i = 0; i < 10; i++) {
KeyPair keyPair = crypto.generateSignatureKeyPair();
// Check the length of the public key
byte[] publicKey = keyPair.getPublic().getEncoded();
assertTrue(publicKey.length <= MAX_PUBLIC_KEY_LENGTH);
// Sign some random data and check the length of the signature
byte[] toBeSigned = new byte[1234];
random.nextBytes(toBeSigned);
sig.initSign(keyPair.getPrivate());
sig.update(toBeSigned);
byte[] signature = sig.sign();
assertTrue(signature.length <= MAX_SIGNATURE_LENGTH);
}
}
@Test
public void testPrivateMessageFitsIntoPacket() throws Exception {
// Create a maximum-length private message
GroupId groupId = new GroupId(TestUtils.getRandomId());
long timestamp = Long.MAX_VALUE;
MessageId parent = new MessageId(TestUtils.getRandomId());
String contentType = TestUtils.createRandomString(
String contentType = TestUtils.getRandomString(
MessagingConstants.MAX_CONTENT_TYPE_LENGTH);
byte[] body = new byte[MAX_PRIVATE_MESSAGE_BODY_LENGTH];
PrivateMessage message = privateMessageFactory.createPrivateMessage(
@@ -104,7 +63,7 @@ public class ConstantsTest extends BriarTestCase {
@Test
public void testForumPostFitsIntoPacket() throws Exception {
// Create a maximum-length author
String authorName = TestUtils.createRandomString(
String authorName = TestUtils.getRandomString(
MAX_AUTHOR_NAME_LENGTH);
byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
Author author = authorFactory.createAuthor(authorName, authorPublic);
@@ -112,7 +71,7 @@ public class ConstantsTest extends BriarTestCase {
GroupId groupId = new GroupId(TestUtils.getRandomId());
long timestamp = Long.MAX_VALUE;
MessageId parent = new MessageId(TestUtils.getRandomId());
String contentType = TestUtils.createRandomString(
String contentType = TestUtils.getRandomString(
ForumConstants.MAX_CONTENT_TYPE_LENGTH);
byte[] body = new byte[MAX_FORUM_POST_BODY_LENGTH];
PrivateKey privateKey = crypto.generateSignatureKeyPair().getPrivate();

View File

@@ -0,0 +1,34 @@
package org.briarproject;
import org.briarproject.clients.ClientsModule;
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.sync.SyncModule;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {
TestDatabaseModule.class,
TestLifecycleModule.class,
TestSystemModule.class,
ClientsModule.class,
CryptoModule.class,
DataModule.class,
DatabaseModule.class,
EventModule.class,
ForumModule.class,
IdentityModule.class,
MessagingModule.class,
SyncModule.class
})
public interface MessageSizeIntegrationTestComponent {
void inject(MessageSizeIntegrationTest testCase);
}

View File

@@ -1,15 +1,9 @@
package org.briarproject.sync;
package org.briarproject;
import org.briarproject.BriarTestCase;
import org.briarproject.ImmediateExecutor;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestUtils;
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.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.identity.Author;
@@ -21,11 +15,8 @@ import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateMessage;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.PacketReader;
import org.briarproject.api.sync.PacketReaderFactory;
import org.briarproject.api.sync.PacketWriter;
import org.briarproject.api.sync.PacketWriterFactory;
import org.briarproject.api.sync.SyncSession;
import org.briarproject.api.sync.SyncSessionFactory;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.StreamReaderFactory;
@@ -54,19 +45,19 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
private final File aliceDir = new File(testDir, "alice");
private final File bobDir = new File(testDir, "bob");
private final SecretKey master = TestUtils.createSecretKey();
private final SecretKey master = TestUtils.getSecretKey();
private final long timestamp = System.currentTimeMillis();
private final AuthorId aliceId = new AuthorId(TestUtils.getRandomId());
private final AuthorId bobId = new AuthorId(TestUtils.getRandomId());
private SimplexMessagingComponent alice, bob;
private SimplexMessagingIntegrationTestComponent alice, bob;
@Before
public void setUp() {
assertTrue(testDir.mkdirs());
alice = DaggerSimplexMessagingComponent.builder()
alice = DaggerSimplexMessagingIntegrationTestComponent.builder()
.testDatabaseModule(new TestDatabaseModule(aliceDir)).build();
bob = DaggerSimplexMessagingComponent.builder()
bob = DaggerSimplexMessagingIntegrationTestComponent.builder()
.testDatabaseModule(new TestDatabaseModule(bobDir)).build();
}
@@ -78,18 +69,15 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private byte[] write() throws Exception {
// Instantiate Alice's services
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();
SyncSessionFactory syncSessionFactory = alice.getSyncSessionFactory();
// Start the lifecycle manager
lifecycleManager.startServices();
@@ -119,11 +107,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
out, ctx);
// Create an outgoing sync session
PacketWriter packetWriter = packetWriterFactory.createPacketWriter(
streamWriter);
SyncSession session = new SimplexOutgoingSession(db,
new ImmediateExecutor(), eventBus, contactId, MAX_LATENCY,
packetWriter);
SyncSession session = syncSessionFactory.createSimplexOutgoingSession(
contactId, MAX_LATENCY, streamWriter);
// Write whatever needs to be written
session.run();
streamWriter.close();
@@ -139,13 +124,11 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
private void read(byte[] stream) throws Exception {
// Instantiate Bob's services
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();
SyncSessionFactory syncSessionFactory = bob.getSyncSessionFactory();
// Bob needs a MessagingManager even though we're not using it directly
bob.getMessagingManager();
@@ -176,10 +159,8 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
InputStream streamReader = streamReaderFactory.createStreamReader(
in, ctx);
// Create an incoming sync session
PacketReader packetReader = packetReaderFactory.createPacketReader(
streamReader);
SyncSession session = new IncomingSession(db, new ImmediateExecutor(),
eventBus, contactId, packetReader);
SyncSession session = syncSessionFactory.createIncomingSession(
contactId, streamReader);
// No messages should have been added yet
assertFalse(listener.messageAdded);
// Read whatever needs to be read

View File

@@ -1,17 +1,12 @@
package org.briarproject.sync;
package org.briarproject;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestPluginsModule;
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.sync.SyncSessionFactory;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
@@ -25,6 +20,7 @@ import org.briarproject.identity.IdentityModule;
import org.briarproject.lifecycle.LifecycleModule;
import org.briarproject.messaging.MessagingModule;
import org.briarproject.plugins.PluginsModule;
import org.briarproject.sync.SyncModule;
import org.briarproject.transport.TransportModule;
import javax.inject.Singleton;
@@ -32,20 +28,29 @@ import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {TestDatabaseModule.class, TestPluginsModule.class,
TestSystemModule.class, LifecycleModule.class, ContactModule.class,
CryptoModule.class, DatabaseModule.class, EventModule.class,
SyncModule.class, DataModule.class, TransportModule.class,
IdentityModule.class, MessagingModule.class, ClientsModule.class,
PluginsModule.class})
public interface SimplexMessagingComponent {
@Component(modules = {
TestDatabaseModule.class,
TestPluginsModule.class,
TestSystemModule.class,
ClientsModule.class,
ContactModule.class,
CryptoModule.class,
DataModule.class,
DatabaseModule.class,
EventModule.class,
IdentityModule.class,
LifecycleModule.class,
MessagingModule.class,
PluginsModule.class,
SyncModule.class,
TransportModule.class
})
public interface SimplexMessagingIntegrationTestComponent {
void inject(SimplexMessagingIntegrationTest testCase);
LifecycleManager getLifecycleManager();
DatabaseComponent getDatabaseComponent();
IdentityManager getIdentityManager();
ContactManager getContactManager();
@@ -56,13 +61,11 @@ public interface SimplexMessagingComponent {
PrivateMessageFactory getPrivateMessageFactory();
PacketWriterFactory getPacketWriterFactory();
EventBus getEventBus();
StreamWriterFactory getStreamWriterFactory();
StreamReaderFactory getStreamReaderFactory();
PacketReaderFactory getPacketReaderFactory();
SyncSessionFactory getSyncSessionFactory();
}

View File

@@ -1,9 +1,8 @@
package org.briarproject.protocol;
package org.briarproject;
import org.briarproject.BriarTestCase;
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.crypto.SecretKey;
import org.briarproject.api.sync.Ack;
import org.briarproject.api.sync.ClientId;
@@ -23,13 +22,6 @@ import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory;
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;
@@ -39,8 +31,19 @@ import java.util.Collection;
import javax.inject.Inject;
public class ProtocolIntegrationTest extends BriarTestCase {
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;
public class SyncIntegrationTest extends BriarTestCase {
@Inject
GroupFactory groupFactory;
@Inject
MessageFactory messageFactory;
@Inject
StreamReaderFactory streamReaderFactory;
@Inject
@@ -49,37 +52,37 @@ public class ProtocolIntegrationTest extends BriarTestCase {
PacketReaderFactory packetReaderFactory;
@Inject
PacketWriterFactory packetWriterFactory;
@Inject
CryptoComponent crypto;
private final ContactId contactId;
private final TransportId transportId;
private final SecretKey tagKey, headerKey;
private final long streamNumber;
private final Message message, message1;
private final Collection<MessageId> messageIds;
private final ProtocolTestComponent component;
public ProtocolIntegrationTest() throws Exception {
public SyncIntegrationTest() throws Exception {
component = DaggerProtocolTestComponent.builder().build();
SyncIntegrationTestComponent component =
DaggerSyncIntegrationTestComponent.builder().build();
component.inject(this);
contactId = new ContactId(234);
transportId = new TransportId("id");
// Create the transport keys
tagKey = TestUtils.createSecretKey();
headerKey = TestUtils.createSecretKey();
tagKey = TestUtils.getSecretKey();
headerKey = TestUtils.getSecretKey();
streamNumber = 123;
// Create a group
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 = component.getMessageFactory();
long timestamp = System.currentTimeMillis();
String messageBody = "Hello world";
message = messageFactory.createMessage(group.getId(), timestamp,
messageBody.getBytes("UTF-8"));
message1 = messageFactory.createMessage(group.getId(), timestamp,
messageBody.getBytes("UTF-8"));
byte[] body = "Hello world".getBytes("UTF-8");
message = messageFactory.createMessage(group.getId(), timestamp, body);
message1 = messageFactory.createMessage(group.getId(), timestamp, body);
messageIds = Arrays.asList(message.getId(), message1.getId());
}
@@ -91,19 +94,16 @@ public class ProtocolIntegrationTest extends BriarTestCase {
private byte[] write() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamContext ctx = new StreamContext(contactId, transportId, tagKey,
headerKey, 0);
OutputStream streamWriter =
streamWriterFactory.createStreamWriter(out, ctx);
headerKey, streamNumber);
OutputStream streamWriter = streamWriterFactory.createStreamWriter(out,
ctx);
PacketWriter packetWriter = packetWriterFactory.createPacketWriter(
streamWriter);
packetWriter.writeAck(new Ack(messageIds));
packetWriter.writeMessage(message.getRaw());
packetWriter.writeMessage(message1.getRaw());
packetWriter.writeOffer(new Offer(messageIds));
packetWriter.writeRequest(new Request(messageIds));
streamWriter.flush();
@@ -111,14 +111,21 @@ public class ProtocolIntegrationTest extends BriarTestCase {
}
private void read(byte[] connectionData) throws Exception {
// Calculate the expected tag
byte[] expectedTag = new byte[TAG_LENGTH];
crypto.encodeTag(expectedTag, tagKey, streamNumber);
// Read the tag
InputStream in = new ByteArrayInputStream(connectionData);
byte[] tag = new byte[TAG_LENGTH];
assertEquals(TAG_LENGTH, in.read(tag, 0, TAG_LENGTH));
// FIXME: Check that the expected tag was received
assertArrayEquals(expectedTag, tag);
// Create the readers
StreamContext ctx = new StreamContext(contactId, transportId, tagKey,
headerKey, 0);
InputStream streamReader =
streamReaderFactory.createStreamReader(in, ctx);
InputStream streamReader = streamReaderFactory.createStreamReader(in,
ctx);
PacketReader packetReader = packetReaderFactory.createPacketReader(
streamReader);
@@ -127,7 +134,7 @@ public class ProtocolIntegrationTest extends BriarTestCase {
Ack a = packetReader.readAck();
assertEquals(messageIds, a.getMessageIds());
// Read and verify the messages
// Read the messages
assertTrue(packetReader.hasMessage());
Message m = packetReader.readMessage();
checkMessageEquality(message, m);
@@ -150,9 +157,11 @@ public class ProtocolIntegrationTest extends BriarTestCase {
}
private void checkMessageEquality(Message m1, Message m2) {
assertEquals(m1.getId(), m2.getId());
assertArrayEquals(m1.getId().getBytes(), m2.getId().getBytes());
assertArrayEquals(m1.getGroupId().getBytes(),
m2.getGroupId().getBytes());
assertEquals(m1.getTimestamp(), m2.getTimestamp());
assertEquals(m1.getLength(), m2.getLength());
assertArrayEquals(m1.getRaw(), m2.getRaw());
}
}

View File

@@ -0,0 +1,20 @@
package org.briarproject;
import org.briarproject.crypto.CryptoModule;
import org.briarproject.sync.SyncModule;
import org.briarproject.transport.TransportModule;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = {
TestSystemModule.class,
CryptoModule.class,
SyncModule.class,
TransportModule.class
})
public interface SyncIntegrationTestComponent {
void inject(SyncIntegrationTest testCase);
}

View File

@@ -1,26 +0,0 @@
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();
}

View File

@@ -1,29 +0,0 @@
package org.briarproject.sync;
import org.briarproject.TestDatabaseModule;
import org.briarproject.TestLifecycleModule;
import org.briarproject.TestSystemModule;
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 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, ClientsModule.class})
public interface ConstantsComponent {
void inject(ConstantsTest testCase);
}