blob: b5083e9671fcc4465dcbd40ee329548713bb7207 [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_2022_20349;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeTrue;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.UserManager;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.regex.Pattern;
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
Context mContext;
UiDevice mDevice;
DevicePolicyManager mDevicePolicyManager;
ComponentName mComponentName;
static final String USER_RESTRICTION = UserManager.DISALLOW_CONFIG_LOCATION;
static final int UI_TIMEOUT_MS = 5000;
String getStringRes(int key) {
return mContext.getResources().getString(key);
}
int getIntegerRes(int key) {
return mContext.getResources().getInteger(key);
}
@After
public void tearDown() {
try {
/* Return to home screen after test */
mDevice.pressHome();
/*
* Clear user restriction "DISALLOW_CONFIG_LOCATION" set by the test and also clear the
* app as device owner.
*/
mDevicePolicyManager.clearUserRestriction(mComponentName, USER_RESTRICTION);
mDevicePolicyManager.clearDeviceOwnerApp(mContext.getPackageName());
} catch (Exception e) {
// ignore the exception as the test is already complete
}
}
@Test
public void testBluetoothScanningDisallowed() {
try {
mDevice = UiDevice.getInstance(getInstrumentation());
mContext = getApplicationContext();
mDevicePolicyManager =
mContext.getSystemService(DevicePolicyManager.class);
mComponentName =
new ComponentName(PocDeviceAdminReceiver.class.getPackage().getName(),
PocDeviceAdminReceiver.class.getName());
mDevicePolicyManager.addUserRestriction(mComponentName, USER_RESTRICTION);
UserManager userManager = mContext.getSystemService(UserManager.class);
assumeTrue(getStringRes(R.string.setUserRestrictionFailed),
userManager.getUserRestrictions().getBoolean(USER_RESTRICTION));
/* Start the window that contains option to toggle "Bluetooth scanning" on/off */
Intent intent = new Intent(getStringRes(R.string.locationIntentAction));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
/* Wait for the window that contains option to toggle "Bluetooth scanning" */
Pattern btScanningPattern = Pattern.compile(getStringRes(R.string.btScanningPattern),
Pattern.CASE_INSENSITIVE);
boolean btScanningFound = mDevice.wait(
Until.hasObject(
By.text(btScanningPattern).res(getStringRes(R.string.resBtScanning))),
UI_TIMEOUT_MS);
assumeTrue(getStringRes(R.string.btScanningTimedOut), btScanningFound);
/*
* Check if the toggle "Bluetooth scanning" is enabled, it is supposed to be disabled by
* the Device Admin in presence of fix
*/
UiObject2 btScanningToggle = mDevice.findObject(
By.text(btScanningPattern).res(getStringRes(R.string.resBtScanning)));
assertFalse(getStringRes(R.string.failMsg), btScanningToggle.isEnabled());
} catch (Exception e) {
assumeNoException(e);
}
}
}