Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {
public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
try {
Socks5BytestreamSession session = request.accept();
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
InputStream inputStream = session.getInputStream();
byte[] receivedData = new byte[3];
inputStream.read(receivedData);
queue.put(receivedData);
session.close();
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
assertTrue(session.isMediated());
// verify stream
final byte[] receivedData = new byte[3];
final InputStream inputStream = session.getInputStream();
FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() throws Exception {
return inputStream.read(receivedData);
}
});
Thread executor = new Thread(futureTask);
executor.start();
try {
futureTask.get(2000, TimeUnit.MILLISECONDS);
}
catch (TimeoutException e) {
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.getSocks5Proxy().start();
fail("Couldn't send data from target to inititator");
}
assertEquals("sent data not equal to received data", data, receivedData);
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
session.close();
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.getSocks5Proxy().start();