blob: 193f3806dbf6d5f310970436c8c8fc3b238063e4 [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;
markchien293422f2019-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.support.test.filters.SmallTest;
46import android.support.test.runner.AndroidJUnit4;
47import android.telephony.CarrierConfigManager;
markchienf2731272019-01-16 17:44:13 +080048import android.test.mock.MockContentResolver;
markchienb6eb2c22018-07-18 14:29:20 +080049
50import com.android.internal.R;
markchienf2731272019-01-16 17:44:13 +080051import com.android.internal.util.State;
52import com.android.internal.util.StateMachine;
53import com.android.internal.util.test.BroadcastInterceptingContext;
54import com.android.internal.util.test.FakeSettingsProvider;
markchienb6eb2c22018-07-18 14:29:20 +080055import com.android.server.connectivity.MockableSystemProperties;
56
57import org.junit.After;
58import org.junit.Before;
59import org.junit.Test;
60import org.junit.runner.RunWith;
61import org.mockito.Mock;
62import org.mockito.MockitoAnnotations;
63
markchienf2731272019-01-16 17:44:13 +080064import java.util.ArrayList;
65import java.util.concurrent.CountDownLatch;
66import java.util.concurrent.TimeUnit;
67
markchienb6eb2c22018-07-18 14:29:20 +080068@RunWith(AndroidJUnit4.class)
69@SmallTest
70public final class EntitlementManagerTest {
71
72 private static final int EVENT_EM_UPDATE = 1;
73 private static final String[] PROVISIONING_APP_NAME = {"some", "app"};
74
75 @Mock private CarrierConfigManager mCarrierConfigManager;
76 @Mock private Context mContext;
markchienb6eb2c22018-07-18 14:29:20 +080077 @Mock private MockableSystemProperties mSystemProperties;
78 @Mock private Resources mResources;
79 @Mock private SharedLog mLog;
80
81 // Like so many Android system APIs, these cannot be mocked because it is marked final.
82 // We have to use the real versions.
83 private final PersistableBundle mCarrierConfig = new PersistableBundle();
markchienf2731272019-01-16 17:44:13 +080084 private final TestLooper mLooper = new TestLooper();
85 private Context mMockContext;
86 private MockContentResolver mContentResolver;
markchienb6eb2c22018-07-18 14:29:20 +080087
markchienf2731272019-01-16 17:44:13 +080088 private TestStateMachine mSM;
89 private WrappedEntitlementManager mEnMgr;
90
91 private class MockContext extends BroadcastInterceptingContext {
92 MockContext(Context base) {
93 super(base);
94 }
95
96 @Override
97 public Resources getResources() {
98 return mResources;
99 }
100
101 @Override
102 public ContentResolver getContentResolver() {
103 return mContentResolver;
104 }
105 }
106
107 public class WrappedEntitlementManager extends EntitlementManager {
108 public int fakeEntitlementResult = TETHER_ERROR_ENTITLEMENT_UNKONWN;
109 public boolean everRunUiEntitlement = false;
110
111 public WrappedEntitlementManager(Context ctx, StateMachine target,
112 SharedLog log, MockableSystemProperties systemProperties) {
113 super(ctx, target, log, systemProperties);
114 }
115
116 @Override
117 protected void runUiTetherProvisioning(int type, ResultReceiver receiver) {
118 everRunUiEntitlement = true;
119 receiver.send(fakeEntitlementResult, null);
120 }
121 }
markchienb6eb2c22018-07-18 14:29:20 +0800122
123 @Before
124 public void setUp() {
125 MockitoAnnotations.initMocks(this);
126
markchienb6eb2c22018-07-18 14:29:20 +0800127 when(mResources.getStringArray(R.array.config_tether_dhcp_range))
128 .thenReturn(new String[0]);
129 when(mResources.getStringArray(R.array.config_tether_usb_regexs))
130 .thenReturn(new String[0]);
131 when(mResources.getStringArray(R.array.config_tether_wifi_regexs))
132 .thenReturn(new String[0]);
133 when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs))
134 .thenReturn(new String[0]);
135 when(mResources.getIntArray(R.array.config_tether_upstream_types))
136 .thenReturn(new int[0]);
137 when(mLog.forSubComponent(anyString())).thenReturn(mLog);
138
markchienf2731272019-01-16 17:44:13 +0800139 mContentResolver = new MockContentResolver();
140 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
141 mMockContext = new MockContext(mContext);
142 mSM = new TestStateMachine();
143 mEnMgr = new WrappedEntitlementManager(mMockContext, mSM, mLog, mSystemProperties);
markchien293422f2019-01-08 23:52:21 +0800144 mEnMgr.updateConfiguration(
145 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800146 }
147
148 @After
markchienf2731272019-01-16 17:44:13 +0800149 public void tearDown() throws Exception {
150 if (mSM != null) {
151 mSM.quit();
152 mSM = null;
153 }
154 }
markchienb6eb2c22018-07-18 14:29:20 +0800155
156 private void setupForRequiredProvisioning() {
157 // Produce some acceptable looking provision app setting if requested.
158 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
159 .thenReturn(PROVISIONING_APP_NAME);
160 // Don't disable tethering provisioning unless requested.
161 when(mSystemProperties.getBoolean(eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY),
162 anyBoolean())).thenReturn(false);
163 // Act like the CarrierConfigManager is present and ready unless told otherwise.
164 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
165 .thenReturn(mCarrierConfigManager);
166 when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig);
167 mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true);
168 }
169
170 @Test
171 public void canRequireProvisioning() {
172 setupForRequiredProvisioning();
markchien293422f2019-01-08 23:52:21 +0800173 mEnMgr.updateConfiguration(
174 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800175 assertTrue(mEnMgr.isTetherProvisioningRequired());
176 }
177
178 @Test
179 public void toleratesCarrierConfigManagerMissing() {
180 setupForRequiredProvisioning();
181 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
182 .thenReturn(null);
markchien293422f2019-01-08 23:52:21 +0800183 mEnMgr.updateConfiguration(
184 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800185 // Couldn't get the CarrierConfigManager, but still had a declared provisioning app.
186 // Therefore provisioning still be required.
187 assertTrue(mEnMgr.isTetherProvisioningRequired());
188 }
189
190 @Test
191 public void toleratesCarrierConfigMissing() {
192 setupForRequiredProvisioning();
193 when(mCarrierConfigManager.getConfig()).thenReturn(null);
markchien293422f2019-01-08 23:52:21 +0800194 mEnMgr.updateConfiguration(
195 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800196 // We still have a provisioning app configured, so still require provisioning.
197 assertTrue(mEnMgr.isTetherProvisioningRequired());
198 }
199
200 @Test
201 public void provisioningNotRequiredWhenAppNotFound() {
202 setupForRequiredProvisioning();
203 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
204 .thenReturn(null);
markchien293422f2019-01-08 23:52:21 +0800205 mEnMgr.updateConfiguration(
206 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800207 assertFalse(mEnMgr.isTetherProvisioningRequired());
208 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
209 .thenReturn(new String[] {"malformedApp"});
markchien293422f2019-01-08 23:52:21 +0800210 mEnMgr.updateConfiguration(
211 new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID));
markchienb6eb2c22018-07-18 14:29:20 +0800212 assertFalse(mEnMgr.isTetherProvisioningRequired());
213 }
214
markchienf2731272019-01-16 17:44:13 +0800215 @Test
216 public void testGetLastEntitlementCacheValue() throws Exception {
217 final CountDownLatch mCallbacklatch = new CountDownLatch(1);
218 // 1. Entitlement check is not required.
219 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
220 mEnMgr.everRunUiEntitlement = false;
221 ResultReceiver receiver = new ResultReceiver(null) {
222 @Override
223 protected void onReceiveResult(int resultCode, Bundle resultData) {
224 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
225 mCallbacklatch.countDown();
226 }
227 };
228 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
229 callbackTimeoutHelper(mCallbacklatch);
230 assertFalse(mEnMgr.everRunUiEntitlement);
231
232 setupForRequiredProvisioning();
markchien293422f2019-01-08 23:52:21 +0800233 mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog,
234 INVALID_SUBSCRIPTION_ID));
markchienf2731272019-01-16 17:44:13 +0800235 // 2. No cache value and don't need to run entitlement check.
236 mEnMgr.everRunUiEntitlement = false;
237 receiver = new ResultReceiver(null) {
238 @Override
239 protected void onReceiveResult(int resultCode, Bundle resultData) {
240 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
241 mCallbacklatch.countDown();
242 }
243 };
244 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, false);
245 callbackTimeoutHelper(mCallbacklatch);
246 assertFalse(mEnMgr.everRunUiEntitlement);
247 // 3. No cache value and ui entitlement check is needed.
248 mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISION_FAILED;
249 mEnMgr.everRunUiEntitlement = false;
250 receiver = new ResultReceiver(null) {
251 @Override
252 protected void onReceiveResult(int resultCode, Bundle resultData) {
253 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
254 mCallbacklatch.countDown();
255 }
256 };
257 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
258 mLooper.dispatchAll();
259 callbackTimeoutHelper(mCallbacklatch);
260 assertTrue(mEnMgr.everRunUiEntitlement);
261 // 4. Cache value is TETHER_ERROR_PROVISION_FAILED and don't need to run entitlement check.
262 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
263 mEnMgr.everRunUiEntitlement = false;
264 receiver = new ResultReceiver(null) {
265 @Override
266 protected void onReceiveResult(int resultCode, Bundle resultData) {
267 assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
268 mCallbacklatch.countDown();
269 }
270 };
271 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, false);
272 callbackTimeoutHelper(mCallbacklatch);
273 assertFalse(mEnMgr.everRunUiEntitlement);
274 // 5. Cache value is TETHER_ERROR_PROVISION_FAILED and ui entitlement check is needed.
275 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
276 mEnMgr.everRunUiEntitlement = false;
277 receiver = new ResultReceiver(null) {
278 @Override
279 protected void onReceiveResult(int resultCode, Bundle resultData) {
280 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
281 mCallbacklatch.countDown();
282 }
283 };
284 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
285 mLooper.dispatchAll();
286 callbackTimeoutHelper(mCallbacklatch);
287 assertTrue(mEnMgr.everRunUiEntitlement);
288 // 6. Cache value is TETHER_ERROR_NO_ERROR.
289 mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
290 mEnMgr.everRunUiEntitlement = false;
291 receiver = new ResultReceiver(null) {
292 @Override
293 protected void onReceiveResult(int resultCode, Bundle resultData) {
294 assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
295 mCallbacklatch.countDown();
296 }
297 };
298 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
299 callbackTimeoutHelper(mCallbacklatch);
300 assertFalse(mEnMgr.everRunUiEntitlement);
301 // 7. Test get value for other downstream type.
302 mEnMgr.everRunUiEntitlement = false;
303 receiver = new ResultReceiver(null) {
304 @Override
305 protected void onReceiveResult(int resultCode, Bundle resultData) {
306 assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
307 mCallbacklatch.countDown();
308 }
309 };
310 mEnMgr.getLatestTetheringEntitlementValue(TETHERING_USB, receiver, false);
311 callbackTimeoutHelper(mCallbacklatch);
312 assertFalse(mEnMgr.everRunUiEntitlement);
313 }
314
315 void callbackTimeoutHelper(final CountDownLatch latch) throws Exception {
316 if (!latch.await(1, TimeUnit.SECONDS)) {
317 fail("Timout, fail to recieve callback");
318 }
319 }
320 public class TestStateMachine extends StateMachine {
321 public final ArrayList<Message> messages = new ArrayList<>();
322 private final State mLoggingState =
323 new EntitlementManagerTest.TestStateMachine.LoggingState();
324
325 class LoggingState extends State {
326 @Override public void enter() {
327 messages.clear();
328 }
329
330 @Override public void exit() {
331 messages.clear();
332 }
333
334 @Override public boolean processMessage(Message msg) {
335 messages.add(msg);
336 return false;
337 }
338 }
339
340 public TestStateMachine() {
341 super("EntitlementManagerTest.TestStateMachine", mLooper.getLooper());
342 addState(mLoggingState);
343 setInitialState(mLoggingState);
344 super.start();
345 }
346 }
markchienb6eb2c22018-07-18 14:29:20 +0800347}