Search in sources :

Example 1 with EvoSuiteLocalAddress

use of org.evosuite.runtime.testdata.EvoSuiteLocalAddress in project evosuite by EvoSuite.

the class UdpTest method testReceivePacket.

@Test
public void testReceivePacket() throws Exception {
    int port = 12345;
    String host = "127.0.0.1";
    MockDatagramSocket socket = new MockDatagramSocket(port, InetAddress.getByName(host));
    byte[] data = new byte[] { 42 };
    NetworkHandling.sendUdpPacket(new EvoSuiteLocalAddress(host, port), data);
    DatagramPacket p = new DatagramPacket(new byte[1], 1);
    socket.receive(p);
    byte[] received = p.getData();
    Assert.assertNotNull(received);
    Assert.assertEquals(42, received[0]);
}
Also used : EvoSuiteLocalAddress(org.evosuite.runtime.testdata.EvoSuiteLocalAddress) DatagramPacket(java.net.DatagramPacket) Test(org.junit.Test)

Example 2 with EvoSuiteLocalAddress

use of org.evosuite.runtime.testdata.EvoSuiteLocalAddress in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(LocalAddressPrimitiveStatement s, Scope scope) {
    EvoSuiteLocalAddress conc_local_address = s.getValue();
    VariableReference varRef = s.getReturnValue();
    String varRefName = varRef.getName();
    ReferenceExpression addressRef;
    if (conc_local_address == null) {
        addressRef = env.heap.getReference(null);
        if (addressRef.getConcreteValue() != null) {
            throw new IllegalStateException("Expected null concrete object");
        }
    } else {
        addressRef = env.heap.getReference(conc_local_address);
        if (addressRef.getConcreteValue() == null) {
            throw new IllegalStateException("Expected non-null concrete object");
        }
    }
    symb_references.put(varRefName, addressRef);
}
Also used : EvoSuiteLocalAddress(org.evosuite.runtime.testdata.EvoSuiteLocalAddress) VariableReference(org.evosuite.testcase.variable.VariableReference) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression)

Example 3 with EvoSuiteLocalAddress

use of org.evosuite.runtime.testdata.EvoSuiteLocalAddress in project evosuite by EvoSuite.

the class LocalAddressPrimitiveStatement method randomize.

@Override
public void randomize() {
    EvoSuiteLocalAddress addr;
    if (!tc.getAccessedEnvironment().getViewOfLocalListeningPorts().isEmpty()) {
        EndPointInfo info = Randomness.choice(tc.getAccessedEnvironment().getViewOfLocalListeningPorts());
        String host = info.getHost();
        int port = info.getPort();
        addr = new EvoSuiteLocalAddress(host, port);
    } else {
        /*
                no point in creating local addresses that the SUT has
                never accessed
             */
        addr = null;
    }
    setValue(addr);
}
Also used : EvoSuiteLocalAddress(org.evosuite.runtime.testdata.EvoSuiteLocalAddress) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo)

Example 4 with EvoSuiteLocalAddress

use of org.evosuite.runtime.testdata.EvoSuiteLocalAddress 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)

Aggregations

EvoSuiteLocalAddress (org.evosuite.runtime.testdata.EvoSuiteLocalAddress)4 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 DatagramPacket (java.net.DatagramPacket)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Scanner (java.util.Scanner)1 EndPointInfo (org.evosuite.runtime.vnet.EndPointInfo)1 NativeTcp (org.evosuite.runtime.vnet.NativeTcp)1 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1