blob: bac509802258886f7d849426b4947d0d9a8d5c34 [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);
markchien29a650c2019-03-19 20:57:04 +0800169 mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
markchienb6eb2c22018-07-18 14:29:20 +0800170 }
171
172 @Test
173 public void canRequireProvisioning() {
174 setupForRequiredProvisioning();
markchien0b595072019-01-08 23:52:21 +0800175 mEnMgr.updateConfiguration(
176 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800177 assertTrue(mEnMgr.isTetherProvisioningRequired());
178 }
179
180 @Test
181 public void toleratesCarrierConfigManagerMissing() {
182 setupForRequiredProvisioning();
183 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
184 .thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800185 mEnMgr.updateConfiguration(
186 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800187 // Couldn't get the CarrierConfigManager, but still had a declared provisioning app.
188 // Therefore provisioning still be required.
189 assertTrue(mEnMgr.isTetherProvisioningRequired());
190 }
191
192 @Test
193 public void toleratesCarrierConfigMissing() {
194 setupForRequiredProvisioning();
195 when(mCarrierConfigManager.getConfig()).thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800196 mEnMgr.updateConfiguration(
197 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800198 // We still have a provisioning app configured, so still require provisioning.
199 assertTrue(mEnMgr.isTetherProvisioningRequired());
200 }
201
202 @Test
markchien29a650c2019-03-19 20:57:04 +0800203 public void toleratesCarrierConfigNotLoaded() {
204 setupForRequiredProvisioning();
205 mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, false);
206 mEnMgr.updateConfiguration(
207 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
208 // We still have a provisioning app configured, so still require provisioning.
209 assertTrue(mEnMgr.isTetherProvisioningRequired());
210 }
211
212 @Test
markchienb6eb2c22018-07-18 14:29:20 +0800213 public void provisioningNotRequiredWhenAppNotFound() {
214 setupForRequiredProvisioning();
215 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
216 .thenReturn(null);
markchien0b595072019-01-08 23:52:21 +0800217 mEnMgr.updateConfiguration(
218 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800219 assertFalse(mEnMgr.isTetherProvisioningRequired());
220 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
221 .thenReturn(new String[] {"malformedApp"});
markchien0b595072019-01-08 23:52:21 +0800222 mEnMgr.updateConfiguration(
223 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800224 assertFalse(mEnMgr.isTetherProvisioningRequired());
225 }
226
markchienf2731272019-01-16 17:44:13 +0800227 @Test
228 public void testGetLastEntitlementCacheValue() throws Exception {
229 final CountDownLatch mCallbacklatch = new CountDownLatch(1);
230 // 1. Entitlement check is not required.
231 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
232 mEnMgr.everRunUiEntitlement = false;
233 ResultReceiver receiver = new ResultReceiver(null) {
234 @Override
235 protected void onReceiveResult(int resultCode, Bundle resultData) {
236 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
237 mCallbacklatch.countDown();
238 }
239 };
markchien9554abf2019-03-06 16:25:00 +0800240 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800241 callbackTimeoutHelper(mCallbacklatch);
242 assertFalse(mEnMgr.everRunUiEntitlement);
243
244 setupForRequiredProvisioning();
markchien0b595072019-01-08 23:52:21 +0800245 mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog,
246 INVALID_SUBSCRIPTION_ID));
markchienf2731272019-01-16 17:44:13 +0800247 // 2. No cache value and don't need to run entitlement check.
248 mEnMgr.everRunUiEntitlement = false;
249 receiver = new ResultReceiver(null) {
250 @Override
251 protected void onReceiveResult(int resultCode, Bundle resultData) {
252 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
253 mCallbacklatch.countDown();
254 }
255 };
markchien9554abf2019-03-06 16:25:00 +0800256 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800257 callbackTimeoutHelper(mCallbacklatch);
258 assertFalse(mEnMgr.everRunUiEntitlement);
259 // 3. No cache value and ui entitlement check is needed.
260 mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISION_FAILED;
261 mEnMgr.everRunUiEntitlement = false;
262 receiver = new ResultReceiver(null) {
263 @Override
264 protected void onReceiveResult(int resultCode, Bundle resultData) {
265 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
266 mCallbacklatch.countDown();
267 }
268 };
markchien9554abf2019-03-06 16:25:00 +0800269 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800270 mLooper.dispatchAll();
271 callbackTimeoutHelper(mCallbacklatch);
272 assertTrue(mEnMgr.everRunUiEntitlement);
273 // 4. Cache value is TETHER_ERROR_PROVISION_FAILED and don't need to run entitlement check.
274 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
275 mEnMgr.everRunUiEntitlement = false;
276 receiver = new ResultReceiver(null) {
277 @Override
278 protected void onReceiveResult(int resultCode, Bundle resultData) {
279 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
280 mCallbacklatch.countDown();
281 }
282 };
markchien9554abf2019-03-06 16:25:00 +0800283 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800284 callbackTimeoutHelper(mCallbacklatch);
285 assertFalse(mEnMgr.everRunUiEntitlement);
286 // 5. Cache value is TETHER_ERROR_PROVISION_FAILED and ui entitlement check is needed.
287 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
288 mEnMgr.everRunUiEntitlement = false;
289 receiver = new ResultReceiver(null) {
290 @Override
291 protected void onReceiveResult(int resultCode, Bundle resultData) {
292 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
293 mCallbacklatch.countDown();
294 }
295 };
markchien9554abf2019-03-06 16:25:00 +0800296 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800297 mLooper.dispatchAll();
298 callbackTimeoutHelper(mCallbacklatch);
299 assertTrue(mEnMgr.everRunUiEntitlement);
300 // 6. Cache value is TETHER_ERROR_NO_ERROR.
301 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
302 mEnMgr.everRunUiEntitlement = false;
303 receiver = new ResultReceiver(null) {
304 @Override
305 protected void onReceiveResult(int resultCode, Bundle resultData) {
306 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
307 mCallbacklatch.countDown();
308 }
309 };
markchien9554abf2019-03-06 16:25:00 +0800310 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true);
markchienf2731272019-01-16 17:44:13 +0800311 callbackTimeoutHelper(mCallbacklatch);
312 assertFalse(mEnMgr.everRunUiEntitlement);
313 // 7. Test get value for other downstream type.
314 mEnMgr.everRunUiEntitlement = false;
315 receiver = new ResultReceiver(null) {
316 @Override
317 protected void onReceiveResult(int resultCode, Bundle resultData) {
318 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
319 mCallbacklatch.countDown();
320 }
321 };
markchien9554abf2019-03-06 16:25:00 +0800322 mEnMgr.getLatestTetheringEntitlementResult(TETHERING_USB, receiver, false);
markchienf2731272019-01-16 17:44:13 +0800323 callbackTimeoutHelper(mCallbacklatch);
324 assertFalse(mEnMgr.everRunUiEntitlement);
325 }
326
327 void callbackTimeoutHelper(final CountDownLatch latch) throws Exception {
328 if (!latch.await(1, TimeUnit.SECONDS)) {
329 fail("Timout, fail to recieve callback");
330 }
331 }
332 public class TestStateMachine extends StateMachine {
333 public final ArrayList<Message> messages = new ArrayList<>();
334 private final State mLoggingState =
335 new EntitlementManagerTest.TestStateMachine.LoggingState();
336
337 class LoggingState extends State {
338 @Override public void enter() {
339 messages.clear();
340 }
341
342 @Override public void exit() {
343 messages.clear();
344 }
345
346 @Override public boolean processMessage(Message msg) {
347 messages.add(msg);
348 return false;
349 }
350 }
351
352 public TestStateMachine() {
353 super("EntitlementManagerTest.TestStateMachine", mLooper.getLooper());
354 addState(mLoggingState);
355 setInitialState(mLoggingState);
356 super.start();
357 }
358 }
markchienb6eb2c22018-07-18 14:29:20 +0800359}