markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.connectivity.tethering; |
| 18 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 20 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
| 21 | import static android.net.ConnectivityManager.TETHER_ERROR_ENTITLEMENT_UNKONWN; |
| 22 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 23 | import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 24 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertTrue; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 29 | import static org.junit.Assert.fail; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 30 | import static org.mockito.Matchers.anyBoolean; |
| 31 | import static org.mockito.Matchers.anyString; |
| 32 | import static org.mockito.Matchers.eq; |
| 33 | import static org.mockito.Mockito.when; |
| 34 | |
| 35 | import android.content.ContentResolver; |
| 36 | import android.content.Context; |
| 37 | import android.content.res.Resources; |
| 38 | import android.net.util.SharedLog; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 39 | import android.os.Bundle; |
| 40 | import android.os.Message; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 41 | import android.os.PersistableBundle; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 42 | import android.os.ResultReceiver; |
| 43 | import android.os.test.TestLooper; |
| 44 | import android.provider.Settings; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 45 | import android.telephony.CarrierConfigManager; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 46 | import android.test.mock.MockContentResolver; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 47 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 48 | import androidx.test.filters.SmallTest; |
| 49 | import androidx.test.runner.AndroidJUnit4; |
| 50 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 51 | import com.android.internal.R; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 52 | import com.android.internal.util.State; |
| 53 | import com.android.internal.util.StateMachine; |
| 54 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 55 | import com.android.internal.util.test.FakeSettingsProvider; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 56 | import com.android.server.connectivity.MockableSystemProperties; |
| 57 | |
| 58 | import org.junit.After; |
| 59 | import org.junit.Before; |
| 60 | import org.junit.Test; |
| 61 | import org.junit.runner.RunWith; |
| 62 | import org.mockito.Mock; |
| 63 | import org.mockito.MockitoAnnotations; |
| 64 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 65 | import java.util.ArrayList; |
| 66 | import java.util.concurrent.CountDownLatch; |
| 67 | import java.util.concurrent.TimeUnit; |
| 68 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 69 | @RunWith(AndroidJUnit4.class) |
| 70 | @SmallTest |
| 71 | public 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; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 78 | @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(); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 85 | private final TestLooper mLooper = new TestLooper(); |
| 86 | private Context mMockContext; |
| 87 | private MockContentResolver mContentResolver; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 88 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 89 | 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 | } |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 123 | |
| 124 | @Before |
| 125 | public void setUp() { |
| 126 | MockitoAnnotations.initMocks(this); |
| 127 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 128 | 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 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 140 | 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); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 145 | mEnMgr.updateConfiguration( |
| 146 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @After |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 150 | public void tearDown() throws Exception { |
| 151 | if (mSM != null) { |
| 152 | mSM.quit(); |
| 153 | mSM = null; |
| 154 | } |
| 155 | } |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 156 | |
| 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(); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 174 | mEnMgr.updateConfiguration( |
| 175 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 176 | assertTrue(mEnMgr.isTetherProvisioningRequired()); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void toleratesCarrierConfigManagerMissing() { |
| 181 | setupForRequiredProvisioning(); |
| 182 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 183 | .thenReturn(null); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 184 | mEnMgr.updateConfiguration( |
| 185 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 186 | // 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); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 195 | mEnMgr.updateConfiguration( |
| 196 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 197 | // 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); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 206 | mEnMgr.updateConfiguration( |
| 207 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 208 | assertFalse(mEnMgr.isTetherProvisioningRequired()); |
| 209 | when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app)) |
| 210 | .thenReturn(new String[] {"malformedApp"}); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 211 | mEnMgr.updateConfiguration( |
| 212 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID)); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 213 | assertFalse(mEnMgr.isTetherProvisioningRequired()); |
| 214 | } |
| 215 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 216 | @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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 229 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 230 | callbackTimeoutHelper(mCallbacklatch); |
| 231 | assertFalse(mEnMgr.everRunUiEntitlement); |
| 232 | |
| 233 | setupForRequiredProvisioning(); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 234 | mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog, |
| 235 | INVALID_SUBSCRIPTION_ID)); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 236 | // 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 245 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 246 | 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 258 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 259 | 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 272 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 273 | 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 285 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 286 | 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 299 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 300 | 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 | }; |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 311 | mEnMgr.getLatestTetheringEntitlementResult(TETHERING_USB, receiver, false); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 312 | 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 | } |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 348 | } |