Merge "Lazy-create IpSecTunnelInterface"

This commit is contained in:
Benedict Wong
2021-02-12 23:11:01 +00:00
committed by Gerrit Code Review
2 changed files with 33 additions and 18 deletions

View File

@@ -125,10 +125,11 @@ import java.util.concurrent.TimeUnit;
public class VcnGatewayConnection extends StateMachine { public class VcnGatewayConnection extends StateMachine {
private static final String TAG = VcnGatewayConnection.class.getSimpleName(); private static final String TAG = VcnGatewayConnection.class.getSimpleName();
@VisibleForTesting(visibility = Visibility.PRIVATE)
static final InetAddress DUMMY_ADDR = InetAddresses.parseNumericAddress("192.0.2.0");
private static final int[] MERGED_CAPABILITIES = private static final int[] MERGED_CAPABILITIES =
new int[] {NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_NOT_ROAMING}; new int[] {NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_NOT_ROAMING};
private static final InetAddress DUMMY_ADDR = InetAddresses.parseNumericAddress("192.0.2.0");
private static final int ARG_NOT_PRESENT = Integer.MIN_VALUE; private static final int ARG_NOT_PRESENT = Integer.MIN_VALUE;
private static final String DISCONNECT_REASON_INTERNAL_ERROR = "Uncaught exception: "; private static final String DISCONNECT_REASON_INTERNAL_ERROR = "Uncaught exception: ";
@@ -412,11 +413,11 @@ public class VcnGatewayConnection extends StateMachine {
@NonNull private final VcnGatewayConnectionConfig mConnectionConfig; @NonNull private final VcnGatewayConnectionConfig mConnectionConfig;
@NonNull private final VcnGatewayStatusCallback mGatewayStatusCallback; @NonNull private final VcnGatewayStatusCallback mGatewayStatusCallback;
@NonNull private final Dependencies mDeps; @NonNull private final Dependencies mDeps;
@NonNull private final VcnUnderlyingNetworkTrackerCallback mUnderlyingNetworkTrackerCallback; @NonNull private final VcnUnderlyingNetworkTrackerCallback mUnderlyingNetworkTrackerCallback;
@NonNull private final IpSecManager mIpSecManager; @NonNull private final IpSecManager mIpSecManager;
@NonNull private final IpSecTunnelInterface mTunnelIface;
@Nullable private IpSecTunnelInterface mTunnelIface = null;
/** Running state of this VcnGatewayConnection. */ /** Running state of this VcnGatewayConnection. */
private boolean mIsRunning = true; private boolean mIsRunning = true;
@@ -526,20 +527,6 @@ public class VcnGatewayConnection extends StateMachine {
mUnderlyingNetworkTrackerCallback); mUnderlyingNetworkTrackerCallback);
mIpSecManager = mVcnContext.getContext().getSystemService(IpSecManager.class); mIpSecManager = mVcnContext.getContext().getSystemService(IpSecManager.class);
IpSecTunnelInterface iface;
try {
iface =
mIpSecManager.createIpSecTunnelInterface(
DUMMY_ADDR, DUMMY_ADDR, new Network(-1));
} catch (IOException | ResourceUnavailableException e) {
teardownAsynchronously();
mTunnelIface = null;
return;
}
mTunnelIface = iface;
addState(mDisconnectedState); addState(mDisconnectedState);
addState(mDisconnectingState); addState(mDisconnectingState);
addState(mConnectingState); addState(mConnectingState);
@@ -1117,6 +1104,18 @@ public class VcnGatewayConnection extends StateMachine {
class ConnectedState extends ConnectedStateBase { class ConnectedState extends ConnectedStateBase {
@Override @Override
protected void enterState() throws Exception { protected void enterState() throws Exception {
if (mTunnelIface == null) {
try {
// Requires a real Network object in order to be created; doing this any earlier
// means not having a real Network object, or picking an incorrect Network.
mTunnelIface =
mIpSecManager.createIpSecTunnelInterface(
DUMMY_ADDR, DUMMY_ADDR, mUnderlying.network);
} catch (IOException | ResourceUnavailableException e) {
teardownAsynchronously();
}
}
// Successful connection, clear failed attempt counter // Successful connection, clear failed attempt counter
mFailedAttempts = 0; mFailedAttempts = 0;
} }
@@ -1433,6 +1432,11 @@ public class VcnGatewayConnection extends StateMachine {
} }
} }
@VisibleForTesting(visibility = Visibility.PRIVATE)
void setTunnelInterface(IpSecTunnelInterface tunnelIface) {
mTunnelIface = tunnelIface;
}
@VisibleForTesting(visibility = Visibility.PRIVATE) @VisibleForTesting(visibility = Visibility.PRIVATE)
UnderlyingNetworkTrackerCallback getUnderlyingNetworkTrackerCallback() { UnderlyingNetworkTrackerCallback getUnderlyingNetworkTrackerCallback() {
return mUnderlyingNetworkTrackerCallback; return mUnderlyingNetworkTrackerCallback;

View File

@@ -16,12 +16,18 @@
package com.android.server.vcn; package com.android.server.vcn;
import static android.net.IpSecManager.IpSecTunnelInterface;
import static com.android.server.vcn.VcnGatewayConnection.DUMMY_ADDR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import android.net.IpSecManager;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
@@ -37,6 +43,11 @@ public class VcnGatewayConnectionDisconnectedStateTest extends VcnGatewayConnect
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
final IpSecTunnelInterface tunnelIface =
mContext.getSystemService(IpSecManager.class)
.createIpSecTunnelInterface(
DUMMY_ADDR, DUMMY_ADDR, TEST_UNDERLYING_NETWORK_RECORD_1.network);
mGatewayConnection.setTunnelInterface(tunnelIface);
mGatewayConnection.transitionTo(mGatewayConnection.mDisconnectedState); mGatewayConnection.transitionTo(mGatewayConnection.mDisconnectedState);
mTestLooper.dispatchAll(); mTestLooper.dispatchAll();
} }