mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Outgoing connection test.
This commit is contained in:
@@ -2,11 +2,15 @@ package net.sf.briar.plugins.socket;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
@@ -33,7 +37,7 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindAndAccept() throws Exception {
|
public void testIncomingConnection() throws Exception {
|
||||||
StubCallback callback = new StubCallback();
|
StubCallback callback = new StubCallback();
|
||||||
localProperties.put("host", "127.0.0.1");
|
localProperties.put("host", "127.0.0.1");
|
||||||
localProperties.put("port", "0");
|
localProperties.put("port", "0");
|
||||||
@@ -66,6 +70,46 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
} catch(IOException expected) {}
|
} catch(IOException expected) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOutgoingConnection() throws Exception {
|
||||||
|
StubCallback callback = new StubCallback();
|
||||||
|
SimpleSocketPlugin plugin =
|
||||||
|
new SimpleSocketPlugin(new ImmediateExecutor(), 10);
|
||||||
|
plugin.start(localProperties, remoteProperties, config, callback);
|
||||||
|
// Listen on a local port
|
||||||
|
final ServerSocket ss = new ServerSocket();
|
||||||
|
ss.bind(new InetSocketAddress("127.0.0.1", 0), 10);
|
||||||
|
int port = ss.getLocalPort();
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
final AtomicBoolean error = new AtomicBoolean(false);
|
||||||
|
new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
ss.accept();
|
||||||
|
latch.countDown();
|
||||||
|
} catch(IOException e) {
|
||||||
|
error.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
// Tell the plugin about the port
|
||||||
|
Map<String, String> properties = new TreeMap<String, String>();
|
||||||
|
properties.put("host", "127.0.0.1");
|
||||||
|
properties.put("port", String.valueOf(port));
|
||||||
|
plugin.setRemoteProperties(contactId, properties);
|
||||||
|
// Connect to the port
|
||||||
|
StreamTransportConnection conn = plugin.createConnection(contactId);
|
||||||
|
assertNotNull(conn);
|
||||||
|
// Check that the connection was accepted
|
||||||
|
assertTrue(latch.await(1, TimeUnit.SECONDS));
|
||||||
|
assertFalse(error.get());
|
||||||
|
// Clean up
|
||||||
|
conn.getInputStream().close(); // FIXME: Change the API
|
||||||
|
ss.close();
|
||||||
|
plugin.stop();
|
||||||
|
}
|
||||||
|
|
||||||
private static class ImmediateExecutor implements Executor {
|
private static class ImmediateExecutor implements Executor {
|
||||||
|
|
||||||
public void execute(Runnable r) {
|
public void execute(Runnable r) {
|
||||||
|
|||||||
Reference in New Issue
Block a user