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

Commit 905b0b64 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Fix NetworkStackCoverageTests on Q

Fix regressions in the coverage tests when run on a Q device:
- Do not attempt to verify CaptivePortalData in
  IpClientIntegrationTest on Q, since it cannot be set considering that
  the class does not exist on Q.
- Replace HexEncoding.encodeToString(byte[], boolean) with
  HexEncoding.encodeToString(byte[]) in StructNdOptPref64Test.
- Skip ModuleNetworkStackClientTest using DevSdkIgnoreRule instead of
  assumeTrue() in setUp, as the latter would still run tearDown().
- Ignore NetworkStatsUtilsTest: this is a unit test for a test utility,
  and that utility uses R APIs (so can only be used in R-only tests).
- Lazy-initialize LinkProperties with capport attributes in
  NetworkStackNotifierTest as initializing at class creation would
  crash.
- Manually create a INetworkMonitorCallbacks mock in
  NetworkMonitorTest, create capport LinkProperties on demand, and skip
  capport tests that cannot run on Q.

Test: atest NetworkStackCoverageTests
Bug: 152723363
Change-Id: I3ae98e24a542c6313e06bcc787ad5a0a2a1db723
parent 4ee76530
Loading
Loading
Loading
Loading
+16 −5
Original line number Original line Diff line number Diff line
@@ -129,12 +129,14 @@ import com.android.networkstack.arp.ArpPacket;
import com.android.server.NetworkObserverRegistry;
import com.android.server.NetworkObserverRegistry;
import com.android.server.NetworkStackService.NetworkStackServiceManager;
import com.android.server.NetworkStackService.NetworkStackServiceManager;
import com.android.server.connectivity.ipmemorystore.IpMemoryStoreService;
import com.android.server.connectivity.ipmemorystore.IpMemoryStoreService;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.HandlerUtilsKt;
import com.android.testutils.HandlerUtilsKt;
import com.android.testutils.TapPacketReader;
import com.android.testutils.TapPacketReader;


import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentCaptor;
@@ -170,6 +172,9 @@ public class IpClientIntegrationTest {
    private static final String TEST_GROUPHINT = "some grouphint";
    private static final String TEST_GROUPHINT = "some grouphint";
    private static final int TEST_LEASE_DURATION_S = 3_600; // 1 hour
    private static final int TEST_LEASE_DURATION_S = 3_600; // 1 hour


    @Rule
    public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();

    @Mock private Context mContext;
    @Mock private Context mContext;
    @Mock private ConnectivityManager mCm;
    @Mock private ConnectivityManager mCm;
    @Mock private Resources mResources;
    @Mock private Resources mResources;
@@ -1572,11 +1577,17 @@ public class IpClientIntegrationTest {


        final Uri expectedUrl = featureEnabled && serverSendsOption
        final Uri expectedUrl = featureEnabled && serverSendsOption
                ? Uri.parse(TEST_CAPTIVE_PORTAL_URL) : null;
                ? Uri.parse(TEST_CAPTIVE_PORTAL_URL) : null;
        // Wait for LinkProperties containing DHCP-obtained info, such as MTU, and ensure that the
        // Wait for LinkProperties containing DHCP-obtained info, such as MTU
        // URL is set as expected
        final ArgumentCaptor<LinkProperties> captor = ArgumentCaptor.forClass(LinkProperties.class);
        verify(mCb, timeout(TEST_TIMEOUT_MS)).onLinkPropertiesChange(argThat(lp ->
        verify(mCb, timeout(TEST_TIMEOUT_MS)).onLinkPropertiesChange(
                lp.getMtu() == testMtu
                argThat(lp -> lp.getMtu() == testMtu));
                        && Objects.equals(expectedUrl, lp.getCaptivePortalApiUrl())));

        // Ensure that the URL was set as expected in the callbacks.
        // Can't verify the URL up to Q as there is no such attribute in LinkProperties.
        if (!ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) return;
        verify(mCb).onLinkPropertiesChange(captor.capture());
        assertTrue(captor.getAllValues().stream().anyMatch(
                lp -> Objects.equals(expectedUrl, lp.getCaptivePortalApiUrl())));
    }
    }


    @Test
    @Test
+7 −7
Original line number Original line Diff line number Diff line
@@ -64,7 +64,7 @@ public class StructNdOptPref64Test {
    }
    }


    private void assertToByteBufferMatches(StructNdOptPref64 opt, String expected) {
    private void assertToByteBufferMatches(StructNdOptPref64 opt, String expected) {
        String actual = HexEncoding.encodeToString(opt.toByteBuffer().array(), false /*upperCase*/);
        String actual = HexEncoding.encodeToString(opt.toByteBuffer().array());
        assertEquals(expected, actual);
        assertEquals(expected, actual);
    }
    }


@@ -85,18 +85,18 @@ public class StructNdOptPref64Test {
    public void testParseCannedOption() throws Exception {
    public void testParseCannedOption() throws Exception {
        String hexBytes = "2602"               // type=38, len=2 (16 bytes)
        String hexBytes = "2602"               // type=38, len=2 (16 bytes)
                + "0088"                       // lifetime=136, PLC=0 (/96)
                + "0088"                       // lifetime=136, PLC=0 (/96)
                + "20010db80003000400050006";  // 2001:db8:3:4:5:6/96
                + "20010DB80003000400050006";  // 2001:db8:3:4:5:6/96
        byte[] rawBytes = HexEncoding.decode(hexBytes);
        byte[] rawBytes = HexEncoding.decode(hexBytes);
        StructNdOptPref64 opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
        StructNdOptPref64 opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
        assertPref64OptMatches(136, prefix("2001:db8:3:4:5:6::", 96), opt);
        assertPref64OptMatches(136, prefix("2001:DB8:3:4:5:6::", 96), opt);
        assertToByteBufferMatches(opt, hexBytes);
        assertToByteBufferMatches(opt, hexBytes);


        hexBytes = "2602"                      // type=38, len=2 (16 bytes)
        hexBytes = "2602"                      // type=38, len=2 (16 bytes)
                + "2752"                       // lifetime=10064, PLC=2 (/56)
                + "2752"                       // lifetime=10064, PLC=2 (/56)
                + "0064ff9b0000000000000000";  // 64:ff9b::/56
                + "0064FF9B0000000000000000";  // 64:ff9b::/56
        rawBytes = HexEncoding.decode(hexBytes);
        rawBytes = HexEncoding.decode(hexBytes);
        opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
        opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
        assertPref64OptMatches(10064, prefix("64:ff9b::", 56), opt);
        assertPref64OptMatches(10064, prefix("64:FF9B::", 56), opt);
        assertToByteBufferMatches(opt, hexBytes);
        assertToByteBufferMatches(opt, hexBytes);
    }
    }


@@ -185,12 +185,12 @@ public class StructNdOptPref64Test {
        final IpPrefix prefix2 = prefix(PREFIX2, 96);
        final IpPrefix prefix2 = prefix(PREFIX2, 96);


        StructNdOptPref64 opt = new StructNdOptPref64(prefix1, 600);
        StructNdOptPref64 opt = new StructNdOptPref64(prefix1, 600);
        assertToByteBufferMatches(opt, "2602025a0064ff9b0000000000000000");
        assertToByteBufferMatches(opt, "2602025A0064FF9B0000000000000000");
        assertEquals(new IpPrefix("64:ff9b::/56"), opt.prefix);
        assertEquals(new IpPrefix("64:ff9b::/56"), opt.prefix);
        assertEquals(600, opt.lifetime);
        assertEquals(600, opt.lifetime);


        opt = new StructNdOptPref64(prefix2, 65519);
        opt = new StructNdOptPref64(prefix2, 65519);
        assertToByteBufferMatches(opt, "2602ffe820010db80001000200030064");
        assertToByteBufferMatches(opt, "2602FFE820010DB80001000200030064");
        assertEquals(new IpPrefix("2001:db8:1:2:3:64::/96"), opt.prefix);
        assertEquals(new IpPrefix("2001:db8:1:2:3:64::/96"), opt.prefix);
        assertEquals(65512, opt.lifetime);
        assertEquals(65512, opt.lifetime);


+6 −4
Original line number Original line Diff line number Diff line
@@ -29,10 +29,10 @@ import android.os.Build
import android.os.IBinder
import android.os.IBinder
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.filters.SmallTest
import com.android.networkstack.apishim.ShimUtils
import com.android.testutils.DevSdkIgnoreRule
import org.junit.After
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.any
@@ -50,6 +50,10 @@ class ModuleNetworkStackClientTest {
    private val TEST_NETWORK = Network(43)
    private val TEST_NETWORK = Network(43)
    private val TEST_TIMEOUT_MS = 500L
    private val TEST_TIMEOUT_MS = 500L


    // ModuleNetworkStackClient is only available after Q
    @Rule @JvmField
    val mIgnoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.Q)

    @Mock
    @Mock
    private lateinit var mContext: Context
    private lateinit var mContext: Context
    @Mock
    @Mock
@@ -67,8 +71,6 @@ class ModuleNetworkStackClientTest {


    @Before
    @Before
    fun setUp() {
    fun setUp() {
        // ModuleNetworkStackClient is only available after Q
        assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q))
        MockitoAnnotations.initMocks(this)
        MockitoAnnotations.initMocks(this)
        doReturn(mConnector).`when`(mConnectorBinder).queryLocalInterface(
        doReturn(mConnector).`when`(mConnectorBinder).queryLocalInterface(
                INetworkStackConnector::class.qualifiedName!!)
                INetworkStackConnector::class.qualifiedName!!)
+7 −0
Original line number Original line Diff line number Diff line
@@ -19,7 +19,10 @@ package android.net.testutils
import android.net.NetworkStats
import android.net.NetworkStats
import android.net.NetworkStats.SET_DEFAULT
import android.net.NetworkStats.SET_DEFAULT
import android.net.NetworkStats.TAG_NONE
import android.net.NetworkStats.TAG_NONE
import android.os.Build
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.orderInsensitiveEquals
import com.android.testutils.orderInsensitiveEquals
import org.junit.Rule
import org.junit.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.runners.JUnit4
@@ -32,6 +35,10 @@ private const val TEST_START = 1194220800000L


@RunWith(JUnit4::class)
@RunWith(JUnit4::class)
class NetworkStatsUtilsTest {
class NetworkStatsUtilsTest {
    // This is a unit test for a test utility that uses R APIs
    @Rule @JvmField
    val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.Q)

    @Test
    @Test
    fun testOrderInsensitiveEquals() {
    fun testOrderInsensitiveEquals() {
        val testEntry = arrayOf(
        val testEntry = arrayOf(
+16 −12
Original line number Original line Diff line number Diff line
@@ -98,6 +98,16 @@ class NetworkStackNotifierTest {
    private lateinit var mAllNetworksCb: NetworkCallback
    private lateinit var mAllNetworksCb: NetworkCallback
    private lateinit var mDefaultNetworkCb: NetworkCallback
    private lateinit var mDefaultNetworkCb: NetworkCallback


    // Lazy-init as CaptivePortalData does not exist on Q.
    private val mTestCapportLp by lazy {
        LinkProperties().apply {
            captivePortalData = CaptivePortalData.Builder()
                    .setCaptive(false)
                    .setVenueInfoUrl(Uri.parse(TEST_VENUE_INFO_URL))
                    .build()
        }
    }

    private val TEST_NETWORK = Network(42)
    private val TEST_NETWORK = Network(42)
    private val TEST_NETWORK_TAG = TEST_NETWORK.networkHandle.toString()
    private val TEST_NETWORK_TAG = TEST_NETWORK.networkHandle.toString()
    private val TEST_SSID = "TestSsid"
    private val TEST_SSID = "TestSsid"
@@ -111,12 +121,6 @@ class NetworkStackNotifierTest {


    private val TEST_VENUE_INFO_URL = "https://testvenue.example.com/info"
    private val TEST_VENUE_INFO_URL = "https://testvenue.example.com/info"
    private val EMPTY_CAPPORT_LP = LinkProperties()
    private val EMPTY_CAPPORT_LP = LinkProperties()
    private val TEST_CAPPORT_LP = LinkProperties().apply {
        captivePortalData = CaptivePortalData.Builder()
                .setCaptive(false)
                .setVenueInfoUrl(Uri.parse(TEST_VENUE_INFO_URL))
                .build()
    }


    @Before
    @Before
    fun setUp() {
    fun setUp() {
@@ -226,7 +230,7 @@ class NetworkStackNotifierTest {
        // Venue info (CaptivePortalData) is not available for API <= Q
        // Venue info (CaptivePortalData) is not available for API <= Q
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        mNotifier.notifyCaptivePortalValidationPending(TEST_NETWORK)
        mNotifier.notifyCaptivePortalValidationPending(TEST_NETWORK)
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onDefaultNetworkAvailable(TEST_NETWORK)
        onDefaultNetworkAvailable(TEST_NETWORK)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        onCapabilitiesChanged(capabilities)
        onCapabilitiesChanged(capabilities)
@@ -245,7 +249,7 @@ class NetworkStackNotifierTest {
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        doReturn(null).`when`(mNm).getNotificationChannel(CHANNEL_VENUE_INFO)
        doReturn(null).`when`(mNm).getNotificationChannel(CHANNEL_VENUE_INFO)
        mNotifier.notifyCaptivePortalValidationPending(TEST_NETWORK)
        mNotifier.notifyCaptivePortalValidationPending(TEST_NETWORK)
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onDefaultNetworkAvailable(TEST_NETWORK)
        onDefaultNetworkAvailable(TEST_NETWORK)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        onCapabilitiesChanged(capabilities)
        onCapabilitiesChanged(capabilities)
@@ -261,7 +265,7 @@ class NetworkStackNotifierTest {
    fun testVenueInfoNotification() {
    fun testVenueInfoNotification() {
        // Venue info (CaptivePortalData) is not available for API <= Q
        // Venue info (CaptivePortalData) is not available for API <= Q
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onDefaultNetworkAvailable(TEST_NETWORK)
        onDefaultNetworkAvailable(TEST_NETWORK)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        val capabilities = NetworkCapabilities(VALIDATED_CAPABILITIES).setSSID(TEST_SSID)
        onCapabilitiesChanged(capabilities)
        onCapabilitiesChanged(capabilities)
@@ -279,7 +283,7 @@ class NetworkStackNotifierTest {
        // Venue info (CaptivePortalData) is not available for API <= Q
        // Venue info (CaptivePortalData) is not available for API <= Q
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        doReturn(null).`when`(mNm).getNotificationChannel(CHANNEL_VENUE_INFO)
        doReturn(null).`when`(mNm).getNotificationChannel(CHANNEL_VENUE_INFO)
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onDefaultNetworkAvailable(TEST_NETWORK)
        onDefaultNetworkAvailable(TEST_NETWORK)
        onCapabilitiesChanged(VALIDATED_CAPABILITIES)
        onCapabilitiesChanged(VALIDATED_CAPABILITIES)
        mLooper.processAllMessages()
        mLooper.processAllMessages()
@@ -291,7 +295,7 @@ class NetworkStackNotifierTest {
    fun testNonDefaultVenueInfoNotification() {
    fun testNonDefaultVenueInfoNotification() {
        // Venue info (CaptivePortalData) is not available for API <= Q
        // Venue info (CaptivePortalData) is not available for API <= Q
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onCapabilitiesChanged(VALIDATED_CAPABILITIES)
        onCapabilitiesChanged(VALIDATED_CAPABILITIES)
        mLooper.processAllMessages()
        mLooper.processAllMessages()


@@ -313,7 +317,7 @@ class NetworkStackNotifierTest {
    fun testUnvalidatedNetworkVenueInfoNotification() {
    fun testUnvalidatedNetworkVenueInfoNotification() {
        // Venue info (CaptivePortalData) is not available for API <= Q
        // Venue info (CaptivePortalData) is not available for API <= Q
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        assumeTrue(NetworkInformationShimImpl.useApiAboveQ())
        onLinkPropertiesChanged(TEST_CAPPORT_LP)
        onLinkPropertiesChanged(mTestCapportLp)
        onCapabilitiesChanged(EMPTY_CAPABILITIES)
        onCapabilitiesChanged(EMPTY_CAPABILITIES)
        mLooper.processAllMessages()
        mLooper.processAllMessages()


Loading