Search in sources :

Example 1 with NativeTcp

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();
}
Also used : Scanner(java.util.Scanner) EvoSuiteLocalAddress(org.evosuite.runtime.testdata.EvoSuiteLocalAddress) NativeTcp(org.evosuite.runtime.vnet.NativeTcp) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 2 with NativeTcp

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());
    }
}
Also used : NativeTcp(org.evosuite.runtime.vnet.NativeTcp) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Aggregations

NativeTcp (org.evosuite.runtime.vnet.NativeTcp)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Scanner (java.util.Scanner)1 EvoSuiteLocalAddress (org.evosuite.runtime.testdata.EvoSuiteLocalAddress)1 Test (org.junit.Test)1