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); |
Nucca Chen | 95e0bac | 2020-05-12 11:34:28 +0000 | [diff] [blame] | 130 | initializeBpfOffloadConfiguration(true, null /* unset */); |
| 131 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 132 | mHasTelephonyManager = true; |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 133 | mMockContext = new MockContext(mContext); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 134 | mEnableLegacyDhcpServer = false; |
| 135 | } |
| 136 | |
| 137 | @After |
| 138 | public void tearDown() throws Exception { |
| 139 | mMockingSession.finishMocking(); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 140 | } |
| 141 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 142 | private TetheringConfiguration getTetheringConfiguration(int... legacyTetherUpstreamTypes) { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 143 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 144 | legacyTetherUpstreamTypes); |
| 145 | return new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void testNoTelephonyManagerMeansNoDun() { |
| 150 | mHasTelephonyManager = false; |
| 151 | final TetheringConfiguration cfg = getTetheringConfiguration( |
| 152 | new int[]{TYPE_MOBILE_DUN, TYPE_WIFI}); |
| 153 | assertFalse(cfg.isDunRequired); |
| 154 | assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 155 | // Just to prove we haven't clobbered Wi-Fi: |
| 156 | assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 157 | } |
| 158 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 159 | @Test |
| 160 | public void testDunFromTelephonyManagerMeansDun() { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 161 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(true); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 162 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 163 | final TetheringConfiguration cfgWifi = getTetheringConfiguration(TYPE_WIFI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 164 | final TetheringConfiguration cfgMobileWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 165 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 166 | final TetheringConfiguration cfgWifiDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 167 | TYPE_WIFI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 168 | final TetheringConfiguration cfgMobileWifiHipriDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 169 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 170 | |
| 171 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, |
| 172 | cfgWifiDun, cfgMobileWifiHipriDun)) { |
| 173 | String msg = "config=" + cfg.toString(); |
| 174 | assertTrue(msg, cfg.isDunRequired); |
| 175 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 176 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 177 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 178 | // Just to prove we haven't clobbered Wi-Fi: |
| 179 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 180 | } |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testDunNotRequiredFromTelephonyManagerMeansNoDun() { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 185 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 186 | |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 187 | final TetheringConfiguration cfgWifi = getTetheringConfiguration(TYPE_WIFI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 188 | final TetheringConfiguration cfgMobileWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 189 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 190 | final TetheringConfiguration cfgWifiDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 191 | TYPE_WIFI, TYPE_MOBILE_DUN); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 192 | final TetheringConfiguration cfgWifiMobile = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 193 | TYPE_WIFI, TYPE_MOBILE); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 194 | final TetheringConfiguration cfgWifiHipri = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 195 | TYPE_WIFI, TYPE_MOBILE_HIPRI); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 196 | final TetheringConfiguration cfgMobileWifiHipriDun = getTetheringConfiguration( |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 197 | TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI, TYPE_MOBILE_DUN); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 198 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 199 | String msg; |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 200 | // TYPE_MOBILE_DUN should be present in none of the combinations. |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 201 | // TYPE_WIFI should not be affected. |
| 202 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, cfgWifiDun, |
| 203 | cfgWifiMobile, cfgWifiHipri, cfgMobileWifiHipriDun)) { |
| 204 | msg = "config=" + cfg.toString(); |
| 205 | assertFalse(msg, cfg.isDunRequired); |
| 206 | assertFalse(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); |
| 207 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI)); |
| 208 | } |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 209 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 210 | for (TetheringConfiguration cfg : Arrays.asList(cfgWifi, cfgMobileWifiHipri, cfgWifiDun, |
| 211 | cfgMobileWifiHipriDun)) { |
| 212 | msg = "config=" + cfg.toString(); |
| 213 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 214 | assertTrue(msg, cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 215 | } |
| 216 | msg = "config=" + cfgWifiMobile.toString(); |
| 217 | assertTrue(msg, cfgWifiMobile.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 218 | assertFalse(msg, cfgWifiMobile.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 219 | msg = "config=" + cfgWifiHipri.toString(); |
| 220 | assertFalse(msg, cfgWifiHipri.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); |
| 221 | assertTrue(msg, cfgWifiHipri.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI)); |
| 222 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 223 | } |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 224 | |
| 225 | @Test |
| 226 | public void testNoDefinedUpstreamTypesAddsEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 227 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn(new int[]{}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 228 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 229 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 230 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 231 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 232 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 233 | assertTrue(upstreamIterator.hasNext()); |
| 234 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 235 | // The following is because the code always adds some kind of mobile |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 236 | // upstream, be it DUN or, in this case where DUN is NOT required, |
| 237 | // make sure there is at least one of MOBILE or HIPRI. With the empty |
| 238 | // list of the configuration in this test, it will always add both |
| 239 | // MOBILE and HIPRI, in that order. |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 240 | assertTrue(upstreamIterator.hasNext()); |
| 241 | assertEquals(TYPE_MOBILE, upstreamIterator.next().intValue()); |
| 242 | assertTrue(upstreamIterator.hasNext()); |
| 243 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 244 | assertFalse(upstreamIterator.hasNext()); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public void testDefinedUpstreamTypesSansEthernetAddsEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 249 | when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 250 | new int[]{TYPE_WIFI, TYPE_MOBILE_HIPRI}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 251 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 252 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 253 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 254 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 255 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 256 | assertTrue(upstreamIterator.hasNext()); |
| 257 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 258 | assertTrue(upstreamIterator.hasNext()); |
| 259 | assertEquals(TYPE_WIFI, upstreamIterator.next().intValue()); |
| 260 | assertTrue(upstreamIterator.hasNext()); |
| 261 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 262 | assertFalse(upstreamIterator.hasNext()); |
| 263 | } |
| 264 | |
| 265 | @Test |
| 266 | public void testDefinedUpstreamTypesWithEthernetDoesNotAddEthernet() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 267 | when(mResources.getIntArray(R.array.config_tether_upstream_types)) |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 268 | .thenReturn(new int[]{TYPE_WIFI, TYPE_ETHERNET, TYPE_MOBILE_HIPRI}); |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 269 | when(mTelephonyManager.isTetheringApnRequired()).thenReturn(false); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 270 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 271 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 272 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
Erik Kline | 0e61baa | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 273 | final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator(); |
| 274 | assertTrue(upstreamIterator.hasNext()); |
| 275 | assertEquals(TYPE_WIFI, upstreamIterator.next().intValue()); |
| 276 | assertTrue(upstreamIterator.hasNext()); |
| 277 | assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue()); |
| 278 | assertTrue(upstreamIterator.hasNext()); |
| 279 | assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue()); |
| 280 | assertFalse(upstreamIterator.hasNext()); |
| 281 | } |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 282 | |
Nucca Chen | 95e0bac | 2020-05-12 11:34:28 +0000 | [diff] [blame] | 283 | private void initializeBpfOffloadConfiguration( |
| 284 | final boolean fromRes, final String fromDevConfig) { |
| 285 | when(mResources.getBoolean(R.bool.config_tether_enable_bpf_offload)).thenReturn(fromRes); |
| 286 | doReturn(fromDevConfig).when( |
| 287 | () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY), |
| 288 | eq(TetheringConfiguration.OVERRIDE_TETHER_ENABLE_BPF_OFFLOAD))); |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testBpfOffloadEnabledByResource() { |
| 293 | initializeBpfOffloadConfiguration(true, null /* unset */); |
| 294 | final TetheringConfiguration enableByRes = |
| 295 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 296 | assertTrue(enableByRes.enableBpfOffload); |
| 297 | } |
| 298 | |
| 299 | @Test |
| 300 | public void testBpfOffloadEnabledByDeviceConfigOverride() { |
| 301 | for (boolean res : new boolean[]{true, false}) { |
| 302 | initializeBpfOffloadConfiguration(res, "true"); |
| 303 | final TetheringConfiguration enableByDevConOverride = |
| 304 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 305 | assertTrue(enableByDevConOverride.enableBpfOffload); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | @Test |
| 310 | public void testBpfOffloadDisabledByResource() { |
| 311 | initializeBpfOffloadConfiguration(false, null /* unset */); |
| 312 | final TetheringConfiguration disableByRes = |
| 313 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 314 | assertFalse(disableByRes.enableBpfOffload); |
| 315 | } |
| 316 | |
| 317 | @Test |
| 318 | public void testBpfOffloadDisabledByDeviceConfigOverride() { |
| 319 | for (boolean res : new boolean[]{true, false}) { |
| 320 | initializeBpfOffloadConfiguration(res, "false"); |
| 321 | final TetheringConfiguration disableByDevConOverride = |
| 322 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 323 | assertFalse(disableByDevConOverride.enableBpfOffload); |
| 324 | } |
| 325 | } |
| 326 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 327 | @Test |
| 328 | public void testNewDhcpServerDisabled() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 329 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 330 | true); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 331 | doReturn(false).when( |
| 332 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 333 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 334 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 335 | final TetheringConfiguration enableByRes = |
| 336 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 337 | assertTrue(enableByRes.enableLegacyDhcpServer); |
| 338 | |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 339 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 340 | false); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 341 | doReturn(true).when( |
| 342 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 343 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
| 344 | |
| 345 | final TetheringConfiguration enableByDevConfig = |
| 346 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 347 | assertTrue(enableByDevConfig.enableLegacyDhcpServer); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Test |
| 351 | public void testNewDhcpServerEnabled() { |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 352 | when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( |
| 353 | false); |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 354 | doReturn(false).when( |
| 355 | () -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), |
| 356 | eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 357 | |
markchien | 2dfee02 | 2020-01-13 16:09:42 +0800 | [diff] [blame] | 358 | final TetheringConfiguration cfg = |
| 359 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 360 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 361 | assertFalse(cfg.enableLegacyDhcpServer); |
| 362 | } |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 363 | |
| 364 | @Test |
junyulai | aea13ae | 2020-04-30 15:21:55 +0800 | [diff] [blame] | 365 | public void testOffloadIntervalByResource() { |
| 366 | final TetheringConfiguration intervalByDefault = |
| 367 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 368 | assertEquals(TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS, |
| 369 | intervalByDefault.getOffloadPollInterval()); |
| 370 | |
| 371 | final int[] testOverrides = {0, 3000, -1}; |
| 372 | for (final int override : testOverrides) { |
| 373 | when(mResources.getInteger(R.integer.config_tether_offload_poll_interval)).thenReturn( |
| 374 | override); |
| 375 | final TetheringConfiguration overrideByRes = |
| 376 | new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 377 | assertEquals(override, overrideByRes.getOffloadPollInterval()); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | @Test |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 382 | public void testGetResourcesBySubId() { |
| 383 | setUpResourceForSubId(); |
| 384 | final TetheringConfiguration cfg = new TetheringConfiguration( |
| 385 | mMockContext, mLog, INVALID_SUBSCRIPTION_ID); |
| 386 | assertTrue(cfg.provisioningApp.length == 0); |
| 387 | final int anyValidSubId = 1; |
| 388 | final MockTetheringConfiguration mockCfg = |
| 389 | new MockTetheringConfiguration(mMockContext, mLog, anyValidSubId); |
| 390 | assertEquals(mockCfg.provisioningApp[0], PROVISIONING_APP_NAME[0]); |
| 391 | assertEquals(mockCfg.provisioningApp[1], PROVISIONING_APP_NAME[1]); |
| 392 | } |
| 393 | |
| 394 | private void setUpResourceForSubId() { |
| 395 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 396 | R.array.config_tether_dhcp_range)).thenReturn(new String[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 397 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 398 | R.array.config_tether_usb_regexs)).thenReturn(new String[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 399 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 400 | R.array.config_tether_wifi_regexs)).thenReturn(new String[]{ "test_wlan\\d" }); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 401 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 402 | R.array.config_tether_bluetooth_regexs)).thenReturn(new String[0]); |
| 403 | when(mResourcesForSubId.getIntArray(R.array.config_tether_upstream_types)).thenReturn( |
| 404 | new int[0]); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 405 | when(mResourcesForSubId.getStringArray( |
markchien | da4519a | 2020-01-14 12:46:53 +0800 | [diff] [blame] | 406 | R.array.config_mobile_hotspot_provision_app)).thenReturn(PROVISIONING_APP_NAME); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 407 | } |
| 408 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 409 | } |