Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
markchien | 503be61 | 2020-04-12 21:41:29 +0800 | [diff] [blame] | 17 | package com.android.networkstack.tethering; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 18 | |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 21 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 22 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 23 | import static android.net.ConnectivityManager.TYPE_WIFI; |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 24 | import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 25 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 26 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 28 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 29 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertFalse; |
| 32 | import static org.junit.Assert.assertTrue; |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 33 | import static org.mockito.Matchers.anyBoolean; |
| 34 | import static org.mockito.Matchers.eq; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 35 | import static org.mockito.Mockito.when; |
| 36 | |
| 37 | import android.content.Context; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 38 | import android.content.res.Resources; |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 39 | import android.net.util.SharedLog; |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 40 | import android.provider.DeviceConfig; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 41 | import android.telephony.TelephonyManager; |
| 42 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 43 | import androidx.test.filters.SmallTest; |
| 44 | import androidx.test.runner.AndroidJUnit4; |
| 45 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 46 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 47 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 48 | import org.junit.After; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.mockito.Mock; |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 53 | import org.mockito.MockitoSession; |
| 54 | import org.mockito.quality.Strictness; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 55 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 56 | import java.util.Arrays; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 57 | import java.util.Iterator; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 58 | |
| 59 | @RunWith(AndroidJUnit4.class) |
| 60 | @SmallTest |
| 61 | public class TetheringConfigurationTest { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 62 | private final SharedLog mLog = new SharedLog("TetheringConfigurationTest"); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 63 | |
| 64 | private static final String[] PROVISIONING_APP_NAME = {"some", "app"}; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 65 | @Mock private Context mContext; |
| 66 | @Mock private TelephonyManager mTelephonyManager; |
| 67 | @Mock private Resources mResources; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 68 | @Mock private Resources mResourcesForSubId; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 69 | private Context mMockContext; |
| 70 | private boolean mHasTelephonyManager; |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 71 | private boolean mEnableLegacyDhcpServer; |
| 72 | private MockitoSession mMockingSession; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 73 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 74 | private class MockTetheringConfiguration extends TetheringConfiguration { |
| 75 | MockTetheringConfiguration(Context ctx, SharedLog log, int id) { |
| 76 | super(ctx, log, id); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) { |
| 81 | return mResourcesForSubId; |
| 82 | } |
| 83 | } |
| 84 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 85 | private class MockContext extends BroadcastInterceptingContext { |
| 86 | MockContext(Context base) { |
| 87 | super(base); |
| 88 | } |
| 89 | |
| 90 | @Override |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 91 | public Resources getResources() { |
| 92 | return mResources; |
| 93 | } |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public Object getSystemService(String name) { |
| 97 | if (Context.TELEPHONY_SERVICE.equals(name)) { |
| 98 | return mHasTelephonyManager ? mTelephonyManager : null; |
| 99 | } |
| 100 | return super.getSystemService(name); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Before |
| 105 | public void setUp() throws Exception { |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 106 | // TODO: use a dependencies class instead of mock statics. |
| 107 | mMockingSession = mockitoSession() |
| 108 | .initMocks(this) |
| 109 | .mockStatic(DeviceConfig.class) |
| 110 | .strictness(Strictness.WARN) |
| 111 | .startMocking(); |
| 112 | doReturn(false).when( |
| 113 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 114 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
| 115 | |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 116 | when(mResources.getStringArray(R.array.config_tether_dhcp_range)).thenReturn( |
| 117 | new String[0]); |
junyulai | aea13ae | 2020-04-30 15:21:55 +0800 | [diff] [blame] | 118 | when(mResources.getInteger(R.integer.config_tether_offload_poll_interval)).thenReturn( |
| 119 | TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS); |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 120 | when(mResources.getStringArray(R.array.config_tether_usb_regexs)).thenReturn(new String[0]); |
| 121 | when(mResources.getStringArray(R.array.config_tether_wifi_regexs)) |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 122 | .thenReturn(new String[]{ "test_wlan\\d" }); |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 123 | when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs)).thenReturn( |
| 124 | new String[0]); |
| 125 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn(new int[0]); |
| 126 | when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app)) |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 127 | .thenReturn(new String[0]); |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 128 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 129 | false); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 130 | mHasTelephonyManager = true; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 131 | mMockContext = new MockContext(mContext); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 132 | mEnableLegacyDhcpServer = false; |
| 133 | } |
| 134 | |
| 135 | @After |
| 136 | public void tearDown() throws Exception { |
| 137 | mMockingSession.finishMocking(); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 138 | } |
| 139 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 140 | private TetheringConfiguration getTetheringConfiguration(int... legacyTetherUpstreamTypes) { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 141 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 142 | legacyTetherUpstreamTypes); |
| 143 | return new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testNoTelephonyManagerMeansNoDun() { |
| 148 | mHasTelephonyManager = false; |
| 149 | final TetheringConfiguration cfg = getTetheringConfiguration( |
| 150 | new int[]{TYPE_MOBILE_DUN, TYPE_WIFI}); |
| 151 | assertFalse(cfg.isDunRequired); |
| 152 | assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 153 | // Just to prove we haven't clobbered Wi-Fi: |
| 154 | assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 155 | } |
| 156 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 157 | @Test |
| 158 | public void testDunFromTelephonyManagerMeansDun() { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 159 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(true); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 160 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 161 | final TetheringConfiguration cfgWifi = getTetheringConfiguration(TYPE_WIFI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 162 | final TetheringConfiguration cfgMobileWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 163 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 164 | final TetheringConfiguration cfgWifiDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 165 | TYPE_WIFI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 166 | final TetheringConfiguration cfgMobileWifiHipriDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 167 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 168 | |
| 169 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, |
| 170 | cfgWifiDun, cfgMobileWifiHipriDun)) { |
| 171 | String msg = "config=" + cfg.toString(); |
| 172 | assertTrue(msg, cfg.isDunRequired); |
| 173 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 174 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 175 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 176 | // Just to prove we haven't clobbered Wi-Fi: |
| 177 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 178 | } |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testDunNotRequiredFromTelephonyManagerMeansNoDun() { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 183 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 184 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 185 | final TetheringConfiguration cfgWifi = getTetheringConfiguration(TYPE_WIFI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 186 | final TetheringConfiguration cfgMobileWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 187 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 188 | final TetheringConfiguration cfgWifiDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 189 | TYPE_WIFI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 190 | final TetheringConfiguration cfgWifiMobile = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 191 | TYPE_WIFI, TYPE_MOBILE); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 192 | final TetheringConfiguration cfgWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 193 | TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 194 | final TetheringConfiguration cfgMobileWifiHipriDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 195 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI, TYPE_MOBILE_DUN); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 196 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 197 | String msg; |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 198 | // TYPE_MOBILE_DUN should be present in none of the combinations. |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 199 | // TYPE_WIFI should not be affected. |
| 200 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, cfgWifiDun, |
| 201 | cfgWifiMobile, cfgWifiHipri, cfgMobileWifiHipriDun)) { |
| 202 | msg = "config=" + cfg.toString(); |
| 203 | assertFalse(msg, cfg.isDunRequired); |
| 204 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 205 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 206 | } |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 207 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 208 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, cfgWifiDun, |
| 209 | cfgMobileWifiHipriDun)) { |
| 210 | msg = "config=" + cfg.toString(); |
| 211 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 212 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 213 | } |
| 214 | msg = "config=" + cfgWifiMobile.toString(); |
| 215 | assertTrue(msg, cfgWifiMobile.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 216 | assertFalse(msg, cfgWifiMobile.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 217 | msg = "config=" + cfgWifiHipri.toString(); |
| 218 | assertFalse(msg, cfgWifiHipri.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 219 | assertTrue(msg, cfgWifiHipri.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 220 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 221 | } |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 222 | |
| 223 | @Test |
| 224 | public void testNoDefinedUpstreamTypesAddsEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 225 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn(new int[]{}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 226 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 227 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 228 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 229 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 230 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 231 | assertTrue(upstreamIterator.hasNext()); |
| 232 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 233 | // The following is because the code always adds some kind of mobile |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 234 | // upstream, be it DUN or, in this case where DUN is NOT required, |
| 235 | // make sure there is at least one of MOBILE or HIPRI. With the empty |
| 236 | // list of the configuration in this test, it will always add both |
| 237 | // MOBILE and HIPRI, in that order. |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 238 | assertTrue(upstreamIterator.hasNext()); |
| 239 | assertEquals(TYPE_MOBILE, upstreamIterator.next().intValue()); |
| 240 | assertTrue(upstreamIterator.hasNext()); |
| 241 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 242 | assertFalse(upstreamIterator.hasNext()); |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void testDefinedUpstreamTypesSansEthernetAddsEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 247 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 248 | new int[]{TYPE_WIFI, TYPE_MOBILE_HIPRI}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 249 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 250 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 251 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 252 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 253 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 254 | assertTrue(upstreamIterator.hasNext()); |
| 255 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 256 | assertTrue(upstreamIterator.hasNext()); |
| 257 | assertEquals(TYPE_WIFI, upstreamIterator.next().intValue()); |
| 258 | assertTrue(upstreamIterator.hasNext()); |
| 259 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 260 | assertFalse(upstreamIterator.hasNext()); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void testDefinedUpstreamTypesWithEthernetDoesNotAddEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 265 | when(mResources.getIntArray(R.array.config_tether_upstream_types)) |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 266 | .thenReturn(new int[]{TYPE_WIFI, TYPE_ETHERNET, TYPE_MOBILE_HIPRI}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 267 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 268 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 269 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 270 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 271 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 272 | assertTrue(upstreamIterator.hasNext()); |
| 273 | assertEquals(TYPE_WIFI, upstreamIterator.next().intValue()); |
| 274 | assertTrue(upstreamIterator.hasNext()); |
| 275 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 276 | assertTrue(upstreamIterator.hasNext()); |
| 277 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 278 | assertFalse(upstreamIterator.hasNext()); |
| 279 | } |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 280 | |
| 281 | @Test |
| 282 | public void testNewDhcpServerDisabled() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 283 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 284 | true); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 285 | doReturn(false).when( |
| 286 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 287 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 288 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 289 | final TetheringConfiguration enableByRes = |
| 290 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 291 | assertTrue(enableByRes.enableLegacyDhcpServer); |
| 292 | |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 293 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 294 | false); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 295 | doReturn(true).when( |
| 296 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 297 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
| 298 | |
| 299 | final TetheringConfiguration enableByDevConfig = |
| 300 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 301 | assertTrue(enableByDevConfig.enableLegacyDhcpServer); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void testNewDhcpServerEnabled() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 306 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 307 | false); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 308 | doReturn(false).when( |
| 309 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 310 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 311 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 312 | final TetheringConfiguration cfg = |
| 313 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 314 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 315 | assertFalse(cfg.enableLegacyDhcpServer); |
| 316 | } |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 317 | |
| 318 | @Test |
junyulai | aea13ae | 2020-04-30 15:21:55 +0800 | [diff] [blame] | 319 | public void testOffloadIntervalByResource() { |
| 320 | final TetheringConfiguration intervalByDefault = |
| 321 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 322 | assertEquals(TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS, |
| 323 | intervalByDefault.getOffloadPollInterval()); |
| 324 | |
| 325 | final int[] testOverrides = {0, 3000, -1}; |
| 326 | for (final int override : testOverrides) { |
| 327 | when(mResources.getInteger(R.integer.config_tether_offload_poll_interval)).thenReturn( |
| 328 | override); |
| 329 | final TetheringConfiguration overrideByRes = |
| 330 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 331 | assertEquals(override, overrideByRes.getOffloadPollInterval()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | @Test |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 336 | public void testGetResourcesBySubId() { |
| 337 | setUpResourceForSubId(); |
| 338 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 339 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 340 | assertTrue(cfg.provisioningApp.length == 0); |
| 341 | final int anyValidSubId = 1; |
| 342 | final MockTetheringConfiguration mockCfg = |
| 343 | new MockTetheringConfiguration(mMockContext, mLog, anyValidSubId); |
| 344 | assertEquals(mockCfg.provisioningApp[0], PROVISIONING_APP_NAME[0]); |
| 345 | assertEquals(mockCfg.provisioningApp[1], PROVISIONING_APP_NAME[1]); |
| 346 | } |
| 347 | |
| 348 | private void setUpResourceForSubId() { |
| 349 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 350 | R.array.config_tether_dhcp_range)).thenReturn(new String[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 351 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 352 | R.array.config_tether_usb_regexs)).thenReturn(new String[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 353 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 354 | R.array.config_tether_wifi_regexs)).thenReturn(new String[]{ "test_wlan\\d" }); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 355 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 356 | R.array.config_tether_bluetooth_regexs)).thenReturn(new String[0]); |
| 357 | when(mResourcesForSubId.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
| 358 | new int[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 359 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 360 | R.array.config_mobile_hotspot_provision_app)).thenReturn(PROVISIONING_APP_NAME); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 363 | } |