1 /* 2 * Copyright (C) 2018 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 package android.net; 17 18 import android.annotation.NonNull; 19 import android.annotation.TestApi; 20 import android.os.IBinder; 21 import android.os.RemoteException; 22 23 import com.android.internal.util.Preconditions; 24 25 /** 26 * Class that allows creation and management of per-app, test-only networks 27 * 28 * @hide 29 */ 30 @TestApi 31 public class TestNetworkManager { 32 @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName(); 33 34 @NonNull private final ITestNetworkManager mService; 35 36 /** @hide */ TestNetworkManager(@onNull ITestNetworkManager service)37 public TestNetworkManager(@NonNull ITestNetworkManager service) { 38 mService = Preconditions.checkNotNull(service, "missing ITestNetworkManager"); 39 } 40 41 /** 42 * Teardown the capability-limited, testing-only network for a given interface 43 * 44 * @param network The test network that should be torn down 45 * @hide 46 */ 47 @TestApi teardownTestNetwork(@onNull Network network)48 public void teardownTestNetwork(@NonNull Network network) { 49 try { 50 mService.teardownTestNetwork(network.netId); 51 } catch (RemoteException e) { 52 throw e.rethrowFromSystemServer(); 53 } 54 } 55 56 /** 57 * Sets up a capability-limited, testing-only network for a given interface 58 * 59 * @param iface the name of the interface to be used for the Network LinkProperties. 60 * @param binder A binder object guarding the lifecycle of this test network. 61 * @hide 62 */ 63 @TestApi setupTestNetwork(@onNull String iface, @NonNull IBinder binder)64 public void setupTestNetwork(@NonNull String iface, @NonNull IBinder binder) { 65 try { 66 mService.setupTestNetwork(iface, binder); 67 } catch (RemoteException e) { 68 throw e.rethrowFromSystemServer(); 69 } 70 } 71 72 /** 73 * Create a tun interface for testing purposes 74 * 75 * @param linkAddrs an array of LinkAddresses to assign to the TUN interface 76 * @return A ParcelFileDescriptor of the underlying TUN interface. Close this to tear down the 77 * TUN interface. 78 * @hide 79 */ 80 @TestApi createTunInterface(@onNull LinkAddress[] linkAddrs)81 public TestNetworkInterface createTunInterface(@NonNull LinkAddress[] linkAddrs) { 82 try { 83 return mService.createTunInterface(linkAddrs); 84 } catch (RemoteException e) { 85 throw e.rethrowFromSystemServer(); 86 } 87 } 88 89 /** 90 * Create a tap interface for testing purposes 91 * 92 * @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the 93 * TAP interface. 94 * @hide 95 */ 96 @TestApi createTapInterface()97 public TestNetworkInterface createTapInterface() { 98 try { 99 return mService.createTapInterface(); 100 } catch (RemoteException e) { 101 throw e.rethrowFromSystemServer(); 102 } 103 } 104 105 } 106