Lazy-create IpSecTunnelInterface

With the Network(int) constructor moving to mainline, we cannot use the
constructor, and should just create the IpSecTunnelInterface lazily, at
first use.

Bug: 165827287
Test: atest FrameworksVcnTests
Change-Id: Ic01858e0f9c80d316676cfbae5edca114655c3fe
This commit is contained in:
Benedict Wong
2021-02-09 18:41:01 -08:00
parent cebde1ed9a
commit acf7a30f3e
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 {
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 =
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 String DISCONNECT_REASON_INTERNAL_ERROR = "Uncaught exception: ";
@@ -412,11 +413,11 @@ public class VcnGatewayConnection extends StateMachine {
@NonNull private final VcnGatewayConnectionConfig mConnectionConfig;
@NonNull private final VcnGatewayStatusCallback mGatewayStatusCallback;
@NonNull private final Dependencies mDeps;
@NonNull private final VcnUnderlyingNetworkTrackerCallback mUnderlyingNetworkTrackerCallback;
@NonNull private final IpSecManager mIpSecManager;
@NonNull private final IpSecTunnelInterface mTunnelIface;
@Nullable private IpSecTunnelInterface mTunnelIface = null;
/** Running state of this VcnGatewayConnection. */
private boolean mIsRunning = true;
@@ -526,20 +527,6 @@ public class VcnGatewayConnection extends StateMachine {
mUnderlyingNetworkTrackerCallback);
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(mDisconnectingState);
addState(mConnectingState);
@@ -1117,6 +1104,18 @@ public class VcnGatewayConnection extends StateMachine {
class ConnectedState extends ConnectedStateBase {
@Override
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
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)
UnderlyingNetworkTrackerCallback getUnderlyingNetworkTrackerCallback() {
return mUnderlyingNetworkTrackerCallback;

View File

@@ -16,12 +16,18 @@
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.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import android.net.IpSecManager;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@@ -37,6 +43,11 @@ public class VcnGatewayConnectionDisconnectedStateTest extends VcnGatewayConnect
public void setUp() throws Exception {
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);
mTestLooper.dispatchAll();
}