Avoid overlapping NetIDs in ConnectivityServiceTest with real NetIDs
Overlapping the NetIDs can cause the ConnectivityService instance under test to inadvertently use real networks, for example when NetworkMonitor attempts to validate a network. This fixes test hangs when run on devices with active internet connections. Change-Id: I5e1898953f0117b9f75beccac4a52ae2db173567
This commit is contained in:
@@ -100,6 +100,7 @@ import android.util.Xml;
|
|||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
import com.android.internal.annotations.GuardedBy;
|
import com.android.internal.annotations.GuardedBy;
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.app.IBatteryStats;
|
import com.android.internal.app.IBatteryStats;
|
||||||
import com.android.internal.net.LegacyVpnInfo;
|
import com.android.internal.net.LegacyVpnInfo;
|
||||||
import com.android.internal.net.NetworkStatsFactory;
|
import com.android.internal.net.NetworkStatsFactory;
|
||||||
@@ -767,7 +768,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
return mNextNetworkRequestId++;
|
return mNextNetworkRequestId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int reserveNetId() {
|
@VisibleForTesting
|
||||||
|
protected int reserveNetId() {
|
||||||
synchronized (mNetworkForNetId) {
|
synchronized (mNetworkForNetId) {
|
||||||
for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) {
|
for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) {
|
||||||
int netId = mNextNetId;
|
int netId = mNextNetId;
|
||||||
@@ -1664,6 +1666,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd";
|
private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd";
|
||||||
|
|
||||||
// Overridden for testing purposes to avoid writing to SystemProperties.
|
// Overridden for testing purposes to avoid writing to SystemProperties.
|
||||||
|
@VisibleForTesting
|
||||||
protected int getDefaultTcpRwnd() {
|
protected int getDefaultTcpRwnd() {
|
||||||
return SystemProperties.getInt(DEFAULT_TCP_RWND_KEY, 0);
|
return SystemProperties.getInt(DEFAULT_TCP_RWND_KEY, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,6 +235,27 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
// Prevent wrapped ConnectivityService from trying to write to SystemProperties.
|
// Prevent wrapped ConnectivityService from trying to write to SystemProperties.
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int reserveNetId() {
|
||||||
|
while (true) {
|
||||||
|
final int netId = super.reserveNetId();
|
||||||
|
|
||||||
|
// Don't overlap test NetIDs with real NetIDs as binding sockets to real networks
|
||||||
|
// can have odd side-effects, like network validations succeeding.
|
||||||
|
final Network[] networks = ConnectivityManager.from(getContext()).getAllNetworks();
|
||||||
|
boolean overlaps = false;
|
||||||
|
for (Network network : networks) {
|
||||||
|
if (netId == network.netId) {
|
||||||
|
overlaps = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (overlaps) continue;
|
||||||
|
|
||||||
|
return netId;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user