Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 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; |
Automerger Merge Worker | fcfddef | 2020-03-16 20:31:58 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 20 | import android.annotation.TestApi; |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 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 { |
Lorenzo Colitti | 6fba2a8 | 2020-03-19 11:46:55 +0000 | [diff] [blame] | 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 | |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 45 | @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName(); |
| 46 | |
| 47 | @NonNull private final ITestNetworkManager mService; |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 48 | |
| 49 | /** @hide */ |
Benedict Wong | 7df36ed | 2019-03-12 21:54:16 -0700 | [diff] [blame] | 50 | public TestNetworkManager(@NonNull ITestNetworkManager service) { |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 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 |
| 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 | |
Automerger Merge Worker | fcfddef | 2020-03-16 20:31:58 +0000 | [diff] [blame] | 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 | |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Sets up a capability-limited, testing-only network for a given interface |
| 84 | * |
Benedict Wong | 512ab0d | 2019-04-18 19:18:43 -0700 | [diff] [blame] | 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 | */ |
| 92 | public void setupTestNetwork( |
| 93 | @NonNull LinkProperties lp, boolean isMetered, @NonNull IBinder binder) { |
| 94 | Preconditions.checkNotNull(lp, "Invalid LinkProperties"); |
Automerger Merge Worker | fcfddef | 2020-03-16 20:31:58 +0000 | [diff] [blame] | 95 | setupTestNetwork(lp.getInterfaceName(), lp, isMetered, new int[0], binder); |
Benedict Wong | 512ab0d | 2019-04-18 19:18:43 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets up a capability-limited, testing-only network for a given interface |
| 100 | * |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 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 |
| 106 | public void setupTestNetwork(@NonNull String iface, @NonNull IBinder binder) { |
Automerger Merge Worker | fcfddef | 2020-03-16 20:31:58 +0000 | [diff] [blame] | 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 | */ |
| 119 | public void setupTestNetwork( |
| 120 | @NonNull String iface, @NonNull int[] administratorUids, @NonNull IBinder binder) { |
| 121 | setupTestNetwork(iface, null, true, administratorUids, binder); |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 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 |
| 133 | public TestNetworkInterface createTunInterface(@NonNull LinkAddress[] linkAddrs) { |
| 134 | try { |
| 135 | return mService.createTunInterface(linkAddrs); |
| 136 | } catch (RemoteException e) { |
| 137 | throw e.rethrowFromSystemServer(); |
| 138 | } |
| 139 | } |
Lorenzo Colitti | b15fcce | 2019-04-01 23:41:12 +0900 | [diff] [blame] | 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 |
| 149 | public TestNetworkInterface createTapInterface() { |
| 150 | try { |
| 151 | return mService.createTapInterface(); |
| 152 | } catch (RemoteException e) { |
| 153 | throw e.rethrowFromSystemServer(); |
| 154 | } |
| 155 | } |
| 156 | |
Benedict Wong | e40eab6 | 2018-11-14 17:50:13 -0800 | [diff] [blame] | 157 | } |