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
commit 31022d6cda
145 changed files with 42447 additions and 0 deletions

8
tests/OWNERS Normal file
View file

@ -0,0 +1,8 @@
set noparent
codewiz@google.com
jchalard@google.com
junyulai@google.com
lorenzo@google.com
reminv@google.com
satk@google.com

34
tests/TEST_MAPPING Normal file
View file

@ -0,0 +1,34 @@
{
"presubmit": [
{
"name": "FrameworksNetIntegrationTests"
}
],
"postsubmit": [
{
"name": "FrameworksNetDeflakeTest"
}
],
"auto-postsubmit": [
// Test tag for automotive targets. These are only running in postsubmit so as to harden the
// automotive targets to avoid introducing additional test flake and build time. The plan for
// presubmit testing for auto is to augment the existing tests to cover auto use cases as well.
// Additionally, this tag is used in targeted test suites to limit resource usage on the test
// infra during the hardening phase.
// TODO: this tag to be removed once the above is no longer an issue.
{
"name": "FrameworksNetTests"
},
{
"name": "FrameworksNetIntegrationTests"
},
{
"name": "FrameworksNetDeflakeTest"
}
],
"imports": [
{
"path": "packages/modules/Connectivity"
}
]
}

44
tests/common/Android.bp Normal file
View file

@ -0,0 +1,44 @@
//
// 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.
//
// Tests in this folder are included both in unit tests and CTS.
// They must be fast and stable, and exercise public or test APIs.
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
java_library {
name: "FrameworksNetCommonTests",
srcs: ["java/**/*.java", "java/**/*.kt"],
static_libs: [
"androidx.core_core",
"androidx.test.rules",
"junit",
"mockito-target-minus-junit4",
"modules-utils-build",
"net-tests-utils",
"net-utils-framework-common",
"platform-test-annotations",
],
libs: [
"android.test.base.stubs",
],
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.net.ParseException
import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.testutils.DevSdkIgnoreRule
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@SmallTest
@RunWith(AndroidJUnit4::class)
class ParseExceptionTest {
@get:Rule
val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.R)
@Test
fun testConstructor_WithCause() {
val testMessage = "Test message"
val base = Exception("Test")
val exception = ParseException(testMessage, base)
assertEquals(testMessage, exception.response)
assertEquals(base, exception.cause)
}
@Test
fun testConstructor_NoCause() {
val testMessage = "Test message"
val exception = ParseException(testMessage)
assertEquals(testMessage, exception.response)
assertNull(exception.cause)
}
}

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

Some files were not shown because too many files have changed in this diff Show more