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.Nullable; 20 import android.annotation.TestApi; 21 import android.os.IBinder; 22 import android.os.RemoteException; 23 24 import com.android.internal.util.Preconditions; 25 26 /** 27 * Class that allows creation and management of per-app, test-only networks 28 * 29 * @hide 30 */ 31 @TestApi 32 public class TestNetworkManager { 33 /** 34 * Prefix for tun interfaces created by this class. 35 * @hide 36 */ 37 public static final String TEST_TUN_PREFIX = "testtun"; 38 39 /** 40 * Prefix for tap interfaces created by this class. 41 * @hide 42 */ 43 public static final String TEST_TAP_PREFIX = "testtap"; 44 45 @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName(); 46 47 @NonNull private final ITestNetworkManager mService; 48 49 /** @hide */ TestNetworkManager(@onNull ITestNetworkManager service)50 public TestNetworkManager(@NonNull ITestNetworkManager service) { 51 mService = Preconditions.checkNotNull(service, "missing ITestNetworkManager"); 52 } 53 54 /** 55 * Teardown the capability-limited, testing-only network for a given interface 56 * 57 * @param network The test network that should be torn down 58 * @hide 59 */ 60 @TestApi teardownTestNetwork(@onNull Network network)61 public void teardownTestNetwork(@NonNull Network network) { 62 try { 63 mService.teardownTestNetwork(network.netId); 64 } catch (RemoteException e) { 65 throw e.rethrowFromSystemServer(); 66 } 67 } 68 setupTestNetwork( @onNull String iface, @Nullable LinkProperties lp, boolean isMetered, @NonNull int[] administratorUids, @NonNull IBinder binder)69 private void setupTestNetwork( 70 @NonNull String iface, 71 @Nullable LinkProperties lp, 72 boolean isMetered, 73 @NonNull int[] administratorUids, 74 @NonNull IBinder binder) { 75 try { 76 mService.setupTestNetwork(iface, lp, isMetered, administratorUids, binder); 77 } catch (RemoteException e) { 78 throw e.rethrowFromSystemServer(); 79 } 80 } 81 82 /** 83 * Sets up a capability-limited, testing-only network for a given interface 84 * 85 * @param lp The LinkProperties for the TestNetworkService to use for this test network. Note 86 * that the interface name and link addresses will be overwritten, and the passed-in values 87 * discarded. 88 * @param isMetered Whether or not the network should be considered metered. 89 * @param binder A binder object guarding the lifecycle of this test network. 90 * @hide 91 */ setupTestNetwork( @onNull LinkProperties lp, boolean isMetered, @NonNull IBinder binder)92 public void setupTestNetwork( 93 @NonNull LinkProperties lp, boolean isMetered, @NonNull IBinder binder) { 94 Preconditions.checkNotNull(lp, "Invalid LinkProperties"); 95 setupTestNetwork(lp.getInterfaceName(), lp, isMetered, new int[0], binder); 96 } 97 98 /** 99 * Sets up a capability-limited, testing-only network for a given interface 100 * 101 * @param iface the name of the interface to be used for the Network LinkProperties. 102 * @param binder A binder object guarding the lifecycle of this test network. 103 * @hide 104 */ 105 @TestApi setupTestNetwork(@onNull String iface, @NonNull IBinder binder)106 public void setupTestNetwork(@NonNull String iface, @NonNull IBinder binder) { 107 setupTestNetwork(iface, null, true, new int[0], binder); 108 } 109 110 /** 111 * Sets up a capability-limited, testing-only network for a given interface with the given 112 * administrator UIDs. 113 * 114 * @param iface the name of the interface to be used for the Network LinkProperties. 115 * @param administratorUids The administrator UIDs to be used for the test-only network 116 * @param binder A binder object guarding the lifecycle of this test network. 117 * @hide 118 */ setupTestNetwork( @onNull String iface, @NonNull int[] administratorUids, @NonNull IBinder binder)119 public void setupTestNetwork( 120 @NonNull String iface, @NonNull int[] administratorUids, @NonNull IBinder binder) { 121 setupTestNetwork(iface, null, true, administratorUids, binder); 122 } 123 124 /** 125 * Create a tun interface for testing purposes 126 * 127 * @param linkAddrs an array of LinkAddresses to assign to the TUN interface 128 * @return A ParcelFileDescriptor of the underlying TUN interface. Close this to tear down the 129 * TUN interface. 130 * @hide 131 */ 132 @TestApi createTunInterface(@onNull LinkAddress[] linkAddrs)133 public TestNetworkInterface createTunInterface(@NonNull LinkAddress[] linkAddrs) { 134 try { 135 return mService.createTunInterface(linkAddrs); 136 } catch (RemoteException e) { 137 throw e.rethrowFromSystemServer(); 138 } 139 } 140 141 /** 142 * Create a tap interface for testing purposes 143 * 144 * @return A ParcelFileDescriptor of the underlying TAP interface. Close this to tear down the 145 * TAP interface. 146 * @hide 147 */ 148 @TestApi createTapInterface()149 public TestNetworkInterface createTapInterface() { 150 try { 151 return mService.createTapInterface(); 152 } catch (RemoteException e) { 153 throw e.rethrowFromSystemServer(); 154 } 155 } 156 157 } 158