mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Add integration test for new connection manager methods.
This commit is contained in:
@@ -3,9 +3,9 @@ package org.briarproject.bramble.contact;
|
|||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager.HandshakeResult;
|
|
||||||
import org.briarproject.bramble.api.contact.PendingContact;
|
import org.briarproject.bramble.api.contact.PendingContact;
|
||||||
import org.briarproject.bramble.api.contact.PendingContactState;
|
import org.briarproject.bramble.api.contact.PendingContactState;
|
||||||
|
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.identity.Identity;
|
import org.briarproject.bramble.api.identity.Identity;
|
||||||
@@ -14,19 +14,14 @@ import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
|||||||
import org.briarproject.bramble.test.BrambleTestCase;
|
import org.briarproject.bramble.test.BrambleTestCase;
|
||||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||||
import org.briarproject.bramble.test.TestDuplexTransportConnection;
|
import org.briarproject.bramble.test.TestDuplexTransportConnection;
|
||||||
import org.briarproject.bramble.test.TestStreamWriter;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.PipedInputStream;
|
|
||||||
import java.io.PipedOutputStream;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
import static junit.framework.TestCase.assertNotNull;
|
import static junit.framework.TestCase.assertNotNull;
|
||||||
@@ -34,12 +29,12 @@ import static junit.framework.TestCase.assertNull;
|
|||||||
import static junit.framework.TestCase.fail;
|
import static junit.framework.TestCase.fail;
|
||||||
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||||
import static org.briarproject.bramble.test.TestDuplexTransportConnection.createPair;
|
import static org.briarproject.bramble.test.TestDuplexTransportConnection.createPair;
|
||||||
|
import static org.briarproject.bramble.test.TestPluginConfigModule.DUPLEX_TRANSPORT_ID;
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
||||||
@@ -116,8 +111,8 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
assertTrue(aliceFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
bobFinished.await(TIMEOUT, MILLISECONDS);
|
assertTrue(bobFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
assertContacts(verified, false);
|
assertContacts(verified, false);
|
||||||
assertNoPendingContacts();
|
assertNoPendingContacts();
|
||||||
}
|
}
|
||||||
@@ -155,8 +150,8 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
assertTrue(aliceFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
bobFinished.await(TIMEOUT, MILLISECONDS);
|
assertTrue(bobFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
assertContacts(verified, true);
|
assertContacts(verified, true);
|
||||||
assertNoPendingContacts();
|
assertNoPendingContacts();
|
||||||
}
|
}
|
||||||
@@ -168,54 +163,29 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
|||||||
PendingContact aliceFromBob = addPendingContact(bob, alice);
|
PendingContact aliceFromBob = addPendingContact(bob, alice);
|
||||||
assertPendingContacts();
|
assertPendingContacts();
|
||||||
|
|
||||||
PipedInputStream aliceHandshakeIn = new PipedInputStream();
|
|
||||||
PipedInputStream bobHandshakeIn = new PipedInputStream();
|
|
||||||
OutputStream aliceHandshakeOut = new PipedOutputStream(bobHandshakeIn);
|
|
||||||
OutputStream bobHandshakeOut = new PipedOutputStream(aliceHandshakeIn);
|
|
||||||
AtomicReference<HandshakeResult> aliceResult = new AtomicReference<>();
|
|
||||||
AtomicReference<HandshakeResult> bobResult = new AtomicReference<>();
|
|
||||||
|
|
||||||
TestDuplexTransportConnection[] pair = createPair();
|
TestDuplexTransportConnection[] pair = createPair();
|
||||||
TestDuplexTransportConnection aliceConnection = pair[0];
|
TestDuplexTransportConnection aliceConnection = pair[0];
|
||||||
TestDuplexTransportConnection bobConnection = pair[1];
|
TestDuplexTransportConnection bobConnection = pair[1];
|
||||||
CountDownLatch aliceFinished = new CountDownLatch(1);
|
CountDownLatch aliceFinished = new CountDownLatch(1);
|
||||||
CountDownLatch bobFinished = new CountDownLatch(1);
|
CountDownLatch bobFinished = new CountDownLatch(1);
|
||||||
boolean verified = random.nextBoolean();
|
|
||||||
|
|
||||||
alice.getIoExecutor().execute(() -> {
|
alice.getEventBus().addListener(e -> {
|
||||||
try {
|
if (e instanceof ContactAddedEvent) aliceFinished.countDown();
|
||||||
HandshakeResult result = alice.getHandshakeManager().handshake(
|
|
||||||
bobFromAlice.getId(), aliceHandshakeIn,
|
|
||||||
new TestStreamWriter(aliceHandshakeOut));
|
|
||||||
aliceResult.set(result);
|
|
||||||
alice.getContactExchangeManager().exchangeContacts(
|
|
||||||
bobFromAlice.getId(), aliceConnection,
|
|
||||||
result.getMasterKey(), result.isAlice(), verified);
|
|
||||||
aliceFinished.countDown();
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
bob.getIoExecutor().execute(() -> {
|
alice.getIoExecutor().execute(() ->
|
||||||
try {
|
alice.getConnectionManager().manageOutgoingConnection(
|
||||||
HandshakeResult result = bob.getHandshakeManager().handshake(
|
bobFromAlice.getId(), DUPLEX_TRANSPORT_ID,
|
||||||
aliceFromBob.getId(), bobHandshakeIn,
|
aliceConnection));
|
||||||
new TestStreamWriter(bobHandshakeOut));
|
bob.getEventBus().addListener(e -> {
|
||||||
bobResult.set(result);
|
if (e instanceof ContactAddedEvent) bobFinished.countDown();
|
||||||
bob.getContactExchangeManager().exchangeContacts(
|
|
||||||
aliceFromBob.getId(), bobConnection,
|
|
||||||
result.getMasterKey(), result.isAlice(), verified);
|
|
||||||
bobFinished.countDown();
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
aliceFinished.await(TIMEOUT, MILLISECONDS);
|
bob.getIoExecutor().execute(() ->
|
||||||
bobFinished.await(TIMEOUT, MILLISECONDS);
|
bob.getConnectionManager().manageIncomingConnection(
|
||||||
assertArrayEquals(aliceResult.get().getMasterKey().getBytes(),
|
aliceFromBob.getId(), DUPLEX_TRANSPORT_ID,
|
||||||
bobResult.get().getMasterKey().getBytes());
|
bobConnection));
|
||||||
assertNotEquals(aliceResult.get().isAlice(), bobResult.get().isAlice());
|
assertTrue(aliceFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
assertContacts(verified, true);
|
assertTrue(bobFinished.await(TIMEOUT, MILLISECONDS));
|
||||||
|
assertContacts(false, true);
|
||||||
assertNoPendingContacts();
|
assertNoPendingContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
|||||||
import org.briarproject.bramble.BrambleCoreModule;
|
import org.briarproject.bramble.BrambleCoreModule;
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -24,11 +25,13 @@ import dagger.Component;
|
|||||||
interface ContactExchangeIntegrationTestComponent
|
interface ContactExchangeIntegrationTestComponent
|
||||||
extends BrambleCoreEagerSingletons {
|
extends BrambleCoreEagerSingletons {
|
||||||
|
|
||||||
|
ConnectionManager getConnectionManager();
|
||||||
|
|
||||||
ContactExchangeManager getContactExchangeManager();
|
ContactExchangeManager getContactExchangeManager();
|
||||||
|
|
||||||
ContactManager getContactManager();
|
ContactManager getContactManager();
|
||||||
|
|
||||||
HandshakeManager getHandshakeManager();
|
EventBus getEventBus();
|
||||||
|
|
||||||
IdentityManager getIdentityManager();
|
IdentityManager getIdentityManager();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user