From 950e10572b4d999787bd46c6f3758a0e9f89f213 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Fri, 29 Mar 2019 22:05:29 +0900 Subject: [PATCH] Add test coverage for ICMP echo constants. Bug: 129251251 Test: atest android.net.ipv6.cts.PingTest com.android.cts.net.HostsideVpnTests Change-Id: Icca18e0870588ca250225ce79cf30dbd8c361e84 --- .../cts/net/hostside/PacketReflector.java | 19 +++++++++++++++---- .../src/android/net/ipv6/cts/PingTest.java | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/PacketReflector.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/PacketReflector.java index a4a2956d3a..124c2c3862 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/PacketReflector.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/PacketReflector.java @@ -16,6 +16,11 @@ package com.android.cts.net.hostside; +import static android.system.OsConstants.ICMP6_ECHO_REPLY; +import static android.system.OsConstants.ICMP6_ECHO_REQUEST; +import static android.system.OsConstants.ICMP_ECHO; +import static android.system.OsConstants.ICMP_ECHOREPLY; + import android.system.ErrnoException; import android.system.Os; import android.util.Log; @@ -47,8 +52,6 @@ public class PacketReflector extends Thread { private static final byte ICMP_ECHO = 8; private static final byte ICMP_ECHOREPLY = 0; - private static final byte ICMPV6_ECHO_REQUEST = (byte) 128; - private static final byte ICMPV6_ECHO_REPLY = (byte) 129; private static String TAG = "PacketReflector"; @@ -125,7 +128,7 @@ public class PacketReflector extends Thread { byte type = buf[hdrLen]; if (!(version == 4 && type == ICMP_ECHO) && - !(version == 6 && type == ICMPV6_ECHO_REQUEST)) { + !(version == 6 && type == (byte) ICMP6_ECHO_REQUEST)) { return; } @@ -145,10 +148,18 @@ public class PacketReflector extends Thread { return; } + byte replyType = buf[hdrLen]; + if ((type == ICMP_ECHO && replyType != ICMP_ECHOREPLY) + || (type == (byte) ICMP6_ECHO_REQUEST && replyType != (byte) ICMP6_ECHO_REPLY)) { + Log.i(TAG, "Received unexpected ICMP reply: original " + type + + ", reply " + replyType); + return; + } + // Compare the response we got with the original packet. // The only thing that should have changed are addresses, type and checksum. // Overwrite them with the received bytes and see if the packet is otherwise identical. - request[hdrLen] = buf[hdrLen]; // Type. + request[hdrLen] = buf[hdrLen]; // Type request[hdrLen + 2] = buf[hdrLen + 2]; // Checksum byte 1. request[hdrLen + 3] = buf[hdrLen + 3]; // Checksum byte 2. diff --git a/tests/cts/net/src/android/net/ipv6/cts/PingTest.java b/tests/cts/net/src/android/net/ipv6/cts/PingTest.java index c23ad30f05..146fd83978 100644 --- a/tests/cts/net/src/android/net/ipv6/cts/PingTest.java +++ b/tests/cts/net/src/android/net/ipv6/cts/PingTest.java @@ -61,7 +61,7 @@ public class PingTest extends AndroidTestCase { /** The beginning of an ICMPv6 echo request: type, code, and uninitialized checksum. */ private static final byte[] PING_HEADER = new byte[] { - (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00 + (byte) ICMP6_ECHO_REQUEST, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; /** @@ -135,7 +135,7 @@ public class PingTest extends AndroidTestCase { byte[] response = new byte[bytesRead]; responseBuffer.flip(); responseBuffer.get(response, 0, bytesRead); - assertEquals((byte) 0x81, response[0]); + assertEquals((byte) ICMP6_ECHO_REPLY, response[0]); // Find out what ICMP ID was used in the packet that was sent. int id = ((InetSocketAddress) Os.getsockname(s)).getPort();