1 /* 2 * Copyright (C) 2019 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.appsecurity.cts.deviceids; 18 19 import static junit.framework.Assert.assertEquals; 20 import static junit.framework.Assert.fail; 21 22 import android.content.Context; 23 import android.os.Build; 24 import android.telephony.TelephonyManager; 25 import android.test.AndroidTestCase; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import com.android.compatibility.common.util.ShellIdentityUtils; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Verify apps can access device identifiers with the app op granted. 37 */ 38 @RunWith(AndroidJUnit4.class) 39 public class DeviceIdentifierAppOpTest { 40 private static final String DEVICE_ID_WITH_APPOP_ERROR_MESSAGE = 41 "An unexpected value was received by an app with the READ_DEVICE_IDENTIFIERS app op " 42 + "granted when invoking %s."; 43 44 @Test testAccessToDeviceIdentifiersWithAppOp()45 public void testAccessToDeviceIdentifiersWithAppOp() throws Exception { 46 TelephonyManager telephonyManager = 47 (TelephonyManager) InstrumentationRegistry.getContext().getSystemService( 48 Context.TELEPHONY_SERVICE); 49 try { 50 assertEquals(String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "getDeviceId"), 51 ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager, 52 (tm) -> tm.getDeviceId()), telephonyManager.getDeviceId()); 53 assertEquals(String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "getImei"), 54 ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager, 55 (tm) -> tm.getImei()), telephonyManager.getImei()); 56 assertEquals(String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "getMeid"), 57 ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager, 58 (tm) -> tm.getMeid()), telephonyManager.getMeid()); 59 assertEquals(String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "getSubscriberId"), 60 ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager, 61 (tm) -> tm.getSubscriberId()), telephonyManager.getSubscriberId()); 62 assertEquals( 63 String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "getSimSerialNumber"), 64 ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager, 65 (tm) -> tm.getSimSerialNumber()), 66 telephonyManager.getSimSerialNumber()); 67 assertEquals(String.format(DEVICE_ID_WITH_APPOP_ERROR_MESSAGE, "Build#getSerial"), 68 ShellIdentityUtils.invokeStaticMethodWithShellPermissions(Build::getSerial), 69 Build.getSerial()); 70 } catch (SecurityException e) { 71 fail("An app with the READ_DEVICE_IDENTIFIERS app op set to allowed must be able to " 72 + "access the device identifiers; caught SecurityException instead: " 73 + e); 74 } 75 } 76 } 77