Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a0c5d7e authored by Hugo Benichi's avatar Hugo Benichi
Browse files

Migrate most of core networking tests to junit4

This patch is a batch of mechanical changes to test classes to migrate
away from AndroidTestCase and TestCase.

Bug: 62918393
Test: runtest frameworks-net
Change-Id: I74134609e511f22c4d9ecd65780e981f9ba7ae3f
parent 5f123e6a
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -31,19 +31,21 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.system.Os;
import android.test.AndroidTestCase;

import com.android.server.IpSecService;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link IpSecManager}. */
@SmallTest
@RunWith(JUnit4.class)
@RunWith(AndroidJUnit4.class)
public class IpSecManagerTest {

    private static final int TEST_UDP_ENCAP_PORT = 34567;
+29 −4
Original line number Diff line number Diff line
@@ -30,23 +30,30 @@ import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.test.suitebuilder.annotation.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.SmallTest;

import com.google.android.collect.Sets;

import junit.framework.TestCase;

import java.util.HashSet;

import org.junit.runner.RunWith;
import org.junit.Test;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class NetworkStatsTest extends TestCase {
public class NetworkStatsTest {

    private static final String TEST_IFACE = "test0";
    private static final String TEST_IFACE2 = "test2";
    private static final int TEST_UID = 1001;
    private static final long TEST_START = 1194220800000L;

    @Test
    public void testFindIndex() throws Exception {
        final NetworkStats stats = new NetworkStats(TEST_START, 5)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
@@ -74,6 +81,7 @@ public class NetworkStatsTest extends TestCase {
                ROAMING_NO));
    }

    @Test
    public void testFindIndexHinted() {
        final NetworkStats stats = new NetworkStats(TEST_START, 3)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
@@ -116,6 +124,7 @@ public class NetworkStatsTest extends TestCase {
        }
    }

    @Test
    public void testAddEntryGrow() throws Exception {
        final NetworkStats stats = new NetworkStats(TEST_START, 4);

@@ -168,6 +177,7 @@ public class NetworkStatsTest extends TestCase {
                ROAMING_YES, 7L, 70L, 5L, 50L, 11);
    }

    @Test
    public void testCombineExisting() throws Exception {
        final NetworkStats stats = new NetworkStats(TEST_START, 10);

@@ -190,6 +200,7 @@ public class NetworkStatsTest extends TestCase {
                256L, 2L, 256L, 2L, 6);
    }

    @Test
    public void testSubtractIdenticalData() throws Exception {
        final NetworkStats before = new NetworkStats(TEST_START, 2)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
@@ -208,6 +219,7 @@ public class NetworkStatsTest extends TestCase {
                0L, 0L, 0L, 0);
    }

    @Test
    public void testSubtractIdenticalRows() throws Exception {
        final NetworkStats before = new NetworkStats(TEST_START, 2)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
@@ -226,6 +238,7 @@ public class NetworkStatsTest extends TestCase {
                1L, 4L, 1L, 8);
    }

    @Test
    public void testSubtractNewRows() throws Exception {
        final NetworkStats before = new NetworkStats(TEST_START, 2)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
@@ -247,6 +260,7 @@ public class NetworkStatsTest extends TestCase {
                1024L, 8L, 1024L, 8L, 20);
    }

    @Test
    public void testSubtractMissingRows() throws Exception {
        final NetworkStats before = new NetworkStats(TEST_START, 2)
                .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 0L, 0L, 0L, 0)
@@ -264,6 +278,7 @@ public class NetworkStatsTest extends TestCase {
        assertEquals(4L, result.getTotalBytes());
    }

    @Test
    public void testTotalBytes() throws Exception {
        final NetworkStats iface = new NetworkStats(TEST_START, 2)
                .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 128L, 0L, 0L, 0L, 0L)
@@ -304,6 +319,7 @@ public class NetworkStatsTest extends TestCase {
        assertEquals(96L, uidRoaming.getTotalBytes());
    }

    @Test
    public void testGroupedByIfaceEmpty() throws Exception {
        final NetworkStats uidStats = new NetworkStats(TEST_START, 3);
        final NetworkStats grouped = uidStats.groupedByIface();
@@ -312,6 +328,7 @@ public class NetworkStatsTest extends TestCase {
        assertEquals(0, grouped.size());
    }

    @Test
    public void testGroupedByIfaceAll() throws Exception {
        final NetworkStats uidStats = new NetworkStats(TEST_START, 3)
                .addValues(IFACE_ALL, 100, SET_ALL, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, 0L,
@@ -329,6 +346,7 @@ public class NetworkStatsTest extends TestCase {
                384L, 24L, 0L, 6L, 0L);
    }

    @Test
    public void testGroupedByIface() throws Exception {
        final NetworkStats uidStats = new NetworkStats(TEST_START, 7)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L,
@@ -357,6 +375,7 @@ public class NetworkStatsTest extends TestCase {
                1024L, 64L, 0L, 0L, 0L);
    }

    @Test
    public void testAddAllValues() {
        final NetworkStats first = new NetworkStats(TEST_START, 5)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
@@ -387,6 +406,7 @@ public class NetworkStatsTest extends TestCase {
                32L, 0L, 0L, 0L, 0L);
    }

    @Test
    public void testGetTotal() {
        final NetworkStats stats = new NetworkStats(TEST_START, 7)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L,
@@ -415,6 +435,7 @@ public class NetworkStatsTest extends TestCase {
        assertValues(stats.getTotal(null, ifaces), 1024L, 64L, 0L, 0L, 0L);
    }

    @Test
    public void testWithoutUid() throws Exception {
        final NetworkStats before = new NetworkStats(TEST_START, 3)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
@@ -433,6 +454,7 @@ public class NetworkStatsTest extends TestCase {
                8L, 0L, 0L, 0L);
    }

    @Test
    public void testClone() throws Exception {
        final NetworkStats original = new NetworkStats(TEST_START, 5)
                .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
@@ -449,6 +471,7 @@ public class NetworkStatsTest extends TestCase {
        assertEquals(128L + 512L, clone.getTotalBytes());
    }

    @Test
    public void testAddWhenEmpty() throws Exception {
        final NetworkStats red = new NetworkStats(TEST_START, -1);
        final NetworkStats blue = new NetworkStats(TEST_START, 5)
@@ -459,6 +482,7 @@ public class NetworkStatsTest extends TestCase {
        red.combineAllValues(blue);
    }

    @Test
    public void testMigrateTun() throws Exception {
        final int tunUid = 10030;
        final String tunIface = "tun0";
@@ -556,6 +580,7 @@ public class NetworkStatsTest extends TestCase {
    // interface by the vpn app before it's sent out of the underlying interface. The VPN app should
    // not be charged for the echoed data but it should still be charged for any extra data it sends
    // via the underlying interface.
    @Test
    public void testMigrateTun_VpnAsLoopback() {
        final int tunUid = 10030;
        final String tunIface = "tun0";
+16 −10
Original line number Diff line number Diff line
@@ -16,14 +16,20 @@

package android.net;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import android.os.Parcel;
import android.test.suitebuilder.annotation.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.SmallTest;

import junit.framework.TestCase;
import org.junit.runner.RunWith;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class UidRangeTest extends TestCase {
@RunWith(AndroidJUnit4.class)
@SmallTest
public class UidRangeTest {

    static {
        System.loadLibrary("frameworksnettestsjni");
@@ -33,7 +39,7 @@ public class UidRangeTest extends TestCase {
    private static native int getStart(byte[] inParcel);
    private static native int getStop(byte[] inParcel);

    @SmallTest
    @Test
    public void testNativeParcelUnparcel() {
        UidRange original = new UidRange(1234, Integer.MAX_VALUE);

@@ -45,7 +51,7 @@ public class UidRangeTest extends TestCase {
        assertArrayEquals(inParcel, outParcel);
    }

    @SmallTest
    @Test
    public void testIndividualNativeFields() {
        UidRange original = new UidRange(0x11115678, 0x22224321);
        byte[] originalBytes = marshall(original);
@@ -54,14 +60,14 @@ public class UidRangeTest extends TestCase {
        assertEquals(original.stop, getStop(originalBytes));
    }

    @SmallTest
    @Test
    public void testSingleItemUidRangeAllowed() {
        new UidRange(123, 123);
        new UidRange(0, 0);
        new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    @SmallTest
    @Test
    public void testNegativeUidsDisallowed() {
        try {
            new UidRange(-2, 100);
@@ -76,7 +82,7 @@ public class UidRangeTest extends TestCase {
        }
    }

    @SmallTest
    @Test
    public void testStopLessThanStartDisallowed() {
        final int x = 4195000;
        try {
+41 −30
Original line number Diff line number Diff line
@@ -16,6 +16,16 @@

package android.net.apf;

import static android.system.OsConstants.*;
import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.internal.util.BitUtils.put;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkUtils;
@@ -30,23 +40,22 @@ import android.net.metrics.RaEvent;
import android.os.ConditionVariable;
import android.os.Parcelable;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.SmallTest;
import android.system.ErrnoException;
import android.system.Os;
import android.test.AndroidTestCase;
import android.text.format.DateUtils;
import android.test.suitebuilder.annotation.SmallTest;
import static android.system.OsConstants.*;

import com.android.frameworks.tests.net.R;
import com.android.internal.util.HexDump;
import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.internal.util.BitUtils.put;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

import java.io.File;
import java.io.FileDescriptor;
@@ -69,14 +78,15 @@ import libcore.io.Streams;
 * Build, install and run with:
 *  runtest frameworks-net -c android.net.apf.ApfTest
 */
public class ApfTest extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ApfTest {
    private static final int TIMEOUT_MS = 500;

    @Mock IpConnectivityLog mLog;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        // Load up native shared library containing APF interpreter exposed via JNI.
        System.loadLibrary("frameworksnettestsjni");
@@ -161,7 +171,7 @@ public class ApfTest extends AndroidTestCase {
     * generating bytecode for that program and running it through the
     * interpreter to verify it functions correctly.
     */
    @SmallTest
    @Test
    public void testApfInstructions() throws IllegalInstructionException {
        // Empty program should pass because having the program counter reach the
        // location immediately after the program indicates the packet should be
@@ -569,7 +579,7 @@ public class ApfTest extends AndroidTestCase {
     * Generate some BPF programs, translate them to APF, then run APF and BPF programs
     * over packet traces and verify both programs filter out the same packets.
     */
    @SmallTest
    @Test
    public void testApfAgainstBpf() throws Exception {
        String[] tcpdump_filters = new String[]{ "udp", "tcp", "icmp", "icmp6", "udp port 53",
                "arp", "dst 239.255.255.250", "arp or tcp or udp port 53", "net 192.168.1.0/24",
@@ -739,7 +749,7 @@ public class ApfTest extends AndroidTestCase {
    private static final byte[] ANOTHER_IPV4_ADDR        = {10, 0, 0, 2};
    private static final byte[] IPV4_ANY_HOST_ADDR       = {0, 0, 0, 0};

    @SmallTest
    @Test
    public void testApfFilterIPv4() throws Exception {
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
        LinkAddress link = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 19);
@@ -796,7 +806,7 @@ public class ApfTest extends AndroidTestCase {
        apfFilter.shutdown();
    }

    @SmallTest
    @Test
    public void testApfFilterIPv6() throws Exception {
        final int[] ethTypeBlackList = {};
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
@@ -830,7 +840,7 @@ public class ApfTest extends AndroidTestCase {
        apfFilter.shutdown();
    }

    @SmallTest
    @Test
    public void testApfFilterMulticast() throws Exception {
        final byte[] unicastIpv4Addr   = {(byte)192,0,2,63};
        final byte[] broadcastIpv4Addr = {(byte)192,0,2,(byte)255};
@@ -922,7 +932,7 @@ public class ApfTest extends AndroidTestCase {
        apfFilter.shutdown();
    }

    @SmallTest
    @Test
    public void testApfFilter802_3() throws Exception {
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
        LinkAddress link = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 19);
@@ -973,7 +983,7 @@ public class ApfTest extends AndroidTestCase {
        apfFilter.shutdown();
    }

    @SmallTest
    @Test
    public void testApfFilterEthTypeBL() throws Exception {
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
        LinkAddress link = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 19);
@@ -1058,7 +1068,7 @@ public class ApfTest extends AndroidTestCase {
        assertDrop(program, garpReply());
    }

    @SmallTest
    @Test
    public void testApfFilterArp() throws Exception {
        final int[] ethTypeBlackList = {};
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
@@ -1136,7 +1146,7 @@ public class ApfTest extends AndroidTestCase {

    // Test that when ApfFilter is shown the given packet, it generates a program to filter it
    // for the given lifetime.
    private void testRaLifetime(TestApfFilter apfFilter, MockIpManagerCallback ipManagerCallback,
    private void verifyRaLifetime(TestApfFilter apfFilter, MockIpManagerCallback ipManagerCallback,
            ByteBuffer packet, int lifetime) throws IOException, ErrnoException {
        // Verify new program generated if ApfFilter witnesses RA
        ipManagerCallback.resetApfProgramWait();
@@ -1181,7 +1191,7 @@ public class ApfTest extends AndroidTestCase {
        ipManagerCallback.assertNoProgramUpdate();
    }

    @SmallTest
    @Test
    public void testApfFilterRa() throws Exception {
        MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
        final int[] ethTypeBlackList = {};
@@ -1212,7 +1222,7 @@ public class ApfTest extends AndroidTestCase {
        basePacket.put(IPV6_ALL_NODES_ADDRESS);
        assertPass(program, basePacket.array());

        testRaLifetime(apfFilter, ipManagerCallback, basePacket, ROUTER_LIFETIME);
        verifyRaLifetime(apfFilter, ipManagerCallback, basePacket, ROUTER_LIFETIME);
        verifyRaEvent(new RaEvent(ROUTER_LIFETIME, -1, -1, -1, -1, -1));

        ByteBuffer newFlowLabelPacket = ByteBuffer.wrap(new byte[ICMP6_RA_OPTION_OFFSET]);
@@ -1247,7 +1257,8 @@ public class ApfTest extends AndroidTestCase {
        prefixOptionPacket.putInt(
                ICMP6_RA_OPTION_OFFSET + ICMP6_PREFIX_OPTION_VALID_LIFETIME_OFFSET,
                PREFIX_VALID_LIFETIME);
        testRaLifetime(apfFilter, ipManagerCallback, prefixOptionPacket, PREFIX_PREFERRED_LIFETIME);
        verifyRaLifetime(
                apfFilter, ipManagerCallback, prefixOptionPacket, PREFIX_PREFERRED_LIFETIME);
        verifyRaEvent(new RaEvent(
                ROUTER_LIFETIME, PREFIX_VALID_LIFETIME, PREFIX_PREFERRED_LIFETIME, -1, -1, -1));

@@ -1259,7 +1270,7 @@ public class ApfTest extends AndroidTestCase {
        rdnssOptionPacket.put((byte)(ICMP6_4_BYTE_OPTION_LEN / 8));
        rdnssOptionPacket.putInt(
                ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_LIFETIME_OFFSET, RDNSS_LIFETIME);
        testRaLifetime(apfFilter, ipManagerCallback, rdnssOptionPacket, RDNSS_LIFETIME);
        verifyRaLifetime(apfFilter, ipManagerCallback, rdnssOptionPacket, RDNSS_LIFETIME);
        verifyRaEvent(new RaEvent(ROUTER_LIFETIME, -1, -1, -1, RDNSS_LIFETIME, -1));

        ByteBuffer routeInfoOptionPacket = ByteBuffer.wrap(
@@ -1270,7 +1281,7 @@ public class ApfTest extends AndroidTestCase {
        routeInfoOptionPacket.put((byte)(ICMP6_4_BYTE_OPTION_LEN / 8));
        routeInfoOptionPacket.putInt(
                ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_LIFETIME_OFFSET, ROUTE_LIFETIME);
        testRaLifetime(apfFilter, ipManagerCallback, routeInfoOptionPacket, ROUTE_LIFETIME);
        verifyRaLifetime(apfFilter, ipManagerCallback, routeInfoOptionPacket, ROUTE_LIFETIME);
        verifyRaEvent(new RaEvent(ROUTER_LIFETIME, -1, -1, ROUTE_LIFETIME, -1, -1));

        ByteBuffer dnsslOptionPacket = ByteBuffer.wrap(
@@ -1281,7 +1292,7 @@ public class ApfTest extends AndroidTestCase {
        dnsslOptionPacket.put((byte)(ICMP6_4_BYTE_OPTION_LEN / 8));
        dnsslOptionPacket.putInt(
                ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_LIFETIME_OFFSET, DNSSL_LIFETIME);
        testRaLifetime(apfFilter, ipManagerCallback, dnsslOptionPacket, ROUTER_LIFETIME);
        verifyRaLifetime(apfFilter, ipManagerCallback, dnsslOptionPacket, ROUTER_LIFETIME);
        verifyRaEvent(new RaEvent(ROUTER_LIFETIME, -1, -1, -1, -1, DNSSL_LIFETIME));

        // Verify that current program filters all five RAs:
@@ -1301,12 +1312,12 @@ public class ApfTest extends AndroidTestCase {
     * copy that resource into the app's data directory and return the path to it.
     */
    private String stageFile(int rawId) throws Exception {
        File file = new File(getContext().getFilesDir(), "staged_file");
        File file = new File(InstrumentationRegistry.getContext().getFilesDir(), "staged_file");
        new File(file.getParent()).mkdirs();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = getContext().getResources().openRawResource(rawId);
            in = InstrumentationRegistry.getContext().getResources().openRawResource(rawId);
            out = new FileOutputStream(file);
            Streams.copy(in, out);
        } finally {
@@ -1323,7 +1334,7 @@ public class ApfTest extends AndroidTestCase {
        buffer.position(original);
    }

    @SmallTest
    @Test
    public void testRaParsing() throws Exception {
        final int maxRandomPacketSize = 512;
        final Random r = new Random();
@@ -1343,7 +1354,7 @@ public class ApfTest extends AndroidTestCase {
        }
    }

    @SmallTest
    @Test
    public void testRaProcessing() throws Exception {
        final int maxRandomPacketSize = 512;
        final Random r = new Random();
@@ -1383,7 +1394,7 @@ public class ApfTest extends AndroidTestCase {
    private native static boolean compareBpfApf(String filter, String pcap_filename,
            byte[] apf_program);

    @SmallTest
    @Test
    public void testBroadcastAddress() throws Exception {
        assertEqualsIp("255.255.255.255", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 0));
        assertEqualsIp("0.0.0.0", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 32));
+42 −28
Original line number Diff line number Diff line
@@ -16,23 +16,36 @@

package android.net.dhcp;

import static android.net.dhcp.DhcpPacket.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.net.DhcpResults;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.metrics.DhcpErrorEvent;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.SmallTest;
import android.system.OsConstants;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.internal.util.HexDump;

import java.net.Inet4Address;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import junit.framework.TestCase;

import static android.net.dhcp.DhcpPacket.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

public class DhcpPacketTest extends TestCase {
@RunWith(AndroidJUnit4.class)
@SmallTest
public class DhcpPacketTest {

    private static Inet4Address SERVER_ADDR = v4Address("192.0.2.1");
    private static Inet4Address CLIENT_ADDR = v4Address("192.0.2.234");
@@ -46,6 +59,7 @@ public class DhcpPacketTest extends TestCase {
        return (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
    }

    @Before
    public void setUp() {
        DhcpPacket.testOverrideVendorId = "android-dhcp-???";
        DhcpPacket.testOverrideHostname = "android-01234567890abcde";
@@ -131,7 +145,7 @@ public class DhcpPacketTest extends TestCase {
        assertEquals(expectedVendorInfo, offerPacket.mVendorInfo);
    }

    @SmallTest
    @Test
    public void testDomainName() throws Exception {
        byte[] nullByte = new byte[] { 0x00 };
        byte[] twoNullBytes = new byte[] { 0x00, 0x00 };
@@ -186,7 +200,7 @@ public class DhcpPacketTest extends TestCase {
        assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis());
    }

    @SmallTest
    @Test
    public void testLeaseTime() throws Exception {
        byte[] noLease = null;
        byte[] tooShortLease = new byte[] { 0x00, 0x00 };
@@ -234,7 +248,7 @@ public class DhcpPacketTest extends TestCase {
        }
    }

    @SmallTest
    @Test
    public void testIpAddress() throws Exception {
        byte[] slash11Netmask = new byte[] { (byte) 0xff, (byte) 0xe0, 0x00, 0x00 };
        byte[] slash24Netmask = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 };
@@ -278,11 +292,11 @@ public class DhcpPacketTest extends TestCase {
        assertEquals(mtu, dhcpResults.mtu);
    }

    @SmallTest
    @Test
    public void testOffer1() throws Exception {
        // TODO: Turn all of these into golden files. This will probably require modifying
        // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
        // the golden files from the test APK's assets via mContext.getAssets().
        // TODO: Turn all of these into golden files. This will probably require using
        // android.support.test.InstrumentationRegistry for obtaining a Context object
        // to read such golden files, along with an appropriate Android.mk.
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "451001480000000080118849c0a89003c0a89ff7" +
@@ -311,7 +325,7 @@ public class DhcpPacketTest extends TestCase {
                null, "192.168.144.3", null, 7200, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testOffer2() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
@@ -343,7 +357,7 @@ public class DhcpPacketTest extends TestCase {
        assertTrue(dhcpResults.hasMeteredHint());
    }

    @SmallTest
    @Test
    public void testBadIpPacket() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -358,7 +372,7 @@ public class DhcpPacketTest extends TestCase {
        fail("Dhcp packet parsing should have failed");
    }

    @SmallTest
    @Test
    public void testBadDhcpPacket() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -377,7 +391,7 @@ public class DhcpPacketTest extends TestCase {
        fail("Dhcp packet parsing should have failed");
    }

    @SmallTest
    @Test
    public void testBadTruncatedOffer() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -406,7 +420,7 @@ public class DhcpPacketTest extends TestCase {
        fail("Dhcp packet parsing should have failed");
    }

    @SmallTest
    @Test
    public void testBadOfferWithoutACookie() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -437,7 +451,7 @@ public class DhcpPacketTest extends TestCase {
        fail("Dhcp packet parsing should have failed");
    }

    @SmallTest
    @Test
    public void testOfferWithBadCookie() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -473,7 +487,7 @@ public class DhcpPacketTest extends TestCase {
        assertEquals(Integer.toHexString(expected), Integer.toHexString(got));
    }

    @SmallTest
    @Test
    public void testTruncatedOfferPackets() throws Exception {
        final byte[] packet = HexDump.hexStringToByteArray(
            // IP header.
@@ -507,7 +521,7 @@ public class DhcpPacketTest extends TestCase {
        }
    }

    @SmallTest
    @Test
    public void testRandomPackets() throws Exception {
        final int maxRandomPacketSize = 512;
        final Random r = new Random();
@@ -547,7 +561,7 @@ public class DhcpPacketTest extends TestCase {
                null, "192.168.144.3", null, 7200, false, expectedMtu, dhcpResults);
    }

    @SmallTest
    @Test
    public void testMtu() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
@@ -583,7 +597,7 @@ public class DhcpPacketTest extends TestCase {
        checkMtu(packet, 0, mtuBytes(-1));
    }

    @SmallTest
    @Test
    public void testBadHwaddrLength() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
@@ -652,7 +666,7 @@ public class DhcpPacketTest extends TestCase {
        assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
    }

    @SmallTest
    @Test
    public void testPadAndOverloadedOptionsOffer() throws Exception {
        // A packet observed in the real world that is interesting for two reasons:
        //
@@ -691,7 +705,7 @@ public class DhcpPacketTest extends TestCase {
                null, "1.1.1.1", null, 43200, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testBug2111() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
@@ -721,7 +735,7 @@ public class DhcpPacketTest extends TestCase {
                "domain123.co.uk", "192.0.2.254", null, 49094, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testBug2136() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
@@ -754,7 +768,7 @@ public class DhcpPacketTest extends TestCase {
                "lancs.ac.uk", "10.32.255.128", null, 7200, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testUdpServerAnySourcePort() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
@@ -789,7 +803,7 @@ public class DhcpPacketTest extends TestCase {
                "wvm.edu", "10.1.105.252", null, 86400, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testUdpInvalidDstPort() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
@@ -821,7 +835,7 @@ public class DhcpPacketTest extends TestCase {
        } catch (ParseException expected) {}
    }

    @SmallTest
    @Test
    public void testMultipleRouters() throws Exception {
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
@@ -854,7 +868,7 @@ public class DhcpPacketTest extends TestCase {
                null, "192.171.189.2", null, 28800, false, 0, dhcpResults);
    }

    @SmallTest
    @Test
    public void testDiscoverPacket() throws Exception {
        short secs = 7;
        int transactionId = 0xdeadbeef;
Loading