Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 16 | package com.android.server.appop; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 17 | |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 18 | import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 19 | import static android.app.AppOpsManager.FILTER_BY_PACKAGE_NAME; |
| 20 | import static android.app.AppOpsManager.FILTER_BY_UID; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 21 | import static android.app.AppOpsManager.MODE_ALLOWED; |
| 22 | import static android.app.AppOpsManager.MODE_ERRORED; |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 23 | import static android.app.AppOpsManager.MODE_FOREGROUND; |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 24 | import static android.app.AppOpsManager.OP_COARSE_LOCATION; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 25 | import static android.app.AppOpsManager.OP_FLAGS_ALL; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 26 | import static android.app.AppOpsManager.OP_READ_SMS; |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 27 | import static android.app.AppOpsManager.OP_WIFI_SCAN; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 28 | import static android.app.AppOpsManager.OP_WRITE_SMS; |
| 29 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 30 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
| 31 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 32 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 33 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
| 34 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy; |
| 35 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
| 36 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 37 | import static com.google.common.truth.Truth.assertThat; |
| 38 | import static com.google.common.truth.Truth.assertWithMessage; |
| 39 | |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 40 | import static org.mockito.ArgumentMatchers.any; |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 41 | import static org.mockito.ArgumentMatchers.anyInt; |
| 42 | import static org.mockito.ArgumentMatchers.anyString; |
| 43 | import static org.mockito.ArgumentMatchers.eq; |
| 44 | import static org.mockito.ArgumentMatchers.nullable; |
| 45 | |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 46 | import android.app.ActivityManager; |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 47 | import android.app.AppOpsManager; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 48 | import android.app.AppOpsManager.OpEntry; |
| 49 | import android.app.AppOpsManager.PackageOps; |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 50 | import android.content.ContentResolver; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 51 | import android.content.Context; |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 52 | import android.content.pm.PackageManagerInternal; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 53 | import android.os.Handler; |
| 54 | import android.os.HandlerThread; |
| 55 | import android.os.Process; |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 56 | import android.os.RemoteCallback; |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 57 | import android.provider.Settings; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 58 | |
| 59 | import androidx.test.InstrumentationRegistry; |
| 60 | import androidx.test.filters.SmallTest; |
| 61 | import androidx.test.runner.AndroidJUnit4; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 62 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 63 | import com.android.dx.mockito.inline.extended.StaticMockitoSession; |
| 64 | import com.android.server.LocalServices; |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 65 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 66 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 67 | import org.junit.After; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 68 | import org.junit.Before; |
| 69 | import org.junit.Test; |
| 70 | import org.junit.runner.RunWith; |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 71 | import org.mockito.quality.Strictness; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 72 | |
| 73 | import java.io.File; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 74 | import java.util.Collections; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 75 | import java.util.List; |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 76 | import java.util.concurrent.CountDownLatch; |
| 77 | import java.util.concurrent.TimeUnit; |
| 78 | import java.util.concurrent.atomic.AtomicReference; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Unit tests for AppOpsService. Covers functionality that is difficult to test using CTS tests |
| 82 | * or for which we can write more detailed unit tests than CTS tests (because the internal APIs are |
| 83 | * more finegrained data than the public ones). |
| 84 | */ |
| 85 | @SmallTest |
| 86 | @RunWith(AndroidJUnit4.class) |
| 87 | public class AppOpsServiceTest { |
| 88 | |
| 89 | private static final String TAG = AppOpsServiceTest.class.getSimpleName(); |
| 90 | // State will be persisted into this XML file. |
| 91 | private static final String APP_OPS_FILENAME = "appops-service-test.xml"; |
| 92 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 93 | private static final Context sContext = InstrumentationRegistry.getTargetContext(); |
| 94 | private static final String sMyPackageName = sContext.getOpPackageName(); |
| 95 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 96 | private File mAppOpsFile; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 97 | private Handler mHandler; |
| 98 | private AppOpsService mAppOpsService; |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 99 | private int mMyUid; |
| 100 | private long mTestStartMillis; |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 101 | private StaticMockitoSession mMockingSession; |
| 102 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 103 | private void setupAppOpsService() { |
Philip P. Moltmann | fe2fc3a | 2020-02-05 16:59:39 -0800 | [diff] [blame] | 104 | mAppOpsService = new AppOpsService(mAppOpsFile, mHandler, spy(sContext)); |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 105 | mAppOpsService.mHistoricalRegistry.systemReady(sContext.getContentResolver()); |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 106 | |
| 107 | // Always approve all permission checks |
| 108 | doNothing().when(mAppOpsService.mContext).enforcePermission(anyString(), anyInt(), |
| 109 | anyInt(), nullable(String.class)); |
| 110 | } |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 111 | |
| 112 | @Before |
| 113 | public void setUp() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 114 | mAppOpsFile = new File(sContext.getFilesDir(), APP_OPS_FILENAME); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 115 | if (mAppOpsFile.exists()) { |
| 116 | // Start with a clean state (persisted into XML). |
| 117 | mAppOpsFile.delete(); |
| 118 | } |
| 119 | |
| 120 | HandlerThread handlerThread = new HandlerThread(TAG); |
| 121 | handlerThread.start(); |
| 122 | mHandler = new Handler(handlerThread.getLooper()); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 123 | mMyUid = Process.myUid(); |
| 124 | |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 125 | initializeStaticMocks(); |
| 126 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 127 | setupAppOpsService(); |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 128 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 129 | mTestStartMillis = System.currentTimeMillis(); |
| 130 | } |
| 131 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 132 | @After |
| 133 | public void resetStaticMocks() { |
| 134 | mMockingSession.finishMocking(); |
| 135 | } |
| 136 | |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 137 | private void initializeStaticMocks() { |
| 138 | mMockingSession = mockitoSession() |
| 139 | .strictness(Strictness.LENIENT) |
| 140 | .spyStatic(LocalServices.class) |
| 141 | .spyStatic(Settings.Global.class) |
| 142 | .startMocking(); |
| 143 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 144 | // Mock LocalServices.getService(PackageManagerInternal.class).getPackage dependency |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 145 | // needed by AppOpsService |
| 146 | PackageManagerInternal mockPackageManagerInternal = mock(PackageManagerInternal.class); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 147 | AndroidPackage mockMyPkg = mock(AndroidPackage.class); |
| 148 | when(mockMyPkg.isPrivileged()).thenReturn(false); |
| 149 | when(mockMyPkg.getUid()).thenReturn(mMyUid); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 150 | when(mockMyPkg.getAttributions()).thenReturn(Collections.emptyList()); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 151 | |
| 152 | when(mockPackageManagerInternal.getPackage(sMyPackageName)).thenReturn(mockMyPkg); |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 153 | doReturn(mockPackageManagerInternal).when( |
| 154 | () -> LocalServices.getService(PackageManagerInternal.class)); |
| 155 | |
| 156 | // Mock behavior to use specific Settings.Global.APPOP_HISTORY_PARAMETERS |
| 157 | doReturn(null).when(() -> Settings.Global.getString(any(ContentResolver.class), |
| 158 | eq(Settings.Global.APPOP_HISTORY_PARAMETERS))); |
| 159 | } |
| 160 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 161 | @Test |
| 162 | public void testGetOpsForPackage_noOpsLogged() { |
| 163 | assertThat(getLoggedOps()).isNull(); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testNoteOperationAndGetOpsForPackage() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 168 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
| 169 | mAppOpsService.setMode(OP_WRITE_SMS, mMyUid, sMyPackageName, MODE_ERRORED); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 170 | |
| 171 | // Note an op that's allowed. |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 172 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 173 | List<PackageOps> loggedOps = getLoggedOps(); |
| 174 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 175 | |
| 176 | // Note another op that's not allowed. |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 177 | mAppOpsService.noteOperation(OP_WRITE_SMS, mMyUid, sMyPackageName, null, false, null, |
| 178 | false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 179 | loggedOps = getLoggedOps(); |
| 180 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 181 | assertContainsOp(loggedOps, OP_WRITE_SMS, -1, mTestStartMillis, MODE_ERRORED); |
| 182 | } |
| 183 | |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 184 | /** |
| 185 | * Tests the scenario where an operation's permission is controlled by another operation. |
| 186 | * For example the results of a WIFI_SCAN can be used to infer the location of a user, so the |
| 187 | * ACCESS_COARSE_LOCATION op is used to check whether WIFI_SCAN is allowed. |
| 188 | */ |
| 189 | @Test |
| 190 | public void testNoteOperationAndGetOpsForPackage_controlledByDifferentOp() { |
| 191 | // This op controls WIFI_SCAN |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 192 | mAppOpsService.setMode(OP_COARSE_LOCATION, mMyUid, sMyPackageName, MODE_ALLOWED); |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 193 | |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 194 | assertThat(mAppOpsService.noteOperation(OP_WIFI_SCAN, mMyUid, sMyPackageName, null, false, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 195 | null, false)).isEqualTo(MODE_ALLOWED); |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 196 | |
| 197 | assertContainsOp(getLoggedOps(), OP_WIFI_SCAN, mTestStartMillis, -1, |
| 198 | MODE_ALLOWED /* default for WIFI_SCAN; this is not changed or used in this test */); |
| 199 | |
| 200 | // Now set COARSE_LOCATION to ERRORED -> this will make WIFI_SCAN disabled as well. |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 201 | mAppOpsService.setMode(OP_COARSE_LOCATION, mMyUid, sMyPackageName, MODE_ERRORED); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 202 | assertThat(mAppOpsService.noteOperation(OP_WIFI_SCAN, mMyUid, sMyPackageName, null, false, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 203 | null, false)).isEqualTo(MODE_ERRORED); |
Peter Visontay | 7671236 | 2018-04-19 22:04:14 +0100 | [diff] [blame] | 204 | |
| 205 | assertContainsOp(getLoggedOps(), OP_WIFI_SCAN, mTestStartMillis, mTestStartMillis, |
| 206 | MODE_ALLOWED /* default for WIFI_SCAN; this is not changed or used in this test */); |
| 207 | } |
| 208 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 209 | // Tests the dumping and restoring of the in-memory state to/from XML. |
| 210 | @Test |
| 211 | public void testStatePersistence() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 212 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
| 213 | mAppOpsService.setMode(OP_WRITE_SMS, mMyUid, sMyPackageName, MODE_ERRORED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 214 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
| 215 | mAppOpsService.noteOperation(OP_WRITE_SMS, mMyUid, sMyPackageName, null, false, null, |
| 216 | false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 217 | mAppOpsService.writeState(); |
| 218 | |
| 219 | // Create a new app ops service, and initialize its state from XML. |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 220 | setupAppOpsService(); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 221 | mAppOpsService.readState(); |
| 222 | |
| 223 | // Query the state of the 2nd service. |
| 224 | List<PackageOps> loggedOps = getLoggedOps(); |
| 225 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 226 | assertContainsOp(loggedOps, OP_WRITE_SMS, -1, mTestStartMillis, MODE_ERRORED); |
| 227 | } |
| 228 | |
| 229 | // Tests that ops are persisted during shutdown. |
| 230 | @Test |
| 231 | public void testShutdown() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 232 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 233 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 234 | mAppOpsService.shutdown(); |
| 235 | |
| 236 | // Create a new app ops service, and initialize its state from XML. |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 237 | setupAppOpsService(); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 238 | mAppOpsService.readState(); |
| 239 | |
| 240 | // Query the state of the 2nd service. |
| 241 | List<PackageOps> loggedOps = getLoggedOps(); |
| 242 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void testGetOpsForPackage() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 247 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 248 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 249 | |
| 250 | // Query all ops |
| 251 | List<PackageOps> loggedOps = mAppOpsService.getOpsForPackage( |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 252 | mMyUid, sMyPackageName, null /* all ops */); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 253 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 254 | |
| 255 | // Query specific ops |
| 256 | loggedOps = mAppOpsService.getOpsForPackage( |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 257 | mMyUid, sMyPackageName, new int[]{OP_READ_SMS, OP_WRITE_SMS}); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 258 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 259 | |
| 260 | // Query unknown UID |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 261 | loggedOps = mAppOpsService.getOpsForPackage(mMyUid + 1, sMyPackageName, null /* all ops */); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 262 | assertThat(loggedOps).isNull(); |
| 263 | |
| 264 | // Query unknown package name |
| 265 | loggedOps = mAppOpsService.getOpsForPackage(mMyUid, "fake.package", null /* all ops */); |
| 266 | assertThat(loggedOps).isNull(); |
| 267 | |
| 268 | // Query op code that's not been logged |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 269 | loggedOps = mAppOpsService.getOpsForPackage(mMyUid, sMyPackageName, |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 270 | new int[]{OP_WRITE_SMS}); |
| 271 | assertThat(loggedOps).isNull(); |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testPackageRemoved() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 276 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 277 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 278 | |
| 279 | List<PackageOps> loggedOps = getLoggedOps(); |
| 280 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 281 | |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 282 | mAppOpsService.packageRemoved(mMyUid, sMyPackageName); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 283 | assertThat(getLoggedOps()).isNull(); |
| 284 | } |
| 285 | |
Philip P. Moltmann | 0027a67 | 2019-06-29 03:36:19 +0000 | [diff] [blame] | 286 | |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 287 | /* |
| 288 | TODO ntmyren: re enable when we have time to rewrite test. |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 289 | @Test |
| 290 | public void testPackageRemovedHistoricalOps() throws InterruptedException { |
| 291 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 292 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 293 | |
| 294 | AppOpsManager.HistoricalOps historicalOps = new AppOpsManager.HistoricalOps(0, 15000); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 295 | historicalOps.increaseAccessCount(OP_READ_SMS, mMyUid, sMyPackageName, null, |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 296 | AppOpsManager.UID_STATE_PERSISTENT, 0, 1); |
| 297 | |
| 298 | mAppOpsService.addHistoricalOps(historicalOps); |
| 299 | |
| 300 | AtomicReference<AppOpsManager.HistoricalOps> resultOpsRef = new AtomicReference<>(); |
| 301 | AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(1)); |
| 302 | RemoteCallback callback = new RemoteCallback(result -> { |
| 303 | resultOpsRef.set(result.getParcelable(AppOpsManager.KEY_HISTORICAL_OPS)); |
| 304 | latchRef.get().countDown(); |
| 305 | }); |
| 306 | |
| 307 | // First, do a fetch to ensure it's written |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 308 | mAppOpsService.getHistoricalOps(mMyUid, sMyPackageName, null, null, |
| 309 | FILTER_BY_UID | FILTER_BY_PACKAGE_NAME, 0, Long.MAX_VALUE, 0, callback); |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 310 | |
| 311 | latchRef.get().await(5, TimeUnit.SECONDS); |
| 312 | assertThat(latchRef.get().getCount()).isEqualTo(0); |
| 313 | assertThat(resultOpsRef.get().isEmpty()).isFalse(); |
| 314 | |
| 315 | // Then, check it's deleted on removal |
| 316 | mAppOpsService.packageRemoved(mMyUid, sMyPackageName); |
| 317 | |
| 318 | latchRef.set(new CountDownLatch(1)); |
| 319 | |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 320 | mAppOpsService.getHistoricalOps(mMyUid, sMyPackageName, null, null, |
| 321 | FILTER_BY_UID | FILTER_BY_PACKAGE_NAME, 0, Long.MAX_VALUE, 0, callback); |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 322 | |
| 323 | latchRef.get().await(5, TimeUnit.SECONDS); |
| 324 | assertThat(latchRef.get().getCount()).isEqualTo(0); |
| 325 | assertThat(resultOpsRef.get().isEmpty()).isTrue(); |
| 326 | } |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 327 | */ |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 328 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 329 | @Test |
| 330 | public void testUidRemoved() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 331 | mAppOpsService.setMode(OP_READ_SMS, mMyUid, sMyPackageName, MODE_ALLOWED); |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 332 | mAppOpsService.noteOperation(OP_READ_SMS, mMyUid, sMyPackageName, null, false, null, false); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 333 | |
| 334 | List<PackageOps> loggedOps = getLoggedOps(); |
| 335 | assertContainsOp(loggedOps, OP_READ_SMS, mTestStartMillis, -1, MODE_ALLOWED); |
| 336 | |
| 337 | mAppOpsService.uidRemoved(mMyUid); |
| 338 | assertThat(getLoggedOps()).isNull(); |
| 339 | } |
| 340 | |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 341 | private void setupProcStateTests() { |
| 342 | // For the location proc state tests |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 343 | mAppOpsService.setMode(OP_COARSE_LOCATION, mMyUid, sMyPackageName, MODE_FOREGROUND); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 344 | mAppOpsService.mConstants.FG_SERVICE_STATE_SETTLE_TIME = 0; |
| 345 | mAppOpsService.mConstants.TOP_STATE_SETTLE_TIME = 0; |
| 346 | mAppOpsService.mConstants.BG_STATE_SETTLE_TIME = 0; |
| 347 | } |
| 348 | |
| 349 | @Test |
| 350 | public void testUidProcStateChange_cachedToTopToCached() throws Exception { |
| 351 | setupProcStateTests(); |
| 352 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 353 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 354 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 355 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 356 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 357 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 358 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_TOP, |
| 359 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 360 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 361 | false, null, false)).isEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 362 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 363 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 364 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 365 | // Second time to make sure that settle time is overcome |
| 366 | Thread.sleep(50); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 367 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 368 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 369 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 370 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Test |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 374 | public void testUidProcStateChange_cachedToFgs() { |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 375 | setupProcStateTests(); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 376 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 377 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 378 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 379 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 380 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 381 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 382 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 383 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 384 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Test |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 388 | public void testUidProcStateChange_cachedToFgsLocation() { |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 389 | setupProcStateTests(); |
| 390 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 391 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 392 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 393 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 394 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 395 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 396 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 397 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 398 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 399 | false, null, false)).isEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Test |
| 403 | public void testUidProcStateChange_topToFgs() throws Exception { |
| 404 | setupProcStateTests(); |
| 405 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 406 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 407 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 408 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 409 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 410 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 411 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_TOP, |
| 412 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 413 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 414 | false, null, false)).isEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 415 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 416 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 417 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 418 | // Second time to make sure that settle time is overcome |
| 419 | Thread.sleep(50); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 420 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 421 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 422 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 423 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | @Test |
| 427 | public void testUidProcStateChange_topToFgsLocationToFgs() throws Exception { |
| 428 | setupProcStateTests(); |
| 429 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 430 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_CACHED_EMPTY, |
| 431 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 432 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 433 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 434 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 435 | mAppOpsService.updateUidProcState(mMyUid, ActivityManager.PROCESS_STATE_TOP, |
| 436 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 437 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 438 | false, null, false)).isEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 439 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 440 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 441 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 442 | // Second time to make sure that settle time is overcome |
| 443 | Thread.sleep(50); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 444 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 445 | ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 446 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 447 | false, null, false)).isEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 448 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 449 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 450 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 451 | // Second time to make sure that settle time is overcome |
| 452 | Thread.sleep(50); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 453 | mAppOpsService.updateUidProcState(mMyUid, PROCESS_STATE_FOREGROUND_SERVICE, |
| 454 | ActivityManager.PROCESS_CAPABILITY_NONE); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 455 | assertThat(mAppOpsService.noteOperation(OP_COARSE_LOCATION, mMyUid, sMyPackageName, null, |
Stanislav Zholnin | 54762b2 | 2020-06-19 13:20:48 +0100 | [diff] [blame] | 456 | false, null, false)).isNotEqualTo(MODE_ALLOWED); |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 459 | private List<PackageOps> getLoggedOps() { |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 460 | return mAppOpsService.getOpsForPackage(mMyUid, sMyPackageName, null /* all ops */); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | private void assertContainsOp(List<PackageOps> loggedOps, int opCode, long minMillis, |
| 464 | long minRejectMillis, int mode) { |
| 465 | |
| 466 | boolean opLogged = false; |
| 467 | for (PackageOps pkgOps : loggedOps) { |
| 468 | assertWithMessage("Unexpected UID").that(mMyUid).isEqualTo(pkgOps.getUid()); |
Philip P. Moltmann | 6b4f3d5 | 2019-05-08 15:14:09 -0700 | [diff] [blame] | 469 | assertWithMessage("Unexpected package name").that(sMyPackageName).isEqualTo( |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 470 | pkgOps.getPackageName()); |
| 471 | |
| 472 | for (OpEntry opEntry : pkgOps.getOps()) { |
| 473 | if (opCode != opEntry.getOp()) { |
| 474 | continue; |
| 475 | } |
| 476 | opLogged = true; |
| 477 | |
| 478 | assertWithMessage("Unexpected mode").that(mode).isEqualTo(opEntry.getMode()); |
| 479 | if (minMillis > 0) { |
| 480 | assertWithMessage("Unexpected timestamp") |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 481 | .that(opEntry.getLastAccessTime(OP_FLAGS_ALL)).isAtLeast(minMillis); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 482 | } |
| 483 | if (minRejectMillis > 0) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 484 | assertWithMessage("Unexpected rejection timestamp").that( |
| 485 | opEntry.getLastRejectTime(OP_FLAGS_ALL)).isAtLeast(minRejectMillis); |
Peter Visontay | 859206c | 2018-01-03 15:43:19 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | assertWithMessage("Op was not logged").that(opLogged).isTrue(); |
| 490 | } |
| 491 | } |