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