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_2021_39701; 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.assertEquals; 23 import static org.junit.Assume.assumeNoException; 24 import static org.junit.Assume.assumeNotNull; 25 import static org.junit.Assume.assumeTrue; 26 27 import android.app.UiAutomation; 28 import android.content.ActivityNotFoundException; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.SharedPreferences; 32 import android.content.pm.PackageManager; 33 import android.content.res.Resources; 34 import android.os.RemoteException; 35 import android.view.KeyEvent; 36 37 import androidx.test.runner.AndroidJUnit4; 38 import androidx.test.uiautomator.By; 39 import androidx.test.uiautomator.BySelector; 40 import androidx.test.uiautomator.UiDevice; 41 import androidx.test.uiautomator.UiObject2; 42 import androidx.test.uiautomator.Until; 43 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 47 import java.util.regex.Pattern; 48 49 @RunWith(AndroidJUnit4.class) 50 public class DeviceTest { 51 52 @Test testService()53 public void testService() { 54 final int timeoutMs = 10000; 55 boolean buttonClicked = false; 56 UiAutomation uiAutomation = getInstrumentation().getUiAutomation(); 57 UiDevice device = UiDevice.getInstance(getInstrumentation()); 58 Context context = getApplicationContext(); 59 Resources resources = context.getResources(); 60 assumeNotNull(context); 61 PackageManager packageManager = context.getPackageManager(); 62 assumeNotNull(packageManager); 63 assumeTrue("Controls not supported on this device", 64 packageManager.hasSystemFeature(PackageManager.FEATURE_CONTROLS)); 65 Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName()); 66 assumeNotNull(intent); 67 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 68 try { 69 context.startActivity(intent); 70 } catch (ActivityNotFoundException e) { 71 assumeNoException(e); 72 } 73 String applicationName = resources.getString(R.string.systemUiPackage); 74 assumeTrue(resources.getString(R.string.failMessage) + applicationName, 75 device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs)); 76 77 // Click on Add Button 78 buttonClicked = clickButton(resources.getString(R.string.addButton)); 79 assumeTrue(resources.getString(R.string.addBtnNotFound), buttonClicked); 80 try { 81 if (packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)) { 82 device.pressKeyCode(KeyEvent.KEYCODE_SLEEP); 83 } else { 84 device.sleep(); 85 } 86 } catch (RemoteException e) { 87 assumeNoException(e); 88 } 89 uiAutomation.executeShellCommand(resources.getString(R.string.wakeup)); 90 assumeTrue(resources.getString(R.string.failMessage) + applicationName, 91 device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs)); 92 93 // Click on the icon on locked screen 94 buttonClicked = clickButton(resources.getString(R.string.controlButton)); 95 assumeTrue(resources.getString(R.string.iconNotFound), buttonClicked); 96 assumeTrue(device.pressKeyCode(KeyEvent.KEYCODE_MENU)); 97 assumeTrue(resources.getString(R.string.failMessage) + applicationName, 98 device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs)); 99 100 int result = resources.getInteger(R.integer.pocServNotStart); 101 String message = ""; 102 long startTime = System.currentTimeMillis(); 103 while ((System.currentTimeMillis() - startTime) < timeoutMs) { 104 SharedPreferences sharedpref = getApplicationContext().getSharedPreferences( 105 resources.getString(R.string.pocSharedPref), Context.MODE_APPEND); 106 result = sharedpref.getInt(resources.getString(R.string.flag), 107 resources.getInteger(R.integer.pocServNotStart)); 108 message = sharedpref.getString(resources.getString(R.string.message), ""); 109 if (result < resources.getInteger(R.integer.pocServNotStart)) { 110 break; 111 } 112 } 113 assumeTrue(device.pressHome()); 114 assumeTrue(resources.getString(R.string.pocSerNotStarted), 115 result != resources.getInteger(R.integer.pocServNotStart)); 116 assertEquals(message, resources.getInteger(R.integer.pass), result); 117 } 118 clickButton(String id)119 private boolean clickButton(String id) { 120 UiDevice device = UiDevice.getInstance(getInstrumentation()); 121 Pattern idPattern = Pattern.compile(Pattern.quote(id), Pattern.CASE_INSENSITIVE); 122 BySelector selector = By.clickable(true).res(idPattern); 123 UiObject2 object = device.findObject(selector); 124 if (object != null) { 125 object.click(); 126 return true; 127 } 128 return false; 129 } 130 } 131