blob: 3944fad2ac9eb606e90366d27e38ed4acb70b635 [file] [log] [blame]
markchienb6eb2c22018-07-18 14:29:20 +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 */
16
17package com.android.server.connectivity.tethering;
18
markchienf2731272019-01-16 17:44:13 +080019import static android.net.ConnectivityManager.TETHERING_USB;
20import static android.net.ConnectivityManager.TETHERING_WIFI;
21import static android.net.ConnectivityManager.TETHER_ERROR_ENTITLEMENT_UNKONWN;
22import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
23import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
markchien0b595072019-01-08 23:52:21 +080024import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
markchienf2731272019-01-16 17:44:13 +080025
26import static org.junit.Assert.assertEquals;
markchienb6eb2c22018-07-18 14:29:20 +080027import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertTrue;
markchienf2731272019-01-16 17:44:13 +080029import static org.junit.Assert.fail;
markchienb6eb2c22018-07-18 14:29:20 +080030import static org.mockito.Matchers.anyBoolean;
31import static org.mockito.Matchers.anyString;
32import static org.mockito.Matchers.eq;
33import static org.mockito.Mockito.when;
34
35import android.content.ContentResolver;
36import android.content.Context;
37import android.content.res.Resources;
38import android.net.util.SharedLog;
markchienf2731272019-01-16 17:44:13 +080039import android.os.Bundle;
40import android.os.Message;
markchienb6eb2c22018-07-18 14:29:20 +080041import android.os.PersistableBundle;
markchienf2731272019-01-16 17:44:13 +080042import android.os.ResultReceiver;
43import android.os.test.TestLooper;
44import android.provider.Settings;
markchienb6eb2c22018-07-18 14:29:20 +080045import android.telephony.CarrierConfigManager;
markchienf2731272019-01-16 17:44:13 +080046import android.test.mock.MockContentResolver;
markchienb6eb2c22018-07-18 14:29:20 +080047
Brett Chabot1ae2aa62019-03-04 14:14:56 -080048import androidx.test.filters.SmallTest;
49import androidx.test.runner.AndroidJUnit4;
50
markchienb6eb2c22018-07-18 14:29:20 +080051import com.android.internal.R;
markchienf2731272019-01-16 17:44:13 +080052import com.android.internal.util.State;
53import com.android.internal.util.StateMachine;
54import com.android.internal.util.test.BroadcastInterceptingContext;
55import com.android.internal.util.test.FakeSettingsProvider;
markchienb6eb2c22018-07-18 14:29:20 +080056import com.android.server.connectivity.MockableSystemProperties;
57
58import org.junit.After;
59import org.junit.Before;
60import org.junit.Test;
61import org.junit.runner.RunWith;
62import org.mockito.Mock;
63import org.mockito.MockitoAnnotations;
64
markchienf2731272019-01-16 17:44:13 +080065import java.util.ArrayList;
66import java.util.concurrent.CountDownLatch;
67import java.util.concurrent.TimeUnit;
68
markchienb6eb2c22018-07-18 14:29:20 +080069@RunWith(AndroidJUnit4.class)
70@SmallTest
71public final class EntitlementManagerTest {
72
73 private static final int EVENT_EM_UPDATE = 1;
74 private static final String[] PROVISIONING_APP_NAME = {"some", "app"};
75
76 @Mock private CarrierConfigManager mCarrierConfigManager;
77 @Mock private Context mContext;
markchienb6eb2c22018-07-18 14:29:20 +080078 @Mock private MockableSystemProperties mSystemProperties;
79 @Mock private Resources mResources;
80 @Mock private SharedLog mLog;
81
82 // Like so many Android system APIs, these cannot be mocked because it is marked final.
83 // We have to use the real versions.
84 private final PersistableBundle mCarrierConfig = new PersistableBundle();
markchienf2731272019-01-16 17:44:13 +080085 private final TestLooper mLooper = new TestLooper();
86 private Context mMockContext;
87 private MockContentResolver mContentResolver;
markchienb6eb2c22018-07-18 14:29:20 +080088
markchienf2731272019-01-16 17:44:13 +080089 private TestStateMachine mSM;
90 private WrappedEntitlementManager mEnMgr;
91
92 private class MockContext extends BroadcastInterceptingContext {
93 MockContext(Context base) {
94 super(base);
95 }
96
97 @Override
98 public Resources getResources() {
99 return mResources;
100 }
101
102 @Override
103 public ContentResolver getContentResolver() {
104 return mContentResolver;
105 }
106 }
107
108 public class WrappedEntitlementManager extends EntitlementManager {
109 public int fakeEntitlementResult = TETHER_ERROR_ENTITLEMENT_UNKONWN;
110 public boolean everRunUiEntitlement = false;
111
112 public WrappedEntitlementManager(Context ctx, StateMachine target,
113 SharedLog log, MockableSystemProperties systemProperties) {
114 super(ctx, target, log, systemProperties);
115 }
116
117 @Override
118 protected void runUiTetherProvisioning(int type, ResultReceiver receiver) {
119 everRunUiEntitlement = true;
120 receiver.send(fakeEntitlementResult, null);
121 }
122 }
markchienb6eb2c22018-07-18 14:29:20 +0800123
124 @Before
125 public void setUp() {
126 MockitoAnnotations.initMocks(this);
127
markchienb6eb2c22018-07-18 14:29:20 +0800128 when(mResources.getStringArray(R.array.config_tether_dhcp_range))
129 .thenReturn(new String[0]);
130 when(mResources.getStringArray(R.array.config_tether_usb_regexs))
131 .thenReturn(new String[0]);
132 when(mResources.getStringArray(R.array.config_tether_wifi_regexs))
133 .thenReturn(new String[0]);
134 when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs))
135 .thenReturn(new String[0]);
136 when(mResources.getIntArray(R.array.config_tether_upstream_types))
137 .thenReturn(new int[0]);
138 when(mLog.forSubComponent(anyString())).thenReturn(mLog);
139
markchienf2731272019-01-16 17:44:13 +0800140 mContentResolver = new MockContentResolver();
141 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
142 mMockContext = new MockContext(mContext);
143 mSM = new TestStateMachine();
144 mEnMgr = new WrappedEntitlementManager(mMockContext, mSM, mLog, mSystemProperties);
markchien0b595072019-01-08 23:52:21 +0800145 mEnMgr.updateConfiguration(
146 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800147 }
148
149 @After
markchienf2731272019-01-16 17:44:13 +0800150 public void tearDown() throws Exception {
151 if (mSM != null) {
152 mSM.quit();
153 mSM = null;
154 }
155 }
markchienb6eb2c22018-07-18 14:29:20 +0800156
157 private void setupForRequiredProvisioning() {
158 // Produce some acceptable looking provision app setting if requested.
159 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
160 .thenReturn(PROVISIONING_APP_NAME);
161 // Don't disable tethering provisioning unless requested.
162 when(mSystemProperties.getBoolean(eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY),
163 anyBoolean())).thenReturn(false);
164 // Act like the CarrierConfigManager is present and ready unless told otherwise.
165 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
166 .thenReturn(mCarrierConfigManager);
167 when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig);
168 mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true);
169 }
170
171 @Test
172 public void canRequireProvisioning() {
173 setupForRequiredProvisioning();
markchien0b595072019-01-08 23:52:21 +0800174 mEnMgr.updateConfiguration(
175 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800176 assertTrue(mEnMgr.isTetherProvisioningRequired());
177 }
178
179 @Test
180 public void toleratesCarrierConfigManagerMissing() {
181 setupForRequiredProvisioning();
182 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
183 .thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800184 mEnMgr.updateConfiguration(
185 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800186 // Couldn't get the CarrierConfigManager, but still had a declared provisioning app.
187 // Therefore provisioning still be required.
188 assertTrue(mEnMgr.isTetherProvisioningRequired());
189 }
190
191 @Test
192 public void toleratesCarrierConfigMissing() {
193 setupForRequiredProvisioning();
194 when(mCarrierConfigManager.getConfig()).thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800195 mEnMgr.updateConfiguration(
196 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800197 // We still have a provisioning app configured, so still require provisioning.
198 assertTrue(mEnMgr.isTetherProvisioningRequired());
199 }
200
201 @Test
202 public void provisioningNotRequiredWhenAppNotFound() {
203 setupForRequiredProvisioning();
204 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
205 .thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800206 mEnMgr.updateConfiguration(
207 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800208 assertFalse(mEnMgr.isTetherProvisioningRequired());
209 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
210 .thenReturn(new String[] {"malformedApp"});
markchien0b595072019-01-08 23:52:21 +0800211 mEnMgr.updateConfiguration(
212 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800213 assertFalse(mEnMgr.isTetherProvisioningRequired());
214 }
215
markchienf2731272019-01-16 17:44:13 +0800216 @Test
217 public void testGetLastEntitlementCacheValue() throws Exception {
218 final CountDownLatch mCallbacklatch = new CountDownLatch(1);
219 // 1. Entitlement check is not required.
220 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
221 mEnMgr.everRunUiEntitlement = false;
222 ResultReceiver receiver = new ResultReceiver(null) {
223 @Override
224 protected void onReceiveResult(int resultCode, Bundle resultData) {
225 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
226 mCallbacklatch.countDown();
227 }
228 };
markchien9554abf2019-03-06 16:25:00 +0800229 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800230 callbackTimeoutHelper(mCallbacklatch);
231 assertFalse(mEnMgr.everRunUiEntitlement);
232
233 setupForRequiredProvisioning();
markchien0b595072019-01-08 23:52:21 +0800234 mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog,
235 INVALID_SUBSCRIPTION_ID));
markchienf2731272019-01-16 17:44:13 +0800236 // 2. No cache value and don't need to run entitlement check.
237 mEnMgr.everRunUiEntitlement = false;
238 receiver = new ResultReceiver(null) {
239 @Override
240 protected void onReceiveResult(int resultCode, Bundle resultData) {
241 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
242 mCallbacklatch.countDown();
243 }
244 };
markchien9554abf2019-03-06 16:25:00 +0800245 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800246 callbackTimeoutHelper(mCallbacklatch);
247 assertFalse(mEnMgr.everRunUiEntitlement);
248 // 3. No cache value and ui entitlement check is needed.
249 mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISION_FAILED;
250 mEnMgr.everRunUiEntitlement = false;
251 receiver = new ResultReceiver(null) {
252 @Override
253 protected void onReceiveResult(int resultCode, Bundle resultData) {
254 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
255 mCallbacklatch.countDown();
256 }
257 };
markchien9554abf2019-03-06 16:25:00 +0800258 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800259 mLooper.dispatchAll();
260 callbackTimeoutHelper(mCallbacklatch);
261 assertTrue(mEnMgr.everRunUiEntitlement);
262 // 4. Cache value is TETHER_ERROR_PROVISION_FAILED and don't need to run entitlement check.
263 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
264 mEnMgr.everRunUiEntitlement = false;
265 receiver = new ResultReceiver(null) {
266 @Override
267 protected void onReceiveResult(int resultCode, Bundle resultData) {
268 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
269 mCallbacklatch.countDown();
270 }
271 };
markchien9554abf2019-03-06 16:25:00 +0800272 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800273 callbackTimeoutHelper(mCallbacklatch);
274 assertFalse(mEnMgr.everRunUiEntitlement);
275 // 5. Cache value is TETHER_ERROR_PROVISION_FAILED and ui entitlement check is needed.
276 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
277 mEnMgr.everRunUiEntitlement = false;
278 receiver = new ResultReceiver(null) {
279 @Override
280 protected void onReceiveResult(int resultCode, Bundle resultData) {
281 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
282 mCallbacklatch.countDown();
283 }
284 };
markchien9554abf2019-03-06 16:25:00 +0800285 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800286 mLooper.dispatchAll();
287 callbackTimeoutHelper(mCallbacklatch);
288 assertTrue(mEnMgr.everRunUiEntitlement);
289 // 6. Cache value is TETHER_ERROR_NO_ERROR.
290 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
291 mEnMgr.everRunUiEntitlement = false;
292 receiver = new ResultReceiver(null) {
293 @Override
294 protected void onReceiveResult(int resultCode, Bundle resultData) {
295 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
296 mCallbacklatch.countDown();
297 }
298 };
markchien9554abf2019-03-06 16:25:00 +0800299 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800300 callbackTimeoutHelper(mCallbacklatch);
301 assertFalse(mEnMgr.everRunUiEntitlement);
302 // 7. Test get value for other downstream type.
303 mEnMgr.everRunUiEntitlement = false;
304 receiver = new ResultReceiver(null) {
305 @Override
306 protected void onReceiveResult(int resultCode, Bundle resultData) {
307 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
308 mCallbacklatch.countDown();
309 }
310 };
markchien9554abf2019-03-06 16:25:00 +0800311 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_USB, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800312 callbackTimeoutHelper(mCallbacklatch);
313 assertFalse(mEnMgr.everRunUiEntitlement);
314 }
315
316 void callbackTimeoutHelper(final CountDownLatch latch) throws Exception {
317 if (!latch.await(1, TimeUnit.SECONDS)) {
318 fail("Timout, fail to recieve callback");
319 }
320 }
321 public class TestStateMachine extends StateMachine {
322 public final ArrayList<Message> messages = new ArrayList<>();
323 private final State mLoggingState =
324 new EntitlementManagerTest.TestStateMachine.LoggingState();
325
326 class LoggingState extends State {
327 @Override public void enter() {
328 messages.clear();
329 }
330
331 @Override public void exit() {
332 messages.clear();
333 }
334
335 @Override public boolean processMessage(Message msg) {
336 messages.add(msg);
337 return false;
338 }
339 }
340
341 public TestStateMachine() {
342 super("EntitlementManagerTest.TestStateMachine", mLooper.getLooper());
343 addState(mLoggingState);
344 setInitialState(mLoggingState);
345 super.start();
346 }
347 }
markchienb6eb2c22018-07-18 14:29:20 +0800348}