1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.ipsec.ike.cts; 18 19 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; 20 import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; 21 import static android.net.NetworkCapabilities.TRANSPORT_TEST; 22 23 import android.net.ConnectivityManager; 24 import android.net.Network; 25 import android.net.NetworkRequest; 26 import android.net.TestNetworkManager; 27 import android.os.IBinder; 28 import android.os.RemoteException; 29 30 import java.util.concurrent.CompletableFuture; 31 import java.util.concurrent.TimeUnit; 32 33 // TODO(b/148689509): Share this class with net CTS test (e.g. IpSecManagerTunnelTest) 34 public class TestNetworkUtils { 35 private static final int TIMEOUT_MS = 3000; 36 37 /** Callback to receive requested test network. */ 38 public static class TestNetworkCallback extends ConnectivityManager.NetworkCallback { 39 private final CompletableFuture<Network> futureNetwork = new CompletableFuture<>(); 40 41 @Override onAvailable(Network network)42 public void onAvailable(Network network) { 43 futureNetwork.complete(network); 44 } 45 getNetworkBlocking()46 public Network getNetworkBlocking() throws Exception { 47 return futureNetwork.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); 48 } 49 } 50 51 /** 52 * Set up test network. 53 * 54 * <p>Caller MUST have MANAGE_TEST_NETWORKS permission to use this method. 55 * 56 * @param connMgr ConnectivityManager to request network. 57 * @param testNetworkMgr TestNetworkManager to set up test network. 58 * @param ifname the name of the interface to be used for the Network LinkProperties. 59 * @param binder a binder object guarding the lifecycle of this test network. 60 * @return TestNetworkCallback to retrieve the test network. 61 * @throws RemoteException if test network setup failed. 62 * @see android.net.TestNetworkManager 63 */ setupAndGetTestNetwork( ConnectivityManager connMgr, TestNetworkManager testNetworkMgr, String ifname, IBinder binder)64 public static TestNetworkCallback setupAndGetTestNetwork( 65 ConnectivityManager connMgr, 66 TestNetworkManager testNetworkMgr, 67 String ifname, 68 IBinder binder) 69 throws RemoteException { 70 NetworkRequest nr = 71 new NetworkRequest.Builder() 72 .addTransportType(TRANSPORT_TEST) 73 .removeCapability(NET_CAPABILITY_TRUSTED) 74 .removeCapability(NET_CAPABILITY_NOT_VPN) 75 .setNetworkSpecifier(ifname) 76 .build(); 77 78 TestNetworkCallback cb = new TestNetworkCallback(); 79 connMgr.requestNetwork(nr, cb); 80 81 // Setup the test network after network request is filed to prevent Network from being 82 // reaped due to no requests matching it. 83 testNetworkMgr.setupTestNetwork(ifname, binder); 84 85 return cb; 86 } 87 } 88