Merge "Test both recvfrom() and read() in PingTest."

This commit is contained in:
Lorenzo Colitti
2014-02-03 23:30:55 +00:00
committed by Gerrit Code Review

View File

@@ -105,19 +105,25 @@ public class PingTest extends AndroidTestCase {
/** /**
* Checks that a socket has received a response appropriate to the specified packet. * Checks that a socket has received a response appropriate to the specified packet.
*/ */
private void checkResponse(FileDescriptor s, private void checkResponse(FileDescriptor s, InetAddress dest,
InetAddress dest, byte[] sent) throws ErrnoException, IOException { byte[] sent, boolean useRecvfrom) throws ErrnoException, IOException {
// Receive the response.
InetSocketAddress from = new InetSocketAddress();
ByteBuffer responseBuffer = ByteBuffer.allocate(MAX_SIZE); ByteBuffer responseBuffer = ByteBuffer.allocate(MAX_SIZE);
int bytesRead = Libcore.os.recvfrom(s, responseBuffer, 0, from); int bytesRead;
// Check the source address and scope ID. // Receive the response.
assertTrue(from.getAddress() instanceof Inet6Address); if (useRecvfrom) {
Inet6Address fromAddress = (Inet6Address) from.getAddress(); InetSocketAddress from = new InetSocketAddress();
assertEquals(0, fromAddress.getScopeId()); bytesRead = Libcore.os.recvfrom(s, responseBuffer, 0, from);
assertNull(fromAddress.getScopedInterface());
assertEquals(dest.getHostAddress(), fromAddress.getHostAddress()); // Check the source address and scope ID.
assertTrue(from.getAddress() instanceof Inet6Address);
Inet6Address fromAddress = (Inet6Address) from.getAddress();
assertEquals(0, fromAddress.getScopeId());
assertNull(fromAddress.getScopedInterface());
assertEquals(dest.getHostAddress(), fromAddress.getHostAddress());
} else {
bytesRead = Libcore.os.read(s, responseBuffer);
}
// Check the packet length. // Check the packet length.
assertEquals(sent.length, bytesRead); assertEquals(sent.length, bytesRead);
@@ -150,8 +156,11 @@ public class PingTest extends AndroidTestCase {
for (int i = 0; i < NUM_PACKETS; i++) { for (int i = 0; i < NUM_PACKETS; i++) {
byte[] packet = pingPacket((int) (Math.random() * MAX_SIZE)); byte[] packet = pingPacket((int) (Math.random() * MAX_SIZE));
FileDescriptor s = createPingSocket(); FileDescriptor s = createPingSocket();
// Use both recvfrom and read().
sendPing(s, ipv6Loopback, packet); sendPing(s, ipv6Loopback, packet);
checkResponse(s, ipv6Loopback, packet); checkResponse(s, ipv6Loopback, packet, true);
sendPing(s, ipv6Loopback, packet);
checkResponse(s, ipv6Loopback, packet, false);
// Check closing the socket doesn't raise an exception. // Check closing the socket doesn't raise an exception.
Libcore.os.close(s); Libcore.os.close(s);
} }