### Question:
LocalControlConsole implements HostControlConsole { @Override public Destroyable openSocket(SocketHandler handler) { ensureRunning(); ServerSocket sock; try { sock = new ServerSocket(); sock.setReuseAddress(true); sock.bind(new InetSocketAddress("127.0.0.1", 0), 1); } catch (SocketException e) { handler.terminated(e.toString()); return new DestroyableStub(); } catch (IOException e) { handler.terminated(e.toString()); return new DestroyableStub(); } SockerAcceptor accpector = new SockerAcceptor(sock, handler); Thread thread = new Thread(accpector); thread.setDaemon(false); thread.setName("ACCEPT[" + sock.getLocalSocketAddress() + "]"); thread.start(); return accpector; } LocalControlConsole();  LocalControlConsole(String tmpDir); @Override boolean isLocalFileSystem(); @Override String cacheFile(FileBlob blob); @Override List<String> cacheFiles(List<? extends FileBlob> blobs); @Override Destroyable openSocket(SocketHandler handler); @Override Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler); @Override void terminate(); File getCacheDir();  }### Answer:
@Test public void verify_tunneled_connection() throws IOException, InterruptedException, ExecutionException { final FutureBox<SocketAddress> bindAddress = new FutureBox<SocketAddress>(); final FutureBox<SocketAddress> clientAddress = new FutureBox<SocketAddress>(); SocketHandler sockHandler = new SocketHandler() { @Override public void bound(String host, int port) { System.out.println("Bound: " + host + ":" + port); bindAddress.setData(new InetSocketAddress(host, port)); } @Override public void accepted(String remoteHost, int remotePort, final InputStream soIn, final OutputStream soOut) { System.out.println("Connected: " + remoteHost + ":" + remotePort); clientAddress.setData(new InetSocketAddress(remoteHost, remotePort)); new Thread() { @Override public void run() { try { soOut.write("Ping".getBytes()); soOut.flush(); soOut.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } @Override public void terminated(String message) { System.out.println(message); } }; console.openSocket(sockHandler); Socket sock = new Socket(); sock.connect(bindAddress.get()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamHelper.copy(sock.getInputStream(), bos); assertEquals(((InetSocketAddress)clientAddress.get()).getPort(), sock.getLocalPort()); assertEquals("Ping", new String(bos.toByteArray())); }
@Test public void verify_multi_bind_tunneled_connection() throws IOException, InterruptedException, ExecutionException { final FutureBox<SocketAddress> bindAddress = new FutureBox<SocketAddress>(); SocketHandler sockHandler = new SocketHandler() { @Override public void bound(String host, int port) { System.out.println("Bound: " + host + ":" + port); bindAddress.setData(new InetSocketAddress(host, port)); } @Override public void accepted(String remoteHost, int remotePort, final InputStream soIn, final OutputStream soOut) { System.out.println("Connected: " + remoteHost + ":" + remotePort); new Thread() { @Override public void run() { try { soOut.write("Ping".getBytes()); soOut.flush(); soOut.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } @Override public void terminated(String message) { System.out.println(message); } }; console.openSocket(sockHandler); System.out.println("Connecting first time"); Socket sock = new Socket(); sock.connect(bindAddress.get()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamHelper.copy(sock.getInputStream(), bos); assertEquals("Ping", new String(bos.toByteArray())); System.out.println("Connecting second time"); sock = new Socket(); sock.connect(bindAddress.get()); bos = new ByteArrayOutputStream(); StreamHelper.copy(sock.getInputStream(), bos); assertEquals("Ping", new String(bos.toByteArray())); }