Search in sources :

Example 1 with EndPointInfo

use of org.evosuite.runtime.vnet.EndPointInfo in project evosuite by EvoSuite.

the class UdpTest method testSendPacket.

@Test
public void testSendPacket() throws Exception {
    String first = "127.0.42.1";
    String second = "127.0.42.2";
    MockDatagramSocket socket = new MockDatagramSocket(500);
    byte[] data = new byte[0];
    DatagramPacket packet = new DatagramPacket(data, 0, InetAddress.getByName(first), 1234);
    // 1 to first address
    socket.send(packet);
    packet.setAddress(InetAddress.getByName(second));
    packet.setPort(4567);
    // 3 packets to the other
    socket.send(packet);
    socket.send(packet);
    socket.send(packet);
    Map<EndPointInfo, Integer> map = VirtualNetwork.getInstance().getCopyOfSentUDP();
    Assert.assertEquals(2, map.size());
    for (Map.Entry<EndPointInfo, Integer> entry : map.entrySet()) {
        if (entry.getKey().getHost().equals(first)) {
            Assert.assertEquals(1, entry.getValue().intValue());
        } else if (entry.getKey().getHost().equals(second)) {
            Assert.assertEquals(3, entry.getValue().intValue());
        } else {
            Assert.fail();
        }
    }
}
Also used : DatagramPacket(java.net.DatagramPacket) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo) Map(java.util.Map) Test(org.junit.Test)

Example 2 with EndPointInfo

use of org.evosuite.runtime.vnet.EndPointInfo in project evosuite by EvoSuite.

the class EnvironmentTestClusterAugmenter method handleNetwork.

private void handleNetwork(TestCase test) {
    /*
		 * there are several things that are mocked in the network. based on
		 * what the SUT used, we might only need a subset of methods used to
		 * manipulate the mocked network
		 */
    // TODO might need more stuff once we handle assertion generation
    test.getAccessedEnvironment().addLocalListeningPorts(VirtualNetwork.getInstance().getViewOfLocalListeningPorts());
    test.getAccessedEnvironment().addRemoteURLs(VirtualNetwork.getInstance().getViewOfRemoteAccessedFiles());
    test.getAccessedEnvironment().addRemoteContactedPorts(VirtualNetwork.getInstance().getViewOfRemoteContactedPorts());
    if (!hasAddedRemoteURLs && test.getAccessedEnvironment().getViewOfRemoteURLs().size() > 0) {
        hasAddedRemoteURLs = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("createRemoteTextFile", new Class<?>[] { EvoSuiteURL.class, String.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedRemoteURLs: " + e.getMessage(), e);
        }
    }
    boolean openedTCP = false;
    boolean openedUDP = false;
    for (EndPointInfo info : test.getAccessedEnvironment().getViewOfLocalListeningPorts()) {
        if (info.getType().equals(VirtualNetwork.ConnectionType.TCP)) {
            openedTCP = true;
        } else if (info.getType().equals(VirtualNetwork.ConnectionType.UDP)) {
            openedUDP = true;
        }
        if (openedTCP && openedUDP) {
            break;
        }
    }
    if (!hasAddedUdpSupport && openedUDP) {
        hasAddedUdpSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendUdpPacket", new Class<?>[] { EvoSuiteLocalAddress.class, EvoSuiteRemoteAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendUdpPacket", new Class<?>[] { EvoSuiteLocalAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedUdpSupport: " + e.getMessage(), e);
        }
    }
    if (!hasAddedTcpListeningSupport && openedTCP) {
        hasAddedTcpListeningSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendDataOnTcp", new Class<?>[] { EvoSuiteLocalAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendMessageOnTcp", new Class<?>[] { EvoSuiteLocalAddress.class, String.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedTcpListeningSupport: " + e.getMessage(), e);
        }
    }
    if (!hasAddedTcpRemoteSupport && test.getAccessedEnvironment().getViewOfRemoteContactedPorts().size() > 0) {
        hasAddedTcpRemoteSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("openRemoteTcpServer", new Class<?>[] { EvoSuiteRemoteAddress.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedTcpRemoteSupport: " + e.getMessage(), e);
        }
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 3 with EndPointInfo

use of org.evosuite.runtime.vnet.EndPointInfo 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 EndPointInfo

use of org.evosuite.runtime.vnet.EndPointInfo in project evosuite by EvoSuite.

the class RemoteAddressPrimitiveStatement method randomize.

@Override
public void randomize() {
    EvoSuiteRemoteAddress addr;
    // TODO parameter
    double threshold = 0.8;
    boolean accessed = Randomness.nextDouble() <= threshold;
    if (accessed && !tc.getAccessedEnvironment().getViewOfRemoteContactedPorts().isEmpty()) {
        // use an address that the SUT tried to contact
        EndPointInfo info = Randomness.choice(tc.getAccessedEnvironment().getViewOfRemoteContactedPorts());
        String host = info.getHost();
        // TODO check why it can be a 0 here
        int port = info.getPort();
        port = getPort(port);
        addr = new EvoSuiteRemoteAddress(host, port);
    } else {
        /*
                make up an address based on string/int constants.
                this is needed to handle the cases when the SUT get
                an incoming message, and then check its remote address.

                TODO: here we could validate if host/port values are
                indeed valid. However, as this is kind of special case,
                and likely not so common, it doesn't have high priority.
             */
        ConstantPool constantPool = ConstantPoolManager.getInstance().getConstantPool();
        String host = constantPool.getRandomString();
        int port = constantPool.getRandomInt();
        port = getPort(port);
        addr = new EvoSuiteRemoteAddress(host, port);
    }
    setValue(addr);
}
Also used : EvoSuiteRemoteAddress(org.evosuite.runtime.testdata.EvoSuiteRemoteAddress) ConstantPool(org.evosuite.seeding.ConstantPool) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo)

Example 5 with EndPointInfo

use of org.evosuite.runtime.vnet.EndPointInfo in project evosuite by EvoSuite.

the class SocketTest method testSingleConnection.

@Test
public void testSingleConnection() throws IOException {
    String remoteHost = "127.42.0.42";
    int remotePort = 666;
    InetSocketAddress saddr = new MockInetSocketAddress(MockInetAddress.getByName(remoteHost), remotePort);
    MockSocket s = new MockSocket();
    RemoteTcpServer server = new RemoteTcpServer(new EndPointInfo(saddr.getAddress().getHostAddress(), saddr.getPort(), ConnectionType.TCP));
    VirtualNetwork.getInstance().addRemoteTcpServer(server);
    String msgFromServer = "server";
    server.sendMessage(msgFromServer);
    s.connect(saddr);
    String msgFromSUT = "SUT";
    s.getOutputStream().write(msgFromSUT.getBytes());
    String sutReceived = new Scanner(s.getInputStream()).nextLine();
    Assert.assertEquals(msgFromServer, sutReceived);
    Assert.assertEquals(msgFromSUT, server.getAllReceivedDataAsString());
    s.close();
}
Also used : Scanner(java.util.Scanner) RemoteTcpServer(org.evosuite.runtime.vnet.RemoteTcpServer) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo) Test(org.junit.Test)

Aggregations

EndPointInfo (org.evosuite.runtime.vnet.EndPointInfo)7 RemoteTcpServer (org.evosuite.runtime.vnet.RemoteTcpServer)2 Test (org.junit.Test)2 DatagramPacket (java.net.DatagramPacket)1 InetAddress (java.net.InetAddress)1 Map (java.util.Map)1 Scanner (java.util.Scanner)1 EvoSuiteLocalAddress (org.evosuite.runtime.testdata.EvoSuiteLocalAddress)1 EvoSuiteRemoteAddress (org.evosuite.runtime.testdata.EvoSuiteRemoteAddress)1 ConstantPool (org.evosuite.seeding.ConstantPool)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 GenericMethod (org.evosuite.utils.generic.GenericMethod)1