Move net unit tests to packages/Connectivity

Move the tests together with packages/Connectivity code, so both can be
moved to packages/modules/Connectivity together.

Also reorganize unit tests in a unit/ directory, as other tests
(integration/, common/ etc.) have been added in tests/net since they
were created. This makes the directory structure consistent.

Test: atest FrameworksNetTests
Bug: 187814163
Merged-In: I254ffd1c08ec058d594b4ea55cbae5505f8497cc

Change-Id: I254ffd1c08ec058d594b4ea55cbae5505f8497cc
This commit is contained in:
Remi NGUYEN VAN
2021-05-11 13:37:06 +00:00
parent 26cc1ff94e
commit 0d51e44e09
145 changed files with 625 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
/*
* 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))
}
}

View File

@@ -0,0 +1,121 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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));
}
}

View File

@@ -0,0 +1,112 @@
/*
* 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));
}
}

View File

@@ -0,0 +1,374 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,120 @@
/*
* 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))
}

View File

@@ -0,0 +1,518 @@
/*
* 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");
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,75 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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())
}
}

View File

@@ -0,0 +1,91 @@
/*
* 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())
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,201 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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")))
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,160 @@
/*
* 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.platform.test.annotations.AppModeFull;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
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);
@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());
}
@Test
public void testGetPrivateDnsBypassingCopy() {
final Network copy = mNetwork.getPrivateDnsBypassingCopy();
assertEquals(mNetwork.netId, copy.netId);
assertNotEquals(copy.netId, copy.getNetIdForResolv());
assertNotEquals(mNetwork.getNetIdForResolv(), copy.getNetIdForResolv());
}
}

View File

@@ -0,0 +1,152 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,434 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,269 @@
/*
* 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));
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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());
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.ownerUid)
assertEquals(TEST_IFACE, testInfo.iface)
assertEquals(TEST_IFACE_LIST, testInfo.underlyingIfaces)
assertParcelSane(testInfo, 3)
val emptyInfo = UnderlyingNetworkInfo(0, String(), listOf())
assertEquals(0, emptyInfo.ownerUid)
assertEquals(String(), emptyInfo.iface)
assertEquals(listOf(), emptyInfo.underlyingIfaces)
assertParcelSane(emptyInfo, 3)
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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));
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,65 @@
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())
}
}

View File

@@ -0,0 +1,161 @@
/*
* 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);
}

View File

@@ -0,0 +1,39 @@
/*
* 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)
}
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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)
}
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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)
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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)
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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)
}
}
}

View File

@@ -0,0 +1,209 @@
/*
* 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)
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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())
}
}