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 | |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.Matchers.anyBoolean; |
| 22 | import static org.mockito.Matchers.anyString; |
| 23 | import static org.mockito.Matchers.eq; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.res.Resources; |
| 29 | import android.net.util.SharedLog; |
| 30 | import android.os.PersistableBundle; |
| 31 | import android.support.test.filters.SmallTest; |
| 32 | import android.support.test.runner.AndroidJUnit4; |
| 33 | import android.telephony.CarrierConfigManager; |
| 34 | |
| 35 | import com.android.internal.R; |
| 36 | import com.android.server.connectivity.MockableSystemProperties; |
| 37 | |
| 38 | import org.junit.After; |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | @SmallTest |
| 47 | public final class EntitlementManagerTest { |
| 48 | |
| 49 | private static final int EVENT_EM_UPDATE = 1; |
| 50 | private static final String[] PROVISIONING_APP_NAME = {"some", "app"}; |
| 51 | |
| 52 | @Mock private CarrierConfigManager mCarrierConfigManager; |
| 53 | @Mock private Context mContext; |
| 54 | @Mock private ContentResolver mContent; |
| 55 | @Mock private MockableSystemProperties mSystemProperties; |
| 56 | @Mock private Resources mResources; |
| 57 | @Mock private SharedLog mLog; |
| 58 | |
| 59 | // Like so many Android system APIs, these cannot be mocked because it is marked final. |
| 60 | // We have to use the real versions. |
| 61 | private final PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 62 | |
| 63 | private EntitlementManager mEnMgr; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | MockitoAnnotations.initMocks(this); |
| 68 | |
| 69 | when(mContext.getResources()).thenReturn(mResources); |
| 70 | when(mContext.getContentResolver()).thenReturn(mContent); |
| 71 | when(mResources.getStringArray(R.array.config_tether_dhcp_range)) |
| 72 | .thenReturn(new String[0]); |
| 73 | when(mResources.getStringArray(R.array.config_tether_usb_regexs)) |
| 74 | .thenReturn(new String[0]); |
| 75 | when(mResources.getStringArray(R.array.config_tether_wifi_regexs)) |
| 76 | .thenReturn(new String[0]); |
| 77 | when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs)) |
| 78 | .thenReturn(new String[0]); |
| 79 | when(mResources.getIntArray(R.array.config_tether_upstream_types)) |
| 80 | .thenReturn(new int[0]); |
| 81 | when(mLog.forSubComponent(anyString())).thenReturn(mLog); |
| 82 | |
| 83 | mEnMgr = new EntitlementManager(mContext, mLog, mSystemProperties); |
| 84 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() throws Exception {} |
| 89 | |
| 90 | private void setupForRequiredProvisioning() { |
| 91 | // Produce some acceptable looking provision app setting if requested. |
| 92 | when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app)) |
| 93 | .thenReturn(PROVISIONING_APP_NAME); |
| 94 | // Don't disable tethering provisioning unless requested. |
| 95 | when(mSystemProperties.getBoolean(eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY), |
| 96 | anyBoolean())).thenReturn(false); |
| 97 | // Act like the CarrierConfigManager is present and ready unless told otherwise. |
| 98 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 99 | .thenReturn(mCarrierConfigManager); |
| 100 | when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig); |
| 101 | mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void canRequireProvisioning() { |
| 106 | setupForRequiredProvisioning(); |
| 107 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 108 | assertTrue(mEnMgr.isTetherProvisioningRequired()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void toleratesCarrierConfigManagerMissing() { |
| 113 | setupForRequiredProvisioning(); |
| 114 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 115 | .thenReturn(null); |
| 116 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 117 | // Couldn't get the CarrierConfigManager, but still had a declared provisioning app. |
| 118 | // Therefore provisioning still be required. |
| 119 | assertTrue(mEnMgr.isTetherProvisioningRequired()); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void toleratesCarrierConfigMissing() { |
| 124 | setupForRequiredProvisioning(); |
| 125 | when(mCarrierConfigManager.getConfig()).thenReturn(null); |
| 126 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 127 | // We still have a provisioning app configured, so still require provisioning. |
| 128 | assertTrue(mEnMgr.isTetherProvisioningRequired()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void provisioningNotRequiredWhenAppNotFound() { |
| 133 | setupForRequiredProvisioning(); |
| 134 | when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app)) |
| 135 | .thenReturn(null); |
| 136 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 137 | assertFalse(mEnMgr.isTetherProvisioningRequired()); |
| 138 | when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app)) |
| 139 | .thenReturn(new String[] {"malformedApp"}); |
| 140 | mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog)); |
| 141 | assertFalse(mEnMgr.isTetherProvisioningRequired()); |
| 142 | } |
| 143 | |
| 144 | } |