Factored out non-Bluetooth-specific code.

This commit is contained in:
akwizgran
2011-10-29 09:47:19 +01:00
parent 4a9d700e61
commit 70c7a9c077
6 changed files with 296 additions and 240 deletions

View File

@@ -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) {}
}
}

View File

@@ -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) {}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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) {}
}
}

View File

@@ -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) {}
}
}

View File

@@ -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";
}