Loading packages/Tethering/tests/privileged/src/android/net/ip/DadProxyTest.java +21 −83 Original line number Diff line number Diff line Loading @@ -17,9 +17,9 @@ package android.net.ip; import static android.system.OsConstants.IPPROTO_ICMPV6; import static android.system.OsConstants.IPPROTO_TCP; import static com.android.internal.util.BitUtils.uint16; import static com.android.net.module.util.IpUtils.icmpv6Checksum; import static com.android.net.module.util.NetworkStackConstants.ETHER_SRC_ADDR_OFFSET; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; Loading @@ -30,34 +30,29 @@ import android.content.Context; import android.net.INetd; import android.net.InetAddresses; import android.net.MacAddress; import android.net.TestNetworkInterface; import android.net.TestNetworkManager; import android.net.util.InterfaceParams; import android.net.util.TetheringUtils; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.system.ErrnoException; import android.system.Os; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.net.module.util.IpUtils; import com.android.testutils.TapPacketReader; import com.android.testutils.TapPacketReaderRule; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; import java.io.FileDescriptor; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicReference; @RunWith(AndroidJUnit4.class) @SmallTest Loading @@ -65,16 +60,18 @@ public class DadProxyTest { private static final int DATA_BUFFER_LEN = 4096; private static final int PACKET_TIMEOUT_MS = 5_000; // TODO: make NetworkStackConstants accessible to this test and use the constant from there. private static final int ETHER_SRC_ADDR_OFFSET = 6; // Start the readers manually on a common handler shared with DadProxy, for simplicity @Rule public final TapPacketReaderRule mUpstreamReader = new TapPacketReaderRule( DATA_BUFFER_LEN, false /* autoStart */); @Rule public final TapPacketReaderRule mTetheredReader = new TapPacketReaderRule( DATA_BUFFER_LEN, false /* autoStart */); private DadProxy mProxy; TestNetworkInterface mUpstreamTestIface, mTetheredTestIface; private InterfaceParams mUpstreamParams, mTetheredParams; private HandlerThread mHandlerThread; private Handler mHandler; private TapPacketReader mUpstreamPacketReader, mTetheredPacketReader; private FileDescriptor mUpstreamTapFd, mTetheredTapFd; private static INetd sNetd; Loading Loading @@ -106,12 +103,12 @@ public class DadProxyTest { @After public void tearDown() throws Exception { mUpstreamReader.stop(); mTetheredReader.stop(); if (mHandlerThread != null) { mHandler.post(mUpstreamPacketReader::stop); // Also closes the socket mHandler.post(mTetheredPacketReader::stop); // Also closes the socket mUpstreamTapFd = null; mTetheredTapFd = null; mHandlerThread.quitSafely(); mHandlerThread.join(PACKET_TIMEOUT_MS); } if (mTetheredParams != null) { Loading @@ -120,54 +117,20 @@ public class DadProxyTest { if (mUpstreamParams != null) { sNetd.networkRemoveInterface(INetd.LOCAL_NET_ID, mUpstreamParams.name); } if (mUpstreamTestIface != null) { try { Os.close(mUpstreamTestIface.getFileDescriptor().getFileDescriptor()); } catch (ErrnoException e) { } } if (mTetheredTestIface != null) { try { Os.close(mTetheredTestIface.getFileDescriptor().getFileDescriptor()); } catch (ErrnoException e) { } } } private TestNetworkInterface setupTapInterface() { final Instrumentation inst = InstrumentationRegistry.getInstrumentation(); AtomicReference<TestNetworkInterface> iface = new AtomicReference<>(); inst.getUiAutomation().adoptShellPermissionIdentity(); try { final TestNetworkManager tnm = (TestNetworkManager) inst.getContext().getSystemService( Context.TEST_NETWORK_SERVICE); iface.set(tnm.createTapInterface()); } finally { inst.getUiAutomation().dropShellPermissionIdentity(); } return iface.get(); } private void setupTapInterfaces() { // Create upstream test iface. mUpstreamTestIface = setupTapInterface(); mUpstreamParams = InterfaceParams.getByName(mUpstreamTestIface.getInterfaceName()); mUpstreamReader.start(mHandler); mUpstreamParams = InterfaceParams.getByName(mUpstreamReader.iface.getInterfaceName()); assertNotNull(mUpstreamParams); mUpstreamTapFd = mUpstreamTestIface.getFileDescriptor().getFileDescriptor(); mUpstreamPacketReader = new TapPacketReader(mHandler, mUpstreamTapFd, DATA_BUFFER_LEN); mHandler.post(mUpstreamPacketReader::start); mUpstreamPacketReader = mUpstreamReader.getReader(); // Create tethered test iface. mTetheredTestIface = setupTapInterface(); mTetheredParams = InterfaceParams.getByName(mTetheredTestIface.getInterfaceName()); mTetheredReader.start(mHandler); mTetheredParams = InterfaceParams.getByName(mTetheredReader.getIface().getInterfaceName()); assertNotNull(mTetheredParams); mTetheredTapFd = mTetheredTestIface.getFileDescriptor().getFileDescriptor(); mTetheredPacketReader = new TapPacketReader(mHandler, mTetheredTapFd, DATA_BUFFER_LEN); mHandler.post(mTetheredPacketReader::start); mTetheredPacketReader = mTetheredReader.getReader(); } private static final int IPV6_HEADER_LEN = 40; Loading @@ -177,31 +140,6 @@ public class DadProxyTest { private static final int ICMPV6_CHECKSUM_OFFSET = 2; private static final int ETHER_TYPE_IPV6 = 0x86dd; // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static int checksumFold(int sum) { while (sum > 0xffff) { sum = (sum >> 16) + (sum & 0xffff); } return sum; } // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static short checksumAdjust(short checksum, short oldWord, short newWord) { checksum = (short) ~checksum; int tempSum = checksumFold(uint16(checksum) + uint16(newWord) + 0xffff - uint16(oldWord)); return (short) ~tempSum; } // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static short icmpv6Checksum(ByteBuffer buf, int ipOffset, int transportOffset, int transportLen) { // The ICMPv6 checksum is the same as the TCP checksum, except the pseudo-header uses // 58 (ICMPv6) instead of 6 (TCP). Calculate the TCP checksum, and then do an incremental // checksum adjustment for the change in the next header byte. short checksum = IpUtils.tcpChecksum(buf, ipOffset, transportOffset, transportLen); return checksumAdjust(checksum, (short) IPPROTO_TCP, (short) IPPROTO_ICMPV6); } private static ByteBuffer createDadPacket(int type) { // Refer to buildArpPacket() int icmpLen = ICMPV6_NA_NS_LEN Loading Loading
packages/Tethering/tests/privileged/src/android/net/ip/DadProxyTest.java +21 −83 Original line number Diff line number Diff line Loading @@ -17,9 +17,9 @@ package android.net.ip; import static android.system.OsConstants.IPPROTO_ICMPV6; import static android.system.OsConstants.IPPROTO_TCP; import static com.android.internal.util.BitUtils.uint16; import static com.android.net.module.util.IpUtils.icmpv6Checksum; import static com.android.net.module.util.NetworkStackConstants.ETHER_SRC_ADDR_OFFSET; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; Loading @@ -30,34 +30,29 @@ import android.content.Context; import android.net.INetd; import android.net.InetAddresses; import android.net.MacAddress; import android.net.TestNetworkInterface; import android.net.TestNetworkManager; import android.net.util.InterfaceParams; import android.net.util.TetheringUtils; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.system.ErrnoException; import android.system.Os; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.net.module.util.IpUtils; import com.android.testutils.TapPacketReader; import com.android.testutils.TapPacketReaderRule; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; import java.io.FileDescriptor; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicReference; @RunWith(AndroidJUnit4.class) @SmallTest Loading @@ -65,16 +60,18 @@ public class DadProxyTest { private static final int DATA_BUFFER_LEN = 4096; private static final int PACKET_TIMEOUT_MS = 5_000; // TODO: make NetworkStackConstants accessible to this test and use the constant from there. private static final int ETHER_SRC_ADDR_OFFSET = 6; // Start the readers manually on a common handler shared with DadProxy, for simplicity @Rule public final TapPacketReaderRule mUpstreamReader = new TapPacketReaderRule( DATA_BUFFER_LEN, false /* autoStart */); @Rule public final TapPacketReaderRule mTetheredReader = new TapPacketReaderRule( DATA_BUFFER_LEN, false /* autoStart */); private DadProxy mProxy; TestNetworkInterface mUpstreamTestIface, mTetheredTestIface; private InterfaceParams mUpstreamParams, mTetheredParams; private HandlerThread mHandlerThread; private Handler mHandler; private TapPacketReader mUpstreamPacketReader, mTetheredPacketReader; private FileDescriptor mUpstreamTapFd, mTetheredTapFd; private static INetd sNetd; Loading Loading @@ -106,12 +103,12 @@ public class DadProxyTest { @After public void tearDown() throws Exception { mUpstreamReader.stop(); mTetheredReader.stop(); if (mHandlerThread != null) { mHandler.post(mUpstreamPacketReader::stop); // Also closes the socket mHandler.post(mTetheredPacketReader::stop); // Also closes the socket mUpstreamTapFd = null; mTetheredTapFd = null; mHandlerThread.quitSafely(); mHandlerThread.join(PACKET_TIMEOUT_MS); } if (mTetheredParams != null) { Loading @@ -120,54 +117,20 @@ public class DadProxyTest { if (mUpstreamParams != null) { sNetd.networkRemoveInterface(INetd.LOCAL_NET_ID, mUpstreamParams.name); } if (mUpstreamTestIface != null) { try { Os.close(mUpstreamTestIface.getFileDescriptor().getFileDescriptor()); } catch (ErrnoException e) { } } if (mTetheredTestIface != null) { try { Os.close(mTetheredTestIface.getFileDescriptor().getFileDescriptor()); } catch (ErrnoException e) { } } } private TestNetworkInterface setupTapInterface() { final Instrumentation inst = InstrumentationRegistry.getInstrumentation(); AtomicReference<TestNetworkInterface> iface = new AtomicReference<>(); inst.getUiAutomation().adoptShellPermissionIdentity(); try { final TestNetworkManager tnm = (TestNetworkManager) inst.getContext().getSystemService( Context.TEST_NETWORK_SERVICE); iface.set(tnm.createTapInterface()); } finally { inst.getUiAutomation().dropShellPermissionIdentity(); } return iface.get(); } private void setupTapInterfaces() { // Create upstream test iface. mUpstreamTestIface = setupTapInterface(); mUpstreamParams = InterfaceParams.getByName(mUpstreamTestIface.getInterfaceName()); mUpstreamReader.start(mHandler); mUpstreamParams = InterfaceParams.getByName(mUpstreamReader.iface.getInterfaceName()); assertNotNull(mUpstreamParams); mUpstreamTapFd = mUpstreamTestIface.getFileDescriptor().getFileDescriptor(); mUpstreamPacketReader = new TapPacketReader(mHandler, mUpstreamTapFd, DATA_BUFFER_LEN); mHandler.post(mUpstreamPacketReader::start); mUpstreamPacketReader = mUpstreamReader.getReader(); // Create tethered test iface. mTetheredTestIface = setupTapInterface(); mTetheredParams = InterfaceParams.getByName(mTetheredTestIface.getInterfaceName()); mTetheredReader.start(mHandler); mTetheredParams = InterfaceParams.getByName(mTetheredReader.getIface().getInterfaceName()); assertNotNull(mTetheredParams); mTetheredTapFd = mTetheredTestIface.getFileDescriptor().getFileDescriptor(); mTetheredPacketReader = new TapPacketReader(mHandler, mTetheredTapFd, DATA_BUFFER_LEN); mHandler.post(mTetheredPacketReader::start); mTetheredPacketReader = mTetheredReader.getReader(); } private static final int IPV6_HEADER_LEN = 40; Loading @@ -177,31 +140,6 @@ public class DadProxyTest { private static final int ICMPV6_CHECKSUM_OFFSET = 2; private static final int ETHER_TYPE_IPV6 = 0x86dd; // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static int checksumFold(int sum) { while (sum > 0xffff) { sum = (sum >> 16) + (sum & 0xffff); } return sum; } // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static short checksumAdjust(short checksum, short oldWord, short newWord) { checksum = (short) ~checksum; int tempSum = checksumFold(uint16(checksum) + uint16(newWord) + 0xffff - uint16(oldWord)); return (short) ~tempSum; } // TODO: move the IpUtils code to frameworks/lib/net and link it statically. private static short icmpv6Checksum(ByteBuffer buf, int ipOffset, int transportOffset, int transportLen) { // The ICMPv6 checksum is the same as the TCP checksum, except the pseudo-header uses // 58 (ICMPv6) instead of 6 (TCP). Calculate the TCP checksum, and then do an incremental // checksum adjustment for the change in the next header byte. short checksum = IpUtils.tcpChecksum(buf, ipOffset, transportOffset, transportLen); return checksumAdjust(checksum, (short) IPPROTO_TCP, (short) IPPROTO_ICMPV6); } private static ByteBuffer createDadPacket(int type) { // Refer to buildArpPacket() int icmpLen = ICMPV6_NA_NS_LEN Loading