Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 android.permission.cts;
     18 
     19 import android.app.PendingIntent;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.PackageManager;
     23 import android.location.Location;
     24 import android.location.LocationListener;
     25 import android.location.LocationManager;
     26 import android.os.Bundle;
     27 import android.os.Looper;
     28 import android.telephony.PhoneStateListener;
     29 import android.telephony.TelephonyManager;
     30 import android.test.InstrumentationTestCase;
     31 import android.test.suitebuilder.annotation.SmallTest;
     32 import android.test.UiThreadTest;
     33 
     34 import java.util.List;
     35 
     36 /**
     37  * Verify the location access without specific permissions.
     38  */
     39 public class NoLocationPermissionTest extends InstrumentationTestCase {
     40     private static final String TEST_PROVIDER_NAME = "testProvider";
     41 
     42     private LocationManager mLocationManager;
     43     private List<String> mAllProviders;
     44     private boolean mHasTelephony;
     45     private Context mContext;
     46 
     47     @Override
     48     protected void setUp() throws Exception {
     49         super.setUp();
     50         mContext = getInstrumentation().getTargetContext();
     51         mLocationManager = (LocationManager) mContext.getSystemService(
     52                 Context.LOCATION_SERVICE);
     53         mAllProviders = mLocationManager.getAllProviders();
     54         mHasTelephony = mContext.getPackageManager().hasSystemFeature(
     55                 PackageManager.FEATURE_TELEPHONY);
     56 
     57         assertNotNull(mLocationManager);
     58         assertNotNull(mAllProviders);
     59     }
     60 
     61     private boolean isKnownLocationProvider(String provider) {
     62         return mAllProviders.contains(provider);
     63     }
     64 
     65     /**
     66      * Verify that listen or get cell location requires permissions.
     67      * <p>
     68      * Requires Permission: {@link
     69      * android.Manifest.permission#ACCESS_COARSE_LOCATION.}
     70      */
     71     @UiThreadTest
     72     public void testListenCellLocation() {
     73         if (!mHasTelephony) {
     74             return;
     75         }
     76 
     77         TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
     78                 Context.TELEPHONY_SERVICE);
     79         PhoneStateListener phoneStateListener = new PhoneStateListener();
     80         try {
     81             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
     82             fail("TelephonyManager.listen(LISTEN_CELL_LOCATION) did not" +
     83                     " throw SecurityException as expected");
     84         } catch (SecurityException e) {
     85             // expected
     86         }
     87 
     88         try {
     89             telephonyManager.getCellLocation();
     90             fail("TelephonyManager.getCellLocation did not throw SecurityException as expected");
     91         } catch (SecurityException e) {
     92             // expected
     93         }
     94     }
     95 
     96     /**
     97      * Verify that get cell location requires permissions.
     98      * <p>
     99      * Requires Permission: {@link
    100      * android.Manifest.permission#ACCESS_COARSE_LOCATION.}
    101      */
    102     @UiThreadTest
    103     public void testListenCellLocation2() {
    104         if (!mHasTelephony) {
    105             return;
    106         }
    107 
    108         TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
    109                 Context.TELEPHONY_SERVICE);
    110         PhoneStateListener phoneStateListener = new PhoneStateListener();
    111 
    112         try {
    113             telephonyManager.getNeighboringCellInfo();
    114             fail("TelephonyManager.getNeighbouringCellInfo did not throw SecurityException as expected");
    115         } catch (SecurityException e) {
    116             // expected
    117         }
    118 
    119         try {
    120             telephonyManager.getAllCellInfo();
    121             fail("TelephonyManager.getAllCellInfo did not throw SecurityException as expected");
    122         } catch (SecurityException e) {
    123             // expected
    124         }
    125     }
    126 
    127     /**
    128      * Helper method to verify that calling requestLocationUpdates with given
    129      * provider throws SecurityException.
    130      *
    131      * @param provider the String provider name.
    132      */
    133     private void checkRequestLocationUpdates(String provider) {
    134         if (!isKnownLocationProvider(provider)) {
    135             // skip this test if the provider is unknown
    136             return;
    137         }
    138 
    139         LocationListener mockListener = new MockLocationListener();
    140         Looper looper = Looper.myLooper();
    141         try {
    142             mLocationManager.requestLocationUpdates(provider, 0, 0, mockListener);
    143             fail("LocationManager.requestLocationUpdates did not" +
    144                     " throw SecurityException as expected");
    145         } catch (SecurityException e) {
    146             // expected
    147         }
    148 
    149         try {
    150             mLocationManager.requestLocationUpdates(provider, 0, 0, mockListener, looper);
    151             fail("LocationManager.requestLocationUpdates did not" +
    152                     " throw SecurityException as expected");
    153         } catch (SecurityException e) {
    154             // expected
    155         }
    156     }
    157 
    158     /**
    159      * Verify that listening for network requires permissions.
    160      * <p>
    161      * Requires Permission:
    162      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    163      */
    164     @UiThreadTest
    165     public void testRequestLocationUpdatesNetwork() {
    166         checkRequestLocationUpdates(LocationManager.NETWORK_PROVIDER);
    167     }
    168 
    169     /**
    170      * Verify that listening for GPS location requires permissions.
    171      * <p>
    172      * Requires Permission:
    173      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    174      */
    175     @UiThreadTest
    176     public void testRequestLocationUpdatesGps() {
    177         checkRequestLocationUpdates(LocationManager.GPS_PROVIDER);
    178     }
    179 
    180     /**
    181      * Verify that adding a proximity alert requires permissions.
    182      * <p>
    183      * Requires Permission:
    184      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    185      */
    186     @SmallTest
    187     public void testAddProximityAlert() {
    188         PendingIntent mockPendingIntent = PendingIntent.getBroadcast(mContext,
    189                 0, new Intent("mockIntent"), PendingIntent.FLAG_ONE_SHOT);
    190         try {
    191             mLocationManager.addProximityAlert(0, 0, 100, -1, mockPendingIntent);
    192             fail("LocationManager.addProximityAlert did not throw SecurityException as expected");
    193         } catch (SecurityException e) {
    194             // expected
    195         }
    196     }
    197 
    198     /**
    199      * Helper method to verify that calling getLastKnownLocation with given
    200      * provider throws SecurityException.
    201      *
    202      * @param provider the String provider name.
    203      */
    204     private void checkGetLastKnownLocation(String provider) {
    205         if (!isKnownLocationProvider(provider)) {
    206             // skip this test if the provider is unknown
    207             return;
    208         }
    209 
    210         try {
    211             mLocationManager.getLastKnownLocation(provider);
    212             fail("LocationManager.getLastKnownLocation did not" +
    213                     " throw SecurityException as expected");
    214         } catch (SecurityException e) {
    215             // expected
    216         }
    217     }
    218 
    219     /**
    220      * Verify that getting the last known GPS location requires permissions.
    221      * <p>
    222      * Requires Permission:
    223      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    224      */
    225     @SmallTest
    226     public void testGetLastKnownLocationGps() {
    227         checkGetLastKnownLocation(LocationManager.GPS_PROVIDER);
    228     }
    229 
    230     /**
    231      * Verify that getting the last known network location requires permissions.
    232      * <p>
    233      * Requires Permission:
    234      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    235      */
    236     @SmallTest
    237     public void testGetLastKnownLocationNetwork() {
    238         checkGetLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    239     }
    240 
    241     /**
    242      * Helper method to verify that calling getProvider with given provider
    243      * throws SecurityException.
    244      *
    245      * @param provider the String provider name.
    246      */
    247     private void checkGetProvider(String provider) {
    248         if (!isKnownLocationProvider(provider)) {
    249             // skip this test if the provider is unknown
    250             return;
    251         }
    252 
    253         try {
    254             mLocationManager.getProvider(provider);
    255             fail("LocationManager.getProvider did not throw SecurityException as expected");
    256         } catch (SecurityException e) {
    257             // expected
    258         }
    259     }
    260 
    261     /**
    262      * Verify that getting the GPS provider requires permissions.
    263      * <p>
    264      * Requires Permission:
    265      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    266      */
    267     @SmallTest
    268     public void testGetProviderGps() {
    269         checkGetProvider(LocationManager.GPS_PROVIDER);
    270     }
    271 
    272     /**
    273      * Verify that getting the network provider requires permissions.
    274      * <p>
    275      * Requires Permission:
    276      * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
    277      */
    278     // TODO: remove from small test suite until network provider can be enabled
    279     // on test devices
    280     // @SmallTest
    281     public void testGetProviderNetwork() {
    282         checkGetProvider(LocationManager.NETWORK_PROVIDER);
    283     }
    284 
    285     /**
    286      * Helper method to verify that calling
    287      * {@link LocationManager#isProviderEnabled(String)} with given
    288      * provider completes without an exception. (Note that under the conditions
    289      * of these tests, that method threw SecurityException on OS levels before
    290      * {@link android.os.Build.VERSION_CODES#LOLLIPOP}. See the method's javadoc for
    291      * details.)
    292      *
    293      * @param provider the String provider name.
    294      */
    295     private void checkIsProviderEnabled(String provider) {
    296         if (!isKnownLocationProvider(provider)) {
    297             // skip this test if the provider is unknown
    298             return;
    299         }
    300         mLocationManager.isProviderEnabled(provider);
    301     }
    302 
    303     /**
    304      * Verify that checking IsProviderEnabled for GPS requires permissions.
    305      * <p>
    306      * Requires Permission:
    307      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    308      */
    309     @SmallTest
    310     public void testIsProviderEnabledGps() {
    311         checkIsProviderEnabled(LocationManager.GPS_PROVIDER);
    312     }
    313 
    314     /**
    315      * Verify that checking IsProviderEnabled for network requires permissions.
    316      * <p>
    317      * Requires Permission:
    318      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
    319      */
    320     @SmallTest
    321     public void testIsProviderEnabledNetwork() {
    322         checkIsProviderEnabled(LocationManager.NETWORK_PROVIDER);
    323     }
    324 
    325     /**
    326      * Verify that checking addTestProvider for network requires permissions.
    327      * <p>
    328      * Requires Permission:
    329      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    330      */
    331     @SmallTest
    332     public void testAddTestProvider() {
    333         final int TEST_POWER_REQUIREMENT_VALE = 0;
    334         final int TEST_ACCURACY_VALUE = 1;
    335 
    336         try {
    337             mLocationManager.addTestProvider(TEST_PROVIDER_NAME, true, true, true, true,
    338                     true, true, true, TEST_POWER_REQUIREMENT_VALE, TEST_ACCURACY_VALUE);
    339             fail("LocationManager.addTestProvider did not throw SecurityException as expected");
    340         } catch (SecurityException e) {
    341             // expected
    342         }
    343     }
    344 
    345     /**
    346      * Verify that checking removeTestProvider for network requires permissions.
    347      * <p>
    348      * Requires Permission:
    349      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    350      */
    351     @SmallTest
    352     public void testRemoveTestProvider() {
    353         try {
    354             mLocationManager.removeTestProvider(TEST_PROVIDER_NAME);
    355             fail("LocationManager.removeTestProvider did not throw SecurityException as"
    356                     + " expected");
    357         } catch (SecurityException e) {
    358             // expected
    359         }
    360     }
    361 
    362     /**
    363      * Verify that checking setTestProviderLocation for network requires
    364      * permissions.
    365      * <p>
    366      * Requires Permission:
    367      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    368      */
    369     @SmallTest
    370     public void testSetTestProviderLocation() {
    371         Location location = new Location(TEST_PROVIDER_NAME);
    372         location.makeComplete();
    373 
    374         try {
    375             mLocationManager.setTestProviderLocation(TEST_PROVIDER_NAME, location);
    376             fail("LocationManager.setTestProviderLocation did not throw SecurityException as"
    377                     + " expected");
    378         } catch (SecurityException e) {
    379             // expected
    380         }
    381     }
    382 
    383     /**
    384      * Verify that checking clearTestProviderLocation for network requires
    385      * permissions.
    386      * <p>
    387      * Requires Permission:
    388      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    389      */
    390     @SmallTest
    391     public void testClearTestProviderLocation() {
    392         try {
    393             mLocationManager.clearTestProviderLocation(TEST_PROVIDER_NAME);
    394             fail("LocationManager.clearTestProviderLocation did not throw SecurityException as"
    395                     + " expected");
    396         } catch (SecurityException e) {
    397             // expected
    398         }
    399     }
    400 
    401     /**
    402      * Verify that checking setTestProviderEnabled requires permissions.
    403      * <p>
    404      * Requires Permission:
    405      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    406      */
    407     @SmallTest
    408     public void testSetTestProviderEnabled() {
    409         try {
    410             mLocationManager.setTestProviderEnabled(TEST_PROVIDER_NAME, true);
    411             fail("LocationManager.setTestProviderEnabled did not throw SecurityException as"
    412                     + " expected");
    413         } catch (SecurityException e) {
    414             // expected
    415         }
    416     }
    417 
    418     /**
    419      * Verify that checking clearTestProviderEnabled requires permissions.
    420      * <p>
    421      * Requires Permission:
    422      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    423      */
    424     @SmallTest
    425     public void testClearTestProviderEnabled() {
    426         try {
    427             mLocationManager.clearTestProviderEnabled(TEST_PROVIDER_NAME);
    428             fail("LocationManager.setTestProviderEnabled did not throw SecurityException as"
    429                     + " expected");
    430         } catch (SecurityException e) {
    431             // expected
    432         }
    433     }
    434 
    435     /**
    436      * Verify that checking setTestProviderStatus requires permissions.
    437      * <p>
    438      * Requires Permission:
    439      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    440      */
    441     @SmallTest
    442     public void testSetTestProviderStatus() {
    443         try {
    444             mLocationManager.setTestProviderStatus(TEST_PROVIDER_NAME, 0, Bundle.EMPTY, 0);
    445             fail("LocationManager.setTestProviderStatus did not throw SecurityException as"
    446                     + " expected");
    447         } catch (SecurityException e) {
    448         }
    449     }
    450 
    451     /**
    452      * Verify that checking clearTestProviderStatus requires permissions.
    453      * <p>
    454      * Requires Permission:
    455      * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
    456      */
    457     @SmallTest
    458     public void testClearTestProviderStatus() {
    459         try {
    460             mLocationManager.clearTestProviderStatus(TEST_PROVIDER_NAME);
    461             fail("LocationManager.setTestProviderStatus did not throw SecurityException as"
    462                     + " expected");
    463         } catch (SecurityException e) {
    464             // expected
    465         }
    466     }
    467 
    468     private static class MockLocationListener implements LocationListener {
    469         public void onLocationChanged(Location location) {
    470             // ignore
    471         }
    472 
    473         public void onProviderDisabled(String provider) {
    474             // ignore
    475         }
    476 
    477         public void onProviderEnabled(String provider) {
    478             // ignore
    479         }
    480 
    481         public void onStatusChanged(String provider, int status, Bundle extras) {
    482             // ignore
    483         }
    484     }
    485 }
    486