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_20348;
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
testWifiScanningDisallowed()79     public void testWifiScanningDisallowed() {
80         try {
81             mDevice = UiDevice.getInstance(getInstrumentation());
82             mContext = getApplicationContext();
83             mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
84             mComponentName = new ComponentName(PocDeviceAdminReceiver.class.getPackage().getName(),
85                     PocDeviceAdminReceiver.class.getName());
86             mDevicePolicyManager.addUserRestriction(mComponentName, USER_RESTRICTION);
87             UserManager userManager = mContext.getSystemService(UserManager.class);
88             assumeTrue(getStringRes(R.string.setUserRestrictionFailed),
89                     userManager.getUserRestrictions().getBoolean(USER_RESTRICTION));
90 
91             /* Start the window that contains option to toggle "Wi-Fi scanning" on/off */
92             Intent intent = new Intent(getStringRes(R.string.locationIntentAction));
93             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
94             mContext.startActivity(intent);
95 
96             /* Wait for the window that contains option to toggle "Wi-Fi scanning" */
97             Pattern wifiScanningPattern = Pattern
98                     .compile(getStringRes(R.string.wifiScanningPattern), Pattern.CASE_INSENSITIVE);
99             boolean wifiScanningFound = mDevice.wait(Until.hasObject(
100                     By.text(wifiScanningPattern).res(getStringRes(R.string.resWifiScanning))),
101                     UI_TIMEOUT_MS);
102             assumeTrue(getStringRes(R.string.wifiScanningTimedOut), wifiScanningFound);
103 
104             /*
105              * Check if the toggle "Wi-Fi scanning" is enabled, it is supposed to be disabled by
106              * the Device Admin in presence of fix
107              */
108             UiObject2 wifiScanningToggle = mDevice.findObject(
109                     By.text(wifiScanningPattern).res(getStringRes(R.string.resWifiScanning)));
110             assertFalse(getStringRes(R.string.failMsg), wifiScanningToggle.isEnabled());
111         } catch (Exception e) {
112             assumeNoException(e);
113         }
114     }
115 }
116