From 70c7a9c077995cf7a4da433adc856be1a6946e6f Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Sat, 29 Oct 2011 09:47:19 +0100 Subject: [PATCH] Factored out non-Bluetooth-specific code. --- .../sf/briar/plugins/StreamClientTest.java | 99 +++++++++++++++++ .../sf/briar/plugins/StreamServerTest.java | 105 ++++++++++++++++++ test/net/sf/briar/plugins/StreamTest.java | 71 ++++++++++++ .../bluetooth/BluetoothClientTest.java | 103 ++--------------- .../bluetooth/BluetoothServerTest.java | 98 ++-------------- .../plugins/bluetooth/BluetoothTest.java | 60 +--------- 6 files changed, 296 insertions(+), 240 deletions(-) create mode 100644 test/net/sf/briar/plugins/StreamClientTest.java create mode 100644 test/net/sf/briar/plugins/StreamServerTest.java create mode 100644 test/net/sf/briar/plugins/StreamTest.java diff --git a/test/net/sf/briar/plugins/StreamClientTest.java b/test/net/sf/briar/plugins/StreamClientTest.java new file mode 100644 index 0000000000..d1ddbe0fea --- /dev/null +++ b/test/net/sf/briar/plugins/StreamClientTest.java @@ -0,0 +1,99 @@ +package net.sf.briar.plugins; + +import java.io.IOException; +import java.util.Map; + +import net.sf.briar.api.ContactId; +import net.sf.briar.api.TransportConfig; +import net.sf.briar.api.TransportProperties; +import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.transport.StreamTransportConnection; + +public abstract class StreamClientTest extends StreamTest { + + protected void run() throws IOException { + assert plugin != null; + // Start the plugin + System.out.println("Starting plugin"); + plugin.start(); + // Try to connect to the server + System.out.println("Creating connection"); + StreamTransportConnection s = plugin.createConnection(contactId); + if(s == null) { + System.out.println("Connection failed"); + } else { + System.out.println("Connection created"); + receiveChallengeSendResponse(s); + } + // Try to send an invitation + System.out.println("Sending invitation"); + s = plugin.sendInvitation(123, INVITATION_TIMEOUT); + if(s == null) { + System.out.println("Connection failed"); + } else { + System.out.println("Connection created"); + receiveChallengeSendResponse(s); + } + // Try to accept an invitation + System.out.println("Accepting invitation"); + s = plugin.acceptInvitation(456, INVITATION_TIMEOUT); + if(s == null) { + System.out.println("Connection failed"); + } else { + System.out.println("Connection created"); + sendChallengeReceiveResponse(s); + } + // Stop the plugin + System.out.println("Stopping plugin"); + plugin.stop(); + } + + protected static class ClientCallback implements StreamPluginCallback { + + private TransportConfig config = null; + private TransportProperties local = null; + private Map<ContactId, TransportProperties> remote = null; + + public ClientCallback(TransportConfig config, TransportProperties local, + Map<ContactId, TransportProperties> remote) { + this.config = config; + this.local = local; + this.remote = remote; + } + + public TransportConfig getConfig() { + return config; + } + + public TransportProperties getLocalProperties() { + return local; + } + + public Map<ContactId, TransportProperties> getRemoteProperties() { + return remote; + } + + public void setConfig(TransportConfig c) { + config = c; + } + + public void setLocalProperties(TransportProperties p) { + local = p; + } + + public int showChoice(String[] options, String... message) { + return -1; + } + + public boolean showConfirmationMessage(String... message) { + return false; + } + + public void showMessage(String... message) {} + + public void incomingConnectionCreated(StreamTransportConnection c) {} + + public void outgoingConnectionCreated(ContactId contactId, + StreamTransportConnection c) {} + } +} diff --git a/test/net/sf/briar/plugins/StreamServerTest.java b/test/net/sf/briar/plugins/StreamServerTest.java new file mode 100644 index 0000000000..cfd5a22106 --- /dev/null +++ b/test/net/sf/briar/plugins/StreamServerTest.java @@ -0,0 +1,105 @@ +package net.sf.briar.plugins; + +import java.util.Map; + +import net.sf.briar.api.ContactId; +import net.sf.briar.api.TransportConfig; +import net.sf.briar.api.TransportProperties; +import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.transport.StreamTransportConnection; + +public abstract class StreamServerTest extends StreamTest { + + protected void run() throws Exception { + assert callback != null; + assert plugin != null; + // Start the plugin + System.out.println("Starting plugin"); + plugin.start(); + // Print the local transport properties + System.out.println("Local transport properties:"); + System.out.println(callback.getLocalProperties()); + // Wait for a connection + System.out.println("Waiting for connection"); + synchronized(callback) { + callback.wait(); + } + // Try to accept an invitation + System.out.println("Accepting invitation"); + StreamTransportConnection s = plugin.acceptInvitation(123, + INVITATION_TIMEOUT); + if(s == null) { + System.out.println("Connection failed"); + } else { + System.out.println("Connection created"); + sendChallengeReceiveResponse(s); + } + // Try to send an invitation + System.out.println("Sending invitation"); + s = plugin.sendInvitation(456, INVITATION_TIMEOUT); + if(s == null) { + System.out.println("Connection failed"); + } else { + System.out.println("Connection created"); + receiveChallengeSendResponse(s); + } + // Stop the plugin + System.out.println("Stopping plugin"); + plugin.stop(); + } + + protected class ServerCallback implements StreamPluginCallback { + + private TransportConfig config; + private TransportProperties local; + private Map<ContactId, TransportProperties> remote; + + public ServerCallback(TransportConfig config, TransportProperties local, + Map<ContactId, TransportProperties> remote) { + this.config = config; + this.local = local; + this.remote = remote; + } + + public TransportConfig getConfig() { + return config; + } + + public TransportProperties getLocalProperties() { + return local; + } + + public Map<ContactId, TransportProperties> getRemoteProperties() { + return remote; + } + + public void setConfig(TransportConfig c) { + config = c; + } + + public void setLocalProperties(TransportProperties p) { + local = p; + } + + public int showChoice(String[] options, String... message) { + return -1; + } + + public boolean showConfirmationMessage(String... message) { + return false; + } + + public void showMessage(String... message) {} + + public void incomingConnectionCreated(StreamTransportConnection s) { + System.out.println("Connection received"); + sendChallengeReceiveResponse(s); + synchronized(this) { + notifyAll(); + } + } + + public void outgoingConnectionCreated(ContactId contactId, + StreamTransportConnection c) {} + } +} diff --git a/test/net/sf/briar/plugins/StreamTest.java b/test/net/sf/briar/plugins/StreamTest.java new file mode 100644 index 0000000000..abf4c5d342 --- /dev/null +++ b/test/net/sf/briar/plugins/StreamTest.java @@ -0,0 +1,71 @@ +package net.sf.briar.plugins; + +import java.io.IOException; +import java.io.PrintStream; +import java.util.Scanner; + +import net.sf.briar.api.ContactId; +import net.sf.briar.api.plugins.StreamPlugin; +import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.transport.StreamTransportConnection; + +abstract class StreamTest { + + protected static final String CHALLENGE = "Carrots!"; + protected static final String RESPONSE = "Potatoes!"; + protected static final long INVITATION_TIMEOUT = 30 * 1000; + + protected final ContactId contactId = new ContactId(0); + + protected StreamPluginCallback callback = null; + protected StreamPlugin plugin = null; + + protected void sendChallengeReceiveResponse(StreamTransportConnection s) { + assert plugin != null; + try { + PrintStream out = new PrintStream(s.getOutputStream()); + out.println(CHALLENGE); + System.out.println("Sent challenge: " + CHALLENGE); + Scanner in = new Scanner(s.getInputStream()); + if(in.hasNextLine()) { + String response = in.nextLine(); + System.out.println("Received response: " + response); + if(RESPONSE.equals(response)) { + System.out.println("Correct response"); + } else { + System.out.println("Incorrect response"); + } + } else { + System.out.println("No response"); + } + s.dispose(true); + } catch(IOException e) { + e.printStackTrace(); + s.dispose(false); + } + } + + protected void receiveChallengeSendResponse(StreamTransportConnection s) { + assert plugin != null; + try { + Scanner in = new Scanner(s.getInputStream()); + if(in.hasNextLine()) { + String challenge = in.nextLine(); + System.out.println("Received challenge: " + challenge); + if(CHALLENGE.equals(challenge)) { + PrintStream out = new PrintStream(s.getOutputStream()); + out.println(RESPONSE); + System.out.println("Sent response: " + RESPONSE); + } else { + System.out.println("Incorrect challenge"); + } + } else { + System.out.println("No challenge"); + } + s.dispose(true); + } catch(IOException e) { + e.printStackTrace(); + s.dispose(false); + } + } +} diff --git a/test/net/sf/briar/plugins/bluetooth/BluetoothClientTest.java b/test/net/sf/briar/plugins/bluetooth/BluetoothClientTest.java index e53cb47077..19f829756e 100644 --- a/test/net/sf/briar/plugins/bluetooth/BluetoothClientTest.java +++ b/test/net/sf/briar/plugins/bluetooth/BluetoothClientTest.java @@ -1,7 +1,6 @@ package net.sf.briar.plugins.bluetooth; -import java.io.IOException; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -9,63 +8,24 @@ import java.util.concurrent.Executors; import net.sf.briar.api.ContactId; import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportProperties; -import net.sf.briar.api.plugins.StreamPluginCallback; -import net.sf.briar.api.transport.StreamTransportConnection; +import net.sf.briar.plugins.StreamClientTest; // This is not a JUnit test - it has to be run manually while the server test // is running on another machine -public class BluetoothClientTest extends BluetoothTest { +public class BluetoothClientTest extends StreamClientTest { - private final String serverAddress; - - BluetoothClientTest(String serverAddress) { - this.serverAddress = serverAddress; - } - - void run() throws IOException { - ContactId contactId = new ContactId(0); - ClientCallback callback = new ClientCallback(); + private BluetoothClientTest(String serverAddress) { // Store the server's Bluetooth address and UUID TransportProperties p = new TransportProperties(); p.put("address", serverAddress); - p.put("uuid", BluetoothServerTest.UUID); - callback.remote.put(contactId, p); + p.put("uuid", BluetoothTest.UUID); + Map<ContactId, TransportProperties> remote = + Collections.singletonMap(contactId, p); // Create the plugin + callback = new ClientCallback(new TransportConfig(), + new TransportProperties(), remote); Executor e = Executors.newCachedThreadPool(); - BluetoothPlugin plugin = new BluetoothPlugin(e, callback, 0L); - // Start the plugin - System.out.println("Starting plugin"); - plugin.start(); - // Try to connect to the server - System.out.println("Creating connection"); - StreamTransportConnection s = plugin.createConnection(contactId); - if(s == null) { - System.out.println("Connection failed"); - } else { - System.out.println("Connection created"); - receiveChallengeAndSendResponse(s); - } - // Try to send an invitation - System.out.println("Sending invitation"); - s = plugin.sendInvitation(123, INVITATION_TIMEOUT); - if(s == null) { - System.out.println("Connection failed"); - } else { - System.out.println("Connection created"); - receiveChallengeAndSendResponse(s); - } - // Try to accept an invitation - System.out.println("Accepting invitation"); - s = plugin.acceptInvitation(456, INVITATION_TIMEOUT); - if(s == null) { - System.out.println("Connection failed"); - } else { - System.out.println("Connection created"); - sendChallengeAndReceiveResponse(s); - } - // Stop the plugin - System.out.println("Stopping plugin"); - plugin.stop(); + plugin = new BluetoothPlugin(e, callback, 0L); } public static void main(String[] args) throws Exception { @@ -75,47 +35,4 @@ public class BluetoothClientTest extends BluetoothTest { } new BluetoothClientTest(args[0]).run(); } - - private static class ClientCallback implements StreamPluginCallback { - - private TransportConfig config = new TransportConfig(); - private TransportProperties local = new TransportProperties(); - private Map<ContactId, TransportProperties> remote = - new HashMap<ContactId, TransportProperties>(); - - public TransportConfig getConfig() { - return config; - } - - public TransportProperties getLocalProperties() { - return local; - } - - public Map<ContactId, TransportProperties> getRemoteProperties() { - return remote; - } - - public void setConfig(TransportConfig c) { - config = c; - } - - public void setLocalProperties(TransportProperties p) { - local = p; - } - - public int showChoice(String[] options, String... message) { - return -1; - } - - public boolean showConfirmationMessage(String... message) { - return false; - } - - public void showMessage(String... message) {} - - public void incomingConnectionCreated(StreamTransportConnection c) {} - - public void outgoingConnectionCreated(ContactId contactId, - StreamTransportConnection c) {} - } } diff --git a/test/net/sf/briar/plugins/bluetooth/BluetoothServerTest.java b/test/net/sf/briar/plugins/bluetooth/BluetoothServerTest.java index 571de2dd3c..029703cba9 100644 --- a/test/net/sf/briar/plugins/bluetooth/BluetoothServerTest.java +++ b/test/net/sf/briar/plugins/bluetooth/BluetoothServerTest.java @@ -1,109 +1,29 @@ package net.sf.briar.plugins.bluetooth; -import java.util.HashMap; -import java.util.Map; +import java.util.Collections; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import net.sf.briar.api.ContactId; import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportProperties; -import net.sf.briar.api.plugins.StreamPluginCallback; -import net.sf.briar.api.transport.StreamTransportConnection; +import net.sf.briar.plugins.StreamServerTest; //This is not a JUnit test - it has to be run manually while the server test //is running on another machine -public class BluetoothServerTest extends BluetoothTest { +public class BluetoothServerTest extends StreamServerTest { - void run() throws Exception { - ServerCallback callback = new ServerCallback(); + private BluetoothServerTest() { // Store the UUID - callback.local.put("uuid", UUID); + TransportProperties local = new TransportProperties(); + local.put("uuid", BluetoothTest.UUID); // Create the plugin + callback = new ServerCallback(new TransportConfig(), local, + Collections.singletonMap(contactId, new TransportProperties())); Executor e = Executors.newCachedThreadPool(); - BluetoothPlugin plugin = new BluetoothPlugin(e, callback, 0L); - // Start the plugin - System.out.println("Starting plugin"); - plugin.start(); - // Wait for a connection - System.out.println("Waiting for connection"); - synchronized(callback) { - callback.wait(); - } - // Try to accept an invitation - System.out.println("Accepting invitation"); - StreamTransportConnection s = plugin.acceptInvitation(123, - INVITATION_TIMEOUT); - if(s == null) { - System.out.println("Connection failed"); - } else { - System.out.println("Connection created"); - sendChallengeAndReceiveResponse(s); - } - // Try to send an invitation - System.out.println("Sending invitation"); - s = plugin.sendInvitation(456, INVITATION_TIMEOUT); - if(s == null) { - System.out.println("Connection failed"); - } else { - System.out.println("Connection created"); - receiveChallengeAndSendResponse(s); - } - // Stop the plugin - System.out.println("Stopping plugin"); - plugin.stop(); + plugin = new BluetoothPlugin(e, callback, 0L); } public static void main(String[] args) throws Exception { new BluetoothServerTest().run(); } - - private class ServerCallback implements StreamPluginCallback { - - private TransportConfig config = new TransportConfig(); - private TransportProperties local = new TransportProperties(); - private Map<ContactId, TransportProperties> remote = - new HashMap<ContactId, TransportProperties>(); - - public TransportConfig getConfig() { - return config; - } - - public TransportProperties getLocalProperties() { - return local; - } - - public Map<ContactId, TransportProperties> getRemoteProperties() { - return remote; - } - - public void setConfig(TransportConfig c) { - config = c; - } - - public void setLocalProperties(TransportProperties p) { - local = p; - } - - public int showChoice(String[] options, String... message) { - return -1; - } - - public boolean showConfirmationMessage(String... message) { - return false; - } - - public void showMessage(String... message) {} - - public void incomingConnectionCreated(StreamTransportConnection s) { - System.out.println("Connection received"); - sendChallengeAndReceiveResponse(s); - synchronized(this) { - notifyAll(); - } - } - - public void outgoingConnectionCreated(ContactId contactId, - StreamTransportConnection c) {} - } } diff --git a/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java b/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java index 8b24fb1a01..b67c4b3c4f 100644 --- a/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java +++ b/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java @@ -1,62 +1,6 @@ package net.sf.briar.plugins.bluetooth; -import java.io.IOException; -import java.io.PrintStream; -import java.util.Scanner; +interface BluetoothTest { -import net.sf.briar.api.transport.StreamTransportConnection; - -abstract class BluetoothTest { - - protected static final String UUID = "CABBA6E5CABBA6E5CABBA6E5CABBA6E5"; - protected static final String CHALLENGE = "Carrots!"; - protected static final String RESPONSE = "Potatoes!"; - protected static final long INVITATION_TIMEOUT = 30 * 1000; - - void sendChallengeAndReceiveResponse(StreamTransportConnection s) { - try { - PrintStream out = new PrintStream(s.getOutputStream()); - out.println(CHALLENGE); - System.out.println("Sent challenge: " + CHALLENGE); - Scanner in = new Scanner(s.getInputStream()); - if(in.hasNextLine()) { - String response = in.nextLine(); - System.out.println("Received response: " + response); - if(BluetoothClientTest.RESPONSE.equals(response)) { - System.out.println("Correct response"); - } else { - System.out.println("Incorrect response"); - } - } else { - System.out.println("No response"); - } - s.dispose(true); - } catch(IOException e) { - e.printStackTrace(); - s.dispose(false); - } - } - - void receiveChallengeAndSendResponse(StreamTransportConnection s) { - try { - Scanner in = new Scanner(s.getInputStream()); - if(in.hasNextLine()) { - String challenge = in.nextLine(); - System.out.println("Received challenge: " + challenge); - if(BluetoothServerTest.CHALLENGE.equals(challenge)) { - PrintStream out = new PrintStream(s.getOutputStream()); - out.println(RESPONSE); - System.out.println("Sent response: " + RESPONSE); - } else { - System.out.println("Incorrect challenge"); - } - } else { - System.out.println("No challenge"); - } - s.dispose(true); - } catch(IOException e) { - e.printStackTrace(); - s.dispose(false); - } - } + static final String UUID = "CABBA6E5CABBA6E5CABBA6E5CABBA6E5"; } \ No newline at end of file -- GitLab