2025-03-24 16:49:13 +09:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2025 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.ParcelUtils.assertParcelingIsLossless;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertNotEquals;
|
2025-05-21 15:05:35 +09:00
|
|
|
import static org.junit.Assert.assertNull;
|
2025-03-24 16:49:13 +09:00
|
|
|
import static org.junit.Assert.assertThrows;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
|
|
|
|
|
import androidx.test.filters.SmallTest;
|
|
|
|
|
|
|
|
|
|
import com.android.modules.utils.build.SdkLevel;
|
|
|
|
|
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
|
|
|
|
|
import com.android.testutils.DevSdkIgnoreRunner;
|
|
|
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
@SmallTest
|
|
|
|
|
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
|
|
|
|
|
@RunWith(DevSdkIgnoreRunner.class)
|
|
|
|
|
public class EthernetConfigurationTest {
|
|
|
|
|
// Setup 2 different IP configurations for building different ethernet configurations.
|
|
|
|
|
private IpConfiguration mIpConfiguration1;
|
|
|
|
|
private IpConfiguration mIpConfiguration2;
|
|
|
|
|
private NetworkCapabilities mNetworkCapabilities;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
LinkAddress linkAddr = new LinkAddress("192.168.1.100/25");
|
|
|
|
|
InetAddress gateway = InetAddresses.parseNumericAddress("192.168.1.1");
|
|
|
|
|
InetAddress dns1 = InetAddresses.parseNumericAddress("8.8.8.8");
|
|
|
|
|
InetAddress dns2 = InetAddresses.parseNumericAddress("8.8.4.4");
|
|
|
|
|
ArrayList<InetAddress> dnsServers = new ArrayList<>();
|
|
|
|
|
dnsServers.add(dns1);
|
|
|
|
|
dnsServers.add(dns2);
|
|
|
|
|
StaticIpConfiguration staticIpConfig = new StaticIpConfiguration.Builder()
|
|
|
|
|
.setIpAddress(linkAddr)
|
|
|
|
|
.setGateway(gateway)
|
|
|
|
|
.setDnsServers(dnsServers)
|
|
|
|
|
.build();
|
|
|
|
|
ProxyInfo proxy1 = ProxyInfo.buildDirectProxy("test1", 8888);
|
|
|
|
|
ProxyInfo proxy2 = ProxyInfo.buildDirectProxy("test2", 8888);
|
|
|
|
|
|
|
|
|
|
mIpConfiguration1 = new IpConfiguration.Builder()
|
|
|
|
|
.setStaticIpConfiguration(staticIpConfig)
|
|
|
|
|
.setHttpProxy(proxy1)
|
|
|
|
|
.build();
|
|
|
|
|
mIpConfiguration2 = new IpConfiguration.Builder()
|
|
|
|
|
.setStaticIpConfiguration(staticIpConfig)
|
|
|
|
|
.setHttpProxy(proxy2)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
mNetworkCapabilities = new NetworkCapabilities.Builder()
|
|
|
|
|
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertIpConfigurationEqual(IpConfiguration source, IpConfiguration target) {
|
|
|
|
|
assertEquals(source.getIpAssignment(), target.getIpAssignment());
|
|
|
|
|
assertEquals(source.getProxySettings(), target.getProxySettings());
|
|
|
|
|
assertEquals(source.getHttpProxy(), target.getHttpProxy());
|
|
|
|
|
assertEquals(source.getStaticIpConfiguration(), target.getStaticIpConfiguration());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertNetworkCapabilitiesEqual(
|
|
|
|
|
NetworkCapabilities source, NetworkCapabilities target) {
|
2025-05-21 15:05:35 +09:00
|
|
|
if (source == null || target == null) {
|
|
|
|
|
assertNull(target);
|
|
|
|
|
assertNull(source);
|
|
|
|
|
} else {
|
|
|
|
|
assertTrue(source.equalsNetCapabilities(target));
|
|
|
|
|
}
|
2025-03-24 16:49:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2025-05-21 15:05:35 +09:00
|
|
|
public void testBuilderNotPersisted() {
|
|
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
assertIpConfigurationEqual(config.getIpConfiguration(), mIpConfiguration1);
|
|
|
|
|
assertNetworkCapabilitiesEqual(config.getNetworkCapabilities(), mNetworkCapabilities);
|
2025-06-19 10:58:25 +09:00
|
|
|
assertEquals(config.getMeteredOverride(),
|
|
|
|
|
EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED);
|
2025-05-21 15:05:35 +09:00
|
|
|
assertEquals(config.getPersistence(), EthernetConfiguration.PERSISTENCE_NOT_PERSISTED);
|
|
|
|
|
}
|
|
|
|
|
@Test
|
|
|
|
|
public void testBuilderPersisted() {
|
|
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_IS_PERSISTED)
|
|
|
|
|
.build();
|
|
|
|
|
assertIpConfigurationEqual(config.getIpConfiguration(), mIpConfiguration1);
|
|
|
|
|
assertNetworkCapabilitiesEqual(config.getNetworkCapabilities(), null);
|
2025-06-19 10:58:25 +09:00
|
|
|
assertEquals(config.getMeteredOverride(),
|
|
|
|
|
EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED);
|
2025-05-21 15:05:35 +09:00
|
|
|
assertEquals(config.getPersistence(), EthernetConfiguration.PERSISTENCE_IS_PERSISTED);
|
2025-03-24 16:49:13 +09:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 10:58:25 +09:00
|
|
|
@Test
|
|
|
|
|
public void testConstructorMeteredOverrideNotSet() {
|
|
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
|
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
|
|
|
|
assertEquals(config.getMeteredOverride(),
|
|
|
|
|
EthernetConfiguration.METERED_OVERRIDE_NONE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 16:49:13 +09:00
|
|
|
@Test
|
2025-05-21 15:05:35 +09:00
|
|
|
public void testConstructorIsPersistedNotSet() {
|
|
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.build();
|
|
|
|
|
assertEquals(config.getPersistence(), EthernetConfiguration.PERSISTENCE_NOT_PERSISTED);
|
2025-03-24 16:49:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2025-05-21 15:05:35 +09:00
|
|
|
public void testConstructorWithIpConfigNotSet() {
|
|
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(null)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
|
|
|
|
assertNull(config.getIpConfiguration());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testConstructorWithPersistedNonNullNetCapabilities() {
|
|
|
|
|
assertThrows("Network capabilities cannot be persisted.",
|
|
|
|
|
IllegalArgumentException.class,
|
|
|
|
|
() -> new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_IS_PERSISTED)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testConstructorWithNullIpConfigAndNullNetCapabilities() {
|
|
|
|
|
assertThrows("IP configuration and network capabilities are"
|
|
|
|
|
+ "both null, cannot construct an empty EthernetConfiguration.",
|
|
|
|
|
IllegalArgumentException.class,
|
|
|
|
|
() -> new EthernetConfiguration.Builder()
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_IS_PERSISTED)
|
|
|
|
|
.build());
|
2025-03-24 16:49:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testHashCodeConsistency() {
|
2025-05-21 15:05:35 +09:00
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
int hash1 = config.hashCode();
|
|
|
|
|
int hash2 = config.hashCode();
|
|
|
|
|
assertEquals(hash1, hash2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testHashCodeOfDifferentConfig() {
|
2025-05-21 15:05:35 +09:00
|
|
|
EthernetConfiguration config1 = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
|
|
|
|
EthernetConfiguration config2 = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration2)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
int hash1 = config1.hashCode();
|
|
|
|
|
int hash2 = config2.hashCode();
|
|
|
|
|
assertNotEquals(hash1, hash2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testHashCodeOfTheSameConfig() {
|
2025-05-21 15:05:35 +09:00
|
|
|
EthernetConfiguration config1 = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
|
|
|
|
EthernetConfiguration config2 = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
int hash1 = config1.hashCode();
|
|
|
|
|
int hash2 = config2.hashCode();
|
|
|
|
|
assertEquals(hash1, hash2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testToString() {
|
2025-05-21 15:05:35 +09:00
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
String expect;
|
2025-05-21 15:05:35 +09:00
|
|
|
// After version V, NOT_BANDWIDTH_CONSTRAINED is added to network capabilities by default
|
2025-03-24 16:49:13 +09:00
|
|
|
// while devices before V do not have this default capability.
|
|
|
|
|
if (SdkLevel.isAtLeastV()) {
|
|
|
|
|
expect = "IP configurations: IP assignment: STATIC\n"
|
|
|
|
|
+ "Static configuration: IP address 192.168.1.100/25 Gateway 192.168.1.1 "
|
|
|
|
|
+ " DNS servers: [ 8.8.8.8 8.8.4.4 ] Domains \n"
|
|
|
|
|
+ "Proxy settings: PAC\n"
|
|
|
|
|
+ "HTTP proxy: [test1] 8888\n"
|
|
|
|
|
+ "Network capabilities: [ Transports: ETHERNET Capabilities: NOT_RESTRICTED"
|
2025-05-21 15:05:35 +09:00
|
|
|
+ "&TRUSTED&NOT_VPN&NOT_BANDWIDTH_CONSTRAINED UnderlyingNetworks: Null]"
|
2025-06-20 17:41:33 +09:00
|
|
|
+ ", Metered override: FORCE_METERED, Is persisted: NOT_PERSISTED";
|
2025-03-24 16:49:13 +09:00
|
|
|
} else {
|
|
|
|
|
expect = "IP configurations: IP assignment: STATIC\n"
|
|
|
|
|
+ "Static configuration: IP address 192.168.1.100/25 Gateway 192.168.1.1 "
|
|
|
|
|
+ " DNS servers: [ 8.8.8.8 8.8.4.4 ] Domains \n"
|
|
|
|
|
+ "Proxy settings: PAC\n"
|
|
|
|
|
+ "HTTP proxy: [test1] 8888\n"
|
|
|
|
|
+ "Network capabilities: [ Transports: ETHERNET Capabilities: NOT_RESTRICTED"
|
2025-06-20 17:41:33 +09:00
|
|
|
+ "&TRUSTED&NOT_VPN UnderlyingNetworks: Null], Metered override: FORCE_METERED"
|
|
|
|
|
+ ", Is persisted: NOT_PERSISTED";
|
2025-03-24 16:49:13 +09:00
|
|
|
}
|
|
|
|
|
assertEquals(config.toString(), expect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testParcel() {
|
2025-05-21 15:05:35 +09:00
|
|
|
EthernetConfiguration config = new EthernetConfiguration.Builder()
|
|
|
|
|
.setIpConfiguration(mIpConfiguration1)
|
|
|
|
|
.setNetworkCapabilities(mNetworkCapabilities)
|
2025-06-19 10:58:25 +09:00
|
|
|
.setMeteredOverride(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED)
|
2025-05-21 15:05:35 +09:00
|
|
|
.setPersistence(EthernetConfiguration.PERSISTENCE_NOT_PERSISTED)
|
|
|
|
|
.build();
|
2025-03-24 16:49:13 +09:00
|
|
|
assertParcelingIsLossless(config);
|
|
|
|
|
}
|
|
|
|
|
}
|