mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Optionally create a Tor plugin without a hidden service. Should speed up
tests.
This commit is contained in:
@@ -73,10 +73,32 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
|
// Connect to Tor
|
||||||
|
NetFactory netFactory = NetFactory.getInstance();
|
||||||
|
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
||||||
|
nl.waitUntilReady();
|
||||||
|
synchronized(this) {
|
||||||
|
if(!running) {
|
||||||
|
tryToClear(nl);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
netLayer = nl;
|
||||||
|
connected = true;
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
// If we're configure not to create a hidden service, return
|
||||||
|
TransportConfig c = callback.getConfig();
|
||||||
|
if(c.containsKey("noHiddenService")) {
|
||||||
|
if(LOG.isLoggable(Level.INFO))
|
||||||
|
LOG.info("Not creating hidden service");
|
||||||
|
TransportProperties p = callback.getLocalProperties();
|
||||||
|
p.remove("onion");
|
||||||
|
callback.setLocalProperties(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Retrieve the hidden service address, or create one if necessary
|
// Retrieve the hidden service address, or create one if necessary
|
||||||
TorHiddenServicePrivateNetAddress addr;
|
TorHiddenServicePrivateNetAddress addr;
|
||||||
TorNetLayerUtil util = TorNetLayerUtil.getInstance();
|
TorNetLayerUtil util = TorNetLayerUtil.getInstance();
|
||||||
TransportConfig c = callback.getConfig();
|
|
||||||
String privateKey = c.get("privateKey");
|
String privateKey = c.get("privateKey");
|
||||||
if(privateKey == null) {
|
if(privateKey == null) {
|
||||||
addr = createHiddenServiceAddress(util, c);
|
addr = createHiddenServiceAddress(util, c);
|
||||||
@@ -91,19 +113,6 @@ class TorPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
TorHiddenServicePortPrivateNetAddress addrPort =
|
TorHiddenServicePortPrivateNetAddress addrPort =
|
||||||
new TorHiddenServicePortPrivateNetAddress(addr, 80);
|
new TorHiddenServicePortPrivateNetAddress(addr, 80);
|
||||||
// Connect to Tor
|
|
||||||
NetFactory netFactory = NetFactory.getInstance();
|
|
||||||
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
|
||||||
nl.waitUntilReady();
|
|
||||||
synchronized(this) {
|
|
||||||
if(!running) {
|
|
||||||
tryToClear(nl);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
netLayer = nl;
|
|
||||||
connected = true;
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
// Publish the hidden service
|
// Publish the hidden service
|
||||||
NetServerSocket ss;
|
NetServerSocket ss;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -30,20 +30,23 @@ public class TorPluginTest extends BriarTestCase {
|
|||||||
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L);
|
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L);
|
||||||
serverPlugin.start();
|
serverPlugin.start();
|
||||||
// The plugin should create a hidden service... eventually
|
// The plugin should create a hidden service... eventually
|
||||||
serverCallback.latch.await(10, TimeUnit.MINUTES);
|
assertTrue(serverCallback.latch.await(10, TimeUnit.MINUTES));
|
||||||
String onion = serverCallback.local.get("onion");
|
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
|
// Create another plugin instance for the client
|
||||||
Callback clientCallback = new Callback();
|
Callback clientCallback = new Callback();
|
||||||
|
clientCallback.config.put("noHiddenService", "");
|
||||||
TransportProperties p = new TransportProperties();
|
TransportProperties p = new TransportProperties();
|
||||||
p.put("onion", onion);
|
p.put("onion", onion);
|
||||||
clientCallback.remote.put(contactId, p);
|
clientCallback.remote.put(contactId, p);
|
||||||
TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L);
|
TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L);
|
||||||
clientPlugin.start();
|
clientPlugin.start();
|
||||||
|
// The plugin should start without creating a hidden service
|
||||||
|
assertTrue(clientCallback.latch.await(10, TimeUnit.MINUTES));
|
||||||
// Connect to the server's hidden service
|
// Connect to the server's hidden service
|
||||||
DuplexTransportConnection clientEnd =
|
DuplexTransportConnection clientEnd =
|
||||||
clientPlugin.createConnection(contactId);
|
clientPlugin.createConnection(contactId);
|
||||||
assertNotNull(clientEnd);
|
assertNotNull(clientEnd);
|
||||||
DuplexTransportConnection serverEnd = serverCallback.incomingConnection;
|
DuplexTransportConnection serverEnd = serverCallback.incomingConnection;
|
||||||
assertNotNull(serverEnd);
|
assertNotNull(serverEnd);
|
||||||
@@ -69,13 +72,14 @@ public class TorPluginTest extends BriarTestCase {
|
|||||||
TorPlugin plugin = new TorPlugin(e, callback, 0L);
|
TorPlugin plugin = new TorPlugin(e, callback, 0L);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
// The plugin should create a hidden service... eventually
|
// The plugin should create a hidden service... eventually
|
||||||
callback.latch.await(10, TimeUnit.MINUTES);
|
assertTrue(callback.latch.await(10, TimeUnit.MINUTES));
|
||||||
String onion = callback.local.get("onion");
|
String onion = callback.local.get("onion");
|
||||||
assertNotNull(onion);
|
assertNotNull(onion);
|
||||||
assertTrue(onion.endsWith(".onion"));
|
assertTrue(onion.endsWith(".onion"));
|
||||||
// Get the PEM-encoded private key and stop the plugin
|
// Get the PEM-encoded private key
|
||||||
String privateKey = callback.config.get("privateKey");
|
String privateKey = callback.config.get("privateKey");
|
||||||
assertNotNull(privateKey);
|
assertNotNull(privateKey);
|
||||||
|
// Stop the plugin
|
||||||
plugin.stop();
|
plugin.stop();
|
||||||
// Start another instance, reusing the private key
|
// Start another instance, reusing the private key
|
||||||
callback = new Callback();
|
callback = new Callback();
|
||||||
@@ -83,7 +87,7 @@ public class TorPluginTest extends BriarTestCase {
|
|||||||
plugin = new TorPlugin(e, callback, 0L);
|
plugin = new TorPlugin(e, callback, 0L);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
// The plugin should create a hidden service... eventually
|
// The plugin should create a hidden service... eventually
|
||||||
callback.latch.await(10, TimeUnit.MINUTES);
|
assertTrue(callback.latch.await(10, TimeUnit.MINUTES));
|
||||||
// The onion URL should be the same
|
// The onion URL should be the same
|
||||||
assertEquals(onion, callback.local.get("onion"));
|
assertEquals(onion, callback.local.get("onion"));
|
||||||
// The private key should be the same
|
// The private key should be the same
|
||||||
@@ -95,7 +99,7 @@ public class TorPluginTest 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 Hashtable<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();
|
||||||
|
|||||||
Reference in New Issue
Block a user