From a82ae821081014bf84f045850c1d68fb0feeada5 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 4 May 2012 17:46:29 +0100
Subject: [PATCH] Optionally create a Tor plugin without a hidden service.
 Should speed up tests.

---
 .../net/sf/briar/plugins/tor/TorPlugin.java   | 37 ++++++++++++-------
 .../sf/briar/plugins/tor/TorPluginTest.java   | 16 +++++---
 2 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/components/net/sf/briar/plugins/tor/TorPlugin.java b/components/net/sf/briar/plugins/tor/TorPlugin.java
index e1b61f3858..793c632b70 100644
--- a/components/net/sf/briar/plugins/tor/TorPlugin.java
+++ b/components/net/sf/briar/plugins/tor/TorPlugin.java
@@ -73,10 +73,32 @@ class TorPlugin implements DuplexPlugin {
 	}
 
 	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
 		TorHiddenServicePrivateNetAddress addr;
 		TorNetLayerUtil util = TorNetLayerUtil.getInstance();
-		TransportConfig c = callback.getConfig();
 		String privateKey = c.get("privateKey");
 		if(privateKey == null) {
 			addr = createHiddenServiceAddress(util, c);
@@ -91,19 +113,6 @@ class TorPlugin implements DuplexPlugin {
 		}
 		TorHiddenServicePortPrivateNetAddress addrPort =
 				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
 		NetServerSocket ss;
 		try {
diff --git a/test/net/sf/briar/plugins/tor/TorPluginTest.java b/test/net/sf/briar/plugins/tor/TorPluginTest.java
index 4fbce020b5..446b53ecaa 100644
--- a/test/net/sf/briar/plugins/tor/TorPluginTest.java
+++ b/test/net/sf/briar/plugins/tor/TorPluginTest.java
@@ -30,20 +30,23 @@ public class TorPluginTest extends BriarTestCase {
 		TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L);
 		serverPlugin.start();
 		// 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");
 		assertNotNull(onion);
 		assertTrue(onion.endsWith(".onion"));
 		// Create another plugin instance for the client
 		Callback clientCallback = new Callback();
+		clientCallback.config.put("noHiddenService", "");
 		TransportProperties p = new TransportProperties();
 		p.put("onion", onion);
 		clientCallback.remote.put(contactId, p);
 		TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L);
 		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
 		DuplexTransportConnection clientEnd =
-			clientPlugin.createConnection(contactId);
+				clientPlugin.createConnection(contactId);
 		assertNotNull(clientEnd);
 		DuplexTransportConnection serverEnd = serverCallback.incomingConnection;
 		assertNotNull(serverEnd);
@@ -69,13 +72,14 @@ public class TorPluginTest extends BriarTestCase {
 		TorPlugin plugin = new TorPlugin(e, callback, 0L);
 		plugin.start();
 		// 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");
 		assertNotNull(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");
 		assertNotNull(privateKey);
+		// Stop the plugin
 		plugin.stop();
 		// Start another instance, reusing the private key
 		callback = new Callback();
@@ -83,7 +87,7 @@ public class TorPluginTest extends BriarTestCase {
 		plugin = new TorPlugin(e, callback, 0L);
 		plugin.start();
 		// 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
 		assertEquals(onion, callback.local.get("onion"));
 		// The private key should be the same
@@ -95,7 +99,7 @@ public class TorPluginTest extends BriarTestCase {
 	private static class Callback implements DuplexPluginCallback {
 
 		private final Map<ContactId, TransportProperties> remote =
-			new Hashtable<ContactId, TransportProperties>();
+				new Hashtable<ContactId, TransportProperties>();
 		private final CountDownLatch latch = new CountDownLatch(1);
 
 		private TransportConfig config = new TransportConfig();
-- 
GitLab