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]);
}
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);
}
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);
}
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();
}
Aggregations