/** * Open new connection toward {@code sutServer} and buffer the content of {@code data} to be later * sent once {@code sutServer} is opened * * @param sutServer the host/port of the SUT * @param data if {@code null}, just simulate opening of connection * @return {@code false} if {@code sutServer} is {@code null} */ public static boolean sendDataOnTcp(EvoSuiteLocalAddress sutServer, byte[] data) { if (sutServer == null) { return false; } NativeTcp connection = VirtualNetwork.getInstance() .registerIncomingTcpConnection( DEFAULT_REMOTE_ADDRESS, VirtualNetwork.getInstance().getNewRemoteEphemeralPort(), sutServer.getHost(), sutServer.getPort()); /* * At this point in time the SUT has not opened a connection yet (if it did, * it would had thrown an IOException). * But we can already put the message on the buffer */ if (data != null) { for (byte b : data) { connection.writeToSUT(b); } } // TODO close connection? or should rather be in another helper function? return true; }
/** * Create a send a new UDP packet to the SUT. The packets are buffered till the SUT opens a socket * to read them. * * @param sutAddress * @param data * @return */ public static boolean sendUdpPacket( EvoSuiteLocalAddress sutAddress, EvoSuiteRemoteAddress remoteAddress, byte[] data) { if (sutAddress == null) { return false; } // data can be null if (data == null) { data = new byte[0]; } InetAddress address = null; try { address = MockInetAddress.getByName(remoteAddress.getHost()); } catch (UnknownHostException e) { return false; } VirtualNetwork.getInstance() .sendPacketToSUT( data, address, remoteAddress.getPort(), sutAddress.getHost(), sutAddress.getPort()); return true; }