summaryrefslogtreecommitdiff
path: root/hostsidetests/securitybulletin/test-apps/CVE-2021-0586/src/android/security/cts/CVE_2021_0586/DeviceTest.java
blob: 3ffb7df9664aa9aaf5e54d4a7935ff14dd4b2ca7 (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
/*
 * Copyright (C) 2021 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.security.cts.cve_2021_0586;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.Until;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.regex.Pattern;

@RunWith(AndroidJUnit4.class)
public class DeviceTest {
    private static final String TEST_PKG = "android.security.cts.cve_2021_0586";
    private static final int LAUNCH_TIMEOUT_MS = 20000;
    private Pattern overlayTextPattern;
    private UiDevice mDevice;
    String activityDump = "";

    public void startDevicePickerActivity() {
        Context context = getApplicationContext();
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        assertNotNull(sharingIntent);
        sharingIntent.setType("image/*");
        sharingIntent.setPackage("com.android.bluetooth");
        Uri uri = Uri.parse("android.resource://android.security.cts.CVE_2021_0586"
                + "/drawable/cve_2021_0586.png");
        assertNotNull(uri);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        Intent intent = Intent.createChooser(sharingIntent, "Share image");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Before
    public void startMainActivityFromHomeScreen() {
        mDevice = UiDevice.getInstance(getInstrumentation());
        Context context = getApplicationContext();
        assertNotNull(context);
        PackageManager packageManager = context.getPackageManager();
        assertNotNull(packageManager);
        final Intent intent = packageManager.getLaunchIntentForPackage(TEST_PKG);
        assertNotNull(intent);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        /* Start the launcher activity */
        context.startActivity(intent);
        overlayTextPattern = Pattern.compile(
                getApplicationContext().getResources().getString(R.string.overlay_button),
                Pattern.CASE_INSENSITIVE);
    }

    @Test
    public void testOverlayButtonPresence() {
        /* Wait for the overlay window */
        if (!mDevice.wait(Until.hasObject(By.text(overlayTextPattern)), LAUNCH_TIMEOUT_MS)) {
            return;
        }

        /* Start the DevicePickerActivity */
        startDevicePickerActivity();

        /* Wait until the object of launcher activity is gone */
        if (mDevice.wait(Until.gone(By.pkg(TEST_PKG)), LAUNCH_TIMEOUT_MS)) {
            return;
        }

        /* Check if the currently running activity is DevicePickerActivity */
        try {
            activityDump = mDevice.executeShellCommand("dumpsys activity");
        } catch (IOException e) {
            throw new RuntimeException("Could not execute dumpsys activity command");
        }
        Pattern activityPattern = Pattern.compile("mResumedActivity.*DevicePickerActivity.*\n");
        if (!activityPattern.matcher(activityDump).find()) {
            return;
        }

        /* Failing the test as fix is not present */
        String message = "Device is vulnerable to b/182584940 hence any app with "
                + "SYSTEM_ALERT_WINDOW can overlay the Bluetooth DevicePickerActivity screen";
        fail(message);
    }
}