1 /* 2 * Copyright (C) 2016 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; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.device.NativeDevice; 22 import com.android.tradefed.testtype.DeviceTestCase; 23 import com.android.tradefed.log.LogUtil.CLog; 24 25 import java.util.regex.Pattern; 26 27 public class SecurityTestCase extends DeviceTestCase { 28 29 private long kernelStartTime; 30 31 /** 32 * Waits for device to be online, marks the most recent boottime of the device 33 */ 34 @Override 35 public void setUp() throws Exception { 36 super.setUp(); 37 38 String uptime = getDevice().executeShellCommand("cat /proc/uptime"); 39 kernelStartTime = System.currentTimeMillis()/1000 - 40 Integer.parseInt(uptime.substring(0, uptime.indexOf('.'))); 41 //TODO:(badash@): Watch for other things to track. 42 // Specifically time when app framework starts 43 } 44 45 /** 46 * Allows a CTS test to pass if called after a planned reboot. 47 */ 48 public void updateKernelStartTime() throws Exception { 49 String uptime = getDevice().executeShellCommand("cat /proc/uptime"); 50 kernelStartTime = System.currentTimeMillis()/1000 - 51 Integer.parseInt(uptime.substring(0, uptime.indexOf('.'))); 52 } 53 54 /** 55 * Use {@link NativeDevice#enableAdbRoot()} internally. 56 * 57 * The test methods calling this function should run even if enableAdbRoot fails, which is why 58 * the return value is ignored. However, we may want to act on that data point in the future. 59 */ 60 public boolean enableAdbRoot(ITestDevice mDevice) throws DeviceNotAvailableException { 61 if(mDevice.enableAdbRoot()) { 62 return true; 63 } else { 64 CLog.w("\"enable-root\" set to false! Root is required to check if device is vulnerable."); 65 return false; 66 } 67 } 68 69 /** 70 * Check if a driver is present on a machine 71 */ 72 public boolean containsDriver(ITestDevice mDevice, String driver) throws Exception { 73 String result = mDevice.executeShellCommand("ls -Zl " + driver); 74 if(result.contains("No such file or directory")) { 75 return false; 76 } 77 return true; 78 } 79 80 /** 81 * Makes sure the phone is online, and the ensure the current boottime is within 2 seconds 82 * (due to rounding) of the previous boottime to check if The phone has crashed. 83 */ 84 @Override 85 public void tearDown() throws Exception { 86 getDevice().waitForDeviceAvailable(120 * 1000); 87 String uptime = getDevice().executeShellCommand("cat /proc/uptime"); 88 assertTrue("Phone has had a hard reset", 89 (System.currentTimeMillis()/1000 - 90 Integer.parseInt(uptime.substring(0, uptime.indexOf('.'))) 91 - kernelStartTime < 2)); 92 //TODO(badash@): add ability to catch runtime restart 93 getDevice().disableAdbRoot(); 94 } 95 96 public void assertMatches(String pattern, String input) throws Exception { 97 assertTrue("Pattern not found", Pattern.matches(pattern, input)); 98 } 99 100 public void assertNotMatches(String pattern, String input) throws Exception { 101 assertFalse("Pattern found", Pattern.matches(pattern, input)); 102 } 103 104 public void assertNotMatchesMultiLine(String pattern, String input) throws Exception { 105 assertFalse("Pattern found", 106 Pattern.compile(pattern, 107 Pattern.DOTALL).matcher(input).matches()); 108 } 109 } 110