use of org.evosuite.runtime.vnet.NativeTcp in project evosuite by EvoSuite.
the class ServerSocketTest method testReceiveAndReplyMessage.
@Test
public void testReceiveAndReplyMessage() throws IOException {
int n = VirtualNetwork.getInstance().getViewOfOpenedTcpConnections().size();
Assert.assertEquals(0, n);
// first bind a listening server
MockServerSocket server = new MockServerSocket();
String localAddress = "127.0.0.1";
int localPort = 42;
server.bind(new InetSocketAddress(localAddress, localPort));
n = VirtualNetwork.getInstance().getViewOfOpenedTcpConnections().size();
Assert.assertEquals(0, n);
// send a message on tcp connection, although SUT is not listening yet
String msg = "Hello World! Sent from mocked TCP connection";
EvoSuiteLocalAddress addr = new EvoSuiteLocalAddress(localAddress, localPort);
NetworkHandling.sendMessageOnTcp(addr, msg);
// open listening port, and read message
Socket socket = server.accept();
Assert.assertNotNull(socket);
InputStream in = socket.getInputStream();
Assert.assertNotNull(in);
Scanner inScan = new Scanner(in);
String received = inScan.nextLine();
inScan.close();
Assert.assertEquals(msg, received);
// send a reply to remote host on same TCP connection
String reply = "Reply to Hello Message";
OutputStream out = socket.getOutputStream();
out.write(reply.getBytes());
n = VirtualNetwork.getInstance().getViewOfOpenedTcpConnections().size();
Assert.assertEquals(1, n);
NativeTcp connection = VirtualNetwork.getInstance().getViewOfOpenedTcpConnections().iterator().next();
Assert.assertEquals(reply.length(), connection.getAmountOfDataInRemoteBuffer());
server.close();
}
use of org.evosuite.runtime.vnet.NativeTcp in project evosuite by EvoSuite.
the class EvoSuiteSocket method accept.
@Override
protected void accept(SocketImpl s) throws IOException {
if (!(s instanceof EvoSuiteSocket)) {
throw new IOException("Can only handle mocked sockets");
}
EvoSuiteSocket mock = (EvoSuiteSocket) s;
/*
* If the test case has set up an incoming connection, then
* simulate an immediate connection.
* If not, there is no point in blocking the SUT for
* a connection that will never arrive: just throw an exception
*/
InetAddress localHost = (InetAddress) options.get(SocketOptions.SO_BINDADDR);
NativeTcp tcp = VirtualNetwork.getInstance().pullTcpConnection(localHost.getHostAddress(), localport);
if (tcp == null) {
throw new IOException("Simulated exception on waiting server");
} else {
mock.openedConnection = tcp;
mock.setOption(SocketOptions.SO_BINDADDR, localHost);
mock.setLocalPort(localport);
mock.setRemoteAddress(InetAddress.getByName(tcp.getRemoteEndPoint().getHost()));
mock.setRemotePort(tcp.getRemoteEndPoint().getPort());
}
}
Aggregations