summaryrefslogtreecommitdiff
path: root/tests/tests/permission/src/android/permission/cts/BaseNotificationListenerCheckTest.java
blob: 9daeff2af4a08313814d5b753d5dc1b9cc1f49a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (C) 2022 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 static android.Manifest.permission.WRITE_DEVICE_CONFIG;
import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
import static android.os.Process.myUserHandle;
import static android.permission.cts.PermissionUtils.clearAppState;
import static android.permission.cts.TestUtils.eventually;

import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
import static com.android.server.job.nano.JobPackageHistoryProto.START_PERIODIC_JOB;
import static com.android.server.job.nano.JobPackageHistoryProto.STOP_PERIODIC_JOB;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import static java.lang.Math.max;
import static java.util.concurrent.TimeUnit.SECONDS;

import android.app.NotificationManager;
import android.app.UiAutomation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.platform.test.annotations.AppModeFull;
import android.provider.DeviceConfig;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SdkSuppress;
import androidx.test.runner.AndroidJUnit4;

import com.android.compatibility.common.util.DeviceConfigStateChangerRule;
import com.android.compatibility.common.util.ProtoUtils;
import com.android.server.job.nano.JobPackageHistoryProto;
import com.android.server.job.nano.JobSchedulerServiceDumpProto;
import com.android.server.job.nano.JobSchedulerServiceDumpProto.RegisteredJob;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.runner.RunWith;

import java.util.List;

/**
 * Base test class used for {@code NotificationListenerCheckTest} and
 * {@code NotificationListenerCheckWithSafetyCenterUnsupportedTest}
 */
@RunWith(AndroidJUnit4.class)
@AppModeFull(reason = "Cannot set system settings as instant app. Also we never show a notification"
        + " listener check notification for instant apps.")
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu")
public class BaseNotificationListenerCheckTest {
    private static final String LOG_TAG = BaseNotificationListenerCheckTest.class.getSimpleName();
    private static final boolean DEBUG = false;

    protected static final String TEST_APP_PKG =
            "android.permission.cts.appthathasnotificationlistener";
    private static final String TEST_APP_NOTIFICATION_SERVICE =
            TEST_APP_PKG + ".CtsNotificationListenerService";
    protected static final String TEST_APP_NOTIFICATION_LISTENER_APK =
            "/data/local/tmp/cts/permissions/CtsAppThatHasNotificationListener.apk";

    private static final int NOTIFICATION_LISTENER_CHECK_JOB_ID = 4;

    /**
     * Device config property for whether notification listener check is enabled on the device
     */
    private static final String PROPERTY_NOTIFICATION_LISTENER_CHECK_ENABLED =
            "notification_listener_check_enabled";

    /**
     * Device config property for time period in milliseconds after which current enabled
     * notification
     * listeners are queried
     */
    private static final String PROPERTY_NOTIFICATION_LISTENER_CHECK_INTERVAL_MILLIS =
            "notification_listener_check_interval_millis";

    private static final Long OVERRIDE_NOTIFICATION_LISTENER_CHECK_INTERVAL_MILLIS =
            SECONDS.toMillis(1);

    private static final String PROPERTY_JOB_SCHEDULER_MAX_JOB_PER_RATE_LIMIT_WINDOW =
            "qc_max_job_count_per_rate_limiting_window";

    private static final String PROPERTY_JOB_SCHEDULER_RATE_LIMIT_WINDOW_MILLIS =
            "qc_rate_limiting_window_ms";

    private static final String ACTION_SET_UP_NOTIFICATION_LISTENER_CHECK =
            "com.android.permissioncontroller.action.SET_UP_NOTIFICATION_LISTENER_CHECK";
    private static final String NotificationListenerOnBootReceiver =
            "com.android.permissioncontroller.privacysources.SetupPeriodicNotificationListenerCheck";

    /**
     * ID for notification shown by
     * {@link com.android.permissioncontroller.privacysources.NotificationListenerCheck}.
     */
    public static final int NOTIFICATION_LISTENER_CHECK_NOTIFICATION_ID = 3;

    protected static final long UNEXPECTED_TIMEOUT_MILLIS = 10000;
    protected static final long ENSURE_NOTIFICATION_NOT_SHOWN_EXPECTED_TIMEOUT_MILLIS = 5000;

    private static final Context sContext = InstrumentationRegistry.getTargetContext();
    private static final PackageManager sPackageManager = sContext.getPackageManager();
    private static final UiAutomation sUiAutomation = InstrumentationRegistry.getInstrumentation()
            .getUiAutomation();

    private static final String PERMISSION_CONTROLLER_PKG = sContext.getPackageManager()
            .getPermissionControllerPackageName();

    private static List<ComponentName> sPreviouslyEnabledNotificationListeners;

    // Override SafetyCenter enabled flag
    @Rule
    public DeviceConfigStateChangerRule sPrivacyDeviceConfigSafetyCenterEnabled =
            new DeviceConfigStateChangerRule(sContext,
                    DeviceConfig.NAMESPACE_PRIVACY,
                    SafetyCenterUtils.PROPERTY_SAFETY_CENTER_ENABLED,
                    Boolean.toString(true));

    // Override NlsCheck enabled flag
    @Rule
    public DeviceConfigStateChangerRule sPrivacyDeviceConfigNlsCheckEnabled =
            new DeviceConfigStateChangerRule(sContext,
                    DeviceConfig.NAMESPACE_PRIVACY,
                    PROPERTY_NOTIFICATION_LISTENER_CHECK_ENABLED,
                    Boolean.toString(true));

    // Override general notification interval from once every day to once ever 1 second
    @Rule
    public DeviceConfigStateChangerRule sPrivacyDeviceConfigNlsCheckIntervalMillis =
            new DeviceConfigStateChangerRule(sContext,
                    DeviceConfig.NAMESPACE_PRIVACY,
                    PROPERTY_NOTIFICATION_LISTENER_CHECK_INTERVAL_MILLIS,
                    Long.toString(OVERRIDE_NOTIFICATION_LISTENER_CHECK_INTERVAL_MILLIS));

    // Disable job scheduler throttling by allowing 300000 jobs per 30 sec
    @Rule
    public DeviceConfigStateChangerRule sJobSchedulerDeviceConfig1 =
            new DeviceConfigStateChangerRule(sContext,
                    DeviceConfig.NAMESPACE_JOB_SCHEDULER,
                    PROPERTY_JOB_SCHEDULER_MAX_JOB_PER_RATE_LIMIT_WINDOW,
                    Integer.toString(3000000));

    // Disable job scheduler throttling by allowing 300000 jobs per 30 sec
    @Rule
    public DeviceConfigStateChangerRule sJobSchedulerDeviceConfig2 =
            new DeviceConfigStateChangerRule(sContext,
                    DeviceConfig.NAMESPACE_JOB_SCHEDULER,
                    PROPERTY_JOB_SCHEDULER_RATE_LIMIT_WINDOW_MILLIS,
                    Integer.toString(30000));

    @Rule
    public CtsNotificationListenerHelperRule ctsNotificationListenerHelper =
            new CtsNotificationListenerHelperRule(sContext);

    @BeforeClass
    public static void beforeClassSetup() throws Exception {
        // Disallow any OEM enabled NLS
        disallowPreexistingNotificationListeners();
    }

    @AfterClass
    public static void afterClassTearDown() throws Throwable {
        // Reallow any previously OEM allowed NLS
        reallowPreexistingNotificationListeners();
    }

    private static void setDeviceConfigPrivacyProperty(String propertyName, String value) {
        runWithShellPermissionIdentity(() -> {
            boolean valueWasSet =  DeviceConfig.setProperty(
                    DeviceConfig.NAMESPACE_PRIVACY,
                    /* name = */ propertyName,
                    /* value = */ value,
                    /* makeDefault = */ false);
            if (!valueWasSet) {
                throw new  IllegalStateException("Could not set " + propertyName + " to " + value);
            }
        }, WRITE_DEVICE_CONFIG);
    }

    /**
     * Enable or disable notification listener check
     */
    protected static void setNotificationListenerCheckEnabled(boolean enabled) {
        setDeviceConfigPrivacyProperty(
                PROPERTY_NOTIFICATION_LISTENER_CHECK_ENABLED,
                /* value = */ String.valueOf(enabled));
    }

    /**
     * Allow or disallow a {@link NotificationListenerService} component for the current user
     *
     * @param listenerComponent {@link NotificationListenerService} component to allow or disallow
     */
    private static void setNotificationListenerServiceAllowed(ComponentName listenerComponent,
            boolean allowed) {
        String command = " cmd notification " + (allowed ? "allow_listener " : "disallow_listener ")
                + listenerComponent.flattenToString();
        runShellCommand(command);
    }

    private static void disallowPreexistingNotificationListeners() {
        runWithShellPermissionIdentity(() -> {
            NotificationManager notificationManager =
                    sContext.getSystemService(NotificationManager.class);
            sPreviouslyEnabledNotificationListeners =
                    notificationManager.getEnabledNotificationListeners();
        });
        if (DEBUG) {
            Log.d(LOG_TAG, "Found " + sPreviouslyEnabledNotificationListeners.size()
                    + " previously allowed notification listeners. Disabling before test run.");
        }
        for (ComponentName listener : sPreviouslyEnabledNotificationListeners) {
            setNotificationListenerServiceAllowed(listener, false);
        }
    }

    private static void reallowPreexistingNotificationListeners() {
        if (DEBUG) {
            Log.d(LOG_TAG, "Re-allowing " + sPreviouslyEnabledNotificationListeners.size()
                    + " previously allowed notification listeners found before test run.");
        }
        for (ComponentName listener : sPreviouslyEnabledNotificationListeners) {
            setNotificationListenerServiceAllowed(listener, true);
        }
    }

    protected void allowTestAppNotificationListenerService() {
        setNotificationListenerServiceAllowed(
                new ComponentName(TEST_APP_PKG, TEST_APP_NOTIFICATION_SERVICE), true);
    }

    protected void disallowTestAppNotificationListenerService() {
        setNotificationListenerServiceAllowed(
                new ComponentName(TEST_APP_PKG, TEST_APP_NOTIFICATION_SERVICE), false);
    }

    /**
     * Get the state of the job scheduler
     */
    private static JobSchedulerServiceDumpProto getJobSchedulerDump() throws Exception {
        return ProtoUtils.getProto(sUiAutomation, JobSchedulerServiceDumpProto.class,
                ProtoUtils.DUMPSYS_JOB_SCHEDULER);
    }

    /**
     * Get the last time the NOTIFICATION_LISTENER_CHECK_JOB_ID job was started/stopped for
     * permission
     * controller.
     *
     * @param event the job event (start/stop)
     * @return the last time the event happened.
     */
    private static long getLastJobTime(int event) throws Exception {
        int permControllerUid = sPackageManager.getPackageUid(PERMISSION_CONTROLLER_PKG, 0);

        long lastTime = -1;

        for (JobPackageHistoryProto.HistoryEvent historyEvent :
                getJobSchedulerDump().history.historyEvent) {
            if (historyEvent.uid == permControllerUid
                    && historyEvent.jobId == NOTIFICATION_LISTENER_CHECK_JOB_ID
                    && historyEvent.event == event) {
                lastTime = max(lastTime,
                        System.currentTimeMillis() - historyEvent.timeSinceEventMs);
            }
        }

        return lastTime;
    }

    /**
     * Force a run of the notification listener check.
     */
    protected static void runNotificationListenerCheck() throws Throwable {
        // Sleep a little to make sure we don't have overlap in timing
        Thread.sleep(1000);

        long beforeJob = System.currentTimeMillis();

        // Sleep a little to avoid raciness in time keeping
        Thread.sleep(1000);

        runShellCommand("cmd jobscheduler run -u " + myUserHandle().getIdentifier() + " -f "
                + PERMISSION_CONTROLLER_PKG + " " + NOTIFICATION_LISTENER_CHECK_JOB_ID);

        eventually(() -> {
            long startTime = getLastJobTime(START_PERIODIC_JOB);
            assertTrue(startTime + " !> " + beforeJob, startTime > beforeJob);
        }, UNEXPECTED_TIMEOUT_MILLIS);

        // We can't simply require startTime <= endTime because the time being reported isn't
        // accurate, and sometimes the end time may come before the start time by around 100 ms.
        eventually(() -> {
            long stopTime = getLastJobTime(STOP_PERIODIC_JOB);
            assertTrue(stopTime + " !> " + beforeJob, stopTime > beforeJob);
        }, UNEXPECTED_TIMEOUT_MILLIS);
    }

    /**
     * Skip tests for if Safety Center not supported
     */
    protected void assumeDeviceSupportsSafetyCenter() {
        assumeTrue(SafetyCenterUtils.deviceSupportsSafetyCenter(sContext));
    }

    /**
     * Skip tests for if Safety Center IS supported
     */
    protected void assumeDeviceDoesNotSupportSafetyCenter() {
        assumeFalse(SafetyCenterUtils.deviceSupportsSafetyCenter(sContext));
    }

    protected void wakeUpAndDismissKeyguard() {
        runShellCommand("input keyevent KEYCODE_WAKEUP");
        runShellCommand("wm dismiss-keyguard");
    }

    /**
     * Reset the permission controllers state before each test
     */
    protected void resetPermissionControllerBeforeEachTest() throws Throwable {
        resetPermissionController();

        // ensure no posted notification listener notifications exits
        eventually(() -> assertNull(getNotification(false)), UNEXPECTED_TIMEOUT_MILLIS);

        // Reset job scheduler stats (to allow more jobs to be run)
        runShellCommand(
                "cmd jobscheduler reset-execution-quota -u " + myUserHandle().getIdentifier() + " "
                        + PERMISSION_CONTROLLER_PKG);
    }

    /**
     * Reset the permission controllers state.
     */
    private static void resetPermissionController() throws Throwable {
        clearAppState(PERMISSION_CONTROLLER_PKG);
        int currentUserId = myUserHandle().getIdentifier();

        // Wait until jobs are cleared
        eventually(() -> {
            JobSchedulerServiceDumpProto dump = getJobSchedulerDump();

            for (RegisteredJob job : dump.registeredJobs) {
                if (job.dump.sourceUserId == currentUserId) {
                    assertNotEquals(job.dump.sourcePackageName, PERMISSION_CONTROLLER_PKG);
                }
            }
        }, UNEXPECTED_TIMEOUT_MILLIS);

        // Setup up permission controller again (simulate a reboot)
        Intent permissionControllerSetupIntent = new Intent(
                ACTION_SET_UP_NOTIFICATION_LISTENER_CHECK).setPackage(
                PERMISSION_CONTROLLER_PKG).setFlags(FLAG_RECEIVER_FOREGROUND);

        // Query for the setup broadcast receiver
        List<ResolveInfo> resolveInfos = sContext.getPackageManager().queryBroadcastReceivers(
                permissionControllerSetupIntent, 0);

        if (resolveInfos.size() > 0) {
            sContext.sendBroadcast(permissionControllerSetupIntent);
        } else {
            sContext.sendBroadcast(new Intent()
                    .setClassName(PERMISSION_CONTROLLER_PKG, NotificationListenerOnBootReceiver)
                    .setFlags(FLAG_RECEIVER_FOREGROUND)
                    .setPackage(PERMISSION_CONTROLLER_PKG));
        }

        // Wait until jobs are set up
        eventually(() -> {
            JobSchedulerServiceDumpProto dump = getJobSchedulerDump();

            for (RegisteredJob job : dump.registeredJobs) {
                if (job.dump.sourceUserId == currentUserId
                        && job.dump.sourcePackageName.equals(PERMISSION_CONTROLLER_PKG)
                        && job.dump.jobInfo.service.className.contains(
                        "NotificationListenerCheck")) {
                    return;
                }
            }

            fail("Permission controller jobs not found");
        }, UNEXPECTED_TIMEOUT_MILLIS);
    }

    /**
     * Preshow/dismiss cts NotificationListener notification as it negatively affects test results
     * (can result in unexpected test pass/failures)
     */
    protected void triggerAndDismissCtsNotificationListenerNotification() throws Throwable {
        // CtsNotificationListenerService isn't enabled at this point, but NotificationListener
        // should be. Mark as notified by showing and dismissing
        runNotificationListenerCheck();

        // Ensure notification shows and dismiss
        eventually(() -> assertNotNull(getNotification(true)),
                UNEXPECTED_TIMEOUT_MILLIS);
    }

    /**
     * Get a notification listener notification that is currently visible.
     *
     * @param cancelNotification if `true` the notification is canceled inside this method
     * @return The notification or `null` if there is none
     */
    protected StatusBarNotification getNotification(boolean cancelNotification) throws Throwable {
        return NotificationUtils.getNotificationForPackageAndId(
                PERMISSION_CONTROLLER_PKG,
                NOTIFICATION_LISTENER_CHECK_NOTIFICATION_ID,
                cancelNotification);
    }

    /**
     * Clear any notifications related to NotificationListenerCheck to ensure clean test setup
     */
    protected void clearNotifications() throws Throwable {
        // Clear notification if present
        NotificationUtils.clearNotificationsForPackage(PERMISSION_CONTROLLER_PKG);
    }
}