From 23e025e47ccb0b2f19d2dc428809880331d760e0 Mon Sep 17 00:00:00 2001 From: Yan Yan Date: Thu, 16 Apr 2020 00:19:16 +0000 Subject: [PATCH 1/2] Create TestNetworkUtils for IKE and IPsec CTS Create TestNetworkUtils that provides interfaces to set up test network. It will be used by both IKE and IPsec CTS Bug: 148689509 Test: atest CtsIkeTestCases Change-Id: I1c49711d3c6ce03ceafdbf3004e25d9d59a6201c Merged-In: I1c49711d3c6ce03ceafdbf3004e25d9d59a6201c (cherry picked from commit 91b034d5c649d8b7de1ff6d936f4859f927eb202) --- .../net/ipsec/ike/cts/TestNetworkUtils.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/TestNetworkUtils.java diff --git a/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/TestNetworkUtils.java b/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/TestNetworkUtils.java new file mode 100644 index 0000000000..5b08cdc8f2 --- /dev/null +++ b/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/TestNetworkUtils.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net.ipsec.ike.cts; + +import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; +import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; +import static android.net.NetworkCapabilities.TRANSPORT_TEST; + +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkRequest; +import android.net.TestNetworkManager; +import android.os.IBinder; +import android.os.RemoteException; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +// TODO(b/148689509): Share this class with net CTS test (e.g. IpSecManagerTunnelTest) +public class TestNetworkUtils { + private static final int TIMEOUT_MS = 500; + + /** Callback to receive requested test network. */ + public static class TestNetworkCallback extends ConnectivityManager.NetworkCallback { + private final CompletableFuture futureNetwork = new CompletableFuture<>(); + + @Override + public void onAvailable(Network network) { + futureNetwork.complete(network); + } + + public Network getNetworkBlocking() throws Exception { + return futureNetwork.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); + } + } + + /** + * Set up test network. + * + *

Caller MUST have MANAGE_TEST_NETWORKS permission to use this method. + * + * @param connMgr ConnectivityManager to request network. + * @param testNetworkMgr TestNetworkManager to set up test network. + * @param ifname the name of the interface to be used for the Network LinkProperties. + * @param binder a binder object guarding the lifecycle of this test network. + * @return TestNetworkCallback to retrieve the test network. + * @throws RemoteException if test network setup failed. + * @see android.net.TestNetworkManager + */ + public static TestNetworkCallback setupAndGetTestNetwork( + ConnectivityManager connMgr, + TestNetworkManager testNetworkMgr, + String ifname, + IBinder binder) + throws RemoteException { + NetworkRequest nr = + new NetworkRequest.Builder() + .addTransportType(TRANSPORT_TEST) + .removeCapability(NET_CAPABILITY_TRUSTED) + .removeCapability(NET_CAPABILITY_NOT_VPN) + .setNetworkSpecifier(ifname) + .build(); + + TestNetworkCallback cb = new TestNetworkCallback(); + connMgr.requestNetwork(nr, cb); + + // Setup the test network after network request is filed to prevent Network from being + // reaped due to no requests matching it. + testNetworkMgr.setupTestNetwork(ifname, binder); + + return cb; + } +} From de7f511bd1866d17c8c5e259a6363bba5340571c Mon Sep 17 00:00:00 2001 From: Yan Yan Date: Thu, 23 Apr 2020 23:20:51 +0000 Subject: [PATCH 2/2] Create base class that sets up test network This class will be extended by both IkeSessionParamsTest and IkeSessionTestBase Bug: 148689509 Test: atest CtsIkeTestCases Change-Id: I98979758a7a684219e35c02ded93224ea172d44f Merged-In: I98979758a7a684219e35c02ded93224ea172d44f (cherry picked from commit 50908b926635355f0753b7388f96b684f6be351d) --- .../ike/cts/IkeSessionParamsTestBase.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/IkeSessionParamsTestBase.java diff --git a/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/IkeSessionParamsTestBase.java b/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/IkeSessionParamsTestBase.java new file mode 100644 index 0000000000..c3e3ba353c --- /dev/null +++ b/tests/cts/net/ipsec/src/android/net/ipsec/ike/cts/IkeSessionParamsTestBase.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net.ipsec.ike.cts; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.LinkAddress; +import android.net.Network; +import android.net.TestNetworkInterface; +import android.net.TestNetworkManager; +import android.net.ipsec.ike.cts.TestNetworkUtils.TestNetworkCallback; +import android.os.Binder; +import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.platform.test.annotations.AppModeFull; + +import androidx.test.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@AppModeFull(reason = "MANAGE_TEST_NETWORKS permission can't be granted to instant apps") +abstract class IkeSessionParamsTestBase extends IkeTestBase { + // Static state to reduce setup/teardown + static ConnectivityManager sCM; + static TestNetworkManager sTNM; + static ParcelFileDescriptor sTunFd; + static TestNetworkCallback sTunNetworkCallback; + static Network sTunNetwork; + + static Context sContext = InstrumentationRegistry.getContext(); + static IBinder sBinder = new Binder(); + + // This method is guaranteed to run in subclasses and will run before subclasses' @BeforeClass + // methods. + @BeforeClass + public static void setUpTestNetworkBeforeClass() throws Exception { + InstrumentationRegistry.getInstrumentation() + .getUiAutomation() + .adoptShellPermissionIdentity(); + sCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE); + sTNM = (TestNetworkManager) sContext.getSystemService(Context.TEST_NETWORK_SERVICE); + + TestNetworkInterface testIface = + sTNM.createTunInterface( + new LinkAddress[] {new LinkAddress(IPV4_ADDRESS_LOCAL, IP4_PREFIX_LEN)}); + + sTunFd = testIface.getFileDescriptor(); + sTunNetworkCallback = + TestNetworkUtils.setupAndGetTestNetwork( + sCM, sTNM, testIface.getInterfaceName(), sBinder); + sTunNetwork = sTunNetworkCallback.getNetworkBlocking(); + } + + // This method is guaranteed to run in subclasses and will run after subclasses' @AfterClass + // methods. + @AfterClass + public static void tearDownTestNetworkAfterClass() throws Exception { + sCM.unregisterNetworkCallback(sTunNetworkCallback); + + sTNM.teardownTestNetwork(sTunNetwork); + sTunFd.close(); + + InstrumentationRegistry.getInstrumentation() + .getUiAutomation() + .dropShellPermissionIdentity(); + } +}