Added tests to CTS to check permissions are enforced.

Change-Id: Ia8922762a22242a6b685730bdc2c6ef64b5f8e07
diff --git a/tests/tests/permission/src/android/permission/cts/AccountManagerTest.java b/tests/tests/permission/src/android/permission/cts/AccountManagerTest.java
new file mode 100644
index 0000000..5d4378f
--- /dev/null
+++ b/tests/tests/permission/src/android/permission/cts/AccountManagerTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.permission.cts;
+
+import android.content.Context;
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.accounts.AccountManagerFuture;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+/**
+ * Verify GET_ACCOUNTS permissions are enforced.
+ */
+public class AccountManagerTest extends AndroidTestCase {
+
+    private AccountManager mAccountManager;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        mAccountManager = AccountManager.get(getContext());
+        assertNotNull(mAccountManager);
+    }
+
+    /**
+     * Verifies that AccountManager.getAccounts() requires Permission.
+     * <p>
+     * Requires Permission: {@link android.Manifest.permission#GET_ACCOUNTS}.
+     */
+    @SmallTest
+    public void testGetAccounts() {
+        try {
+            mAccountManager.getAccounts();
+            fail("AccountManager.getAccounts() did not throw SecurityException as expected");
+        } catch (SecurityException se) {
+            // Expected Exception
+        }
+    }
+
+    /**
+     * Verifies that AccountManager.getAccountsByType() requires Permission.
+     * <p>
+     * Requires Permission: {@link android.Manifest.permission#GET_ACCOUNTS}.
+     */
+    @SmallTest
+    public void testGetAccountsByType() {
+        try {
+            mAccountManager.getAccountsByType(null);
+            fail("AccountManager.getAccountsByType() did not throw SecurityException as expected");
+        } catch (SecurityException se) {
+            // Expected Exception
+        }
+    }
+
+    /**
+     * Verifies that AccountManager.getAccountsByTypeAndFeatures() requires
+     * Permission.
+     * <p>
+     * Requires Permission: {@link android.Manifest.permission#GET_ACCOUNTS}.
+     */
+    @SmallTest
+    public void testGetAccountsByTypeAndFeatures() {
+        try {
+            mAccountManager.getAccountsByTypeAndFeatures("", null, null, null);
+            fail("AccountManager.getAccountsByTypeAndFeatures() did not throw SecurityException as expected");
+        } catch (SecurityException se) {
+            // Expected Exception
+        }
+    }
+}
diff --git a/tests/tests/permission/src/android/permission/cts/ContactsProviderTest.java b/tests/tests/permission/src/android/permission/cts/ContactsProviderTest.java
new file mode 100644
index 0000000..b487693
--- /dev/null
+++ b/tests/tests/permission/src/android/permission/cts/ContactsProviderTest.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.permission.cts;
+
+import android.os.RemoteException;
+import android.content.ContentProviderClient;
+import android.content.ContentResolver;
+import android.database.Cursor;
+import android.provider.Contacts;
+import android.provider.ContactsContract;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+/**
+ * Verify permissions are enforced.
+ */
+public class ContactsProviderTest extends AndroidTestCase {
+    private ContentProviderClient mProvider;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mProvider = getContext()
+                .getContentResolver().acquireContentProviderClient(Contacts.AUTHORITY);
+        assertNotNull(mProvider);
+    }
+
+    /**
+     * Verifies that query(ContactsContract.Contacts.CONTENT_URI) requires
+     * Permission.
+     * <p>
+     * Requires Permission: {@link android.Manifest.permission#READ_CONTACTS}.
+     */
+    @SmallTest
+    public void testQueryContacts() {
+        try {
+            mProvider.query(ContactsContract.Contacts.CONTENT_URI,
+                    null, null, null, null);
+            fail("query(ContactsContract.Contacts.CONTENT_URI) did not throw SecurityException as expected");
+        } catch (SecurityException se) {
+            // Expected Exception
+        } catch (RemoteException e) {
+            fail("testQueryContacts throws unexpected exception");
+        }
+    }
+
+    /**
+     * Verifies that query(ContactsContract.Profile.CONTENT_URI) requires
+     * Permission.
+     * <p>
+     * Requires Permission: {@link android.Manifest.permission#READ_PROFILE}.
+     */
+    @SmallTest
+    public void testQueryProfile() {
+        try {
+            Cursor cursor = mProvider.query(ContactsContract.Profile.CONTENT_URI,
+                    null, null, null, null);
+            fail("query(ContactsContract.Profile.CONTENT_URI) did not throw SecurityException as expected");
+        } catch (SecurityException se) {
+            // Expected Exception
+        } catch (RemoteException e) {
+            fail("testQueryProfile throws unexpected exception");
+        }
+    }
+}
diff --git a/tests/tests/permission/src/android/permission/cts/NoLocationPermissionTest.java b/tests/tests/permission/src/android/permission/cts/NoLocationPermissionTest.java
index b3cf682..6e5d5f1 100644
--- a/tests/tests/permission/src/android/permission/cts/NoLocationPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/NoLocationPermissionTest.java
@@ -16,8 +16,6 @@
 
 package android.permission.cts;
 
-import java.util.List;
-
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
@@ -31,6 +29,8 @@
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.SmallTest;
 
+import java.util.List;
+
 /**
  * Verify the location access without specific permissions.
  */
@@ -57,13 +57,14 @@
 
     /**
      * Verify that listen or get cell location requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_COARSE_LOCATION.}
+     * <p>
+     * Requires Permission: {@link
+     * android.Manifest.permission#ACCESS_COARSE_LOCATION.}
      */
     @SmallTest
     public void testListenCellLocation() {
         TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(
-                   Context.TELEPHONY_SERVICE);
+                Context.TELEPHONY_SERVICE);
         PhoneStateListener phoneStateListener = new PhoneStateListener();
         try {
             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
@@ -79,16 +80,30 @@
         } catch (SecurityException e) {
             // expected
         }
+
+        try {
+            telephonyManager.getNeighboringCellInfo();
+            fail("TelephonyManager.getNeighbouringCellInfo did not throw SecurityException as expected");
+        } catch (SecurityException e) {
+            // expected
+        }
+
+        try {
+            telephonyManager.getAllCellInfo();
+            fail("TelephonyManager.getAllCellInfo did not throw SecurityException as expected");
+        } catch (SecurityException e) {
+            // expected
+        }
     }
 
     /**
      * Helper method to verify that calling requestLocationUpdates with given
      * provider throws SecurityException.
-     *
+     * 
      * @param provider the String provider name.
      */
     private void checkRequestLocationUpdates(String provider) {
-        if ( !isKnownLocationProvider(provider) ) {
+        if (!isKnownLocationProvider(provider)) {
             // skip this test if the provider is unknown
             return;
         }
@@ -114,8 +129,9 @@
 
     /**
      * Verify that listening for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testRequestLocationUpdatesNetwork() {
@@ -124,8 +140,9 @@
 
     /**
      * Verify that listening for GPS location requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testRequestLocationUpdatesGps() {
@@ -134,8 +151,9 @@
 
     /**
      * Verify that adding a proximity alert requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testAddProximityAlert() {
@@ -152,11 +170,11 @@
     /**
      * Helper method to verify that calling getLastKnownLocation with given
      * provider throws SecurityException.
-     *
+     * 
      * @param provider the String provider name.
      */
     private void checkGetLastKnownLocation(String provider) {
-        if ( !isKnownLocationProvider(provider) ) {
+        if (!isKnownLocationProvider(provider)) {
             // skip this test if the provider is unknown
             return;
         }
@@ -172,8 +190,9 @@
 
     /**
      * Verify that getting the last known GPS location requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testGetLastKnownLocationGps() {
@@ -182,8 +201,9 @@
 
     /**
      * Verify that getting the last known network location requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testGetLastKnownLocationNetwork() {
@@ -193,11 +213,11 @@
     /**
      * Helper method to verify that calling getProvider with given provider
      * throws SecurityException.
-     *
+     * 
      * @param provider the String provider name.
      */
     private void checkGetProvider(String provider) {
-        if ( !isKnownLocationProvider(provider) ) {
+        if (!isKnownLocationProvider(provider)) {
             // skip this test if the provider is unknown
             return;
         }
@@ -212,8 +232,9 @@
 
     /**
      * Verify that getting the GPS provider requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testGetProviderGps() {
@@ -222,11 +243,13 @@
 
     /**
      * Verify that getting the network provider requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
      */
-    // TODO: remove from small test suite until network provider can be enabled on test devices
-    //@SmallTest
+    // TODO: remove from small test suite until network provider can be enabled
+    // on test devices
+    // @SmallTest
     public void testGetProviderNetwork() {
         checkGetProvider(LocationManager.NETWORK_PROVIDER);
     }
@@ -234,11 +257,11 @@
     /**
      * Helper method to verify that calling isProviderEnabled with given
      * provider throws SecurityException.
-     *
+     * 
      * @param provider the String provider name.
      */
     private void checkIsProviderEnabled(String provider) {
-        if ( !isKnownLocationProvider(provider) ) {
+        if (!isKnownLocationProvider(provider)) {
             // skip this test if the provider is unknown
             return;
         }
@@ -253,8 +276,9 @@
 
     /**
      * Verify that checking IsProviderEnabled for GPS requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testIsProviderEnabledGps() {
@@ -263,8 +287,9 @@
 
     /**
      * Verify that checking IsProviderEnabled for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
      */
     @SmallTest
     public void testIsProviderEnabledNetwork() {
@@ -273,8 +298,9 @@
 
     /**
      * Verify that checking addTestProvider for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testAddTestProvider() {
@@ -286,13 +312,15 @@
                     true, true, true, TEST_POWER_REQUIREMENT_VALE, TEST_ACCURACY_VALUE);
             fail("LocationManager.addTestProvider did not throw SecurityException as expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
      * Verify that checking removeTestProvider for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testRemoveTestProvider() {
@@ -301,13 +329,16 @@
             fail("LocationManager.removeTestProvider did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
-     * Verify that checking setTestProviderLocation for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * Verify that checking setTestProviderLocation for network requires
+     * permissions.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testSetTestProviderLocation() {
@@ -319,13 +350,16 @@
             fail("LocationManager.setTestProviderLocation did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
-     * Verify that checking clearTestProviderLocation for network requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * Verify that checking clearTestProviderLocation for network requires
+     * permissions.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testClearTestProviderLocation() {
@@ -334,13 +368,15 @@
             fail("LocationManager.clearTestProviderLocation did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
      * Verify that checking setTestProviderEnabled requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testSetTestProviderEnabled() {
@@ -349,13 +385,15 @@
             fail("LocationManager.setTestProviderEnabled did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
      * Verify that checking clearTestProviderEnabled requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testClearTestProviderEnabled() {
@@ -364,13 +402,15 @@
             fail("LocationManager.setTestProviderEnabled did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
     /**
      * Verify that checking setTestProviderStatus requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testSetTestProviderStatus() {
@@ -384,8 +424,9 @@
 
     /**
      * Verify that checking clearTestProviderStatus requires permissions.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#ACCESS_MOCK_LOCATION}.
      */
     @SmallTest
     public void testClearTestProviderStatus() {
@@ -394,6 +435,7 @@
             fail("LocationManager.setTestProviderStatus did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
+            // expected
         }
     }
 
diff --git a/tests/tests/permission/src/android/permission/cts/TelephonyManagerPermissionTest.java b/tests/tests/permission/src/android/permission/cts/TelephonyManagerPermissionTest.java
index cbd1be1..4c116dd 100644
--- a/tests/tests/permission/src/android/permission/cts/TelephonyManagerPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/TelephonyManagerPermissionTest.java
@@ -22,8 +22,8 @@
 import android.test.suitebuilder.annotation.SmallTest;
 
 /**
-* Test the non-location-related functionality of TelephonyManager.
-*/
+ * Test the non-location-related functionality of TelephonyManager.
+ */
 public class TelephonyManagerPermissionTest extends AndroidTestCase {
 
     TelephonyManager mTelephonyManager = null;
@@ -37,8 +37,9 @@
 
     /**
      * Verify that TelephonyManager.getDeviceId requires Permission.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#READ_PHONE_STATE}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#READ_PHONE_STATE}.
      */
     @SmallTest
     public void testGetDeviceId() {
@@ -52,8 +53,9 @@
 
     /**
      * Verify that TelephonyManager.getLine1Number requires Permission.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#READ_PHONE_STATE}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#READ_PHONE_STATE}.
      */
     @SmallTest
     public void testGetLine1Number() {
@@ -67,8 +69,9 @@
 
     /**
      * Verify that TelephonyManager.getSimSerialNumber requires Permission.
-     * <p>Requires Permission:
-     *   {@link android.Manifest.permission#READ_PHONE_STATE}.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#READ_PHONE_STATE}.
      */
     @SmallTest
     public void testGetSimSerialNumber() {
@@ -79,5 +82,36 @@
             // expected
         }
     }
-}
 
+    /**
+     * Verify that TelephonyManager.getSubscriberId requires Permission.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#READ_PHONE_STATE}.
+     */
+    @SmallTest
+    public void testGetSubscriberId() {
+        try {
+            String sid = mTelephonyManager.getSubscriberId();
+            fail("Got subscriber id: " + sid);
+        } catch (SecurityException e) {
+            // expected
+        }
+    }
+
+    /**
+     * Verify that TelephonyManager.getVoiceMailNumber requires Permission.
+     * <p>
+     * Requires Permission:
+     * {@link android.Manifest.permission#READ_PHONE_STATE}.
+     */
+    @SmallTest
+    public void testVoiceMailNumber() {
+        try {
+            String vmnum = mTelephonyManager.getVoiceMailNumber();
+            fail("Got voicemail number: " + vmnum);
+        } catch (SecurityException e) {
+            // expected
+        }
+    }
+}