blob: a0a563b37025695cb76f8f1346c385b9196b73a7 [file] [log] [blame]
Benedict Wonge40eab62018-11-14 17:50:13 -08001/*
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 */
16package android.net;
17
18import android.annotation.NonNull;
Automerger Merge Workerfcfddef2020-03-16 20:31:58 +000019import android.annotation.Nullable;
Benedict Wonge40eab62018-11-14 17:50:13 -080020import android.annotation.TestApi;
Benedict Wonge40eab62018-11-14 17:50:13 -080021import android.os.IBinder;
22import android.os.RemoteException;
23
24import 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
32public class TestNetworkManager {
Lorenzo Colitti6fba2a82020-03-19 11:46:55 +000033 /**
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 Wonge40eab62018-11-14 17:50:13 -080045 @NonNull private static final String TAG = TestNetworkManager.class.getSimpleName();
46
47 @NonNull private final ITestNetworkManager mService;
Benedict Wonge40eab62018-11-14 17:50:13 -080048
49 /** @hide */
Benedict Wong7df36ed2019-03-12 21:54:16 -070050 public TestNetworkManager(@NonNull ITestNetworkManager service) {
Benedict Wonge40eab62018-11-14 17:50:13 -080051 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 Workerfcfddef2020-03-16 20:31:58 +000069 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 Wonge40eab62018-11-14 17:50:13 -080082 /**
83 * Sets up a capability-limited, testing-only network for a given interface
84 *
Benedict Wong512ab0d2019-04-18 19:18:43 -070085 * @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 Workerfcfddef2020-03-16 20:31:58 +000095 setupTestNetwork(lp.getInterfaceName(), lp, isMetered, new int[0], binder);
Benedict Wong512ab0d2019-04-18 19:18:43 -070096 }
97
98 /**
99 * Sets up a capability-limited, testing-only network for a given interface
100 *
Benedict Wonge40eab62018-11-14 17:50:13 -0800101 * @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 Workerfcfddef2020-03-16 20:31:58 +0000107 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 Wonge40eab62018-11-14 17:50:13 -0800122 }
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 Colittib15fcce2019-04-01 23:41:12 +0900140
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 Wonge40eab62018-11-14 17:50:13 -0800157}