Debugging code to track down TorPuginTest failures.

This commit is contained in:
akwizgran
2012-05-12 16:58:00 +02:00
parent 46ed0cc4b1
commit c1133644e8
3 changed files with 110 additions and 65 deletions

View File

@@ -76,7 +76,11 @@ class TorPlugin implements DuplexPlugin {
// Connect to Tor // Connect to Tor
NetFactory netFactory = NetFactory.getInstance(); NetFactory netFactory = NetFactory.getInstance();
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR); NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
if(LOG.isLoggable(Level.INFO))
LOG.info("Waiting for net layer to be ready");
nl.waitUntilReady(); nl.waitUntilReady();
if(LOG.isLoggable(Level.INFO))
LOG.info("Net layer is ready");
synchronized(this) { synchronized(this) {
if(!running) { if(!running) {
tryToClear(nl); tryToClear(nl);
@@ -101,8 +105,12 @@ class TorPlugin implements DuplexPlugin {
TorNetLayerUtil util = TorNetLayerUtil.getInstance(); TorNetLayerUtil util = TorNetLayerUtil.getInstance();
String privateKey = c.get("privateKey"); String privateKey = c.get("privateKey");
if(privateKey == null) { if(privateKey == null) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Creating hidden service address");
addr = createHiddenServiceAddress(util, c); addr = createHiddenServiceAddress(util, c);
} else { } else {
if(LOG.isLoggable(Level.INFO))
LOG.info("Parsing hidden service address");
try { try {
addr = util.parseTorHiddenServicePrivateNetAddressFromStrings( addr = util.parseTorHiddenServicePrivateNetAddressFromStrings(
privateKey, "", false); privateKey, "", false);
@@ -115,6 +123,7 @@ class TorPlugin implements DuplexPlugin {
new TorHiddenServicePortPrivateNetAddress(addr, 80); new TorHiddenServicePortPrivateNetAddress(addr, 80);
// Publish the hidden service // Publish the hidden service
NetServerSocket ss; NetServerSocket ss;
if(LOG.isLoggable(Level.INFO)) LOG.info("Publishing hidden service");
try { try {
ss = nl.createNetServerSocket(null, addrPort); ss = nl.createNetServerSocket(null, addrPort);
} catch(IOException e) { } catch(IOException e) {
@@ -234,6 +243,8 @@ class TorPlugin implements DuplexPlugin {
synchronized(this) { synchronized(this) {
while(!connected) { while(!connected) {
if(!running) return null; if(!running) return null;
if(LOG.isLoggable(Level.INFO))
LOG.info("Waiting for net layer before connecting");
try { try {
wait(); wait();
} catch(InterruptedException e) { } catch(InterruptedException e) {
@@ -251,7 +262,11 @@ class TorPlugin implements DuplexPlugin {
if(onion == null) return null; if(onion == null) return null;
NetAddress addr = new TcpipNetAddress(onion, 80); NetAddress addr = new TcpipNetAddress(onion, 80);
try { try {
if(LOG.isLoggable(Level.INFO))
LOG.info("Connecting to hidden service");
NetSocket s = nl.createNetSocket(null, null, addr); NetSocket s = nl.createNetSocket(null, null, addr);
if(LOG.isLoggable(Level.INFO))
LOG.info("Connected to hidden service");
return new TorTransportConnection(s); return new TorTransportConnection(s);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.INFO)) LOG.info(e.toString()); if(LOG.isLoggable(Level.INFO)) LOG.info(e.toString());

View File

@@ -63,7 +63,7 @@
</junit> </junit>
</target> </target>
<target name='test-slow' depends='depend'> <target name='test-slow' depends='depend'>
<junit printsummary='on' fork='yes' forkmode='once'> <junit printsummary='withOutAndErr' fork='yes' forkmode='once'>
<assertions> <assertions>
<enable/> <enable/>
</assertions> </assertions>

View File

@@ -9,6 +9,8 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
@@ -25,12 +27,16 @@ public class TorPluginTest extends BriarTestCase {
@Test @Test
public void testHiddenService() throws Exception { public void testHiddenService() throws Exception {
Executor e = Executors.newCachedThreadPool(); Executor e = Executors.newCachedThreadPool();
TorPlugin serverPlugin = null, clientPlugin = null;
try {
// Create a plugin instance for the server // Create a plugin instance for the server
Callback serverCallback = new Callback(); Callback serverCallback = new Callback();
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L); serverPlugin = new TorPlugin(e, serverCallback, 0L);
System.out.println("Starting server plugin");
serverPlugin.start(); serverPlugin.start();
// The plugin should create a hidden service... eventually // The plugin should create a hidden service... eventually
assertTrue(serverCallback.latch.await(10, TimeUnit.MINUTES)); assertTrue(serverCallback.latch.await(5, TimeUnit.MINUTES));
System.out.println("Started server plugin");
String onion = serverCallback.local.get("onion"); String onion = serverCallback.local.get("onion");
assertNotNull(onion); assertNotNull(onion);
assertTrue(onion.endsWith(".onion")); assertTrue(onion.endsWith(".onion"));
@@ -40,16 +46,20 @@ public class TorPluginTest extends BriarTestCase {
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); clientPlugin = new TorPlugin(e, clientCallback, 0L);
System.out.println("Starting client plugin");
clientPlugin.start(); clientPlugin.start();
// The plugin should start without creating a hidden service // The plugin should start without creating a hidden service
assertTrue(clientCallback.latch.await(10, TimeUnit.MINUTES)); assertTrue(clientCallback.latch.await(5, TimeUnit.MINUTES));
System.out.println("Started client plugin");
// Connect to the server's hidden service // Connect to the server's hidden service
System.out.println("Connecting to 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);
System.out.println("Connected to hidden service");
// Send some data through the Tor connection // Send some data through the Tor connection
PrintStream out = new PrintStream(clientEnd.getOutputStream()); PrintStream out = new PrintStream(clientEnd.getOutputStream());
out.println("Hello world"); out.println("Hello world");
@@ -59,20 +69,30 @@ public class TorPluginTest extends BriarTestCase {
assertEquals("Hello world", in.nextLine()); assertEquals("Hello world", in.nextLine());
serverEnd.dispose(false, false); serverEnd.dispose(false, false);
clientEnd.dispose(false, false); clientEnd.dispose(false, false);
} catch(AssertionFailedError e1) {
System.out.println(e);
} finally {
// Stop the plugins // Stop the plugins
serverPlugin.stop(); System.out.println("Stopping plugins");
clientPlugin.stop(); if(serverPlugin != null) serverPlugin.stop();
if(clientPlugin != null) clientPlugin.stop();
System.out.println("Stopped plugins");
}
} }
@Test @Test
public void testStoreAndRetrievePrivateKey() throws Exception { public void testStoreAndRetrievePrivateKey() throws Exception {
Executor e = Executors.newCachedThreadPool(); Executor e = Executors.newCachedThreadPool();
TorPlugin plugin = null;
try {
// Start a plugin instance with no private key // Start a plugin instance with no private key
Callback callback = new Callback(); Callback callback = new Callback();
TorPlugin plugin = new TorPlugin(e, callback, 0L); plugin = new TorPlugin(e, callback, 0L);
System.out.println("Starting plugin without private key");
plugin.start(); plugin.start();
// The plugin should create a hidden service... eventually // The plugin should create a hidden service... eventually
assertTrue(callback.latch.await(10, TimeUnit.MINUTES)); assertTrue(callback.latch.await(5, TimeUnit.MINUTES));
System.out.println("Started plugin");
String onion = callback.local.get("onion"); String onion = callback.local.get("onion");
assertNotNull(onion); assertNotNull(onion);
assertTrue(onion.endsWith(".onion")); assertTrue(onion.endsWith(".onion"));
@@ -80,20 +100,30 @@ public class TorPluginTest extends BriarTestCase {
String privateKey = callback.config.get("privateKey"); String privateKey = callback.config.get("privateKey");
assertNotNull(privateKey); assertNotNull(privateKey);
// Stop the plugin // Stop the plugin
System.out.println("Stopping plugin");
plugin.stop(); plugin.stop();
System.out.println("Stopped plugin");
// Start another instance, reusing the private key // Start another instance, reusing the private key
callback = new Callback(); callback = new Callback();
callback.config.put("privateKey", privateKey); callback.config.put("privateKey", privateKey);
plugin = new TorPlugin(e, callback, 0L); plugin = new TorPlugin(e, callback, 0L);
System.out.println("Starting plugin with private key");
plugin.start(); plugin.start();
// The plugin should create a hidden service... eventually // The plugin should create a hidden service... eventually
assertTrue(callback.latch.await(10, TimeUnit.MINUTES)); assertTrue(callback.latch.await(5, TimeUnit.MINUTES));
System.out.println("Started plugin");
// 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
assertEquals(privateKey, callback.config.get("privateKey")); assertEquals(privateKey, callback.config.get("privateKey"));
} catch(AssertionFailedError e1) {
System.out.println(e);
} finally {
// Stop the plugin // Stop the plugin
plugin.stop(); System.out.println("Stopping plugin");
if(plugin != null) plugin.stop();
System.out.println("Stopped plugin");
}
} }
private static class Callback implements DuplexPluginCallback { private static class Callback implements DuplexPluginCallback {