/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*
*
* Used in conjunction to EchoService to test System.inheritedChannel().
*
* The first test is the TCP echo test. A service is launched with a TCP
* socket and a TCP message is sent to the service. The test checks that
* the message is correctly echoed.
*
* The second test is a UDP echo test. A service is launched with a UDP
* socket and a UDP packet is sent to the service. The test checks that
* the packet is correctly echoed.
*
*/
public class EchoTest {
/*
* Sends a message with random bytes to the service, and then waits for
* a reply (with timeout). Once the reply is received it is checked to ensure
* that it matches the original message.
*/
int repeat = 100;
// generate bytes into a buffer and send it to the service
for (int i=0; i<repeat; i++) {
}
// now we put the channel into non-blocking mode and we read the
// reply from the service into a second buffer.
sc.configureBlocking(false);
int nread = 0;
long to = 5000;
if (sk.isReadable()) {
if (n > 0) {
nread += n;
}
if (n < 0) {
break; // EOF
}
}
if (to <= 0) {
break;
}
}
// and compare the response
boolean err = false;
err = true;
} else {
while (bb1.hasRemaining()) {
err = true;
}
}
}
// if error print out the response from the service (could be a stack trace)
if (err) {
while (bb2.hasRemaining()) {
}
throw new RuntimeException("Bad response or premature EOF from service");
}
}
/*
* Send a UDP packet to the service, wait for a reply (with timeout). Finally
* check that the packet is the same length as the original.
*/
// and receive the echo
throw new RuntimeException("Received packet of incorrect length");
}
}
// TCP echo
try {
TCPEchoTest();
} catch (Exception x) {
failures++;
}
// UDP echo
try {
UDPEchoTest();
} catch (Exception x) {
x.printStackTrace();
failures++;
}
if (failures > 0) {
throw new RuntimeException("Test failed - see log for details");
}
}
}