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

Commit 23b797db authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Gerrit Code Review
Browse files

Merge "Fix NetworkStackCoverageTests on Q"

parents 3b2f6cda 905b0b64
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
@@ -130,12 +130,14 @@ import com.android.networkstack.arp.ArpPacket;
import com.android.server.NetworkObserverRegistry;
import com.android.server.NetworkStackService.NetworkStackServiceManager;
import com.android.server.connectivity.ipmemorystore.IpMemoryStoreService;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.HandlerUtilsKt;
import com.android.testutils.TapPacketReader;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -171,6 +173,9 @@ public class IpClientIntegrationTest {
    private static final String TEST_GROUPHINT = "some grouphint";
    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 ConnectivityManager mCm;
    @Mock private Resources mResources;
@@ -1566,11 +1571,17 @@ public class IpClientIntegrationTest {

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

        // 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
+7 −7
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ public class StructNdOptPref64Test {
    }

    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);
    }

@@ -85,18 +85,18 @@ public class StructNdOptPref64Test {
    public void testParseCannedOption() throws Exception {
        String hexBytes = "2602"               // type=38, len=2 (16 bytes)
                + "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);
        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);

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

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

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

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

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

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

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

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

@RunWith(JUnit4::class)
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
    fun testOrderInsensitiveEquals() {
        val testEntry = arrayOf(
+16 −12
Original line number Diff line number Diff line
@@ -98,6 +98,16 @@ class NetworkStackNotifierTest {
    private lateinit var mAllNetworksCb: 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_TAG = TEST_NETWORK.networkHandle.toString()
    private val TEST_SSID = "TestSsid"
@@ -111,12 +121,6 @@ class NetworkStackNotifierTest {

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

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

Loading