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,75 +27,103 @@ 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();
// Create a plugin instance for the server TorPlugin serverPlugin = null, clientPlugin = null;
Callback serverCallback = new Callback(); try {
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L); // Create a plugin instance for the server
serverPlugin.start(); Callback serverCallback = new Callback();
// The plugin should create a hidden service... eventually serverPlugin = new TorPlugin(e, serverCallback, 0L);
assertTrue(serverCallback.latch.await(10, TimeUnit.MINUTES)); System.out.println("Starting server plugin");
String onion = serverCallback.local.get("onion"); serverPlugin.start();
assertNotNull(onion); // The plugin should create a hidden service... eventually
assertTrue(onion.endsWith(".onion")); assertTrue(serverCallback.latch.await(5, TimeUnit.MINUTES));
// Create another plugin instance for the client System.out.println("Started server plugin");
Callback clientCallback = new Callback(); String onion = serverCallback.local.get("onion");
clientCallback.config.put("noHiddenService", ""); assertNotNull(onion);
TransportProperties p = new TransportProperties(); assertTrue(onion.endsWith(".onion"));
p.put("onion", onion); // Create another plugin instance for the client
clientCallback.remote.put(contactId, p); Callback clientCallback = new Callback();
TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L); clientCallback.config.put("noHiddenService", "");
clientPlugin.start(); TransportProperties p = new TransportProperties();
// The plugin should start without creating a hidden service p.put("onion", onion);
assertTrue(clientCallback.latch.await(10, TimeUnit.MINUTES)); clientCallback.remote.put(contactId, p);
// Connect to the server's hidden service clientPlugin = new TorPlugin(e, clientCallback, 0L);
DuplexTransportConnection clientEnd = System.out.println("Starting client plugin");
clientPlugin.createConnection(contactId); clientPlugin.start();
assertNotNull(clientEnd); // The plugin should start without creating a hidden service
DuplexTransportConnection serverEnd = serverCallback.incomingConnection; assertTrue(clientCallback.latch.await(5, TimeUnit.MINUTES));
assertNotNull(serverEnd); System.out.println("Started client plugin");
// Send some data through the Tor connection // Connect to the server's hidden service
PrintStream out = new PrintStream(clientEnd.getOutputStream()); System.out.println("Connecting to hidden service");
out.println("Hello world"); DuplexTransportConnection clientEnd =
out.flush(); clientPlugin.createConnection(contactId);
Scanner in = new Scanner(serverEnd.getInputStream()); assertNotNull(clientEnd);
assertTrue(in.hasNextLine()); DuplexTransportConnection serverEnd = serverCallback.incomingConnection;
assertEquals("Hello world", in.nextLine()); assertNotNull(serverEnd);
serverEnd.dispose(false, false); System.out.println("Connected to hidden service");
clientEnd.dispose(false, false); // Send some data through the Tor connection
// Stop the plugins PrintStream out = new PrintStream(clientEnd.getOutputStream());
serverPlugin.stop(); out.println("Hello world");
clientPlugin.stop(); out.flush();
Scanner in = new Scanner(serverEnd.getInputStream());
assertTrue(in.hasNextLine());
assertEquals("Hello world", in.nextLine());
serverEnd.dispose(false, false);
clientEnd.dispose(false, false);
} catch(AssertionFailedError e1) {
System.out.println(e);
} finally {
// Stop the plugins
System.out.println("Stopping plugins");
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();
// Start a plugin instance with no private key TorPlugin plugin = null;
Callback callback = new Callback(); try {
TorPlugin plugin = new TorPlugin(e, callback, 0L); // Start a plugin instance with no private key
plugin.start(); Callback callback = new Callback();
// The plugin should create a hidden service... eventually plugin = new TorPlugin(e, callback, 0L);
assertTrue(callback.latch.await(10, TimeUnit.MINUTES)); System.out.println("Starting plugin without private key");
String onion = callback.local.get("onion"); plugin.start();
assertNotNull(onion); // The plugin should create a hidden service... eventually
assertTrue(onion.endsWith(".onion")); assertTrue(callback.latch.await(5, TimeUnit.MINUTES));
// Get the PEM-encoded private key System.out.println("Started plugin");
String privateKey = callback.config.get("privateKey"); String onion = callback.local.get("onion");
assertNotNull(privateKey); assertNotNull(onion);
// Stop the plugin assertTrue(onion.endsWith(".onion"));
plugin.stop(); // Get the PEM-encoded private key
// Start another instance, reusing the private key String privateKey = callback.config.get("privateKey");
callback = new Callback(); assertNotNull(privateKey);
callback.config.put("privateKey", privateKey); // Stop the plugin
plugin = new TorPlugin(e, callback, 0L); System.out.println("Stopping plugin");
plugin.start(); plugin.stop();
// The plugin should create a hidden service... eventually System.out.println("Stopped plugin");
assertTrue(callback.latch.await(10, TimeUnit.MINUTES)); // Start another instance, reusing the private key
// The onion URL should be the same callback = new Callback();
assertEquals(onion, callback.local.get("onion")); callback.config.put("privateKey", privateKey);
// The private key should be the same plugin = new TorPlugin(e, callback, 0L);
assertEquals(privateKey, callback.config.get("privateKey")); System.out.println("Starting plugin with private key");
// Stop the plugin plugin.start();
plugin.stop(); // The plugin should create a hidden service... eventually
assertTrue(callback.latch.await(5, TimeUnit.MINUTES));
System.out.println("Started plugin");
// The onion URL should be the same
assertEquals(onion, callback.local.get("onion"));
// The private key should be the same
assertEquals(privateKey, callback.config.get("privateKey"));
} catch(AssertionFailedError e1) {
System.out.println(e);
} finally {
// Stop the plugin
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 {