Accept connections on the local socket.

This commit is contained in:
akwizgran
2011-10-06 12:00:44 +01:00
parent 1ee765a052
commit 5bc9baff95
2 changed files with 97 additions and 32 deletions

View File

@@ -1,5 +1,8 @@
package net.sf.briar.plugins.socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@@ -30,21 +33,34 @@ public class SimpleSocketPluginTest extends TestCase {
}
@Test
public void testBind() throws Exception {
public void testBindAndAccept() throws Exception {
StubCallback callback = new StubCallback();
localProperties.put("host", "127.0.0.1");
localProperties.put("port", "0");
SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), 10);
plugin.start(localProperties, remoteProperties, config, callback);
// The plugin should have bound a socket and stored the port number
assertNotNull(callback.localProperties);
String host = callback.localProperties.get("host");
assertNotNull(host);
assertEquals("127.0.0.1", host);
String port = callback.localProperties.get("port");
assertNotNull(port);
assertTrue(Integer.valueOf(port) > 0 && Integer.valueOf(port) < 65536);
String portString = callback.localProperties.get("port");
assertNotNull(portString);
int port = Integer.valueOf(portString);
assertTrue(port > 0 && port < 65536);
// The plugin should be listening on the port
InetSocketAddress addr = new InetSocketAddress(host, port);
Socket s = new Socket();
s.connect(addr, 100);
s.close();
// Stop the plugin
plugin.stop();
// The plugin should no longer be listening
try {
s.connect(addr, 100);
fail();
} catch(IOException expected) {}
}
private static class ImmediateExecutor implements Executor {