1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.security.cts.CVE_2022_20349;
18 
19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
21 
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assume.assumeNoException;
24 import static org.junit.Assume.assumeTrue;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.UserManager;
31 
32 import androidx.test.runner.AndroidJUnit4;
33 import androidx.test.uiautomator.By;
34 import androidx.test.uiautomator.UiDevice;
35 import androidx.test.uiautomator.UiObject2;
36 import androidx.test.uiautomator.Until;
37 
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 import java.util.regex.Pattern;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class DeviceTest {
46     Context mContext;
47     UiDevice mDevice;
48     DevicePolicyManager mDevicePolicyManager;
49     ComponentName mComponentName;
50     static final String USER_RESTRICTION = UserManager.DISALLOW_CONFIG_LOCATION;
51     static final int UI_TIMEOUT_MS = 5000;
52 
getStringRes(int key)53     String getStringRes(int key) {
54         return mContext.getResources().getString(key);
55     }
56 
getIntegerRes(int key)57     int getIntegerRes(int key) {
58         return mContext.getResources().getInteger(key);
59     }
60 
61     @After
tearDown()62     public void tearDown() {
63         try {
64             /* Return to home screen after test */
65             mDevice.pressHome();
66 
67             /*
68              * Clear user restriction "DISALLOW_CONFIG_LOCATION" set by the test and also clear the
69              * app as device owner.
70              */
71             mDevicePolicyManager.clearUserRestriction(mComponentName, USER_RESTRICTION);
72             mDevicePolicyManager.clearDeviceOwnerApp(mContext.getPackageName());
73         } catch (Exception e) {
74             // ignore the exception as the test is already complete
75         }
76     }
77 
78     @Test
testBluetoothScanningDisallowed()79     public void testBluetoothScanningDisallowed() {
80         try {
81             mDevice = UiDevice.getInstance(getInstrumentation());
82             mContext = getApplicationContext();
83             mDevicePolicyManager =
84                     mContext.getSystemService(DevicePolicyManager.class);
85             mComponentName =
86                     new ComponentName(PocDeviceAdminReceiver.class.getPackage().getName(),
87                             PocDeviceAdminReceiver.class.getName());
88             mDevicePolicyManager.addUserRestriction(mComponentName, USER_RESTRICTION);
89             UserManager userManager = mContext.getSystemService(UserManager.class);
90             assumeTrue(getStringRes(R.string.setUserRestrictionFailed),
91                     userManager.getUserRestrictions().getBoolean(USER_RESTRICTION));
92 
93             /* Start the window that contains option to toggle "Bluetooth scanning" on/off */
94             Intent intent = new Intent(getStringRes(R.string.locationIntentAction));
95             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
96             mContext.startActivity(intent);
97 
98             /* Wait for the window that contains option to toggle "Bluetooth scanning" */
99             Pattern btScanningPattern = Pattern.compile(getStringRes(R.string.btScanningPattern),
100                     Pattern.CASE_INSENSITIVE);
101             boolean btScanningFound = mDevice.wait(
102                     Until.hasObject(
103                             By.text(btScanningPattern).res(getStringRes(R.string.resBtScanning))),
104                     UI_TIMEOUT_MS);
105             assumeTrue(getStringRes(R.string.btScanningTimedOut), btScanningFound);
106 
107             /*
108              * Check if the toggle "Bluetooth scanning" is enabled, it is supposed to be disabled by
109              * the Device Admin in presence of fix
110              */
111             UiObject2 btScanningToggle = mDevice.findObject(
112                     By.text(btScanningPattern).res(getStringRes(R.string.resBtScanning)));
113             assertFalse(getStringRes(R.string.failMsg), btScanningToggle.isEnabled());
114         } catch (Exception e) {
115             assumeNoException(e);
116         }
117     }
118 }
119