1 /* 2 * Copyright (C) 2011 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.config.Option; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.result.ITestInvocationListener; 24 import com.android.tradefed.testtype.IDeviceTest; 25 import com.android.tradefed.testtype.IRemoteTest; 26 import com.android.tradefed.util.SimpleStats; 27 28 import org.junit.Assert; 29 30 import java.util.HashMap; 31 import java.util.Map; 32 33 /** 34 * Runs the encryption stress tests. 35 * <p> 36 * Repeatedly encrypts the device inplace, and unlocks the device, and then unencrypts the device. 37 * The time taken to encrypt the device is measured, and each step is verified. 38 * </p> 39 */ 40 public class EncryptionStressTest implements IDeviceTest, IRemoteTest { 41 42 SimpleStats mEncryptionStats = new SimpleStats(); 43 ITestDevice mTestDevice = null; 44 45 @Option(name="iterations", description="The number of iterations to run") 46 private int mIterations = 10; 47 48 /** 49 * Encrypts the device inplace, unlocks the device, and then unencrypts the device. 50 * 51 * @throws DeviceNotAvailableException If the device is not available or if there is an error 52 * when encrypting the device. 53 */ 54 private void encryptDevice() throws DeviceNotAvailableException { 55 // unencrypt the device and verify that it is not encrypted 56 if (!(mTestDevice.unencryptDevice() && !mTestDevice.isDeviceEncrypted())) { 57 String message = String.format("Failed to unencrypt device %s", 58 mTestDevice.getSerialNumber()); 59 CLog.e(message); 60 throw new DeviceNotAvailableException(message, mTestDevice.getSerialNumber()); 61 } 62 63 long start = System.currentTimeMillis(); 64 65 // encrypt the device and verify that it is encrypted 66 if (!(mTestDevice.encryptDevice(true) && mTestDevice.isDeviceEncrypted())) { 67 String message = String.format("Failed to unencrypt device %s", 68 mTestDevice.getSerialNumber()); 69 CLog.e(message); 70 throw new DeviceNotAvailableException(message, mTestDevice.getSerialNumber()); 71 } 72 73 mEncryptionStats.add((System.currentTimeMillis() - start) / 1000.0); 74 75 if (!mTestDevice.unlockDevice()) { 76 String message = String.format("Failed to unencrypt device %s", 77 mTestDevice.getSerialNumber()); 78 CLog.e(message); 79 throw new DeviceNotAvailableException(message, mTestDevice.getSerialNumber()); 80 } 81 82 mTestDevice.waitForDeviceAvailable(); 83 mTestDevice.postBootSetup(); 84 85 // unencrypt the device and verify that it is not encrypted 86 if (!(mTestDevice.unencryptDevice() && !mTestDevice.isDeviceEncrypted())) { 87 String message = String.format("Failed to unencrypt device %s", 88 mTestDevice.getSerialNumber()); 89 CLog.e(message); 90 throw new DeviceNotAvailableException(message, mTestDevice.getSerialNumber()); 91 } 92 } 93 94 /** 95 * {@inheritDoc} 96 */ 97 @Override 98 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 99 Assert.assertNotNull(mTestDevice); 100 101 listener.testRunStarted("EncryptionStressTest", 0); 102 103 for (int i = 0; i < mIterations; i++) { 104 CLog.i("Starting encryption stress iteration %d of %d on device %s", i + 1, mIterations, 105 mTestDevice.getSerialNumber()); 106 encryptDevice(); 107 } 108 109 Map<String, String> metrics = new HashMap<String, String>(2); 110 metrics.put( 111 mTestDevice.getSerialNumber() + "_iterations", 112 Integer.valueOf(mEncryptionStats.size()).toString()); 113 metrics.put(mTestDevice.getSerialNumber() + "_mean", mEncryptionStats.mean().toString()); 114 115 listener.testRunEnded(0, metrics); 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 public void setDevice(ITestDevice device) { 123 mTestDevice = device; 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public ITestDevice getDevice() { 131 return mTestDevice; 132 } 133 } 134