Merge "Make Vpn more testable with a wrapper class"
This commit is contained in:
@@ -519,12 +519,8 @@ public class Vpn {
|
|||||||
@NonNull NetworkScore score,
|
@NonNull NetworkScore score,
|
||||||
@NonNull NetworkAgentConfig config,
|
@NonNull NetworkAgentConfig config,
|
||||||
@Nullable NetworkProvider provider) {
|
@Nullable NetworkProvider provider) {
|
||||||
return new NetworkAgent(context, looper, logTag, nc, lp, score, config, provider) {
|
return new VpnNetworkAgentWrapper(
|
||||||
@Override
|
context, looper, logTag, nc, lp, score, config, provider);
|
||||||
public void onNetworkUnwanted() {
|
|
||||||
// We are user controlled, not driven by NetworkRequest.
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1818,7 +1814,7 @@ public class Vpn {
|
|||||||
Log.wtf(TAG, "Failed to add restricted user to owner", e);
|
Log.wtf(TAG, "Failed to add restricted user to owner", e);
|
||||||
}
|
}
|
||||||
if (mNetworkAgent != null) {
|
if (mNetworkAgent != null) {
|
||||||
mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
|
doSendNetworkCapabilities(mNetworkAgent, mNetworkCapabilities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setVpnForcedLocked(mLockdown);
|
setVpnForcedLocked(mLockdown);
|
||||||
@@ -1848,7 +1844,7 @@ public class Vpn {
|
|||||||
Log.wtf(TAG, "Failed to remove restricted user to owner", e);
|
Log.wtf(TAG, "Failed to remove restricted user to owner", e);
|
||||||
}
|
}
|
||||||
if (mNetworkAgent != null) {
|
if (mNetworkAgent != null) {
|
||||||
mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
|
doSendNetworkCapabilities(mNetworkAgent, mNetworkCapabilities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setVpnForcedLocked(mLockdown);
|
setVpnForcedLocked(mLockdown);
|
||||||
@@ -2082,7 +2078,7 @@ public class Vpn {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
boolean success = jniAddAddress(mInterface, address, prefixLength);
|
boolean success = jniAddAddress(mInterface, address, prefixLength);
|
||||||
mNetworkAgent.sendLinkProperties(makeLinkProperties());
|
doSendLinkProperties(mNetworkAgent, makeLinkProperties());
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2091,7 +2087,7 @@ public class Vpn {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
boolean success = jniDelAddress(mInterface, address, prefixLength);
|
boolean success = jniDelAddress(mInterface, address, prefixLength);
|
||||||
mNetworkAgent.sendLinkProperties(makeLinkProperties());
|
doSendLinkProperties(mNetworkAgent, makeLinkProperties());
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2105,8 +2101,11 @@ public class Vpn {
|
|||||||
// Make defensive copy since the content of array might be altered by the caller.
|
// Make defensive copy since the content of array might be altered by the caller.
|
||||||
mConfig.underlyingNetworks =
|
mConfig.underlyingNetworks =
|
||||||
(networks != null) ? Arrays.copyOf(networks, networks.length) : null;
|
(networks != null) ? Arrays.copyOf(networks, networks.length) : null;
|
||||||
mNetworkAgent.setUnderlyingNetworks((mConfig.underlyingNetworks != null)
|
doSetUnderlyingNetworks(
|
||||||
? Arrays.asList(mConfig.underlyingNetworks) : null);
|
mNetworkAgent,
|
||||||
|
(mConfig.underlyingNetworks != null)
|
||||||
|
? Arrays.asList(mConfig.underlyingNetworks)
|
||||||
|
: null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2916,7 +2915,7 @@ public class Vpn {
|
|||||||
return; // Link properties are already sent.
|
return; // Link properties are already sent.
|
||||||
} else {
|
} else {
|
||||||
// Underlying networks also set in agentConnect()
|
// Underlying networks also set in agentConnect()
|
||||||
networkAgent.setUnderlyingNetworks(Collections.singletonList(network));
|
doSetUnderlyingNetworks(networkAgent, Collections.singletonList(network));
|
||||||
mNetworkCapabilities =
|
mNetworkCapabilities =
|
||||||
new NetworkCapabilities.Builder(mNetworkCapabilities)
|
new NetworkCapabilities.Builder(mNetworkCapabilities)
|
||||||
.setUnderlyingNetworks(Collections.singletonList(network))
|
.setUnderlyingNetworks(Collections.singletonList(network))
|
||||||
@@ -2926,7 +2925,7 @@ public class Vpn {
|
|||||||
lp = makeLinkProperties(); // Accesses VPN instance fields; must be locked
|
lp = makeLinkProperties(); // Accesses VPN instance fields; must be locked
|
||||||
}
|
}
|
||||||
|
|
||||||
networkAgent.sendLinkProperties(lp);
|
doSendLinkProperties(networkAgent, lp);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.d(TAG, "Error in ChildOpened for token " + token, e);
|
Log.d(TAG, "Error in ChildOpened for token " + token, e);
|
||||||
onSessionLost(token, e);
|
onSessionLost(token, e);
|
||||||
@@ -2993,7 +2992,7 @@ public class Vpn {
|
|||||||
new NetworkCapabilities.Builder(mNetworkCapabilities)
|
new NetworkCapabilities.Builder(mNetworkCapabilities)
|
||||||
.setUnderlyingNetworks(Collections.singletonList(network))
|
.setUnderlyingNetworks(Collections.singletonList(network))
|
||||||
.build();
|
.build();
|
||||||
mNetworkAgent.setUnderlyingNetworks(Collections.singletonList(network));
|
doSetUnderlyingNetworks(mNetworkAgent, Collections.singletonList(network));
|
||||||
}
|
}
|
||||||
|
|
||||||
mTunnelIface.setUnderlyingNetwork(network);
|
mTunnelIface.setUnderlyingNetwork(network);
|
||||||
@@ -3389,7 +3388,7 @@ public class Vpn {
|
|||||||
null /*gateway*/, null /*iface*/, RTN_UNREACHABLE));
|
null /*gateway*/, null /*iface*/, RTN_UNREACHABLE));
|
||||||
}
|
}
|
||||||
if (mNetworkAgent != null) {
|
if (mNetworkAgent != null) {
|
||||||
mNetworkAgent.sendLinkProperties(makeLinkProperties());
|
doSendLinkProperties(mNetworkAgent, makeLinkProperties());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4126,7 +4125,7 @@ public class Vpn {
|
|||||||
.setUids(createUserAndRestrictedProfilesRanges(
|
.setUids(createUserAndRestrictedProfilesRanges(
|
||||||
mUserId, null /* allowedApplications */, excludedApps))
|
mUserId, null /* allowedApplications */, excludedApps))
|
||||||
.build();
|
.build();
|
||||||
mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
|
doSendNetworkCapabilities(mNetworkAgent, mNetworkCapabilities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4203,6 +4202,85 @@ public class Vpn {
|
|||||||
return isCurrentIkev2VpnLocked(packageName) ? makeVpnProfileStateLocked() : null;
|
return isCurrentIkev2VpnLocked(packageName) ? makeVpnProfileStateLocked() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Proxy to allow different testing setups */
|
||||||
|
// TODO: b/240492694 Remove VpnNetworkAgentWrapper and this method when
|
||||||
|
// NetworkAgent#sendLinkProperties can be un-finalized.
|
||||||
|
private static void doSendLinkProperties(
|
||||||
|
@NonNull NetworkAgent agent, @NonNull LinkProperties lp) {
|
||||||
|
if (agent instanceof VpnNetworkAgentWrapper) {
|
||||||
|
((VpnNetworkAgentWrapper) agent).doSendLinkProperties(lp);
|
||||||
|
} else {
|
||||||
|
agent.sendLinkProperties(lp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Proxy to allow different testing setups */
|
||||||
|
// TODO: b/240492694 Remove VpnNetworkAgentWrapper and this method when
|
||||||
|
// NetworkAgent#sendNetworkCapabilities can be un-finalized.
|
||||||
|
private static void doSendNetworkCapabilities(
|
||||||
|
@NonNull NetworkAgent agent, @NonNull NetworkCapabilities nc) {
|
||||||
|
if (agent instanceof VpnNetworkAgentWrapper) {
|
||||||
|
((VpnNetworkAgentWrapper) agent).doSendNetworkCapabilities(nc);
|
||||||
|
} else {
|
||||||
|
agent.sendNetworkCapabilities(nc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Proxy to allow different testing setups */
|
||||||
|
// TODO: b/240492694 Remove VpnNetworkAgentWrapper and this method when
|
||||||
|
// NetworkAgent#setUnderlyingNetworks can be un-finalized.
|
||||||
|
private static void doSetUnderlyingNetworks(
|
||||||
|
@NonNull NetworkAgent agent, @NonNull List<Network> networks) {
|
||||||
|
if (agent instanceof VpnNetworkAgentWrapper) {
|
||||||
|
((VpnNetworkAgentWrapper) agent).doSetUnderlyingNetworks(networks);
|
||||||
|
} else {
|
||||||
|
agent.setUnderlyingNetworks(networks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proxy to allow testing
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
// TODO: b/240492694 Remove VpnNetworkAgentWrapper when NetworkAgent's methods can be
|
||||||
|
// un-finalized.
|
||||||
|
@VisibleForTesting
|
||||||
|
public static class VpnNetworkAgentWrapper extends NetworkAgent {
|
||||||
|
/** Create an VpnNetworkAgentWrapper */
|
||||||
|
public VpnNetworkAgentWrapper(
|
||||||
|
@NonNull Context context,
|
||||||
|
@NonNull Looper looper,
|
||||||
|
@NonNull String logTag,
|
||||||
|
@NonNull NetworkCapabilities nc,
|
||||||
|
@NonNull LinkProperties lp,
|
||||||
|
@NonNull NetworkScore score,
|
||||||
|
@NonNull NetworkAgentConfig config,
|
||||||
|
@Nullable NetworkProvider provider) {
|
||||||
|
super(context, looper, logTag, nc, lp, score, config, provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Update the LinkProperties */
|
||||||
|
public void doSendLinkProperties(@NonNull LinkProperties lp) {
|
||||||
|
sendLinkProperties(lp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Update the NetworkCapabilities */
|
||||||
|
public void doSendNetworkCapabilities(@NonNull NetworkCapabilities nc) {
|
||||||
|
sendNetworkCapabilities(nc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set the underlying networks */
|
||||||
|
public void doSetUnderlyingNetworks(@NonNull List<Network> networks) {
|
||||||
|
setUnderlyingNetworks(networks);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNetworkUnwanted() {
|
||||||
|
// We are user controlled, not driven by NetworkRequest.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxy to allow testing
|
* Proxy to allow testing
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user