Merge "Wait for test network disconnection before exiting tearDown"
diff --git a/framework/api/system-current.txt b/framework/api/system-current.txt
index f1298ce..8b35197 100644
--- a/framework/api/system-current.txt
+++ b/framework/api/system-current.txt
@@ -433,6 +433,7 @@
public abstract class QosFilter {
method @NonNull public abstract android.net.Network getNetwork();
method public abstract boolean matchesLocalAddress(@NonNull java.net.InetAddress, int, int);
+ method public boolean matchesProtocol(int);
method public abstract boolean matchesRemoteAddress(@NonNull java.net.InetAddress, int, int);
}
@@ -453,6 +454,7 @@
public final class QosSocketInfo implements android.os.Parcelable {
ctor public QosSocketInfo(@NonNull android.net.Network, @NonNull java.net.Socket) throws java.io.IOException;
+ ctor public QosSocketInfo(@NonNull android.net.Network, @NonNull java.net.DatagramSocket) throws java.io.IOException;
method public int describeContents();
method @NonNull public java.net.InetSocketAddress getLocalSocketAddress();
method @NonNull public android.net.Network getNetwork();
@@ -480,6 +482,14 @@
ctor public SocketNotBoundException();
}
+ public class SocketNotConnectedException extends java.lang.Exception {
+ ctor public SocketNotConnectedException();
+ }
+
+ public class SocketRemoteAddressChangedException extends java.lang.Exception {
+ ctor public SocketRemoteAddressChangedException();
+ }
+
public final class StaticIpConfiguration implements android.os.Parcelable {
ctor public StaticIpConfiguration();
ctor public StaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
diff --git a/framework/src/android/net/QosFilter.java b/framework/src/android/net/QosFilter.java
index 01dc4bb..a731b23 100644
--- a/framework/src/android/net/QosFilter.java
+++ b/framework/src/android/net/QosFilter.java
@@ -97,10 +97,15 @@
* Determines whether or not the parameter will be matched with this filter.
*
* @param protocol the protocol such as TCP or UDP included in IP packet filter set of a QoS
- * flow assigned on {@link Network}.
+ * flow assigned on {@link Network}. Only {@code IPPROTO_TCP} and {@code
+ * IPPROTO_UDP} currently supported.
* @return whether the parameters match the socket type of the filter
- * @hide
*/
- public abstract boolean matchesProtocol(int protocol);
+ // Since this method is added in U, it's required to be default method for binary compatibility
+ // with existing @SystemApi.
+ // IPPROTO_* are not compile-time constants, so they are not annotated with @IntDef.
+ public boolean matchesProtocol(int protocol) {
+ return false;
+ }
}
diff --git a/framework/src/android/net/QosSocketInfo.java b/framework/src/android/net/QosSocketInfo.java
index da9b356..1c3db23 100644
--- a/framework/src/android/net/QosSocketInfo.java
+++ b/framework/src/android/net/QosSocketInfo.java
@@ -144,7 +144,6 @@
*
* @param network the network
* @param socket the bound {@link DatagramSocket}
- * @hide
*/
public QosSocketInfo(@NonNull final Network network, @NonNull final DatagramSocket socket)
throws IOException {
diff --git a/framework/src/android/net/SocketNotConnectedException.java b/framework/src/android/net/SocketNotConnectedException.java
index fa2a615..a6357c7 100644
--- a/framework/src/android/net/SocketNotConnectedException.java
+++ b/framework/src/android/net/SocketNotConnectedException.java
@@ -16,13 +16,18 @@
package android.net;
+import android.annotation.SystemApi;
+
+import com.android.internal.annotations.VisibleForTesting;
+
/**
* Thrown when a previously bound socket becomes unbound.
*
* @hide
*/
+@SystemApi
public class SocketNotConnectedException extends Exception {
- /** @hide */
+ @VisibleForTesting
public SocketNotConnectedException() {
super("The socket is not connected");
}
diff --git a/framework/src/android/net/SocketRemoteAddressChangedException.java b/framework/src/android/net/SocketRemoteAddressChangedException.java
index ecaeebc..e13d5ed 100644
--- a/framework/src/android/net/SocketRemoteAddressChangedException.java
+++ b/framework/src/android/net/SocketRemoteAddressChangedException.java
@@ -16,13 +16,18 @@
package android.net;
+import android.annotation.SystemApi;
+
+import com.android.internal.annotations.VisibleForTesting;
+
/**
* Thrown when the local address of the socket has changed.
*
* @hide
*/
+@SystemApi
public class SocketRemoteAddressChangedException extends Exception {
- /** @hide */
+ @VisibleForTesting
public SocketRemoteAddressChangedException() {
super("The remote address of the socket changed");
}
diff --git a/nearby/halfsheet/res/values-ro/strings.xml b/nearby/halfsheet/res/values-ro/strings.xml
index 5b50f15..189f698 100644
--- a/nearby/halfsheet/res/values-ro/strings.xml
+++ b/nearby/halfsheet/res/values-ro/strings.xml
@@ -18,12 +18,12 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="fast_pair_setup_in_progress" msgid="4158762239172829807">"Începe configurarea…"</string>
- <string name="fast_pair_title_setup" msgid="2894360355540593246">"Configurați dispozitivul"</string>
+ <string name="fast_pair_title_setup" msgid="2894360355540593246">"Configurează dispozitivul"</string>
<string name="fast_pair_device_ready" msgid="2903490346082833101">"Dispozitivul s-a conectat"</string>
<string name="fast_pair_title_fail" msgid="5677174346601290232">"Nu s-a putut conecta"</string>
<string name="paring_action_done" msgid="6888875159174470731">"Gata"</string>
- <string name="paring_action_save" msgid="6259357442067880136">"Salvați"</string>
- <string name="paring_action_connect" msgid="4801102939608129181">"Conectați"</string>
- <string name="paring_action_launch" msgid="8940808384126591230">"Configurați"</string>
+ <string name="paring_action_save" msgid="6259357442067880136">"Salvează"</string>
+ <string name="paring_action_connect" msgid="4801102939608129181">"Conectează"</string>
+ <string name="paring_action_launch" msgid="8940808384126591230">"Configurează"</string>
<string name="paring_action_settings" msgid="424875657242864302">"Setări"</string>
</resources>
diff --git a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceBroadcastRequestTest.java b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceBroadcastRequestTest.java
index 1daa410..eaa5ca1 100644
--- a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceBroadcastRequestTest.java
+++ b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceBroadcastRequestTest.java
@@ -54,7 +54,7 @@
private static final byte[] SECRETE_ID = new byte[]{1, 2, 3, 4};
private static final byte[] AUTHENTICITY_KEY = new byte[]{0, 1, 1, 1};
private static final byte[] METADATA_ENCRYPTION_KEY = new byte[]{1, 1, 3, 4, 5};
- private static final int KEY = 1234;
+ private static final int KEY = 3;
private static final byte[] VALUE = new byte[]{1, 1, 1, 1};
private static final String DEVICE_NAME = "test_device";
diff --git a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceDeviceTest.java b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceDeviceTest.java
index 5fefc68..94f8fe7 100644
--- a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceDeviceTest.java
+++ b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceDeviceTest.java
@@ -45,7 +45,7 @@
private static final int RSSI = -40;
private static final int MEDIUM = NearbyDevice.Medium.BLE;
private static final String DEVICE_NAME = "testDevice";
- private static final int KEY = 1234;
+ private static final int KEY = 3;
private static final byte[] VALUE = new byte[]{1, 1, 1, 1};
private static final byte[] SALT = new byte[]{2, 3};
private static final byte[] SECRET_ID = new byte[]{11, 13};
diff --git a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceScanFilterTest.java b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceScanFilterTest.java
index b7fe40a..cecdfd2 100644
--- a/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceScanFilterTest.java
+++ b/nearby/tests/cts/fastpair/src/android/nearby/cts/PresenceScanFilterTest.java
@@ -48,7 +48,7 @@
private static final byte[] PUBLIC_KEY = new byte[]{1, 1, 2, 2};
private static final byte[] ENCRYPTED_METADATA = new byte[]{1, 2, 3, 4, 5};
private static final byte[] METADATA_ENCRYPTION_KEY_TAG = new byte[]{1, 1, 3, 4, 5};
- private static final int KEY = 1234;
+ private static final int KEY = 3;
private static final byte[] VALUE = new byte[]{1, 1, 1, 1};
diff --git a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
index 56c21eb..2196bf8 100644
--- a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
+++ b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
@@ -586,6 +586,14 @@
}
private void stop() {
+ // Unregister NetworkAgent before stopping IpClient, so destroyNativeNetwork (which
+ // deletes routes) hopefully happens before stop() finishes execution. Otherwise, it may
+ // delete the new routes when IpClient gets restarted.
+ if (mNetworkAgent != null) {
+ mNetworkAgent.unregister();
+ mNetworkAgent = null;
+ }
+
// Invalidate all previous start requests
if (mIpClient != null) {
mIpClient.shutdown();
@@ -595,10 +603,6 @@
mIpClientCallback = null;
- if (mNetworkAgent != null) {
- mNetworkAgent.unregister();
- mNetworkAgent = null;
- }
mLinkProperties.clear();
}
diff --git a/service/Android.bp b/service/Android.bp
index 7d3b39a..0a00362 100644
--- a/service/Android.bp
+++ b/service/Android.bp
@@ -223,6 +223,9 @@
name: "service-connectivity-defaults",
sdk_version: "system_server_current",
min_sdk_version: "30",
+ defaults: [
+ "standalone-system-server-module-optimize-defaults",
+ ],
// This library combines system server jars that have access to different bootclasspath jars.
// Lower SDK service jars must not depend on higher SDK jars as that would let them
// transitively depend on the wrong bootclasspath jars. Sources also cannot be added here as
@@ -252,8 +255,6 @@
"com.android.tethering",
],
optimize: {
- enabled: true,
- shrink: true,
proguard_flags_files: ["proguard.flags"],
},
lint: { strict_updatability_linting: true },
diff --git a/service/ServiceConnectivityResources/res/values-kk/strings.xml b/service/ServiceConnectivityResources/res/values-kk/strings.xml
index 00c0f39..efe23b6 100644
--- a/service/ServiceConnectivityResources/res/values-kk/strings.xml
+++ b/service/ServiceConnectivityResources/res/values-kk/strings.xml
@@ -33,7 +33,7 @@
<string name="network_switch_metered_detail" msgid="1257300152739542096">"Құрылғы <xliff:g id="PREVIOUS_NETWORK">%2$s</xliff:g> желісінде интернетпен байланыс жоғалған жағдайда <xliff:g id="NEW_NETWORK">%1$s</xliff:g> желісін пайдаланады. Деректер ақысы алынуы мүмкін."</string>
<string name="network_switch_metered_toast" msgid="70691146054130335">"<xliff:g id="PREVIOUS_NETWORK">%1$s</xliff:g> желісінен <xliff:g id="NEW_NETWORK">%2$s</xliff:g> желісіне ауысты"</string>
<string-array name="network_switch_type_name">
- <item msgid="3004933964374161223">"мобильдік деректер"</item>
+ <item msgid="3004933964374161223">"мобильдік интернет"</item>
<item msgid="5624324321165953608">"Wi-Fi"</item>
<item msgid="5667906231066981731">"Bluetooth"</item>
<item msgid="346574747471703768">"Ethernet"</item>
diff --git a/service/ServiceConnectivityResources/res/values-ro/strings.xml b/service/ServiceConnectivityResources/res/values-ro/strings.xml
index fa5848f..4ff5290 100644
--- a/service/ServiceConnectivityResources/res/values-ro/strings.xml
+++ b/service/ServiceConnectivityResources/res/values-ro/strings.xml
@@ -18,12 +18,12 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="connectivityResourcesAppLabel" msgid="2476261877900882974">"Resurse pentru conectivitatea sistemului"</string>
- <string name="wifi_available_sign_in" msgid="8041178343789805553">"Conectați-vă la rețeaua Wi-Fi"</string>
- <string name="network_available_sign_in" msgid="2622520134876355561">"Conectați-vă la rețea"</string>
+ <string name="wifi_available_sign_in" msgid="8041178343789805553">"Conectează-te la rețeaua Wi-Fi"</string>
+ <string name="network_available_sign_in" msgid="2622520134876355561">"Conectează-te la rețea"</string>
<!-- no translation found for network_available_sign_in_detailed (8439369644697866359) -->
<skip />
<string name="wifi_no_internet" msgid="1326348603404555475">"<xliff:g id="NETWORK_SSID">%1$s</xliff:g> nu are acces la internet"</string>
- <string name="wifi_no_internet_detailed" msgid="1746921096565304090">"Atingeți pentru opțiuni"</string>
+ <string name="wifi_no_internet_detailed" msgid="1746921096565304090">"Atinge pentru opțiuni"</string>
<string name="mobile_no_internet" msgid="4087718456753201450">"Rețeaua mobilă nu are acces la internet"</string>
<string name="other_networks_no_internet" msgid="5693932964749676542">"Rețeaua nu are acces la internet"</string>
<string name="private_dns_broken_detailed" msgid="2677123850463207823">"Serverul DNS privat nu poate fi accesat"</string>
diff --git a/service/ServiceConnectivityResources/res/values/config.xml b/service/ServiceConnectivityResources/res/values/config.xml
index bff6953..7d777ec 100644
--- a/service/ServiceConnectivityResources/res/values/config.xml
+++ b/service/ServiceConnectivityResources/res/values/config.xml
@@ -78,6 +78,27 @@
Settings.Global.NETWORK_AVOID_BAD_WIFI. This is the default value of that setting. -->
<integer translatable="false" name="config_networkAvoidBadWifi">1</integer>
+ <!-- Whether the device should actively prefer bad wifi to good cell on Android 12/13.
+
+ This setting only makes sense if the system is configured not to avoid bad wifis
+ (config_networkAvoidBadWifi=0 and Settings.Global.NETWORK_AVOID_BAD_WIFI=IGNORE
+ or PROMPT), otherwise it's not used.
+
+ On Android 12 and 13, if this is 0, when ranking a bad wifi that never validated against
+ validated mobile data, the system will prefer mobile data. It will prefer wifi if wifi
+ loses validation later. This is the default behavior up to Android 13.
+ This behavior avoids the device losing internet access when walking past a wifi network
+ with no internet access.
+
+ If this is 1, then in the same scenario, the system will prefer mobile data until the wifi
+ completes its first validation attempt (or the attempt times out), and after that it
+ will prefer the wifi even if it doesn't provide internet access, unless there is a captive
+ portal on that wifi.
+
+ On Android 14 and above, the behavior is always like 1, regardless of the value of this
+ setting. -->
+ <integer translatable="false" name="config_activelyPreferBadWifi">1</integer>
+
<!-- Array of ConnectivityManager.TYPE_xxxx constants for networks that may only
be controlled by systemOrSignature apps. -->
<integer-array translatable="false" name="config_protectedNetworks">
diff --git a/service/ServiceConnectivityResources/res/values/overlayable.xml b/service/ServiceConnectivityResources/res/values/overlayable.xml
index 3389d63..4c85e8c 100644
--- a/service/ServiceConnectivityResources/res/values/overlayable.xml
+++ b/service/ServiceConnectivityResources/res/values/overlayable.xml
@@ -24,6 +24,7 @@
<item type="integer" name="config_networkMeteredMultipathPreference"/>
<item type="array" name="config_networkSupportedKeepaliveCount"/>
<item type="integer" name="config_networkAvoidBadWifi"/>
+ <item type="integer" name="config_activelyPreferBadWifi"/>
<item type="array" name="config_protectedNetworks"/>
<item type="bool" name="config_vehicleInternalNetworkAlwaysRequested"/>
<item type="integer" name="config_networkWakeupPacketMark"/>
diff --git a/service/proguard.flags b/service/proguard.flags
index f546e82..478566c 100644
--- a/service/proguard.flags
+++ b/service/proguard.flags
@@ -1,15 +1,8 @@
-# Make sure proguard keeps all connectivity classes
-# TODO: instead of keeping everything, consider listing only "entry points"
-# (service loader, JNI registered methods, etc) and letting the optimizer do its job
--keep class android.net.** { *; }
--keep class !com.android.server.nearby.**,com.android.server.** { *; }
-# Prevent proguard from stripping out any nearby-service and fast-pair-lite-protos fields.
--keep class com.android.server.nearby.NearbyService { *; }
+# Keep JNI registered methods
+-keepclasseswithmembers,includedescriptorclasses class * { native <methods>; }
-# The lite proto runtime uses reflection to access fields based on the names in
-# the schema, keep all the fields.
-# This replicates the base proguard rule used by the build by default
-# (proguard_basic_keeps.flags), but needs to be specified here because the
-# com.google.protobuf package is jarjared to use a package prefix.
--keepclassmembers class * extends **.com.google.protobuf.MessageLite { <fields>; }
+# Keep classes extending structured message.
+-keepclassmembers public class * extends **.com.android.net.module.util.Struct {
+ *;
+}
diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index 93265e5..1236243 100755
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -205,7 +205,6 @@
import android.net.resolv.aidl.Nat64PrefixEventParcel;
import android.net.resolv.aidl.PrivateDnsValidationEventParcel;
import android.net.shared.PrivateDnsConfig;
-import android.net.util.MultinetworkPolicyTracker;
import android.net.wifi.WifiInfo;
import android.os.BatteryStatsManager;
import android.os.Binder;
@@ -272,6 +271,7 @@
import com.android.server.connectivity.KeepaliveTracker;
import com.android.server.connectivity.LingerMonitor;
import com.android.server.connectivity.MockableSystemProperties;
+import com.android.server.connectivity.MultinetworkPolicyTracker;
import com.android.server.connectivity.NetworkAgentInfo;
import com.android.server.connectivity.NetworkDiagnostics;
import com.android.server.connectivity.NetworkNotificationManager;
@@ -357,7 +357,10 @@
// of connectivity (valid, partial, captive portal). If none has been detected after this
// delay, the stack considers this network bad, which may affect how it's handled in ranking
// according to config_networkAvoidBadWifi.
- private static final int INITIAL_EVALUATION_TIMEOUT_MS = 20 * 1000;
+ // Timeout in case the "actively prefer bad wifi" feature is on
+ private static final int ACTIVELY_PREFER_BAD_WIFI_INITIAL_TIMEOUT_MS = 20 * 1000;
+ // Timeout in case the "actively prefer bad wifi" feature is off
+ private static final int DONT_ACTIVELY_PREFER_BAD_WIFI_INITIAL_TIMEOUT_MS = 8 * 1000;
// Default to 30s linger time-out, and 5s for nascent network. Modifiable only for testing.
private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger";
@@ -783,7 +786,8 @@
final ConnectivityDiagnosticsHandler mConnectivityDiagnosticsHandler;
private final DnsManager mDnsManager;
- private final NetworkRanker mNetworkRanker;
+ @VisibleForTesting
+ final NetworkRanker mNetworkRanker;
private boolean mSystemReady;
private Intent mInitialBroadcast;
@@ -1417,7 +1421,6 @@
new RequestInfoPerUidCounter(MAX_NETWORK_REQUESTS_PER_SYSTEM_UID - 1);
mMetricsLog = logger;
- mNetworkRanker = new NetworkRanker();
final NetworkRequest defaultInternetRequest = createDefaultRequest();
mDefaultRequest = new NetworkRequestInfo(
Process.myUid(), defaultInternetRequest, null,
@@ -1538,6 +1541,9 @@
mMultinetworkPolicyTracker = mDeps.makeMultinetworkPolicyTracker(
mContext, mHandler, () -> updateAvoidBadWifi());
+ mNetworkRanker =
+ new NetworkRanker(new NetworkRanker.Configuration(activelyPreferBadWifi()));
+
mMultinetworkPolicyTracker.start();
mDnsManager = new DnsManager(mContext, mDnsResolver);
@@ -3742,17 +3748,6 @@
case EVENT_PROVISIONING_NOTIFICATION: {
final boolean visible = toBool(msg.arg1);
// If captive portal status has changed, update capabilities or disconnect.
- if (nai != null && (visible != nai.captivePortalDetected())) {
- nai.setCaptivePortalDetected(visible);
- if (visible && ConnectivitySettingsManager.CAPTIVE_PORTAL_MODE_AVOID
- == getCaptivePortalMode()) {
- if (DBG) log("Avoiding captive portal network: " + nai.toShortString());
- nai.onPreventAutomaticReconnect();
- teardownUnneededNetwork(nai);
- break;
- }
- updateCapabilitiesForNetwork(nai);
- }
if (!visible) {
// Only clear SIGN_IN and NETWORK_SWITCH notifications here, or else other
// notifications belong to the same network may be cleared unexpectedly.
@@ -3790,13 +3785,18 @@
@NonNull NetworkAgentInfo nai, int testResult, @NonNull String redirectUrl) {
final boolean valid = (testResult & NETWORK_VALIDATION_RESULT_VALID) != 0;
final boolean partial = (testResult & NETWORK_VALIDATION_RESULT_PARTIAL) != 0;
- final boolean captive = !TextUtils.isEmpty(redirectUrl);
+ final boolean portal = !TextUtils.isEmpty(redirectUrl);
// If there is any kind of working networking, then the NAI has been evaluated
// once. {@see NetworkAgentInfo#setEvaluated}, which returns whether this is
// the first time this ever happened.
- final boolean someConnectivity = (valid || partial || captive);
+ final boolean someConnectivity = (valid || partial || portal);
final boolean becameEvaluated = someConnectivity && nai.setEvaluated();
+ // Because of b/245893397, if the score is updated when updateCapabilities is called,
+ // any callback that receives onAvailable for that rematch receives an extra caps
+ // callback. To prevent that, update the score in the agent so the updates below won't
+ // see an update to both caps and score at the same time.
+ // TODO : fix b/245893397 and remove this.
if (becameEvaluated) nai.updateScoreForNetworkAgentUpdate();
if (!valid && shouldIgnoreValidationFailureAfterRoam(nai)) {
@@ -3809,8 +3809,12 @@
final boolean wasValidated = nai.isValidated();
final boolean wasPartial = nai.partialConnectivity();
- nai.setPartialConnectivity((testResult & NETWORK_VALIDATION_RESULT_PARTIAL) != 0);
- final boolean partialConnectivityChanged = (wasPartial != nai.partialConnectivity());
+ final boolean wasPortal = nai.captivePortalDetected();
+ nai.setPartialConnectivity(partial);
+ nai.setCaptivePortalDetected(portal);
+ nai.updateScoreForNetworkAgentUpdate();
+ final boolean partialConnectivityChanged = (wasPartial != partial);
+ final boolean portalChanged = (wasPortal != portal);
if (DBG) {
final String logMsg = !TextUtils.isEmpty(redirectUrl)
@@ -3818,7 +3822,7 @@
: "";
log(nai.toShortString() + " validation " + (valid ? "passed" : "failed") + logMsg);
}
- if (valid != nai.isValidated()) {
+ if (valid != wasValidated) {
final FullScore oldScore = nai.getScore();
nai.setValidated(valid);
updateCapabilities(oldScore, nai, nai.networkCapabilities);
@@ -3841,6 +3845,16 @@
}
} else if (partialConnectivityChanged) {
updateCapabilitiesForNetwork(nai);
+ } else if (portalChanged) {
+ if (portal && ConnectivitySettingsManager.CAPTIVE_PORTAL_MODE_AVOID
+ == getCaptivePortalMode()) {
+ if (DBG) log("Avoiding captive portal network: " + nai.toShortString());
+ nai.onPreventAutomaticReconnect();
+ teardownUnneededNetwork(nai);
+ return;
+ } else {
+ updateCapabilitiesForNetwork(nai);
+ }
} else if (becameEvaluated) {
// If valid or partial connectivity changed, updateCapabilities* has
// done the rematch.
@@ -5050,6 +5064,10 @@
return mMultinetworkPolicyTracker.getAvoidBadWifi();
}
+ private boolean activelyPreferBadWifi() {
+ return mMultinetworkPolicyTracker.getActivelyPreferBadWifi();
+ }
+
/**
* Return whether the device should maintain continuous, working connectivity by switching away
* from WiFi networks having no connectivity.
@@ -5065,14 +5083,21 @@
private void updateAvoidBadWifi() {
ensureRunningOnConnectivityServiceThread();
// Agent info scores and offer scores depend on whether cells yields to bad wifi.
+ final boolean avoidBadWifi = avoidBadWifi();
for (final NetworkAgentInfo nai : mNetworkAgentInfos) {
nai.updateScoreForNetworkAgentUpdate();
+ if (avoidBadWifi) {
+ // If the device is now avoiding bad wifi, remove notifications that might have
+ // been put up when the device didn't.
+ mNotifier.clearNotification(nai.network.getNetId(), NotificationType.LOST_INTERNET);
+ }
}
// UpdateOfferScore will update mNetworkOffers inline, so make a copy first.
final ArrayList<NetworkOfferInfo> offersToUpdate = new ArrayList<>(mNetworkOffers);
for (final NetworkOfferInfo noi : offersToUpdate) {
updateOfferScore(noi.offer);
}
+ mNetworkRanker.setConfiguration(new NetworkRanker.Configuration(activelyPreferBadWifi()));
rematchAllNetworksAndRequests();
}
@@ -5088,6 +5113,7 @@
pw.println("Bad Wi-Fi avoidance: " + avoidBadWifi());
pw.increaseIndent();
pw.println("Config restrict: " + configRestrict);
+ pw.println("Actively prefer: " + activelyPreferBadWifi());
final String value = mMultinetworkPolicyTracker.getAvoidBadWifiSetting();
String description;
@@ -5212,6 +5238,16 @@
// network was found not to have Internet access.
nai.updateScoreForNetworkAgentUpdate();
rematchAllNetworksAndRequests();
+
+ // Also, if this is WiFi and it should be preferred actively, now is the time to
+ // prompt the user that they walked past and connected to a bad WiFi.
+ if (nai.networkCapabilities.hasTransport(TRANSPORT_WIFI)
+ && !avoidBadWifi()
+ && activelyPreferBadWifi()) {
+ // The notification will be removed if the network validates or disconnects.
+ showNetworkNotification(nai, NotificationType.LOST_INTERNET);
+ return;
+ }
}
if (!shouldPromptUnvalidated(nai)) return;
@@ -9225,7 +9261,10 @@
networkAgent.networkMonitor().notifyNetworkConnected(params.linkProperties,
params.networkCapabilities);
}
- scheduleEvaluationTimeout(networkAgent.network, INITIAL_EVALUATION_TIMEOUT_MS);
+ final long delay = activelyPreferBadWifi()
+ ? ACTIVELY_PREFER_BAD_WIFI_INITIAL_TIMEOUT_MS
+ : DONT_ACTIVELY_PREFER_BAD_WIFI_INITIAL_TIMEOUT_MS;
+ scheduleEvaluationTimeout(networkAgent.network, delay);
// Whether a particular NetworkRequest listen should cause signal strength thresholds to
// be communicated to a particular NetworkAgent depends only on the network's immutable,
diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java
index e1c7b64..d7c3287 100644
--- a/service/src/com/android/server/connectivity/ClatCoordinator.java
+++ b/service/src/com/android/server/connectivity/ClatCoordinator.java
@@ -595,13 +595,17 @@
Log.i(TAG, "untag socket cookie " + cookie);
}
+ private boolean isStarted() {
+ return mClatdTracker != null;
+ }
+
/**
* Start clatd for a given interface and NAT64 prefix.
*/
public String clatStart(final String iface, final int netId,
@NonNull final IpPrefix nat64Prefix)
throws IOException {
- if (mClatdTracker != null) {
+ if (isStarted()) {
throw new IOException("Clatd is already running on " + mClatdTracker.iface
+ " (pid " + mClatdTracker.pid + ")");
}
@@ -833,7 +837,7 @@
* Stop clatd
*/
public void clatStop() throws IOException {
- if (mClatdTracker == null) {
+ if (!isStarted()) {
throw new IOException("Clatd has not started");
}
Log.i(TAG, "Stopping clatd pid=" + mClatdTracker.pid + " on " + mClatdTracker.iface);
@@ -902,12 +906,16 @@
public void dump(@NonNull IndentingPrintWriter pw) {
// TODO: move map dump to a global place to avoid duplicate dump while there are two or
// more IPv6 only networks.
- pw.println("CLAT tracker: " + mClatdTracker.toString());
- pw.println("Forwarding rules:");
- pw.increaseIndent();
- dumpBpfIngress(pw);
- dumpBpfEgress(pw);
- pw.decreaseIndent();
+ if (isStarted()) {
+ pw.println("CLAT tracker: " + mClatdTracker.toString());
+ pw.println("Forwarding rules:");
+ pw.increaseIndent();
+ dumpBpfIngress(pw);
+ dumpBpfEgress(pw);
+ pw.decreaseIndent();
+ } else {
+ pw.println("<not started>");
+ }
pw.println();
}
diff --git a/framework/src/android/net/util/MultinetworkPolicyTracker.java b/service/src/com/android/server/connectivity/MultinetworkPolicyTracker.java
similarity index 75%
rename from framework/src/android/net/util/MultinetworkPolicyTracker.java
rename to service/src/com/android/server/connectivity/MultinetworkPolicyTracker.java
index 2dcf932..a36bf4b 100644
--- a/framework/src/android/net/util/MultinetworkPolicyTracker.java
+++ b/service/src/com/android/server/connectivity/MultinetworkPolicyTracker.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package android.net.util;
+package com.android.server.connectivity;
import static android.net.ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI;
import static android.net.ConnectivitySettingsManager.NETWORK_METERED_MULTIPATH_PREFERENCE;
@@ -38,7 +38,9 @@
import android.telephony.TelephonyManager;
import android.util.Log;
+import com.android.connectivity.resources.R;
import com.android.internal.annotations.VisibleForTesting;
+import com.android.modules.utils.build.SdkLevel;
import java.util.Arrays;
import java.util.List;
@@ -79,6 +81,28 @@
private int mActiveSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
private volatile long mTestAllowBadWifiUntilMs = 0;
+ /**
+ * Whether to prefer bad wifi to a network that yields to bad wifis, even if it never validated
+ *
+ * This setting only makes sense if the system is configured not to avoid bad wifis, i.e.
+ * if mAvoidBadWifi is true. If it's not, then no network ever yields to bad wifis
+ * ({@see FullScore#POLICY_YIELD_TO_BAD_WIFI}) and this setting has therefore no effect.
+ *
+ * If this is false, when ranking a bad wifi that never validated against cell data (or any
+ * network that yields to bad wifis), the ranker will prefer cell data. It will prefer wifi
+ * if wifi loses validation later. This behavior avoids the device losing internet access when
+ * walking past a wifi network with no internet access.
+ * This is the default behavior up to Android T, but it can be overridden through an overlay
+ * to behave like below.
+ *
+ * If this is true, then in the same scenario, the ranker will prefer cell data until
+ * the wifi completes its first validation attempt (or the attempt times out after
+ * ConnectivityService#PROMPT_UNVALIDATED_DELAY_MS), then it will prefer the wifi even if it
+ * doesn't provide internet access, unless there is a captive portal on that wifi.
+ * This is the behavior in U and above.
+ */
+ private boolean mActivelyPreferBadWifi;
+
// Mainline module can't use internal HandlerExecutor, so add an identical executor here.
private static class HandlerExecutor implements Executor {
@NonNull
@@ -158,6 +182,10 @@
return mAvoidBadWifi;
}
+ public boolean getActivelyPreferBadWifi() {
+ return mActivelyPreferBadWifi;
+ }
+
// TODO: move this to MultipathPolicyTracker.
public int getMeteredMultipathPreference() {
return mMeteredMultipathPreference;
@@ -173,10 +201,30 @@
// NETWORK_AVOID_BAD_WIFI setting.
if (allowBadWifi) return true;
- // TODO: use R.integer.config_networkAvoidBadWifi directly
- final int id = mResources.get().getIdentifier("config_networkAvoidBadWifi",
- "integer", mResources.getResourcesContext().getPackageName());
- return (getResourcesForActiveSubId().getInteger(id) == 0);
+ return getResourcesForActiveSubId()
+ .getInteger(R.integer.config_networkAvoidBadWifi) == 0;
+ }
+
+ /**
+ * Whether the device config prefers bad wifi actively, when it doesn't avoid them
+ *
+ * This is only relevant when the device is configured not to avoid bad wifis. In this
+ * case, "actively" preferring a bad wifi means that the device will switch to a bad
+ * wifi it just connected to, as long as it's not a captive portal.
+ *
+ * On U and above this always returns true. On T and below it reads a configuration option.
+ */
+ public boolean configActivelyPrefersBadWifi() {
+ // See the definition of config_activelyPreferBadWifi for a description of its meaning.
+ // On U and above, the config is ignored, and bad wifi is always actively preferred.
+ if (SdkLevel.isAtLeastU()) return true;
+
+ // On T and below, 1 means to actively prefer bad wifi, 0 means not to prefer
+ // bad wifi (only stay stuck on it if already on there). This implementation treats
+ // any non-0 value like 1, on the assumption that anybody setting it non-zero wants
+ // the newer behavior.
+ return 0 != getResourcesForActiveSubId()
+ .getInteger(R.integer.config_activelyPreferBadWifi);
}
/**
@@ -224,9 +272,13 @@
public boolean updateAvoidBadWifi() {
final boolean settingAvoidBadWifi = "1".equals(getAvoidBadWifiSetting());
- final boolean prev = mAvoidBadWifi;
+ final boolean prevAvoid = mAvoidBadWifi;
mAvoidBadWifi = settingAvoidBadWifi || !configRestrictsAvoidBadWifi();
- return mAvoidBadWifi != prev;
+
+ final boolean prevActive = mActivelyPreferBadWifi;
+ mActivelyPreferBadWifi = configActivelyPrefersBadWifi();
+
+ return mAvoidBadWifi != prevAvoid || mActivelyPreferBadWifi != prevActive;
}
/**
diff --git a/service/src/com/android/server/connectivity/Nat464Xlat.java b/service/src/com/android/server/connectivity/Nat464Xlat.java
index a57e992..4e19781 100644
--- a/service/src/com/android/server/connectivity/Nat464Xlat.java
+++ b/service/src/com/android/server/connectivity/Nat464Xlat.java
@@ -540,6 +540,9 @@
*/
public void dump(IndentingPrintWriter pw) {
if (SdkLevel.isAtLeastT()) {
+ // Dump ClatCoordinator information while clatd has been started but not running. The
+ // reason is that it helps to have more information if clatd is started but the
+ // v4-* interface doesn't bring up. See #isStarted, #isRunning.
if (isStarted()) {
pw.println("ClatCoordinator:");
pw.increaseIndent();
diff --git a/service/src/com/android/server/connectivity/NetworkRanker.java b/service/src/com/android/server/connectivity/NetworkRanker.java
index f2c6aa1..d94c8dc 100644
--- a/service/src/com/android/server/connectivity/NetworkRanker.java
+++ b/service/src/com/android/server/connectivity/NetworkRanker.java
@@ -16,6 +16,7 @@
package com.android.server.connectivity;
+import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL;
import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
@@ -27,6 +28,7 @@
import static com.android.net.module.util.CollectionUtils.filter;
import static com.android.server.connectivity.FullScore.POLICY_ACCEPT_UNVALIDATED;
import static com.android.server.connectivity.FullScore.POLICY_AVOIDED_WHEN_UNVALIDATED;
+import static com.android.server.connectivity.FullScore.POLICY_EVER_EVALUATED;
import static com.android.server.connectivity.FullScore.POLICY_EVER_USER_SELECTED;
import static com.android.server.connectivity.FullScore.POLICY_EVER_VALIDATED;
import static com.android.server.connectivity.FullScore.POLICY_IS_DESTROYED;
@@ -39,18 +41,39 @@
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
+import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import java.util.Objects;
import java.util.function.Predicate;
/**
* A class that knows how to find the best network matching a request out of a list of networks.
*/
public class NetworkRanker {
+ /**
+ * Home for all configurations of NetworkRanker
+ */
+ public static final class Configuration {
+ private final boolean mActivelyPreferBadWifi;
+
+ public Configuration(final boolean activelyPreferBadWifi) {
+ this.mActivelyPreferBadWifi = activelyPreferBadWifi;
+ }
+
+ /**
+ * @see MultinetworkPolicyTracker#getActivelyPreferBadWifi()
+ */
+ public boolean activelyPreferBadWifi() {
+ return mActivelyPreferBadWifi;
+ }
+ }
+ @NonNull private volatile Configuration mConf;
+
// Historically the legacy ints have been 0~100 in principle (though the highest score in
// AOSP has always been 90). This is relied on by VPNs that send a legacy score of 101.
public static final int LEGACY_INT_MAX = 100;
@@ -65,7 +88,22 @@
NetworkCapabilities getCapsNoCopy();
}
- public NetworkRanker() { }
+ public NetworkRanker(@NonNull final Configuration conf) {
+ // Because mConf is volatile, the only way it could be seen null would be an access to it
+ // on some other thread during this constructor. But this is not possible because mConf is
+ // private and `this` doesn't escape this constructor.
+ setConfiguration(conf);
+ }
+
+ public void setConfiguration(@NonNull final Configuration conf) {
+ mConf = Objects.requireNonNull(conf);
+ }
+
+ // There shouldn't be a use case outside of testing
+ @VisibleForTesting
+ public Configuration getConfiguration() {
+ return mConf;
+ }
/**
* Find the best network satisfying this request among the list of passed networks.
@@ -104,11 +142,42 @@
}
}
- private <T extends Scoreable> boolean isBadWiFi(@NonNull final T candidate) {
+ /**
+ * Returns whether the wifi passed as an argument is a preferred network to yielding cell.
+ *
+ * When comparing bad wifi to cell with POLICY_YIELD_TO_BAD_WIFI, it may be necessary to
+ * know if a particular bad wifi is preferred to such a cell network. This method computes
+ * and returns this.
+ *
+ * @param candidate a bad wifi to evaluate
+ * @return whether this candidate is preferred to cell with POLICY_YIELD_TO_BAD_WIFI
+ */
+ private <T extends Scoreable> boolean isPreferredBadWiFi(@NonNull final T candidate) {
final FullScore score = candidate.getScore();
- return score.hasPolicy(POLICY_EVER_VALIDATED)
- && !score.hasPolicy(POLICY_AVOIDED_WHEN_UNVALIDATED)
- && candidate.getCapsNoCopy().hasTransport(TRANSPORT_WIFI);
+ final NetworkCapabilities caps = candidate.getCapsNoCopy();
+
+ // Whatever the policy, only WiFis can be preferred bad WiFis.
+ if (!caps.hasTransport(TRANSPORT_WIFI)) return false;
+ // Validated networks aren't bad networks, so a fortiori can't be preferred bad WiFis.
+ if (score.hasPolicy(POLICY_IS_VALIDATED)) return false;
+ // A WiFi that the user explicitly wanted to avoid in UI is never a preferred bad WiFi.
+ if (score.hasPolicy(POLICY_AVOIDED_WHEN_UNVALIDATED)) return false;
+
+ if (mConf.activelyPreferBadWifi()) {
+ // If a network is still evaluating, don't prefer it.
+ if (!score.hasPolicy(POLICY_EVER_EVALUATED)) return false;
+
+ // If a network is not a captive portal, then prefer it.
+ if (!caps.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return true;
+
+ // If it's a captive portal, prefer it if it previously validated but is no longer
+ // validated (i.e., the user logged in in the past, but later the portal closed).
+ return score.hasPolicy(POLICY_EVER_VALIDATED);
+ } else {
+ // Under the original "prefer bad WiFi" policy, only networks that have ever validated
+ // are preferred.
+ return score.hasPolicy(POLICY_EVER_VALIDATED);
+ }
}
/**
@@ -131,7 +200,7 @@
// No network with the policy : do nothing.
return;
}
- if (!CollectionUtils.any(rejected, n -> isBadWiFi(n))) {
+ if (!CollectionUtils.any(rejected, n -> isPreferredBadWiFi(n))) {
// No bad WiFi : do nothing.
return;
}
@@ -141,7 +210,7 @@
// wifis by the following policies (e.g. exiting).
final ArrayList<T> acceptedYielders = new ArrayList<>(accepted);
final ArrayList<T> rejectedWithBadWiFis = new ArrayList<>(rejected);
- partitionInto(rejectedWithBadWiFis, n -> isBadWiFi(n), accepted, rejected);
+ partitionInto(rejectedWithBadWiFis, n -> isPreferredBadWiFi(n), accepted, rejected);
accepted.addAll(acceptedYielders);
return;
}
diff --git a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
index f1a6d92..74e57c9 100644
--- a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
+++ b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
@@ -41,6 +41,7 @@
import android.net.MacAddress
import android.net.Network
import android.net.NetworkCapabilities
+import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED
import android.net.NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED
import android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED
import android.net.NetworkCapabilities.TRANSPORT_ETHERNET
@@ -56,6 +57,7 @@
import android.os.Looper
import android.os.OutcomeReceiver
import android.os.SystemProperties
+import android.os.Process
import android.platform.test.annotations.AppModeFull
import android.util.ArraySet
import androidx.test.platform.app.InstrumentationRegistry
@@ -72,17 +74,19 @@
import com.android.testutils.TapPacketReader
import com.android.testutils.TestableNetworkCallback
import com.android.testutils.anyNetwork
+import com.android.testutils.assertThrows
import com.android.testutils.runAsShell
import com.android.testutils.waitForIdle
import org.junit.After
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeTrue
import org.junit.Before
-import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
+import java.io.IOException
import java.net.Inet6Address
import java.util.Random
+import java.net.Socket
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
@@ -503,7 +507,7 @@
// NetworkRequest.Builder does not create a copy of the passed NetworkRequest, so in order to
// keep ETH_REQUEST as it is, a defensive copy is created here.
- private fun NetworkRequest.createCopyWithEthernetSpecifier(ifaceName: String) =
+ private fun NetworkRequest.copyWithEthernetSpecifier(ifaceName: String) =
NetworkRequest.Builder(NetworkRequest(ETH_REQUEST))
.setNetworkSpecifier(EthernetNetworkSpecifier(ifaceName)).build()
@@ -717,7 +721,7 @@
val iface1 = createInterface()
val iface2 = createInterface()
- val cb = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface2.name))
+ val cb = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface2.name))
val network = cb.expectAvailable()
cb.expectCapabilitiesWithInterfaceName(iface2.name)
@@ -750,8 +754,8 @@
val iface1 = createInterface()
val iface2 = createInterface()
- val cb1 = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface1.name))
- val cb2 = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface2.name))
+ val cb1 = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface1.name))
+ val cb2 = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface2.name))
val cb3 = requestNetwork(ETH_REQUEST)
cb1.expectAvailable()
@@ -862,7 +866,7 @@
@Test
fun testUpdateConfiguration_forBothIpConfigAndCapabilities() {
val iface = createInterface()
- val cb = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface.name))
+ val cb = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface.name))
val network = cb.expectAvailable()
cb.assertNeverLost()
@@ -885,7 +889,7 @@
@Test
fun testUpdateConfiguration_forOnlyIpConfig() {
val iface = createInterface()
- val cb = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface.name))
+ val cb = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface.name))
val network = cb.expectAvailable()
cb.assertNeverLost()
@@ -900,13 +904,10 @@
STATIC_IP_CONFIGURATION.staticIpConfiguration.ipAddress!!)
}
- // TODO(b/240323229): This test is currently flaky due to a race between IpClient restarting and
- // NetworkAgent tearing down the routes. This problem is exacerbated by disabling RS delay.
- @Ignore
@Test
fun testUpdateConfiguration_forOnlyCapabilities() {
val iface = createInterface()
- val cb = requestNetwork(ETH_REQUEST.createCopyWithEthernetSpecifier(iface.name))
+ val cb = requestNetwork(ETH_REQUEST.copyWithEthernetSpecifier(iface.name))
val network = cb.expectAvailable()
cb.assertNeverLost()
@@ -923,4 +924,37 @@
cb.expectAvailable()
cb.expectCapabilitiesWith(testCapability)
}
+
+ @Test
+ fun testUpdateConfiguration_forAllowedUids() {
+ // Configure a restricted network.
+ val iface = createInterface()
+ val request = NetworkRequest.Builder(ETH_REQUEST.copyWithEthernetSpecifier(iface.name))
+ .removeCapability(NET_CAPABILITY_NOT_RESTRICTED).build()
+ updateConfiguration(iface, capabilities = request.networkCapabilities)
+ .expectResult(iface.name)
+
+ // Request the restricted network as the shell with CONNECTIVITY_USE_RESTRICTED_NETWORKS.
+ val cb = runAsShell(CONNECTIVITY_USE_RESTRICTED_NETWORKS) { requestNetwork(request) }
+ val network = cb.expectAvailable()
+ cb.assertNeverLost(network)
+
+ // The network is restricted therefore binding to it when available will fail.
+ Socket().use { socket ->
+ assertThrows(IOException::class.java, { network.bindSocket(socket) })
+ }
+
+ // Add the test process UID to the allowed UIDs for the network and ultimately bind again.
+ val allowedUids = setOf(Process.myUid())
+ val nc = NetworkCapabilities.Builder(request.networkCapabilities)
+ .setAllowedUids(allowedUids).build()
+ updateConfiguration(iface, capabilities = nc).expectResult(iface.name)
+
+ // UpdateConfiguration() currently does a restart on the ethernet interface therefore lost
+ // will be expected first before available, as part of the restart.
+ cb.expectLost(network)
+ val updatedNetwork = cb.expectAvailable()
+ // With the test process UID allowed, binding to a restricted network should be successful.
+ Socket().use { socket -> updatedNetwork.bindSocket(socket) }
+ }
}
diff --git a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt
index d4f3d57..d2cd7aa 100644
--- a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt
+++ b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt
@@ -73,6 +73,8 @@
import android.os.Message
import android.os.SystemClock
import android.platform.test.annotations.AppModeFull
+import android.system.OsConstants.IPPROTO_TCP
+import android.system.OsConstants.IPPROTO_UDP
import android.telephony.TelephonyManager
import android.telephony.data.EpsBearerQosSessionAttributes
import android.util.DebugUtils.valueToString
@@ -117,6 +119,7 @@
import org.mockito.Mockito.mock
import org.mockito.Mockito.timeout
import org.mockito.Mockito.verify
+import java.io.Closeable
import java.io.IOException
import java.net.DatagramSocket
import java.net.InetAddress
@@ -174,7 +177,7 @@
private val mFakeConnectivityService = FakeConnectivityService()
private val agentsToCleanUp = mutableListOf<NetworkAgent>()
private val callbacksToCleanUp = mutableListOf<TestableNetworkCallback>()
- private var qosTestSocket: Socket? = null
+ private var qosTestSocket: Closeable? = null // either Socket or DatagramSocket
@Before
fun setUp() {
@@ -930,34 +933,49 @@
}
}
- private fun setupForQosCallbackTesting(): Pair<TestableNetworkAgent, Socket> {
- val request = NetworkRequest.Builder()
- .clearCapabilities()
- .addTransportType(TRANSPORT_TEST)
- .build()
+ private fun <T : Closeable> setupForQosCallbackTest(creator: (TestableNetworkAgent) -> T) =
+ createConnectedNetworkAgent().first.let { Pair(it, creator(it)) }
- val callback = TestableNetworkCallback(timeoutMs = DEFAULT_TIMEOUT_MS)
- requestNetwork(request, callback)
- val (agent, _) = createConnectedNetworkAgent()
+ private fun setupForQosSocket() = setupForQosCallbackTest {
+ agent: TestableNetworkAgent -> Socket()
+ .also { assertNotNull(agent.network?.bindSocket(it))
+ it.bind(InetSocketAddress(InetAddress.getLoopbackAddress(), 0)) }
+ }
- qosTestSocket = assertNotNull(agent.network?.socketFactory?.createSocket()).also {
- it.bind(InetSocketAddress(InetAddress.getLoopbackAddress(), 0))
- }
- return Pair(agent, qosTestSocket!!)
+ private fun setupForQosDatagram() = setupForQosCallbackTest {
+ agent: TestableNetworkAgent -> DatagramSocket(
+ InetSocketAddress(InetAddress.getLoopbackAddress(), 0))
+ .also { assertNotNull(agent.network?.bindSocket(it)) }
}
@AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
@Test
- fun testQosCallbackRegisterWithUnregister() {
- val (agent, socket) = setupForQosCallbackTesting()
+ fun testQosCallbackRegisterAndUnregister() {
+ validateQosCallbackRegisterAndUnregister(IPPROTO_TCP)
+ }
+ @AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
+ @Test
+ fun testQosCallbackRegisterAndUnregisterWithDatagramSocket() {
+ validateQosCallbackRegisterAndUnregister(IPPROTO_UDP)
+ }
+
+ private fun validateQosCallbackRegisterAndUnregister(proto: Int) {
+ val (agent, qosTestSocket) = when (proto) {
+ IPPROTO_TCP -> setupForQosSocket()
+ IPPROTO_UDP -> setupForQosDatagram()
+ else -> fail("unsupported protocol")
+ }
val qosCallback = TestableQosCallback()
var callbackId = -1
Executors.newSingleThreadExecutor().let { executor ->
try {
- val info = QosSocketInfo(agent.network!!, socket)
+ val info = QosSocketInfo(agent, qosTestSocket)
mCM.registerQosCallback(info, executor, qosCallback)
- callbackId = agent.expectCallback<OnRegisterQosCallback>().callbackId
+ agent.expectCallback<OnRegisterQosCallback>().let {
+ callbackId = it.callbackId
+ assertTrue(it.filter.matchesProtocol(proto))
+ }
assertFailsWith<QosCallbackRegistrationException>(
"The same callback cannot be " +
@@ -965,7 +983,7 @@
mCM.registerQosCallback(info, executor, qosCallback)
}
} finally {
- socket.close()
+ qosTestSocket.close()
mCM.unregisterQosCallback(qosCallback)
agent.expectCallback<OnUnregisterQosCallback> { it.callbackId == callbackId }
executor.shutdown()
@@ -976,11 +994,31 @@
@AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
@Test
fun testQosCallbackOnQosSession() {
- val (agent, socket) = setupForQosCallbackTesting()
+ validateQosCallbackOnQosSession(IPPROTO_TCP)
+ }
+
+ @AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
+ @Test
+ fun testQosCallbackOnQosSessionWithDatagramSocket() {
+ validateQosCallbackOnQosSession(IPPROTO_UDP)
+ }
+
+ fun QosSocketInfo(agent: NetworkAgent, socket: Closeable) = when (socket) {
+ is Socket -> QosSocketInfo(agent.network, socket)
+ is DatagramSocket -> QosSocketInfo(agent.network, socket)
+ else -> fail("unexpected socket type")
+ }
+
+ private fun validateQosCallbackOnQosSession(proto: Int) {
+ val (agent, qosTestSocket) = when (proto) {
+ IPPROTO_TCP -> setupForQosSocket()
+ IPPROTO_UDP -> setupForQosDatagram()
+ else -> fail("unsupported protocol")
+ }
val qosCallback = TestableQosCallback()
Executors.newSingleThreadExecutor().let { executor ->
try {
- val info = QosSocketInfo(agent.network!!, socket)
+ val info = QosSocketInfo(agent, qosTestSocket)
assertEquals(agent.network, info.getNetwork())
mCM.registerQosCallback(info, executor, qosCallback)
val callbackId = agent.expectCallback<OnRegisterQosCallback>().callbackId
@@ -1009,8 +1047,7 @@
agent.sendQosSessionLost(callbackId, sessId, QosSession.TYPE_EPS_BEARER)
qosCallback.assertNoCallback()
} finally {
- socket.close()
-
+ qosTestSocket.close()
// safety precaution
mCM.unregisterQosCallback(qosCallback)
@@ -1022,11 +1059,11 @@
@AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
@Test
fun testQosCallbackOnError() {
- val (agent, socket) = setupForQosCallbackTesting()
+ val (agent, qosTestSocket) = setupForQosSocket()
val qosCallback = TestableQosCallback()
Executors.newSingleThreadExecutor().let { executor ->
try {
- val info = QosSocketInfo(agent.network!!, socket)
+ val info = QosSocketInfo(agent.network!!, qosTestSocket)
mCM.registerQosCallback(info, executor, qosCallback)
val callbackId = agent.expectCallback<OnRegisterQosCallback>().callbackId
@@ -1048,7 +1085,7 @@
agent.sendQosSessionLost(callbackId, sessId, QosSession.TYPE_EPS_BEARER)
qosCallback.assertNoCallback()
} finally {
- socket.close()
+ qosTestSocket.close()
// Make sure that the callback is fully unregistered
mCM.unregisterQosCallback(qosCallback)
@@ -1061,12 +1098,12 @@
@AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
@Test
fun testQosCallbackIdsAreMappedCorrectly() {
- val (agent, socket) = setupForQosCallbackTesting()
+ val (agent, qosTestSocket) = setupForQosSocket()
val qosCallback1 = TestableQosCallback()
val qosCallback2 = TestableQosCallback()
Executors.newSingleThreadExecutor().let { executor ->
try {
- val info = QosSocketInfo(agent.network!!, socket)
+ val info = QosSocketInfo(agent.network!!, qosTestSocket)
mCM.registerQosCallback(info, executor, qosCallback1)
val callbackId1 = agent.expectCallback<OnRegisterQosCallback>().callbackId
@@ -1088,7 +1125,7 @@
qosCallback1.assertNoCallback()
qosCallback2.expectCallback<OnQosSessionAvailable> { sessId2 == it.sess.sessionId }
} finally {
- socket.close()
+ qosTestSocket.close()
// Make sure that the callback is fully unregistered
mCM.unregisterQosCallback(qosCallback1)
@@ -1102,13 +1139,13 @@
@AppModeFull(reason = "Instant apps don't have permission to bind sockets.")
@Test
fun testQosCallbackWhenNetworkReleased() {
- val (agent, socket) = setupForQosCallbackTesting()
+ val (agent, qosTestSocket) = setupForQosSocket()
Executors.newSingleThreadExecutor().let { executor ->
try {
val qosCallback1 = TestableQosCallback()
val qosCallback2 = TestableQosCallback()
try {
- val info = QosSocketInfo(agent.network!!, socket)
+ val info = QosSocketInfo(agent.network!!, qosTestSocket)
mCM.registerQosCallback(info, executor, qosCallback1)
mCM.registerQosCallback(info, executor, qosCallback2)
agent.unregister()
@@ -1121,12 +1158,12 @@
it.ex.cause is NetworkReleasedException
}
} finally {
- socket.close()
+ qosTestSocket.close()
mCM.unregisterQosCallback(qosCallback1)
mCM.unregisterQosCallback(qosCallback2)
}
} finally {
- socket.close()
+ qosTestSocket.close()
executor.shutdown()
}
}
diff --git a/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java b/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java
index cd43a34..ffb34e6 100644
--- a/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java
+++ b/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java
@@ -17,6 +17,7 @@
package android.net.cts;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -24,6 +25,8 @@
import android.net.QosCallbackException;
import android.net.SocketLocalAddressChangedException;
import android.net.SocketNotBoundException;
+import android.net.SocketNotConnectedException;
+import android.net.SocketRemoteAddressChangedException;
import android.os.Build;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
@@ -36,12 +39,6 @@
@IgnoreUpTo(Build.VERSION_CODES.R)
public class QosCallbackExceptionTest {
private static final String ERROR_MESSAGE = "Test Error Message";
- private static final String ERROR_MSG_SOCK_NOT_BOUND = "The socket is unbound";
- private static final String ERROR_MSG_NET_RELEASED =
- "The network was released and is no longer available";
- private static final String ERROR_MSG_SOCK_ADDR_CHANGED =
- "The local address of the socket changed";
-
@Test
public void testQosCallbackException() throws Exception {
@@ -57,33 +54,65 @@
public void testNetworkReleasedExceptions() throws Exception {
final Throwable netReleasedException = new NetworkReleasedException();
final QosCallbackException exception = new QosCallbackException(netReleasedException);
-
- assertTrue(exception.getCause() instanceof NetworkReleasedException);
- assertEquals(netReleasedException, exception.getCause());
- assertTrue(exception.getMessage().contains(ERROR_MSG_NET_RELEASED));
- assertThrowableMessageContains(exception, ERROR_MSG_NET_RELEASED);
+ validateQosCallbackException(
+ exception, netReleasedException, NetworkReleasedException.class);
}
@Test
public void testSocketNotBoundExceptions() throws Exception {
final Throwable sockNotBoundException = new SocketNotBoundException();
final QosCallbackException exception = new QosCallbackException(sockNotBoundException);
-
- assertTrue(exception.getCause() instanceof SocketNotBoundException);
- assertEquals(sockNotBoundException, exception.getCause());
- assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_NOT_BOUND));
- assertThrowableMessageContains(exception, ERROR_MSG_SOCK_NOT_BOUND);
+ validateQosCallbackException(
+ exception, sockNotBoundException, SocketNotBoundException.class);
}
@Test
public void testSocketLocalAddressChangedExceptions() throws Exception {
- final Throwable localAddrChangedException = new SocketLocalAddressChangedException();
- final QosCallbackException exception = new QosCallbackException(localAddrChangedException);
+ final Throwable localAddressChangedException = new SocketLocalAddressChangedException();
+ final QosCallbackException exception =
+ new QosCallbackException(localAddressChangedException);
+ validateQosCallbackException(
+ exception, localAddressChangedException, SocketLocalAddressChangedException.class);
+ }
- assertTrue(exception.getCause() instanceof SocketLocalAddressChangedException);
- assertEquals(localAddrChangedException, exception.getCause());
- assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_ADDR_CHANGED));
- assertThrowableMessageContains(exception, ERROR_MSG_SOCK_ADDR_CHANGED);
+ @Test
+ @IgnoreUpTo(Build.VERSION_CODES.S)
+ public void testSocketNotConnectedExceptions() throws Exception {
+ final Throwable sockNotConnectedException = new SocketNotConnectedException();
+ final QosCallbackException exception = new QosCallbackException(sockNotConnectedException);
+ validateQosCallbackException(
+ exception, sockNotConnectedException, SocketNotConnectedException.class);
+ }
+
+ @Test
+ @IgnoreUpTo(Build.VERSION_CODES.S)
+ public void testSocketRemoteAddressChangedExceptions() throws Exception {
+ final Throwable remoteAddressChangedException = new SocketRemoteAddressChangedException();
+ final QosCallbackException exception =
+ new QosCallbackException(remoteAddressChangedException);
+ validateQosCallbackException(
+ exception, remoteAddressChangedException,
+ SocketRemoteAddressChangedException.class);
+ }
+
+ private void validateQosCallbackException(
+ QosCallbackException e, Throwable cause, Class c) throws Exception {
+ if (c == SocketNotConnectedException.class) {
+ assertTrue(e.getCause() instanceof SocketNotConnectedException);
+ } else if (c == SocketRemoteAddressChangedException.class) {
+ assertTrue(e.getCause() instanceof SocketRemoteAddressChangedException);
+ } else if (c == SocketLocalAddressChangedException.class) {
+ assertTrue(e.getCause() instanceof SocketLocalAddressChangedException);
+ } else if (c == SocketNotBoundException.class) {
+ assertTrue(e.getCause() instanceof SocketNotBoundException);
+ } else if (c == NetworkReleasedException.class) {
+ assertTrue(e.getCause() instanceof NetworkReleasedException);
+ } else {
+ fail("unexpected error msg.");
+ }
+ assertEquals(cause, e.getCause());
+ assertFalse(e.getMessage().isEmpty());
+ assertThrowableMessageContains(e, e.getMessage());
}
private void assertThrowableMessageContains(QosCallbackException exception, String errorMsg)
diff --git a/tests/integration/src/com/android/server/net/integrationtests/ConnectivityServiceIntegrationTest.kt b/tests/integration/src/com/android/server/net/integrationtests/ConnectivityServiceIntegrationTest.kt
index 73e4c0e..5b3f37d 100644
--- a/tests/integration/src/com/android/server/net/integrationtests/ConnectivityServiceIntegrationTest.kt
+++ b/tests/integration/src/com/android/server/net/integrationtests/ConnectivityServiceIntegrationTest.kt
@@ -37,7 +37,7 @@
import android.net.TestNetworkStackClient
import android.net.Uri
import android.net.metrics.IpConnectivityLog
-import android.net.util.MultinetworkPolicyTracker
+import com.android.server.connectivity.MultinetworkPolicyTracker
import android.os.ConditionVariable
import android.os.IBinder
import android.os.SystemConfigManager
diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
index d2a7135..e1208ef 100755
--- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
@@ -29,6 +29,7 @@
import static android.Manifest.permission.NETWORK_SETTINGS;
import static android.Manifest.permission.NETWORK_STACK;
import static android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD;
+import static android.Manifest.permission.READ_DEVICE_CONFIG;
import static android.app.PendingIntent.FLAG_IMMUTABLE;
import static android.content.Intent.ACTION_PACKAGE_ADDED;
import static android.content.Intent.ACTION_PACKAGE_REMOVED;
@@ -302,7 +303,6 @@
import android.net.resolv.aidl.Nat64PrefixEventParcel;
import android.net.resolv.aidl.PrivateDnsValidationEventParcel;
import android.net.shared.PrivateDnsConfig;
-import android.net.util.MultinetworkPolicyTracker;
import android.net.wifi.WifiInfo;
import android.os.BadParcelableException;
import android.os.BatteryStatsManager;
@@ -357,14 +357,15 @@
import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.LocationPermissionChecker;
import com.android.net.module.util.NetworkMonitorUtils;
+import com.android.networkstack.apishim.ConstantsShim;
import com.android.networkstack.apishim.NetworkAgentConfigShimImpl;
-import com.android.networkstack.apishim.api29.ConstantsShim;
import com.android.server.ConnectivityService.ConnectivityDiagnosticsCallbackInfo;
import com.android.server.ConnectivityService.NetworkRequestInfo;
import com.android.server.ConnectivityServiceTest.ConnectivityServiceDependencies.ReportedInterfaces;
import com.android.server.connectivity.CarrierPrivilegeAuthenticator;
import com.android.server.connectivity.ClatCoordinator;
import com.android.server.connectivity.ConnectivityFlags;
+import com.android.server.connectivity.MultinetworkPolicyTracker;
import com.android.server.connectivity.Nat464Xlat;
import com.android.server.connectivity.NetworkAgentInfo;
import com.android.server.connectivity.NetworkNotificationManager;
@@ -1169,10 +1170,11 @@
void setNetworkPartialValid(boolean isStrictMode) {
setNetworkPartial();
mNmValidationResult |= NETWORK_VALIDATION_RESULT_VALID;
+ mNmValidationRedirectUrl = null;
int probesCompleted = NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTPS
| NETWORK_VALIDATION_PROBE_HTTP;
int probesSucceeded = NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP;
- // Suppose the partial network cannot pass the private DNS validation as well, so only
+ // Assume the partial network cannot pass the private DNS validation as well, so only
// add NETWORK_VALIDATION_PROBE_DNS in probesCompleted but not probesSucceeded.
if (isStrictMode) {
probesCompleted |= NETWORK_VALIDATION_PROBE_PRIVDNS;
@@ -1836,9 +1838,8 @@
.getIdentifier(eq("config_networkSupportedKeepaliveCount"), eq("array"), any());
doReturn(R.array.network_switch_type_name).when(mResources)
.getIdentifier(eq("network_switch_type_name"), eq("array"), any());
- doReturn(R.integer.config_networkAvoidBadWifi).when(mResources)
- .getIdentifier(eq("config_networkAvoidBadWifi"), eq("integer"), any());
doReturn(1).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
+ doReturn(0).when(mResources).getInteger(R.integer.config_activelyPreferBadWifi);
doReturn(true).when(mResources)
.getBoolean(R.bool.config_cellular_radio_timesharing_capable);
}
@@ -3526,12 +3527,16 @@
/** Expects the specified notification and returns the notification ID. */
private int expectNotification(TestNetworkAgentWrapper agent, NotificationType type) {
- verify(mNotificationManager).notify(
+ verify(mNotificationManager, timeout(TIMEOUT_MS)).notify(
eq(NetworkNotificationManager.tagFor(agent.getNetwork().netId)),
eq(type.eventId), any());
return type.eventId;
}
+ private void expectNoNotification(@NonNull final TestNetworkAgentWrapper agent) {
+ verify(mNotificationManager, never()).notifyAsUser(anyString(), anyInt(), any(), any());
+ }
+
/**
* Expects the specified notification happens when the unvalidated prompt message arrives
*
@@ -3550,9 +3555,9 @@
* This generally happens when the network disconnects or when the newtwork validates. During
* normal usage the notification is also cleared by the system when the notification is tapped.
*/
- private void expectClearNotification(TestNetworkAgentWrapper agent, int expectedId) {
- verify(mNotificationManager).cancel(
- eq(NetworkNotificationManager.tagFor(agent.getNetwork().netId)), eq(expectedId));
+ private void expectClearNotification(TestNetworkAgentWrapper agent, NotificationType type) {
+ verify(mNotificationManager, timeout(TIMEOUT_MS)).cancel(
+ eq(NetworkNotificationManager.tagFor(agent.getNetwork().netId)), eq(type.eventId));
}
/**
@@ -3563,13 +3568,13 @@
private void expectUnvalidationCheckWillNotNotify(TestNetworkAgentWrapper agent) {
mService.scheduleEvaluationTimeout(agent.getNetwork(), 0 /*delayMs */);
waitForIdle();
- verify(mNotificationManager, never()).notifyAsUser(anyString(), anyInt(), any(), any());
+ expectNoNotification(agent);
}
private void expectDisconnectAndClearNotifications(TestNetworkCallback callback,
- TestNetworkAgentWrapper agent, int id) {
+ TestNetworkAgentWrapper agent, NotificationType type) {
callback.expectCallback(CallbackEntry.LOST, agent);
- expectClearNotification(agent, id);
+ expectClearNotification(agent, type);
}
private NativeNetworkConfig nativeNetworkConfigPhysical(int netId, int permission) {
@@ -3713,7 +3718,7 @@
// Disconnect wifi, and then reconnect, again with explicitlySelected=true.
mWiFiNetworkAgent.disconnect();
expectDisconnectAndClearNotifications(callback, mWiFiNetworkAgent,
- NotificationType.NO_INTERNET.eventId);
+ NotificationType.NO_INTERNET);
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(true, false);
@@ -3727,7 +3732,7 @@
// network to disconnect.
mCm.setAcceptUnvalidated(mWiFiNetworkAgent.getNetwork(), false, false);
expectDisconnectAndClearNotifications(callback, mWiFiNetworkAgent,
- NotificationType.NO_INTERNET.eventId);
+ NotificationType.NO_INTERNET);
reset(mNotificationManager);
// Reconnect, again with explicitlySelected=true, but this time validate.
@@ -4227,7 +4232,7 @@
assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
// Once the network validates, the notification disappears.
- expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY.eventId);
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
// Disconnect and reconnect wifi with partial connectivity again.
mWiFiNetworkAgent.disconnect();
@@ -4250,7 +4255,7 @@
mCm.setAcceptPartialConnectivity(mWiFiNetworkAgent.getNetwork(), false /* accept */,
false /* always */);
callback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
- expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY.eventId);
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
reset(mNotificationManager);
// If the user accepted partial connectivity before, and the device connects to that network
@@ -4326,10 +4331,11 @@
@Test
public void testCaptivePortalOnPartialConnectivity() throws Exception {
- final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
- final NetworkRequest captivePortalRequest = new NetworkRequest.Builder()
- .addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
- mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
+ final TestNetworkCallback wifiCallback = new TestNetworkCallback();
+ final NetworkRequest wifiRequest = new NetworkRequest.Builder()
+ .addTransportType(TRANSPORT_WIFI)
+ .build();
+ mCm.registerNetworkCallback(wifiRequest, wifiCallback);
final TestNetworkCallback validatedCallback = new TestNetworkCallback();
final NetworkRequest validatedRequest = new NetworkRequest.Builder()
@@ -4341,21 +4347,28 @@
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
String redirectUrl = "http://android.com/path";
mWiFiNetworkAgent.connectWithCaptivePortal(redirectUrl, false /* isStrictMode */);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), redirectUrl);
+ // This is necessary because of b/245893397, the same bug that happens where we use
+ // expectAvailableDoubleValidatedCallbacks.
+ // TODO : fix b/245893397 and remove this.
+ wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_CAPTIVE_PORTAL, mWiFiNetworkAgent);
+
// Check that startCaptivePortalApp sends the expected command to NetworkMonitor.
mCm.startCaptivePortalApp(mWiFiNetworkAgent.getNetwork());
verify(mWiFiNetworkAgent.mNetworkMonitor, timeout(TIMEOUT_MS).times(1))
.launchCaptivePortalApp();
// Report that the captive portal is dismissed with partial connectivity, and check that
- // callbacks are fired.
+ // callbacks are fired with PARTIAL and without CAPTIVE_PORTAL.
mWiFiNetworkAgent.setNetworkPartial();
mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
waitForIdle();
- captivePortalCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
- mWiFiNetworkAgent);
+ wifiCallback.expectCapabilitiesThat(
+ mWiFiNetworkAgent, nc ->
+ nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY)
+ && !nc.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
// Report partial connectivity is accepted.
mWiFiNetworkAgent.setNetworkPartialValid(false /* isStrictMode */);
@@ -4363,13 +4376,12 @@
false /* always */);
waitForIdle();
mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- captivePortalCallback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
+ wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
validatedCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- NetworkCapabilities nc =
- validatedCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
+ validatedCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
mWiFiNetworkAgent);
- mCm.unregisterNetworkCallback(captivePortalCallback);
+ mCm.unregisterNetworkCallback(wifiCallback);
mCm.unregisterNetworkCallback(validatedCallback);
}
@@ -4452,6 +4464,11 @@
mCm.reportNetworkConnectivity(wifiNetwork, false);
captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
validatedCallback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
+ // This is necessary because of b/245893397, the same bug that happens where we use
+ // expectAvailableDoubleValidatedCallbacks.
+ // TODO : fix b/245893397 and remove this.
+ captivePortalCallback.expectCallback(CallbackEntry.NETWORK_CAPS_UPDATED,
+ mWiFiNetworkAgent);
// Check that startCaptivePortalApp sends the expected command to NetworkMonitor.
mCm.startCaptivePortalApp(wifiNetwork);
@@ -5621,6 +5638,24 @@
}
@Test
+ public void testActivelyPreferBadWifiSetting() throws Exception {
+ doReturn(1).when(mResources).getInteger(R.integer.config_activelyPreferBadWifi);
+ mPolicyTracker.reevaluate();
+ waitForIdle();
+ assertTrue(mService.mNetworkRanker.getConfiguration().activelyPreferBadWifi());
+
+ doReturn(0).when(mResources).getInteger(R.integer.config_activelyPreferBadWifi);
+ mPolicyTracker.reevaluate();
+ waitForIdle();
+ if (SdkLevel.isAtLeastU()) {
+ // U+ ignore the setting and always actively prefers bad wifi
+ assertTrue(mService.mNetworkRanker.getConfiguration().activelyPreferBadWifi());
+ } else {
+ assertFalse(mService.mNetworkRanker.getConfiguration().activelyPreferBadWifi());
+ }
+ }
+
+ @Test
public void testOffersAvoidsBadWifi() throws Exception {
// Normal mode : the carrier doesn't restrict moving away from bad wifi.
// This has getAvoidBadWifi return true.
@@ -5737,6 +5772,56 @@
wifiCallback.assertNoCallback();
}
+ public void doTestPreferBadWifi(final boolean preferBadWifi) throws Exception {
+ // Pretend we're on a carrier that restricts switching away from bad wifi, and
+ // depending on the parameter one that may indeed prefer bad wifi.
+ doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
+ doReturn(preferBadWifi ? 1 : 0).when(mResources)
+ .getInteger(R.integer.config_activelyPreferBadWifi);
+ mPolicyTracker.reevaluate();
+
+ registerDefaultNetworkCallbacks();
+ final NetworkRequest wifiRequest = new NetworkRequest.Builder()
+ .clearCapabilities()
+ .addTransportType(TRANSPORT_WIFI)
+ .build();
+ final TestableNetworkCallback wifiCallback = new TestableNetworkCallback();
+ mCm.registerNetworkCallback(wifiRequest, wifiCallback);
+
+ // Bring up validated cell and unvalidated wifi.
+ mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellNetworkAgent.connect(true);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+
+ mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiNetworkAgent.connect(false);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+
+ if (preferBadWifi) {
+ expectUnvalidationCheckWillNotify(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ mDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ } else {
+ expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ mDefaultNetworkCallback.assertNoCallback();
+ }
+ }
+
+ @Test
+ public void testPreferBadWifi_doNotPrefer() throws Exception {
+ // Starting with U this mode is no longer supported and can't actually be tested
+ assumeFalse(SdkLevel.isAtLeastU());
+ runAsShell(READ_DEVICE_CONFIG, () -> {
+ doTestPreferBadWifi(false /* preferBadWifi */);
+ });
+ }
+
+ @Test
+ public void testPreferBadWifi_doPrefer() throws Exception {
+ runAsShell(READ_DEVICE_CONFIG, () -> {
+ doTestPreferBadWifi(true /* preferBadWifi */);
+ });
+ }
+
@Test
public void testAvoidBadWifi() throws Exception {
final ContentResolver cr = mServiceContext.getContentResolver();
@@ -5760,7 +5845,8 @@
TestNetworkCallback validatedWifiCallback = new TestNetworkCallback();
mCm.registerNetworkCallback(validatedWifiRequest, validatedWifiCallback);
- Settings.Global.putInt(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, 0);
+ // Prompt mode, so notifications can be tested
+ Settings.Global.putString(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, null);
mPolicyTracker.reevaluate();
// Bring up validated cell.
@@ -5782,6 +5868,7 @@
mCm.reportNetworkConnectivity(wifiNetwork, false);
defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
validatedWifiCallback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
+ expectNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
// Because avoid bad wifi is off, we don't switch to cellular.
defaultCallback.assertNoCallback();
@@ -5797,14 +5884,20 @@
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
assertEquals(mCm.getActiveNetwork(), cellNetwork);
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
// Switch back to a restrictive carrier.
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
assertEquals(mCm.getActiveNetwork(), wifiNetwork);
+ // A notification was already shown for this very network.
+ expectNoNotification(mWiFiNetworkAgent);
// Simulate the user selecting "switch" on the dialog, and check that we switch to cell.
+ // In principle this is a little bit unrealistic because the switch to a less restrictive
+ // carrier above should have remove the notification but this doesn't matter for the
+ // purposes of this test.
mCm.setAvoidUnvalidated(wifiNetwork);
defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
@@ -5826,6 +5919,7 @@
mCm.reportNetworkConnectivity(wifiNetwork, false);
defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
validatedWifiCallback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
+ expectNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
// Simulate the user selecting "switch" and checking the don't ask again checkbox.
Settings.Global.putInt(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, 1);
@@ -5838,6 +5932,7 @@
assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
NET_CAPABILITY_VALIDATED));
assertEquals(mCm.getActiveNetwork(), cellNetwork);
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
// Simulate the user turning the cellular fallback setting off and then on.
// We switch to wifi and then to cell.
@@ -5845,6 +5940,9 @@
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
assertEquals(mCm.getActiveNetwork(), wifiNetwork);
+ // Notification is cleared again because CS doesn't particularly remember that it has
+ // cleared it before, and if it hasn't cleared it before then it should do so now.
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
Settings.Global.putInt(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, 1);
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
@@ -5855,6 +5953,8 @@
defaultCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent);
defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
validatedWifiCallback.assertNoCallback();
+ // Notification is cleared yet again because the device switched to wifi.
+ expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
mCm.unregisterNetworkCallback(cellNetworkCallback);
mCm.unregisterNetworkCallback(validatedWifiCallback);
@@ -12958,6 +13058,12 @@
if (null != mTestPackageDefaultNetworkCallback2) {
mCm.unregisterNetworkCallback(mTestPackageDefaultNetworkCallback2);
}
+ mSystemDefaultNetworkCallback = null;
+ mDefaultNetworkCallback = null;
+ mProfileDefaultNetworkCallback = null;
+ mTestPackageDefaultNetworkCallback = null;
+ mProfileDefaultNetworkCallbackAsAppUid2 = null;
+ mTestPackageDefaultNetworkCallback2 = null;
}
private void setupMultipleDefaultNetworksForOemNetworkPreferenceNotCurrentUidTest(
diff --git a/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java b/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java
index 7646c19..85bc4a9 100644
--- a/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java
+++ b/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java
@@ -516,28 +516,38 @@
assertEquals(65508, ClatCoordinator.adjustMtu(CLAT_MAX_MTU + 1 /* over maximum mtu */));
}
- @Test
- public void testDump() throws Exception {
- final ClatCoordinator coordinator = makeClatCoordinator();
+ private void verifyDump(final ClatCoordinator coordinator, boolean clatStarted) {
final StringWriter stringWriter = new StringWriter();
final IndentingPrintWriter ipw = new IndentingPrintWriter(stringWriter, " ");
- coordinator.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX);
coordinator.dump(ipw);
final String[] dumpStrings = stringWriter.toString().split("\n");
- assertEquals(6, dumpStrings.length);
- assertEquals("CLAT tracker: iface: test0 (1000), v4iface: v4-test0 (1001), "
- + "v4: /192.0.0.46, v6: /2001:db8:0:b11::464, pfx96: /64:ff9b::, "
- + "pid: 10483, cookie: 27149", dumpStrings[0].trim());
- assertEquals("Forwarding rules:", dumpStrings[1].trim());
- assertEquals("BPF ingress map: iif nat64Prefix v6Addr -> v4Addr oif",
- dumpStrings[2].trim());
- assertEquals("1000 /64:ff9b::/96 /2001:db8:0:b11::464 -> /192.0.0.46 1001",
- dumpStrings[3].trim());
- assertEquals("BPF egress map: iif v4Addr -> v6Addr nat64Prefix oif",
- dumpStrings[4].trim());
- assertEquals("1001 /192.0.0.46 -> /2001:db8:0:b11::464 /64:ff9b::/96 1000 ether",
- dumpStrings[5].trim());
+ if (clatStarted) {
+ assertEquals(6, dumpStrings.length);
+ assertEquals("CLAT tracker: iface: test0 (1000), v4iface: v4-test0 (1001), "
+ + "v4: /192.0.0.46, v6: /2001:db8:0:b11::464, pfx96: /64:ff9b::, "
+ + "pid: 10483, cookie: 27149", dumpStrings[0].trim());
+ assertEquals("Forwarding rules:", dumpStrings[1].trim());
+ assertEquals("BPF ingress map: iif nat64Prefix v6Addr -> v4Addr oif",
+ dumpStrings[2].trim());
+ assertEquals("1000 /64:ff9b::/96 /2001:db8:0:b11::464 -> /192.0.0.46 1001",
+ dumpStrings[3].trim());
+ assertEquals("BPF egress map: iif v4Addr -> v6Addr nat64Prefix oif",
+ dumpStrings[4].trim());
+ assertEquals("1001 /192.0.0.46 -> /2001:db8:0:b11::464 /64:ff9b::/96 1000 ether",
+ dumpStrings[5].trim());
+ } else {
+ assertEquals(1, dumpStrings.length);
+ assertEquals("<not started>", dumpStrings[0].trim());
+ }
+ }
+
+ @Test
+ public void testDump() throws Exception {
+ final ClatCoordinator coordinator = makeClatCoordinator();
+ verifyDump(coordinator, false /* clatStarted */);
+ coordinator.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX);
+ verifyDump(coordinator, true /* clatStarted */);
}
@Test
@@ -548,25 +558,18 @@
() -> coordinator.clatStart(BASE_IFACE, NETID, invalidPrefix));
}
- private void assertStartClat(final TestDependencies deps) throws Exception {
- final ClatCoordinator coordinator = new ClatCoordinator(deps);
- assertNotNull(coordinator.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX));
- }
-
- private void assertNotStartClat(final TestDependencies deps) {
- // Expect that the injection function of TestDependencies causes clatStart() failed.
- final ClatCoordinator coordinator = new ClatCoordinator(deps);
- assertThrows(IOException.class,
- () -> coordinator.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX));
- }
-
private void checkNotStartClat(final TestDependencies deps, final boolean needToCloseTunFd,
final boolean needToClosePacketSockFd, final boolean needToCloseRawSockFd)
throws Exception {
- // [1] Expect that modified TestDependencies can't start clatd.
- // Use precise check to make sure that there is no unexpected file descriptor closing.
clearInvocations(TUN_PFD, RAW_SOCK_PFD, PACKET_SOCK_PFD);
- assertNotStartClat(deps);
+
+ // [1] Expect that modified TestDependencies can't start clatd.
+ // Expect that the injection function of TestDependencies causes clatStart() failed.
+ final ClatCoordinator coordinatorWithBrokenDeps = new ClatCoordinator(deps);
+ assertThrows(IOException.class,
+ () -> coordinatorWithBrokenDeps.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX));
+
+ // Use precise check to make sure that there is no unexpected file descriptor closing.
if (needToCloseTunFd) {
verify(TUN_PFD).close();
} else {
@@ -583,10 +586,15 @@
verify(RAW_SOCK_PFD, never()).close();
}
+ // Check that dump doesn't crash after any clat starting failure.
+ verifyDump(coordinatorWithBrokenDeps, false /* clatStarted */);
+
// [2] Expect that unmodified TestDependencies can start clatd.
// Used to make sure that the above modified TestDependencies has really broken the
// clatd starting.
- assertStartClat(new TestDependencies());
+ final ClatCoordinator coordinatorWithDefaultDeps = new ClatCoordinator(
+ new TestDependencies());
+ assertNotNull(coordinatorWithDefaultDeps.clatStart(BASE_IFACE, NETID, NAT64_IP_PREFIX));
}
// The following testNotStartClat* tests verifies bunches of code for unwinding the
diff --git a/tests/unit/java/android/net/util/MultinetworkPolicyTrackerTest.kt b/tests/unit/java/com/android/server/connectivity/MultinetworkPolicyTrackerTest.kt
similarity index 85%
rename from tests/unit/java/android/net/util/MultinetworkPolicyTrackerTest.kt
rename to tests/unit/java/com/android/server/connectivity/MultinetworkPolicyTrackerTest.kt
index 0dbee5d..8348675 100644
--- a/tests/unit/java/android/net/util/MultinetworkPolicyTrackerTest.kt
+++ b/tests/unit/java/com/android/server/connectivity/MultinetworkPolicyTrackerTest.kt
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package android.net.util
+package com.android.server.connectivity
import android.content.Context
import android.content.res.Resources
@@ -24,7 +24,7 @@
import android.net.ConnectivityResources
import android.net.ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI
import android.net.ConnectivitySettingsManager.NETWORK_METERED_MULTIPATH_PREFERENCE
-import android.net.util.MultinetworkPolicyTracker.ActiveDataSubscriptionIdListener
+import com.android.server.connectivity.MultinetworkPolicyTracker.ActiveDataSubscriptionIdListener
import android.os.Build
import android.os.Handler
import android.provider.Settings
@@ -35,6 +35,7 @@
import androidx.test.filters.SmallTest
import com.android.connectivity.resources.R
import com.android.internal.util.test.FakeSettingsProvider
+import com.android.modules.utils.build.SdkLevel
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import org.junit.After
@@ -47,10 +48,8 @@
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.argThat
-import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.any
import org.mockito.Mockito.doCallRealMethod
-import org.mockito.Mockito.doNothing
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
@@ -60,16 +59,15 @@
* Tests for [MultinetworkPolicyTracker].
*
* Build, install and run with:
- * atest android.net.util.MultinetworkPolicyTrackerTest
+ * atest FrameworksNetTest:MultinetworkPolicyTrackerTest
*/
@RunWith(DevSdkIgnoreRunner::class)
@SmallTest
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
class MultinetworkPolicyTrackerTest {
private val resources = mock(Resources::class.java).also {
- doReturn(R.integer.config_networkAvoidBadWifi).`when`(it).getIdentifier(
- eq("config_networkAvoidBadWifi"), eq("integer"), any())
doReturn(0).`when`(it).getInteger(R.integer.config_networkAvoidBadWifi)
+ doReturn(0).`when`(it).getInteger(R.integer.config_activelyPreferBadWifi)
}
private val telephonyManager = mock(TelephonyManager::class.java)
private val subscriptionManager = mock(SubscriptionManager::class.java).also {
@@ -122,6 +120,7 @@
@Test
fun testUpdateAvoidBadWifi() {
+ doReturn(0).`when`(resources).getInteger(R.integer.config_activelyPreferBadWifi)
Settings.Global.putString(resolver, NETWORK_AVOID_BAD_WIFI, "0")
assertTrue(tracker.updateAvoidBadWifi())
assertFalse(tracker.avoidBadWifi)
@@ -129,6 +128,24 @@
doReturn(1).`when`(resources).getInteger(R.integer.config_networkAvoidBadWifi)
assertTrue(tracker.updateAvoidBadWifi())
assertTrue(tracker.avoidBadWifi)
+
+ if (SdkLevel.isAtLeastU()) {
+ // On U+, the system always prefers bad wifi.
+ assertTrue(tracker.activelyPreferBadWifi)
+ } else {
+ assertFalse(tracker.activelyPreferBadWifi)
+ }
+
+ doReturn(1).`when`(resources).getInteger(R.integer.config_activelyPreferBadWifi)
+ if (SdkLevel.isAtLeastU()) {
+ // On U+, this didn't change the setting
+ assertFalse(tracker.updateAvoidBadWifi())
+ } else {
+ // On T-, this must have changed the setting
+ assertTrue(tracker.updateAvoidBadWifi())
+ }
+ // In all cases, now the system actively prefers bad wifi
+ assertTrue(tracker.activelyPreferBadWifi)
}
@Test
diff --git a/tests/unit/java/com/android/server/connectivity/NetworkRankerTest.kt b/tests/unit/java/com/android/server/connectivity/NetworkRankerTest.kt
index 8b1c510..ae8b438 100644
--- a/tests/unit/java/com/android/server/connectivity/NetworkRankerTest.kt
+++ b/tests/unit/java/com/android/server/connectivity/NetworkRankerTest.kt
@@ -17,6 +17,7 @@
package com.android.server.connectivity
import android.net.NetworkCapabilities
+import android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.net.NetworkScore.KEEP_CONNECTED_NONE
@@ -25,24 +26,29 @@
import android.net.NetworkScore.POLICY_YIELD_TO_BAD_WIFI
import android.os.Build
import androidx.test.filters.SmallTest
+import com.android.connectivity.resources.R
import com.android.server.connectivity.FullScore.POLICY_AVOIDED_WHEN_UNVALIDATED
+import com.android.server.connectivity.FullScore.POLICY_EVER_EVALUATED
import com.android.server.connectivity.FullScore.POLICY_EVER_VALIDATED
import com.android.server.connectivity.FullScore.POLICY_IS_VALIDATED
import com.android.testutils.DevSdkIgnoreRule
-import com.android.testutils.DevSdkIgnoreRunner
+import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
import kotlin.test.assertEquals
private fun score(vararg policies: Int) = FullScore(
policies.fold(0L) { acc, e -> acc or (1L shl e) }, KEEP_CONNECTED_NONE)
-private fun caps(transport: Int) = NetworkCapabilities.Builder().addTransportType(transport).build()
+private fun caps(transport: Int, vararg capabilities: Int) =
+ NetworkCapabilities.Builder().addTransportType(transport).apply {
+ capabilities.forEach { addCapability(it) }
+ }.build()
@SmallTest
-@RunWith(DevSdkIgnoreRunner::class)
-@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
-class NetworkRankerTest {
- private val mRanker = NetworkRanker()
+@RunWith(Parameterized::class)
+class NetworkRankerTest(private val activelyPreferBadWifi: Boolean) {
+ private val mRanker = NetworkRanker(NetworkRanker.Configuration(activelyPreferBadWifi))
private class TestScore(private val sc: FullScore, private val nc: NetworkCapabilities)
: NetworkRanker.Scoreable {
@@ -50,35 +56,91 @@
override fun getCapsNoCopy(): NetworkCapabilities = nc
}
+ @get:Rule
+ val mIgnoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.R)
+
+ companion object {
+ @JvmStatic
+ @Parameterized.Parameters
+ fun ranker() = listOf(true, false)
+ }
+
@Test
fun testYieldToBadWiFiOneCell() {
// Only cell, it wins
- val winner = TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ val winner = TestScore(
+ score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED, POLICY_EVER_EVALUATED),
caps(TRANSPORT_CELLULAR))
val scores = listOf(winner)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
}
@Test
+ fun testPreferBadWifiOneCellOneEvaluatingWifi() {
+ // TODO : refactor the tests to name each network like this test
+ val wifi = TestScore(score(), caps(TRANSPORT_WIFI))
+ val cell = TestScore(
+ score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED, POLICY_EVER_EVALUATED),
+ caps(TRANSPORT_CELLULAR))
+ assertEquals(cell, mRanker.getBestNetworkByPolicy(listOf(wifi, cell), null))
+ }
+
+ @Test
fun testYieldToBadWiFiOneCellOneBadWiFi() {
// Bad wifi wins against yielding validated cell
- val winner = TestScore(score(POLICY_EVER_VALIDATED), caps(TRANSPORT_WIFI))
+ val winner = TestScore(score(POLICY_EVER_VALIDATED, POLICY_EVER_EVALUATED),
+ caps(TRANSPORT_WIFI))
val scores = listOf(
winner,
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(
+ score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED, POLICY_EVER_EVALUATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
}
@Test
+ fun testPreferBadWifiOneCellOneBadWifi() {
+ val wifi = TestScore(score(POLICY_EVER_EVALUATED), caps(TRANSPORT_WIFI))
+ val cell = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR))
+ val scores = listOf(wifi, cell)
+ val winner = if (activelyPreferBadWifi) wifi else cell
+ assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
+ }
+
+ @Test
+ fun testPreferBadWifiOneCellOneCaptivePortalWifi() {
+ val wifi = TestScore(score(POLICY_EVER_EVALUATED),
+ caps(TRANSPORT_WIFI, NET_CAPABILITY_CAPTIVE_PORTAL))
+ val cell = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR))
+ assertEquals(cell, mRanker.getBestNetworkByPolicy(listOf(wifi, cell), null))
+ }
+
+ @Test
+ fun testYieldToBadWifiOneCellOneCaptivePortalWifiThatClosed() {
+ val wifi = TestScore(score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED),
+ caps(TRANSPORT_WIFI, NET_CAPABILITY_CAPTIVE_PORTAL))
+ val cell = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR))
+ assertEquals(wifi, mRanker.getBestNetworkByPolicy(listOf(wifi, cell), null))
+ }
+
+ @Test
fun testYieldToBadWifiAvoidUnvalidated() {
// Bad wifi avoided when unvalidated loses against yielding validated cell
- val winner = TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED, POLICY_AVOIDED_WHEN_UNVALIDATED),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED,
+ POLICY_AVOIDED_WHEN_UNVALIDATED),
caps(TRANSPORT_WIFI))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
@@ -87,12 +149,16 @@
@Test
fun testYieldToBadWiFiOneCellTwoBadWiFi() {
// Bad wifi wins against yielding validated cell. Prefer the one that's primary.
- val winner = TestScore(score(POLICY_EVER_VALIDATED,
- POLICY_TRANSPORT_PRIMARY), caps(TRANSPORT_WIFI))
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED, POLICY_TRANSPORT_PRIMARY),
+ caps(TRANSPORT_WIFI))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED), caps(TRANSPORT_WIFI)),
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED),
+ caps(TRANSPORT_WIFI)),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
@@ -102,11 +168,13 @@
fun testYieldToBadWiFiOneCellTwoBadWiFiOneNotAvoided() {
// Bad wifi ever validated wins against bad wifi that never was validated (or was
// avoided when bad).
- val winner = TestScore(score(POLICY_EVER_VALIDATED), caps(TRANSPORT_WIFI))
+ val winner = TestScore(score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED),
+ caps(TRANSPORT_WIFI))
val scores = listOf(
winner,
- TestScore(score(), caps(TRANSPORT_WIFI)),
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(score(POLICY_EVER_EVALUATED), caps(TRANSPORT_WIFI)),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
@@ -115,27 +183,49 @@
@Test
fun testYieldToBadWiFiOneCellOneBadWiFiOneGoodWiFi() {
// Good wifi wins
- val winner = TestScore(score(POLICY_EVER_VALIDATED, POLICY_IS_VALIDATED),
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED, POLICY_IS_VALIDATED),
caps(TRANSPORT_WIFI))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED, POLICY_TRANSPORT_PRIMARY),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED,
+ POLICY_TRANSPORT_PRIMARY),
caps(TRANSPORT_WIFI)),
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
}
@Test
+ fun testPreferBadWifiOneCellOneBadWifiOneEvaluatingWifi() {
+ val cell = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR))
+ val badWifi = TestScore(score(POLICY_EVER_EVALUATED), caps(TRANSPORT_WIFI))
+ val evaluatingWifi = TestScore(score(), caps(TRANSPORT_WIFI))
+ val winner = if (activelyPreferBadWifi) badWifi else cell
+ assertEquals(winner,
+ mRanker.getBestNetworkByPolicy(listOf(cell, badWifi, evaluatingWifi), null))
+ }
+
+ @Test
fun testYieldToBadWiFiTwoCellsOneBadWiFi() {
// Cell that doesn't yield wins over cell that yields and bad wifi
- val winner = TestScore(score(POLICY_IS_VALIDATED), caps(TRANSPORT_CELLULAR))
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED, POLICY_TRANSPORT_PRIMARY),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED,
+ POLICY_TRANSPORT_PRIMARY),
caps(TRANSPORT_WIFI)),
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI,
+ POLICY_EVER_VALIDATED, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
@@ -144,13 +234,20 @@
@Test
fun testYieldToBadWiFiTwoCellsOneBadWiFiOneGoodWiFi() {
// Good wifi wins over cell that doesn't yield and cell that yields
- val winner = TestScore(score(POLICY_IS_VALIDATED), caps(TRANSPORT_WIFI))
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_WIFI))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED, POLICY_TRANSPORT_PRIMARY),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED,
+ POLICY_TRANSPORT_PRIMARY),
caps(TRANSPORT_WIFI)),
- TestScore(score(POLICY_IS_VALIDATED), caps(TRANSPORT_CELLULAR)),
- TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_IS_VALIDATED),
+ caps(TRANSPORT_CELLULAR)),
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
@@ -159,11 +256,14 @@
@Test
fun testYieldToBadWiFiOneExitingGoodWiFi() {
// Yielding cell wins over good exiting wifi
- val winner = TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
val scores = listOf(
winner,
- TestScore(score(POLICY_IS_VALIDATED, POLICY_EXITING), caps(TRANSPORT_WIFI))
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_IS_VALIDATED, POLICY_EXITING),
+ caps(TRANSPORT_WIFI))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
}
@@ -171,11 +271,14 @@
@Test
fun testYieldToBadWiFiOneExitingBadWiFi() {
// Yielding cell wins over bad exiting wifi
- val winner = TestScore(score(POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
+ val winner = TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_YIELD_TO_BAD_WIFI, POLICY_IS_VALIDATED),
caps(TRANSPORT_CELLULAR))
val scores = listOf(
winner,
- TestScore(score(POLICY_EVER_VALIDATED, POLICY_EXITING), caps(TRANSPORT_WIFI))
+ TestScore(
+ score(POLICY_EVER_EVALUATED, POLICY_EVER_VALIDATED, POLICY_EXITING),
+ caps(TRANSPORT_WIFI))
)
assertEquals(winner, mRanker.getBestNetworkByPolicy(scores, null))
}
diff --git a/tests/unit/java/com/android/server/connectivity/VpnTest.java b/tests/unit/java/com/android/server/connectivity/VpnTest.java
index 06dabb0..1c54651 100644
--- a/tests/unit/java/com/android/server/connectivity/VpnTest.java
+++ b/tests/unit/java/com/android/server/connectivity/VpnTest.java
@@ -29,7 +29,6 @@
import static android.os.Build.VERSION_CODES.S_V2;
import static android.os.UserHandle.PER_USER_RANGE;
-import static com.android.modules.utils.build.SdkLevel.isAtLeastT;
import static com.android.testutils.Cleanup.testAndCleanup;
import static com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import static com.android.testutils.MiscAsserts.assertThrows;
@@ -41,7 +40,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -136,7 +134,6 @@
import com.android.internal.net.VpnConfig;
import com.android.internal.net.VpnProfile;
import com.android.internal.util.HexDump;
-import com.android.modules.utils.build.SdkLevel;
import com.android.server.DeviceIdleInternal;
import com.android.server.IpSecService;
import com.android.server.VpnTestBase;
@@ -341,7 +338,7 @@
@After
public void tearDown() throws Exception {
- doReturn(PERMISSION_DENIED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN);
+ doReturn(PERMISSION_DENIED).when(mContext).checkCallingOrSelfPermission(any());
}
private <T> void mockService(Class<T> clazz, String name, T service) {
@@ -695,7 +692,6 @@
@Test
public void testPrepare_throwSecurityExceptionWhenGivenPackageDoesNotBelongToTheCaller()
throws Exception {
- assumeTrue(isAtLeastT());
final Vpn vpn = createVpnAndSetupUidChecks();
assertThrows(SecurityException.class,
() -> vpn.prepare("com.not.vpn.owner", null, VpnManager.TYPE_VPN_SERVICE));
@@ -1168,7 +1164,6 @@
@Test
public void testStartOpAndFinishOpWillBeCalledWhenPlatformVpnIsOnAndOff() throws Exception {
- assumeTrue(SdkLevel.isAtLeastT());
final Vpn vpn = createVpnAndSetupUidChecks(AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN);
when(mVpnProfileStore.get(vpn.getProfileNameForPackage(TEST_VPN_PKG)))
.thenReturn(mVpnProfile.encode());
@@ -1194,7 +1189,6 @@
@Test
public void testStartOpWithSeamlessHandover() throws Exception {
- assumeTrue(SdkLevel.isAtLeastT());
final Vpn vpn = createVpnAndSetupUidChecks(AppOpsManager.OPSTR_ACTIVATE_VPN);
assertTrue(vpn.prepare(TEST_VPN_PKG, null, VpnManager.TYPE_VPN_SERVICE));
final VpnConfig config = new VpnConfig();
@@ -1226,7 +1220,7 @@
}
private void verifyVpnManagerEvent(String sessionKey, String category, int errorClass,
- int errorCode, VpnProfileState... profileState) {
+ int errorCode, String[] packageName, VpnProfileState... profileState) {
final Context userContext =
mContext.createContextAsUser(UserHandle.of(PRIMARY_USER.id), 0 /* flags */);
final ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
@@ -1236,9 +1230,11 @@
for (int i = 0; i < verifyTimes; i++) {
final Intent intent = intentArgumentCaptor.getAllValues().get(i);
+ assertEquals(packageName[i], intent.getPackage());
assertEquals(sessionKey, intent.getStringExtra(VpnManager.EXTRA_SESSION_KEY));
final Set<String> categories = intent.getCategories();
assertTrue(categories.contains(category));
+ assertEquals(1, categories.size());
assertEquals(errorClass,
intent.getIntExtra(VpnManager.EXTRA_ERROR_CLASS, -1 /* defaultValue */));
assertEquals(errorCode,
@@ -1251,9 +1247,21 @@
reset(userContext);
}
+ private void verifyDeactivatedByUser(String sessionKey, String[] packageName) {
+ // CATEGORY_EVENT_DEACTIVATED_BY_USER is not an error event, so both of errorClass and
+ // errorCode won't be set.
+ verifyVpnManagerEvent(sessionKey, VpnManager.CATEGORY_EVENT_DEACTIVATED_BY_USER,
+ -1 /* errorClass */, -1 /* errorCode */, packageName, null /* profileState */);
+ }
+
+ private void verifyAlwaysOnStateChanged(String[] packageName, VpnProfileState... profileState) {
+ verifyVpnManagerEvent(null /* sessionKey */,
+ VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
+ -1 /* errorCode */, packageName, profileState);
+ }
+
@Test
public void testVpnManagerEventForUserDeactivated() throws Exception {
- assumeTrue(SdkLevel.isAtLeastT());
// For security reasons, Vpn#prepare() will check that oldPackage and newPackage are either
// null or the package of the caller. This test will call Vpn#prepare() to pretend the old
// VPN is replaced by a new one. But only Settings can change to some other packages, and
@@ -1271,10 +1279,7 @@
verifyPlatformVpnIsDeactivated(TEST_VPN_PKG);
verifyPowerSaveTempWhitelistApp(TEST_VPN_PKG);
reset(mDeviceIdleInternal);
- // CATEGORY_EVENT_DEACTIVATED_BY_USER is not an error event, so both of errorClass and
- // errorCode won't be set.
- verifyVpnManagerEvent(sessionKey1, VpnManager.CATEGORY_EVENT_DEACTIVATED_BY_USER,
- -1 /* errorClass */, -1 /* errorCode */, null /* profileState */);
+ verifyDeactivatedByUser(sessionKey1, new String[] {TEST_VPN_PKG});
reset(mAppOps);
// Test the case that the user chooses another vpn and the original one is replaced.
@@ -1284,15 +1289,11 @@
verifyPlatformVpnIsDeactivated(TEST_VPN_PKG);
verifyPowerSaveTempWhitelistApp(TEST_VPN_PKG);
reset(mDeviceIdleInternal);
- // CATEGORY_EVENT_DEACTIVATED_BY_USER is not an error event, so both of errorClass and
- // errorCode won't be set.
- verifyVpnManagerEvent(sessionKey2, VpnManager.CATEGORY_EVENT_DEACTIVATED_BY_USER,
- -1 /* errorClass */, -1 /* errorCode */, null /* profileState */);
+ verifyDeactivatedByUser(sessionKey2, new String[] {TEST_VPN_PKG});
}
@Test
public void testVpnManagerEventForAlwaysOnChanged() throws Exception {
- assumeTrue(SdkLevel.isAtLeastT());
// Calling setAlwaysOnPackage() needs to hold CONTROL_VPN.
doReturn(PERMISSION_GRANTED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN);
final Vpn vpn = createVpn(PRIMARY_USER.id);
@@ -1301,9 +1302,8 @@
null /* lockdownAllowlist */));
verifyPowerSaveTempWhitelistApp(PKGS[1]);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, true /* alwaysOn */, false /* lockdown */));
// Enable VPN lockdown for PKGS[1].
@@ -1311,9 +1311,8 @@
null /* lockdownAllowlist */));
verifyPowerSaveTempWhitelistApp(PKGS[1]);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, true /* alwaysOn */, true /* lockdown */));
// Disable VPN lockdown for PKGS[1].
@@ -1321,9 +1320,8 @@
null /* lockdownAllowlist */));
verifyPowerSaveTempWhitelistApp(PKGS[1]);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, true /* alwaysOn */, false /* lockdown */));
// Disable VPN always-on.
@@ -1331,9 +1329,8 @@
null /* lockdownAllowlist */));
verifyPowerSaveTempWhitelistApp(PKGS[1]);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, false /* alwaysOn */, false /* lockdown */));
// Enable VPN always-on for PKGS[1] again.
@@ -1341,9 +1338,8 @@
null /* lockdownAllowlist */));
verifyPowerSaveTempWhitelistApp(PKGS[1]);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, true /* alwaysOn */, false /* lockdown */));
// Enable VPN always-on for PKGS[2].
@@ -1355,9 +1351,8 @@
// Pass 2 VpnProfileState objects to verifyVpnManagerEvent(), the first one is sent to
// PKGS[1] to notify PKGS[1] that the VPN always-on is disabled, the second one is sent to
// PKGS[2] to notify PKGS[2] that the VPN always-on is enabled.
- verifyVpnManagerEvent(null /* sessionKey */,
- VpnManager.CATEGORY_EVENT_ALWAYS_ON_STATE_CHANGED, -1 /* errorClass */,
- -1 /* errorCode */, new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
+ verifyAlwaysOnStateChanged(new String[] {PKGS[1], PKGS[2]},
+ new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, false /* alwaysOn */, false /* lockdown */),
new VpnProfileState(VpnProfileState.STATE_DISCONNECTED,
null /* sessionKey */, true /* alwaysOn */, false /* lockdown */));
@@ -1479,7 +1474,8 @@
verifyPowerSaveTempWhitelistApp(TEST_VPN_PKG);
reset(mDeviceIdleInternal);
- verifyVpnManagerEvent(sessionKey, category, errorType, errorCode, null /* profileState */);
+ verifyVpnManagerEvent(sessionKey, category, errorType, errorCode,
+ new String[] {TEST_VPN_PKG}, null /* profileState */);
if (errorType == VpnManager.ERROR_CLASS_NOT_RECOVERABLE) {
verify(mConnectivityManager, timeout(TEST_TIMEOUT_MS))
.unregisterNetworkCallback(eq(cb));
diff --git a/tests/unit/java/com/android/server/ethernet/EthernetTrackerTest.java b/tests/unit/java/com/android/server/ethernet/EthernetTrackerTest.java
index dde1d94..5e7f0ff 100644
--- a/tests/unit/java/com/android/server/ethernet/EthernetTrackerTest.java
+++ b/tests/unit/java/com/android/server/ethernet/EthernetTrackerTest.java
@@ -23,24 +23,15 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
-import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
import android.content.Context;
-import android.net.EthernetManager;
-import android.net.IEthernetServiceListener;
import android.net.INetd;
import android.net.InetAddresses;
-import android.net.InterfaceConfigurationParcel;
import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
@@ -66,7 +57,6 @@
import java.net.InetAddress;
import java.util.ArrayList;
-import java.util.concurrent.atomic.AtomicBoolean;
@SmallTest
@RunWith(DevSdkIgnoreRunner.class)
@@ -384,74 +374,4 @@
assertTrue(isValidTestInterface);
}
-
- public static class EthernetStateListener extends IEthernetServiceListener.Stub {
- @Override
- public void onEthernetStateChanged(int state) { }
-
- @Override
- public void onInterfaceStateChanged(String iface, int state, int role,
- IpConfiguration configuration) { }
- }
-
- private InterfaceConfigurationParcel createMockedIfaceParcel(final String ifname,
- final String hwAddr) {
- final InterfaceConfigurationParcel ifaceParcel = new InterfaceConfigurationParcel();
- ifaceParcel.ifName = ifname;
- ifaceParcel.hwAddr = hwAddr;
- ifaceParcel.flags = new String[] {INetd.IF_STATE_UP};
- return ifaceParcel;
- }
-
- @Test
- public void testListenEthernetStateChange() throws Exception {
- tracker.setIncludeTestInterfaces(true);
- waitForIdle();
-
- final String testIface = "testtap123";
- final String testHwAddr = "11:22:33:44:55:66";
- final InterfaceConfigurationParcel ifaceParcel = createMockedIfaceParcel(testIface,
- testHwAddr);
- when(mNetd.interfaceGetList()).thenReturn(new String[] {testIface});
- when(mNetd.interfaceGetCfg(eq(testIface))).thenReturn(ifaceParcel);
- doReturn(new String[] {testIface}).when(mFactory).getAvailableInterfaces(anyBoolean());
-
- final AtomicBoolean ifaceUp = new AtomicBoolean(true);
- doAnswer(inv -> ifaceUp.get()).when(mFactory).hasInterface(testIface);
- doAnswer(inv ->
- ifaceUp.get() ? EthernetManager.STATE_LINK_UP : EthernetManager.STATE_ABSENT)
- .when(mFactory).getInterfaceState(testIface);
- doAnswer(inv -> {
- ifaceUp.set(true);
- return null;
- }).when(mFactory).addInterface(eq(testIface), eq(testHwAddr), any(), any());
- doAnswer(inv -> {
- ifaceUp.set(false);
- return null;
- }).when(mFactory).removeInterface(testIface);
-
- final EthernetStateListener listener = spy(new EthernetStateListener());
- tracker.addListener(listener, true /* canUseRestrictedNetworks */);
- // Check default state.
- waitForIdle();
- verify(listener).onInterfaceStateChanged(eq(testIface), eq(EthernetManager.STATE_LINK_UP),
- anyInt(), any());
- verify(listener).onEthernetStateChanged(eq(EthernetManager.ETHERNET_STATE_ENABLED));
- reset(listener);
-
- tracker.setEthernetEnabled(false);
- waitForIdle();
- verify(mFactory).removeInterface(eq(testIface));
- verify(listener).onEthernetStateChanged(eq(EthernetManager.ETHERNET_STATE_DISABLED));
- verify(listener).onInterfaceStateChanged(eq(testIface), eq(EthernetManager.STATE_ABSENT),
- anyInt(), any());
- reset(listener);
-
- tracker.setEthernetEnabled(true);
- waitForIdle();
- verify(mFactory).addInterface(eq(testIface), eq(testHwAddr), any(), any());
- verify(listener).onEthernetStateChanged(eq(EthernetManager.ETHERNET_STATE_ENABLED));
- verify(listener).onInterfaceStateChanged(eq(testIface), eq(EthernetManager.STATE_LINK_UP),
- anyInt(), any());
- }
}