Pass a connection handler to plugins when polling.

This commit is contained in:
akwizgran
2019-05-17 13:58:18 +01:00
parent a0772852de
commit 668433dd62
39 changed files with 391 additions and 320 deletions

View File

@@ -2,14 +2,13 @@ package org.briarproject.bramble.plugin;
import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.plugin.ConnectionManager;
import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.PluginException;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginCallback;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
import org.briarproject.bramble.api.properties.TransportPropertyManager;
import org.briarproject.bramble.api.settings.SettingsManager;
@@ -78,15 +77,14 @@ public class PluginManagerImplTest extends BrambleTestCase {
simplexFailFactory)));
oneOf(simplexFactory).getId();
will(returnValue(simplexId));
oneOf(simplexFactory).createPlugin(with(any(
SimplexPluginCallback.class)));
oneOf(simplexFactory).createPlugin(with(any(PluginCallback.class)));
will(returnValue(simplexPlugin)); // Created
oneOf(simplexPlugin).start();
// Second simplex plugin
oneOf(simplexFailFactory).getId();
will(returnValue(simplexFailId));
oneOf(simplexFailFactory).createPlugin(with(any(
SimplexPluginCallback.class)));
PluginCallback.class)));
will(returnValue(simplexFailPlugin)); // Created
oneOf(simplexFailPlugin).start();
will(throwException(new PluginException()));
@@ -95,15 +93,14 @@ public class PluginManagerImplTest extends BrambleTestCase {
will(returnValue(Arrays.asList(duplexFactory, duplexFailFactory)));
oneOf(duplexFactory).getId();
will(returnValue(duplexId));
oneOf(duplexFactory).createPlugin(with(any(
DuplexPluginCallback.class)));
oneOf(duplexFactory).createPlugin(with(any(PluginCallback.class)));
will(returnValue(duplexPlugin)); // Created
oneOf(duplexPlugin).start();
// Second duplex plugin
oneOf(duplexFailFactory).getId();
will(returnValue(duplexFailId));
oneOf(duplexFailFactory).createPlugin(with(any(
DuplexPluginCallback.class)));
PluginCallback.class)));
will(returnValue(null)); // Failed to create a plugin
// stop()
// Stop the plugins

View File

@@ -2,6 +2,7 @@ package org.briarproject.bramble.plugin;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
import org.briarproject.bramble.api.plugin.ConnectionHandler;
import org.briarproject.bramble.api.plugin.ConnectionManager;
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
import org.briarproject.bramble.api.plugin.Plugin;
@@ -37,6 +38,8 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.briarproject.bramble.test.CollectionMatcher.collectionOf;
import static org.briarproject.bramble.test.PairMatcher.pairOf;
import static org.briarproject.bramble.test.TestUtils.getContactId;
import static org.briarproject.bramble.test.TestUtils.getTransportId;
@@ -354,7 +357,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(connectionRegistry).getConnectedContacts(transportId);
will(returnValue(emptyList()));
// Poll the plugin
oneOf(plugin).poll(singletonMap(contactId, properties));
oneOf(plugin).poll(with(collectionOf(
pairOf(equal(properties), any(ConnectionHandler.class)))));
}});
poller.eventOccurred(new TransportEnabledEvent(transportId));

View File

@@ -1,12 +1,13 @@
package org.briarproject.bramble.plugin.tcp;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.Backoff;
import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.settings.Settings;
@@ -21,13 +22,14 @@ import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import static java.net.NetworkInterface.getNetworkInterfaces;
import static java.util.Collections.list;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
@@ -39,10 +41,13 @@ import static org.junit.Assert.assertTrue;
public class LanTcpPluginTest extends BrambleTestCase {
private final Backoff backoff = new TestBackoff();
private final ExecutorService ioExecutor = newCachedThreadPool();
@Test
public void testAddressesAreOnSameLan() {
LanTcpPlugin plugin = new LanTcpPlugin(null, null, null, 0, 0);
Callback callback = new Callback();
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
0, 0);
// Local and remote in 10.0.0.0/8 should return true
assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
makeAddress(10, 255, 255, 255)));
@@ -93,8 +98,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
return;
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
@@ -129,8 +133,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
return;
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
@@ -179,8 +182,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
return;
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
0, 0);
plugin.start();
assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -228,8 +230,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
return;
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
@@ -327,9 +328,8 @@ public class LanTcpPluginTest extends BrambleTestCase {
}
private boolean systemHasLocalIpv4Address() throws Exception {
for (NetworkInterface i : Collections.list(
NetworkInterface.getNetworkInterfaces())) {
for (InetAddress a : Collections.list(i.getInetAddresses())) {
for (NetworkInterface i : list(getNetworkInterfaces())) {
for (InetAddress a : list(i.getInetAddresses())) {
if (a instanceof Inet4Address)
return a.isLinkLocalAddress() || a.isSiteLocalAddress();
}
@@ -338,7 +338,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
}
@NotNullByDefault
private static class Callback implements DuplexPluginCallback {
private static class Callback implements PluginCallback {
private final CountDownLatch propertiesLatch = new CountDownLatch(1);
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
@@ -364,16 +364,6 @@ public class LanTcpPluginTest extends BrambleTestCase {
propertiesLatch.countDown();
}
@Override
public void incomingConnectionCreated(DuplexTransportConnection d) {
connectionsLatch.countDown();
}
@Override
public void outgoingConnectionCreated(ContactId c,
DuplexTransportConnection d) {
}
@Override
public void transportEnabled() {
}
@@ -381,6 +371,19 @@ public class LanTcpPluginTest extends BrambleTestCase {
@Override
public void transportDisabled() {
}
@Override
public void handleConnection(DuplexTransportConnection d) {
connectionsLatch.countDown();
}
@Override
public void handleReader(TransportConnectionReader r) {
}
@Override
public void handleWriter(TransportConnectionWriter w) {
}
}
private static class TestBackoff implements Backoff {

View File

@@ -0,0 +1,39 @@
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import java.util.Collection;
import javax.annotation.Nullable;
@NotNullByDefault
public class CollectionMatcher<T> extends BaseMatcher<Collection<T>> {
private final Matcher<T> elementMatcher;
public CollectionMatcher(Matcher<T> elementMatcher) {
this.elementMatcher = elementMatcher;
}
@Override
public boolean matches(@Nullable Object item) {
if (!(item instanceof Collection)) return false;
Collection collection = (Collection) item;
for (Object element : collection) {
if (!elementMatcher.matches(element)) return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("matches a collection");
}
public static <T> CollectionMatcher<T> collectionOf(Matcher<T> t) {
return new CollectionMatcher<>(t);
}
}

View File

@@ -0,0 +1,38 @@
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.Pair;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import javax.annotation.Nullable;
@NotNullByDefault
public class PairMatcher<A, B> extends BaseMatcher<Pair<A, B>> {
private final Matcher<A> firstMatcher;
private final Matcher<B> secondMatcher;
public PairMatcher(Matcher<A> firstMatcher, Matcher<B> secondMatcher) {
this.firstMatcher = firstMatcher;
this.secondMatcher = secondMatcher;
}
@Override
public boolean matches(@Nullable Object item) {
if (!(item instanceof Pair)) return false;
Pair pair = (Pair) item;
return firstMatcher.matches(pair.getFirst()) &&
secondMatcher.matches(pair.getSecond());
}
@Override
public void describeTo(Description description) {
description.appendText("matches a pair");
}
public static <A, B> PairMatcher<A, B> pairOf(Matcher<A> a, Matcher<B> b) {
return new PairMatcher<>(a, b);
}
}

View File

@@ -1,11 +1,11 @@
package org.briarproject.bramble.test;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginCallback;
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
import java.util.Collection;
@@ -40,7 +40,7 @@ public class TestPluginConfigModule {
@Override
@Nullable
public SimplexPlugin createPlugin(SimplexPluginCallback callback) {
public SimplexPlugin createPlugin(PluginCallback callback) {
return null;
}
};