blob: e0fc3370936005c512f883462833bea995205e06 [file] [log] [blame]
/*
* 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_0523;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
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 java.io.IOException;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
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.assertNull;
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
private static final String TEST_PKG = "android.security.cts.cve_2021_0523";
private static final String TEST_PKG_WIFI = "com.android.settings";
private static final int LAUNCH_TIMEOUT_MS = 20000;
private UiDevice mDevice;
String activityDump = "";
private void startOverlayService() {
Context context = getApplicationContext();
if (Settings.canDrawOverlays(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), PocService.class);
context.startService(intent);
} else {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@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);
/* Wait for the WifiScanModeActivity */
if (!mDevice.wait(Until.hasObject(By.pkg(TEST_PKG_WIFI).depth(0)), LAUNCH_TIMEOUT_MS)) {
return;
}
/* Start the overlay service */
startOverlayService();
}
@Test
public void testOverlayButtonPresence() {
Pattern pattern = Pattern.compile(
getApplicationContext().getResources().getString(R.string.overlay_button),
Pattern.CASE_INSENSITIVE);
BySelector selector = By.text(pattern);
/* Wait for an object of the overlay window */
if (!mDevice.wait(Until.hasObject(selector.depth(0)), LAUNCH_TIMEOUT_MS)) {
return;
}
/* Check if the currently running activity is WifiScanModeActivity */
try {
activityDump = mDevice.executeShellCommand("dumpsys activity");
} catch (IOException e) {
throw new RuntimeException("Could not execute dumpsys activity command");
}
Pattern activityPattern = Pattern.compile("mResumedActivity.*WifiScanModeActivity.*\n");
if (!activityPattern.matcher(activityDump).find()) {
return;
}
String message = "Device is vulnerable to b/174047492 hence any app with "
+ "SYSTEM_ALERT_WINDOW can overlay the WifiScanModeActivity screen";
assertNull(message, mDevice.findObject(selector));
}
}