mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Factory and unit test for the Tor plugin; moved slow tests into their
own ant target.
This commit is contained in:
@@ -48,7 +48,8 @@ class PluginManagerImpl implements PluginManager {
|
|||||||
|
|
||||||
private static final String[] DUPLEX_PLUGIN_FACTORIES = new String[] {
|
private static final String[] DUPLEX_PLUGIN_FACTORIES = new String[] {
|
||||||
"net.sf.briar.plugins.bluetooth.BluetoothPluginFactory",
|
"net.sf.briar.plugins.bluetooth.BluetoothPluginFactory",
|
||||||
"net.sf.briar.plugins.socket.SimpleSocketPluginFactory"
|
"net.sf.briar.plugins.socket.SimpleSocketPluginFactory",
|
||||||
|
"net.sf.briar.plugins.tor.TorPluginFactory"
|
||||||
};
|
};
|
||||||
|
|
||||||
private final ExecutorService pluginExecutor;
|
private final ExecutorService pluginExecutor;
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
private final long pollingInterval;
|
private final long pollingInterval;
|
||||||
|
|
||||||
private boolean running = false; // Locking: this
|
private boolean running = false; // Locking: this
|
||||||
private TorNetServerSocket socket = null; // Locking: this
|
private NetServerSocket socket = null; // Locking: this
|
||||||
|
private NetLayer netLayer = null; // Locking: this
|
||||||
|
|
||||||
TorPlugin(@PluginExecutor Executor pluginExecutor,
|
TorPlugin(@PluginExecutor Executor pluginExecutor,
|
||||||
DuplexPluginCallback callback, long pollingInterval) {
|
DuplexPluginCallback callback, long pollingInterval) {
|
||||||
@@ -90,13 +91,12 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
new TorHiddenServicePortPrivateNetAddress(addr, 80);
|
new TorHiddenServicePortPrivateNetAddress(addr, 80);
|
||||||
// Connect to Tor
|
// Connect to Tor
|
||||||
NetFactory netFactory = NetFactory.getInstance();
|
NetFactory netFactory = NetFactory.getInstance();
|
||||||
NetLayer netLayer = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
||||||
netLayer.waitUntilReady();
|
nl.waitUntilReady();
|
||||||
// Publish the hidden service
|
// Publish the hidden service
|
||||||
TorNetServerSocket ss;
|
TorNetServerSocket ss;
|
||||||
try {
|
try {
|
||||||
ss = (TorNetServerSocket) netLayer.createNetServerSocket(null,
|
ss = (TorNetServerSocket) nl.createNetServerSocket(null, addrPort);
|
||||||
addrPort);
|
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
|
||||||
return;
|
return;
|
||||||
@@ -107,6 +107,7 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socket = ss;
|
socket = ss;
|
||||||
|
netLayer = nl;
|
||||||
}
|
}
|
||||||
String onion = addr.getPublicOnionHostname();
|
String onion = addr.getPublicOnionHostname();
|
||||||
if(LOG.isLoggable(Level.INFO)) LOG.info("Listening on " + onion);
|
if(LOG.isLoggable(Level.INFO)) LOG.info("Listening on " + onion);
|
||||||
@@ -161,6 +162,10 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
tryToClose(socket);
|
tryToClose(socket);
|
||||||
socket = null;
|
socket = null;
|
||||||
}
|
}
|
||||||
|
if(netLayer != null) {
|
||||||
|
netLayer.clear();
|
||||||
|
netLayer = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldPoll() {
|
public boolean shouldPoll() {
|
||||||
@@ -203,11 +208,9 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
TransportProperties p = callback.getRemoteProperties().get(c);
|
TransportProperties p = callback.getRemoteProperties().get(c);
|
||||||
if(p == null) return null;
|
if(p == null) return null;
|
||||||
String onion = p.get("onion");
|
String onion = p.get("onion");
|
||||||
String portString = p.get("port");
|
if(onion == null) return null;
|
||||||
if(onion == null || portString == null) return null;
|
|
||||||
try {
|
try {
|
||||||
int port = Integer.parseInt(portString);
|
TcpipNetAddress addr = new TcpipNetAddress(onion, 80);
|
||||||
TcpipNetAddress addr = new TcpipNetAddress(onion, port);
|
|
||||||
NetFactory netFactory = NetFactory.getInstance();
|
NetFactory netFactory = NetFactory.getInstance();
|
||||||
NetLayer netLayer = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
NetLayer netLayer = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
||||||
netLayer.waitUntilReady();
|
netLayer.waitUntilReady();
|
||||||
|
|||||||
18
components/net/sf/briar/plugins/tor/TorPluginFactory.java
Normal file
18
components/net/sf/briar/plugins/tor/TorPluginFactory.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package net.sf.briar.plugins.tor;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import net.sf.briar.api.plugins.PluginExecutor;
|
||||||
|
import net.sf.briar.api.plugins.duplex.DuplexPlugin;
|
||||||
|
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
|
||||||
|
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
|
||||||
|
|
||||||
|
public class TorPluginFactory implements DuplexPluginFactory {
|
||||||
|
|
||||||
|
private static final long POLLING_INTERVAL = 15L * 60L * 1000L; // 15 mins
|
||||||
|
|
||||||
|
public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
|
||||||
|
DuplexPluginCallback callback) {
|
||||||
|
return new TorPlugin(pluginExecutor, callback, POLLING_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,7 +22,6 @@
|
|||||||
<test name='net.sf.briar.db.BasicH2Test'/>
|
<test name='net.sf.briar.db.BasicH2Test'/>
|
||||||
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
|
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
|
||||||
<test name='net.sf.briar.db.DatabaseComponentImplTest'/>
|
<test name='net.sf.briar.db.DatabaseComponentImplTest'/>
|
||||||
<test name='net.sf.briar.db.H2DatabaseTest'/>
|
|
||||||
<test name='net.sf.briar.i18n.FontManagerTest'/>
|
<test name='net.sf.briar.i18n.FontManagerTest'/>
|
||||||
<test name='net.sf.briar.i18n.I18nTest'/>
|
<test name='net.sf.briar.i18n.I18nTest'/>
|
||||||
<test name='net.sf.briar.invitation.InvitationWorkerTest'/>
|
<test name='net.sf.briar.invitation.InvitationWorkerTest'/>
|
||||||
@@ -64,4 +63,22 @@
|
|||||||
<test name='net.sf.briar.util.ZipUtilsTest'/>
|
<test name='net.sf.briar.util.ZipUtilsTest'/>
|
||||||
</junit>
|
</junit>
|
||||||
</target>
|
</target>
|
||||||
|
<target name='test-slow' depends='depend'>
|
||||||
|
<junit printsummary='on' fork='yes' forkmode='once'>
|
||||||
|
<assertions>
|
||||||
|
<enable/>
|
||||||
|
</assertions>
|
||||||
|
<classpath>
|
||||||
|
<fileset refid='bundled-jars'/>
|
||||||
|
<fileset refid='test-jars'/>
|
||||||
|
<path refid='api-classes'/>
|
||||||
|
<path refid='component-classes'/>
|
||||||
|
<path refid='test-classes'/>
|
||||||
|
<path refid='util-classes'/>
|
||||||
|
</classpath>
|
||||||
|
<jvmarg value='-Djava.library.path=../lib'/>
|
||||||
|
<test name='net.sf.briar.db.H2DatabaseTest'/>
|
||||||
|
<test name='net.sf.briar.plugins.tor.TorPluginTest'/>
|
||||||
|
</junit>
|
||||||
|
</target>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.concurrent.Executors;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import net.sf.briar.BriarTestCase;
|
import net.sf.briar.BriarTestCase;
|
||||||
|
import net.sf.briar.api.TransportConfig;
|
||||||
import net.sf.briar.api.TransportProperties;
|
import net.sf.briar.api.TransportProperties;
|
||||||
import net.sf.briar.api.db.DatabaseComponent;
|
import net.sf.briar.api.db.DatabaseComponent;
|
||||||
import net.sf.briar.api.protocol.TransportId;
|
import net.sf.briar.api.protocol.TransportId;
|
||||||
@@ -35,6 +36,8 @@ public class PluginManagerImplTest extends BriarTestCase {
|
|||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
allowing(db).addTransport(with(any(TransportId.class)));
|
allowing(db).addTransport(with(any(TransportId.class)));
|
||||||
will(returnValue(new TransportIndex(index.getAndIncrement())));
|
will(returnValue(new TransportIndex(index.getAndIncrement())));
|
||||||
|
allowing(db).getConfig(with(any(TransportId.class)));
|
||||||
|
will(returnValue(new TransportConfig()));
|
||||||
allowing(db).getLocalProperties(with(any(TransportId.class)));
|
allowing(db).getLocalProperties(with(any(TransportId.class)));
|
||||||
will(returnValue(new TransportProperties()));
|
will(returnValue(new TransportProperties()));
|
||||||
allowing(db).getRemoteProperties(with(any(TransportId.class)));
|
allowing(db).getRemoteProperties(with(any(TransportId.class)));
|
||||||
@@ -46,7 +49,7 @@ public class PluginManagerImplTest extends BriarTestCase {
|
|||||||
ExecutorService executor = Executors.newCachedThreadPool();
|
ExecutorService executor = Executors.newCachedThreadPool();
|
||||||
PluginManagerImpl p = new PluginManagerImpl(executor, db, poller,
|
PluginManagerImpl p = new PluginManagerImpl(executor, db, poller,
|
||||||
dispatcher, uiCallback);
|
dispatcher, uiCallback);
|
||||||
// We expect either 2 or 3 plugins to be started, depending on whether
|
// We expect either 3 or 4 plugins to be started, depending on whether
|
||||||
// the test machine has a Bluetooth device
|
// the test machine has a Bluetooth device
|
||||||
int started = p.start();
|
int started = p.start();
|
||||||
int stopped = p.stop();
|
int stopped = p.stop();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.Hashtable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -104,7 +104,7 @@ public class SimpleSocketPluginTest extends BriarTestCase {
|
|||||||
private static class Callback implements DuplexPluginCallback {
|
private static class Callback implements DuplexPluginCallback {
|
||||||
|
|
||||||
private final Map<ContactId, TransportProperties> remote =
|
private final Map<ContactId, TransportProperties> remote =
|
||||||
new HashMap<ContactId, TransportProperties>();
|
new Hashtable<ContactId, TransportProperties>();
|
||||||
private final CountDownLatch latch = new CountDownLatch(1);
|
private final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
private TransportConfig config = new TransportConfig();
|
private TransportConfig config = new TransportConfig();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.plugins.tor;
|
package net.sf.briar.plugins.tor;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Hashtable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -18,28 +18,48 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class TorPluginTest extends BriarTestCase {
|
public class TorPluginTest extends BriarTestCase {
|
||||||
|
|
||||||
|
private final ContactId contactId = new ContactId(1);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateHiddenService() throws Exception {
|
public void testHiddenService() throws Exception {
|
||||||
Callback callback = new Callback();
|
|
||||||
Executor e = Executors.newCachedThreadPool();
|
Executor e = Executors.newCachedThreadPool();
|
||||||
TorPlugin plugin = new TorPlugin(e, callback, 0L);
|
// Create a plugin instance for the server
|
||||||
plugin.start();
|
Callback serverCallback = new Callback();
|
||||||
// The plugin should have created a hidden service
|
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L);
|
||||||
callback.latch.await(5, TimeUnit.MINUTES);
|
serverPlugin.start();
|
||||||
String onion = callback.local.get("onion");
|
// The plugin should create a hidden service... eventually
|
||||||
|
serverCallback.latch.await(5, TimeUnit.MINUTES);
|
||||||
|
String onion = serverCallback.local.get("onion");
|
||||||
assertNotNull(onion);
|
assertNotNull(onion);
|
||||||
assertTrue(onion.endsWith(".onion"));
|
assertTrue(onion.endsWith(".onion"));
|
||||||
|
// Create another plugin instance for the client
|
||||||
|
Callback clientCallback = new Callback();
|
||||||
|
TransportProperties p = new TransportProperties();
|
||||||
|
p.put("onion", onion);
|
||||||
|
clientCallback.remote.put(contactId, p);
|
||||||
|
TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L);
|
||||||
|
clientPlugin.start();
|
||||||
|
// Connect to the server's hidden service
|
||||||
|
DuplexTransportConnection c = clientPlugin.createConnection(contactId);
|
||||||
|
assertNotNull(c);
|
||||||
|
c.dispose(false, false);
|
||||||
|
assertEquals(1, serverCallback.incomingConnections);
|
||||||
|
// Stop the plugins
|
||||||
|
serverPlugin.stop();
|
||||||
|
clientPlugin.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Callback implements DuplexPluginCallback {
|
private static class Callback implements DuplexPluginCallback {
|
||||||
|
|
||||||
private final Map<ContactId, TransportProperties> remote =
|
private final Map<ContactId, TransportProperties> remote =
|
||||||
new HashMap<ContactId, TransportProperties>();
|
new Hashtable<ContactId, TransportProperties>();
|
||||||
private final CountDownLatch latch = new CountDownLatch(1);
|
private final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
private TransportConfig config = new TransportConfig();
|
private TransportConfig config = new TransportConfig();
|
||||||
private TransportProperties local = new TransportProperties();
|
private TransportProperties local = new TransportProperties();
|
||||||
|
|
||||||
|
private volatile int incomingConnections = 0;
|
||||||
|
|
||||||
public TransportConfig getConfig() {
|
public TransportConfig getConfig() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
@@ -71,7 +91,9 @@ public class TorPluginTest extends BriarTestCase {
|
|||||||
|
|
||||||
public void showMessage(String... message) {}
|
public void showMessage(String... message) {}
|
||||||
|
|
||||||
public void incomingConnectionCreated(DuplexTransportConnection d) {}
|
public void incomingConnectionCreated(DuplexTransportConnection d) {
|
||||||
|
incomingConnections++;
|
||||||
|
}
|
||||||
|
|
||||||
public void outgoingConnectionCreated(ContactId c,
|
public void outgoingConnectionCreated(ContactId c,
|
||||||
DuplexTransportConnection d) {}
|
DuplexTransportConnection d) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user