blob: 9a94ef953c10266fe5e634d812a70fcfaf76ebac [file] [log] [blame]
/*
* 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.security.cts.CVE_2021_0954;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeTrue;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.provider.Settings;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.Until;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
private Context mContext = getApplicationContext();
private static final int TIMEOUT_MS = 10000;
private boolean hasFeature(String feature) {
return mContext.getPackageManager().hasSystemFeature(feature);
}
private boolean isAuto() {
return hasFeature(PackageManager.FEATURE_AUTOMOTIVE);
}
String getStringRes(int key) {
return mContext.getResources().getString(key);
}
String getStringResWithArg(int key, String arg) {
return mContext.getResources().getString(key, arg);
}
int getIntegerRes(int key) {
return mContext.getResources().getInteger(key);
}
@Test
public void testOverlayButtonPresence() {
try {
UiDevice device = UiDevice.getInstance(getInstrumentation());
/* Start the overlay service */
assumeTrue(getStringRes(R.string.canNotDrawOverlaysMsg),
Settings.canDrawOverlays(mContext));
Intent intent = new Intent(mContext, PocService.class);
mContext.startService(intent);
/* Wait for a result from overlay service */
SharedPreferences sharedPrefs = mContext.getSharedPreferences(
getStringRes(R.string.sharedPreferences), Context.MODE_PRIVATE);
final Semaphore preferenceChanged = new Semaphore(0);
OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(getStringRes(R.string.resultKey))) {
preferenceChanged.release();
}
}
};
sharedPrefs.registerOnSharedPreferenceChangeListener(listener);
assumeTrue(preferenceChanged.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
int result = sharedPrefs.getInt(getStringRes(R.string.resultKey),
getIntegerRes(R.integer.assumptionFailure));
String message = sharedPrefs.getString(getStringRes(R.string.messageKey),
getStringRes(R.string.defaultSemaphoreMsg));
assumeTrue(message, result != getIntegerRes(R.integer.assumptionFailure));
/* Wait for the UI of overlay window to appear */
Pattern overlayTextPattern = Pattern.compile(
mContext.getString(R.string.overlayButtonText), Pattern.CASE_INSENSITIVE);
assumeTrue(mContext.getString(R.string.overlayUiScreenError),
device.wait(Until.hasObject(By.text(overlayTextPattern)), TIMEOUT_MS));
/* Start the vulnerable activity */
intent = new Intent();
String vulActivity = getStringRes(R.string.vulClass);
String vulPkg = getStringRes(R.string.vulPkg);
if (isAuto()) {
vulActivity = getStringRes(R.string.vulClassAuto);
vulPkg = getStringRes(R.string.vulPkgAuto);
}
intent.setClassName(vulPkg, vulActivity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
/* Wait until the object of overlay window is gone */
boolean overlayDisallowed =
device.wait(Until.gone(By.pkg(mContext.getPackageName())), TIMEOUT_MS);
/*
* Check if the currently running activity is the vulnerable activity, if not abort the
* test
*/
String activityDump = device.executeShellCommand(
getStringResWithArg(R.string.cmdDumpsysActivity, vulActivity));
Pattern activityPattern = Pattern.compile(getStringRes(R.string.mResumedTrue));
assumeTrue(getStringRes(R.string.vulActivityNotRunningError),
activityPattern.matcher(activityDump).find());
/* Failing the test as fix is not present */
assertTrue(getStringResWithArg(R.string.overlayErrorMessage, vulActivity),
overlayDisallowed);
} catch (Exception e) {
assumeNoException(e);
}
}
}