Home | History | Annotate | Download | only in tethering
      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 
     17 package com.android.server.connectivity.tethering;
     18 
     19 import static android.net.ConnectivityManager.TYPE_ETHERNET;
     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;
     24 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_NOT_REQUIRED;
     25 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_REQUIRED;
     26 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_UNSPECIFIED;
     27 import static org.junit.Assert.assertEquals;
     28 import static org.junit.Assert.assertFalse;
     29 import static org.junit.Assert.assertTrue;
     30 import static org.mockito.Mockito.when;
     31 
     32 import android.content.Context;
     33 import android.content.ContextWrapper;
     34 import android.content.res.Resources;
     35 import android.net.util.SharedLog;
     36 import android.support.test.filters.SmallTest;
     37 import android.support.test.runner.AndroidJUnit4;
     38 import android.telephony.TelephonyManager;
     39 
     40 import com.android.internal.util.test.BroadcastInterceptingContext;
     41 
     42 import java.util.Iterator;
     43 
     44 import org.junit.Before;
     45 import org.junit.Test;
     46 import org.junit.runner.RunWith;
     47 import org.mockito.Mock;
     48 import org.mockito.MockitoAnnotations;
     49 
     50 
     51 @RunWith(AndroidJUnit4.class)
     52 @SmallTest
     53 public class TetheringConfigurationTest {
     54     private final SharedLog mLog = new SharedLog("TetheringConfigurationTest");
     55     @Mock private Context mContext;
     56     @Mock private TelephonyManager mTelephonyManager;
     57     @Mock private Resources mResources;
     58     private Context mMockContext;
     59     private boolean mHasTelephonyManager;
     60 
     61     private class MockContext extends BroadcastInterceptingContext {
     62         MockContext(Context base) {
     63             super(base);
     64         }
     65 
     66         @Override
     67         public Resources getResources() { return mResources; }
     68 
     69         @Override
     70         public Object getSystemService(String name) {
     71             if (Context.TELEPHONY_SERVICE.equals(name)) {
     72                 return mHasTelephonyManager ? mTelephonyManager : null;
     73             }
     74             return super.getSystemService(name);
     75         }
     76     }
     77 
     78     @Before
     79     public void setUp() throws Exception {
     80         MockitoAnnotations.initMocks(this);
     81         when(mResources.getStringArray(com.android.internal.R.array.config_tether_dhcp_range))
     82                 .thenReturn(new String[0]);
     83         when(mResources.getStringArray(com.android.internal.R.array.config_tether_usb_regexs))
     84                 .thenReturn(new String[0]);
     85         when(mResources.getStringArray(com.android.internal.R.array.config_tether_wifi_regexs))
     86                 .thenReturn(new String[]{ "test_wlan\\d" });
     87         when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs))
     88                 .thenReturn(new String[0]);
     89         mMockContext = new MockContext(mContext);
     90     }
     91 
     92     @Test
     93     public void testDunFromTelephonyManagerMeansDun() {
     94         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
     95                 .thenReturn(new int[]{TYPE_MOBILE, TYPE_WIFI, TYPE_MOBILE_HIPRI});
     96         mHasTelephonyManager = true;
     97         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_REQUIRED);
     98 
     99         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    100         assertTrue(cfg.isDunRequired);
    101         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
    102         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
    103         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI));
    104         // Just to prove we haven't clobbered Wi-Fi:
    105         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI));
    106     }
    107 
    108     @Test
    109     public void testDunNotRequiredFromTelephonyManagerMeansNoDun() {
    110         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
    111                 .thenReturn(new int[]{TYPE_MOBILE_DUN, TYPE_WIFI});
    112         mHasTelephonyManager = true;
    113         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_NOT_REQUIRED);
    114 
    115         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    116         assertFalse(cfg.isDunRequired);
    117         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
    118         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
    119         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI));
    120         // Just to prove we haven't clobbered Wi-Fi:
    121         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI));
    122     }
    123 
    124     @Test
    125     public void testDunFromUpstreamConfigMeansDun() {
    126         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
    127                 .thenReturn(new int[]{TYPE_MOBILE_DUN, TYPE_WIFI});
    128         mHasTelephonyManager = false;
    129         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
    130 
    131         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    132         assertTrue(cfg.isDunRequired);
    133         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
    134         // Just to prove we haven't clobbered Wi-Fi:
    135         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_WIFI));
    136         // Check that we have not added new cellular interface types
    137         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
    138         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_HIPRI));
    139     }
    140 
    141     @Test
    142     public void testNoDefinedUpstreamTypesAddsEthernet() {
    143         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
    144                 .thenReturn(new int[]{});
    145         mHasTelephonyManager = false;
    146         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
    147 
    148         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    149         final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator();
    150         assertTrue(upstreamIterator.hasNext());
    151         assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue());
    152         // The following is because the code always adds some kind of mobile
    153         // upstream, be it DUN or, in this case where we use DUN_UNSPECIFIED,
    154         // both vanilla and hipri mobile types.
    155         assertTrue(upstreamIterator.hasNext());
    156         assertEquals(TYPE_MOBILE, upstreamIterator.next().intValue());
    157         assertTrue(upstreamIterator.hasNext());
    158         assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue());
    159         assertFalse(upstreamIterator.hasNext());
    160     }
    161 
    162     @Test
    163     public void testDefinedUpstreamTypesSansEthernetAddsEthernet() {
    164         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
    165                 .thenReturn(new int[]{TYPE_WIFI, TYPE_MOBILE_HIPRI});
    166         mHasTelephonyManager = false;
    167         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
    168 
    169         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    170         final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator();
    171         assertTrue(upstreamIterator.hasNext());
    172         assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue());
    173         assertTrue(upstreamIterator.hasNext());
    174         assertEquals(TYPE_WIFI, upstreamIterator.next().intValue());
    175         assertTrue(upstreamIterator.hasNext());
    176         assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue());
    177         assertFalse(upstreamIterator.hasNext());
    178     }
    179 
    180     @Test
    181     public void testDefinedUpstreamTypesWithEthernetDoesNotAddEthernet() {
    182         when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
    183                 .thenReturn(new int[]{TYPE_WIFI, TYPE_ETHERNET, TYPE_MOBILE_HIPRI});
    184         mHasTelephonyManager = false;
    185         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
    186 
    187         final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
    188         final Iterator<Integer> upstreamIterator = cfg.preferredUpstreamIfaceTypes.iterator();
    189         assertTrue(upstreamIterator.hasNext());
    190         assertEquals(TYPE_WIFI, upstreamIterator.next().intValue());
    191         assertTrue(upstreamIterator.hasNext());
    192         assertEquals(TYPE_ETHERNET, upstreamIterator.next().intValue());
    193         assertTrue(upstreamIterator.hasNext());
    194         assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue());
    195         assertFalse(upstreamIterator.hasNext());
    196     }
    197 }
    198