1 /* 2 * Copyright (C) 2014 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 com.android.encryption.tests; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.testtype.IDeviceTest; 23 import com.android.tradefed.testtype.IRemoteTest; 24 25 import org.junit.Assert; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 /** 31 * Runs the encryption func tests. 32 * <p> 33 * The steps and the stage number: 34 * 1 Encrypts the device inplace. 0->1->2 35 * 2 Checks the password and boot into the system 2->3->4 36 * 3 Changes the password 4->5 37 * 4 Checks if can still access sdcard(Should not) 5 38 * 5 Checks the password and boot into the system 5->6 39 * 6 Change the password to DEFAULT(no password),make sure it boots into the system directly 6->7 40 * The time of every stage is measured. 41 * </p> 42 */ 43 public class EncryptionFunctionalityTest implements IDeviceTest, IRemoteTest { 44 45 private static final int BOOT_TIMEOUT = 2 * 60 * 1000; 46 47 ITestDevice mTestDevice = null; 48 49 static final String[] STAGE_NAME = { 50 "encryption", "online", "bootcomplete", "decryption", 51 "bootcomplete", "pwchanged", "bootcomplete", "nopassword" 52 }; 53 Map<String, String> metrics = new HashMap<String, String>(); 54 int stage = 0; 55 long stageEndTime; 56 long stageStartTime; 57 58 /** 59 * {@inheritDoc} 60 */ 61 @Override 62 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 63 Assert.assertNotNull(mTestDevice); 64 listener.testRunStarted("EncryptionFunc", 0); 65 mTestDevice.unencryptDevice(); 66 mTestDevice.waitForDeviceAvailable(); 67 stageStartTime = System.currentTimeMillis(); 68 mTestDevice.executeShellCommand("vdc cryptfs enablecrypto inplace password \"abcd\""); 69 try { 70 mTestDevice.waitForDeviceOnline(); 71 stageEnd(); // stage 1 72 73 mTestDevice.waitForBootComplete(BOOT_TIMEOUT); 74 stageEnd(); // stage 2 75 mTestDevice.enableAdbRoot(); 76 mTestDevice.executeShellCommand("vdc cryptfs checkpw \"abcd\""); 77 mTestDevice.executeShellCommand("vdc cryptfs restart"); 78 stageEnd(); // stage 3 79 80 mTestDevice.waitForDeviceAvailable(); 81 stageEnd(); // stage 4 82 83 mTestDevice.enableAdbRoot(); 84 mTestDevice.executeShellCommand("vdc cryptfs changepw password \"dcba\""); 85 mTestDevice.nonBlockingReboot(); 86 mTestDevice.waitForBootComplete(BOOT_TIMEOUT); 87 stageEnd(false); // stage 5 88 89 if (!mTestDevice.executeShellCommand("ls /mnt/shell/emulated").trim().isEmpty()) { 90 listener.testRunFailed("Still can access sdcard after password is changed"); 91 } else { 92 mTestDevice.enableAdbRoot(); 93 mTestDevice.executeShellCommand("vdc cryptfs checkpw \"dcba\""); 94 mTestDevice.executeShellCommand("vdc cryptfs restart"); 95 mTestDevice.waitForDeviceAvailable(); 96 stageEnd(false); // stage 6 97 mTestDevice.executeShellCommand("vdc cryptfs changepw default"); 98 mTestDevice.nonBlockingReboot(); 99 mTestDevice.waitForBootComplete(BOOT_TIMEOUT * 3); 100 stageEnd(false); // stage 7 101 } 102 } catch (DeviceNotAvailableException e) { 103 listener.testRunFailed(String.format("Device not avaible after %s before %s.", 104 STAGE_NAME[stage], STAGE_NAME[stage + 1])); 105 } 106 metrics.put("SuccessStage", Integer.toString(stage)); 107 listener.testRunEnded(0, metrics); 108 } 109 110 // measure the time between stages. 111 void stageEnd(boolean postTime) { 112 stageEndTime = System.currentTimeMillis(); 113 if (postTime) { 114 metrics.put(String.format("between%sAnd%s", 115 capitalize(STAGE_NAME[stage]),capitalize(STAGE_NAME[stage + 1])), 116 Long.toString(stageEndTime - stageStartTime)); 117 } 118 stageStartTime = stageEndTime; 119 stage++; 120 } 121 122 void stageEnd() { 123 stageEnd(true); 124 } 125 126 private String capitalize(String line) { 127 return Character.toUpperCase(line.charAt(0)) + line.substring(1); 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override 134 public void setDevice(ITestDevice device) { 135 mTestDevice = device; 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override 142 public ITestDevice getDevice() { 143 return mTestDevice; 144 } 145 } 146