Merge changes I1cf472bd,Ibdaf2b23,I6967ad6f,I65f522c2,I0f220d1f, ...

* changes:
  Make the temporary NC sanitized instead of inlining
  Remove the legacy int from FullScore.
  Remove accessors for legacy int score from NAI
  Log the creation time of network agents
  Show time taken by the rematch
  Address some IDE warnings in ConnectivityService
  Copy arguments from NetworkAgent registration early
This commit is contained in:
Paul Hu
2022-08-10 01:41:43 +00:00
committed by Gerrit Code Review
8 changed files with 204 additions and 258 deletions

View File

@@ -181,7 +181,7 @@ public final class NetworkScore implements Parcelable {
@Override @Override
public String toString() { public String toString() {
return "Score(" + mLegacyInt + " ; Policies : " + mPolicies + ")"; return "Score(Policies : " + mPolicies + ")";
} }
@Override @Override

View File

@@ -380,7 +380,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// See ConnectivitySettingsManager.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS // See ConnectivitySettingsManager.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS
private final int mReleasePendingIntentDelayMs; private final int mReleasePendingIntentDelayMs;
private MockableSystemProperties mSystemProperties; private final MockableSystemProperties mSystemProperties;
@VisibleForTesting @VisibleForTesting
protected final PermissionMonitor mPermissionMonitor; protected final PermissionMonitor mPermissionMonitor;
@@ -396,7 +396,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
* Stale copy of uid blocked reasons provided by NPMS. As long as they are accessed only in * Stale copy of uid blocked reasons provided by NPMS. As long as they are accessed only in
* internal handler thread, they don't need a lock. * internal handler thread, they don't need a lock.
*/ */
private SparseIntArray mUidBlockedReasons = new SparseIntArray(); private final SparseIntArray mUidBlockedReasons = new SparseIntArray();
private final Context mContext; private final Context mContext;
private final ConnectivityResources mResources; private final ConnectivityResources mResources;
@@ -412,9 +412,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
@VisibleForTesting @VisibleForTesting
protected INetd mNetd; protected INetd mNetd;
private DscpPolicyTracker mDscpPolicyTracker = null; private DscpPolicyTracker mDscpPolicyTracker = null;
private NetworkStatsManager mStatsManager; private final NetworkStatsManager mStatsManager;
private NetworkPolicyManager mPolicyManager; private final NetworkPolicyManager mPolicyManager;
private final NetdCallback mNetdCallback;
private final BpfNetMaps mBpfNetMaps; private final BpfNetMaps mBpfNetMaps;
/** /**
@@ -780,7 +779,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private boolean mSystemReady; private boolean mSystemReady;
private Intent mInitialBroadcast; private Intent mInitialBroadcast;
private PowerManager.WakeLock mNetTransitionWakeLock; private final PowerManager.WakeLock mNetTransitionWakeLock;
private final PowerManager.WakeLock mPendingIntentWakeLock; private final PowerManager.WakeLock mPendingIntentWakeLock;
// A helper object to track the current default HTTP proxy. ConnectivityService needs to tell // A helper object to track the current default HTTP proxy. ConnectivityService needs to tell
@@ -790,10 +789,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
final private SettingsObserver mSettingsObserver; final private SettingsObserver mSettingsObserver;
private UserManager mUserManager; private final UserManager mUserManager;
// the set of network types that can only be enabled by system/sig apps // the set of network types that can only be enabled by system/sig apps
private List<Integer> mProtectedNetworks; private final List<Integer> mProtectedNetworks;
private Set<String> mWolSupportedInterfaces; private Set<String> mWolSupportedInterfaces;
@@ -803,10 +802,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
private final LocationPermissionChecker mLocationPermissionChecker; private final LocationPermissionChecker mLocationPermissionChecker;
private KeepaliveTracker mKeepaliveTracker; private final KeepaliveTracker mKeepaliveTracker;
private QosCallbackTracker mQosCallbackTracker; private final QosCallbackTracker mQosCallbackTracker;
private NetworkNotificationManager mNotifier; private final NetworkNotificationManager mNotifier;
private LingerMonitor mLingerMonitor; private final LingerMonitor mLingerMonitor;
// sequence number of NetworkRequests // sequence number of NetworkRequests
private int mNextNetworkRequestId = NetworkRequest.FIRST_REQUEST_ID; private int mNextNetworkRequestId = NetworkRequest.FIRST_REQUEST_ID;
@@ -834,7 +833,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private final IpConnectivityLog mMetricsLog; private final IpConnectivityLog mMetricsLog;
@GuardedBy("mBandwidthRequests") @GuardedBy("mBandwidthRequests")
private final SparseArray<Integer> mBandwidthRequests = new SparseArray(10); private final SparseArray<Integer> mBandwidthRequests = new SparseArray<>(10);
@VisibleForTesting @VisibleForTesting
final MultinetworkPolicyTracker mMultinetworkPolicyTracker; final MultinetworkPolicyTracker mMultinetworkPolicyTracker;
@@ -893,7 +892,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
* - getRestoreTimerForType(type) is also synchronized on mTypeLists. * - getRestoreTimerForType(type) is also synchronized on mTypeLists.
* - dump is thread-safe with respect to concurrent add and remove calls. * - dump is thread-safe with respect to concurrent add and remove calls.
*/ */
private final ArrayList<NetworkAgentInfo> mTypeLists[]; private final ArrayList<NetworkAgentInfo>[] mTypeLists;
@NonNull @NonNull
private final ConnectivityService mService; private final ConnectivityService mService;
@@ -1096,8 +1095,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
} }
// send out another legacy broadcast - currently only used for suspend/unsuspend // send out another legacy broadcast - currently only used for suspend/unsuspend toggle
// toggle
public void update(NetworkAgentInfo nai) { public void update(NetworkAgentInfo nai) {
final boolean isDefault = mService.isDefaultNetwork(nai); final boolean isDefault = mService.isDefaultNetwork(nai);
final DetailedState state = nai.networkInfo.getDetailedState(); final DetailedState state = nai.networkInfo.getDetailedState();
@@ -1221,31 +1219,25 @@ public class ConnectivityService extends IConnectivityManager.Stub
*/ */
public void incrementCountOrThrow(final int uid) { public void incrementCountOrThrow(final int uid) {
synchronized (mUidToNetworkRequestCount) { synchronized (mUidToNetworkRequestCount) {
incrementCountOrThrow(uid, 1 /* numToIncrement */); final int newRequestCount = mUidToNetworkRequestCount.get(uid, 0) + 1;
if (newRequestCount >= mMaxCountPerUid
// HACK : the system server is allowed to go over the request count limit
// when it is creating requests on behalf of another app (but not itself,
// so it can still detect its own request leaks). This only happens in the
// per-app API flows in which case the old requests for that particular
// UID will be removed soon.
// TODO : with the removal of the legacy transact() method, exempting the
// system server UID should no longer be necessary. Make sure this is the
// case and remove this test.
&& (Process.myUid() == uid || Process.myUid() != Binder.getCallingUid())) {
throw new ServiceSpecificException(
ConnectivityManager.Errors.TOO_MANY_REQUESTS,
"Uid " + uid + " exceeded its allotted requests limit");
}
mUidToNetworkRequestCount.put(uid, newRequestCount);
} }
} }
private void incrementCountOrThrow(final int uid, final int numToIncrement) {
final int newRequestCount =
mUidToNetworkRequestCount.get(uid, 0) + numToIncrement;
if (newRequestCount >= mMaxCountPerUid
// HACK : the system server is allowed to go over the request count limit
// when it is creating requests on behalf of another app (but not itself,
// so it can still detect its own request leaks). This only happens in the
// per-app API flows in which case the old requests for that particular
// UID will be removed soon.
// TODO : instead of this hack, addPerAppDefaultNetworkRequests and other
// users of transact() should unregister the requests to decrease the count
// before they increase it again by creating a new NRI. Then remove the
// transact() method.
&& (Process.myUid() == uid || Process.myUid() != Binder.getCallingUid())) {
throw new ServiceSpecificException(
ConnectivityManager.Errors.TOO_MANY_REQUESTS,
"Uid " + uid + " exceeded its allotted requests limit");
}
mUidToNetworkRequestCount.put(uid, newRequestCount);
}
/** /**
* Decrements the request count of the given uid. * Decrements the request count of the given uid.
* *
@@ -1253,21 +1245,18 @@ public class ConnectivityService extends IConnectivityManager.Stub
*/ */
public void decrementCount(final int uid) { public void decrementCount(final int uid) {
synchronized (mUidToNetworkRequestCount) { synchronized (mUidToNetworkRequestCount) {
decrementCount(uid, 1 /* numToDecrement */); /* numToDecrement */
final int newRequestCount = mUidToNetworkRequestCount.get(uid, 0) - 1;
if (newRequestCount < 0) {
logwtf("BUG: too small request count " + newRequestCount + " for UID " + uid);
} else if (newRequestCount == 0) {
mUidToNetworkRequestCount.delete(uid);
} else {
mUidToNetworkRequestCount.put(uid, newRequestCount);
}
} }
} }
private void decrementCount(final int uid, final int numToDecrement) {
final int newRequestCount =
mUidToNetworkRequestCount.get(uid, 0) - numToDecrement;
if (newRequestCount < 0) {
logwtf("BUG: too small request count " + newRequestCount + " for UID " + uid);
} else if (newRequestCount == 0) {
mUidToNetworkRequestCount.delete(uid);
} else {
mUidToNetworkRequestCount.put(uid, newRequestCount);
}
}
} }
/** /**
@@ -1376,7 +1365,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** /**
* @see CarrierPrivilegeAuthenticator * @see CarrierPrivilegeAuthenticator
*
* This method returns null in versions before T, where carrier privilege
* authentication is not supported.
*/ */
@Nullable
public CarrierPrivilegeAuthenticator makeCarrierPrivilegeAuthenticator( public CarrierPrivilegeAuthenticator makeCarrierPrivilegeAuthenticator(
@NonNull final Context context, @NonNull final TelephonyManager tm) { @NonNull final Context context, @NonNull final TelephonyManager tm) {
if (SdkLevel.isAtLeastT()) { if (SdkLevel.isAtLeastT()) {
@@ -1396,7 +1389,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** /**
* Get the BpfNetMaps implementation to use in ConnectivityService. * Get the BpfNetMaps implementation to use in ConnectivityService.
* @param netd * @param netd a netd binder
* @return BpfNetMaps implementation. * @return BpfNetMaps implementation.
*/ */
public BpfNetMaps getBpfNetMaps(Context context, INetd netd) { public BpfNetMaps getBpfNetMaps(Context context, INetd netd) {
@@ -1581,9 +1574,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mHandler, mNetd); mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mHandler, mNetd);
mNetdCallback = new NetdCallback(); final NetdCallback netdCallback = new NetdCallback();
try { try {
mNetd.registerUnsolicitedEventListener(mNetdCallback); mNetd.registerUnsolicitedEventListener(netdCallback);
} catch (RemoteException | ServiceSpecificException e) { } catch (RemoteException | ServiceSpecificException e) {
loge("Error registering event listener :" + e); loge("Error registering event listener :" + e);
} }
@@ -1722,11 +1715,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
mHandler.sendEmptyMessage(EVENT_INGRESS_RATE_LIMIT_CHANGED); mHandler.sendEmptyMessage(EVENT_INGRESS_RATE_LIMIT_CHANGED);
} }
private void handleAlwaysOnNetworkRequest(NetworkRequest networkRequest, int id) {
final boolean enable = mContext.getResources().getBoolean(id);
handleAlwaysOnNetworkRequest(networkRequest, enable);
}
private void handleAlwaysOnNetworkRequest( private void handleAlwaysOnNetworkRequest(
NetworkRequest networkRequest, String settingName, boolean defaultValue) { NetworkRequest networkRequest, String settingName, boolean defaultValue) {
final boolean enable = toBool(Settings.Global.getInt( final boolean enable = toBool(Settings.Global.getInt(
@@ -1769,12 +1757,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), Settings.Global.getUriFor(Settings.Global.HTTP_PROXY),
EVENT_APPLY_GLOBAL_HTTP_PROXY); EVENT_APPLY_GLOBAL_HTTP_PROXY);
// Watch for whether or not to keep mobile data always on. // Watch for whether to keep mobile data always on.
mSettingsObserver.observe( mSettingsObserver.observe(
Settings.Global.getUriFor(ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON), Settings.Global.getUriFor(ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON),
EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); EVENT_CONFIGURE_ALWAYS_ON_NETWORKS);
// Watch for whether or not to keep wifi always on. // Watch for whether to keep wifi always on.
mSettingsObserver.observe( mSettingsObserver.observe(
Settings.Global.getUriFor(ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED), Settings.Global.getUriFor(ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED),
EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); EVENT_CONFIGURE_ALWAYS_ON_NETWORKS);
@@ -1804,6 +1792,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@VisibleForTesting @VisibleForTesting
@Nullable
protected NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { protected NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) {
if (network == null) { if (network == null) {
return null; return null;
@@ -1818,6 +1807,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
// TODO: determine what to do when more than one VPN applies to |uid|. // TODO: determine what to do when more than one VPN applies to |uid|.
@Nullable
private NetworkAgentInfo getVpnForUid(int uid) { private NetworkAgentInfo getVpnForUid(int uid) {
synchronized (mNetworkForNetId) { synchronized (mNetworkForNetId) {
for (int i = 0; i < mNetworkForNetId.size(); i++) { for (int i = 0; i < mNetworkForNetId.size(); i++) {
@@ -1830,6 +1820,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
return null; return null;
} }
@Nullable
private Network[] getVpnUnderlyingNetworks(int uid) { private Network[] getVpnUnderlyingNetworks(int uid) {
if (mLockdownEnabled) return null; if (mLockdownEnabled) return null;
final NetworkAgentInfo nai = getVpnForUid(uid); final NetworkAgentInfo nai = getVpnForUid(uid);
@@ -1941,6 +1932,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
* active * active
*/ */
@Override @Override
@Nullable
public NetworkInfo getActiveNetworkInfo() { public NetworkInfo getActiveNetworkInfo() {
enforceAccessPermission(); enforceAccessPermission();
final int uid = mDeps.getCallingUid(); final int uid = mDeps.getCallingUid();
@@ -1952,17 +1944,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@Nullable
public Network getActiveNetwork() { public Network getActiveNetwork() {
enforceAccessPermission(); enforceAccessPermission();
return getActiveNetworkForUidInternal(mDeps.getCallingUid(), false); return getActiveNetworkForUidInternal(mDeps.getCallingUid(), false);
} }
@Override @Override
@Nullable
public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
enforceNetworkStackPermission(mContext); enforceNetworkStackPermission(mContext);
return getActiveNetworkForUidInternal(uid, ignoreBlocked); return getActiveNetworkForUidInternal(uid, ignoreBlocked);
} }
@Nullable
private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) { private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) {
final NetworkAgentInfo vpnNai = getVpnForUid(uid); final NetworkAgentInfo vpnNai = getVpnForUid(uid);
if (vpnNai != null) { if (vpnNai != null) {
@@ -1981,6 +1976,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@Nullable
public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
enforceNetworkStackPermission(mContext); enforceNetworkStackPermission(mContext);
final NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid); final NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid);
@@ -2017,6 +2013,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@Nullable
public NetworkInfo getNetworkInfo(int networkType) { public NetworkInfo getNetworkInfo(int networkType) {
enforceAccessPermission(); enforceAccessPermission();
final int uid = mDeps.getCallingUid(); final int uid = mDeps.getCallingUid();
@@ -2035,6 +2032,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@Nullable
public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
enforceAccessPermission(); enforceAccessPermission();
final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
@@ -2057,6 +2055,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@Nullable
public Network getNetworkForType(int networkType) { public Network getNetworkForType(int networkType) {
enforceAccessPermission(); enforceAccessPermission();
if (!mLegacyTypeTracker.isTypeSupported(networkType)) { if (!mLegacyTypeTracker.isTypeSupported(networkType)) {
@@ -2074,6 +2073,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
@NonNull
public Network[] getAllNetworks() { public Network[] getAllNetworks() {
enforceAccessPermission(); enforceAccessPermission();
synchronized (mNetworkForNetId) { synchronized (mNetworkForNetId) {
@@ -2534,7 +2534,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
snapshot.getNetwork(), snapshot.getSubscriberId())); snapshot.getNetwork(), snapshot.getSubscriberId()));
} }
} }
return result.toArray(new NetworkState[result.size()]); return result.toArray(new NetworkState[0]);
} }
@Override @Override
@@ -2632,7 +2632,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
try { try {
addr = InetAddress.getByAddress(hostAddress); addr = InetAddress.getByAddress(hostAddress);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
if (DBG) log("requestRouteToHostAddress got " + e.toString()); if (DBG) log("requestRouteToHostAddress got " + e);
return false; return false;
} }
@@ -2643,7 +2643,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
if (nai == null) { if (nai == null) {
if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { if (!mLegacyTypeTracker.isTypeSupported(networkType)) {
if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType);
} else { } else {
if (DBG) log("requestRouteToHostAddress on down network: " + networkType); if (DBG) log("requestRouteToHostAddress on down network: " + networkType);
@@ -2736,7 +2736,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// the caller thread of registerNetworkAgent. Thus, it's not allowed to register netd // the caller thread of registerNetworkAgent. Thus, it's not allowed to register netd
// event callback for certain nai. e.g. cellular. Register here to pass to // event callback for certain nai. e.g. cellular. Register here to pass to
// NetworkMonitor instead. // NetworkMonitor instead.
// TODO: Move the Dns Event to NetworkMonitor. NetdEventListenerService only allow one // TODO: Move the Dns Event to NetworkMonitor. NetdEventListenerService only allows one
// callback from each caller type. Need to re-factor NetdEventListenerService to allow // callback from each caller type. Need to re-factor NetdEventListenerService to allow
// multiple NetworkMonitor registrants. // multiple NetworkMonitor registrants.
if (nai != null && nai.satisfies(mDefaultRequest.mRequests.get(0))) { if (nai != null && nai.satisfies(mDefaultRequest.mRequests.get(0))) {
@@ -3101,8 +3101,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS)); mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS));
// Update mobile data preference if necessary. // Update mobile data preference if necessary.
// Note that empty uid list can be skip here only because no uid rules applied before system // Note that updating can be skipped here if the list is empty only because no uid
// ready. Normally, the empty uid list means to clear the uids rules on netd. // rules are applied before system ready. Normally, the empty uid list means to clear
// the uids rules on netd.
if (!ConnectivitySettingsManager.getMobileDataPreferredUids(mContext).isEmpty()) { if (!ConnectivitySettingsManager.getMobileDataPreferredUids(mContext).isEmpty()) {
updateMobileDataPreferredUids(); updateMobileDataPreferredUids();
} }
@@ -3216,7 +3217,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { private void dumpNetworkDiagnostics(IndentingPrintWriter pw) {
final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); final List<NetworkDiagnostics> netDiags = new ArrayList<>();
final long DIAG_TIME_MS = 5000; final long DIAG_TIME_MS = 5000;
for (NetworkAgentInfo nai : networksSortedById()) { for (NetworkAgentInfo nai : networksSortedById()) {
PrivateDnsConfig privateDnsCfg = mDnsManager.getPrivateDnsConfig(nai.network); PrivateDnsConfig privateDnsCfg = mDnsManager.getPrivateDnsConfig(nai.network);
@@ -3608,7 +3609,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
final NetworkCapabilities sanitized = final NetworkCapabilities sanitized =
nai.getDeclaredCapabilitiesSanitized(mCarrierPrivilegeAuthenticator); nai.getDeclaredCapabilitiesSanitized(mCarrierPrivilegeAuthenticator);
maybeUpdateWifiRoamTimestamp(nai, sanitized); maybeUpdateWifiRoamTimestamp(nai, sanitized);
updateCapabilities(nai.getCurrentScore(), nai, sanitized); updateCapabilities(nai.getScore(), nai, sanitized);
break; break;
} }
case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: {
@@ -3876,7 +3877,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
log(nai.toShortString() + " validation " + (valid ? "passed" : "failed") + logMsg); log(nai.toShortString() + " validation " + (valid ? "passed" : "failed") + logMsg);
} }
if (valid != nai.lastValidated) { if (valid != nai.lastValidated) {
final int oldScore = nai.getCurrentScore(); final FullScore oldScore = nai.getScore();
nai.lastValidated = valid; nai.lastValidated = valid;
nai.everValidated |= valid; nai.everValidated |= valid;
updateCapabilities(oldScore, nai, nai.networkCapabilities); updateCapabilities(oldScore, nai, nai.networkCapabilities);
@@ -3952,7 +3953,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(@NonNull Message msg) {
if (!maybeHandleNetworkMonitorMessage(msg) if (!maybeHandleNetworkMonitorMessage(msg)
&& !maybeHandleNetworkAgentInfoMessage(msg)) { && !maybeHandleNetworkAgentInfoMessage(msg)) {
maybeHandleNetworkAgentMessage(msg); maybeHandleNetworkAgentMessage(msg);
@@ -4421,12 +4422,14 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
config = new NativeNetworkConfig(nai.network.getNetId(), NativeNetworkType.VIRTUAL, config = new NativeNetworkConfig(nai.network.getNetId(), NativeNetworkType.VIRTUAL,
INetd.PERMISSION_NONE, INetd.PERMISSION_NONE,
(nai.networkAgentConfig == null || !nai.networkAgentConfig.allowBypass), !nai.networkAgentConfig.allowBypass /* secure */,
getVpnType(nai), nai.networkAgentConfig.excludeLocalRouteVpn); getVpnType(nai), nai.networkAgentConfig.excludeLocalRouteVpn);
} else { } else {
config = new NativeNetworkConfig(nai.network.getNetId(), NativeNetworkType.PHYSICAL, config = new NativeNetworkConfig(nai.network.getNetId(), NativeNetworkType.PHYSICAL,
getNetworkPermission(nai.networkCapabilities), /*secure=*/ false, getNetworkPermission(nai.networkCapabilities),
VpnManager.TYPE_VPN_NONE, /*excludeLocalRoutes=*/ false); false /* secure */,
VpnManager.TYPE_VPN_NONE,
false /* excludeLocalRoutes */);
} }
mNetd.networkCreate(config); mNetd.networkCreate(config);
mDnsResolver.createNetworkCache(nai.network.getNetId()); mDnsResolver.createNetworkCache(nai.network.getNetId());
@@ -7259,8 +7262,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
* later : see {@link #updateLinkProperties}. * later : see {@link #updateLinkProperties}.
* @param networkCapabilities the initial capabilites of this network. They can be updated * @param networkCapabilities the initial capabilites of this network. They can be updated
* later : see {@link #updateCapabilities}. * later : see {@link #updateCapabilities}.
* @param initialScore the initial score of the network. See * @param initialScore the initial score of the network. See {@link NetworkAgentInfo#getScore}.
* {@link NetworkAgentInfo#getCurrentScore}.
* @param networkAgentConfig metadata about the network. This is never updated. * @param networkAgentConfig metadata about the network. This is never updated.
* @param providerId the ID of the provider owning this NetworkAgent. * @param providerId the ID of the provider owning this NetworkAgent.
* @return the network created for this agent. * @return the network created for this agent.
@@ -7295,18 +7297,23 @@ public class ConnectivityService extends IConnectivityManager.Stub
NetworkScore currentScore, NetworkAgentConfig networkAgentConfig, int providerId, NetworkScore currentScore, NetworkAgentConfig networkAgentConfig, int providerId,
int uid) { int uid) {
// Make a copy of the passed NI, LP, NC as the caller may hold a reference to them
// and mutate them at any time.
final NetworkInfo niCopy = new NetworkInfo(networkInfo);
final NetworkCapabilities ncCopy = new NetworkCapabilities(networkCapabilities);
final LinkProperties lpCopy = new LinkProperties(linkProperties);
// At this point the capabilities/properties are untrusted and unverified, e.g. checks that // At this point the capabilities/properties are untrusted and unverified, e.g. checks that
// the capabilities' access UID comply with security limitations. They will be sanitized // the capabilities' access UIDs comply with security limitations. They will be sanitized
// as the NAI registration finishes, in handleRegisterNetworkAgent(). This is // as the NAI registration finishes, in handleRegisterNetworkAgent(). This is
// because some of the checks must happen on the handler thread. // because some of the checks must happen on the handler thread.
final NetworkAgentInfo nai = new NetworkAgentInfo(na, final NetworkAgentInfo nai = new NetworkAgentInfo(na,
new Network(mNetIdManager.reserveNetId()), new NetworkInfo(networkInfo), new Network(mNetIdManager.reserveNetId()), niCopy, lpCopy, ncCopy,
linkProperties, networkCapabilities,
currentScore, mContext, mTrackerHandler, new NetworkAgentConfig(networkAgentConfig), currentScore, mContext, mTrackerHandler, new NetworkAgentConfig(networkAgentConfig),
this, mNetd, mDnsResolver, providerId, uid, mLingerDelayMs, this, mNetd, mDnsResolver, providerId, uid, mLingerDelayMs,
mQosCallbackTracker, mDeps); mQosCallbackTracker, mDeps);
final String extraInfo = networkInfo.getExtraInfo(); final String extraInfo = niCopy.getExtraInfo();
final String name = TextUtils.isEmpty(extraInfo) final String name = TextUtils.isEmpty(extraInfo)
? nai.networkCapabilities.getSsid() : extraInfo; ? nai.networkCapabilities.getSsid() : extraInfo;
if (DBG) log("registerNetworkAgent " + nai); if (DBG) log("registerNetworkAgent " + nai);
@@ -7321,19 +7328,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) { private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) {
if (VDBG) log("Network Monitor created for " + nai); if (VDBG) log("Network Monitor created for " + nai);
// nai.nc and nai.lp are the same object that was passed by the network agent if the agent
// lives in the same process as this code (e.g. wifi), so make sure this code doesn't
// mutate their object. TODO : make this copy much earlier to avoid them mutating it
// while the network monitor is starting.
final LinkProperties lp = new LinkProperties(nai.linkProperties);
// Store a copy of the declared capabilities. // Store a copy of the declared capabilities.
nai.setDeclaredCapabilities(nai.networkCapabilities); nai.setDeclaredCapabilities(nai.networkCapabilities);
// Make sure the LinkProperties and NetworkCapabilities reflect what the agent info said. // Make sure the LinkProperties and NetworkCapabilities reflect what the agent info said.
final NetworkCapabilities sanitized = nai.getAndSetNetworkCapabilities(mixInCapabilities(nai,
nai.getDeclaredCapabilitiesSanitized(mCarrierPrivilegeAuthenticator); nai.getDeclaredCapabilitiesSanitized(mCarrierPrivilegeAuthenticator)));
nai.getAndSetNetworkCapabilities(mixInCapabilities(nai, sanitized)); processLinkPropertiesFromAgent(nai, nai.linkProperties);
processLinkPropertiesFromAgent(nai, lp);
nai.linkProperties = lp;
nai.onNetworkMonitorCreated(networkMonitor); nai.onNetworkMonitorCreated(networkMonitor);
@@ -7961,7 +7961,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
* @param nai the network having its capabilities updated. * @param nai the network having its capabilities updated.
* @param nc the new network capabilities. * @param nc the new network capabilities.
*/ */
private void updateCapabilities(final int oldScore, @NonNull final NetworkAgentInfo nai, private void updateCapabilities(final FullScore oldScore, @NonNull final NetworkAgentInfo nai,
@NonNull final NetworkCapabilities nc) { @NonNull final NetworkCapabilities nc) {
NetworkCapabilities newNc = mixInCapabilities(nai, nc); NetworkCapabilities newNc = mixInCapabilities(nai, nc);
if (Objects.equals(nai.networkCapabilities, newNc)) return; if (Objects.equals(nai.networkCapabilities, newNc)) return;
@@ -7972,7 +7972,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
updateAllowedUids(nai, prevNc, newNc); updateAllowedUids(nai, prevNc, newNc);
nai.updateScoreForNetworkAgentUpdate(); nai.updateScoreForNetworkAgentUpdate();
if (nai.getCurrentScore() == oldScore && newNc.equalRequestableCapabilities(prevNc)) { if (nai.getScore().equals(oldScore) && newNc.equalRequestableCapabilities(prevNc)) {
// If the requestable capabilities haven't changed, and the score hasn't changed, then // If the requestable capabilities haven't changed, and the score hasn't changed, then
// the change we're processing can't affect any requests, it can only affect the listens // the change we're processing can't affect any requests, it can only affect the listens
// on this network. We might have been called by rematchNetworkAndRequests when a // on this network. We might have been called by rematchNetworkAndRequests when a
@@ -8016,7 +8016,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** Convenience method to update the capabilities for a given network. */ /** Convenience method to update the capabilities for a given network. */
private void updateCapabilitiesForNetwork(NetworkAgentInfo nai) { private void updateCapabilitiesForNetwork(NetworkAgentInfo nai) {
updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); updateCapabilities(nai.getScore(), nai, nai.networkCapabilities);
} }
/** /**
@@ -8326,8 +8326,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
releasePendingNetworkRequestWithDelay(pendingIntent); releasePendingNetworkRequestWithDelay(pendingIntent);
} }
// networkAgent is only allowed to be null if notificationType is
// CALLBACK_UNAVAIL. This is because UNAVAIL is about no network being
// available, while all other cases are about some particular network.
private void callCallbackForRequest(@NonNull final NetworkRequestInfo nri, private void callCallbackForRequest(@NonNull final NetworkRequestInfo nri,
@NonNull final NetworkAgentInfo networkAgent, final int notificationType, @Nullable final NetworkAgentInfo networkAgent, final int notificationType,
final int arg1) { final int arg1) {
if (nri.mMessenger == null) { if (nri.mMessenger == null) {
// Default request has no msgr. Also prevents callbacks from being invoked for // Default request has no msgr. Also prevents callbacks from being invoked for
@@ -8349,14 +8352,13 @@ public class ConnectivityService extends IConnectivityManager.Stub
switch (notificationType) { switch (notificationType) {
case ConnectivityManager.CALLBACK_AVAILABLE: { case ConnectivityManager.CALLBACK_AVAILABLE: {
final NetworkCapabilities nc = final NetworkCapabilities nc =
networkCapabilitiesRestrictedForCallerPermissions(
networkAgent.networkCapabilities, nri.mPid, nri.mUid);
putParcelable(
bundle,
createWithLocationInfoSanitizedIfNecessaryWhenParceled( createWithLocationInfoSanitizedIfNecessaryWhenParceled(
nc, includeLocationSensitiveInfo, nri.mPid, nri.mUid, networkCapabilitiesRestrictedForCallerPermissions(
networkAgent.networkCapabilities, nri.mPid, nri.mUid),
includeLocationSensitiveInfo, nri.mPid, nri.mUid,
nrForCallback.getRequestorPackageName(), nrForCallback.getRequestorPackageName(),
nri.mCallingAttributionTag)); nri.mCallingAttributionTag);
putParcelable(bundle, nc);
putParcelable(bundle, linkPropertiesRestrictedForCallerPermissions( putParcelable(bundle, linkPropertiesRestrictedForCallerPermissions(
networkAgent.linkProperties, nri.mPid, nri.mUid)); networkAgent.linkProperties, nri.mPid, nri.mUid));
// For this notification, arg1 contains the blocked status. // For this notification, arg1 contains the blocked status.
@@ -8810,15 +8812,22 @@ public class ConnectivityService extends IConnectivityManager.Stub
@NonNull final Set<NetworkRequestInfo> networkRequests) { @NonNull final Set<NetworkRequestInfo> networkRequests) {
ensureRunningOnConnectivityServiceThread(); ensureRunningOnConnectivityServiceThread();
// TODO: This may be slow, and should be optimized. // TODO: This may be slow, and should be optimized.
final long now = SystemClock.elapsedRealtime(); final long start = SystemClock.elapsedRealtime();
final NetworkReassignment changes = computeNetworkReassignment(networkRequests); final NetworkReassignment changes = computeNetworkReassignment(networkRequests);
final long computed = SystemClock.elapsedRealtime();
applyNetworkReassignment(changes, start);
final long applied = SystemClock.elapsedRealtime();
issueNetworkNeeds();
final long end = SystemClock.elapsedRealtime();
if (VDBG || DDBG) { if (VDBG || DDBG) {
log(String.format("Rematched networks [computed %dms] [applied %dms] [issued %d]",
computed - start, applied - computed, end - applied));
log(changes.debugString()); log(changes.debugString());
} else if (DBG) { } else if (DBG) {
log(changes.toString()); // Shorter form, only one line of log // Shorter form, only one line of log
log(String.format("%s [c %d] [a %d] [i %d]", changes.toString(),
computed - start, applied - computed, end - applied));
} }
applyNetworkReassignment(changes, now);
issueNetworkNeeds();
} }
private void applyNetworkReassignment(@NonNull final NetworkReassignment changes, private void applyNetworkReassignment(@NonNull final NetworkReassignment changes,
@@ -9490,9 +9499,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
final UnderlyingNetworkInfo[] underlyingNetworkInfos = getAllVpnInfo(); final UnderlyingNetworkInfo[] underlyingNetworkInfos = getAllVpnInfo();
try { try {
final ArrayList<NetworkStateSnapshot> snapshots = new ArrayList<>(); final ArrayList<NetworkStateSnapshot> snapshots = new ArrayList<>();
for (final NetworkStateSnapshot snapshot : getAllNetworkStateSnapshots()) { snapshots.addAll(getAllNetworkStateSnapshots());
snapshots.add(snapshot);
}
mStatsManager.notifyNetworkStatus(getDefaultNetworks(), mStatsManager.notifyNetworkStatus(getDefaultNetworks(),
snapshots, activeIface, Arrays.asList(underlyingNetworkInfos)); snapshots, activeIface, Arrays.asList(underlyingNetworkInfos));
} catch (Exception ignored) { } catch (Exception ignored) {
@@ -9879,14 +9886,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
private static class NetworkTestedResults { private static class NetworkTestedResults {
private final int mNetId; private final int mNetId;
private final int mTestResult; private final int mTestResult;
private final long mTimestampMillis;
@Nullable private final String mRedirectUrl; @Nullable private final String mRedirectUrl;
private NetworkTestedResults( private NetworkTestedResults(
int netId, int testResult, long timestampMillis, @Nullable String redirectUrl) { int netId, int testResult, long timestampMillis, @Nullable String redirectUrl) {
mNetId = netId; mNetId = netId;
mTestResult = testResult; mTestResult = testResult;
mTimestampMillis = timestampMillis;
mRedirectUrl = redirectUrl; mRedirectUrl = redirectUrl;
} }
} }
@@ -10310,14 +10315,14 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
public void onInterfaceLinkStateChanged(String iface, boolean up) { public void onInterfaceLinkStateChanged(@NonNull String iface, boolean up) {
for (NetworkAgentInfo nai : mNetworkAgentInfos) { for (NetworkAgentInfo nai : mNetworkAgentInfos) {
nai.clatd.interfaceLinkStateChanged(iface, up); nai.clatd.interfaceLinkStateChanged(iface, up);
} }
} }
@Override @Override
public void onInterfaceRemoved(String iface) { public void onInterfaceRemoved(@NonNull String iface) {
for (NetworkAgentInfo nai : mNetworkAgentInfos) { for (NetworkAgentInfo nai : mNetworkAgentInfos) {
nai.clatd.interfaceRemoved(iface); nai.clatd.interfaceRemoved(iface);
} }
@@ -10340,10 +10345,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
@GuardedBy("mActiveIdleTimers") @GuardedBy("mActiveIdleTimers")
private boolean mNetworkActive; private boolean mNetworkActive;
@GuardedBy("mActiveIdleTimers") @GuardedBy("mActiveIdleTimers")
private final ArrayMap<String, IdleTimerParams> mActiveIdleTimers = new ArrayMap(); private final ArrayMap<String, IdleTimerParams> mActiveIdleTimers = new ArrayMap<>();
private final Handler mHandler; private final Handler mHandler;
private class IdleTimerParams { private static class IdleTimerParams {
public final int timeout; public final int timeout;
public final int transportType; public final int transportType;
@@ -10389,7 +10394,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
try { try {
mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive(); mNetworkActivityListeners.getBroadcastItem(i).onNetworkActive();
} catch (RemoteException | RuntimeException e) { } catch (RemoteException | RuntimeException e) {
loge("Fail to send network activie to listener " + e); loge("Fail to send network activity to listener " + e);
} }
} }
} finally { } finally {
@@ -10610,8 +10615,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
@VisibleForTesting @VisibleForTesting
public void registerQosCallbackInternal(@NonNull final QosFilter filter, public void registerQosCallbackInternal(@NonNull final QosFilter filter,
@NonNull final IQosCallback callback, @NonNull final NetworkAgentInfo nai) { @NonNull final IQosCallback callback, @NonNull final NetworkAgentInfo nai) {
if (filter == null) throw new IllegalArgumentException("filter must be non-null"); Objects.requireNonNull(filter, "filter must be non-null");
if (callback == null) throw new IllegalArgumentException("callback must be non-null"); Objects.requireNonNull(callback, "callback must be non-null");
if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) {
// TODO: Check allowed list here and ensure that either a) any QoS callback registered // TODO: Check allowed list here and ensure that either a) any QoS callback registered
@@ -10689,8 +10694,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
+ "or the device owner must be set. "); + "or the device owner must be set. ");
} }
final List<ProfileNetworkPreferenceList.Preference> preferenceList = final List<ProfileNetworkPreferenceList.Preference> preferenceList = new ArrayList<>();
new ArrayList<ProfileNetworkPreferenceList.Preference>();
boolean hasDefaultPreference = false; boolean hasDefaultPreference = false;
for (final ProfileNetworkPreference preference : preferences) { for (final ProfileNetworkPreference preference : preferences) {
final NetworkCapabilities nc; final NetworkCapabilities nc;
@@ -10771,7 +10775,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
uidRangeSet = UidRangeUtils.removeRangeSetFromUidRange(profileUids, uidRangeSet = UidRangeUtils.removeRangeSetFromUidRange(profileUids,
disallowUidRangeSet); disallowUidRangeSet);
} else { } else {
uidRangeSet = new ArraySet<UidRange>(); uidRangeSet = new ArraySet<>();
uidRangeSet.add(profileUids); uidRangeSet.add(profileUids);
} }
} }
@@ -10780,8 +10784,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private boolean isEnterpriseIdentifierValid( private boolean isEnterpriseIdentifierValid(
@NetworkCapabilities.EnterpriseId int identifier) { @NetworkCapabilities.EnterpriseId int identifier) {
if ((identifier >= NET_ENTERPRISE_ID_1) if ((identifier >= NET_ENTERPRISE_ID_1) && (identifier <= NET_ENTERPRISE_ID_5)) {
&& (identifier <= NET_ENTERPRISE_ID_5)) {
return true; return true;
} }
return false; return false;

View File

@@ -49,10 +49,6 @@ import java.util.StringJoiner;
public class FullScore { public class FullScore {
private static final String TAG = FullScore.class.getSimpleName(); private static final String TAG = FullScore.class.getSimpleName();
// This will be removed soon. Do *NOT* depend on it for any new code that is not part of
// a migration.
private final int mLegacyInt;
/** @hide */ /** @hide */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = {"POLICY_"}, value = { @IntDef(prefix = {"POLICY_"}, value = {
@@ -146,9 +142,7 @@ public class FullScore {
private final int mKeepConnectedReason; private final int mKeepConnectedReason;
FullScore(final int legacyInt, final long policies, FullScore(final long policies, @KeepConnectedReason final int keepConnectedReason) {
@KeepConnectedReason final int keepConnectedReason) {
mLegacyInt = legacyInt;
mPolicies = policies; mPolicies = policies;
mKeepConnectedReason = keepConnectedReason; mKeepConnectedReason = keepConnectedReason;
} }
@@ -170,7 +164,7 @@ public class FullScore {
public static FullScore fromNetworkScore(@NonNull final NetworkScore score, public static FullScore fromNetworkScore(@NonNull final NetworkScore score,
@NonNull final NetworkCapabilities caps, @NonNull final NetworkAgentConfig config, @NonNull final NetworkCapabilities caps, @NonNull final NetworkAgentConfig config,
final boolean everValidated, final boolean yieldToBadWiFi, final boolean destroyed) { final boolean everValidated, final boolean yieldToBadWiFi, final boolean destroyed) {
return withPolicies(score.getLegacyInt(), score.getPolicies(), return withPolicies(score.getPolicies(),
score.getKeepConnectedReason(), score.getKeepConnectedReason(),
caps.hasCapability(NET_CAPABILITY_VALIDATED), caps.hasCapability(NET_CAPABILITY_VALIDATED),
caps.hasTransport(TRANSPORT_VPN), caps.hasTransport(TRANSPORT_VPN),
@@ -216,7 +210,7 @@ public class FullScore {
// A prospective score is invincible if the legacy int in the filter is over the maximum // A prospective score is invincible if the legacy int in the filter is over the maximum
// score. // score.
final boolean invincible = score.getLegacyInt() > NetworkRanker.LEGACY_INT_MAX; final boolean invincible = score.getLegacyInt() > NetworkRanker.LEGACY_INT_MAX;
return withPolicies(score.getLegacyInt(), score.getPolicies(), KEEP_CONNECTED_NONE, return withPolicies(score.getPolicies(), KEEP_CONNECTED_NONE,
mayValidate, vpn, unmetered, everValidated, everUserSelected, acceptUnvalidated, mayValidate, vpn, unmetered, everValidated, everUserSelected, acceptUnvalidated,
yieldToBadWiFi, destroyed, invincible); yieldToBadWiFi, destroyed, invincible);
} }
@@ -236,7 +230,7 @@ public class FullScore {
final boolean everValidated, final boolean everValidated,
final boolean yieldToBadWifi, final boolean yieldToBadWifi,
final boolean destroyed) { final boolean destroyed) {
return withPolicies(mLegacyInt, mPolicies, mKeepConnectedReason, return withPolicies(mPolicies, mKeepConnectedReason,
caps.hasCapability(NET_CAPABILITY_VALIDATED), caps.hasCapability(NET_CAPABILITY_VALIDATED),
caps.hasTransport(TRANSPORT_VPN), caps.hasTransport(TRANSPORT_VPN),
caps.hasCapability(NET_CAPABILITY_NOT_METERED), caps.hasCapability(NET_CAPABILITY_NOT_METERED),
@@ -251,8 +245,7 @@ public class FullScore {
// TODO : this shouldn't manage bad wifi avoidance instead this should be done by the // TODO : this shouldn't manage bad wifi avoidance instead this should be done by the
// telephony factory, so that it depends on the carrier. For now this is handled by // telephony factory, so that it depends on the carrier. For now this is handled by
// connectivity for backward compatibility. // connectivity for backward compatibility.
private static FullScore withPolicies(@NonNull final int legacyInt, private static FullScore withPolicies(final long externalPolicies,
final long externalPolicies,
@KeepConnectedReason final int keepConnectedReason, @KeepConnectedReason final int keepConnectedReason,
final boolean isValidated, final boolean isValidated,
final boolean isVpn, final boolean isVpn,
@@ -263,7 +256,7 @@ public class FullScore {
final boolean yieldToBadWiFi, final boolean yieldToBadWiFi,
final boolean destroyed, final boolean destroyed,
final boolean invincible) { final boolean invincible) {
return new FullScore(legacyInt, (externalPolicies & EXTERNAL_POLICIES_MASK) return new FullScore((externalPolicies & EXTERNAL_POLICIES_MASK)
| (isValidated ? 1L << POLICY_IS_VALIDATED : 0) | (isValidated ? 1L << POLICY_IS_VALIDATED : 0)
| (isVpn ? 1L << POLICY_IS_VPN : 0) | (isVpn ? 1L << POLICY_IS_VPN : 0)
| (isUnmetered ? 1L << POLICY_IS_UNMETERED : 0) | (isUnmetered ? 1L << POLICY_IS_UNMETERED : 0)
@@ -280,8 +273,7 @@ public class FullScore {
* Returns this score but with the specified yield to bad wifi policy. * Returns this score but with the specified yield to bad wifi policy.
*/ */
public FullScore withYieldToBadWiFi(final boolean newYield) { public FullScore withYieldToBadWiFi(final boolean newYield) {
return new FullScore(mLegacyInt, return new FullScore(newYield ? mPolicies | (1L << POLICY_YIELD_TO_BAD_WIFI)
newYield ? mPolicies | (1L << POLICY_YIELD_TO_BAD_WIFI)
: mPolicies & ~(1L << POLICY_YIELD_TO_BAD_WIFI), : mPolicies & ~(1L << POLICY_YIELD_TO_BAD_WIFI),
mKeepConnectedReason); mKeepConnectedReason);
} }
@@ -290,49 +282,7 @@ public class FullScore {
* Returns this score but validated. * Returns this score but validated.
*/ */
public FullScore asValidated() { public FullScore asValidated() {
return new FullScore(mLegacyInt, mPolicies | (1L << POLICY_IS_VALIDATED), return new FullScore(mPolicies | (1L << POLICY_IS_VALIDATED), mKeepConnectedReason);
mKeepConnectedReason);
}
/**
* For backward compatibility, get the legacy int.
* This will be removed before S is published.
*/
public int getLegacyInt() {
return getLegacyInt(false /* pretendValidated */);
}
public int getLegacyIntAsValidated() {
return getLegacyInt(true /* pretendValidated */);
}
// TODO : remove these two constants
// Penalty applied to scores of Networks that have not been validated.
private static final int UNVALIDATED_SCORE_PENALTY = 40;
// Score for a network that can be used unvalidated
private static final int ACCEPT_UNVALIDATED_NETWORK_SCORE = 100;
private int getLegacyInt(boolean pretendValidated) {
// If the user has chosen this network at least once, give it the maximum score when
// checking to pretend it's validated, or if it doesn't need to validate because the
// user said to use it even if it doesn't validate.
// This ensures that networks that have been selected in UI are not torn down before the
// user gets a chance to prefer it when a higher-scoring network (e.g., Ethernet) is
// available.
if (hasPolicy(POLICY_EVER_USER_SELECTED)
&& (hasPolicy(POLICY_ACCEPT_UNVALIDATED) || pretendValidated)) {
return ACCEPT_UNVALIDATED_NETWORK_SCORE;
}
int score = mLegacyInt;
// Except for VPNs, networks are subject to a penalty for not being validated.
// Apply the penalty unless the network is a VPN, or it's validated or pretending to be.
if (!hasPolicy(POLICY_IS_VALIDATED) && !pretendValidated && !hasPolicy(POLICY_IS_VPN)) {
score -= UNVALIDATED_SCORE_PENALTY;
}
if (score < 0) score = 0;
return score;
} }
/** /**
@@ -350,15 +300,32 @@ public class FullScore {
return mKeepConnectedReason; return mKeepConnectedReason;
} }
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final FullScore fullScore = (FullScore) o;
if (mPolicies != fullScore.mPolicies) return false;
return mKeepConnectedReason == fullScore.mKeepConnectedReason;
}
@Override
public int hashCode() {
return 2 * ((int) mPolicies)
+ 3 * (int) (mPolicies >>> 32)
+ 5 * mKeepConnectedReason;
}
// Example output : // Example output :
// Score(50 ; Policies : EVER_USER_SELECTED&IS_VALIDATED) // Score(Policies : EVER_USER_SELECTED&IS_VALIDATED ; KeepConnected : )
@Override @Override
public String toString() { public String toString() {
final StringJoiner sj = new StringJoiner( final StringJoiner sj = new StringJoiner(
"&", // delimiter "&", // delimiter
"Score(" + mLegacyInt + " ; KeepConnected : " + mKeepConnectedReason "Score(Policies : ", // prefix
+ " ; Policies : ", // prefix " ; KeepConnected : " + mKeepConnectedReason + ")"); // suffix
")"); // suffix
for (int i = NetworkScore.MIN_AGENT_MANAGED_POLICY; for (int i = NetworkScore.MIN_AGENT_MANAGED_POLICY;
i <= NetworkScore.MAX_AGENT_MANAGED_POLICY; ++i) { i <= NetworkScore.MAX_AGENT_MANAGED_POLICY; ++i) {
if (hasPolicy(i)) sj.add(policyNameOf(i)); if (hasPolicy(i)) sj.add(policyNameOf(i));

View File

@@ -67,6 +67,7 @@ import com.android.modules.utils.build.SdkLevel;
import com.android.server.ConnectivityService; import com.android.server.ConnectivityService;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -105,7 +106,7 @@ import java.util.TreeSet;
// for example: // for example:
// a. a captive portal is present, or // a. a captive portal is present, or
// b. a WiFi router whose Internet backhaul is down, or // b. a WiFi router whose Internet backhaul is down, or
// c. a wireless connection stops transfering packets temporarily (e.g. device is in elevator // c. a wireless connection stops transferring packets temporarily (e.g. device is in elevator
// or tunnel) but does not disconnect from the AP/cell tower, or // or tunnel) but does not disconnect from the AP/cell tower, or
// d. a stand-alone device offering a WiFi AP without an uplink for configuration purposes. // d. a stand-alone device offering a WiFi AP without an uplink for configuration purposes.
// 5. registered, created, connected, validated // 5. registered, created, connected, validated
@@ -158,7 +159,7 @@ import java.util.TreeSet;
// the network is no longer considered "lingering". After the linger timer expires, if the network // the network is no longer considered "lingering". After the linger timer expires, if the network
// is satisfying one or more background NetworkRequests it is kept up in the background. If it is // is satisfying one or more background NetworkRequests it is kept up in the background. If it is
// not, ConnectivityService disconnects the NetworkAgent's AsyncChannel. // not, ConnectivityService disconnects the NetworkAgent's AsyncChannel.
public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRanker.Scoreable { public class NetworkAgentInfo implements NetworkRanker.Scoreable {
@NonNull public NetworkInfo networkInfo; @NonNull public NetworkInfo networkInfo;
// This Network object should always be used if possible, so as to encourage reuse of the // This Network object should always be used if possible, so as to encourage reuse of the
@@ -417,6 +418,8 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
private final Handler mHandler; private final Handler mHandler;
private final QosCallbackTracker mQosCallbackTracker; private final QosCallbackTracker mQosCallbackTracker;
private final long mCreationTime;
public NetworkAgentInfo(INetworkAgent na, Network net, NetworkInfo info, public NetworkAgentInfo(INetworkAgent na, Network net, NetworkInfo info,
@NonNull LinkProperties lp, @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, @NonNull NetworkCapabilities nc,
@NonNull NetworkScore score, Context context, @NonNull NetworkScore score, Context context,
@@ -449,6 +452,7 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
declaredUnderlyingNetworks = (nc.getUnderlyingNetworks() != null) declaredUnderlyingNetworks = (nc.getUnderlyingNetworks() != null)
? nc.getUnderlyingNetworks().toArray(new Network[0]) ? nc.getUnderlyingNetworks().toArray(new Network[0])
: null; : null;
mCreationTime = System.currentTimeMillis();
} }
private class AgentDeathMonitor implements IBinder.DeathRecipient { private class AgentDeathMonitor implements IBinder.DeathRecipient {
@@ -1014,18 +1018,6 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
return mScore; return mScore;
} }
// Get the current score for this Network. This may be modified from what the
// NetworkAgent sent, as it has modifiers applied to it.
public int getCurrentScore() {
return mScore.getLegacyInt();
}
// Get the current score for this Network as if it was validated. This may be modified from
// what the NetworkAgent sent, as it has modifiers applied to it.
public int getCurrentScoreAsValidated() {
return mScore.getLegacyIntAsValidated();
}
/** /**
* Mix-in the ConnectivityService-managed bits in the score. * Mix-in the ConnectivityService-managed bits in the score.
*/ */
@@ -1330,6 +1322,7 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
return "NetworkAgentInfo{" return "NetworkAgentInfo{"
+ "network{" + network + "} handle{" + network.getNetworkHandle() + "} ni{" + "network{" + network + "} handle{" + network.getNetworkHandle() + "} ni{"
+ networkInfo.toShortString() + "} " + networkInfo.toShortString() + "} "
+ "created=" + Instant.ofEpochMilli(mCreationTime) + " "
+ mScore + " " + mScore + " "
+ (created ? " created" : "") + (created ? " created" : "")
+ (destroyed ? " destroyed" : "") + (destroyed ? " destroyed" : "")
@@ -1363,12 +1356,6 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo>, NetworkRa
+ transportNamesOf(networkCapabilities.getTransportTypes()) + "]"; + transportNamesOf(networkCapabilities.getTransportTypes()) + "]";
} }
// Enables sorting in descending order of score.
@Override
public int compareTo(NetworkAgentInfo other) {
return other.getCurrentScore() - getCurrentScore();
}
/** /**
* Null-guarding version of NetworkAgentInfo#toShortString() * Null-guarding version of NetworkAgentInfo#toShortString()
*/ */

View File

@@ -18,6 +18,8 @@ package com.android.server.connectivity
import android.net.NetworkAgentConfig import android.net.NetworkAgentConfig
import android.net.NetworkCapabilities import android.net.NetworkCapabilities
import android.net.NetworkScore
import android.net.NetworkScore.KEEP_CONNECTED_FOR_HANDOVER
import android.net.NetworkScore.KEEP_CONNECTED_NONE import android.net.NetworkScore.KEEP_CONNECTED_NONE
import android.os.Build import android.os.Build
import android.text.TextUtils import android.text.TextUtils
@@ -25,6 +27,7 @@ import android.util.ArraySet
import android.util.Log import android.util.Log
import androidx.test.filters.SmallTest import androidx.test.filters.SmallTest
import com.android.server.connectivity.FullScore.MAX_CS_MANAGED_POLICY import com.android.server.connectivity.FullScore.MAX_CS_MANAGED_POLICY
import com.android.server.connectivity.FullScore.MIN_CS_MANAGED_POLICY
import com.android.server.connectivity.FullScore.POLICY_ACCEPT_UNVALIDATED import com.android.server.connectivity.FullScore.POLICY_ACCEPT_UNVALIDATED
import com.android.server.connectivity.FullScore.POLICY_EVER_USER_SELECTED import com.android.server.connectivity.FullScore.POLICY_EVER_USER_SELECTED
import com.android.server.connectivity.FullScore.POLICY_IS_DESTROYED import com.android.server.connectivity.FullScore.POLICY_IS_DESTROYED
@@ -40,6 +43,7 @@ import org.junit.runner.RunWith
import kotlin.reflect.full.staticProperties import kotlin.reflect.full.staticProperties
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
@RunWith(DevSdkIgnoreRunner::class) @RunWith(DevSdkIgnoreRunner::class)
@@ -82,34 +86,11 @@ class FullScoreTest {
Log.setWtfHandler(wtfHandler) Log.setWtfHandler(wtfHandler)
} }
@Test
fun testGetLegacyInt() {
val ns = FullScore(50, 0L /* policy */, KEEP_CONNECTED_NONE)
assertEquals(10, ns.legacyInt) // -40 penalty for not being validated
assertEquals(50, ns.legacyIntAsValidated)
val vpnNs = FullScore(101, 0L /* policy */, KEEP_CONNECTED_NONE).withPolicies(vpn = true)
assertEquals(101, vpnNs.legacyInt) // VPNs are not subject to unvalidation penalty
assertEquals(101, vpnNs.legacyIntAsValidated)
assertEquals(101, vpnNs.withPolicies(validated = true).legacyInt)
assertEquals(101, vpnNs.withPolicies(validated = true).legacyIntAsValidated)
val validatedNs = ns.withPolicies(validated = true)
assertEquals(50, validatedNs.legacyInt) // No penalty, this is validated
assertEquals(50, validatedNs.legacyIntAsValidated)
val chosenNs = ns.withPolicies(onceChosen = true)
assertEquals(10, chosenNs.legacyInt)
assertEquals(100, chosenNs.legacyIntAsValidated)
assertEquals(10, chosenNs.withPolicies(acceptUnvalidated = true).legacyInt)
assertEquals(50, chosenNs.withPolicies(acceptUnvalidated = true).legacyIntAsValidated)
}
@Test @Test
fun testToString() { fun testToString() {
val string = FullScore(10, 0L /* policy */, KEEP_CONNECTED_NONE) val string = FullScore(0L /* policy */, KEEP_CONNECTED_NONE)
.withPolicies(vpn = true, acceptUnvalidated = true).toString() .withPolicies(vpn = true, acceptUnvalidated = true).toString()
assertTrue(string.contains("Score(10"), string) assertTrue(string.contains("Score("), string)
assertTrue(string.contains("ACCEPT_UNVALIDATED"), string) assertTrue(string.contains("ACCEPT_UNVALIDATED"), string)
assertTrue(string.contains("IS_VPN"), string) assertTrue(string.contains("IS_VPN"), string)
assertFalse(string.contains("IS_VALIDATED"), string) assertFalse(string.contains("IS_VALIDATED"), string)
@@ -131,7 +112,7 @@ class FullScoreTest {
@Test @Test
fun testHasPolicy() { fun testHasPolicy() {
val ns = FullScore(50, 0L /* policy */, KEEP_CONNECTED_NONE) val ns = FullScore(0L /* policy */, KEEP_CONNECTED_NONE)
assertFalse(ns.hasPolicy(POLICY_IS_VALIDATED)) assertFalse(ns.hasPolicy(POLICY_IS_VALIDATED))
assertFalse(ns.hasPolicy(POLICY_IS_VPN)) assertFalse(ns.hasPolicy(POLICY_IS_VPN))
assertFalse(ns.hasPolicy(POLICY_EVER_USER_SELECTED)) assertFalse(ns.hasPolicy(POLICY_EVER_USER_SELECTED))
@@ -148,12 +129,23 @@ class FullScoreTest {
val policies = getAllPolicies() val policies = getAllPolicies()
policies.forEach { policy -> policies.forEach { policy ->
assertTrue(policy.get() as Int >= FullScore.MIN_CS_MANAGED_POLICY) assertTrue(policy.get() as Int >= MIN_CS_MANAGED_POLICY)
assertTrue(policy.get() as Int <= FullScore.MAX_CS_MANAGED_POLICY) assertTrue(policy.get() as Int <= MAX_CS_MANAGED_POLICY)
} }
assertEquals(FullScore.MIN_CS_MANAGED_POLICY, assertEquals(MIN_CS_MANAGED_POLICY, policies.minOfOrNull { it.get() as Int })
policies.minOfOrNull { it.get() as Int }) assertEquals(MAX_CS_MANAGED_POLICY, policies.maxOfOrNull { it.get() as Int })
assertEquals(FullScore.MAX_CS_MANAGED_POLICY, }
policies.maxOfOrNull { it.get() as Int })
@Test
fun testEquals() {
val ns1 = FullScore(0L /* policy */, KEEP_CONNECTED_NONE)
val ns2 = FullScore(0L /* policy */, KEEP_CONNECTED_NONE)
val ns3 = FullScore(0L /* policy */, KEEP_CONNECTED_FOR_HANDOVER)
val ns4 = NetworkScore.Builder().setLegacyInt(50).build()
assertEquals(ns1, ns1)
assertEquals(ns2, ns1)
assertNotEquals(ns1.withPolicies(validated = true), ns1)
assertNotEquals(ns3, ns1)
assertFalse(ns1.equals(ns4))
} }
} }

View File

@@ -138,18 +138,16 @@ public class IpConnectivityMetricsTest {
private void logDefaultNetworkEvent(long timeMs, NetworkAgentInfo nai, private void logDefaultNetworkEvent(long timeMs, NetworkAgentInfo nai,
NetworkAgentInfo oldNai) { NetworkAgentInfo oldNai) {
final Network network = (nai != null) ? nai.network() : null; final Network network = (nai != null) ? nai.network() : null;
final int score = (nai != null) ? nai.getCurrentScore() : 0;
final boolean validated = (nai != null) ? nai.lastValidated : false; final boolean validated = (nai != null) ? nai.lastValidated : false;
final LinkProperties lp = (nai != null) ? nai.linkProperties : null; final LinkProperties lp = (nai != null) ? nai.linkProperties : null;
final NetworkCapabilities nc = (nai != null) ? nai.networkCapabilities : null; final NetworkCapabilities nc = (nai != null) ? nai.networkCapabilities : null;
final Network prevNetwork = (oldNai != null) ? oldNai.network() : null; final Network prevNetwork = (oldNai != null) ? oldNai.network() : null;
final int prevScore = (oldNai != null) ? oldNai.getCurrentScore() : 0;
final LinkProperties prevLp = (oldNai != null) ? oldNai.linkProperties : null; final LinkProperties prevLp = (oldNai != null) ? oldNai.linkProperties : null;
final NetworkCapabilities prevNc = (oldNai != null) ? oldNai.networkCapabilities : null; final NetworkCapabilities prevNc = (oldNai != null) ? oldNai.networkCapabilities : null;
mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs, network, score, validated, mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs, network, 0 /* legacyScore */,
lp, nc, prevNetwork, prevScore, prevLp, prevNc); validated, lp, nc, prevNetwork, 0 /* prevLegacyScore */, prevLp, prevNc);
} }
@Test @Test
public void testDefaultNetworkEvents() throws Exception { public void testDefaultNetworkEvents() throws Exception {
@@ -158,15 +156,15 @@ public class IpConnectivityMetricsTest {
NetworkAgentInfo[][] defaultNetworks = { NetworkAgentInfo[][] defaultNetworks = {
// nothing -> cell // nothing -> cell
{null, makeNai(100, 10, false, true, cell)}, {null, makeNai(100, false, true, cell)},
// cell -> wifi // cell -> wifi
{makeNai(100, 50, true, true, cell), makeNai(101, 20, true, false, wifi)}, {makeNai(100, true, true, cell), makeNai(101, true, false, wifi)},
// wifi -> nothing // wifi -> nothing
{makeNai(101, 60, true, false, wifi), null}, {makeNai(101, true, false, wifi), null},
// nothing -> cell // nothing -> cell
{null, makeNai(102, 10, true, true, cell)}, {null, makeNai(102, true, true, cell)},
// cell -> wifi // cell -> wifi
{makeNai(102, 50, true, true, cell), makeNai(103, 20, true, false, wifi)}, {makeNai(102, true, true, cell), makeNai(103, true, false, wifi)},
}; };
long timeMs = mService.mDefaultNetworkMetrics.creationTimeMs; long timeMs = mService.mDefaultNetworkMetrics.creationTimeMs;
@@ -204,8 +202,8 @@ public class IpConnectivityMetricsTest {
" transports: 1", " transports: 1",
" default_network_event <", " default_network_event <",
" default_network_duration_ms: 2002", " default_network_duration_ms: 2002",
" final_score: 50", " final_score: 0",
" initial_score: 10", " initial_score: 0",
" ip_support: 3", " ip_support: 3",
" no_default_network_duration_ms: 0", " no_default_network_duration_ms: 0",
" previous_default_network_link_layer: 0", " previous_default_network_link_layer: 0",
@@ -221,8 +219,8 @@ public class IpConnectivityMetricsTest {
" transports: 2", " transports: 2",
" default_network_event <", " default_network_event <",
" default_network_duration_ms: 4004", " default_network_duration_ms: 4004",
" final_score: 60", " final_score: 0",
" initial_score: 20", " initial_score: 0",
" ip_support: 1", " ip_support: 1",
" no_default_network_duration_ms: 0", " no_default_network_duration_ms: 0",
" previous_default_network_link_layer: 2", " previous_default_network_link_layer: 2",
@@ -255,8 +253,8 @@ public class IpConnectivityMetricsTest {
" transports: 1", " transports: 1",
" default_network_event <", " default_network_event <",
" default_network_duration_ms: 16016", " default_network_duration_ms: 16016",
" final_score: 50", " final_score: 0",
" initial_score: 10", " initial_score: 0",
" ip_support: 3", " ip_support: 3",
" no_default_network_duration_ms: 0", " no_default_network_duration_ms: 0",
" previous_default_network_link_layer: 4", " previous_default_network_link_layer: 4",
@@ -348,8 +346,8 @@ public class IpConnectivityMetricsTest {
long timeMs = mService.mDefaultNetworkMetrics.creationTimeMs; long timeMs = mService.mDefaultNetworkMetrics.creationTimeMs;
final long cell = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_CELLULAR}); final long cell = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_CELLULAR});
final long wifi = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_WIFI}); final long wifi = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_WIFI});
NetworkAgentInfo cellNai = makeNai(100, 50, false, true, cell); final NetworkAgentInfo cellNai = makeNai(100, false, true, cell);
NetworkAgentInfo wifiNai = makeNai(101, 60, true, false, wifi); final NetworkAgentInfo wifiNai = makeNai(101, true, false, wifi);
logDefaultNetworkEvent(timeMs + 200L, cellNai, null); logDefaultNetworkEvent(timeMs + 200L, cellNai, null);
logDefaultNetworkEvent(timeMs + 300L, wifiNai, cellNai); logDefaultNetworkEvent(timeMs + 300L, wifiNai, cellNai);
@@ -463,8 +461,8 @@ public class IpConnectivityMetricsTest {
" transports: 1", " transports: 1",
" default_network_event <", " default_network_event <",
" default_network_duration_ms: 100", " default_network_duration_ms: 100",
" final_score: 50", " final_score: 0",
" initial_score: 50", " initial_score: 0",
" ip_support: 2", " ip_support: 2",
" no_default_network_duration_ms: 0", " no_default_network_duration_ms: 0",
" previous_default_network_link_layer: 0", " previous_default_network_link_layer: 0",
@@ -611,10 +609,9 @@ public class IpConnectivityMetricsTest {
mNetdListener.onWakeupEvent(prefix, uid, ether, ip, mac, srcIp, dstIp, sport, dport, now); mNetdListener.onWakeupEvent(prefix, uid, ether, ip, mac, srcIp, dstIp, sport, dport, now);
} }
NetworkAgentInfo makeNai(int netId, int score, boolean ipv4, boolean ipv6, long transports) { NetworkAgentInfo makeNai(int netId, boolean ipv4, boolean ipv6, long transports) {
NetworkAgentInfo nai = mock(NetworkAgentInfo.class); NetworkAgentInfo nai = mock(NetworkAgentInfo.class);
when(nai.network()).thenReturn(new Network(netId)); when(nai.network()).thenReturn(new Network(netId));
when(nai.getCurrentScore()).thenReturn(score);
nai.linkProperties = new LinkProperties(); nai.linkProperties = new LinkProperties();
nai.networkCapabilities = new NetworkCapabilities(); nai.networkCapabilities = new NetworkCapabilities();
nai.lastValidated = true; nai.lastValidated = true;

View File

@@ -42,7 +42,7 @@ class NetworkOfferTest {
@Test @Test
fun testOfferNeededUnneeded() { fun testOfferNeededUnneeded() {
val score = FullScore(50, POLICY_NONE, KEEP_CONNECTED_NONE) val score = FullScore(POLICY_NONE, KEEP_CONNECTED_NONE)
val offer = NetworkOffer(score, NetworkCapabilities.Builder().build(), mockCallback, val offer = NetworkOffer(score, NetworkCapabilities.Builder().build(), mockCallback,
1 /* providerId */) 1 /* providerId */)
val request1 = mock(NetworkRequest::class.java) val request1 = mock(NetworkRequest::class.java)

View File

@@ -33,7 +33,7 @@ import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import kotlin.test.assertEquals import kotlin.test.assertEquals
private fun score(vararg policies: Int) = FullScore(0, private fun score(vararg policies: Int) = FullScore(
policies.fold(0L) { acc, e -> acc or (1L shl e) }, KEEP_CONNECTED_NONE) 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) = NetworkCapabilities.Builder().addTransportType(transport).build()