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
3 changed files with 52 additions and 13 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

@@ -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.