Merge "If Internet probe is redirected, pass redirect destination to NetworkAgent." into nyc-dev

This commit is contained in:
Paul Jensen
2016-04-13 01:33:35 +00:00
committed by Android (Google) Code Review
4 changed files with 87 additions and 28 deletions

View File

@@ -120,12 +120,17 @@ public abstract class NetworkAgent extends Handler {
* either a bad network configuration (no internet link) or captive portal. * either a bad network configuration (no internet link) or captive portal.
* *
* arg1 = either {@code VALID_NETWORK} or {@code INVALID_NETWORK} * arg1 = either {@code VALID_NETWORK} or {@code INVALID_NETWORK}
* obj = Bundle containing map from {@code REDIRECT_URL_KEY} to {@code String}
* representing URL that Internet probe was redirect to, if it was redirected,
* or mapping to {@code null} otherwise.
*/ */
public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7; public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7;
public static final int VALID_NETWORK = 1; public static final int VALID_NETWORK = 1;
public static final int INVALID_NETWORK = 2; public static final int INVALID_NETWORK = 2;
public static String REDIRECT_URL_KEY = "redirect URL";
/** /**
* Sent by the NetworkAgent to ConnectivityService to indicate this network was * Sent by the NetworkAgent to ConnectivityService to indicate this network was
* explicitly selected. This should be sent before the NetworkInfo is marked * explicitly selected. This should be sent before the NetworkInfo is marked
@@ -283,11 +288,12 @@ public abstract class NetworkAgent extends Handler {
break; break;
} }
case CMD_REPORT_NETWORK_STATUS: { case CMD_REPORT_NETWORK_STATUS: {
String redirectUrl = ((Bundle)msg.obj).getString(REDIRECT_URL_KEY);
if (VDBG) { if (VDBG) {
log("CMD_REPORT_NETWORK_STATUS(" + log("CMD_REPORT_NETWORK_STATUS(" +
(msg.arg1 == VALID_NETWORK ? "VALID)" : "INVALID)")); (msg.arg1 == VALID_NETWORK ? "VALID, " : "INVALID, ") + redirectUrl);
} }
networkStatus(msg.arg1); networkStatus(msg.arg1, redirectUrl);
break; break;
} }
case CMD_SAVE_ACCEPT_UNVALIDATED: { case CMD_SAVE_ACCEPT_UNVALIDATED: {
@@ -443,8 +449,12 @@ public abstract class NetworkAgent extends Handler {
* *
* This may be called multiple times as the network status changes and may * This may be called multiple times as the network status changes and may
* generate false negatives if we lose ip connectivity before the link is torn down. * generate false negatives if we lose ip connectivity before the link is torn down.
*
* @param status one of {@code VALID_NETWORK} or {@code INVALID_NETWORK}.
* @param redirectUrl If the Internet probe was redirected, this is the destination it was
* redirected to, otherwise {@code null}.
*/ */
protected void networkStatus(int status) { protected void networkStatus(int status, String redirectUrl) {
} }
/** /**

View File

@@ -2025,11 +2025,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
default: default:
return false; return false;
case NetworkMonitor.EVENT_NETWORK_TESTED: { case NetworkMonitor.EVENT_NETWORK_TESTED: {
NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; final NetworkAgentInfo nai;
if (isLiveNetworkAgent(nai, msg.what)) { synchronized (mNetworkForNetId) {
nai = mNetworkForNetId.get(msg.arg2);
}
if (nai != null) {
final boolean valid = final boolean valid =
(msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID); (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID);
if (DBG) log(nai.name() + " validation " + (valid ? " passed" : "failed")); if (DBG) log(nai.name() + " validation " + (valid ? "passed" : "failed") +
(msg.obj == null ? "" : " with redirect to " + (String)msg.obj));
if (valid != nai.lastValidated) { if (valid != nai.lastValidated) {
final int oldScore = nai.getCurrentScore(); final int oldScore = nai.getCurrentScore();
nai.lastValidated = valid; nai.lastValidated = valid;
@@ -2040,10 +2044,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
updateInetCondition(nai); updateInetCondition(nai);
// Let the NetworkAgent know the state of its network // Let the NetworkAgent know the state of its network
Bundle redirectUrlBundle = new Bundle();
redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, (String)msg.obj);
nai.asyncChannel.sendMessage( nai.asyncChannel.sendMessage(
android.net.NetworkAgent.CMD_REPORT_NETWORK_STATUS, NetworkAgent.CMD_REPORT_NETWORK_STATUS,
(valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK),
0, null); 0, redirectUrlBundle);
} }
break; break;
} }

View File

@@ -120,8 +120,9 @@ public class NetworkMonitor extends StateMachine {
/** /**
* Inform ConnectivityService that the network has been tested. * Inform ConnectivityService that the network has been tested.
* obj = NetworkAgentInfo * obj = String representing URL that Internet probe was redirect to, if it was redirected.
* arg1 = One of the NETWORK_TESTED_RESULT_* constants. * arg1 = One of the NETWORK_TESTED_RESULT_* constants.
* arg2 = NetID.
*/ */
public static final int EVENT_NETWORK_TESTED = BASE + 2; public static final int EVENT_NETWORK_TESTED = BASE + 2;
@@ -334,8 +335,8 @@ public class NetworkMonitor extends StateMachine {
mDontDisplaySigninNotification = true; mDontDisplaySigninNotification = true;
mUserDoesNotWant = true; mUserDoesNotWant = true;
mConnectivityServiceHandler.sendMessage(obtainMessage( mConnectivityServiceHandler.sendMessage(obtainMessage(
EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0, EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID,
mNetworkAgentInfo)); mNetworkAgentInfo.network.netId, null));
// TODO: Should teardown network. // TODO: Should teardown network.
mUidResponsibleForReeval = 0; mUidResponsibleForReeval = 0;
transitionTo(mEvaluatingState); transitionTo(mEvaluatingState);
@@ -358,7 +359,7 @@ public class NetworkMonitor extends StateMachine {
CaptivePortalStateChangeEvent.logEvent( CaptivePortalStateChangeEvent.logEvent(
CaptivePortalStateChangeEvent.NETWORK_MONITOR_VALIDATED); CaptivePortalStateChangeEvent.NETWORK_MONITOR_VALIDATED);
mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
NETWORK_TEST_RESULT_VALID, 0, mNetworkAgentInfo)); NETWORK_TEST_RESULT_VALID, mNetworkAgentInfo.network.netId, null));
} }
@Override @Override
@@ -412,6 +413,21 @@ public class NetworkMonitor extends StateMachine {
} }
} }
/**
* Result of calling isCaptivePortal().
* @hide
*/
@VisibleForTesting
public static final class CaptivePortalProbeResult {
final int mHttpResponseCode; // HTTP response code returned from Internet probe.
final String mRedirectUrl; // Redirect destination returned from Internet probe.
public CaptivePortalProbeResult(int httpResponseCode, String redirectUrl) {
mHttpResponseCode = httpResponseCode;
mRedirectUrl = redirectUrl;
}
}
// Being in the EvaluatingState State indicates the Network is being evaluated for internet // Being in the EvaluatingState State indicates the Network is being evaluated for internet
// connectivity, or that the user has indicated that this network is unwanted. // connectivity, or that the user has indicated that this network is unwanted.
private class EvaluatingState extends State { private class EvaluatingState extends State {
@@ -464,19 +480,23 @@ public class NetworkMonitor extends StateMachine {
// IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine // IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine
// will be unresponsive. isCaptivePortal() could be executed on another Thread // will be unresponsive. isCaptivePortal() could be executed on another Thread
// if this is found to cause problems. // if this is found to cause problems.
int httpResponseCode = isCaptivePortal(); CaptivePortalProbeResult probeResult = isCaptivePortal();
CaptivePortalCheckResultEvent.logEvent(mNetworkAgentInfo.network.netId, CaptivePortalCheckResultEvent.logEvent(mNetworkAgentInfo.network.netId,
httpResponseCode); probeResult.mHttpResponseCode);
if (httpResponseCode == 204) { if (probeResult.mHttpResponseCode == 204) {
transitionTo(mValidatedState); transitionTo(mValidatedState);
} else if (httpResponseCode >= 200 && httpResponseCode <= 399) { } else if (probeResult.mHttpResponseCode >= 200 &&
probeResult.mHttpResponseCode <= 399) {
mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
NETWORK_TEST_RESULT_INVALID, mNetworkAgentInfo.network.netId,
probeResult.mRedirectUrl));
transitionTo(mCaptivePortalState); transitionTo(mCaptivePortalState);
} else { } else {
final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
sendMessageDelayed(msg, mReevaluateDelayMs); sendMessageDelayed(msg, mReevaluateDelayMs);
mConnectivityServiceHandler.sendMessage(obtainMessage( mConnectivityServiceHandler.sendMessage(obtainMessage(
EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, 0, EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID,
mNetworkAgentInfo)); mNetworkAgentInfo.network.netId, probeResult.mRedirectUrl));
if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) { if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
// Don't continue to blame UID forever. // Don't continue to blame UID forever.
TrafficStats.clearThreadStatsUid(); TrafficStats.clearThreadStatsUid();
@@ -533,8 +553,6 @@ public class NetworkMonitor extends StateMachine {
@Override @Override
public void enter() { public void enter() {
mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
NETWORK_TEST_RESULT_INVALID, 0, mNetworkAgentInfo));
// Don't annoy user with sign-in notifications. // Don't annoy user with sign-in notifications.
if (mDontDisplaySigninNotification) return; if (mDontDisplaySigninNotification) return;
// Create a CustomIntentReceiver that sends us a // Create a CustomIntentReceiver that sends us a
@@ -639,11 +657,12 @@ public class NetworkMonitor extends StateMachine {
* Returns HTTP response code. * Returns HTTP response code.
*/ */
@VisibleForTesting @VisibleForTesting
protected int isCaptivePortal() { protected CaptivePortalProbeResult isCaptivePortal() {
if (!mIsCaptivePortalCheckEnabled) return 204; if (!mIsCaptivePortalCheckEnabled) return new CaptivePortalProbeResult(204, null);
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
int httpResponseCode = 599; int httpResponseCode = 599;
String redirectUrl = null;
try { try {
URL url = new URL(getCaptivePortalServerUrl(mContext)); URL url = new URL(getCaptivePortalServerUrl(mContext));
// On networks with a PAC instead of fetching a URL that should result in a 204 // On networks with a PAC instead of fetching a URL that should result in a 204
@@ -699,6 +718,7 @@ public class NetworkMonitor extends StateMachine {
long requestTimestamp = SystemClock.elapsedRealtime(); long requestTimestamp = SystemClock.elapsedRealtime();
httpResponseCode = urlConnection.getResponseCode(); httpResponseCode = urlConnection.getResponseCode();
redirectUrl = urlConnection.getHeaderField("location");
// Time how long it takes to get a response to our request // Time how long it takes to get a response to our request
long responseTimestamp = SystemClock.elapsedRealtime(); long responseTimestamp = SystemClock.elapsedRealtime();
@@ -739,7 +759,7 @@ public class NetworkMonitor extends StateMachine {
urlConnection.disconnect(); urlConnection.disconnect();
} }
} }
return httpResponseCode; return new CaptivePortalProbeResult(httpResponseCode, redirectUrl);
} }
/** /**

View File

@@ -70,6 +70,7 @@ import android.util.LogPrinter;
import com.android.internal.util.WakeupMessage; import com.android.internal.util.WakeupMessage;
import com.android.server.connectivity.NetworkAgentInfo; import com.android.server.connectivity.NetworkAgentInfo;
import com.android.server.connectivity.NetworkMonitor; import com.android.server.connectivity.NetworkMonitor;
import com.android.server.connectivity.NetworkMonitor.CaptivePortalProbeResult;
import com.android.server.net.NetworkPinner; import com.android.server.net.NetworkPinner;
import java.net.InetAddress; import java.net.InetAddress;
@@ -223,11 +224,15 @@ public class ConnectivityServiceTest extends AndroidTestCase {
private final NetworkCapabilities mNetworkCapabilities; private final NetworkCapabilities mNetworkCapabilities;
private final IdleableHandlerThread mHandlerThread; private final IdleableHandlerThread mHandlerThread;
private final ConditionVariable mDisconnected = new ConditionVariable(); private final ConditionVariable mDisconnected = new ConditionVariable();
private final ConditionVariable mNetworkStatusReceived = new ConditionVariable();
private int mScore; private int mScore;
private NetworkAgent mNetworkAgent; private NetworkAgent mNetworkAgent;
private int mStartKeepaliveError = PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED; private int mStartKeepaliveError = PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED;
private int mStopKeepaliveError = PacketKeepalive.NO_KEEPALIVE; private int mStopKeepaliveError = PacketKeepalive.NO_KEEPALIVE;
private Integer mExpectedKeepaliveSlot = null; private Integer mExpectedKeepaliveSlot = null;
// Contains the redirectUrl from networkStatus(). Before reading, wait for
// mNetworkStatusReceived.
private String mRedirectUrl;
MockNetworkAgent(int transport) { MockNetworkAgent(int transport) {
final int type = transportToLegacyType(transport); final int type = transportToLegacyType(transport);
@@ -266,6 +271,12 @@ public class ConnectivityServiceTest extends AndroidTestCase {
public void stopPacketKeepalive(Message msg) { public void stopPacketKeepalive(Message msg) {
onPacketKeepaliveEvent(msg.arg1, mStopKeepaliveError); onPacketKeepaliveEvent(msg.arg1, mStopKeepaliveError);
} }
@Override
public void networkStatus(int status, String redirectUrl) {
mRedirectUrl = redirectUrl;
mNetworkStatusReceived.open();
}
}; };
// Waits for the NetworkAgent to be registered, which includes the creation of the // Waits for the NetworkAgent to be registered, which includes the creation of the
// NetworkMonitor. // NetworkMonitor.
@@ -340,13 +351,15 @@ public class ConnectivityServiceTest extends AndroidTestCase {
if (callback != null) mCm.unregisterNetworkCallback(callback); if (callback != null) mCm.unregisterNetworkCallback(callback);
} }
public void connectWithCaptivePortal() { public void connectWithCaptivePortal(String redirectUrl) {
mWrappedNetworkMonitor.gen204ProbeResult = 200; mWrappedNetworkMonitor.gen204ProbeResult = 200;
mWrappedNetworkMonitor.gen204ProbeRedirectUrl = redirectUrl;
connect(false); connect(false);
waitFor(new Criteria() { public boolean get() { waitFor(new Criteria() { public boolean get() {
NetworkCapabilities caps = mCm.getNetworkCapabilities(getNetwork()); NetworkCapabilities caps = mCm.getNetworkCapabilities(getNetwork());
return caps != null && caps.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL);} }); return caps != null && caps.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL);} });
mWrappedNetworkMonitor.gen204ProbeResult = 500; mWrappedNetworkMonitor.gen204ProbeResult = 500;
mWrappedNetworkMonitor.gen204ProbeRedirectUrl = null;
} }
public void disconnect() { public void disconnect() {
@@ -381,6 +394,11 @@ public class ConnectivityServiceTest extends AndroidTestCase {
public void setExpectedKeepaliveSlot(Integer slot) { public void setExpectedKeepaliveSlot(Integer slot) {
mExpectedKeepaliveSlot = slot; mExpectedKeepaliveSlot = slot;
} }
public String waitForRedirectUrl() {
assertTrue(mNetworkStatusReceived.block(TIMEOUT_MS));
return mRedirectUrl;
}
} }
/** /**
@@ -543,6 +561,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
private class WrappedNetworkMonitor extends NetworkMonitor { private class WrappedNetworkMonitor extends NetworkMonitor {
// HTTP response code fed back to NetworkMonitor for Internet connectivity probe. // HTTP response code fed back to NetworkMonitor for Internet connectivity probe.
public int gen204ProbeResult = 500; public int gen204ProbeResult = 500;
public String gen204ProbeRedirectUrl = null;
public WrappedNetworkMonitor(Context context, Handler handler, public WrappedNetworkMonitor(Context context, Handler handler,
NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest) { NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest) {
@@ -550,8 +569,8 @@ public class ConnectivityServiceTest extends AndroidTestCase {
} }
@Override @Override
protected int isCaptivePortal() { protected CaptivePortalProbeResult isCaptivePortal() {
return gen204ProbeResult; return new CaptivePortalProbeResult(gen204ProbeResult, gen204ProbeRedirectUrl);
} }
@Override @Override
@@ -1344,8 +1363,10 @@ public class ConnectivityServiceTest extends AndroidTestCase {
// Bring up a network with a captive portal. // Bring up a network with a captive portal.
// Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL. // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.connectWithCaptivePortal(); String firstRedirectUrl = "http://example.com/firstPath";
mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl);
captivePortalCallback.expectCallback(CallbackState.AVAILABLE); captivePortalCallback.expectCallback(CallbackState.AVAILABLE);
assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), firstRedirectUrl);
// Take down network. // Take down network.
// Expect onLost callback. // Expect onLost callback.
@@ -1355,8 +1376,10 @@ public class ConnectivityServiceTest extends AndroidTestCase {
// Bring up a network with a captive portal. // Bring up a network with a captive portal.
// Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL. // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.connectWithCaptivePortal(); String secondRedirectUrl = "http://example.com/secondPath";
mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl);
captivePortalCallback.expectCallback(CallbackState.AVAILABLE); captivePortalCallback.expectCallback(CallbackState.AVAILABLE);
assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), secondRedirectUrl);
// Make captive portal disappear then revalidate. // Make captive portal disappear then revalidate.
// Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL. // Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL.