| /* |
| * 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); |
| } |
| } |