Invitation API and two (untested) implementations.

This commit is contained in:
akwizgran
2011-10-27 17:52:03 +01:00
parent 59485fe392
commit 48d638746c
26 changed files with 834 additions and 251 deletions

View File

@@ -76,7 +76,8 @@ public class LockFairnessTest extends TestCase {
try {
Thread.sleep(sleepTime);
finished.add(this);
} catch(InterruptedException ignored) {
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
@@ -99,7 +100,8 @@ public class LockFairnessTest extends TestCase {
try {
Thread.sleep(sleepTime);
finished.add(this);
} catch(InterruptedException ignored) {
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}

View File

@@ -737,9 +737,7 @@ public class H2DatabaseTest extends TestCase {
for(int i = 0; i < ids.length; i++) {
db.addOutstandingBatch(txn, contactId, ids[i],
Collections.<MessageId>emptySet());
try {
Thread.sleep(5);
} catch(InterruptedException ignored) {}
Thread.sleep(5);
}
// The contact acks the batches in reverse order. The first
@@ -779,9 +777,7 @@ public class H2DatabaseTest extends TestCase {
for(int i = 0; i < ids.length; i++) {
db.addOutstandingBatch(txn, contactId, ids[i],
Collections.<MessageId>emptySet());
try {
Thread.sleep(5);
} catch(InterruptedException ignored) {}
Thread.sleep(5);
}
// The contact acks the batches in the order they were sent - nothing
@@ -946,9 +942,7 @@ public class H2DatabaseTest extends TestCase {
};
t.start();
// Do whatever the transaction needs to do
try {
Thread.sleep(10);
} catch(InterruptedException ignored) {}
Thread.sleep(10);
transactionFinished.set(true);
// Commit the transaction
db.commitTransaction(txn);
@@ -981,9 +975,7 @@ public class H2DatabaseTest extends TestCase {
};
t.start();
// Do whatever the transaction needs to do
try {
Thread.sleep(10);
} catch(InterruptedException ignored) {}
Thread.sleep(10);
transactionFinished.set(true);
// Abort the transaction
db.abortTransaction(txn);

View File

@@ -1,63 +1,79 @@
package net.sf.briar.plugins.bluetooth;
import java.io.PrintStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
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.ImmediateExecutor;
// This is not a JUnit test - it has to be run manually while the server test
// is running on another machine
public class BluetoothClientTest {
public class BluetoothClientTest extends BluetoothTest {
public static final String RESPONSE = "Carrots!";
private final String serverAddress;
BluetoothClientTest(String serverAddress) {
this.serverAddress = serverAddress;
}
void run() throws IOException {
ContactId contactId = new ContactId(0);
ClientCallback callback = new ClientCallback();
// 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);
// Create the plugin
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();
}
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.err.println("Please specify the server's Bluetooth address");
System.exit(1);
}
ContactId contactId = new ContactId(0);
ClientCallback callback = new ClientCallback();
// Store the server's Bluetooth address and UUID
TransportProperties p = new TransportProperties();
p.put("address", args[0]);
p.put("uuid", BluetoothServerTest.UUID);
callback.remote.put(contactId, p);
// Create the plugin
BluetoothPlugin plugin =
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
// Start the plugin
System.out.println("Starting plugin");
plugin.start();
// Try to connect to the server
System.out.println("Creating connection");
StreamTransportConnection conn = plugin.createConnection(contactId);
if(conn == null) {
System.out.println("Connection failed");
} else {
System.out.println("Connection created");
Scanner in = new Scanner(conn.getInputStream());
String challenge = in.nextLine();
System.out.println("Received challenge: " + challenge);
if(BluetoothServerTest.CHALLENGE.equals(challenge)) {
PrintStream out = new PrintStream(conn.getOutputStream());
out.println(RESPONSE);
System.out.println("Sent response: " + RESPONSE);
} else {
System.out.println("Incorrect challenge");
}
conn.dispose(true);
}
// Stop the plugin
System.out.println("Stopping plugin");
plugin.stop();
new BluetoothClientTest(args[0]).run();
}
private static class ClientCallback implements StreamPluginCallback {

View File

@@ -1,32 +1,27 @@
package net.sf.briar.plugins.bluetooth;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
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.ImmediateExecutor;
//This is not a JUnit test - it has to be run manually while the server test
//is running on another machine
public class BluetoothServerTest {
public class BluetoothServerTest extends BluetoothTest {
public static final String UUID = "CABBA6E5CABBA6E5CABBA6E5CABBA6E5";
public static final String CHALLENGE = "Potatoes!";
public static void main(String[] args) throws Exception {
void run() throws Exception {
ServerCallback callback = new ServerCallback();
// Store the UUID
callback.config.put("uuid", UUID);
// Create the plugin
BluetoothPlugin plugin =
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
Executor e = Executors.newCachedThreadPool();
BluetoothPlugin plugin = new BluetoothPlugin(e, callback, 0L);
// Start the plugin
System.out.println("Starting plugin");
plugin.start();
@@ -35,12 +30,35 @@ public class BluetoothServerTest {
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();
}
private static class ServerCallback implements StreamPluginCallback {
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();
@@ -77,24 +95,9 @@ public class BluetoothServerTest {
public void showMessage(String... message) {}
public void incomingConnectionCreated(StreamTransportConnection conn) {
public void incomingConnectionCreated(StreamTransportConnection s) {
System.out.println("Connection received");
try {
PrintStream out = new PrintStream(conn.getOutputStream());
out.println(CHALLENGE);
System.out.println("Sent challenge: " + CHALLENGE);
Scanner in = new Scanner(conn.getInputStream());
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");
}
conn.dispose(true);
} catch(IOException e) {
e.printStackTrace();
}
sendChallengeAndReceiveResponse(s);
synchronized(this) {
notifyAll();
}

View File

@@ -0,0 +1,54 @@
package net.sf.briar.plugins.bluetooth;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
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());
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");
}
s.dispose(true);
} catch(IOException e) {
e.printStackTrace();
s.dispose(false);
}
}
void receiveChallengeAndSendResponse(StreamTransportConnection s) {
try {
Scanner in = new Scanner(s.getInputStream());
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");
}
s.dispose(true);
} catch(IOException e) {
e.printStackTrace();
s.dispose(false);
}
}
}

View File

@@ -339,35 +339,6 @@ public class RemovableDrivePluginTest extends TestCase {
context.assertIsSatisfied();
}
@Test
public void testSmallFileIsIgnored() throws Exception {
Mockery context = new Mockery();
final BatchPluginCallback callback =
context.mock(BatchPluginCallback.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), callback, finder, monitor);
plugin.start();
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
out.write(new byte[TransportConstants.MIN_CONNECTION_LENGTH - 1]);
out.flush();
out.close();
assertEquals(TransportConstants.MIN_CONNECTION_LENGTH - 1, f.length());
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@Test
public void testReaderIsCreated() throws Exception {
Mockery context = new Mockery();

View File

@@ -27,13 +27,13 @@ public class SimpleSocketPluginTest extends TestCase {
@Test
public void testIncomingConnection() throws Exception {
StreamCallback callback = new StreamCallback();
callback.local.put("host", "127.0.0.1");
callback.local.put("internal", "127.0.0.1");
callback.local.put("port", "0");
SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
plugin.start();
// The plugin should have bound a socket and stored the port number
String host = callback.local.get("host");
String host = callback.local.get("internal");
assertNotNull(host);
assertEquals("127.0.0.1", host);
String portString = callback.local.get("port");
@@ -84,7 +84,7 @@ public class SimpleSocketPluginTest extends TestCase {
}.start();
// Tell the plugin about the port
TransportProperties p = new TransportProperties();
p.put("host", "127.0.0.1");
p.put("internal", "127.0.0.1");
p.put("port", String.valueOf(port));
callback.remote.put(contactId, p);
// Connect to the port