Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.mockito.Matchers.anyBoolean; |
| 21 | import static org.mockito.Matchers.eq; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import android.content.Context; |
| 25 | import android.content.res.Resources; |
| 26 | import android.net.INetworkPolicyManager; |
| 27 | import android.net.INetworkStatsService; |
| 28 | import android.os.INetworkManagementService; |
| 29 | import android.os.PersistableBundle; |
| 30 | import android.os.test.TestLooper; |
| 31 | import android.support.test.filters.SmallTest; |
| 32 | import android.support.test.runner.AndroidJUnit4; |
| 33 | import android.telephony.CarrierConfigManager; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
| 41 | @RunWith(AndroidJUnit4.class) |
| 42 | @SmallTest |
| 43 | public class TetheringTest { |
| 44 | private static final String[] PROVISIONING_APP_NAME = {"some", "app"}; |
| 45 | |
| 46 | @Mock private Context mContext; |
| 47 | @Mock private INetworkManagementService mNMService; |
| 48 | @Mock private INetworkStatsService mStatsService; |
| 49 | @Mock private INetworkPolicyManager mPolicyManager; |
| 50 | @Mock private MockableSystemProperties mSystemProperties; |
| 51 | @Mock private Resources mResources; |
| 52 | @Mock private CarrierConfigManager mCarrierConfigManager; |
| 53 | |
| 54 | // Like so many Android system APIs, these cannot be mocked because it is marked final. |
| 55 | // We have to use the real versions. |
| 56 | private final PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 57 | private final TestLooper mLooper = new TestLooper(); |
| 58 | |
| 59 | private Tethering mTethering; |
| 60 | |
| 61 | @Before public void setUp() throws Exception { |
| 62 | MockitoAnnotations.initMocks(this); |
| 63 | when(mContext.getResources()).thenReturn(mResources); |
| 64 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_dhcp_range)) |
| 65 | .thenReturn(new String[0]); |
| 66 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_usb_regexs)) |
| 67 | .thenReturn(new String[0]); |
| 68 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_regexs)) |
| 69 | .thenReturn(new String[0]); |
| 70 | when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs)) |
| 71 | .thenReturn(new String[0]); |
| 72 | when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types)) |
| 73 | .thenReturn(new int[0]); |
| 74 | mTethering = new Tethering(mContext, mNMService, mStatsService, mPolicyManager, |
| 75 | mLooper.getLooper(), mSystemProperties); |
| 76 | } |
| 77 | |
| 78 | private void setupForRequiredProvisioning() { |
| 79 | // Produce some acceptable looking provision app setting if requested. |
| 80 | when(mResources.getStringArray( |
| 81 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 82 | .thenReturn(PROVISIONING_APP_NAME); |
| 83 | // Don't disable tethering provisioning unless requested. |
| 84 | when(mSystemProperties.getBoolean(eq(Tethering.DISABLE_PROVISIONING_SYSPROP_KEY), |
| 85 | anyBoolean())).thenReturn(false); |
| 86 | // Act like the CarrierConfigManager is present and ready unless told otherwise. |
| 87 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 88 | .thenReturn(mCarrierConfigManager); |
| 89 | when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig); |
| 90 | mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void canRequireProvisioning() { |
| 95 | setupForRequiredProvisioning(); |
| 96 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void toleratesCarrierConfigManagerMissing() { |
| 101 | setupForRequiredProvisioning(); |
| 102 | when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) |
| 103 | .thenReturn(null); |
| 104 | // Couldn't get the CarrierConfigManager, but still had a declared provisioning app. |
| 105 | // We therefore still require provisioning. |
| 106 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void toleratesCarrierConfigMissing() { |
| 111 | setupForRequiredProvisioning(); |
| 112 | when(mCarrierConfigManager.getConfig()).thenReturn(null); |
| 113 | // We still have a provisioning app configured, so still require provisioning. |
| 114 | assertTrue(mTethering.isTetherProvisioningRequired()); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void provisioningNotRequiredWhenAppNotFound() { |
| 119 | setupForRequiredProvisioning(); |
| 120 | when(mResources.getStringArray( |
| 121 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 122 | .thenReturn(null); |
| 123 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 124 | when(mResources.getStringArray( |
| 125 | com.android.internal.R.array.config_mobile_hotspot_provision_app)) |
| 126 | .thenReturn(new String[] {"malformedApp"}); |
| 127 | assertTrue(!mTethering.isTetherProvisioningRequired()); |
| 128 | } |
| 129 | } |