diff options
Diffstat (limited to 'tests/net/common/java')
37 files changed, 0 insertions, 7087 deletions
diff --git a/tests/net/common/java/ParseExceptionTest.kt b/tests/net/common/java/ParseExceptionTest.kt deleted file mode 100644 index b702d61a9fe1..000000000000 --- a/tests/net/common/java/ParseExceptionTest.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import android.net.ParseException -import android.os.Build -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.filters.SmallTest -import com.android.testutils.DevSdkIgnoreRule -import junit.framework.Assert.assertEquals -import junit.framework.Assert.assertNull -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -@SmallTest -@RunWith(AndroidJUnit4::class) -class ParseExceptionTest { - @get:Rule - val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.R) - - @Test - fun testConstructor_WithCause() { - val testMessage = "Test message" - val base = Exception("Test") - val exception = ParseException(testMessage, base) - - assertEquals(testMessage, exception.response) - assertEquals(base, exception.cause) - } - - @Test - fun testConstructor_NoCause() { - val testMessage = "Test message" - val exception = ParseException(testMessage) - - assertEquals(testMessage, exception.response) - assertNull(exception.cause) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/CaptivePortalDataTest.kt b/tests/net/common/java/android/net/CaptivePortalDataTest.kt deleted file mode 100644 index 18a93319b271..000000000000 --- a/tests/net/common/java/android/net/CaptivePortalDataTest.kt +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.os.Build -import androidx.test.filters.SmallTest -import com.android.modules.utils.build.SdkLevel -import com.android.testutils.assertParcelSane -import com.android.testutils.assertParcelingIsLossless -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import com.android.testutils.DevSdkIgnoreRunner -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals - -@SmallTest -@RunWith(DevSdkIgnoreRunner::class) -@IgnoreUpTo(Build.VERSION_CODES.Q) -class CaptivePortalDataTest { - @Rule @JvmField - val ignoreRule = DevSdkIgnoreRule() - - private val data = CaptivePortalData.Builder() - .setRefreshTime(123L) - .setUserPortalUrl(Uri.parse("https://portal.example.com/test")) - .setVenueInfoUrl(Uri.parse("https://venue.example.com/test")) - .setSessionExtendable(true) - .setBytesRemaining(456L) - .setExpiryTime(789L) - .setCaptive(true) - .apply { - if (SdkLevel.isAtLeastS()) { - setVenueFriendlyName("venue friendly name") - } - } - .build() - - private val dataFromPasspoint = CaptivePortalData.Builder() - .setCaptive(true) - .apply { - if (SdkLevel.isAtLeastS()) { - setVenueFriendlyName("venue friendly name") - setUserPortalUrl(Uri.parse("https://tc.example.com/passpoint"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) - setVenueInfoUrl(Uri.parse("https://venue.example.com/passpoint"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) - } - } - .build() - - private fun makeBuilder() = CaptivePortalData.Builder(data) - - @Test - fun testParcelUnparcel() { - val fieldCount = if (SdkLevel.isAtLeastS()) 10 else 7 - assertParcelSane(data, fieldCount) - assertParcelSane(dataFromPasspoint, fieldCount) - - assertParcelingIsLossless(makeBuilder().setUserPortalUrl(null).build()) - assertParcelingIsLossless(makeBuilder().setVenueInfoUrl(null).build()) - } - - @Test - fun testEquals() { - assertEquals(data, makeBuilder().build()) - - assertNotEqualsAfterChange { it.setRefreshTime(456L) } - assertNotEqualsAfterChange { it.setUserPortalUrl(Uri.parse("https://example.com/")) } - assertNotEqualsAfterChange { it.setUserPortalUrl(null) } - assertNotEqualsAfterChange { it.setVenueInfoUrl(Uri.parse("https://example.com/")) } - assertNotEqualsAfterChange { it.setVenueInfoUrl(null) } - assertNotEqualsAfterChange { it.setSessionExtendable(false) } - assertNotEqualsAfterChange { it.setBytesRemaining(789L) } - assertNotEqualsAfterChange { it.setExpiryTime(12L) } - assertNotEqualsAfterChange { it.setCaptive(false) } - - if (SdkLevel.isAtLeastS()) { - assertNotEqualsAfterChange { it.setVenueFriendlyName("another friendly name") } - assertNotEqualsAfterChange { it.setVenueFriendlyName(null) } - - assertEquals(dataFromPasspoint, CaptivePortalData.Builder(dataFromPasspoint).build()) - assertNotEqualsAfterChange { it.setUserPortalUrl( - Uri.parse("https://tc.example.com/passpoint")) } - assertNotEqualsAfterChange { it.setUserPortalUrl( - Uri.parse("https://tc.example.com/passpoint"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) } - assertNotEqualsAfterChange { it.setUserPortalUrl( - Uri.parse("https://tc.example.com/other"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) } - assertNotEqualsAfterChange { it.setUserPortalUrl( - Uri.parse("https://tc.example.com/passpoint"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) } - assertNotEqualsAfterChange { it.setVenueInfoUrl( - Uri.parse("https://venue.example.com/passpoint")) } - assertNotEqualsAfterChange { it.setVenueInfoUrl( - Uri.parse("https://venue.example.com/other"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) } - assertNotEqualsAfterChange { it.setVenueInfoUrl( - Uri.parse("https://venue.example.com/passpoint"), - CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) } - } - } - - @Test - fun testUserPortalUrl() { - assertEquals(Uri.parse("https://portal.example.com/test"), data.userPortalUrl) - } - - @Test - fun testVenueInfoUrl() { - assertEquals(Uri.parse("https://venue.example.com/test"), data.venueInfoUrl) - } - - @Test - fun testIsSessionExtendable() { - assertTrue(data.isSessionExtendable) - } - - @Test - fun testByteLimit() { - assertEquals(456L, data.byteLimit) - // Test byteLimit unset. - assertEquals(-1L, CaptivePortalData.Builder(null).build().byteLimit) - } - - @Test - fun testRefreshTimeMillis() { - assertEquals(123L, data.refreshTimeMillis) - } - - @Test - fun testExpiryTimeMillis() { - assertEquals(789L, data.expiryTimeMillis) - // Test expiryTimeMillis unset. - assertEquals(-1L, CaptivePortalData.Builder(null).build().expiryTimeMillis) - } - - @Test - fun testIsCaptive() { - assertTrue(data.isCaptive) - assertFalse(makeBuilder().setCaptive(false).build().isCaptive) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - fun testVenueFriendlyName() { - assertEquals("venue friendly name", data.venueFriendlyName) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - fun testGetVenueInfoUrlSource() { - assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER, - data.venueInfoUrlSource) - assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT, - dataFromPasspoint.venueInfoUrlSource) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - fun testGetUserPortalUrlSource() { - assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER, - data.userPortalUrlSource) - assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT, - dataFromPasspoint.userPortalUrlSource) - } - - private fun CaptivePortalData.mutate(mutator: (CaptivePortalData.Builder) -> Unit) = - CaptivePortalData.Builder(this).apply { mutator(this) }.build() - - private fun assertNotEqualsAfterChange(mutator: (CaptivePortalData.Builder) -> Unit) { - assertNotEquals(data, data.mutate(mutator)) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/CaptivePortalTest.java b/tests/net/common/java/android/net/CaptivePortalTest.java deleted file mode 100644 index 15d3398d43c0..000000000000 --- a/tests/net/common/java/android/net/CaptivePortalTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static org.junit.Assert.assertEquals; - -import android.os.Build; -import android.os.RemoteException; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class CaptivePortalTest { - @Rule - public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); - - private static final int DEFAULT_TIMEOUT_MS = 5000; - private static final String TEST_PACKAGE_NAME = "com.google.android.test"; - - private final class MyCaptivePortalImpl extends ICaptivePortal.Stub { - int mCode = -1; - String mPackageName = null; - - @Override - public void appResponse(final int response) throws RemoteException { - mCode = response; - } - - @Override - public void appRequest(final int request) throws RemoteException { - mCode = request; - } - - // This is only @Override on R- - public void logEvent(int eventId, String packageName) throws RemoteException { - mCode = eventId; - mPackageName = packageName; - } - } - - private interface TestFunctor { - void useCaptivePortal(CaptivePortal o); - } - - private MyCaptivePortalImpl runCaptivePortalTest(TestFunctor f) { - final MyCaptivePortalImpl cp = new MyCaptivePortalImpl(); - f.useCaptivePortal(new CaptivePortal(cp.asBinder())); - return cp; - } - - @Test - public void testReportCaptivePortalDismissed() { - final MyCaptivePortalImpl result = - runCaptivePortalTest(c -> c.reportCaptivePortalDismissed()); - assertEquals(result.mCode, CaptivePortal.APP_RETURN_DISMISSED); - } - - @Test - public void testIgnoreNetwork() { - final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.ignoreNetwork()); - assertEquals(result.mCode, CaptivePortal.APP_RETURN_UNWANTED); - } - - @Test - public void testUseNetwork() { - final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.useNetwork()); - assertEquals(result.mCode, CaptivePortal.APP_RETURN_WANTED_AS_IS); - } - - @IgnoreUpTo(Build.VERSION_CODES.Q) - @Test - public void testReevaluateNetwork() { - final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.reevaluateNetwork()); - assertEquals(result.mCode, CaptivePortal.APP_REQUEST_REEVALUATION_REQUIRED); - } - - @IgnoreUpTo(Build.VERSION_CODES.R) - @Test - public void testLogEvent() { - /** - * From S testLogEvent is expected to do nothing but shouldn't crash (the API - * logEvent has been deprecated). - */ - final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent( - 0, - TEST_PACKAGE_NAME)); - } - - @IgnoreAfter(Build.VERSION_CODES.R) - @Test - public void testLogEvent_UntilR() { - final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent( - 42, TEST_PACKAGE_NAME)); - assertEquals(result.mCode, 42); - assertEquals(result.mPackageName, TEST_PACKAGE_NAME); - } -} diff --git a/tests/net/common/java/android/net/DependenciesTest.java b/tests/net/common/java/android/net/DependenciesTest.java deleted file mode 100644 index ac1c28a45462..000000000000 --- a/tests/net/common/java/android/net/DependenciesTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.concurrent.TimeUnit; - -/** - * A simple class that tests dependencies to java standard tools from the - * Network stack. These tests are not meant to be comprehensive tests of - * the relevant APIs : such tests belong in the relevant test suite for - * these dependencies. Instead, this just makes sure coverage is present - * by calling the methods in the exact way (or a representative way of how) - * they are called in the network stack. - */ -@RunWith(AndroidJUnit4.class) -@SmallTest -public class DependenciesTest { - // Used to in ipmemorystore's RegularMaintenanceJobService to convert - // 24 hours into seconds - @Test - public void testTimeUnit() { - final int hours = 24; - final long inSeconds = TimeUnit.HOURS.toMillis(hours); - assertEquals(inSeconds, hours * 60 * 60 * 1000); - } - - private byte[] makeTrivialArray(final int size) { - final byte[] src = new byte[size]; - for (int i = 0; i < size; ++i) { - src[i] = (byte) i; - } - return src; - } - - // Used in ApfFilter to find an IP address from a byte array - @Test - public void testArrays() { - final int size = 128; - final byte[] src = makeTrivialArray(size); - - // Test copy - final int copySize = 16; - final int offset = 24; - final byte[] expected = new byte[copySize]; - for (int i = 0; i < copySize; ++i) { - expected[i] = (byte) (offset + i); - } - - final byte[] copy = Arrays.copyOfRange(src, offset, offset + copySize); - assertArrayEquals(expected, copy); - assertArrayEquals(new byte[0], Arrays.copyOfRange(src, size, size)); - } - - // Used mainly in the Dhcp code - @Test - public void testCopyOf() { - final byte[] src = makeTrivialArray(128); - final byte[] copy = Arrays.copyOf(src, src.length); - assertArrayEquals(src, copy); - assertFalse(src == copy); - - assertArrayEquals(new byte[0], Arrays.copyOf(src, 0)); - - final int excess = 16; - final byte[] biggerCopy = Arrays.copyOf(src, src.length + excess); - for (int i = src.length; i < src.length + excess; ++i) { - assertEquals(0, biggerCopy[i]); - } - for (int i = src.length - 1; i >= 0; --i) { - assertEquals(src[i], biggerCopy[i]); - } - } - - // Used mainly in DnsUtils but also various other places - @Test - public void testAsList() { - final int size = 24; - final Object[] src = new Object[size]; - final ArrayList<Object> expected = new ArrayList<>(size); - for (int i = 0; i < size; ++i) { - final Object o = new Object(); - src[i] = o; - expected.add(o); - } - assertEquals(expected, Arrays.asList(src)); - } -} diff --git a/tests/net/common/java/android/net/DhcpInfoTest.java b/tests/net/common/java/android/net/DhcpInfoTest.java deleted file mode 100644 index ab4726bab573..000000000000 --- a/tests/net/common/java/android/net/DhcpInfoTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static com.android.net.module.util.Inet4AddressUtils.inet4AddressToIntHTL; -import static com.android.testutils.MiscAsserts.assertFieldCountEquals; -import static com.android.testutils.ParcelUtils.parcelingRoundTrip; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import android.annotation.Nullable; - -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.Inet4Address; -import java.net.InetAddress; - -@RunWith(AndroidJUnit4.class) -public class DhcpInfoTest { - private static final String STR_ADDR1 = "255.255.255.255"; - private static final String STR_ADDR2 = "127.0.0.1"; - private static final String STR_ADDR3 = "192.168.1.1"; - private static final String STR_ADDR4 = "192.168.1.0"; - private static final int LEASE_TIME = 9999; - - private int ipToInteger(String ipString) throws Exception { - return inet4AddressToIntHTL((Inet4Address) InetAddress.getByName(ipString)); - } - - private DhcpInfo createDhcpInfoObject() throws Exception { - final DhcpInfo dhcpInfo = new DhcpInfo(); - dhcpInfo.ipAddress = ipToInteger(STR_ADDR1); - dhcpInfo.gateway = ipToInteger(STR_ADDR2); - dhcpInfo.netmask = ipToInteger(STR_ADDR3); - dhcpInfo.dns1 = ipToInteger(STR_ADDR4); - dhcpInfo.dns2 = ipToInteger(STR_ADDR4); - dhcpInfo.serverAddress = ipToInteger(STR_ADDR2); - dhcpInfo.leaseDuration = LEASE_TIME; - return dhcpInfo; - } - - @Test - public void testConstructor() { - new DhcpInfo(); - } - - @Test - public void testToString() throws Exception { - final String expectedDefault = "ipaddr 0.0.0.0 gateway 0.0.0.0 netmask 0.0.0.0 " - + "dns1 0.0.0.0 dns2 0.0.0.0 DHCP server 0.0.0.0 lease 0 seconds"; - - DhcpInfo dhcpInfo = new DhcpInfo(); - - // Test default string. - assertEquals(expectedDefault, dhcpInfo.toString()); - - dhcpInfo = createDhcpInfoObject(); - - final String expected = "ipaddr " + STR_ADDR1 + " gateway " + STR_ADDR2 + " netmask " - + STR_ADDR3 + " dns1 " + STR_ADDR4 + " dns2 " + STR_ADDR4 + " DHCP server " - + STR_ADDR2 + " lease " + LEASE_TIME + " seconds"; - // Test with new values - assertEquals(expected, dhcpInfo.toString()); - } - - private boolean dhcpInfoEquals(@Nullable DhcpInfo left, @Nullable DhcpInfo right) { - if (left == null && right == null) return true; - - if (left == null || right == null) return false; - - return left.ipAddress == right.ipAddress - && left.gateway == right.gateway - && left.netmask == right.netmask - && left.dns1 == right.dns1 - && left.dns2 == right.dns2 - && left.serverAddress == right.serverAddress - && left.leaseDuration == right.leaseDuration; - } - - @Test - public void testParcelDhcpInfo() throws Exception { - // Cannot use assertParcelSane() here because this requires .equals() to work as - // defined, but DhcpInfo has a different legacy behavior that we cannot change. - final DhcpInfo dhcpInfo = createDhcpInfoObject(); - assertFieldCountEquals(7, DhcpInfo.class); - - final DhcpInfo dhcpInfoRoundTrip = parcelingRoundTrip(dhcpInfo); - assertTrue(dhcpInfoEquals(null, null)); - assertFalse(dhcpInfoEquals(null, dhcpInfoRoundTrip)); - assertFalse(dhcpInfoEquals(dhcpInfo, null)); - assertTrue(dhcpInfoEquals(dhcpInfo, dhcpInfoRoundTrip)); - } -} diff --git a/tests/net/common/java/android/net/IpPrefixTest.java b/tests/net/common/java/android/net/IpPrefixTest.java deleted file mode 100644 index 50ecb428359e..000000000000 --- a/tests/net/common/java/android/net/IpPrefixTest.java +++ /dev/null @@ -1,374 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static com.android.testutils.MiscAsserts.assertEqualBothWays; -import static com.android.testutils.MiscAsserts.assertFieldCountEquals; -import static com.android.testutils.MiscAsserts.assertNotEqualEitherWay; -import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.InetAddress; -import java.util.Random; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class IpPrefixTest { - - private static InetAddress address(String addr) { - return InetAddress.parseNumericAddress(addr); - } - - // Explicitly cast everything to byte because "error: possible loss of precision". - private static final byte[] IPV4_BYTES = { (byte) 192, (byte) 0, (byte) 2, (byte) 4}; - private static final byte[] IPV6_BYTES = { - (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, - (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef, - (byte) 0x0f, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa0 - }; - - @Test - public void testConstructor() { - IpPrefix p; - try { - p = new IpPrefix((byte[]) null, 9); - fail("Expected NullPointerException: null byte array"); - } catch (RuntimeException expected) { } - - try { - p = new IpPrefix((InetAddress) null, 10); - fail("Expected NullPointerException: null InetAddress"); - } catch (RuntimeException expected) { } - - try { - p = new IpPrefix((String) null); - fail("Expected NullPointerException: null String"); - } catch (RuntimeException expected) { } - - - try { - byte[] b2 = {1, 2, 3, 4, 5}; - p = new IpPrefix(b2, 29); - fail("Expected IllegalArgumentException: invalid array length"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("1.2.3.4"); - fail("Expected IllegalArgumentException: no prefix length"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("1.2.3.4/"); - fail("Expected IllegalArgumentException: empty prefix length"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("foo/32"); - fail("Expected IllegalArgumentException: invalid address"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("1/32"); - fail("Expected IllegalArgumentException: deprecated IPv4 format"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("1.2.3.256/32"); - fail("Expected IllegalArgumentException: invalid IPv4 address"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("foo/32"); - fail("Expected IllegalArgumentException: non-address"); - } catch (IllegalArgumentException expected) { } - - try { - p = new IpPrefix("f00:::/32"); - fail("Expected IllegalArgumentException: invalid IPv6 address"); - } catch (IllegalArgumentException expected) { } - - p = new IpPrefix("/64"); - assertEquals("::/64", p.toString()); - - p = new IpPrefix("/128"); - assertEquals("::1/128", p.toString()); - - p = new IpPrefix("[2001:db8::123]/64"); - assertEquals("2001:db8::/64", p.toString()); - } - - @Test - public void testTruncation() { - IpPrefix p; - - p = new IpPrefix(IPV4_BYTES, 32); - assertEquals("192.0.2.4/32", p.toString()); - - p = new IpPrefix(IPV4_BYTES, 29); - assertEquals("192.0.2.0/29", p.toString()); - - p = new IpPrefix(IPV4_BYTES, 8); - assertEquals("192.0.0.0/8", p.toString()); - - p = new IpPrefix(IPV4_BYTES, 0); - assertEquals("0.0.0.0/0", p.toString()); - - try { - p = new IpPrefix(IPV4_BYTES, 33); - fail("Expected IllegalArgumentException: invalid prefix length"); - } catch (RuntimeException expected) { } - - try { - p = new IpPrefix(IPV4_BYTES, 128); - fail("Expected IllegalArgumentException: invalid prefix length"); - } catch (RuntimeException expected) { } - - try { - p = new IpPrefix(IPV4_BYTES, -1); - fail("Expected IllegalArgumentException: negative prefix length"); - } catch (RuntimeException expected) { } - - p = new IpPrefix(IPV6_BYTES, 128); - assertEquals("2001:db8:dead:beef:f00::a0/128", p.toString()); - - p = new IpPrefix(IPV6_BYTES, 122); - assertEquals("2001:db8:dead:beef:f00::80/122", p.toString()); - - p = new IpPrefix(IPV6_BYTES, 64); - assertEquals("2001:db8:dead:beef::/64", p.toString()); - - p = new IpPrefix(IPV6_BYTES, 3); - assertEquals("2000::/3", p.toString()); - - p = new IpPrefix(IPV6_BYTES, 0); - assertEquals("::/0", p.toString()); - - try { - p = new IpPrefix(IPV6_BYTES, -1); - fail("Expected IllegalArgumentException: negative prefix length"); - } catch (RuntimeException expected) { } - - try { - p = new IpPrefix(IPV6_BYTES, 129); - fail("Expected IllegalArgumentException: negative prefix length"); - } catch (RuntimeException expected) { } - - } - - @Test - public void testEquals() { - IpPrefix p1, p2; - - p1 = new IpPrefix("192.0.2.251/23"); - p2 = new IpPrefix(new byte[]{(byte) 192, (byte) 0, (byte) 2, (byte) 251}, 23); - assertEqualBothWays(p1, p2); - - p1 = new IpPrefix("192.0.2.5/23"); - assertEqualBothWays(p1, p2); - - p1 = new IpPrefix("192.0.2.5/24"); - assertNotEqualEitherWay(p1, p2); - - p1 = new IpPrefix("192.0.4.5/23"); - assertNotEqualEitherWay(p1, p2); - - - p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122"); - p2 = new IpPrefix(IPV6_BYTES, 122); - assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString()); - assertEqualBothWays(p1, p2); - - p1 = new IpPrefix("2001:db8:dead:beef:f00::bf/122"); - assertEqualBothWays(p1, p2); - - p1 = new IpPrefix("2001:db8:dead:beef:f00::8:0/123"); - assertNotEqualEitherWay(p1, p2); - - p1 = new IpPrefix("2001:db8:dead:beef::/122"); - assertNotEqualEitherWay(p1, p2); - - // 192.0.2.4/32 != c000:0204::/32. - byte[] ipv6bytes = new byte[16]; - System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length); - p1 = new IpPrefix(ipv6bytes, 32); - assertEqualBothWays(p1, new IpPrefix("c000:0204::/32")); - - p2 = new IpPrefix(IPV4_BYTES, 32); - assertNotEqualEitherWay(p1, p2); - } - - @Test - public void testContainsInetAddress() { - IpPrefix p = new IpPrefix("2001:db8:f00::ace:d00d/127"); - assertTrue(p.contains(address("2001:db8:f00::ace:d00c"))); - assertTrue(p.contains(address("2001:db8:f00::ace:d00d"))); - assertFalse(p.contains(address("2001:db8:f00::ace:d00e"))); - assertFalse(p.contains(address("2001:db8:f00::bad:d00d"))); - assertFalse(p.contains(address("2001:4868:4860::8888"))); - assertFalse(p.contains(address("8.8.8.8"))); - - p = new IpPrefix("192.0.2.0/23"); - assertTrue(p.contains(address("192.0.2.43"))); - assertTrue(p.contains(address("192.0.3.21"))); - assertFalse(p.contains(address("192.0.0.21"))); - assertFalse(p.contains(address("8.8.8.8"))); - assertFalse(p.contains(address("2001:4868:4860::8888"))); - - IpPrefix ipv6Default = new IpPrefix("::/0"); - assertTrue(ipv6Default.contains(address("2001:db8::f00"))); - assertFalse(ipv6Default.contains(address("192.0.2.1"))); - - IpPrefix ipv4Default = new IpPrefix("0.0.0.0/0"); - assertTrue(ipv4Default.contains(address("255.255.255.255"))); - assertTrue(ipv4Default.contains(address("192.0.2.1"))); - assertFalse(ipv4Default.contains(address("2001:db8::f00"))); - } - - @Test - public void testContainsIpPrefix() { - assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("0.0.0.0/0"))); - assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/0"))); - assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/8"))); - assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/24"))); - assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/23"))); - - assertTrue(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("1.2.3.4/8"))); - assertTrue(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("1.254.12.9/8"))); - assertTrue(new IpPrefix("1.2.3.4/21").containsPrefix(new IpPrefix("1.2.3.4/21"))); - assertTrue(new IpPrefix("1.2.3.4/32").containsPrefix(new IpPrefix("1.2.3.4/32"))); - - assertTrue(new IpPrefix("1.2.3.4/20").containsPrefix(new IpPrefix("1.2.3.0/24"))); - - assertFalse(new IpPrefix("1.2.3.4/32").containsPrefix(new IpPrefix("1.2.3.5/32"))); - assertFalse(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("2.2.3.4/8"))); - assertFalse(new IpPrefix("0.0.0.0/16").containsPrefix(new IpPrefix("0.0.0.0/15"))); - assertFalse(new IpPrefix("100.0.0.0/8").containsPrefix(new IpPrefix("99.0.0.0/8"))); - - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("::/0"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/1"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("3d8a:661:a0::770/8"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/8"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/64"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/113"))); - assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/128"))); - - assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix( - new IpPrefix("2001:db8:f00::ace:d00d/64"))); - assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix( - new IpPrefix("2001:db8:f00::ace:d00d/120"))); - assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix( - new IpPrefix("2001:db8:f00::ace:d00d/32"))); - assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix( - new IpPrefix("2006:db8:f00::ace:d00d/96"))); - - assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/128").containsPrefix( - new IpPrefix("2001:db8:f00::ace:d00d/128"))); - assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/100").containsPrefix( - new IpPrefix("2001:db8:f00::ace:ccaf/110"))); - - assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/128").containsPrefix( - new IpPrefix("2001:db8:f00::ace:d00e/128"))); - assertFalse(new IpPrefix("::/30").containsPrefix(new IpPrefix("::/29"))); - } - - @Test - public void testHashCode() { - IpPrefix p = new IpPrefix(new byte[4], 0); - Random random = new Random(); - for (int i = 0; i < 100; i++) { - final IpPrefix oldP = p; - if (random.nextBoolean()) { - // IPv4. - byte[] b = new byte[4]; - random.nextBytes(b); - p = new IpPrefix(b, random.nextInt(33)); - } else { - // IPv6. - byte[] b = new byte[16]; - random.nextBytes(b); - p = new IpPrefix(b, random.nextInt(129)); - } - if (p.equals(oldP)) { - assertEquals(p.hashCode(), oldP.hashCode()); - } - if (p.hashCode() != oldP.hashCode()) { - assertNotEquals(p, oldP); - } - } - } - - @Test - public void testHashCodeIsNotConstant() { - IpPrefix[] prefixes = { - new IpPrefix("2001:db8:f00::ace:d00d/127"), - new IpPrefix("192.0.2.0/23"), - new IpPrefix("::/0"), - new IpPrefix("0.0.0.0/0"), - }; - for (int i = 0; i < prefixes.length; i++) { - for (int j = i + 1; j < prefixes.length; j++) { - assertNotEquals(prefixes[i].hashCode(), prefixes[j].hashCode()); - } - } - } - - @Test - public void testMappedAddressesAreBroken() { - // 192.0.2.0/24 != ::ffff:c000:0204/120, but because we use InetAddress, - // we are unable to comprehend that. - byte[] ipv6bytes = { - (byte) 0, (byte) 0, (byte) 0, (byte) 0, - (byte) 0, (byte) 0, (byte) 0, (byte) 0, - (byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff, - (byte) 192, (byte) 0, (byte) 2, (byte) 0}; - IpPrefix p = new IpPrefix(ipv6bytes, 120); - assertEquals(16, p.getRawAddress().length); // Fine. - assertArrayEquals(ipv6bytes, p.getRawAddress()); // Fine. - - // Broken. - assertEquals("192.0.2.0/120", p.toString()); - assertEquals(InetAddress.parseNumericAddress("192.0.2.0"), p.getAddress()); - } - - @Test - public void testParceling() { - IpPrefix p; - - p = new IpPrefix("2001:4860:db8::/64"); - assertParcelingIsLossless(p); - assertTrue(p.isIPv6()); - - p = new IpPrefix("192.0.2.0/25"); - assertParcelingIsLossless(p); - assertTrue(p.isIPv4()); - - assertFieldCountEquals(2, IpPrefix.class); - } -} diff --git a/tests/net/common/java/android/net/KeepalivePacketDataTest.kt b/tests/net/common/java/android/net/KeepalivePacketDataTest.kt deleted file mode 100644 index f464ec6cf0e5..000000000000 --- a/tests/net/common/java/android/net/KeepalivePacketDataTest.kt +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.net - -import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS -import android.net.InvalidPacketException.ERROR_INVALID_PORT -import android.os.Build -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import java.net.InetAddress -import java.util.Arrays -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Assert.fail -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class KeepalivePacketDataTest { - @Rule @JvmField - val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() - - private val INVALID_PORT = 65537 - private val TEST_DST_PORT = 4244 - private val TEST_SRC_PORT = 4243 - - private val TESTBYTES = byteArrayOf(12, 31, 22, 44) - private val TEST_SRC_ADDRV4 = "198.168.0.2".address() - private val TEST_DST_ADDRV4 = "198.168.0.1".address() - private val TEST_ADDRV6 = "2001:db8::1".address() - - private fun String.address() = InetAddresses.parseNumericAddress(this) - - // Add for test because constructor of KeepalivePacketData is protected. - private inner class TestKeepalivePacketData( - srcAddress: InetAddress? = TEST_SRC_ADDRV4, - srcPort: Int = TEST_SRC_PORT, - dstAddress: InetAddress? = TEST_DST_ADDRV4, - dstPort: Int = TEST_DST_PORT, - data: ByteArray = TESTBYTES - ) : KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, data) - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testConstructor() { - var data: TestKeepalivePacketData - - try { - data = TestKeepalivePacketData(srcAddress = null) - fail("Null src address should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) - } - - try { - data = TestKeepalivePacketData(dstAddress = null) - fail("Null dst address should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) - } - - try { - data = TestKeepalivePacketData(dstAddress = TEST_ADDRV6) - fail("Ip family mismatched should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) - } - - try { - data = TestKeepalivePacketData(srcPort = INVALID_PORT) - fail("Invalid srcPort should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_PORT) - } - - try { - data = TestKeepalivePacketData(dstPort = INVALID_PORT) - fail("Invalid dstPort should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_PORT) - } - } - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress) - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress) - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort) - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort) - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet)) -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/LinkAddressTest.java b/tests/net/common/java/android/net/LinkAddressTest.java deleted file mode 100644 index 2cf3cf9c11da..000000000000 --- a/tests/net/common/java/android/net/LinkAddressTest.java +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static android.system.OsConstants.IFA_F_DADFAILED; -import static android.system.OsConstants.IFA_F_DEPRECATED; -import static android.system.OsConstants.IFA_F_OPTIMISTIC; -import static android.system.OsConstants.IFA_F_PERMANENT; -import static android.system.OsConstants.IFA_F_TEMPORARY; -import static android.system.OsConstants.IFA_F_TENTATIVE; -import static android.system.OsConstants.RT_SCOPE_HOST; -import static android.system.OsConstants.RT_SCOPE_LINK; -import static android.system.OsConstants.RT_SCOPE_SITE; -import static android.system.OsConstants.RT_SCOPE_UNIVERSE; - -import static com.android.testutils.MiscAsserts.assertEqualBothWays; -import static com.android.testutils.MiscAsserts.assertFieldCountEquals; -import static com.android.testutils.MiscAsserts.assertNotEqualEitherWay; -import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import android.os.Build; -import android.os.SystemClock; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.Inet4Address; -import java.net.Inet6Address; -import java.net.InetAddress; -import java.net.InterfaceAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Arrays; -import java.util.List; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class LinkAddressTest { - @Rule - public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); - - private static final String V4 = "192.0.2.1"; - private static final String V6 = "2001:db8::1"; - private static final InetAddress V4_ADDRESS = InetAddresses.parseNumericAddress(V4); - private static final InetAddress V6_ADDRESS = InetAddresses.parseNumericAddress(V6); - - @Test - public void testConstants() { - // RT_SCOPE_UNIVERSE = 0, but all the other constants should be nonzero. - assertNotEquals(0, RT_SCOPE_HOST); - assertNotEquals(0, RT_SCOPE_LINK); - assertNotEquals(0, RT_SCOPE_SITE); - - assertNotEquals(0, IFA_F_DEPRECATED); - assertNotEquals(0, IFA_F_PERMANENT); - assertNotEquals(0, IFA_F_TENTATIVE); - } - - @Test - public void testConstructors() throws SocketException { - LinkAddress address; - - // Valid addresses work as expected. - address = new LinkAddress(V4_ADDRESS, 25); - assertEquals(V4_ADDRESS, address.getAddress()); - assertEquals(25, address.getPrefixLength()); - assertEquals(0, address.getFlags()); - assertEquals(RT_SCOPE_UNIVERSE, address.getScope()); - assertTrue(address.isIpv4()); - - address = new LinkAddress(V6_ADDRESS, 127); - assertEquals(V6_ADDRESS, address.getAddress()); - assertEquals(127, address.getPrefixLength()); - assertEquals(0, address.getFlags()); - assertEquals(RT_SCOPE_UNIVERSE, address.getScope()); - assertTrue(address.isIpv6()); - - // Nonsensical flags/scopes or combinations thereof are acceptable. - address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK); - assertEquals(V6_ADDRESS, address.getAddress()); - assertEquals(64, address.getPrefixLength()); - assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags()); - assertEquals(RT_SCOPE_LINK, address.getScope()); - assertTrue(address.isIpv6()); - - address = new LinkAddress(V4 + "/23", 123, 456); - assertEquals(V4_ADDRESS, address.getAddress()); - assertEquals(23, address.getPrefixLength()); - assertEquals(123, address.getFlags()); - assertEquals(456, address.getScope()); - assertTrue(address.isIpv4()); - - address = new LinkAddress("/64", 1 /* flags */, 2 /* scope */); - assertEquals(Inet6Address.LOOPBACK, address.getAddress()); - assertEquals(64, address.getPrefixLength()); - assertEquals(1, address.getFlags()); - assertEquals(2, address.getScope()); - assertTrue(address.isIpv6()); - - address = new LinkAddress("[2001:db8::123]/64", 3 /* flags */, 4 /* scope */); - assertEquals(InetAddresses.parseNumericAddress("2001:db8::123"), address.getAddress()); - assertEquals(64, address.getPrefixLength()); - assertEquals(3, address.getFlags()); - assertEquals(4, address.getScope()); - assertTrue(address.isIpv6()); - - // InterfaceAddress doesn't have a constructor. Fetch some from an interface. - List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses(); - - // We expect to find 127.0.0.1/8 and ::1/128, in any order. - LinkAddress ipv4Loopback, ipv6Loopback; - assertEquals(2, addrs.size()); - if (addrs.get(0).getAddress() instanceof Inet4Address) { - ipv4Loopback = new LinkAddress(addrs.get(0)); - ipv6Loopback = new LinkAddress(addrs.get(1)); - } else { - ipv4Loopback = new LinkAddress(addrs.get(1)); - ipv6Loopback = new LinkAddress(addrs.get(0)); - } - - assertEquals(InetAddresses.parseNumericAddress("127.0.0.1"), ipv4Loopback.getAddress()); - assertEquals(8, ipv4Loopback.getPrefixLength()); - - assertEquals(InetAddresses.parseNumericAddress("::1"), ipv6Loopback.getAddress()); - assertEquals(128, ipv6Loopback.getPrefixLength()); - - // Null addresses are rejected. - try { - address = new LinkAddress(null, 24); - fail("Null InetAddress should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE); - fail("Null string should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress((InterfaceAddress) null); - fail("Null string should cause NullPointerException"); - } catch(NullPointerException expected) {} - - // Invalid prefix lengths are rejected. - try { - address = new LinkAddress(V4_ADDRESS, -1); - fail("Negative IPv4 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress(V6_ADDRESS, -1); - fail("Negative IPv6 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress(V4_ADDRESS, 33); - fail("/33 IPv4 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE); - fail("/33 IPv4 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - - try { - address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE); - fail("/129 IPv6 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE); - fail("/129 IPv6 prefix length should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - // Multicast addresses are rejected. - try { - address = new LinkAddress("224.0.0.2/32"); - fail("IPv4 multicast address should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - - try { - address = new LinkAddress("ff02::1/128"); - fail("IPv6 multicast address should cause IllegalArgumentException"); - } catch(IllegalArgumentException expected) {} - } - - @Test - public void testAddressScopes() { - assertEquals(RT_SCOPE_HOST, new LinkAddress("::/128").getScope()); - assertEquals(RT_SCOPE_HOST, new LinkAddress("0.0.0.0/32").getScope()); - - assertEquals(RT_SCOPE_LINK, new LinkAddress("::1/128").getScope()); - assertEquals(RT_SCOPE_LINK, new LinkAddress("127.0.0.5/8").getScope()); - assertEquals(RT_SCOPE_LINK, new LinkAddress("fe80::ace:d00d/64").getScope()); - assertEquals(RT_SCOPE_LINK, new LinkAddress("169.254.5.12/16").getScope()); - - assertEquals(RT_SCOPE_SITE, new LinkAddress("fec0::dead/64").getScope()); - - assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("10.1.2.3/21").getScope()); - assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("192.0.2.1/25").getScope()); - assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("2001:db8::/64").getScope()); - assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("5000::/127").getScope()); - } - - private void assertIsSameAddressAs(LinkAddress l1, LinkAddress l2) { - assertTrue(l1 + " unexpectedly does not have same address as " + l2, - l1.isSameAddressAs(l2)); - assertTrue(l2 + " unexpectedly does not have same address as " + l1, - l2.isSameAddressAs(l1)); - } - - private void assertIsNotSameAddressAs(LinkAddress l1, LinkAddress l2) { - assertFalse(l1 + " unexpectedly has same address as " + l2, - l1.isSameAddressAs(l2)); - assertFalse(l2 + " unexpectedly has same address as " + l1, - l1.isSameAddressAs(l2)); - } - - @Test - public void testEqualsAndSameAddressAs() { - LinkAddress l1, l2, l3; - - l1 = new LinkAddress("2001:db8::1/64"); - l2 = new LinkAddress("2001:db8::1/64"); - assertEqualBothWays(l1, l2); - assertIsSameAddressAs(l1, l2); - - l2 = new LinkAddress("2001:db8::1/65"); - assertNotEqualEitherWay(l1, l2); - assertIsNotSameAddressAs(l1, l2); - - l2 = new LinkAddress("2001:db8::2/64"); - assertNotEqualEitherWay(l1, l2); - assertIsNotSameAddressAs(l1, l2); - - - l1 = new LinkAddress("192.0.2.1/24"); - l2 = new LinkAddress("192.0.2.1/24"); - assertEqualBothWays(l1, l2); - assertIsSameAddressAs(l1, l2); - - l2 = new LinkAddress("192.0.2.1/23"); - assertNotEqualEitherWay(l1, l2); - assertIsNotSameAddressAs(l1, l2); - - l2 = new LinkAddress("192.0.2.2/24"); - assertNotEqualEitherWay(l1, l2); - assertIsNotSameAddressAs(l1, l2); - - - // Check equals() and isSameAddressAs() on identical addresses with different flags. - l1 = new LinkAddress(V6_ADDRESS, 64); - l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE); - assertEqualBothWays(l1, l2); - assertIsSameAddressAs(l1, l2); - - l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE); - assertNotEqualEitherWay(l1, l2); - assertIsSameAddressAs(l1, l2); - - // Check equals() and isSameAddressAs() on identical addresses with different scope. - l1 = new LinkAddress(V4_ADDRESS, 24); - l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE); - assertEqualBothWays(l1, l2); - assertIsSameAddressAs(l1, l2); - - l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST); - assertNotEqualEitherWay(l1, l2); - assertIsSameAddressAs(l1, l2); - - // Addresses with the same start or end bytes aren't equal between families. - l1 = new LinkAddress("32.1.13.184/24"); - l2 = new LinkAddress("2001:db8::1/24"); - l3 = new LinkAddress("::2001:db8/24"); - - byte[] ipv4Bytes = l1.getAddress().getAddress(); - byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4); - byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16); - assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes)); - assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes)); - - assertNotEqualEitherWay(l1, l2); - assertIsNotSameAddressAs(l1, l2); - - assertNotEqualEitherWay(l1, l3); - assertIsNotSameAddressAs(l1, l3); - - // Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address. - // TODO: Investigate fixing this. - String addressString = V4 + "/24"; - l1 = new LinkAddress(addressString); - l2 = new LinkAddress("::ffff:" + addressString); - assertEqualBothWays(l1, l2); - assertIsSameAddressAs(l1, l2); - } - - @Test - public void testHashCode() { - LinkAddress l1, l2; - - l1 = new LinkAddress(V4_ADDRESS, 23); - l2 = new LinkAddress(V4_ADDRESS, 23, 0, RT_SCOPE_HOST); - assertNotEquals(l1.hashCode(), l2.hashCode()); - - l1 = new LinkAddress(V6_ADDRESS, 128); - l2 = new LinkAddress(V6_ADDRESS, 128, IFA_F_TENTATIVE, RT_SCOPE_UNIVERSE); - assertNotEquals(l1.hashCode(), l2.hashCode()); - } - - @Test - public void testParceling() { - LinkAddress l; - - l = new LinkAddress(V6_ADDRESS, 64, 123, 456); - assertParcelingIsLossless(l); - - l = new LinkAddress(V4 + "/28", IFA_F_PERMANENT, RT_SCOPE_LINK); - assertParcelingIsLossless(l); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testLifetimeParceling() { - final LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 123, 456, 1L, 3600000L); - assertParcelingIsLossless(l); - } - - @Test @IgnoreAfter(Build.VERSION_CODES.Q) - public void testFieldCount_Q() { - assertFieldCountEquals(4, LinkAddress.class); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testFieldCount() { - // Make sure any new field is covered by the above parceling tests when changing this number - assertFieldCountEquals(6, LinkAddress.class); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testDeprecationTime() { - try { - new LinkAddress(V6_ADDRESS, 64, 0, 456, - LinkAddress.LIFETIME_UNKNOWN, 100000L); - fail("Only one time provided should cause exception"); - } catch (IllegalArgumentException expected) { } - - try { - new LinkAddress(V6_ADDRESS, 64, 0, 456, - 200000L, 100000L); - fail("deprecation time later than expiration time should cause exception"); - } catch (IllegalArgumentException expected) { } - - try { - new LinkAddress(V6_ADDRESS, 64, 0, 456, - -2, 100000L); - fail("negative deprecation time should cause exception"); - } catch (IllegalArgumentException expected) { } - - LinkAddress addr = new LinkAddress(V6_ADDRESS, 64, 0, 456, 100000L, 200000L); - assertEquals(100000L, addr.getDeprecationTime()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testExpirationTime() { - try { - new LinkAddress(V6_ADDRESS, 64, 0, 456, - 200000L, LinkAddress.LIFETIME_UNKNOWN); - fail("Only one time provided should cause exception"); - } catch (IllegalArgumentException expected) { } - - try { - new LinkAddress(V6_ADDRESS, 64, 0, 456, - 100000L, -2); - fail("negative expiration time should cause exception"); - } catch (IllegalArgumentException expected) { } - - LinkAddress addr = new LinkAddress(V6_ADDRESS, 64, 0, 456, 100000L, 200000L); - assertEquals(200000L, addr.getExpirationTime()); - } - - @Test - public void testGetFlags() { - LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 123, RT_SCOPE_HOST); - assertEquals(123, l.getFlags()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testGetFlags_Deprecation() { - // Test if deprecated bit was added/remove automatically based on the provided deprecation - // time - LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_HOST, - 1L, LinkAddress.LIFETIME_PERMANENT); - // Check if the flag is added automatically. - assertTrue((l.getFlags() & IFA_F_DEPRECATED) != 0); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_HOST, - SystemClock.elapsedRealtime() + 100000L, LinkAddress.LIFETIME_PERMANENT); - // Check if the flag is removed automatically. - assertTrue((l.getFlags() & IFA_F_DEPRECATED) == 0); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_HOST, - LinkAddress.LIFETIME_PERMANENT, LinkAddress.LIFETIME_PERMANENT); - // Check if the permanent flag is added. - assertTrue((l.getFlags() & IFA_F_PERMANENT) != 0); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_HOST, - 1000L, SystemClock.elapsedRealtime() + 100000L); - // Check if the permanent flag is removed - assertTrue((l.getFlags() & IFA_F_PERMANENT) == 0); - } - - private void assertGlobalPreferred(LinkAddress l, String msg) { - assertTrue(msg, l.isGlobalPreferred()); - } - - private void assertNotGlobalPreferred(LinkAddress l, String msg) { - assertFalse(msg, l.isGlobalPreferred()); - } - - @Test - public void testIsGlobalPreferred() { - LinkAddress l; - - l = new LinkAddress(V4_ADDRESS, 32, 0, RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v4,global,noflags"); - - l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v4-rfc1918,global,noflags"); - - l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_SITE); - assertNotGlobalPreferred(l, "v4-rfc1918,site-local,noflags"); - - l = new LinkAddress("127.0.0.7/8", 0, RT_SCOPE_HOST); - assertNotGlobalPreferred(l, "v4-localhost,node-local,noflags"); - - l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v6,global,noflags"); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v6,global,permanent"); - - // IPv6 ULAs are not acceptable "global preferred" addresses. - l = new LinkAddress("fc12::1/64", 0, RT_SCOPE_UNIVERSE); - assertNotGlobalPreferred(l, "v6,ula1,noflags"); - - l = new LinkAddress("fd34::1/64", 0, RT_SCOPE_UNIVERSE); - assertNotGlobalPreferred(l, "v6,ula2,noflags"); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v6,global,tempaddr"); - - l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DADFAILED), - RT_SCOPE_UNIVERSE); - assertNotGlobalPreferred(l, "v6,global,tempaddr+dadfailed"); - - l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DEPRECATED), - RT_SCOPE_UNIVERSE); - assertNotGlobalPreferred(l, "v6,global,tempaddr+deprecated"); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_SITE); - assertNotGlobalPreferred(l, "v6,site-local,tempaddr"); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_LINK); - assertNotGlobalPreferred(l, "v6,link-local,tempaddr"); - - l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_HOST); - assertNotGlobalPreferred(l, "v6,node-local,tempaddr"); - - l = new LinkAddress("::1/128", IFA_F_PERMANENT, RT_SCOPE_HOST); - assertNotGlobalPreferred(l, "v6-localhost,node-local,permanent"); - - l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_TENTATIVE), - RT_SCOPE_UNIVERSE); - assertNotGlobalPreferred(l, "v6,global,tempaddr+tentative"); - - l = new LinkAddress(V6_ADDRESS, 64, - (IFA_F_TEMPORARY|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC), - RT_SCOPE_UNIVERSE); - assertGlobalPreferred(l, "v6,global,tempaddr+optimistic"); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testIsGlobalPreferred_DeprecatedInFuture() { - final LinkAddress l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, - RT_SCOPE_UNIVERSE, SystemClock.elapsedRealtime() + 100000, - SystemClock.elapsedRealtime() + 200000); - // Although the deprecated bit is set, but the deprecation time is in the future, test - // if the flag is removed automatically. - assertGlobalPreferred(l, "v6,global,tempaddr+deprecated in the future"); - } -} diff --git a/tests/net/common/java/android/net/LinkPropertiesTest.java b/tests/net/common/java/android/net/LinkPropertiesTest.java deleted file mode 100644 index 550953d0612d..000000000000 --- a/tests/net/common/java/android/net/LinkPropertiesTest.java +++ /dev/null @@ -1,1271 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static android.net.RouteInfo.RTN_THROW; -import static android.net.RouteInfo.RTN_UNICAST; -import static android.net.RouteInfo.RTN_UNREACHABLE; - -import static com.android.testutils.ParcelUtils.assertParcelSane; -import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; -import static com.android.testutils.ParcelUtils.parcelingRoundTrip; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import android.net.LinkProperties.ProvisioningChange; -import android.os.Build; -import android.system.OsConstants; -import android.util.ArraySet; - -import androidx.core.os.BuildCompat; -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.net.module.util.LinkPropertiesUtils.CompareResult; -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.Inet4Address; -import java.net.Inet6Address; -import java.net.InetAddress; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class LinkPropertiesTest { - @Rule - public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); - - private static final InetAddress ADDRV4 = address("75.208.6.1"); - private static final InetAddress ADDRV6 = address("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); - private static final InetAddress DNS1 = address("75.208.7.1"); - private static final InetAddress DNS2 = address("69.78.7.1"); - private static final InetAddress DNS6 = address("2001:4860:4860::8888"); - private static final InetAddress PRIVDNS1 = address("1.1.1.1"); - private static final InetAddress PRIVDNS2 = address("1.0.0.1"); - private static final InetAddress PRIVDNS6 = address("2606:4700:4700::1111"); - private static final InetAddress PCSCFV4 = address("10.77.25.37"); - private static final InetAddress PCSCFV6 = address("2001:0db8:85a3:0000:0000:8a2e:0370:1"); - private static final InetAddress GATEWAY1 = address("75.208.8.1"); - private static final InetAddress GATEWAY2 = address("69.78.8.1"); - private static final InetAddress GATEWAY61 = address("fe80::6:0000:613"); - private static final InetAddress GATEWAY62 = address("fe80::6:22%lo"); - private static final InetAddress TESTIPV4ADDR = address("192.168.47.42"); - private static final InetAddress TESTIPV6ADDR = address("fe80::7:33%43"); - private static final Inet4Address DHCPSERVER = (Inet4Address) address("192.0.2.1"); - private static final String NAME = "qmi0"; - private static final String DOMAINS = "google.com"; - private static final String PRIV_DNS_SERVER_NAME = "private.dns.com"; - private static final String TCP_BUFFER_SIZES = "524288,1048576,2097152,262144,524288,1048576"; - private static final int MTU = 1500; - private static final LinkAddress LINKADDRV4 = new LinkAddress(ADDRV4, 32); - private static final LinkAddress LINKADDRV6 = new LinkAddress(ADDRV6, 128); - private static final LinkAddress LINKADDRV6LINKLOCAL = new LinkAddress("fe80::1/64"); - private static final Uri CAPPORT_API_URL = Uri.parse("https://test.example.com/capportapi"); - - // CaptivePortalData cannot be in a constant as it does not exist on Q. - // The test runner also crashes when scanning for tests if it is a return type. - private static Object getCaptivePortalData() { - return new CaptivePortalData.Builder() - .setVenueInfoUrl(Uri.parse("https://test.example.com/venue")).build(); - } - - private static InetAddress address(String addrString) { - return InetAddresses.parseNumericAddress(addrString); - } - - private static boolean isAtLeastR() { - // BuildCompat.isAtLeastR is documented to return false on release SDKs (including R) - return Build.VERSION.SDK_INT > Build.VERSION_CODES.Q || BuildCompat.isAtLeastR(); - } - - private void checkEmpty(final LinkProperties lp) { - assertEquals(0, lp.getAllInterfaceNames().size()); - assertEquals(0, lp.getAllAddresses().size()); - assertEquals(0, lp.getDnsServers().size()); - assertEquals(0, lp.getValidatedPrivateDnsServers().size()); - assertEquals(0, lp.getPcscfServers().size()); - assertEquals(0, lp.getAllRoutes().size()); - assertEquals(0, lp.getAllLinkAddresses().size()); - assertEquals(0, lp.getStackedLinks().size()); - assertEquals(0, lp.getMtu()); - assertNull(lp.getPrivateDnsServerName()); - assertNull(lp.getDomains()); - assertNull(lp.getHttpProxy()); - assertNull(lp.getTcpBufferSizes()); - assertNull(lp.getNat64Prefix()); - assertFalse(lp.isProvisioned()); - assertFalse(lp.isIpv4Provisioned()); - assertFalse(lp.isIpv6Provisioned()); - assertFalse(lp.isPrivateDnsActive()); - - if (isAtLeastR()) { - assertNull(lp.getDhcpServerAddress()); - assertFalse(lp.isWakeOnLanSupported()); - assertNull(lp.getCaptivePortalApiUrl()); - assertNull(lp.getCaptivePortalData()); - } - } - - private LinkProperties makeTestObject() { - final LinkProperties lp = new LinkProperties(); - lp.setInterfaceName(NAME); - lp.addLinkAddress(LINKADDRV4); - lp.addLinkAddress(LINKADDRV6); - lp.addDnsServer(DNS1); - lp.addDnsServer(DNS2); - lp.addValidatedPrivateDnsServer(PRIVDNS1); - lp.addValidatedPrivateDnsServer(PRIVDNS2); - lp.setUsePrivateDns(true); - lp.setPrivateDnsServerName(PRIV_DNS_SERVER_NAME); - lp.addPcscfServer(PCSCFV6); - lp.setDomains(DOMAINS); - lp.addRoute(new RouteInfo(GATEWAY1)); - lp.addRoute(new RouteInfo(GATEWAY2)); - lp.setHttpProxy(ProxyInfo.buildDirectProxy("test", 8888)); - lp.setMtu(MTU); - lp.setTcpBufferSizes(TCP_BUFFER_SIZES); - lp.setNat64Prefix(new IpPrefix("2001:db8:0:64::/96")); - if (isAtLeastR()) { - lp.setDhcpServerAddress(DHCPSERVER); - lp.setWakeOnLanSupported(true); - lp.setCaptivePortalApiUrl(CAPPORT_API_URL); - lp.setCaptivePortalData((CaptivePortalData) getCaptivePortalData()); - } - return lp; - } - - public void assertLinkPropertiesEqual(LinkProperties source, LinkProperties target) { - // Check implementation of equals(), element by element. - assertTrue(source.isIdenticalInterfaceName(target)); - assertTrue(target.isIdenticalInterfaceName(source)); - - assertTrue(source.isIdenticalAddresses(target)); - assertTrue(target.isIdenticalAddresses(source)); - - assertTrue(source.isIdenticalDnses(target)); - assertTrue(target.isIdenticalDnses(source)); - - assertTrue(source.isIdenticalPrivateDns(target)); - assertTrue(target.isIdenticalPrivateDns(source)); - - assertTrue(source.isIdenticalValidatedPrivateDnses(target)); - assertTrue(target.isIdenticalValidatedPrivateDnses(source)); - - assertTrue(source.isIdenticalPcscfs(target)); - assertTrue(target.isIdenticalPcscfs(source)); - - assertTrue(source.isIdenticalRoutes(target)); - assertTrue(target.isIdenticalRoutes(source)); - - assertTrue(source.isIdenticalHttpProxy(target)); - assertTrue(target.isIdenticalHttpProxy(source)); - - assertTrue(source.isIdenticalStackedLinks(target)); - assertTrue(target.isIdenticalStackedLinks(source)); - - assertTrue(source.isIdenticalMtu(target)); - assertTrue(target.isIdenticalMtu(source)); - - assertTrue(source.isIdenticalTcpBufferSizes(target)); - assertTrue(target.isIdenticalTcpBufferSizes(source)); - - if (isAtLeastR()) { - assertTrue(source.isIdenticalDhcpServerAddress(target)); - assertTrue(source.isIdenticalDhcpServerAddress(source)); - - assertTrue(source.isIdenticalWakeOnLan(target)); - assertTrue(target.isIdenticalWakeOnLan(source)); - - assertTrue(source.isIdenticalCaptivePortalApiUrl(target)); - assertTrue(target.isIdenticalCaptivePortalApiUrl(source)); - - assertTrue(source.isIdenticalCaptivePortalData(target)); - assertTrue(target.isIdenticalCaptivePortalData(source)); - } - - // Check result of equals(). - assertTrue(source.equals(target)); - assertTrue(target.equals(source)); - - // Check hashCode. - assertEquals(source.hashCode(), target.hashCode()); - } - - @Test - public void testEqualsNull() { - LinkProperties source = new LinkProperties(); - LinkProperties target = new LinkProperties(); - - assertFalse(source == target); - assertLinkPropertiesEqual(source, target); - } - - @Test - public void testEqualsSameOrder() throws Exception { - LinkProperties source = new LinkProperties(); - source.setInterfaceName(NAME); - // set 2 link addresses - source.addLinkAddress(LINKADDRV4); - source.addLinkAddress(LINKADDRV6); - // set 2 dnses - source.addDnsServer(DNS1); - source.addDnsServer(DNS2); - // set 1 pcscf - source.addPcscfServer(PCSCFV6); - // set 2 gateways - source.addRoute(new RouteInfo(GATEWAY1)); - source.addRoute(new RouteInfo(GATEWAY2)); - source.setMtu(MTU); - - LinkProperties target = new LinkProperties(); - - // All fields are same - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(DNS1); - target.addDnsServer(DNS2); - target.addPcscfServer(PCSCFV6); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - target.setMtu(MTU); - - assertLinkPropertiesEqual(source, target); - - target.clear(); - // change Interface Name - target.setInterfaceName("qmi1"); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(DNS1); - target.addDnsServer(DNS2); - target.addPcscfServer(PCSCFV6); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - target.setMtu(MTU); - assertFalse(source.equals(target)); - - target.clear(); - target.setInterfaceName(NAME); - // change link addresses - target.addLinkAddress(new LinkAddress(address("75.208.6.2"), 32)); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(DNS1); - target.addDnsServer(DNS2); - target.addPcscfServer(PCSCFV6); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - target.setMtu(MTU); - assertFalse(source.equals(target)); - - target.clear(); - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - // change dnses - target.addDnsServer(address("75.208.7.2")); - target.addDnsServer(DNS2); - target.addPcscfServer(PCSCFV6); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - target.setMtu(MTU); - assertFalse(source.equals(target)); - - target.clear(); - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(address("75.208.7.2")); - target.addDnsServer(DNS2); - // change pcscf - target.addPcscfServer(address("2001::1")); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - target.setMtu(MTU); - assertFalse(source.equals(target)); - - target.clear(); - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(DNS1); - target.addDnsServer(DNS2); - // change gateway - target.addRoute(new RouteInfo(address("75.208.8.2"))); - target.setMtu(MTU); - target.addRoute(new RouteInfo(GATEWAY2)); - assertFalse(source.equals(target)); - - target.clear(); - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addDnsServer(DNS1); - target.addDnsServer(DNS2); - target.addRoute(new RouteInfo(GATEWAY1)); - target.addRoute(new RouteInfo(GATEWAY2)); - // change mtu - target.setMtu(1440); - assertFalse(source.equals(target)); - } - - @Test - public void testEqualsDifferentOrder() throws Exception { - LinkProperties source = new LinkProperties(); - source.setInterfaceName(NAME); - // set 2 link addresses - source.addLinkAddress(LINKADDRV4); - source.addLinkAddress(LINKADDRV6); - // set 2 dnses - source.addDnsServer(DNS1); - source.addDnsServer(DNS2); - // set 2 gateways - source.addRoute(new RouteInfo(LINKADDRV4, GATEWAY1)); - source.addRoute(new RouteInfo(GATEWAY2)); - source.setMtu(MTU); - - LinkProperties target = new LinkProperties(); - // Exchange order - target.setInterfaceName(NAME); - target.addLinkAddress(LINKADDRV6); - target.addLinkAddress(LINKADDRV4); - target.addDnsServer(DNS2); - target.addDnsServer(DNS1); - target.addRoute(new RouteInfo(GATEWAY2)); - target.addRoute(new RouteInfo(LINKADDRV4, GATEWAY1)); - target.setMtu(MTU); - - assertLinkPropertiesEqual(source, target); - } - - @Test - public void testEqualsDuplicated() throws Exception { - LinkProperties source = new LinkProperties(); - // set 3 link addresses, eg, [A, A, B] - source.addLinkAddress(LINKADDRV4); - source.addLinkAddress(LINKADDRV4); - source.addLinkAddress(LINKADDRV6); - - LinkProperties target = new LinkProperties(); - // set 3 link addresses, eg, [A, B, B] - target.addLinkAddress(LINKADDRV4); - target.addLinkAddress(LINKADDRV6); - target.addLinkAddress(LINKADDRV6); - - assertLinkPropertiesEqual(source, target); - } - - private void assertAllRoutesHaveInterface(String iface, LinkProperties lp) { - for (RouteInfo r : lp.getRoutes()) { - assertEquals(iface, r.getInterface()); - } - } - - private void assertAllRoutesNotHaveInterface(String iface, LinkProperties lp) { - for (RouteInfo r : lp.getRoutes()) { - assertNotEquals(iface, r.getInterface()); - } - } - - @Test - public void testRouteInterfaces() { - LinkAddress prefix1 = new LinkAddress(address("2001:db8:1::"), 48); - LinkAddress prefix2 = new LinkAddress(address("2001:db8:2::"), 48); - InetAddress address = ADDRV6; - - // Add a route with no interface to a LinkProperties with no interface. No errors. - LinkProperties lp = new LinkProperties(); - RouteInfo r = new RouteInfo(prefix1, address, null); - assertTrue(lp.addRoute(r)); - assertEquals(1, lp.getRoutes().size()); - assertAllRoutesHaveInterface(null, lp); - - // Adding the same route twice has no effect. - assertFalse(lp.addRoute(r)); - assertEquals(1, lp.getRoutes().size()); - - // Add a route with an interface. Expect an exception. - r = new RouteInfo(prefix2, address, "wlan0"); - try { - lp.addRoute(r); - fail("Adding wlan0 route to LP with no interface, expect exception"); - } catch (IllegalArgumentException expected) {} - - // Change the interface name. All the routes should change their interface name too. - lp.setInterfaceName("rmnet0"); - assertAllRoutesHaveInterface("rmnet0", lp); - assertAllRoutesNotHaveInterface(null, lp); - assertAllRoutesNotHaveInterface("wlan0", lp); - - // Now add a route with the wrong interface. This causes an exception too. - try { - lp.addRoute(r); - fail("Adding wlan0 route to rmnet0 LP, expect exception"); - } catch (IllegalArgumentException expected) {} - - // If the interface name matches, the route is added. - r = new RouteInfo(prefix2, null, "wlan0"); - lp.setInterfaceName("wlan0"); - lp.addRoute(r); - assertEquals(2, lp.getRoutes().size()); - assertAllRoutesHaveInterface("wlan0", lp); - assertAllRoutesNotHaveInterface("rmnet0", lp); - - // Routes with null interfaces are converted to wlan0. - r = RouteInfo.makeHostRoute(ADDRV6, null); - lp.addRoute(r); - assertEquals(3, lp.getRoutes().size()); - assertAllRoutesHaveInterface("wlan0", lp); - - // Check routes are updated correctly when calling setInterfaceName. - LinkProperties lp2 = new LinkProperties(lp); - assertAllRoutesHaveInterface("wlan0", lp2); - final CompareResult<RouteInfo> cr1 = - new CompareResult<>(lp.getAllRoutes(), lp2.getAllRoutes()); - assertEquals(0, cr1.added.size()); - assertEquals(0, cr1.removed.size()); - - lp2.setInterfaceName("p2p0"); - assertAllRoutesHaveInterface("p2p0", lp2); - assertAllRoutesNotHaveInterface("wlan0", lp2); - final CompareResult<RouteInfo> cr2 = - new CompareResult<>(lp.getAllRoutes(), lp2.getAllRoutes()); - assertEquals(3, cr2.added.size()); - assertEquals(3, cr2.removed.size()); - - // Remove route with incorrect interface, no route removed. - lp.removeRoute(new RouteInfo(prefix2, null, null)); - assertEquals(3, lp.getRoutes().size()); - - // Check remove works when interface is correct. - lp.removeRoute(new RouteInfo(prefix2, null, "wlan0")); - assertEquals(2, lp.getRoutes().size()); - assertAllRoutesHaveInterface("wlan0", lp); - assertAllRoutesNotHaveInterface("p2p0", lp); - } - - @Test - public void testStackedInterfaces() { - LinkProperties rmnet0 = new LinkProperties(); - rmnet0.setInterfaceName("rmnet0"); - rmnet0.addLinkAddress(LINKADDRV6); - - LinkProperties clat4 = new LinkProperties(); - clat4.setInterfaceName("clat4"); - clat4.addLinkAddress(LINKADDRV4); - - assertEquals(0, rmnet0.getStackedLinks().size()); - assertEquals(1, rmnet0.getAddresses().size()); - assertEquals(1, rmnet0.getLinkAddresses().size()); - assertEquals(1, rmnet0.getAllAddresses().size()); - assertEquals(1, rmnet0.getAllLinkAddresses().size()); - assertEquals(1, rmnet0.getAllInterfaceNames().size()); - assertEquals("rmnet0", rmnet0.getAllInterfaceNames().get(0)); - - rmnet0.addStackedLink(clat4); - assertEquals(1, rmnet0.getStackedLinks().size()); - assertEquals(1, rmnet0.getAddresses().size()); - assertEquals(1, rmnet0.getLinkAddresses().size()); - assertEquals(2, rmnet0.getAllAddresses().size()); - assertEquals(2, rmnet0.getAllLinkAddresses().size()); - assertEquals(2, rmnet0.getAllInterfaceNames().size()); - assertEquals("rmnet0", rmnet0.getAllInterfaceNames().get(0)); - assertEquals("clat4", rmnet0.getAllInterfaceNames().get(1)); - - rmnet0.addStackedLink(clat4); - assertEquals(1, rmnet0.getStackedLinks().size()); - assertEquals(1, rmnet0.getAddresses().size()); - assertEquals(1, rmnet0.getLinkAddresses().size()); - assertEquals(2, rmnet0.getAllAddresses().size()); - assertEquals(2, rmnet0.getAllLinkAddresses().size()); - assertEquals(2, rmnet0.getAllInterfaceNames().size()); - assertEquals("rmnet0", rmnet0.getAllInterfaceNames().get(0)); - assertEquals("clat4", rmnet0.getAllInterfaceNames().get(1)); - - assertEquals(0, clat4.getStackedLinks().size()); - - // Modify an item in the returned collection to see what happens. - for (LinkProperties link : rmnet0.getStackedLinks()) { - if (link.getInterfaceName().equals("clat4")) { - link.setInterfaceName("newname"); - } - } - for (LinkProperties link : rmnet0.getStackedLinks()) { - assertFalse("newname".equals(link.getInterfaceName())); - } - - assertTrue(rmnet0.removeStackedLink("clat4")); - assertEquals(0, rmnet0.getStackedLinks().size()); - assertEquals(1, rmnet0.getAddresses().size()); - assertEquals(1, rmnet0.getLinkAddresses().size()); - assertEquals(1, rmnet0.getAllAddresses().size()); - assertEquals(1, rmnet0.getAllLinkAddresses().size()); - assertEquals(1, rmnet0.getAllInterfaceNames().size()); - assertEquals("rmnet0", rmnet0.getAllInterfaceNames().get(0)); - - assertFalse(rmnet0.removeStackedLink("clat4")); - } - - private LinkAddress getFirstLinkAddress(LinkProperties lp) { - return lp.getLinkAddresses().iterator().next(); - } - - @Test - public void testAddressMethods() { - LinkProperties lp = new LinkProperties(); - - // No addresses. - assertFalse(lp.hasIpv4Address()); - assertFalse(lp.hasGlobalIpv6Address()); - - // Addresses on stacked links don't count. - LinkProperties stacked = new LinkProperties(); - stacked.setInterfaceName("stacked"); - lp.addStackedLink(stacked); - stacked.addLinkAddress(LINKADDRV4); - stacked.addLinkAddress(LINKADDRV6); - assertTrue(stacked.hasIpv4Address()); - assertTrue(stacked.hasGlobalIpv6Address()); - assertFalse(lp.hasIpv4Address()); - assertFalse(lp.hasGlobalIpv6Address()); - lp.removeStackedLink("stacked"); - assertFalse(lp.hasIpv4Address()); - assertFalse(lp.hasGlobalIpv6Address()); - - // Addresses on the base link. - // Check the return values of hasIpvXAddress and ensure the add/remove methods return true - // iff something changes. - assertEquals(0, lp.getLinkAddresses().size()); - assertTrue(lp.addLinkAddress(LINKADDRV6)); - assertEquals(1, lp.getLinkAddresses().size()); - assertFalse(lp.hasIpv4Address()); - assertTrue(lp.hasGlobalIpv6Address()); - - assertTrue(lp.removeLinkAddress(LINKADDRV6)); - assertEquals(0, lp.getLinkAddresses().size()); - - assertTrue(lp.addLinkAddress(LINKADDRV6LINKLOCAL)); - assertEquals(1, lp.getLinkAddresses().size()); - assertFalse(lp.hasGlobalIpv6Address()); - - assertTrue(lp.addLinkAddress(LINKADDRV4)); - assertEquals(2, lp.getLinkAddresses().size()); - assertTrue(lp.hasIpv4Address()); - assertFalse(lp.hasGlobalIpv6Address()); - - assertTrue(lp.addLinkAddress(LINKADDRV6)); - assertEquals(3, lp.getLinkAddresses().size()); - assertTrue(lp.hasIpv4Address()); - assertTrue(lp.hasGlobalIpv6Address()); - - assertTrue(lp.removeLinkAddress(LINKADDRV6LINKLOCAL)); - assertEquals(2, lp.getLinkAddresses().size()); - assertTrue(lp.hasIpv4Address()); - assertTrue(lp.hasGlobalIpv6Address()); - - // Adding an address twice has no effect. - // Removing an address that's not present has no effect. - assertFalse(lp.addLinkAddress(LINKADDRV4)); - assertEquals(2, lp.getLinkAddresses().size()); - assertTrue(lp.hasIpv4Address()); - assertTrue(lp.removeLinkAddress(LINKADDRV4)); - assertEquals(1, lp.getLinkAddresses().size()); - assertFalse(lp.hasIpv4Address()); - assertFalse(lp.removeLinkAddress(LINKADDRV4)); - assertEquals(1, lp.getLinkAddresses().size()); - - // Adding an address that's already present but with different properties causes the - // existing address to be updated and returns true. - // Start with only LINKADDRV6. - assertEquals(1, lp.getLinkAddresses().size()); - assertEquals(LINKADDRV6, getFirstLinkAddress(lp)); - - // Create a LinkAddress object for the same address, but with different flags. - LinkAddress deprecated = new LinkAddress(ADDRV6, 128, - OsConstants.IFA_F_DEPRECATED, OsConstants.RT_SCOPE_UNIVERSE); - assertTrue(deprecated.isSameAddressAs(LINKADDRV6)); - assertFalse(deprecated.equals(LINKADDRV6)); - - // Check that adding it updates the existing address instead of adding a new one. - assertTrue(lp.addLinkAddress(deprecated)); - assertEquals(1, lp.getLinkAddresses().size()); - assertEquals(deprecated, getFirstLinkAddress(lp)); - assertFalse(LINKADDRV6.equals(getFirstLinkAddress(lp))); - - // Removing LINKADDRV6 removes deprecated, because removing addresses ignores properties. - assertTrue(lp.removeLinkAddress(LINKADDRV6)); - assertEquals(0, lp.getLinkAddresses().size()); - } - - @Test - public void testLinkAddresses() { - final LinkProperties lp = new LinkProperties(); - lp.addLinkAddress(LINKADDRV4); - lp.addLinkAddress(LINKADDRV6); - - final LinkProperties lp2 = new LinkProperties(); - lp2.addLinkAddress(LINKADDRV6); - - final LinkProperties lp3 = new LinkProperties(); - final List<LinkAddress> linkAddresses = Arrays.asList(LINKADDRV4); - lp3.setLinkAddresses(linkAddresses); - - assertFalse(lp.equals(lp2)); - assertFalse(lp2.equals(lp3)); - - lp.removeLinkAddress(LINKADDRV4); - assertTrue(lp.equals(lp2)); - - lp2.setLinkAddresses(lp3.getLinkAddresses()); - assertTrue(lp2.equals(lp3)); - } - - @Test - public void testNat64Prefix() throws Exception { - LinkProperties lp = new LinkProperties(); - lp.addLinkAddress(LINKADDRV4); - lp.addLinkAddress(LINKADDRV6); - - assertNull(lp.getNat64Prefix()); - - IpPrefix p = new IpPrefix("64:ff9b::/96"); - lp.setNat64Prefix(p); - assertEquals(p, lp.getNat64Prefix()); - - p = new IpPrefix("2001:db8:a:b:1:2:3::/96"); - lp.setNat64Prefix(p); - assertEquals(p, lp.getNat64Prefix()); - - p = new IpPrefix("2001:db8:a:b:1:2::/80"); - try { - lp.setNat64Prefix(p); - } catch (IllegalArgumentException expected) { - } - - p = new IpPrefix("64:ff9b::/64"); - try { - lp.setNat64Prefix(p); - } catch (IllegalArgumentException expected) { - } - - assertEquals(new IpPrefix("2001:db8:a:b:1:2:3::/96"), lp.getNat64Prefix()); - - lp.setNat64Prefix(null); - assertNull(lp.getNat64Prefix()); - } - - @Test - public void testIsProvisioned() { - LinkProperties lp4 = new LinkProperties(); - assertFalse("v4only:empty", lp4.isProvisioned()); - lp4.addLinkAddress(LINKADDRV4); - assertFalse("v4only:addr-only", lp4.isProvisioned()); - lp4.addDnsServer(DNS1); - assertFalse("v4only:addr+dns", lp4.isProvisioned()); - lp4.addRoute(new RouteInfo(GATEWAY1)); - assertTrue("v4only:addr+dns+route", lp4.isProvisioned()); - assertTrue("v4only:addr+dns+route", lp4.isIpv4Provisioned()); - assertFalse("v4only:addr+dns+route", lp4.isIpv6Provisioned()); - - LinkProperties lp6 = new LinkProperties(); - assertFalse("v6only:empty", lp6.isProvisioned()); - lp6.addLinkAddress(LINKADDRV6LINKLOCAL); - assertFalse("v6only:fe80-only", lp6.isProvisioned()); - lp6.addDnsServer(DNS6); - assertFalse("v6only:fe80+dns", lp6.isProvisioned()); - lp6.addRoute(new RouteInfo(GATEWAY61)); - assertFalse("v6only:fe80+dns+route", lp6.isProvisioned()); - lp6.addLinkAddress(LINKADDRV6); - assertTrue("v6only:fe80+global+dns+route", lp6.isIpv6Provisioned()); - assertTrue("v6only:fe80+global+dns+route", lp6.isProvisioned()); - lp6.removeLinkAddress(LINKADDRV6LINKLOCAL); - assertFalse("v6only:global+dns+route", lp6.isIpv4Provisioned()); - assertTrue("v6only:global+dns+route", lp6.isIpv6Provisioned()); - assertTrue("v6only:global+dns+route", lp6.isProvisioned()); - - LinkProperties lp46 = new LinkProperties(); - lp46.addLinkAddress(LINKADDRV4); - lp46.addLinkAddress(LINKADDRV6); - lp46.addDnsServer(DNS1); - lp46.addDnsServer(DNS6); - assertFalse("dualstack:missing-routes", lp46.isProvisioned()); - lp46.addRoute(new RouteInfo(GATEWAY1)); - assertTrue("dualstack:v4-provisioned", lp46.isIpv4Provisioned()); - assertFalse("dualstack:v4-provisioned", lp46.isIpv6Provisioned()); - assertTrue("dualstack:v4-provisioned", lp46.isProvisioned()); - lp46.addRoute(new RouteInfo(GATEWAY61)); - assertTrue("dualstack:both-provisioned", lp46.isIpv4Provisioned()); - assertTrue("dualstack:both-provisioned", lp46.isIpv6Provisioned()); - assertTrue("dualstack:both-provisioned", lp46.isProvisioned()); - - // A link with an IPv6 address and default route, but IPv4 DNS server. - LinkProperties mixed = new LinkProperties(); - mixed.addLinkAddress(LINKADDRV6); - mixed.addDnsServer(DNS1); - mixed.addRoute(new RouteInfo(GATEWAY61)); - assertFalse("mixed:addr6+route6+dns4", mixed.isIpv4Provisioned()); - assertFalse("mixed:addr6+route6+dns4", mixed.isIpv6Provisioned()); - assertFalse("mixed:addr6+route6+dns4", mixed.isProvisioned()); - } - - @Test - public void testCompareProvisioning() { - LinkProperties v4lp = new LinkProperties(); - v4lp.addLinkAddress(LINKADDRV4); - v4lp.addRoute(new RouteInfo(GATEWAY1)); - v4lp.addDnsServer(DNS1); - assertTrue(v4lp.isProvisioned()); - - LinkProperties v4r = new LinkProperties(v4lp); - v4r.removeDnsServer(DNS1); - assertFalse(v4r.isProvisioned()); - - assertEquals(ProvisioningChange.STILL_NOT_PROVISIONED, - LinkProperties.compareProvisioning(v4r, v4r)); - assertEquals(ProvisioningChange.LOST_PROVISIONING, - LinkProperties.compareProvisioning(v4lp, v4r)); - assertEquals(ProvisioningChange.GAINED_PROVISIONING, - LinkProperties.compareProvisioning(v4r, v4lp)); - assertEquals(ProvisioningChange.STILL_PROVISIONED, - LinkProperties.compareProvisioning(v4lp, v4lp)); - - // Check that losing IPv4 provisioning on a dualstack network is - // seen as a total loss of provisioning. - LinkProperties v6lp = new LinkProperties(); - v6lp.addLinkAddress(LINKADDRV6); - v6lp.addRoute(new RouteInfo(GATEWAY61)); - v6lp.addDnsServer(DNS6); - assertFalse(v6lp.isIpv4Provisioned()); - assertTrue(v6lp.isIpv6Provisioned()); - assertTrue(v6lp.isProvisioned()); - - LinkProperties v46lp = new LinkProperties(v6lp); - v46lp.addLinkAddress(LINKADDRV4); - v46lp.addRoute(new RouteInfo(GATEWAY1)); - v46lp.addDnsServer(DNS1); - assertTrue(v46lp.isIpv4Provisioned()); - assertTrue(v46lp.isIpv6Provisioned()); - assertTrue(v46lp.isProvisioned()); - - assertEquals(ProvisioningChange.STILL_PROVISIONED, - LinkProperties.compareProvisioning(v4lp, v46lp)); - assertEquals(ProvisioningChange.STILL_PROVISIONED, - LinkProperties.compareProvisioning(v6lp, v46lp)); - assertEquals(ProvisioningChange.LOST_PROVISIONING, - LinkProperties.compareProvisioning(v46lp, v6lp)); - assertEquals(ProvisioningChange.LOST_PROVISIONING, - LinkProperties.compareProvisioning(v46lp, v4lp)); - - // Check that losing and gaining a secondary router does not change - // the provisioning status. - LinkProperties v6lp2 = new LinkProperties(v6lp); - v6lp2.addRoute(new RouteInfo(GATEWAY62)); - assertTrue(v6lp2.isProvisioned()); - - assertEquals(ProvisioningChange.STILL_PROVISIONED, - LinkProperties.compareProvisioning(v6lp2, v6lp)); - assertEquals(ProvisioningChange.STILL_PROVISIONED, - LinkProperties.compareProvisioning(v6lp, v6lp2)); - } - - @Test - public void testIsReachable() { - final LinkProperties v4lp = new LinkProperties(); - assertFalse(v4lp.isReachable(DNS1)); - assertFalse(v4lp.isReachable(DNS2)); - - // Add an on-link route, making the on-link DNS server reachable, - // but there is still no IPv4 address. - assertTrue(v4lp.addRoute(new RouteInfo(new IpPrefix(address("75.208.0.0"), 16)))); - assertFalse(v4lp.isReachable(DNS1)); - assertFalse(v4lp.isReachable(DNS2)); - - // Adding an IPv4 address (right now, any IPv4 address) means we use - // the routes to compute likely reachability. - assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16))); - assertTrue(v4lp.isReachable(DNS1)); - assertFalse(v4lp.isReachable(DNS2)); - - // Adding a default route makes the off-link DNS server reachable. - assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1))); - assertTrue(v4lp.isReachable(DNS1)); - assertTrue(v4lp.isReachable(DNS2)); - - final LinkProperties v6lp = new LinkProperties(); - final InetAddress kLinkLocalDns = address("fe80::6:1"); - final InetAddress kLinkLocalDnsWithScope = address("fe80::6:2%43"); - final InetAddress kOnLinkDns = address("2001:db8:85a3::53"); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertFalse(v6lp.isReachable(kOnLinkDns)); - assertFalse(v6lp.isReachable(DNS6)); - - // Add a link-local route, making the link-local DNS servers reachable. Because - // we assume the presence of an IPv6 link-local address, link-local DNS servers - // are considered reachable, but only those with a non-zero scope identifier. - assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(address("fe80::"), 64)))); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertFalse(v6lp.isReachable(kOnLinkDns)); - assertFalse(v6lp.isReachable(DNS6)); - - // Add a link-local address--nothing changes. - assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL)); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertFalse(v6lp.isReachable(kOnLinkDns)); - assertFalse(v6lp.isReachable(DNS6)); - - // Add a global route on link, but no global address yet. DNS servers reachable - // via a route that doesn't require a gateway: give them the benefit of the - // doubt and hope the link-local source address suffices for communication. - assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(address("2001:db8:85a3::"), 64)))); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertTrue(v6lp.isReachable(kOnLinkDns)); - assertFalse(v6lp.isReachable(DNS6)); - - // Add a global address; the on-link global address DNS server is (still) - // presumed reachable. - assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64))); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertTrue(v6lp.isReachable(kOnLinkDns)); - assertFalse(v6lp.isReachable(DNS6)); - - // Adding a default route makes the off-link DNS server reachable. - assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62))); - assertFalse(v6lp.isReachable(kLinkLocalDns)); - assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope)); - assertTrue(v6lp.isReachable(kOnLinkDns)); - assertTrue(v6lp.isReachable(DNS6)); - - // Check isReachable on stacked links. This requires that the source IP address be assigned - // on the interface returned by the route lookup. - LinkProperties stacked = new LinkProperties(); - - // Can't add a stacked link without an interface name. - stacked.setInterfaceName("v4-test0"); - v6lp.addStackedLink(stacked); - - InetAddress stackedAddress = address("192.0.0.4"); - LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32); - assertFalse(v6lp.isReachable(stackedAddress)); - stacked.addLinkAddress(stackedLinkAddress); - assertFalse(v6lp.isReachable(stackedAddress)); - stacked.addRoute(new RouteInfo(stackedLinkAddress)); - assertTrue(stacked.isReachable(stackedAddress)); - assertTrue(v6lp.isReachable(stackedAddress)); - - assertFalse(v6lp.isReachable(DNS1)); - stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress)); - assertTrue(v6lp.isReachable(DNS1)); - } - - @Test - public void testLinkPropertiesEnsureDirectlyConnectedRoutes() { - // IPv4 case: no route added initially - LinkProperties rmnet0 = new LinkProperties(); - rmnet0.setInterfaceName("rmnet0"); - rmnet0.addLinkAddress(new LinkAddress("10.0.0.2/8")); - RouteInfo directRoute0 = new RouteInfo(new IpPrefix("10.0.0.0/8"), null, - rmnet0.getInterfaceName()); - - // Since no routes is added explicitly, getAllRoutes() should return empty. - assertTrue(rmnet0.getAllRoutes().isEmpty()); - rmnet0.ensureDirectlyConnectedRoutes(); - // ensureDirectlyConnectedRoutes() should have added the missing local route. - assertEqualRoutes(Collections.singletonList(directRoute0), rmnet0.getAllRoutes()); - - // IPv4 case: both direct and default routes added initially - LinkProperties rmnet1 = new LinkProperties(); - rmnet1.setInterfaceName("rmnet1"); - rmnet1.addLinkAddress(new LinkAddress("10.0.0.3/8")); - RouteInfo defaultRoute1 = new RouteInfo((IpPrefix) null, address("10.0.0.1"), - rmnet1.getInterfaceName()); - RouteInfo directRoute1 = new RouteInfo(new IpPrefix("10.0.0.0/8"), null, - rmnet1.getInterfaceName()); - rmnet1.addRoute(defaultRoute1); - rmnet1.addRoute(directRoute1); - - // Check added routes - assertEqualRoutes(Arrays.asList(defaultRoute1, directRoute1), rmnet1.getAllRoutes()); - // ensureDirectlyConnectedRoutes() shouldn't change the routes since direct connected - // route is already part of the configuration. - rmnet1.ensureDirectlyConnectedRoutes(); - assertEqualRoutes(Arrays.asList(defaultRoute1, directRoute1), rmnet1.getAllRoutes()); - - // IPv6 case: only default routes added initially - LinkProperties rmnet2 = new LinkProperties(); - rmnet2.setInterfaceName("rmnet2"); - rmnet2.addLinkAddress(new LinkAddress("fe80::cafe/64")); - rmnet2.addLinkAddress(new LinkAddress("2001:db8::2/64")); - RouteInfo defaultRoute2 = new RouteInfo((IpPrefix) null, address("2001:db8::1"), - rmnet2.getInterfaceName()); - RouteInfo directRoute2 = new RouteInfo(new IpPrefix("2001:db8::/64"), null, - rmnet2.getInterfaceName()); - RouteInfo linkLocalRoute2 = new RouteInfo(new IpPrefix("fe80::/64"), null, - rmnet2.getInterfaceName()); - rmnet2.addRoute(defaultRoute2); - - assertEqualRoutes(Arrays.asList(defaultRoute2), rmnet2.getAllRoutes()); - rmnet2.ensureDirectlyConnectedRoutes(); - assertEqualRoutes(Arrays.asList(defaultRoute2, directRoute2, linkLocalRoute2), - rmnet2.getAllRoutes()); - - // Corner case: no interface name - LinkProperties rmnet3 = new LinkProperties(); - rmnet3.addLinkAddress(new LinkAddress("192.168.0.2/24")); - RouteInfo directRoute3 = new RouteInfo(new IpPrefix("192.168.0.0/24"), null, - rmnet3.getInterfaceName()); - - assertTrue(rmnet3.getAllRoutes().isEmpty()); - rmnet3.ensureDirectlyConnectedRoutes(); - assertEqualRoutes(Collections.singletonList(directRoute3), rmnet3.getAllRoutes()); - } - - private void assertEqualRoutes(Collection<RouteInfo> expected, Collection<RouteInfo> actual) { - Set<RouteInfo> expectedSet = new ArraySet<>(expected); - Set<RouteInfo> actualSet = new ArraySet<>(actual); - // Duplicated entries in actual routes are considered failures - assertEquals(actual.size(), actualSet.size()); - - assertEquals(expectedSet, actualSet); - } - - private static LinkProperties makeLinkPropertiesForParceling() { - LinkProperties source = new LinkProperties(); - source.setInterfaceName(NAME); - - source.addLinkAddress(LINKADDRV4); - source.addLinkAddress(LINKADDRV6); - - source.addDnsServer(DNS1); - source.addDnsServer(DNS2); - source.addDnsServer(GATEWAY62); - - source.addPcscfServer(TESTIPV4ADDR); - source.addPcscfServer(TESTIPV6ADDR); - - source.setUsePrivateDns(true); - source.setPrivateDnsServerName(PRIV_DNS_SERVER_NAME); - - source.setDomains(DOMAINS); - - source.addRoute(new RouteInfo(GATEWAY1)); - source.addRoute(new RouteInfo(GATEWAY2)); - - source.addValidatedPrivateDnsServer(DNS6); - source.addValidatedPrivateDnsServer(GATEWAY61); - source.addValidatedPrivateDnsServer(TESTIPV6ADDR); - - source.setHttpProxy(ProxyInfo.buildDirectProxy("test", 8888)); - - source.setMtu(MTU); - - source.setTcpBufferSizes(TCP_BUFFER_SIZES); - - source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96")); - - final LinkProperties stacked = new LinkProperties(); - stacked.setInterfaceName("test-stacked"); - source.addStackedLink(stacked); - - return source; - } - - @Test @IgnoreAfter(Build.VERSION_CODES.Q) - public void testLinkPropertiesParcelable_Q() throws Exception { - final LinkProperties source = makeLinkPropertiesForParceling(); - assertParcelSane(source, 14 /* fieldCount */); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testLinkPropertiesParcelable() throws Exception { - final LinkProperties source = makeLinkPropertiesForParceling(); - - source.setWakeOnLanSupported(true); - source.setCaptivePortalApiUrl(CAPPORT_API_URL); - source.setCaptivePortalData((CaptivePortalData) getCaptivePortalData()); - source.setDhcpServerAddress((Inet4Address) GATEWAY1); - assertParcelSane(new LinkProperties(source, true /* parcelSensitiveFields */), - 18 /* fieldCount */); - - // Verify that without using a sensitiveFieldsParcelingCopy, sensitive fields are cleared. - final LinkProperties sanitized = new LinkProperties(source); - sanitized.setCaptivePortalApiUrl(null); - sanitized.setCaptivePortalData(null); - assertEquals(sanitized, parcelingRoundTrip(source)); - } - - // Parceling of the scope was broken until Q-QPR2 - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testLinkLocalDnsServerParceling() throws Exception { - final String strAddress = "fe80::1%lo"; - final LinkProperties lp = new LinkProperties(); - lp.addDnsServer(address(strAddress)); - final LinkProperties unparceled = parcelingRoundTrip(lp); - // Inet6Address#equals does not test for the scope id - assertEquals(strAddress, unparceled.getDnsServers().get(0).getHostAddress()); - } - - @Test - public void testParcelUninitialized() throws Exception { - LinkProperties empty = new LinkProperties(); - assertParcelingIsLossless(empty); - } - - @Test - public void testConstructor() { - LinkProperties lp = new LinkProperties(); - checkEmpty(lp); - assertLinkPropertiesEqual(lp, new LinkProperties(lp)); - assertLinkPropertiesEqual(lp, new LinkProperties()); - - lp = makeTestObject(); - assertLinkPropertiesEqual(lp, new LinkProperties(lp)); - } - - @Test - public void testDnsServers() { - final LinkProperties lp = new LinkProperties(); - final List<InetAddress> dnsServers = Arrays.asList(DNS1, DNS2); - lp.setDnsServers(dnsServers); - assertEquals(2, lp.getDnsServers().size()); - assertEquals(DNS1, lp.getDnsServers().get(0)); - assertEquals(DNS2, lp.getDnsServers().get(1)); - - lp.removeDnsServer(DNS1); - assertEquals(1, lp.getDnsServers().size()); - assertEquals(DNS2, lp.getDnsServers().get(0)); - - lp.addDnsServer(DNS6); - assertEquals(2, lp.getDnsServers().size()); - assertEquals(DNS2, lp.getDnsServers().get(0)); - assertEquals(DNS6, lp.getDnsServers().get(1)); - } - - @Test - public void testValidatedPrivateDnsServers() { - final LinkProperties lp = new LinkProperties(); - final List<InetAddress> privDnsServers = Arrays.asList(PRIVDNS1, PRIVDNS2); - lp.setValidatedPrivateDnsServers(privDnsServers); - assertEquals(2, lp.getValidatedPrivateDnsServers().size()); - assertEquals(PRIVDNS1, lp.getValidatedPrivateDnsServers().get(0)); - assertEquals(PRIVDNS2, lp.getValidatedPrivateDnsServers().get(1)); - - lp.removeValidatedPrivateDnsServer(PRIVDNS1); - assertEquals(1, lp.getValidatedPrivateDnsServers().size()); - assertEquals(PRIVDNS2, lp.getValidatedPrivateDnsServers().get(0)); - - lp.addValidatedPrivateDnsServer(PRIVDNS6); - assertEquals(2, lp.getValidatedPrivateDnsServers().size()); - assertEquals(PRIVDNS2, lp.getValidatedPrivateDnsServers().get(0)); - assertEquals(PRIVDNS6, lp.getValidatedPrivateDnsServers().get(1)); - } - - @Test - public void testPcscfServers() { - final LinkProperties lp = new LinkProperties(); - final List<InetAddress> pcscfServers = Arrays.asList(PCSCFV4); - lp.setPcscfServers(pcscfServers); - assertEquals(1, lp.getPcscfServers().size()); - assertEquals(PCSCFV4, lp.getPcscfServers().get(0)); - - lp.removePcscfServer(PCSCFV4); - assertEquals(0, lp.getPcscfServers().size()); - - lp.addPcscfServer(PCSCFV6); - assertEquals(1, lp.getPcscfServers().size()); - assertEquals(PCSCFV6, lp.getPcscfServers().get(0)); - } - - @Test - public void testTcpBufferSizes() { - final LinkProperties lp = makeTestObject(); - assertEquals(TCP_BUFFER_SIZES, lp.getTcpBufferSizes()); - - lp.setTcpBufferSizes(null); - assertNull(lp.getTcpBufferSizes()); - } - - @Test - public void testHasIpv6DefaultRoute() { - final LinkProperties lp = makeTestObject(); - assertFalse(lp.hasIPv6DefaultRoute()); - - lp.addRoute(new RouteInfo(GATEWAY61)); - assertTrue(lp.hasIPv6DefaultRoute()); - } - - @Test - public void testHttpProxy() { - final LinkProperties lp = makeTestObject(); - assertTrue(lp.getHttpProxy().equals(ProxyInfo.buildDirectProxy("test", 8888))); - } - - @Test - public void testPrivateDnsServerName() { - final LinkProperties lp = makeTestObject(); - assertEquals(PRIV_DNS_SERVER_NAME, lp.getPrivateDnsServerName()); - - lp.setPrivateDnsServerName(null); - assertNull(lp.getPrivateDnsServerName()); - } - - @Test - public void testUsePrivateDns() { - final LinkProperties lp = makeTestObject(); - assertTrue(lp.isPrivateDnsActive()); - - lp.clear(); - assertFalse(lp.isPrivateDnsActive()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testDhcpServerAddress() { - final LinkProperties lp = makeTestObject(); - assertEquals(DHCPSERVER, lp.getDhcpServerAddress()); - - lp.clear(); - assertNull(lp.getDhcpServerAddress()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testWakeOnLanSupported() { - final LinkProperties lp = makeTestObject(); - assertTrue(lp.isWakeOnLanSupported()); - - lp.clear(); - assertFalse(lp.isWakeOnLanSupported()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testCaptivePortalApiUrl() { - final LinkProperties lp = makeTestObject(); - assertEquals(CAPPORT_API_URL, lp.getCaptivePortalApiUrl()); - - lp.clear(); - assertNull(lp.getCaptivePortalApiUrl()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testCaptivePortalData() { - final LinkProperties lp = makeTestObject(); - assertEquals(getCaptivePortalData(), lp.getCaptivePortalData()); - - lp.clear(); - assertNull(lp.getCaptivePortalData()); - } - - private LinkProperties makeIpv4LinkProperties() { - final LinkProperties linkProperties = new LinkProperties(); - linkProperties.setInterfaceName(NAME); - linkProperties.addLinkAddress(LINKADDRV4); - linkProperties.addDnsServer(DNS1); - linkProperties.addRoute(new RouteInfo(GATEWAY1)); - linkProperties.addRoute(new RouteInfo(GATEWAY2)); - return linkProperties; - } - - private LinkProperties makeIpv6LinkProperties() { - final LinkProperties linkProperties = new LinkProperties(); - linkProperties.setInterfaceName(NAME); - linkProperties.addLinkAddress(LINKADDRV6); - linkProperties.addDnsServer(DNS6); - linkProperties.addRoute(new RouteInfo(GATEWAY61)); - linkProperties.addRoute(new RouteInfo(GATEWAY62)); - return linkProperties; - } - - @Test - public void testHasIpv4DefaultRoute() { - final LinkProperties Ipv4 = makeIpv4LinkProperties(); - assertTrue(Ipv4.hasIpv4DefaultRoute()); - final LinkProperties Ipv6 = makeIpv6LinkProperties(); - assertFalse(Ipv6.hasIpv4DefaultRoute()); - } - - @Test - public void testHasIpv4DnsServer() { - final LinkProperties Ipv4 = makeIpv4LinkProperties(); - assertTrue(Ipv4.hasIpv4DnsServer()); - final LinkProperties Ipv6 = makeIpv6LinkProperties(); - assertFalse(Ipv6.hasIpv4DnsServer()); - } - - @Test - public void testHasIpv6DnsServer() { - final LinkProperties Ipv4 = makeIpv4LinkProperties(); - assertFalse(Ipv4.hasIpv6DnsServer()); - final LinkProperties Ipv6 = makeIpv6LinkProperties(); - assertTrue(Ipv6.hasIpv6DnsServer()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testHasIpv4UnreachableDefaultRoute() { - final LinkProperties lp = makeTestObject(); - assertFalse(lp.hasIpv4UnreachableDefaultRoute()); - assertFalse(lp.hasIpv6UnreachableDefaultRoute()); - - lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE)); - assertTrue(lp.hasIpv4UnreachableDefaultRoute()); - assertFalse(lp.hasIpv6UnreachableDefaultRoute()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testHasIpv6UnreachableDefaultRoute() { - final LinkProperties lp = makeTestObject(); - assertFalse(lp.hasIpv6UnreachableDefaultRoute()); - assertFalse(lp.hasIpv4UnreachableDefaultRoute()); - - lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE)); - assertTrue(lp.hasIpv6UnreachableDefaultRoute()); - assertFalse(lp.hasIpv4UnreachableDefaultRoute()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testRouteAddWithSameKey() throws Exception { - LinkProperties lp = new LinkProperties(); - lp.setInterfaceName("wlan0"); - final IpPrefix v6 = new IpPrefix("64:ff9b::/96"); - lp.addRoute(new RouteInfo(v6, address("fe80::1"), "wlan0", RTN_UNICAST, 1280)); - assertEquals(1, lp.getRoutes().size()); - lp.addRoute(new RouteInfo(v6, address("fe80::1"), "wlan0", RTN_UNICAST, 1500)); - assertEquals(1, lp.getRoutes().size()); - final IpPrefix v4 = new IpPrefix("192.0.2.128/25"); - lp.addRoute(new RouteInfo(v4, address("192.0.2.1"), "wlan0", RTN_UNICAST, 1460)); - assertEquals(2, lp.getRoutes().size()); - lp.addRoute(new RouteInfo(v4, address("192.0.2.1"), "wlan0", RTN_THROW, 1460)); - assertEquals(2, lp.getRoutes().size()); - } -} diff --git a/tests/net/common/java/android/net/MatchAllNetworkSpecifierTest.kt b/tests/net/common/java/android/net/MatchAllNetworkSpecifierTest.kt deleted file mode 100644 index a5e44d59fcab..000000000000 --- a/tests/net/common/java/android/net/MatchAllNetworkSpecifierTest.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.net.wifi.aware.DiscoverySession -import android.net.wifi.aware.PeerHandle -import android.net.wifi.aware.WifiAwareNetworkSpecifier -import android.os.Build -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 - -import com.android.testutils.assertParcelSane -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo - -import java.lang.IllegalStateException - -import org.junit.Assert.assertFalse -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Mockito - -@RunWith(AndroidJUnit4::class) -@SmallTest -class MatchAllNetworkSpecifierTest { - @Rule @JvmField - val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() - - private val specifier = MatchAllNetworkSpecifier() - private val discoverySession = Mockito.mock(DiscoverySession::class.java) - private val peerHandle = Mockito.mock(PeerHandle::class.java) - private val wifiAwareNetworkSpecifier = WifiAwareNetworkSpecifier.Builder(discoverySession, - peerHandle).build() - - @Test - fun testParcel() { - assertParcelSane(MatchAllNetworkSpecifier(), 0) - } - - @Test - @IgnoreUpTo(Build.VERSION_CODES.Q) - @IgnoreAfter(Build.VERSION_CODES.R) - // Only run this test on Android R. - // The method - satisfiedBy() has changed to canBeSatisfiedBy() starting from Android R, so the - // method - canBeSatisfiedBy() cannot be found when running this test on Android Q. - fun testCanBeSatisfiedBy_OnlyForR() { - // MatchAllNetworkSpecifier didn't follow its parent class to change the satisfiedBy() to - // canBeSatisfiedBy(), so if a caller calls MatchAllNetworkSpecifier#canBeSatisfiedBy(), the - // NetworkSpecifier#canBeSatisfiedBy() will be called actually, and false will be returned. - // Although it's not meeting the expectation, the behavior still needs to be verified. - assertFalse(specifier.canBeSatisfiedBy(wifiAwareNetworkSpecifier)) - } - - @Test(expected = IllegalStateException::class) - @IgnoreUpTo(Build.VERSION_CODES.R) - fun testCanBeSatisfiedBy() { - specifier.canBeSatisfiedBy(wifiAwareNetworkSpecifier) - } -} diff --git a/tests/net/common/java/android/net/NattKeepalivePacketDataTest.kt b/tests/net/common/java/android/net/NattKeepalivePacketDataTest.kt deleted file mode 100644 index 46f39dd016fd..000000000000 --- a/tests/net/common/java/android/net/NattKeepalivePacketDataTest.kt +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS -import android.net.InvalidPacketException.ERROR_INVALID_PORT -import android.net.NattSocketKeepalive.NATT_PORT -import android.os.Build -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertEqualBothWays -import com.android.testutils.assertFieldCountEquals -import com.android.testutils.assertParcelSane -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import com.android.testutils.parcelingRoundTrip -import java.net.InetAddress -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotEquals -import org.junit.Assert.fail -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class NattKeepalivePacketDataTest { - @Rule @JvmField - val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() - - /* Refer to the definition in {@code NattKeepalivePacketData} */ - private val IPV4_HEADER_LENGTH = 20 - private val UDP_HEADER_LENGTH = 8 - - private val TEST_PORT = 4243 - private val TEST_PORT2 = 4244 - private val TEST_SRC_ADDRV4 = "198.168.0.2".address() - private val TEST_DST_ADDRV4 = "198.168.0.1".address() - private val TEST_ADDRV6 = "2001:db8::1".address() - - private fun String.address() = InetAddresses.parseNumericAddress(this) - private fun nattKeepalivePacket( - srcAddress: InetAddress? = TEST_SRC_ADDRV4, - srcPort: Int = TEST_PORT, - dstAddress: InetAddress? = TEST_DST_ADDRV4, - dstPort: Int = NATT_PORT - ) = NattKeepalivePacketData.nattKeepalivePacket(srcAddress, srcPort, dstAddress, dstPort) - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testConstructor() { - try { - nattKeepalivePacket(dstPort = TEST_PORT) - fail("Dst port is not NATT port should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_PORT) - } - - try { - nattKeepalivePacket(srcAddress = TEST_ADDRV6) - fail("A v6 srcAddress should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) - } - - try { - nattKeepalivePacket(dstAddress = TEST_ADDRV6) - fail("A v6 dstAddress should cause exception") - } catch (e: InvalidPacketException) { - assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) - } - - try { - parcelingRoundTrip( - NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT, - byteArrayOf(12, 31, 22, 44))) - fail("Invalid data should cause exception") - } catch (e: IllegalArgumentException) { } - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testParcel() { - assertParcelSane(nattKeepalivePacket(), 0) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testEquals() { - assertEqualBothWays(nattKeepalivePacket(), nattKeepalivePacket()) - assertNotEquals(nattKeepalivePacket(dstAddress = TEST_SRC_ADDRV4), nattKeepalivePacket()) - assertNotEquals(nattKeepalivePacket(srcAddress = TEST_DST_ADDRV4), nattKeepalivePacket()) - // Test src port only because dst port have to be NATT_PORT - assertNotEquals(nattKeepalivePacket(srcPort = TEST_PORT2), nattKeepalivePacket()) - // Make sure the parceling test is updated if fields are added in the base class. - assertFieldCountEquals(5, KeepalivePacketData::class.java) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testHashCode() { - assertEquals(nattKeepalivePacket().hashCode(), nattKeepalivePacket().hashCode()) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/NetworkAgentConfigTest.kt b/tests/net/common/java/android/net/NetworkAgentConfigTest.kt deleted file mode 100644 index 2b45b3d69ce9..000000000000 --- a/tests/net/common/java/android/net/NetworkAgentConfigTest.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.os.Build -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.modules.utils.build.SdkLevel.isAtLeastS -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class NetworkAgentConfigTest { - @Rule @JvmField - val ignoreRule = DevSdkIgnoreRule() - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testParcelNetworkAgentConfig() { - val config = NetworkAgentConfig.Builder().apply { - setExplicitlySelected(true) - setLegacyType(ConnectivityManager.TYPE_ETHERNET) - setSubscriberId("MySubId") - setPartialConnectivityAcceptable(false) - setUnvalidatedConnectivityAcceptable(true) - if (isAtLeastS()) { - setBypassableVpn(true) - } - }.build() - if (isAtLeastS()) { - // From S, the config will have 12 items - assertParcelSane(config, 12) - } else { - // For R or below, the config will have 10 items - assertParcelSane(config, 10) - } - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testBuilder() { - val config = NetworkAgentConfig.Builder().apply { - setExplicitlySelected(true) - setLegacyType(ConnectivityManager.TYPE_ETHERNET) - setSubscriberId("MySubId") - setPartialConnectivityAcceptable(false) - setUnvalidatedConnectivityAcceptable(true) - setLegacyTypeName("TEST_NETWORK") - if (isAtLeastS()) { - setNat64DetectionEnabled(false) - setProvisioningNotificationEnabled(false) - setBypassableVpn(true) - } - }.build() - - assertTrue(config.isExplicitlySelected()) - assertEquals(ConnectivityManager.TYPE_ETHERNET, config.getLegacyType()) - assertEquals("MySubId", config.getSubscriberId()) - assertFalse(config.isPartialConnectivityAcceptable()) - assertTrue(config.isUnvalidatedConnectivityAcceptable()) - assertEquals("TEST_NETWORK", config.getLegacyTypeName()) - if (isAtLeastS()) { - assertFalse(config.isNat64DetectionEnabled()) - assertFalse(config.isProvisioningNotificationEnabled()) - assertTrue(config.isBypassableVpn()) - } else { - assertTrue(config.isNat64DetectionEnabled()) - assertTrue(config.isProvisioningNotificationEnabled()) - } - } -} diff --git a/tests/net/common/java/android/net/NetworkCapabilitiesTest.java b/tests/net/common/java/android/net/NetworkCapabilitiesTest.java deleted file mode 100644 index 1c8a1bfeb417..000000000000 --- a/tests/net/common/java/android/net/NetworkCapabilitiesTest.java +++ /dev/null @@ -1,1158 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static android.net.NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; -import static android.net.NetworkCapabilities.MAX_TRANSPORT; -import static android.net.NetworkCapabilities.MIN_TRANSPORT; -import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; -import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS; -import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS; -import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; -import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; -import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; -import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID; -import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE; -import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY; -import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; -import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P; -import static android.net.NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION; -import static android.net.NetworkCapabilities.REDACT_FOR_LOCAL_MAC_ADDRESS; -import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS; -import static android.net.NetworkCapabilities.SIGNAL_STRENGTH_UNSPECIFIED; -import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; -import static android.net.NetworkCapabilities.TRANSPORT_TEST; -import static android.net.NetworkCapabilities.TRANSPORT_VPN; -import static android.net.NetworkCapabilities.TRANSPORT_WIFI; -import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; -import static android.os.Process.INVALID_UID; - -import static com.android.modules.utils.build.SdkLevel.isAtLeastR; -import static com.android.modules.utils.build.SdkLevel.isAtLeastS; -import static com.android.net.module.util.NetworkCapabilitiesUtils.TRANSPORT_USB; -import static com.android.testutils.MiscAsserts.assertEmpty; -import static com.android.testutils.MiscAsserts.assertThrows; -import static com.android.testutils.ParcelUtils.assertParcelSane; -import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; - -import android.net.wifi.aware.DiscoverySession; -import android.net.wifi.aware.PeerHandle; -import android.net.wifi.aware.WifiAwareNetworkSpecifier; -import android.os.Build; -import android.test.suitebuilder.annotation.SmallTest; -import android.util.ArraySet; -import android.util.Range; - -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.CompatUtil; -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.Set; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class NetworkCapabilitiesTest { - private static final String TEST_SSID = "TEST_SSID"; - private static final String DIFFERENT_TEST_SSID = "DIFFERENT_TEST_SSID"; - private static final int TEST_SUBID1 = 1; - private static final int TEST_SUBID2 = 2; - private static final int TEST_SUBID3 = 3; - - @Rule - public DevSdkIgnoreRule mDevSdkIgnoreRule = new DevSdkIgnoreRule(); - - private DiscoverySession mDiscoverySession = Mockito.mock(DiscoverySession.class); - private PeerHandle mPeerHandle = Mockito.mock(PeerHandle.class); - - @Test - public void testMaybeMarkCapabilitiesRestricted() { - // check that internet does not get restricted - NetworkCapabilities netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_INTERNET); - netCap.maybeMarkCapabilitiesRestricted(); - assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // metered-ness shouldn't matter - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_INTERNET); - netCap.addCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_INTERNET); - netCap.removeCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // add EIMS - bundled with unrestricted means it's unrestricted - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_INTERNET); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.addCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_INTERNET); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.removeCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // just a restricted cap should be restricted regardless of meteredness - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.addCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.removeCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // try 2 restricted caps - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_CBS); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.addCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - netCap = new NetworkCapabilities(); - netCap.addCapability(NET_CAPABILITY_CBS); - netCap.addCapability(NET_CAPABILITY_EIMS); - netCap.removeCapability(NET_CAPABILITY_NOT_METERED); - netCap.maybeMarkCapabilitiesRestricted(); - assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - } - - @Test - public void testDescribeImmutableDifferences() { - NetworkCapabilities nc1; - NetworkCapabilities nc2; - - // Transports changing - nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR); - nc2 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI); - assertNotEquals("", nc1.describeImmutableDifferences(nc2)); - assertEquals("", nc1.describeImmutableDifferences(nc1)); - - // Mutable capability changing - nc1 = new NetworkCapabilities().addCapability(NET_CAPABILITY_VALIDATED); - nc2 = new NetworkCapabilities(); - assertEquals("", nc1.describeImmutableDifferences(nc2)); - assertEquals("", nc1.describeImmutableDifferences(nc1)); - - // NOT_METERED changing (http://b/63326103) - nc1 = new NetworkCapabilities() - .addCapability(NET_CAPABILITY_NOT_METERED) - .addCapability(NET_CAPABILITY_INTERNET); - nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET); - assertEquals("", nc1.describeImmutableDifferences(nc2)); - assertEquals("", nc1.describeImmutableDifferences(nc1)); - - // Immutable capability changing - nc1 = new NetworkCapabilities() - .addCapability(NET_CAPABILITY_INTERNET) - .removeCapability(NET_CAPABILITY_NOT_RESTRICTED); - nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET); - assertNotEquals("", nc1.describeImmutableDifferences(nc2)); - assertEquals("", nc1.describeImmutableDifferences(nc1)); - - // Specifier changing - nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI); - nc2 = new NetworkCapabilities() - .addTransportType(TRANSPORT_WIFI) - .setNetworkSpecifier(CompatUtil.makeEthernetNetworkSpecifier("eth42")); - assertNotEquals("", nc1.describeImmutableDifferences(nc2)); - assertEquals("", nc1.describeImmutableDifferences(nc1)); - } - - @Test - public void testLinkBandwidthUtils() { - assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities - .minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED)); - assertEquals(10, NetworkCapabilities - .minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10)); - assertEquals(10, NetworkCapabilities - .minBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED)); - assertEquals(10, NetworkCapabilities - .minBandwidth(10, 20)); - - assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities - .maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED)); - assertEquals(10, NetworkCapabilities - .maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10)); - assertEquals(10, NetworkCapabilities - .maxBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED)); - assertEquals(20, NetworkCapabilities - .maxBandwidth(10, 20)); - } - - @Test - public void testSetUids() { - final NetworkCapabilities netCap = new NetworkCapabilities(); - // Null uids match all UIDs - netCap.setUids(null); - assertTrue(netCap.appliesToUid(10)); - assertTrue(netCap.appliesToUid(200)); - assertTrue(netCap.appliesToUid(3000)); - assertTrue(netCap.appliesToUid(10010)); - assertTrue(netCap.appliesToUidRange(new UidRange(50, 100))); - assertTrue(netCap.appliesToUidRange(new UidRange(70, 72))); - assertTrue(netCap.appliesToUidRange(new UidRange(3500, 3912))); - assertTrue(netCap.appliesToUidRange(new UidRange(1, 100000))); - - if (isAtLeastS()) { - final Set<Range<Integer>> uids = new ArraySet<>(); - uids.add(uidRange(50, 100)); - uids.add(uidRange(3000, 4000)); - netCap.setUids(uids); - assertTrue(netCap.appliesToUid(50)); - assertTrue(netCap.appliesToUid(80)); - assertTrue(netCap.appliesToUid(100)); - assertTrue(netCap.appliesToUid(3000)); - assertTrue(netCap.appliesToUid(3001)); - assertFalse(netCap.appliesToUid(10)); - assertFalse(netCap.appliesToUid(25)); - assertFalse(netCap.appliesToUid(49)); - assertFalse(netCap.appliesToUid(101)); - assertFalse(netCap.appliesToUid(2000)); - assertFalse(netCap.appliesToUid(100000)); - - assertTrue(netCap.appliesToUidRange(new UidRange(50, 100))); - assertTrue(netCap.appliesToUidRange(new UidRange(70, 72))); - assertTrue(netCap.appliesToUidRange(new UidRange(3500, 3912))); - assertFalse(netCap.appliesToUidRange(new UidRange(1, 100))); - assertFalse(netCap.appliesToUidRange(new UidRange(49, 100))); - assertFalse(netCap.appliesToUidRange(new UidRange(1, 10))); - assertFalse(netCap.appliesToUidRange(new UidRange(60, 101))); - assertFalse(netCap.appliesToUidRange(new UidRange(60, 3400))); - - NetworkCapabilities netCap2 = new NetworkCapabilities(); - // A new netcap object has null UIDs, so anything will satisfy it. - assertTrue(netCap2.satisfiedByUids(netCap)); - // Still not equal though. - assertFalse(netCap2.equalsUids(netCap)); - netCap2.setUids(uids); - assertTrue(netCap2.satisfiedByUids(netCap)); - assertTrue(netCap.equalsUids(netCap2)); - assertTrue(netCap2.equalsUids(netCap)); - - uids.add(uidRange(600, 700)); - netCap2.setUids(uids); - assertFalse(netCap2.satisfiedByUids(netCap)); - assertFalse(netCap.appliesToUid(650)); - assertTrue(netCap2.appliesToUid(650)); - netCap.combineCapabilities(netCap2); - assertTrue(netCap2.satisfiedByUids(netCap)); - assertTrue(netCap.appliesToUid(650)); - assertFalse(netCap.appliesToUid(500)); - - assertTrue(new NetworkCapabilities().satisfiedByUids(netCap)); - netCap.combineCapabilities(new NetworkCapabilities()); - assertTrue(netCap.appliesToUid(500)); - assertTrue(netCap.appliesToUidRange(new UidRange(1, 100000))); - assertFalse(netCap2.appliesToUid(500)); - assertFalse(netCap2.appliesToUidRange(new UidRange(1, 100000))); - assertTrue(new NetworkCapabilities().satisfiedByUids(netCap)); - - // Null uids satisfies everything. - netCap.setUids(null); - assertTrue(netCap2.satisfiedByUids(netCap)); - assertTrue(netCap.satisfiedByUids(netCap2)); - netCap2.setUids(null); - assertTrue(netCap2.satisfiedByUids(netCap)); - assertTrue(netCap.satisfiedByUids(netCap2)); - } - } - - @Test - public void testParcelNetworkCapabilities() { - final Set<Range<Integer>> uids = new ArraySet<>(); - uids.add(uidRange(50, 100)); - uids.add(uidRange(3000, 4000)); - final NetworkCapabilities netCap = new NetworkCapabilities() - .addCapability(NET_CAPABILITY_INTERNET) - .addCapability(NET_CAPABILITY_EIMS) - .addCapability(NET_CAPABILITY_NOT_METERED); - if (isAtLeastS()) { - netCap.setSubscriptionIds(Set.of(TEST_SUBID1, TEST_SUBID2)); - netCap.setUids(uids); - } - if (isAtLeastR()) { - netCap.setOwnerUid(123); - netCap.setAdministratorUids(new int[] {5, 11}); - } - assertParcelingIsLossless(netCap); - netCap.setSSID(TEST_SSID); - testParcelSane(netCap); - } - - @Test - public void testParcelNetworkCapabilitiesWithRequestorUidAndPackageName() { - final NetworkCapabilities netCap = new NetworkCapabilities() - .addCapability(NET_CAPABILITY_INTERNET) - .addCapability(NET_CAPABILITY_EIMS) - .addCapability(NET_CAPABILITY_NOT_METERED); - if (isAtLeastR()) { - netCap.setRequestorPackageName("com.android.test"); - netCap.setRequestorUid(9304); - } - assertParcelingIsLossless(netCap); - netCap.setSSID(TEST_SSID); - testParcelSane(netCap); - } - - private void testParcelSane(NetworkCapabilities cap) { - if (isAtLeastS()) { - assertParcelSane(cap, 17); - } else if (isAtLeastR()) { - assertParcelSane(cap, 15); - } else { - assertParcelSane(cap, 11); - } - } - - private static NetworkCapabilities createNetworkCapabilitiesWithTransportInfo() { - return new NetworkCapabilities() - .addCapability(NET_CAPABILITY_INTERNET) - .addCapability(NET_CAPABILITY_EIMS) - .addCapability(NET_CAPABILITY_NOT_METERED) - .setSSID(TEST_SSID) - .setTransportInfo(new TestTransportInfo()) - .setRequestorPackageName("com.android.test") - .setRequestorUid(9304); - } - - @Test - public void testNetworkCapabilitiesCopyWithNoRedactions() { - assumeTrue(isAtLeastS()); - - final NetworkCapabilities netCap = createNetworkCapabilitiesWithTransportInfo(); - final NetworkCapabilities netCapWithNoRedactions = - new NetworkCapabilities(netCap, NetworkCapabilities.REDACT_NONE); - TestTransportInfo testTransportInfo = - (TestTransportInfo) netCapWithNoRedactions.getTransportInfo(); - assertFalse(testTransportInfo.locationRedacted); - assertFalse(testTransportInfo.localMacAddressRedacted); - assertFalse(testTransportInfo.settingsRedacted); - } - - @Test - public void testNetworkCapabilitiesCopyWithoutLocationSensitiveFields() { - assumeTrue(isAtLeastS()); - - final NetworkCapabilities netCap = createNetworkCapabilitiesWithTransportInfo(); - final NetworkCapabilities netCapWithNoRedactions = - new NetworkCapabilities(netCap, REDACT_FOR_ACCESS_FINE_LOCATION); - TestTransportInfo testTransportInfo = - (TestTransportInfo) netCapWithNoRedactions.getTransportInfo(); - assertTrue(testTransportInfo.locationRedacted); - assertFalse(testTransportInfo.localMacAddressRedacted); - assertFalse(testTransportInfo.settingsRedacted); - } - - @Test - public void testOemPaid() { - NetworkCapabilities nc = new NetworkCapabilities(); - // By default OEM_PAID is neither in the required or forbidden lists and the network is not - // restricted. - if (isAtLeastS()) { - assertFalse(nc.hasForbiddenCapability(NET_CAPABILITY_OEM_PAID)); - } - assertFalse(nc.hasCapability(NET_CAPABILITY_OEM_PAID)); - nc.maybeMarkCapabilitiesRestricted(); - assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Adding OEM_PAID to capability list should make network restricted. - nc.addCapability(NET_CAPABILITY_OEM_PAID); - nc.addCapability(NET_CAPABILITY_INTERNET); // Combine with unrestricted capability. - nc.maybeMarkCapabilitiesRestricted(); - assertTrue(nc.hasCapability(NET_CAPABILITY_OEM_PAID)); - assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Now let's make request for OEM_PAID network. - NetworkCapabilities nr = new NetworkCapabilities(); - nr.addCapability(NET_CAPABILITY_OEM_PAID); - nr.maybeMarkCapabilitiesRestricted(); - assertTrue(nr.satisfiedByNetworkCapabilities(nc)); - - // Request fails for network with the default capabilities. - assertFalse(nr.satisfiedByNetworkCapabilities(new NetworkCapabilities())); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testOemPrivate() { - NetworkCapabilities nc = new NetworkCapabilities(); - // By default OEM_PRIVATE is neither in the required or forbidden lists and the network is - // not restricted. - assertFalse(nc.hasForbiddenCapability(NET_CAPABILITY_OEM_PRIVATE)); - assertFalse(nc.hasCapability(NET_CAPABILITY_OEM_PRIVATE)); - nc.maybeMarkCapabilitiesRestricted(); - assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Adding OEM_PRIVATE to capability list should make network restricted. - nc.addCapability(NET_CAPABILITY_OEM_PRIVATE); - nc.addCapability(NET_CAPABILITY_INTERNET); // Combine with unrestricted capability. - nc.maybeMarkCapabilitiesRestricted(); - assertTrue(nc.hasCapability(NET_CAPABILITY_OEM_PRIVATE)); - assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Now let's make request for OEM_PRIVATE network. - NetworkCapabilities nr = new NetworkCapabilities(); - nr.addCapability(NET_CAPABILITY_OEM_PRIVATE); - nr.maybeMarkCapabilitiesRestricted(); - assertTrue(nr.satisfiedByNetworkCapabilities(nc)); - - // Request fails for network with the default capabilities. - assertFalse(nr.satisfiedByNetworkCapabilities(new NetworkCapabilities())); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testForbiddenCapabilities() { - NetworkCapabilities network = new NetworkCapabilities(); - - NetworkCapabilities request = new NetworkCapabilities(); - assertTrue("Request: " + request + ", Network:" + network, - request.satisfiedByNetworkCapabilities(network)); - - // Requesting absence of capabilities that network doesn't have. Request should satisfy. - request.addForbiddenCapability(NET_CAPABILITY_WIFI_P2P); - request.addForbiddenCapability(NET_CAPABILITY_NOT_METERED); - assertTrue(request.satisfiedByNetworkCapabilities(network)); - assertArrayEquals(new int[]{NET_CAPABILITY_WIFI_P2P, - NET_CAPABILITY_NOT_METERED}, - request.getForbiddenCapabilities()); - - // This is a default capability, just want to make sure its there because we use it below. - assertTrue(network.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Verify that adding forbidden capability will effectively remove it from capability list. - request.addForbiddenCapability(NET_CAPABILITY_NOT_RESTRICTED); - assertTrue(request.hasForbiddenCapability(NET_CAPABILITY_NOT_RESTRICTED)); - assertFalse(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - // Now this request won't be satisfied because network contains NOT_RESTRICTED. - assertFalse(request.satisfiedByNetworkCapabilities(network)); - network.removeCapability(NET_CAPABILITY_NOT_RESTRICTED); - assertTrue(request.satisfiedByNetworkCapabilities(network)); - - // Verify that adding capability will effectively remove it from forbidden list - request.addCapability(NET_CAPABILITY_NOT_RESTRICTED); - assertTrue(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)); - assertFalse(request.hasForbiddenCapability(NET_CAPABILITY_NOT_RESTRICTED)); - - assertFalse(request.satisfiedByNetworkCapabilities(network)); - network.addCapability(NET_CAPABILITY_NOT_RESTRICTED); - assertTrue(request.satisfiedByNetworkCapabilities(network)); - } - - @Test - public void testConnectivityManagedCapabilities() { - NetworkCapabilities nc = new NetworkCapabilities(); - assertFalse(nc.hasConnectivityManagedCapability()); - // Check every single system managed capability. - nc.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); - assertTrue(nc.hasConnectivityManagedCapability()); - nc.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); - nc.addCapability(NET_CAPABILITY_FOREGROUND); - assertTrue(nc.hasConnectivityManagedCapability()); - nc.removeCapability(NET_CAPABILITY_FOREGROUND); - nc.addCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); - assertTrue(nc.hasConnectivityManagedCapability()); - nc.removeCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); - nc.addCapability(NET_CAPABILITY_VALIDATED); - assertTrue(nc.hasConnectivityManagedCapability()); - } - - @Test - public void testEqualsNetCapabilities() { - NetworkCapabilities nc1 = new NetworkCapabilities(); - NetworkCapabilities nc2 = new NetworkCapabilities(); - assertTrue(nc1.equalsNetCapabilities(nc2)); - assertEquals(nc1, nc2); - - nc1.addCapability(NET_CAPABILITY_MMS); - assertFalse(nc1.equalsNetCapabilities(nc2)); - assertNotEquals(nc1, nc2); - nc2.addCapability(NET_CAPABILITY_MMS); - assertTrue(nc1.equalsNetCapabilities(nc2)); - assertEquals(nc1, nc2); - - if (isAtLeastS()) { - nc1.addForbiddenCapability(NET_CAPABILITY_INTERNET); - assertFalse(nc1.equalsNetCapabilities(nc2)); - nc2.addForbiddenCapability(NET_CAPABILITY_INTERNET); - assertTrue(nc1.equalsNetCapabilities(nc2)); - - // Remove a required capability doesn't affect forbidden capabilities. - // This is a behaviour change from R to S. - nc1.removeCapability(NET_CAPABILITY_INTERNET); - assertTrue(nc1.equalsNetCapabilities(nc2)); - - nc1.removeForbiddenCapability(NET_CAPABILITY_INTERNET); - assertFalse(nc1.equalsNetCapabilities(nc2)); - nc2.removeForbiddenCapability(NET_CAPABILITY_INTERNET); - assertTrue(nc1.equalsNetCapabilities(nc2)); - } - } - - @Test - public void testSSID() { - NetworkCapabilities nc1 = new NetworkCapabilities(); - NetworkCapabilities nc2 = new NetworkCapabilities(); - assertTrue(nc2.satisfiedBySSID(nc1)); - - nc1.setSSID(TEST_SSID); - assertTrue(nc2.satisfiedBySSID(nc1)); - nc2.setSSID("different " + TEST_SSID); - assertFalse(nc2.satisfiedBySSID(nc1)); - - assertTrue(nc1.satisfiedByImmutableNetworkCapabilities(nc2)); - assertFalse(nc1.satisfiedByNetworkCapabilities(nc2)); - } - - private ArraySet<Range<Integer>> uidRanges(int from, int to) { - final ArraySet<Range<Integer>> range = new ArraySet<>(1); - range.add(uidRange(from, to)); - return range; - } - - private Range<Integer> uidRange(int from, int to) { - return new Range<Integer>(from, to); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testSetAdministratorUids() { - NetworkCapabilities nc = - new NetworkCapabilities().setAdministratorUids(new int[] {2, 1, 3}); - - assertArrayEquals(new int[] {1, 2, 3}, nc.getAdministratorUids()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testSetAdministratorUidsWithDuplicates() { - try { - new NetworkCapabilities().setAdministratorUids(new int[] {1, 1}); - fail("Expected IllegalArgumentException for duplicate uids"); - } catch (IllegalArgumentException expected) { - } - } - - @Test - public void testCombineCapabilities() { - NetworkCapabilities nc1 = new NetworkCapabilities(); - NetworkCapabilities nc2 = new NetworkCapabilities(); - - if (isAtLeastS()) { - nc1.addForbiddenCapability(NET_CAPABILITY_CAPTIVE_PORTAL); - } - nc1.addCapability(NET_CAPABILITY_NOT_ROAMING); - assertNotEquals(nc1, nc2); - nc2.combineCapabilities(nc1); - assertEquals(nc1, nc2); - assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING)); - if (isAtLeastS()) { - assertTrue(nc2.hasForbiddenCapability(NET_CAPABILITY_CAPTIVE_PORTAL)); - } - - if (isAtLeastS()) { - // This will effectively move NOT_ROAMING capability from required to forbidden for nc1. - nc1.addForbiddenCapability(NET_CAPABILITY_NOT_ROAMING); - // It is not allowed to have the same capability in both wanted and forbidden list. - assertThrows(IllegalArgumentException.class, () -> nc2.combineCapabilities(nc1)); - // Remove forbidden capability to continue other tests. - nc1.removeForbiddenCapability(NET_CAPABILITY_NOT_ROAMING); - } - - nc1.setSSID(TEST_SSID); - nc2.combineCapabilities(nc1); - if (isAtLeastR()) { - assertTrue(TEST_SSID.equals(nc2.getSsid())); - } - - // Because they now have the same SSID, the following call should not throw - nc2.combineCapabilities(nc1); - - nc1.setSSID(DIFFERENT_TEST_SSID); - try { - nc2.combineCapabilities(nc1); - fail("Expected IllegalStateException: can't combine different SSIDs"); - } catch (IllegalStateException expected) {} - nc1.setSSID(TEST_SSID); - - if (isAtLeastS()) { - nc1.setUids(uidRanges(10, 13)); - assertNotEquals(nc1, nc2); - nc2.combineCapabilities(nc1); // Everything + 10~13 is still everything. - assertNotEquals(nc1, nc2); - nc1.combineCapabilities(nc2); // 10~13 + everything is everything. - assertEquals(nc1, nc2); - nc1.setUids(uidRanges(10, 13)); - nc2.setUids(uidRanges(20, 23)); - assertNotEquals(nc1, nc2); - nc1.combineCapabilities(nc2); - assertTrue(nc1.appliesToUid(12)); - assertFalse(nc2.appliesToUid(12)); - assertTrue(nc1.appliesToUid(22)); - assertTrue(nc2.appliesToUid(22)); - - // Verify the subscription id list can be combined only when they are equal. - nc1.setSubscriptionIds(Set.of(TEST_SUBID1, TEST_SUBID2)); - nc2.setSubscriptionIds(Set.of(TEST_SUBID2)); - assertThrows(IllegalStateException.class, () -> nc2.combineCapabilities(nc1)); - - nc2.setSubscriptionIds(Set.of()); - assertThrows(IllegalStateException.class, () -> nc2.combineCapabilities(nc1)); - - nc2.setSubscriptionIds(Set.of(TEST_SUBID2, TEST_SUBID1)); - nc2.combineCapabilities(nc1); - assertEquals(Set.of(TEST_SUBID2, TEST_SUBID1), nc2.getSubscriptionIds()); - } - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testCombineCapabilities_AdministratorUids() { - final NetworkCapabilities nc1 = new NetworkCapabilities(); - final NetworkCapabilities nc2 = new NetworkCapabilities(); - - final int[] adminUids = {3, 6, 12}; - nc1.setAdministratorUids(adminUids); - nc2.combineCapabilities(nc1); - assertTrue(nc2.equalsAdministratorUids(nc1)); - assertArrayEquals(nc2.getAdministratorUids(), adminUids); - - final int[] adminUidsOtherOrder = {3, 12, 6}; - nc1.setAdministratorUids(adminUidsOtherOrder); - assertTrue(nc2.equalsAdministratorUids(nc1)); - - final int[] adminUids2 = {11, 1, 12, 3, 6}; - nc1.setAdministratorUids(adminUids2); - assertFalse(nc2.equalsAdministratorUids(nc1)); - assertFalse(Arrays.equals(nc2.getAdministratorUids(), adminUids2)); - try { - nc2.combineCapabilities(nc1); - fail("Shouldn't be able to combine different lists of admin UIDs"); - } catch (IllegalStateException expected) { } - } - - @Test - public void testSetCapabilities() { - final int[] REQUIRED_CAPABILITIES = new int[] { - NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN }; - - NetworkCapabilities nc1 = new NetworkCapabilities(); - NetworkCapabilities nc2 = new NetworkCapabilities(); - - nc1.setCapabilities(REQUIRED_CAPABILITIES); - assertArrayEquals(REQUIRED_CAPABILITIES, nc1.getCapabilities()); - - // Verify that setting and adding capabilities leads to the same object state. - nc2.clearAll(); - for (int cap : REQUIRED_CAPABILITIES) { - nc2.addCapability(cap); - } - assertEquals(nc1, nc2); - - if (isAtLeastS()) { - final int[] forbiddenCapabilities = new int[]{ - NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_NOT_RESTRICTED }; - - nc1.setCapabilities(REQUIRED_CAPABILITIES, forbiddenCapabilities); - assertArrayEquals(REQUIRED_CAPABILITIES, nc1.getCapabilities()); - assertArrayEquals(forbiddenCapabilities, nc1.getForbiddenCapabilities()); - - nc2.clearAll(); - for (int cap : REQUIRED_CAPABILITIES) { - nc2.addCapability(cap); - } - for (int cap : forbiddenCapabilities) { - nc2.addForbiddenCapability(cap); - } - assertEquals(nc1, nc2); - } - } - - @Test - public void testSetNetworkSpecifierOnMultiTransportNc() { - // Sequence 1: Transport + Transport + NetworkSpecifier - NetworkCapabilities nc1 = new NetworkCapabilities(); - nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI); - try { - nc1.setNetworkSpecifier(CompatUtil.makeEthernetNetworkSpecifier("eth0")); - fail("Cannot set NetworkSpecifier on a NetworkCapability with multiple transports!"); - } catch (IllegalStateException expected) { - // empty - } - - // Sequence 2: Transport + NetworkSpecifier + Transport - NetworkCapabilities nc2 = new NetworkCapabilities(); - nc2.addTransportType(TRANSPORT_CELLULAR).setNetworkSpecifier( - CompatUtil.makeEthernetNetworkSpecifier("testtap3")); - try { - nc2.addTransportType(TRANSPORT_WIFI); - fail("Cannot set a second TransportType of a network which has a NetworkSpecifier!"); - } catch (IllegalStateException expected) { - // empty - } - } - - @Test - public void testSetTransportInfoOnMultiTransportNc() { - // Sequence 1: Transport + Transport + TransportInfo - NetworkCapabilities nc1 = new NetworkCapabilities(); - nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI) - .setTransportInfo(new TestTransportInfo()); - - // Sequence 2: Transport + NetworkSpecifier + Transport - NetworkCapabilities nc2 = new NetworkCapabilities(); - nc2.addTransportType(TRANSPORT_CELLULAR).setTransportInfo(new TestTransportInfo()) - .addTransportType(TRANSPORT_WIFI); - } - - @Test - public void testCombineTransportInfo() { - NetworkCapabilities nc1 = new NetworkCapabilities(); - nc1.setTransportInfo(new TestTransportInfo()); - - NetworkCapabilities nc2 = new NetworkCapabilities(); - // new TransportInfo so that object is not #equals to nc1's TransportInfo (that's where - // combine fails) - nc2.setTransportInfo(new TestTransportInfo()); - - try { - nc1.combineCapabilities(nc2); - fail("Should not be able to combine NetworkCabilities which contain TransportInfos"); - } catch (IllegalStateException expected) { - // empty - } - - // verify that can combine with identical TransportInfo objects - NetworkCapabilities nc3 = new NetworkCapabilities(); - nc3.setTransportInfo(nc1.getTransportInfo()); - nc1.combineCapabilities(nc3); - } - - @Test - public void testSet() { - NetworkCapabilities nc1 = new NetworkCapabilities(); - NetworkCapabilities nc2 = new NetworkCapabilities(); - - if (isAtLeastS()) { - nc1.addForbiddenCapability(NET_CAPABILITY_CAPTIVE_PORTAL); - } - nc1.addCapability(NET_CAPABILITY_NOT_ROAMING); - assertNotEquals(nc1, nc2); - nc2.set(nc1); - assertEquals(nc1, nc2); - assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING)); - if (isAtLeastS()) { - assertTrue(nc2.hasForbiddenCapability(NET_CAPABILITY_CAPTIVE_PORTAL)); - } - - if (isAtLeastS()) { - // This will effectively move NOT_ROAMING capability from required to forbidden for nc1. - nc1.addForbiddenCapability(NET_CAPABILITY_NOT_ROAMING); - } - nc1.setSSID(TEST_SSID); - nc2.set(nc1); - assertEquals(nc1, nc2); - if (isAtLeastS()) { - // Contrary to combineCapabilities, set() will have removed the NOT_ROAMING capability - // from nc2. - assertFalse(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING)); - assertTrue(nc2.hasForbiddenCapability(NET_CAPABILITY_NOT_ROAMING)); - } - - if (isAtLeastR()) { - assertTrue(TEST_SSID.equals(nc2.getSsid())); - } - - nc1.setSSID(DIFFERENT_TEST_SSID); - nc2.set(nc1); - assertEquals(nc1, nc2); - if (isAtLeastR()) { - assertTrue(DIFFERENT_TEST_SSID.equals(nc2.getSsid())); - } - if (isAtLeastS()) { - nc1.setUids(uidRanges(10, 13)); - } else { - nc1.setUids(null); - } - nc2.set(nc1); // Overwrites, as opposed to combineCapabilities - assertEquals(nc1, nc2); - - if (isAtLeastS()) { - assertThrows(NullPointerException.class, () -> nc1.setSubscriptionIds(null)); - nc1.setSubscriptionIds(Set.of()); - nc2.set(nc1); - assertEquals(nc1, nc2); - - nc1.setSubscriptionIds(Set.of(TEST_SUBID1)); - nc2.set(nc1); - assertEquals(nc1, nc2); - - nc2.setSubscriptionIds(Set.of(TEST_SUBID2, TEST_SUBID1)); - nc2.set(nc1); - assertEquals(nc1, nc2); - - nc2.setSubscriptionIds(Set.of(TEST_SUBID3, TEST_SUBID2)); - assertNotEquals(nc1, nc2); - } - } - - @Test - public void testGetTransportTypes() { - final NetworkCapabilities nc = new NetworkCapabilities(); - nc.addTransportType(TRANSPORT_CELLULAR); - nc.addTransportType(TRANSPORT_WIFI); - nc.addTransportType(TRANSPORT_VPN); - nc.addTransportType(TRANSPORT_TEST); - - final int[] transportTypes = nc.getTransportTypes(); - assertEquals(4, transportTypes.length); - assertEquals(TRANSPORT_CELLULAR, transportTypes[0]); - assertEquals(TRANSPORT_WIFI, transportTypes[1]); - assertEquals(TRANSPORT_VPN, transportTypes[2]); - assertEquals(TRANSPORT_TEST, transportTypes[3]); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testTelephonyNetworkSpecifier() { - final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier(1); - final NetworkCapabilities nc1 = new NetworkCapabilities.Builder() - .addTransportType(TRANSPORT_WIFI) - .setNetworkSpecifier(specifier) - .build(); - assertEquals(specifier, nc1.getNetworkSpecifier()); - try { - final NetworkCapabilities nc2 = new NetworkCapabilities.Builder() - .setNetworkSpecifier(specifier) - .build(); - fail("Must have a single transport type. Without transport type or multiple transport" - + " types is invalid."); - } catch (IllegalStateException expected) { } - } - - @Test - public void testWifiAwareNetworkSpecifier() { - final NetworkCapabilities nc = new NetworkCapabilities() - .addTransportType(TRANSPORT_WIFI_AWARE); - // If NetworkSpecifier is not set, the default value is null. - assertNull(nc.getNetworkSpecifier()); - final WifiAwareNetworkSpecifier specifier = new WifiAwareNetworkSpecifier.Builder( - mDiscoverySession, mPeerHandle).build(); - nc.setNetworkSpecifier(specifier); - assertEquals(specifier, nc.getNetworkSpecifier()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testAdministratorUidsAndOwnerUid() { - // Test default owner uid. - // If the owner uid is not set, the default value should be Process.INVALID_UID. - final NetworkCapabilities nc1 = new NetworkCapabilities.Builder().build(); - assertEquals(INVALID_UID, nc1.getOwnerUid()); - // Test setAdministratorUids and getAdministratorUids. - final int[] administratorUids = {1001, 10001}; - final NetworkCapabilities nc2 = new NetworkCapabilities.Builder() - .setAdministratorUids(administratorUids) - .build(); - assertTrue(Arrays.equals(administratorUids, nc2.getAdministratorUids())); - // Test setOwnerUid and getOwnerUid. - // The owner UID must be included in administrator UIDs, or throw IllegalStateException. - try { - final NetworkCapabilities nc3 = new NetworkCapabilities.Builder() - .setOwnerUid(1001) - .build(); - fail("The owner UID must be included in administrator UIDs."); - } catch (IllegalStateException expected) { } - final NetworkCapabilities nc4 = new NetworkCapabilities.Builder() - .setAdministratorUids(administratorUids) - .setOwnerUid(1001) - .build(); - assertEquals(1001, nc4.getOwnerUid()); - try { - final NetworkCapabilities nc5 = new NetworkCapabilities.Builder() - .setAdministratorUids(null) - .build(); - fail("Should not set null into setAdministratorUids"); - } catch (NullPointerException expected) { } - } - - private static NetworkCapabilities capsWithSubIds(Integer ... subIds) { - // Since the NetworkRequest would put NOT_VCN_MANAGED capabilities in general, for - // every NetworkCapabilities that simulates networks needs to add it too in order to - // satisfy these requests. - final NetworkCapabilities nc = new NetworkCapabilities.Builder() - .addCapability(NET_CAPABILITY_NOT_VCN_MANAGED) - .setSubscriptionIds(new ArraySet<>(subIds)).build(); - assertEquals(new ArraySet<>(subIds), nc.getSubscriptionIds()); - return nc; - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testSubIds() throws Exception { - final NetworkCapabilities ncWithoutId = capsWithSubIds(); - final NetworkCapabilities ncWithId = capsWithSubIds(TEST_SUBID1); - final NetworkCapabilities ncWithOtherIds = capsWithSubIds(TEST_SUBID1, TEST_SUBID3); - final NetworkCapabilities ncWithoutRequestedIds = capsWithSubIds(TEST_SUBID3); - - final NetworkRequest requestWithoutId = new NetworkRequest.Builder().build(); - assertEmpty(requestWithoutId.networkCapabilities.getSubscriptionIds()); - final NetworkRequest requestWithIds = new NetworkRequest.Builder() - .setSubscriptionIds(Set.of(TEST_SUBID1, TEST_SUBID2)).build(); - assertEquals(Set.of(TEST_SUBID1, TEST_SUBID2), - requestWithIds.networkCapabilities.getSubscriptionIds()); - - assertFalse(requestWithIds.canBeSatisfiedBy(ncWithoutId)); - assertTrue(requestWithIds.canBeSatisfiedBy(ncWithOtherIds)); - assertFalse(requestWithIds.canBeSatisfiedBy(ncWithoutRequestedIds)); - assertTrue(requestWithIds.canBeSatisfiedBy(ncWithId)); - assertTrue(requestWithoutId.canBeSatisfiedBy(ncWithoutId)); - assertTrue(requestWithoutId.canBeSatisfiedBy(ncWithId)); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testEqualsSubIds() throws Exception { - assertEquals(capsWithSubIds(), capsWithSubIds()); - assertNotEquals(capsWithSubIds(), capsWithSubIds(TEST_SUBID1)); - assertEquals(capsWithSubIds(TEST_SUBID1), capsWithSubIds(TEST_SUBID1)); - assertNotEquals(capsWithSubIds(TEST_SUBID1), capsWithSubIds(TEST_SUBID2)); - assertNotEquals(capsWithSubIds(TEST_SUBID1), capsWithSubIds(TEST_SUBID2, TEST_SUBID1)); - assertEquals(capsWithSubIds(TEST_SUBID1, TEST_SUBID2), - capsWithSubIds(TEST_SUBID2, TEST_SUBID1)); - } - - @Test - public void testLinkBandwidthKbps() { - final NetworkCapabilities nc = new NetworkCapabilities(); - // The default value of LinkDown/UpstreamBandwidthKbps should be LINK_BANDWIDTH_UNSPECIFIED. - assertEquals(LINK_BANDWIDTH_UNSPECIFIED, nc.getLinkDownstreamBandwidthKbps()); - assertEquals(LINK_BANDWIDTH_UNSPECIFIED, nc.getLinkUpstreamBandwidthKbps()); - nc.setLinkDownstreamBandwidthKbps(512); - nc.setLinkUpstreamBandwidthKbps(128); - assertEquals(512, nc.getLinkDownstreamBandwidthKbps()); - assertNotEquals(128, nc.getLinkDownstreamBandwidthKbps()); - assertEquals(128, nc.getLinkUpstreamBandwidthKbps()); - assertNotEquals(512, nc.getLinkUpstreamBandwidthKbps()); - } - - private int getMaxTransport() { - if (!isAtLeastS() && MAX_TRANSPORT == TRANSPORT_USB) return MAX_TRANSPORT - 1; - return MAX_TRANSPORT; - } - - @Test - public void testSignalStrength() { - final NetworkCapabilities nc = new NetworkCapabilities(); - // The default value of signal strength should be SIGNAL_STRENGTH_UNSPECIFIED. - assertEquals(SIGNAL_STRENGTH_UNSPECIFIED, nc.getSignalStrength()); - nc.setSignalStrength(-80); - assertEquals(-80, nc.getSignalStrength()); - assertNotEquals(-50, nc.getSignalStrength()); - } - - private void assertNoTransport(NetworkCapabilities nc) { - for (int i = MIN_TRANSPORT; i <= getMaxTransport(); i++) { - assertFalse(nc.hasTransport(i)); - } - } - - // Checks that all transport types from MIN_TRANSPORT to maxTransportType are set and all - // transport types from maxTransportType + 1 to MAX_TRANSPORT are not set when positiveSequence - // is true. If positiveSequence is false, then the check sequence is opposite. - private void checkCurrentTransportTypes(NetworkCapabilities nc, int maxTransportType, - boolean positiveSequence) { - for (int i = MIN_TRANSPORT; i <= maxTransportType; i++) { - if (positiveSequence) { - assertTrue(nc.hasTransport(i)); - } else { - assertFalse(nc.hasTransport(i)); - } - } - for (int i = getMaxTransport(); i > maxTransportType; i--) { - if (positiveSequence) { - assertFalse(nc.hasTransport(i)); - } else { - assertTrue(nc.hasTransport(i)); - } - } - } - - @Test - public void testMultipleTransportTypes() { - final NetworkCapabilities nc = new NetworkCapabilities(); - assertNoTransport(nc); - // Test adding multiple transport types. - for (int i = MIN_TRANSPORT; i <= getMaxTransport(); i++) { - nc.addTransportType(i); - checkCurrentTransportTypes(nc, i, true /* positiveSequence */); - } - // Test removing multiple transport types. - for (int i = MIN_TRANSPORT; i <= getMaxTransport(); i++) { - nc.removeTransportType(i); - checkCurrentTransportTypes(nc, i, false /* positiveSequence */); - } - assertNoTransport(nc); - nc.addTransportType(TRANSPORT_WIFI); - assertTrue(nc.hasTransport(TRANSPORT_WIFI)); - assertFalse(nc.hasTransport(TRANSPORT_VPN)); - nc.addTransportType(TRANSPORT_VPN); - assertTrue(nc.hasTransport(TRANSPORT_WIFI)); - assertTrue(nc.hasTransport(TRANSPORT_VPN)); - nc.removeTransportType(TRANSPORT_WIFI); - assertFalse(nc.hasTransport(TRANSPORT_WIFI)); - assertTrue(nc.hasTransport(TRANSPORT_VPN)); - nc.removeTransportType(TRANSPORT_VPN); - assertFalse(nc.hasTransport(TRANSPORT_WIFI)); - assertFalse(nc.hasTransport(TRANSPORT_VPN)); - assertNoTransport(nc); - } - - @Test - public void testAddAndRemoveTransportType() { - final NetworkCapabilities nc = new NetworkCapabilities(); - try { - nc.addTransportType(-1); - fail("Should not set invalid transport type into addTransportType"); - } catch (IllegalArgumentException expected) { } - try { - nc.removeTransportType(-1); - fail("Should not set invalid transport type into removeTransportType"); - } catch (IllegalArgumentException e) { } - } - - /** - * Test TransportInfo to verify redaction mechanism. - */ - private static class TestTransportInfo implements TransportInfo { - public final boolean locationRedacted; - public final boolean localMacAddressRedacted; - public final boolean settingsRedacted; - - TestTransportInfo() { - locationRedacted = false; - localMacAddressRedacted = false; - settingsRedacted = false; - } - - TestTransportInfo(boolean locationRedacted, - boolean localMacAddressRedacted, - boolean settingsRedacted) { - this.locationRedacted = locationRedacted; - this.localMacAddressRedacted = - localMacAddressRedacted; - this.settingsRedacted = settingsRedacted; - } - - @Override - public TransportInfo makeCopy(@NetworkCapabilities.RedactionType long redactions) { - return new TestTransportInfo( - (redactions & NetworkCapabilities.REDACT_FOR_ACCESS_FINE_LOCATION) != 0, - (redactions & REDACT_FOR_LOCAL_MAC_ADDRESS) != 0, - (redactions & REDACT_FOR_NETWORK_SETTINGS) != 0 - ); - } - - @Override - public @NetworkCapabilities.RedactionType long getApplicableRedactions() { - return REDACT_FOR_ACCESS_FINE_LOCATION | REDACT_FOR_LOCAL_MAC_ADDRESS - | REDACT_FOR_NETWORK_SETTINGS; - } - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testBuilder() { - final int ownerUid = 1001; - final int signalStrength = -80; - final int requestUid = 10100; - final int[] administratorUids = {ownerUid, 10001}; - final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier(1); - final TransportInfo transportInfo = new TransportInfo() {}; - final String ssid = "TEST_SSID"; - final String packageName = "com.google.test.networkcapabilities"; - final NetworkCapabilities nc = new NetworkCapabilities.Builder() - .addTransportType(TRANSPORT_WIFI) - .addTransportType(TRANSPORT_CELLULAR) - .removeTransportType(TRANSPORT_CELLULAR) - .addCapability(NET_CAPABILITY_EIMS) - .addCapability(NET_CAPABILITY_CBS) - .removeCapability(NET_CAPABILITY_CBS) - .setAdministratorUids(administratorUids) - .setOwnerUid(ownerUid) - .setLinkDownstreamBandwidthKbps(512) - .setLinkUpstreamBandwidthKbps(128) - .setNetworkSpecifier(specifier) - .setTransportInfo(transportInfo) - .setSignalStrength(signalStrength) - .setSsid(ssid) - .setRequestorUid(requestUid) - .setRequestorPackageName(packageName) - .build(); - assertEquals(1, nc.getTransportTypes().length); - assertEquals(TRANSPORT_WIFI, nc.getTransportTypes()[0]); - assertTrue(nc.hasCapability(NET_CAPABILITY_EIMS)); - assertFalse(nc.hasCapability(NET_CAPABILITY_CBS)); - assertTrue(Arrays.equals(administratorUids, nc.getAdministratorUids())); - assertEquals(ownerUid, nc.getOwnerUid()); - assertEquals(512, nc.getLinkDownstreamBandwidthKbps()); - assertNotEquals(128, nc.getLinkDownstreamBandwidthKbps()); - assertEquals(128, nc.getLinkUpstreamBandwidthKbps()); - assertNotEquals(512, nc.getLinkUpstreamBandwidthKbps()); - assertEquals(specifier, nc.getNetworkSpecifier()); - assertEquals(transportInfo, nc.getTransportInfo()); - assertEquals(signalStrength, nc.getSignalStrength()); - assertNotEquals(-50, nc.getSignalStrength()); - assertEquals(ssid, nc.getSsid()); - assertEquals(requestUid, nc.getRequestorUid()); - assertEquals(packageName, nc.getRequestorPackageName()); - // Cannot assign null into NetworkCapabilities.Builder - try { - final NetworkCapabilities.Builder builder = new NetworkCapabilities.Builder(null); - fail("Should not set null into NetworkCapabilities.Builder"); - } catch (NullPointerException expected) { } - assertEquals(nc, new NetworkCapabilities.Builder(nc).build()); - - if (isAtLeastS()) { - final NetworkCapabilities nc2 = new NetworkCapabilities.Builder() - .setSubscriptionIds(Set.of(TEST_SUBID1)).build(); - assertEquals(Set.of(TEST_SUBID1), nc2.getSubscriptionIds()); - } - } -} diff --git a/tests/net/common/java/android/net/NetworkProviderTest.kt b/tests/net/common/java/android/net/NetworkProviderTest.kt deleted file mode 100644 index 7424157bea74..000000000000 --- a/tests/net/common/java/android/net/NetworkProviderTest.kt +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.app.Instrumentation -import android.content.Context -import android.net.NetworkCapabilities.TRANSPORT_TEST -import android.net.NetworkProviderTest.TestNetworkCallback.CallbackEntry.OnUnavailable -import android.net.NetworkProviderTest.TestNetworkProvider.CallbackEntry.OnNetworkRequestWithdrawn -import android.net.NetworkProviderTest.TestNetworkProvider.CallbackEntry.OnNetworkRequested -import android.os.Build -import android.os.HandlerThread -import android.os.Looper -import androidx.test.InstrumentationRegistry -import com.android.net.module.util.ArrayTrackRecord -import com.android.testutils.CompatUtil -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import com.android.testutils.DevSdkIgnoreRunner -import com.android.testutils.isDevSdkInRange -import org.junit.After -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.mock -import org.mockito.Mockito.verifyNoMoreInteractions -import java.util.UUID -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals - -private const val DEFAULT_TIMEOUT_MS = 5000L -private val instrumentation: Instrumentation - get() = InstrumentationRegistry.getInstrumentation() -private val context: Context get() = InstrumentationRegistry.getContext() -private val PROVIDER_NAME = "NetworkProviderTest" - -@RunWith(DevSdkIgnoreRunner::class) -@IgnoreUpTo(Build.VERSION_CODES.Q) -class NetworkProviderTest { - private val mCm = context.getSystemService(ConnectivityManager::class.java) - private val mHandlerThread = HandlerThread("${javaClass.simpleName} handler thread") - - @Before - fun setUp() { - instrumentation.getUiAutomation().adoptShellPermissionIdentity() - mHandlerThread.start() - } - - @After - fun tearDown() { - mHandlerThread.quitSafely() - instrumentation.getUiAutomation().dropShellPermissionIdentity() - } - - private class TestNetworkProvider(context: Context, looper: Looper) : - NetworkProvider(context, looper, PROVIDER_NAME) { - private val seenEvents = ArrayTrackRecord<CallbackEntry>().newReadHead() - - sealed class CallbackEntry { - data class OnNetworkRequested( - val request: NetworkRequest, - val score: Int, - val id: Int - ) : CallbackEntry() - data class OnNetworkRequestWithdrawn(val request: NetworkRequest) : CallbackEntry() - } - - override fun onNetworkRequested(request: NetworkRequest, score: Int, id: Int) { - seenEvents.add(OnNetworkRequested(request, score, id)) - } - - override fun onNetworkRequestWithdrawn(request: NetworkRequest) { - seenEvents.add(OnNetworkRequestWithdrawn(request)) - } - - inline fun <reified T : CallbackEntry> expectCallback( - crossinline predicate: (T) -> Boolean - ) = seenEvents.poll(DEFAULT_TIMEOUT_MS) { it is T && predicate(it) } - } - - private fun createNetworkProvider(ctx: Context = context): TestNetworkProvider { - return TestNetworkProvider(ctx, mHandlerThread.looper) - } - - @Test - fun testOnNetworkRequested() { - val provider = createNetworkProvider() - assertEquals(provider.getProviderId(), NetworkProvider.ID_NONE) - mCm.registerNetworkProvider(provider) - assertNotEquals(provider.getProviderId(), NetworkProvider.ID_NONE) - - val specifier = CompatUtil.makeTestNetworkSpecifier( - UUID.randomUUID().toString()) - val nr: NetworkRequest = NetworkRequest.Builder() - .addTransportType(TRANSPORT_TEST) - .setNetworkSpecifier(specifier) - .build() - val cb = ConnectivityManager.NetworkCallback() - mCm.requestNetwork(nr, cb) - provider.expectCallback<OnNetworkRequested>() { callback -> - callback.request.getNetworkSpecifier() == specifier && - callback.request.hasTransport(TRANSPORT_TEST) - } - - val initialScore = 40 - val updatedScore = 60 - val nc = NetworkCapabilities().apply { - addTransportType(NetworkCapabilities.TRANSPORT_TEST) - removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED) - removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED) - addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING) - addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) - setNetworkSpecifier(specifier) - } - val lp = LinkProperties() - val config = NetworkAgentConfig.Builder().build() - val agent = object : NetworkAgent(context, mHandlerThread.looper, "TestAgent", nc, lp, - initialScore, config, provider) {} - - provider.expectCallback<OnNetworkRequested>() { callback -> - callback.request.getNetworkSpecifier() == specifier && - callback.score == initialScore && - callback.id == agent.providerId - } - - agent.sendNetworkScore(updatedScore) - provider.expectCallback<OnNetworkRequested>() { callback -> - callback.request.getNetworkSpecifier() == specifier && - callback.score == updatedScore && - callback.id == agent.providerId - } - - mCm.unregisterNetworkCallback(cb) - provider.expectCallback<OnNetworkRequestWithdrawn>() { callback -> - callback.request.getNetworkSpecifier() == specifier && - callback.request.hasTransport(TRANSPORT_TEST) - } - mCm.unregisterNetworkProvider(provider) - // Provider id should be ID_NONE after unregister network provider - assertEquals(provider.getProviderId(), NetworkProvider.ID_NONE) - // unregisterNetworkProvider should not crash even if it's called on an - // already unregistered provider. - mCm.unregisterNetworkProvider(provider) - } - - private class TestNetworkCallback : ConnectivityManager.NetworkCallback() { - private val seenEvents = ArrayTrackRecord<CallbackEntry>().newReadHead() - sealed class CallbackEntry { - object OnUnavailable : CallbackEntry() - } - - override fun onUnavailable() { - seenEvents.add(OnUnavailable) - } - - inline fun <reified T : CallbackEntry> expectCallback( - crossinline predicate: (T) -> Boolean - ) = seenEvents.poll(DEFAULT_TIMEOUT_MS) { it is T && predicate(it) } - } - - @Test - fun testDeclareNetworkRequestUnfulfillable() { - val mockContext = mock(Context::class.java) - doReturn(mCm).`when`(mockContext).getSystemService(Context.CONNECTIVITY_SERVICE) - val provider = createNetworkProvider(mockContext) - // ConnectivityManager not required at creation time after R - if (!isDevSdkInRange(0, Build.VERSION_CODES.R)) { - verifyNoMoreInteractions(mockContext) - } - - mCm.registerNetworkProvider(provider) - - val specifier = CompatUtil.makeTestNetworkSpecifier( - UUID.randomUUID().toString()) - val nr: NetworkRequest = NetworkRequest.Builder() - .addTransportType(TRANSPORT_TEST) - .setNetworkSpecifier(specifier) - .build() - - val cb = TestNetworkCallback() - mCm.requestNetwork(nr, cb) - provider.declareNetworkRequestUnfulfillable(nr) - cb.expectCallback<OnUnavailable>() { nr.getNetworkSpecifier() == specifier } - mCm.unregisterNetworkProvider(provider) - } -} diff --git a/tests/net/common/java/android/net/NetworkSpecifierTest.kt b/tests/net/common/java/android/net/NetworkSpecifierTest.kt deleted file mode 100644 index f3409f53596f..000000000000 --- a/tests/net/common/java/android/net/NetworkSpecifierTest.kt +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.net - -import android.os.Build -import androidx.test.filters.SmallTest -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import com.android.testutils.DevSdkIgnoreRunner -import kotlin.test.assertTrue -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertNotEquals -import org.junit.Test -import org.junit.runner.RunWith - -@SmallTest -@RunWith(DevSdkIgnoreRunner::class) -@IgnoreUpTo(Build.VERSION_CODES.Q) -class NetworkSpecifierTest { - private class TestNetworkSpecifier( - val intData: Int = 123, - val stringData: String = "init" - ) : NetworkSpecifier() { - override fun canBeSatisfiedBy(other: NetworkSpecifier?): Boolean = - other != null && - other is TestNetworkSpecifier && - other.intData >= intData && - stringData.equals(other.stringData) - - override fun redact(): NetworkSpecifier = TestNetworkSpecifier(intData, "redact") - } - - @Test - fun testRedact() { - val ns: TestNetworkSpecifier = TestNetworkSpecifier() - val redactNs = ns.redact() - assertTrue(redactNs is TestNetworkSpecifier) - assertEquals(ns.intData, redactNs.intData) - assertNotEquals(ns.stringData, redactNs.stringData) - assertTrue("redact".equals(redactNs.stringData)) - } - - @Test - fun testcanBeSatisfiedBy() { - val target: TestNetworkSpecifier = TestNetworkSpecifier() - assertFalse(target.canBeSatisfiedBy(null)) - assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier())) - val otherNs = TelephonyNetworkSpecifier.Builder().setSubscriptionId(123).build() - assertFalse(target.canBeSatisfiedBy(otherNs)) - assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 999))) - assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 1))) - assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(stringData = "diff"))) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/NetworkStackTest.java b/tests/net/common/java/android/net/NetworkStackTest.java deleted file mode 100644 index f8f9c72374ad..000000000000 --- a/tests/net/common/java/android/net/NetworkStackTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.net; - -import static org.junit.Assert.assertEquals; - -import android.os.Build; -import android.os.IBinder; - -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -@RunWith(AndroidJUnit4.class) -public class NetworkStackTest { - @Rule - public DevSdkIgnoreRule mDevSdkIgnoreRule = new DevSdkIgnoreRule(); - - @Mock private IBinder mConnectorBinder; - - @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testGetService() { - NetworkStack.setServiceForTest(mConnectorBinder); - assertEquals(NetworkStack.getService(), mConnectorBinder); - } -} diff --git a/tests/net/common/java/android/net/NetworkStateSnapshotTest.kt b/tests/net/common/java/android/net/NetworkStateSnapshotTest.kt deleted file mode 100644 index 0ca4d9551f39..000000000000 --- a/tests/net/common/java/android/net/NetworkStateSnapshotTest.kt +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.net.ConnectivityManager.TYPE_NONE -import android.net.ConnectivityManager.TYPE_WIFI -import android.net.InetAddresses.parseNumericAddress -import android.net.NetworkCapabilities.TRANSPORT_WIFI -import android.os.Build -import androidx.test.filters.SmallTest -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRunner -import com.android.testutils.assertParcelSane -import org.junit.Test -import org.junit.runner.RunWith -import java.net.Inet4Address -import java.net.Inet6Address - -private const val TEST_IMSI = "imsi1" -private const val TEST_SSID = "SSID1" -private const val TEST_NETID = 123 - -private val TEST_IPV4_GATEWAY = parseNumericAddress("192.168.222.3") as Inet4Address -private val TEST_IPV6_GATEWAY = parseNumericAddress("2001:db8::1") as Inet6Address -private val TEST_IPV4_LINKADDR = LinkAddress("192.168.222.123/24") -private val TEST_IPV6_LINKADDR = LinkAddress("2001:db8::123/64") -private val TEST_IFACE = "fake0" -private val TEST_LINK_PROPERTIES = LinkProperties().apply { - interfaceName = TEST_IFACE - addLinkAddress(TEST_IPV4_LINKADDR) - addLinkAddress(TEST_IPV6_LINKADDR) - - // Add default routes - addRoute(RouteInfo(IpPrefix(parseNumericAddress("0.0.0.0"), 0), TEST_IPV4_GATEWAY)) - addRoute(RouteInfo(IpPrefix(parseNumericAddress("::"), 0), TEST_IPV6_GATEWAY)) -} - -private val TEST_CAPABILITIES = NetworkCapabilities().apply { - addTransportType(TRANSPORT_WIFI) - setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false) - setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true) - setSSID(TEST_SSID) -} - -@SmallTest -@RunWith(DevSdkIgnoreRunner::class) -@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) -class NetworkStateSnapshotTest { - - @Test - fun testParcelUnparcel() { - val emptySnapshot = NetworkStateSnapshot(Network(TEST_NETID), NetworkCapabilities(), - LinkProperties(), null, TYPE_NONE) - val snapshot = NetworkStateSnapshot( - Network(TEST_NETID), TEST_CAPABILITIES, TEST_LINK_PROPERTIES, TEST_IMSI, TYPE_WIFI) - assertParcelSane(emptySnapshot, 5) - assertParcelSane(snapshot, 5) - } -} diff --git a/tests/net/common/java/android/net/NetworkTest.java b/tests/net/common/java/android/net/NetworkTest.java deleted file mode 100644 index 7423c733c6eb..000000000000 --- a/tests/net/common/java/android/net/NetworkTest.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import android.os.Build; -import android.platform.test.annotations.AppModeFull; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.io.File; -import java.io.FileDescriptor; -import java.io.FileInputStream; -import java.net.DatagramSocket; -import java.net.Inet6Address; -import java.net.InetAddress; -import java.net.SocketException; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class NetworkTest { - final Network mNetwork = new Network(99); - - @Rule - public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); - - @Test - public void testBindSocketOfInvalidFdThrows() throws Exception { - - final FileDescriptor fd = new FileDescriptor(); - assertFalse(fd.valid()); - - try { - mNetwork.bindSocket(fd); - fail("SocketException not thrown"); - } catch (SocketException expected) {} - } - - @Test - public void testBindSocketOfNonSocketFdThrows() throws Exception { - final File devNull = new File("/dev/null"); - assertTrue(devNull.canRead()); - - final FileInputStream fis = new FileInputStream(devNull); - assertTrue(null != fis.getFD()); - assertTrue(fis.getFD().valid()); - - try { - mNetwork.bindSocket(fis.getFD()); - fail("SocketException not thrown"); - } catch (SocketException expected) {} - } - - @Test - @AppModeFull(reason = "Socket cannot bind in instant app mode") - public void testBindSocketOfConnectedDatagramSocketThrows() throws Exception { - final DatagramSocket mDgramSocket = new DatagramSocket(0, (InetAddress) Inet6Address.ANY); - mDgramSocket.connect((InetAddress) Inet6Address.LOOPBACK, 53); - assertTrue(mDgramSocket.isConnected()); - - try { - mNetwork.bindSocket(mDgramSocket); - fail("SocketException not thrown"); - } catch (SocketException expected) {} - } - - @Test - public void testBindSocketOfLocalSocketThrows() throws Exception { - final LocalSocket mLocalClient = new LocalSocket(); - mLocalClient.bind(new LocalSocketAddress("testClient")); - assertTrue(mLocalClient.getFileDescriptor().valid()); - - try { - mNetwork.bindSocket(mLocalClient.getFileDescriptor()); - fail("SocketException not thrown"); - } catch (SocketException expected) {} - - final LocalServerSocket mLocalServer = new LocalServerSocket("testServer"); - mLocalClient.connect(mLocalServer.getLocalSocketAddress()); - assertTrue(mLocalClient.isConnected()); - - try { - mNetwork.bindSocket(mLocalClient.getFileDescriptor()); - fail("SocketException not thrown"); - } catch (SocketException expected) {} - } - - @Test - public void testZeroIsObviousForDebugging() { - Network zero = new Network(0); - assertEquals(0, zero.hashCode()); - assertEquals(0, zero.getNetworkHandle()); - assertEquals("0", zero.toString()); - } - - @Test - public void testGetNetworkHandle() { - Network one = new Network(1); - Network two = new Network(2); - Network three = new Network(3); - - // None of the hashcodes are zero. - assertNotEquals(0, one.hashCode()); - assertNotEquals(0, two.hashCode()); - assertNotEquals(0, three.hashCode()); - - // All the hashcodes are distinct. - assertNotEquals(one.hashCode(), two.hashCode()); - assertNotEquals(one.hashCode(), three.hashCode()); - assertNotEquals(two.hashCode(), three.hashCode()); - - // None of the handles are zero. - assertNotEquals(0, one.getNetworkHandle()); - assertNotEquals(0, two.getNetworkHandle()); - assertNotEquals(0, three.getNetworkHandle()); - - // All the handles are distinct. - assertNotEquals(one.getNetworkHandle(), two.getNetworkHandle()); - assertNotEquals(one.getNetworkHandle(), three.getNetworkHandle()); - assertNotEquals(two.getNetworkHandle(), three.getNetworkHandle()); - - // The handles are not equal to the hashcodes. - assertNotEquals(one.hashCode(), one.getNetworkHandle()); - assertNotEquals(two.hashCode(), two.getNetworkHandle()); - assertNotEquals(three.hashCode(), three.getNetworkHandle()); - - // Adjust as necessary to test an implementation's specific constants. - // When running with runtest, "adb logcat -s TestRunner" can be useful. - assertEquals(7700664333L, one.getNetworkHandle()); - assertEquals(11995631629L, two.getNetworkHandle()); - assertEquals(16290598925L, three.getNetworkHandle()); - } - - // getNetId() did not exist in Q - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testGetNetId() { - assertEquals(1234, new Network(1234).getNetId()); - assertEquals(2345, new Network(2345, true).getNetId()); - } - - @Test - public void testFromNetworkHandle() { - final Network network = new Network(1234); - assertEquals(network.netId, Network.fromNetworkHandle(network.getNetworkHandle()).netId); - } - - // Parsing private DNS bypassing handle was not supported until S - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testFromNetworkHandlePrivateDnsBypass_S() { - final Network network = new Network(1234, true); - - final Network recreatedNetwork = Network.fromNetworkHandle(network.getNetworkHandle()); - assertEquals(network.netId, recreatedNetwork.netId); - assertEquals(network.getNetIdForResolv(), recreatedNetwork.getNetIdForResolv()); - } - - @Test @IgnoreAfter(Build.VERSION_CODES.R) - public void testFromNetworkHandlePrivateDnsBypass_R() { - final Network network = new Network(1234, true); - - final Network recreatedNetwork = Network.fromNetworkHandle(network.getNetworkHandle()); - assertEquals(network.netId, recreatedNetwork.netId); - // Until R included, fromNetworkHandle would not parse the private DNS bypass flag - assertEquals(network.netId, recreatedNetwork.getNetIdForResolv()); - } - - @Test - public void testGetPrivateDnsBypassingCopy() { - final Network copy = mNetwork.getPrivateDnsBypassingCopy(); - assertEquals(mNetwork.netId, copy.netId); - assertNotEquals(copy.netId, copy.getNetIdForResolv()); - assertNotEquals(mNetwork.getNetIdForResolv(), copy.getNetIdForResolv()); - } -} diff --git a/tests/net/common/java/android/net/OemNetworkPreferencesTest.java b/tests/net/common/java/android/net/OemNetworkPreferencesTest.java deleted file mode 100644 index fd29a9539de8..000000000000 --- a/tests/net/common/java/android/net/OemNetworkPreferencesTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static com.android.testutils.MiscAsserts.assertThrows; -import static com.android.testutils.ParcelUtils.assertParcelSane; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import android.os.Build; - -import androidx.test.filters.SmallTest; - -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; -import com.android.testutils.DevSdkIgnoreRunner; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.Map; - -@IgnoreUpTo(Build.VERSION_CODES.R) -@RunWith(DevSdkIgnoreRunner.class) -@SmallTest -public class OemNetworkPreferencesTest { - - private static final int TEST_PREF = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_UNINITIALIZED; - private static final String TEST_PACKAGE = "com.google.apps.contacts"; - - private final OemNetworkPreferences.Builder mBuilder = new OemNetworkPreferences.Builder(); - - @Test - public void testBuilderAddNetworkPreferenceRequiresNonNullPackageName() { - assertThrows(NullPointerException.class, - () -> mBuilder.addNetworkPreference(null, TEST_PREF)); - } - - @Test - public void testBuilderRemoveNetworkPreferenceRequiresNonNullPackageName() { - assertThrows(NullPointerException.class, - () -> mBuilder.clearNetworkPreference(null)); - } - - @Test - public void testGetNetworkPreferenceReturnsCorrectValue() { - final int expectedNumberOfMappings = 1; - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - - final Map<String, Integer> networkPreferences = - mBuilder.build().getNetworkPreferences(); - - assertEquals(expectedNumberOfMappings, networkPreferences.size()); - assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); - } - - @Test - public void testGetNetworkPreferenceReturnsUnmodifiableValue() { - final String newPackage = "new.com.google.apps.contacts"; - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - - final Map<String, Integer> networkPreferences = - mBuilder.build().getNetworkPreferences(); - - assertThrows(UnsupportedOperationException.class, - () -> networkPreferences.put(newPackage, TEST_PREF)); - - assertThrows(UnsupportedOperationException.class, - () -> networkPreferences.remove(TEST_PACKAGE)); - - } - - @Test - public void testToStringReturnsCorrectValue() { - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - - final String networkPreferencesString = mBuilder.build().getNetworkPreferences().toString(); - - assertTrue(networkPreferencesString.contains(Integer.toString(TEST_PREF))); - assertTrue(networkPreferencesString.contains(TEST_PACKAGE)); - } - - @Test - public void testOemNetworkPreferencesParcelable() { - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - - final OemNetworkPreferences prefs = mBuilder.build(); - - assertParcelSane(prefs, 1 /* fieldCount */); - } - - @Test - public void testAddNetworkPreferenceOverwritesPriorPreference() { - final int newPref = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_OEM_PAID; - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - Map<String, Integer> networkPreferences = - mBuilder.build().getNetworkPreferences(); - - assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); - assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); - - mBuilder.addNetworkPreference(TEST_PACKAGE, newPref); - networkPreferences = mBuilder.build().getNetworkPreferences(); - - assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); - assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), newPref); - } - - @Test - public void testRemoveNetworkPreferenceRemovesValue() { - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - Map<String, Integer> networkPreferences = - mBuilder.build().getNetworkPreferences(); - - assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); - - mBuilder.clearNetworkPreference(TEST_PACKAGE); - networkPreferences = mBuilder.build().getNetworkPreferences(); - - assertFalse(networkPreferences.containsKey(TEST_PACKAGE)); - } - - @Test - public void testConstructorByOemNetworkPreferencesSetsValue() { - mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); - OemNetworkPreferences networkPreference = mBuilder.build(); - - final Map<String, Integer> networkPreferences = - new OemNetworkPreferences - .Builder(networkPreference) - .build() - .getNetworkPreferences(); - - assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); - assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); - } -} diff --git a/tests/net/common/java/android/net/RouteInfoTest.java b/tests/net/common/java/android/net/RouteInfoTest.java deleted file mode 100644 index 71689f919726..000000000000 --- a/tests/net/common/java/android/net/RouteInfoTest.java +++ /dev/null @@ -1,434 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static android.net.RouteInfo.RTN_UNREACHABLE; - -import static com.android.testutils.MiscAsserts.assertEqualBothWays; -import static com.android.testutils.MiscAsserts.assertFieldCountEquals; -import static com.android.testutils.MiscAsserts.assertNotEqualEitherWay; -import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import android.os.Build; - -import androidx.core.os.BuildCompat; -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.Inet4Address; -import java.net.Inet6Address; -import java.net.InetAddress; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class RouteInfoTest { - @Rule - public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); - - private static final int INVALID_ROUTE_TYPE = -1; - - private InetAddress Address(String addr) { - return InetAddresses.parseNumericAddress(addr); - } - - private IpPrefix Prefix(String prefix) { - return new IpPrefix(prefix); - } - - private static boolean isAtLeastR() { - // BuildCompat.isAtLeastR is documented to return false on release SDKs (including R) - return Build.VERSION.SDK_INT > Build.VERSION_CODES.Q || BuildCompat.isAtLeastR(); - } - - @Test - public void testConstructor() { - RouteInfo r; - // Invalid input. - try { - r = new RouteInfo((IpPrefix) null, null, "rmnet0"); - fail("Expected RuntimeException: destination and gateway null"); - } catch (RuntimeException e) { } - - try { - r = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("2001:db8::1"), "rmnet0", - INVALID_ROUTE_TYPE); - fail("Invalid route type should cause exception"); - } catch (IllegalArgumentException e) { } - - try { - r = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("192.0.2.1"), "rmnet0", - RTN_UNREACHABLE); - fail("Address family mismatch should cause exception"); - } catch (IllegalArgumentException e) { } - - try { - r = new RouteInfo(Prefix("0.0.0.0/0"), Address("2001:db8::1"), "rmnet0", - RTN_UNREACHABLE); - fail("Address family mismatch should cause exception"); - } catch (IllegalArgumentException e) { } - - // Null destination is default route. - r = new RouteInfo((IpPrefix) null, Address("2001:db8::1"), null); - assertEquals(Prefix("::/0"), r.getDestination()); - assertEquals(Address("2001:db8::1"), r.getGateway()); - assertNull(r.getInterface()); - - r = new RouteInfo((IpPrefix) null, Address("192.0.2.1"), "wlan0"); - assertEquals(Prefix("0.0.0.0/0"), r.getDestination()); - assertEquals(Address("192.0.2.1"), r.getGateway()); - assertEquals("wlan0", r.getInterface()); - - // Null gateway sets gateway to unspecified address (why?). - r = new RouteInfo(Prefix("2001:db8:beef:cafe::/48"), null, "lo"); - assertEquals(Prefix("2001:db8:beef::/48"), r.getDestination()); - assertEquals(Address("::"), r.getGateway()); - assertEquals("lo", r.getInterface()); - - r = new RouteInfo(Prefix("192.0.2.5/24"), null); - assertEquals(Prefix("192.0.2.0/24"), r.getDestination()); - assertEquals(Address("0.0.0.0"), r.getGateway()); - assertNull(r.getInterface()); - } - - @Test - public void testMatches() { - class PatchedRouteInfo { - private final RouteInfo mRouteInfo; - - public PatchedRouteInfo(IpPrefix destination, InetAddress gateway, String iface) { - mRouteInfo = new RouteInfo(destination, gateway, iface); - } - - public boolean matches(InetAddress destination) { - return mRouteInfo.matches(destination); - } - } - - PatchedRouteInfo r; - - r = new PatchedRouteInfo(Prefix("2001:db8:f00::ace:d00d/127"), null, "rmnet0"); - assertTrue(r.matches(Address("2001:db8:f00::ace:d00c"))); - assertTrue(r.matches(Address("2001:db8:f00::ace:d00d"))); - assertFalse(r.matches(Address("2001:db8:f00::ace:d00e"))); - assertFalse(r.matches(Address("2001:db8:f00::bad:d00d"))); - assertFalse(r.matches(Address("2001:4868:4860::8888"))); - assertFalse(r.matches(Address("8.8.8.8"))); - - r = new PatchedRouteInfo(Prefix("192.0.2.0/23"), null, "wlan0"); - assertTrue(r.matches(Address("192.0.2.43"))); - assertTrue(r.matches(Address("192.0.3.21"))); - assertFalse(r.matches(Address("192.0.0.21"))); - assertFalse(r.matches(Address("8.8.8.8"))); - - PatchedRouteInfo ipv6Default = new PatchedRouteInfo(Prefix("::/0"), null, "rmnet0"); - assertTrue(ipv6Default.matches(Address("2001:db8::f00"))); - assertFalse(ipv6Default.matches(Address("192.0.2.1"))); - - PatchedRouteInfo ipv4Default = new PatchedRouteInfo(Prefix("0.0.0.0/0"), null, "rmnet0"); - assertTrue(ipv4Default.matches(Address("255.255.255.255"))); - assertTrue(ipv4Default.matches(Address("192.0.2.1"))); - assertFalse(ipv4Default.matches(Address("2001:db8::f00"))); - } - - @Test - public void testEquals() { - // IPv4 - RouteInfo r1 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0"); - RouteInfo r2 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0"); - assertEqualBothWays(r1, r2); - - RouteInfo r3 = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("2001:db8::1"), "wlan0"); - RouteInfo r4 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::2"), "wlan0"); - RouteInfo r5 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "rmnet0"); - assertNotEqualEitherWay(r1, r3); - assertNotEqualEitherWay(r1, r4); - assertNotEqualEitherWay(r1, r5); - - // IPv6 - r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0"); - r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0"); - assertEqualBothWays(r1, r2); - - r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0"); - r4 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.2"), "wlan0"); - r5 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "rmnet0"); - assertNotEqualEitherWay(r1, r3); - assertNotEqualEitherWay(r1, r4); - assertNotEqualEitherWay(r1, r5); - - // Interfaces (but not destinations or gateways) can be null. - r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null); - r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null); - r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0"); - assertEqualBothWays(r1, r2); - assertNotEqualEitherWay(r1, r3); - } - - @Test - public void testHostAndDefaultRoutes() { - RouteInfo r; - - r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0"); - assertFalse(r.isHostRoute()); - assertTrue(r.isDefaultRoute()); - assertTrue(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0"); - assertFalse(r.isHostRoute()); - assertTrue(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertTrue(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0"); - assertFalse(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0"); - assertFalse(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0"); - assertTrue(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE); - assertFalse(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertTrue(r.isIPv4UnreachableDefault()); - assertFalse(r.isIPv6UnreachableDefault()); - } - - r = new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE); - assertFalse(r.isHostRoute()); - assertFalse(r.isDefaultRoute()); - assertFalse(r.isIPv4Default()); - assertFalse(r.isIPv6Default()); - if (isAtLeastR()) { - assertFalse(r.isIPv4UnreachableDefault()); - assertTrue(r.isIPv6UnreachableDefault()); - } - } - - @Test - public void testTruncation() { - LinkAddress l; - RouteInfo r; - - l = new LinkAddress("192.0.2.5/30"); - r = new RouteInfo(l, Address("192.0.2.1"), "wlan0"); - assertEquals("192.0.2.4", r.getDestination().getAddress().getHostAddress()); - - l = new LinkAddress("2001:db8:1:f::5/63"); - r = new RouteInfo(l, Address("2001:db8:5::1"), "wlan0"); - assertEquals("2001:db8:1:e::", r.getDestination().getAddress().getHostAddress()); - } - - // Make sure that creating routes to multicast addresses doesn't throw an exception. Even though - // there's nothing we can do with them, we don't want to crash if, e.g., someone calls - // requestRouteToHostAddress("230.0.0.0", MOBILE_HIPRI); - @Test - public void testMulticastRoute() { - RouteInfo r; - r = new RouteInfo(Prefix("230.0.0.0/32"), Address("192.0.2.1"), "wlan0"); - r = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::1"), "wlan0"); - // No exceptions? Good. - } - - @Test - public void testParceling() { - RouteInfo r; - r = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), null); - assertParcelingIsLossless(r); - r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0"); - assertParcelingIsLossless(r); - r = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0", RTN_UNREACHABLE); - assertParcelingIsLossless(r); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testMtuParceling() { - final RouteInfo r = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::"), "testiface", - RTN_UNREACHABLE, 1450 /* mtu */); - assertParcelingIsLossless(r); - } - - @Test @IgnoreAfter(Build.VERSION_CODES.Q) - public void testFieldCount_Q() { - assertFieldCountEquals(6, RouteInfo.class); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testFieldCount() { - // Make sure any new field is covered by the above parceling tests when changing this number - assertFieldCountEquals(7, RouteInfo.class); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testMtu() { - RouteInfo r; - r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0", - RouteInfo.RTN_UNICAST, 1500); - assertEquals(1500, r.getMtu()); - - r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0"); - assertEquals(0, r.getMtu()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - public void testRouteKey() { - RouteInfo.RouteKey k1, k2; - // Only prefix, null gateway and null interface - k1 = new RouteInfo(Prefix("2001:db8::/128"), null).getRouteKey(); - k2 = new RouteInfo(Prefix("2001:db8::/128"), null).getRouteKey(); - assertEquals(k1, k2); - assertEquals(k1.hashCode(), k2.hashCode()); - - // With prefix, gateway and interface. Type and MTU does not affect RouteKey equality - k1 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0", - RTN_UNREACHABLE, 1450).getRouteKey(); - k2 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0", - RouteInfo.RTN_UNICAST, 1400).getRouteKey(); - assertEquals(k1, k2); - assertEquals(k1.hashCode(), k2.hashCode()); - - // Different scope IDs are ignored by the kernel, so we consider them equal here too. - k1 = new RouteInfo(Prefix("2001:db8::/64"), Address("fe80::1%1"), "wlan0").getRouteKey(); - k2 = new RouteInfo(Prefix("2001:db8::/64"), Address("fe80::1%2"), "wlan0").getRouteKey(); - assertEquals(k1, k2); - assertEquals(k1.hashCode(), k2.hashCode()); - - // Different prefix - k1 = new RouteInfo(Prefix("192.0.2.0/24"), null).getRouteKey(); - k2 = new RouteInfo(Prefix("192.0.3.0/24"), null).getRouteKey(); - assertNotEquals(k1, k2); - - // Different gateway - k1 = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::1"), null).getRouteKey(); - k2 = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::2"), null).getRouteKey(); - assertNotEquals(k1, k2); - - // Different interface - k1 = new RouteInfo(Prefix("ff02::1/128"), null, "tun0").getRouteKey(); - k2 = new RouteInfo(Prefix("ff02::1/128"), null, "tun1").getRouteKey(); - assertNotEquals(k1, k2); - } -} diff --git a/tests/net/common/java/android/net/StaticIpConfigurationTest.java b/tests/net/common/java/android/net/StaticIpConfigurationTest.java deleted file mode 100644 index b5f23bf19a3c..000000000000 --- a/tests/net/common/java/android/net/StaticIpConfigurationTest.java +++ /dev/null @@ -1,269 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import android.os.Parcel; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class StaticIpConfigurationTest { - - private static final String ADDRSTR = "192.0.2.2/25"; - private static final LinkAddress ADDR = new LinkAddress(ADDRSTR); - private static final InetAddress GATEWAY = IpAddress("192.0.2.1"); - private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129"); - private static final InetAddress DNS1 = IpAddress("8.8.8.8"); - private static final InetAddress DNS2 = IpAddress("8.8.4.4"); - private static final InetAddress DNS3 = IpAddress("4.2.2.2"); - private static final String IFACE = "eth0"; - private static final String FAKE_DOMAINS = "google.com"; - - private static InetAddress IpAddress(String addr) { - return InetAddress.parseNumericAddress(addr); - } - - private void checkEmpty(StaticIpConfiguration s) { - assertNull(s.ipAddress); - assertNull(s.gateway); - assertNull(s.domains); - assertEquals(0, s.dnsServers.size()); - } - - private StaticIpConfiguration makeTestObject() { - StaticIpConfiguration s = new StaticIpConfiguration(); - s.ipAddress = ADDR; - s.gateway = GATEWAY; - s.dnsServers.add(DNS1); - s.dnsServers.add(DNS2); - s.dnsServers.add(DNS3); - s.domains = FAKE_DOMAINS; - return s; - } - - @Test - public void testConstructor() { - StaticIpConfiguration s = new StaticIpConfiguration(); - checkEmpty(s); - } - - @Test - public void testCopyAndClear() { - StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null); - checkEmpty(empty); - - StaticIpConfiguration s1 = makeTestObject(); - StaticIpConfiguration s2 = new StaticIpConfiguration(s1); - assertEquals(s1, s2); - s2.clear(); - assertEquals(empty, s2); - } - - @Test - public void testHashCodeAndEquals() { - HashSet<Integer> hashCodes = new HashSet(); - hashCodes.add(0); - - StaticIpConfiguration s = new StaticIpConfiguration(); - // Check that this hash code is nonzero and different from all the ones seen so far. - assertTrue(hashCodes.add(s.hashCode())); - - s.ipAddress = ADDR; - assertTrue(hashCodes.add(s.hashCode())); - - s.gateway = GATEWAY; - assertTrue(hashCodes.add(s.hashCode())); - - s.dnsServers.add(DNS1); - assertTrue(hashCodes.add(s.hashCode())); - - s.dnsServers.add(DNS2); - assertTrue(hashCodes.add(s.hashCode())); - - s.dnsServers.add(DNS3); - assertTrue(hashCodes.add(s.hashCode())); - - s.domains = "example.com"; - assertTrue(hashCodes.add(s.hashCode())); - - assertFalse(s.equals(null)); - assertEquals(s, s); - - StaticIpConfiguration s2 = new StaticIpConfiguration(s); - assertEquals(s, s2); - - s.ipAddress = new LinkAddress(DNS1, 32); - assertNotEquals(s, s2); - - s2 = new StaticIpConfiguration(s); - s.domains = "foo"; - assertNotEquals(s, s2); - - s2 = new StaticIpConfiguration(s); - s.gateway = DNS2; - assertNotEquals(s, s2); - - s2 = new StaticIpConfiguration(s); - s.dnsServers.add(DNS3); - assertNotEquals(s, s2); - } - - @Test - public void testToLinkProperties() { - LinkProperties expected = new LinkProperties(); - expected.setInterfaceName(IFACE); - - StaticIpConfiguration s = new StaticIpConfiguration(); - assertEquals(expected, s.toLinkProperties(IFACE)); - - final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE); - s.ipAddress = ADDR; - expected.addLinkAddress(ADDR); - expected.addRoute(connectedRoute); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.gateway = GATEWAY; - RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE); - expected.addRoute(defaultRoute); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.gateway = OFFLINKGATEWAY; - expected.removeRoute(defaultRoute); - defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE); - expected.addRoute(defaultRoute); - - RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE); - expected.addRoute(gatewayRoute); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.dnsServers.add(DNS1); - expected.addDnsServer(DNS1); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.dnsServers.add(DNS2); - s.dnsServers.add(DNS3); - expected.addDnsServer(DNS2); - expected.addDnsServer(DNS3); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.domains = FAKE_DOMAINS; - expected.setDomains(FAKE_DOMAINS); - assertEquals(expected, s.toLinkProperties(IFACE)); - - s.gateway = null; - expected.removeRoute(defaultRoute); - expected.removeRoute(gatewayRoute); - assertEquals(expected, s.toLinkProperties(IFACE)); - - // Without knowing the IP address, we don't have a directly-connected route, so we can't - // tell if the gateway is off-link or not and we don't add a host route. This isn't a real - // configuration, but we should at least not crash. - s.gateway = OFFLINKGATEWAY; - s.ipAddress = null; - expected.removeLinkAddress(ADDR); - expected.removeRoute(connectedRoute); - expected.addRoute(defaultRoute); - assertEquals(expected, s.toLinkProperties(IFACE)); - } - - private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) { - Parcel p = Parcel.obtain(); - StaticIpConfiguration s2 = null; - try { - s.writeToParcel(p, 0); - p.setDataPosition(0); - s2 = StaticIpConfiguration.readFromParcel(p); - } finally { - p.recycle(); - } - assertNotNull(s2); - return s2; - } - - @Test - public void testParceling() { - StaticIpConfiguration s = makeTestObject(); - StaticIpConfiguration s2 = passThroughParcel(s); - assertEquals(s, s2); - } - - @Test - public void testBuilder() { - final ArrayList<InetAddress> dnsServers = new ArrayList<>(); - dnsServers.add(DNS1); - - final StaticIpConfiguration s = new StaticIpConfiguration.Builder() - .setIpAddress(ADDR) - .setGateway(GATEWAY) - .setDomains(FAKE_DOMAINS) - .setDnsServers(dnsServers) - .build(); - - assertEquals(s.ipAddress, s.getIpAddress()); - assertEquals(ADDR, s.getIpAddress()); - assertEquals(s.gateway, s.getGateway()); - assertEquals(GATEWAY, s.getGateway()); - assertEquals(s.domains, s.getDomains()); - assertEquals(FAKE_DOMAINS, s.getDomains()); - assertTrue(s.dnsServers.equals(s.getDnsServers())); - assertEquals(1, s.getDnsServers().size()); - assertEquals(DNS1, s.getDnsServers().get(0)); - } - - @Test - public void testAddDnsServers() { - final StaticIpConfiguration s = new StaticIpConfiguration((StaticIpConfiguration) null); - checkEmpty(s); - - s.addDnsServer(DNS1); - assertEquals(1, s.getDnsServers().size()); - assertEquals(DNS1, s.getDnsServers().get(0)); - - s.addDnsServer(DNS2); - s.addDnsServer(DNS3); - assertEquals(3, s.getDnsServers().size()); - assertEquals(DNS2, s.getDnsServers().get(1)); - assertEquals(DNS3, s.getDnsServers().get(2)); - } - - @Test - public void testGetRoutes() { - final StaticIpConfiguration s = makeTestObject(); - final List<RouteInfo> routeInfoList = s.getRoutes(IFACE); - - assertEquals(2, routeInfoList.size()); - assertEquals(new RouteInfo(ADDR, (InetAddress) null, IFACE), routeInfoList.get(0)); - assertEquals(new RouteInfo((IpPrefix) null, GATEWAY, IFACE), routeInfoList.get(1)); - } -} diff --git a/tests/net/common/java/android/net/TcpKeepalivePacketDataTest.kt b/tests/net/common/java/android/net/TcpKeepalivePacketDataTest.kt deleted file mode 100644 index 7a18bb08faa8..000000000000 --- a/tests/net/common/java/android/net/TcpKeepalivePacketDataTest.kt +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.net.InetAddresses.parseNumericAddress -import android.os.Build -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRunner -import com.android.testutils.assertFieldCountEquals -import com.android.testutils.assertParcelSane -import org.junit.Test -import org.junit.runner.RunWith -import java.net.InetAddress -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals -import kotlin.test.assertTrue - -@RunWith(DevSdkIgnoreRunner::class) -@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) // TcpKeepalivePacketData added to SDK in S -class TcpKeepalivePacketDataTest { - private fun makeData( - srcAddress: InetAddress = parseNumericAddress("192.0.2.123"), - srcPort: Int = 1234, - dstAddress: InetAddress = parseNumericAddress("192.0.2.231"), - dstPort: Int = 4321, - data: ByteArray = byteArrayOf(1, 2, 3), - tcpSeq: Int = 135, - tcpAck: Int = 246, - tcpWnd: Int = 1234, - tcpWndScale: Int = 2, - ipTos: Int = 0x12, - ipTtl: Int = 10 - ) = TcpKeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, data, tcpSeq, tcpAck, - tcpWnd, tcpWndScale, ipTos, ipTtl) - - @Test - fun testEquals() { - val data1 = makeData() - val data2 = makeData() - assertEquals(data1, data2) - assertEquals(data1.hashCode(), data2.hashCode()) - } - - @Test - fun testNotEquals() { - assertNotEquals(makeData(srcAddress = parseNumericAddress("192.0.2.124")), makeData()) - assertNotEquals(makeData(srcPort = 1235), makeData()) - assertNotEquals(makeData(dstAddress = parseNumericAddress("192.0.2.232")), makeData()) - assertNotEquals(makeData(dstPort = 4322), makeData()) - // .equals does not test .packet, as it should be generated from the other fields - assertNotEquals(makeData(tcpSeq = 136), makeData()) - assertNotEquals(makeData(tcpAck = 247), makeData()) - assertNotEquals(makeData(tcpWnd = 1235), makeData()) - assertNotEquals(makeData(tcpWndScale = 3), makeData()) - assertNotEquals(makeData(ipTos = 0x14), makeData()) - assertNotEquals(makeData(ipTtl = 11), makeData()) - - // Update above assertions if field is added - assertFieldCountEquals(5, KeepalivePacketData::class.java) - assertFieldCountEquals(6, TcpKeepalivePacketData::class.java) - } - - @Test - fun testParcelUnparcel() { - assertParcelSane(makeData(), fieldCount = 6) { a, b -> - // .equals() does not verify .packet - a == b && a.packet contentEquals b.packet - } - } - - @Test - fun testToString() { - val data = makeData() - val str = data.toString() - - assertTrue(str.contains(data.srcAddress.hostAddress)) - assertTrue(str.contains(data.srcPort.toString())) - assertTrue(str.contains(data.dstAddress.hostAddress)) - assertTrue(str.contains(data.dstPort.toString())) - // .packet not included in toString() - assertTrue(str.contains(data.getTcpSeq().toString())) - assertTrue(str.contains(data.getTcpAck().toString())) - assertTrue(str.contains(data.getTcpWindow().toString())) - assertTrue(str.contains(data.getTcpWindowScale().toString())) - assertTrue(str.contains(data.getIpTos().toString())) - assertTrue(str.contains(data.getIpTtl().toString())) - - // Update above assertions if field is added - assertFieldCountEquals(5, KeepalivePacketData::class.java) - assertFieldCountEquals(6, TcpKeepalivePacketData::class.java) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/UidRangeTest.java b/tests/net/common/java/android/net/UidRangeTest.java deleted file mode 100644 index 1b1c95431d6f..000000000000 --- a/tests/net/common/java/android/net/UidRangeTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net; - -import static android.os.UserHandle.MIN_SECONDARY_USER_ID; -import static android.os.UserHandle.SYSTEM; -import static android.os.UserHandle.USER_SYSTEM; -import static android.os.UserHandle.getUid; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import android.os.Build; -import android.os.UserHandle; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.testutils.DevSdkIgnoreRule; -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class UidRangeTest { - - /* - * UidRange is no longer passed to netd. UID ranges between the framework and netd are passed as - * UidRangeParcel objects. - */ - - @Rule - public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); - - @Test - public void testSingleItemUidRangeAllowed() { - new UidRange(123, 123); - new UidRange(0, 0); - new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE); - } - - @Test - public void testNegativeUidsDisallowed() { - try { - new UidRange(-2, 100); - fail("Exception not thrown for negative start UID"); - } catch (IllegalArgumentException expected) { - } - - try { - new UidRange(-200, -100); - fail("Exception not thrown for negative stop UID"); - } catch (IllegalArgumentException expected) { - } - } - - @Test - public void testStopLessThanStartDisallowed() { - final int x = 4195000; - try { - new UidRange(x, x - 1); - fail("Exception not thrown for negative-length UID range"); - } catch (IllegalArgumentException expected) { - } - } - - @Test - public void testGetStartAndEndUser() throws Exception { - final UidRange uidRangeOfPrimaryUser = new UidRange( - getUid(USER_SYSTEM, 10000), getUid(USER_SYSTEM, 10100)); - final UidRange uidRangeOfSecondaryUser = new UidRange( - getUid(MIN_SECONDARY_USER_ID, 10000), getUid(MIN_SECONDARY_USER_ID, 10100)); - assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser()); - assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser()); - assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getStartUser()); - assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser()); - - final UidRange uidRangeForDifferentUsers = new UidRange( - getUid(USER_SYSTEM, 10000), getUid(MIN_SECONDARY_USER_ID, 10100)); - assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser()); - assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser()); - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.R) - public void testCreateForUser() throws Exception { - final UidRange uidRangeOfPrimaryUser = UidRange.createForUser(SYSTEM); - final UidRange uidRangeOfSecondaryUser = UidRange.createForUser( - UserHandle.of(USER_SYSTEM + 1)); - assertTrue(uidRangeOfPrimaryUser.stop < uidRangeOfSecondaryUser.start); - assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser()); - assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser()); - assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getStartUser()); - assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getEndUser()); - } -} diff --git a/tests/net/common/java/android/net/UnderlyingNetworkInfoTest.kt b/tests/net/common/java/android/net/UnderlyingNetworkInfoTest.kt deleted file mode 100644 index f23ba26d0039..000000000000 --- a/tests/net/common/java/android/net/UnderlyingNetworkInfoTest.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net - -import android.os.Build -import androidx.test.filters.SmallTest -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRunner -import com.android.testutils.assertParcelSane -import org.junit.Test -import org.junit.runner.RunWith -import kotlin.test.assertEquals - -private const val TEST_OWNER_UID = 123 -private const val TEST_IFACE = "test_tun0" -private val TEST_IFACE_LIST = listOf("wlan0", "rmnet_data0", "eth0") - -@SmallTest -@RunWith(DevSdkIgnoreRunner::class) -@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) -class UnderlyingNetworkInfoTest { - @Test - fun testParcelUnparcel() { - val testInfo = UnderlyingNetworkInfo(TEST_OWNER_UID, TEST_IFACE, TEST_IFACE_LIST) - assertEquals(TEST_OWNER_UID, testInfo.getOwnerUid()) - assertEquals(TEST_IFACE, testInfo.getInterface()) - assertEquals(TEST_IFACE_LIST, testInfo.getUnderlyingInterfaces()) - assertParcelSane(testInfo, 3) - - val emptyInfo = UnderlyingNetworkInfo(0, String(), listOf()) - assertEquals(0, emptyInfo.getOwnerUid()) - assertEquals(String(), emptyInfo.getInterface()) - assertEquals(listOf(), emptyInfo.getUnderlyingInterfaces()) - assertParcelSane(emptyInfo, 3) - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java b/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java deleted file mode 100644 index d50406fd3a1c..000000000000 --- a/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.apf; - -import static com.android.testutils.ParcelUtils.assertParcelSane; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import android.content.Context; - -import androidx.test.InstrumentationRegistry; -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.Arrays; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class ApfCapabilitiesTest { - private Context mContext; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getContext(); - } - - @Test - public void testConstructAndParcel() { - final ApfCapabilities caps = new ApfCapabilities(123, 456, 789); - assertEquals(123, caps.apfVersionSupported); - assertEquals(456, caps.maximumApfProgramSize); - assertEquals(789, caps.apfPacketFormat); - - assertParcelSane(caps, 3); - } - - @Test - public void testEquals() { - assertEquals(new ApfCapabilities(1, 2, 3), new ApfCapabilities(1, 2, 3)); - assertNotEquals(new ApfCapabilities(2, 2, 3), new ApfCapabilities(1, 2, 3)); - assertNotEquals(new ApfCapabilities(1, 3, 3), new ApfCapabilities(1, 2, 3)); - assertNotEquals(new ApfCapabilities(1, 2, 4), new ApfCapabilities(1, 2, 3)); - } - - @Test - public void testHasDataAccess() { - //hasDataAccess is only supported starting at apf version 4. - ApfCapabilities caps = new ApfCapabilities(1 /* apfVersionSupported */, 2, 3); - assertFalse(caps.hasDataAccess()); - - caps = new ApfCapabilities(4 /* apfVersionSupported */, 5, 6); - assertTrue(caps.hasDataAccess()); - } - - @Test - public void testGetApfDrop8023Frames() { - // Get com.android.internal.R.bool.config_apfDrop802_3Frames. The test cannot directly - // use R.bool.config_apfDrop802_3Frames because that is not a stable resource ID. - final int resId = mContext.getResources().getIdentifier("config_apfDrop802_3Frames", - "bool", "android"); - final boolean shouldDrop8023Frames = mContext.getResources().getBoolean(resId); - final boolean actual = ApfCapabilities.getApfDrop8023Frames(); - assertEquals(shouldDrop8023Frames, actual); - } - - @Test - public void testGetApfEtherTypeBlackList() { - // Get com.android.internal.R.array.config_apfEthTypeBlackList. The test cannot directly - // use R.array.config_apfEthTypeBlackList because that is not a stable resource ID. - final int resId = mContext.getResources().getIdentifier("config_apfEthTypeBlackList", - "array", "android"); - final int[] blacklistedEtherTypeArray = mContext.getResources().getIntArray(resId); - final int[] actual = ApfCapabilities.getApfEtherTypeBlackList(); - assertNotNull(actual); - assertTrue(Arrays.equals(blacklistedEtherTypeArray, actual)); - } -} diff --git a/tests/net/common/java/android/net/metrics/ApfProgramEventTest.kt b/tests/net/common/java/android/net/metrics/ApfProgramEventTest.kt deleted file mode 100644 index 0b7b74097cc6..000000000000 --- a/tests/net/common/java/android/net/metrics/ApfProgramEventTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics; - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class ApfProgramEventTest { - private infix fun Int.hasFlag(flag: Int) = (this and (1 shl flag)) != 0 - - @Test - fun testBuilderAndParcel() { - val apfProgramEvent = ApfProgramEvent.Builder() - .setLifetime(1) - .setActualLifetime(2) - .setFilteredRas(3) - .setCurrentRas(4) - .setProgramLength(5) - .setFlags(true, true) - .build() - - assertEquals(1, apfProgramEvent.lifetime) - assertEquals(2, apfProgramEvent.actualLifetime) - assertEquals(3, apfProgramEvent.filteredRas) - assertEquals(4, apfProgramEvent.currentRas) - assertEquals(5, apfProgramEvent.programLength) - assertEquals(ApfProgramEvent.flagsFor(true, true), apfProgramEvent.flags) - - assertParcelSane(apfProgramEvent, 6) - } - - @Test - fun testFlagsFor() { - var flags = ApfProgramEvent.flagsFor(false, false) - assertFalse(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS) - assertFalse(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON) - - flags = ApfProgramEvent.flagsFor(true, false) - assertTrue(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS) - assertFalse(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON) - - flags = ApfProgramEvent.flagsFor(false, true) - assertFalse(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS) - assertTrue(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON) - - flags = ApfProgramEvent.flagsFor(true, true) - assertTrue(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS) - assertTrue(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON) - } -} diff --git a/tests/net/common/java/android/net/metrics/ApfStatsTest.kt b/tests/net/common/java/android/net/metrics/ApfStatsTest.kt deleted file mode 100644 index 46a8c8e5b509..000000000000 --- a/tests/net/common/java/android/net/metrics/ApfStatsTest.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class ApfStatsTest { - @Test - fun testBuilderAndParcel() { - val apfStats = ApfStats.Builder() - .setDurationMs(Long.MAX_VALUE) - .setReceivedRas(1) - .setMatchingRas(2) - .setDroppedRas(3) - .setZeroLifetimeRas(4) - .setParseErrors(5) - .setProgramUpdates(6) - .setProgramUpdatesAll(7) - .setProgramUpdatesAllowingMulticast(8) - .setMaxProgramSize(9) - .build() - - assertEquals(Long.MAX_VALUE, apfStats.durationMs) - assertEquals(1, apfStats.receivedRas) - assertEquals(2, apfStats.matchingRas) - assertEquals(3, apfStats.droppedRas) - assertEquals(4, apfStats.zeroLifetimeRas) - assertEquals(5, apfStats.parseErrors) - assertEquals(6, apfStats.programUpdates) - assertEquals(7, apfStats.programUpdatesAll) - assertEquals(8, apfStats.programUpdatesAllowingMulticast) - assertEquals(9, apfStats.maxProgramSize) - - assertParcelSane(apfStats, 10) - } -} diff --git a/tests/net/common/java/android/net/metrics/DhcpClientEventTest.kt b/tests/net/common/java/android/net/metrics/DhcpClientEventTest.kt deleted file mode 100644 index 8d7a9c405024..000000000000 --- a/tests/net/common/java/android/net/metrics/DhcpClientEventTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -private const val FAKE_MESSAGE = "test" - -@RunWith(AndroidJUnit4::class) -@SmallTest -class DhcpClientEventTest { - @Test - fun testBuilderAndParcel() { - val dhcpClientEvent = DhcpClientEvent.Builder() - .setMsg(FAKE_MESSAGE) - .setDurationMs(Integer.MAX_VALUE) - .build() - - assertEquals(FAKE_MESSAGE, dhcpClientEvent.msg) - assertEquals(Integer.MAX_VALUE, dhcpClientEvent.durationMs) - - assertParcelSane(dhcpClientEvent, 2) - } -} diff --git a/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt b/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt deleted file mode 100644 index 236f72eafbdc..000000000000 --- a/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt +++ /dev/null @@ -1,65 +0,0 @@ -package android.net.metrics - -import android.net.metrics.DhcpErrorEvent.DHCP_INVALID_OPTION_LENGTH -import android.net.metrics.DhcpErrorEvent.errorCodeWithOption -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.parcelingRoundTrip -import java.lang.reflect.Modifier -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertTrue -import org.junit.Test -import org.junit.runner.RunWith - -private const val TEST_ERROR_CODE = 12345 -//DHCP Optional Type: DHCP Subnet Mask (Copy from DhcpPacket.java due to it's protected) -private const val DHCP_SUBNET_MASK = 1 - -@RunWith(AndroidJUnit4::class) -@SmallTest -class DhcpErrorEventTest { - - @Test - fun testConstructor() { - val event = DhcpErrorEvent(TEST_ERROR_CODE) - assertEquals(TEST_ERROR_CODE, event.errorCode) - } - - @Test - fun testParcelUnparcel() { - val event = DhcpErrorEvent(TEST_ERROR_CODE) - val parceled = parcelingRoundTrip(event) - assertEquals(TEST_ERROR_CODE, parceled.errorCode) - } - - @Test - fun testErrorCodeWithOption() { - val errorCode = errorCodeWithOption(DHCP_INVALID_OPTION_LENGTH, DHCP_SUBNET_MASK); - assertTrue((DHCP_INVALID_OPTION_LENGTH and errorCode) == DHCP_INVALID_OPTION_LENGTH); - assertTrue((DHCP_SUBNET_MASK and errorCode) == DHCP_SUBNET_MASK); - } - - @Test - fun testToString() { - val names = listOf("L2_ERROR", "L3_ERROR", "L4_ERROR", "DHCP_ERROR", "MISC_ERROR") - val errorFields = DhcpErrorEvent::class.java.declaredFields.filter { - it.type == Int::class.javaPrimitiveType - && Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers) - && it.name !in names - } - - errorFields.forEach { - val intValue = it.getInt(null) - val stringValue = DhcpErrorEvent(intValue).toString() - assertTrue("Invalid string for error 0x%08X (field %s): %s".format(intValue, it.name, - stringValue), - stringValue.contains(it.name)) - } - } - - @Test - fun testToString_InvalidErrorCode() { - assertNotNull(DhcpErrorEvent(TEST_ERROR_CODE).toString()) - } -} diff --git a/tests/net/common/java/android/net/metrics/IpConnectivityLogTest.java b/tests/net/common/java/android/net/metrics/IpConnectivityLogTest.java deleted file mode 100644 index d4780d3a5d7b..000000000000 --- a/tests/net/common/java/android/net/metrics/IpConnectivityLogTest.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics; - -import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; -import static android.net.NetworkCapabilities.TRANSPORT_WIFI; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.timeout; -import static org.mockito.Mockito.verify; - -import android.net.ConnectivityMetricsEvent; -import android.net.IIpConnectivityMetrics; -import android.net.Network; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import com.android.internal.util.BitUtils; - -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 java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class IpConnectivityLogTest { - private static final int FAKE_NET_ID = 100; - private static final int[] FAKE_TRANSPORT_TYPES = BitUtils.unpackBits(TRANSPORT_WIFI); - private static final long FAKE_TIME_STAMP = System.currentTimeMillis(); - private static final String FAKE_INTERFACE_NAME = "test"; - private static final IpReachabilityEvent FAKE_EV = - new IpReachabilityEvent(IpReachabilityEvent.NUD_FAILED); - - @Mock IIpConnectivityMetrics mMockService; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testLoggingEvents() throws Exception { - IpConnectivityLog logger = new IpConnectivityLog(mMockService); - - assertTrue(logger.log(FAKE_EV)); - assertTrue(logger.log(FAKE_TIME_STAMP, FAKE_EV)); - assertTrue(logger.log(FAKE_NET_ID, FAKE_TRANSPORT_TYPES, FAKE_EV)); - assertTrue(logger.log(new Network(FAKE_NET_ID), FAKE_TRANSPORT_TYPES, FAKE_EV)); - assertTrue(logger.log(FAKE_INTERFACE_NAME, FAKE_EV)); - assertTrue(logger.log(makeExpectedEvent(FAKE_TIME_STAMP, FAKE_NET_ID, TRANSPORT_WIFI, - FAKE_INTERFACE_NAME))); - - List<ConnectivityMetricsEvent> got = verifyEvents(6); - assertEventsEqual(makeExpectedEvent(got.get(0).timestamp, 0, 0, null), got.get(0)); - assertEventsEqual(makeExpectedEvent(FAKE_TIME_STAMP, 0, 0, null), got.get(1)); - assertEventsEqual(makeExpectedEvent(got.get(2).timestamp, FAKE_NET_ID, - TRANSPORT_WIFI, null), got.get(2)); - assertEventsEqual(makeExpectedEvent(got.get(3).timestamp, FAKE_NET_ID, - TRANSPORT_WIFI, null), got.get(3)); - assertEventsEqual(makeExpectedEvent(got.get(4).timestamp, 0, 0, FAKE_INTERFACE_NAME), - got.get(4)); - assertEventsEqual(makeExpectedEvent(FAKE_TIME_STAMP, FAKE_NET_ID, - TRANSPORT_WIFI, FAKE_INTERFACE_NAME), got.get(5)); - } - - @Test - public void testLoggingEventsWithMultipleCallers() throws Exception { - IpConnectivityLog logger = new IpConnectivityLog(mMockService); - - final int nCallers = 10; - final int nEvents = 10; - for (int n = 0; n < nCallers; n++) { - final int i = n; - new Thread() { - public void run() { - for (int j = 0; j < nEvents; j++) { - assertTrue(logger.log(makeExpectedEvent( - FAKE_TIME_STAMP + i * 100 + j, - FAKE_NET_ID + i * 100 + j, - ((i + j) % 2 == 0) ? TRANSPORT_WIFI : TRANSPORT_CELLULAR, - FAKE_INTERFACE_NAME))); - } - } - }.start(); - } - - List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 200); - Collections.sort(got, EVENT_COMPARATOR); - Iterator<ConnectivityMetricsEvent> iter = got.iterator(); - for (int i = 0; i < nCallers; i++) { - for (int j = 0; j < nEvents; j++) { - final long expectedTimestamp = FAKE_TIME_STAMP + i * 100 + j; - final int expectedNetId = FAKE_NET_ID + i * 100 + j; - final long expectedTransports = - ((i + j) % 2 == 0) ? TRANSPORT_WIFI : TRANSPORT_CELLULAR; - assertEventsEqual(makeExpectedEvent(expectedTimestamp, expectedNetId, - expectedTransports, FAKE_INTERFACE_NAME), iter.next()); - } - } - } - - private List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception { - ArgumentCaptor<ConnectivityMetricsEvent> captor = - ArgumentCaptor.forClass(ConnectivityMetricsEvent.class); - verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture()); - return captor.getAllValues(); - } - - private List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception { - return verifyEvents(n, 10); - } - - - private ConnectivityMetricsEvent makeExpectedEvent(long timestamp, int netId, long transports, - String ifname) { - ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent(); - ev.timestamp = timestamp; - ev.data = FAKE_EV; - ev.netId = netId; - ev.transports = transports; - ev.ifname = ifname; - return ev; - } - - /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */ - private void assertEventsEqual(ConnectivityMetricsEvent expected, - ConnectivityMetricsEvent got) { - assertEquals(expected.data, got.data); - assertEquals(expected.timestamp, got.timestamp); - assertEquals(expected.netId, got.netId); - assertEquals(expected.transports, got.transports); - assertEquals(expected.ifname, got.ifname); - } - - static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR = - Comparator.comparingLong((ev) -> ev.timestamp); -} diff --git a/tests/net/common/java/android/net/metrics/IpManagerEventTest.kt b/tests/net/common/java/android/net/metrics/IpManagerEventTest.kt deleted file mode 100644 index 64be50837fc9..000000000000 --- a/tests/net/common/java/android/net/metrics/IpManagerEventTest.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class IpManagerEventTest { - @Test - fun testConstructorAndParcel() { - (IpManagerEvent.PROVISIONING_OK..IpManagerEvent.ERROR_INTERFACE_NOT_FOUND).forEach { - val ipManagerEvent = IpManagerEvent(it, Long.MAX_VALUE) - assertEquals(it, ipManagerEvent.eventType) - assertEquals(Long.MAX_VALUE, ipManagerEvent.durationMs) - - assertParcelSane(ipManagerEvent, 2) - } - } -} diff --git a/tests/net/common/java/android/net/metrics/IpReachabilityEventTest.kt b/tests/net/common/java/android/net/metrics/IpReachabilityEventTest.kt deleted file mode 100644 index 55b5e492dd47..000000000000 --- a/tests/net/common/java/android/net/metrics/IpReachabilityEventTest.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class IpReachabilityEventTest { - @Test - fun testConstructorAndParcel() { - (IpReachabilityEvent.PROBE..IpReachabilityEvent.PROVISIONING_LOST_ORGANIC).forEach { - val ipReachabilityEvent = IpReachabilityEvent(it) - assertEquals(it, ipReachabilityEvent.eventType) - - assertParcelSane(ipReachabilityEvent, 1) - } - } -} diff --git a/tests/net/common/java/android/net/metrics/NetworkEventTest.kt b/tests/net/common/java/android/net/metrics/NetworkEventTest.kt deleted file mode 100644 index 41430b03a1eb..000000000000 --- a/tests/net/common/java/android/net/metrics/NetworkEventTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -@SmallTest -class NetworkEventTest { - @Test - fun testConstructorAndParcel() { - (NetworkEvent.NETWORK_CONNECTED..NetworkEvent.NETWORK_PARTIAL_CONNECTIVITY).forEach { - var networkEvent = NetworkEvent(it) - assertEquals(it, networkEvent.eventType) - assertEquals(0, networkEvent.durationMs) - - networkEvent = NetworkEvent(it, Long.MAX_VALUE) - assertEquals(it, networkEvent.eventType) - assertEquals(Long.MAX_VALUE, networkEvent.durationMs) - - assertParcelSane(networkEvent, 2) - } - } -} diff --git a/tests/net/common/java/android/net/metrics/RaEventTest.kt b/tests/net/common/java/android/net/metrics/RaEventTest.kt deleted file mode 100644 index d9b720332fbe..000000000000 --- a/tests/net/common/java/android/net/metrics/RaEventTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -private const val NO_LIFETIME: Long = -1L - -@RunWith(AndroidJUnit4::class) -@SmallTest -class RaEventTest { - @Test - fun testConstructorAndParcel() { - var raEvent = RaEvent.Builder().build() - assertEquals(NO_LIFETIME, raEvent.routerLifetime) - assertEquals(NO_LIFETIME, raEvent.prefixValidLifetime) - assertEquals(NO_LIFETIME, raEvent.prefixPreferredLifetime) - assertEquals(NO_LIFETIME, raEvent.routeInfoLifetime) - assertEquals(NO_LIFETIME, raEvent.rdnssLifetime) - assertEquals(NO_LIFETIME, raEvent.dnsslLifetime) - - raEvent = RaEvent.Builder() - .updateRouterLifetime(1) - .updatePrefixValidLifetime(2) - .updatePrefixPreferredLifetime(3) - .updateRouteInfoLifetime(4) - .updateRdnssLifetime(5) - .updateDnsslLifetime(6) - .build() - assertEquals(1, raEvent.routerLifetime) - assertEquals(2, raEvent.prefixValidLifetime) - assertEquals(3, raEvent.prefixPreferredLifetime) - assertEquals(4, raEvent.routeInfoLifetime) - assertEquals(5, raEvent.rdnssLifetime) - assertEquals(6, raEvent.dnsslLifetime) - - raEvent = RaEvent.Builder() - .updateRouterLifetime(Long.MIN_VALUE) - .updateRouterLifetime(Long.MAX_VALUE) - .build() - assertEquals(Long.MIN_VALUE, raEvent.routerLifetime) - - raEvent = RaEvent(1, 2, 3, 4, 5, 6) - assertEquals(1, raEvent.routerLifetime) - assertEquals(2, raEvent.prefixValidLifetime) - assertEquals(3, raEvent.prefixPreferredLifetime) - assertEquals(4, raEvent.routeInfoLifetime) - assertEquals(5, raEvent.rdnssLifetime) - assertEquals(6, raEvent.dnsslLifetime) - - assertParcelSane(raEvent, 6) - } -} diff --git a/tests/net/common/java/android/net/metrics/ValidationProbeEventTest.kt b/tests/net/common/java/android/net/metrics/ValidationProbeEventTest.kt deleted file mode 100644 index 51c0d41bf4d5..000000000000 --- a/tests/net/common/java/android/net/metrics/ValidationProbeEventTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.metrics - -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.assertParcelSane -import java.lang.reflect.Modifier -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Test -import org.junit.runner.RunWith - -private const val FIRST_VALIDATION: Int = 1 shl 8 -private const val REVALIDATION: Int = 2 shl 8 - -@RunWith(AndroidJUnit4::class) -@SmallTest -class ValidationProbeEventTest { - private infix fun Int.hasType(type: Int) = (type and this) == type - - @Test - fun testBuilderAndParcel() { - var validationProbeEvent = ValidationProbeEvent.Builder() - .setProbeType(ValidationProbeEvent.PROBE_DNS, false).build() - - assertTrue(validationProbeEvent.probeType hasType REVALIDATION) - - validationProbeEvent = ValidationProbeEvent.Builder() - .setDurationMs(Long.MAX_VALUE) - .setProbeType(ValidationProbeEvent.PROBE_DNS, true) - .setReturnCode(ValidationProbeEvent.DNS_SUCCESS) - .build() - - assertEquals(Long.MAX_VALUE, validationProbeEvent.durationMs) - assertTrue(validationProbeEvent.probeType hasType ValidationProbeEvent.PROBE_DNS) - assertTrue(validationProbeEvent.probeType hasType FIRST_VALIDATION) - assertEquals(ValidationProbeEvent.DNS_SUCCESS, validationProbeEvent.returnCode) - - assertParcelSane(validationProbeEvent, 3) - } - - @Test - fun testGetProbeName() { - val probeFields = ValidationProbeEvent::class.java.declaredFields.filter { - it.type == Int::class.javaPrimitiveType - && Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers) - && it.name.contains("PROBE") - } - - probeFields.forEach { - val intValue = it.getInt(null) - val stringValue = ValidationProbeEvent.getProbeName(intValue) - assertEquals(it.name, stringValue) - } - - } -} diff --git a/tests/net/common/java/android/net/netstats/NetworkStatsApiTest.kt b/tests/net/common/java/android/net/netstats/NetworkStatsApiTest.kt deleted file mode 100644 index 7b22e45db90a..000000000000 --- a/tests/net/common/java/android/net/netstats/NetworkStatsApiTest.kt +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netstats - -import android.net.NetworkStats -import android.net.NetworkStats.DEFAULT_NETWORK_NO -import android.net.NetworkStats.DEFAULT_NETWORK_YES -import android.net.NetworkStats.Entry -import android.net.NetworkStats.IFACE_VT -import android.net.NetworkStats.METERED_NO -import android.net.NetworkStats.METERED_YES -import android.net.NetworkStats.ROAMING_NO -import android.net.NetworkStats.ROAMING_YES -import android.net.NetworkStats.SET_DEFAULT -import android.net.NetworkStats.SET_FOREGROUND -import android.net.NetworkStats.TAG_NONE -import android.os.Build -import androidx.test.filters.SmallTest -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.assertFieldCountEquals -import com.android.testutils.assertNetworkStatsEquals -import com.android.testutils.assertParcelingIsLossless -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 -import kotlin.test.assertEquals - -@RunWith(JUnit4::class) -@SmallTest -class NetworkStatsApiTest { - @Rule - @JvmField - val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.Q) - - private val testStatsEmpty = NetworkStats(0L, 0) - - // Note that these variables need to be initialized outside of constructor, initialize - // here with methods that don't exist in Q devices will result in crash. - - // stats1 and stats2 will have some entries with common keys, which are expected to - // be merged if performing add on these 2 stats. - private lateinit var testStats1: NetworkStats - private lateinit var testStats2: NetworkStats - - // This is a result of adding stats1 and stats2, while the merging of common key items is - // subject to test later, this should not be initialized with for a loop to add stats1 - // and stats2 above. - private lateinit var testStats3: NetworkStats - - companion object { - private const val TEST_IFACE = "test0" - private const val TEST_UID1 = 1001 - private const val TEST_UID2 = 1002 - } - - @Before - fun setUp() { - testStats1 = NetworkStats(0L, 0) - // Entries which only appear in set1. - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, 20, 3, 57, 40, 3)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_YES, DEFAULT_NETWORK_NO, 31, 7, 24, 5, 8)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_YES, ROAMING_NO, DEFAULT_NETWORK_NO, 25, 3, 47, 8, 2)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_FOREGROUND, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 37, 52, 1, 10, 4)) - // Entries which are common for set1 and set2. - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 101, 2, 103, 4, 5)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 17, 2, 11, 1, 0)) - .addEntry(Entry(TEST_IFACE, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 40, 1, 0, 0, 8)) - .addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 3, 1, 6, 2, 0)) - assertEquals(8, testStats1.size()) - - testStats2 = NetworkStats(0L, 0) - // Entries which are common for set1 and set2. - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 3, 15, 2, 31, 1)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_FOREGROUND, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 13, 61, 10, 1, 45)) - .addEntry(Entry(TEST_IFACE, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 11, 2, 3, 4, 7)) - .addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 4, 3, 2, 1, 0)) - // Entry which only appears in set2. - .addEntry(Entry(IFACE_VT, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 2, 3, 7, 8, 0)) - assertEquals(5, testStats2.size()) - - testStats3 = NetworkStats(0L, 9) - // Entries which are unique either in stats1 or stats2. - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 101, 2, 103, 4, 5)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_YES, DEFAULT_NETWORK_NO, 31, 7, 24, 5, 8)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_YES, ROAMING_NO, DEFAULT_NETWORK_NO, 25, 3, 47, 8, 2)) - .addEntry(Entry(IFACE_VT, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 2, 3, 7, 8, 0)) - // Entries which are common for stats1 and stats2 are being merged. - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, 20, 3, 57, 40, 3)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 20, 17, 13, 32, 1)) - .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_FOREGROUND, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 50, 113, 11, 11, 49)) - .addEntry(Entry(TEST_IFACE, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 51, 3, 3, 4, 15)) - .addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 7, 4, 8, 3, 0)) - assertEquals(9, testStats3.size()) - } - - @Test - fun testAddEntry() { - val expectedEntriesInStats2 = arrayOf( - Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 3, 15, 2, 31, 1), - Entry(TEST_IFACE, TEST_UID1, SET_FOREGROUND, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 13, 61, 10, 1, 45), - Entry(TEST_IFACE, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 11, 2, 3, 4, 7), - Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 4, 3, 2, 1, 0), - Entry(IFACE_VT, TEST_UID2, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 2, 3, 7, 8, 0)) - - // While testStats* are already initialized with addEntry, verify content added - // matches expectation. - for (i in expectedEntriesInStats2.indices) { - val entry = testStats2.getValues(i, null) - assertEquals(expectedEntriesInStats2[i], entry) - } - - // Verify entry updated with addEntry. - val stats = testStats2.addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 12, -5, 7, 0, 9)) - assertEquals(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 16, -2, 9, 1, 9), - stats.getValues(3, null)) - } - - @Test - fun testAdd() { - var stats = NetworkStats(0L, 0) - assertNetworkStatsEquals(testStatsEmpty, stats) - stats = stats.add(testStats2) - assertNetworkStatsEquals(testStats2, stats) - stats = stats.add(testStats1) - // EMPTY + STATS2 + STATS1 = STATS3 - assertNetworkStatsEquals(testStats3, stats) - } - - @Test - fun testParcelUnparcel() { - assertParcelingIsLossless(testStatsEmpty) - assertParcelingIsLossless(testStats1) - assertParcelingIsLossless(testStats2) - assertFieldCountEquals(15, NetworkStats::class.java) - } - - @Test - fun testDescribeContents() { - assertEquals(0, testStatsEmpty.describeContents()) - assertEquals(0, testStats1.describeContents()) - assertEquals(0, testStats2.describeContents()) - assertEquals(0, testStats3.describeContents()) - } - - @Test - fun testSubtract() { - // STATS3 - STATS2 = STATS1 - assertNetworkStatsEquals(testStats1, testStats3.subtract(testStats2)) - // STATS3 - STATS1 = STATS2 - assertNetworkStatsEquals(testStats2, testStats3.subtract(testStats1)) - } - - @Test - fun testMethodsDontModifyReceiver() { - listOf(testStatsEmpty, testStats1, testStats2, testStats3).forEach { - val origStats = it.clone() - it.addEntry(Entry(TEST_IFACE, TEST_UID1, SET_FOREGROUND, TAG_NONE, - METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 13, 61, 10, 1, 45)) - it.add(testStats3) - it.subtract(testStats1) - assertNetworkStatsEquals(origStats, it) - } - } -}
\ No newline at end of file diff --git a/tests/net/common/java/android/net/util/SocketUtilsTest.kt b/tests/net/common/java/android/net/util/SocketUtilsTest.kt deleted file mode 100644 index aaf97f36889b..000000000000 --- a/tests/net/common/java/android/net/util/SocketUtilsTest.kt +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.util - -import android.os.Build -import android.system.NetlinkSocketAddress -import android.system.Os -import android.system.OsConstants.AF_INET -import android.system.OsConstants.ETH_P_ALL -import android.system.OsConstants.IPPROTO_UDP -import android.system.OsConstants.RTMGRP_NEIGH -import android.system.OsConstants.SOCK_DGRAM -import android.system.PacketSocketAddress -import androidx.test.filters.SmallTest -import androidx.test.runner.AndroidJUnit4 -import com.android.testutils.DevSdkIgnoreRule -import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Assert.fail -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -private const val TEST_INDEX = 123 -private const val TEST_PORT = 555 -private const val FF_BYTE = 0xff.toByte() - -@RunWith(AndroidJUnit4::class) -@SmallTest -class SocketUtilsTest { - @Rule @JvmField - val ignoreRule = DevSdkIgnoreRule() - - @Test - fun testMakeNetlinkSocketAddress() { - val nlAddress = SocketUtils.makeNetlinkSocketAddress(TEST_PORT, RTMGRP_NEIGH) - if (nlAddress is NetlinkSocketAddress) { - assertEquals(TEST_PORT, nlAddress.getPortId()) - assertEquals(RTMGRP_NEIGH, nlAddress.getGroupsMask()) - } else { - fail("Not NetlinkSocketAddress object") - } - } - - @Test - fun testMakePacketSocketAddress_Q() { - val pkAddress = SocketUtils.makePacketSocketAddress(ETH_P_ALL, TEST_INDEX) - assertTrue("Not PacketSocketAddress object", pkAddress is PacketSocketAddress) - - val pkAddress2 = SocketUtils.makePacketSocketAddress(TEST_INDEX, ByteArray(6) { FF_BYTE }) - assertTrue("Not PacketSocketAddress object", pkAddress2 is PacketSocketAddress) - } - - @Test @IgnoreUpTo(Build.VERSION_CODES.Q) - fun testMakePacketSocketAddress() { - val pkAddress = SocketUtils.makePacketSocketAddress( - ETH_P_ALL, TEST_INDEX, ByteArray(6) { FF_BYTE }) - assertTrue("Not PacketSocketAddress object", pkAddress is PacketSocketAddress) - } - - @Test - fun testCloseSocket() { - // Expect no exception happening with null object. - SocketUtils.closeSocket(null) - - val fd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) - assertTrue(fd.valid()) - SocketUtils.closeSocket(fd) - assertFalse(fd.valid()) - // Expecting socket should be still invalid even closed socket again. - SocketUtils.closeSocket(fd) - assertFalse(fd.valid()) - } -} |