1 /* 2 * Copyright (C) 2018 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.tradefed.targetprep; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 import static org.mockito.Mockito.*; 22 23 import com.android.compatibility.common.tradefed.build.VtsCompatibilityInvocationHelper; 24 import com.android.tradefed.build.IBuildInfo; 25 import com.android.tradefed.config.OptionSetter; 26 import com.android.tradefed.device.DeviceNotAvailableException; 27 import com.android.tradefed.device.ITestDevice; 28 import com.android.tradefed.testtype.IAbi; 29 import com.android.tradefed.util.CmdUtil; 30 import com.android.tradefed.util.FileUtil; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.junit.runners.JUnit4; 37 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.mockito.InjectMocks; 41 import org.mockito.Mockito; 42 43 import java.io.File; 44 import java.io.FileNotFoundException; 45 import java.io.IOException; 46 import java.util.ArrayList; 47 import java.util.List; 48 import java.util.Vector; 49 import java.util.function.Predicate; 50 /** 51 * Unit tests for {@link VtsHalAdapterPreparer}. 52 */ 53 @RunWith(JUnit4.class) 54 public final class VtsHalAdapterPreparerTest { 55 private int THREAD_COUNT_DEFAULT = VtsHalAdapterPreparer.THREAD_COUNT_DEFAULT; 56 private String SCRIPT_PATH = VtsHalAdapterPreparer.SCRIPT_PATH; 57 private String LIST_HAL_CMD = VtsHalAdapterPreparer.LIST_HAL_CMD; 58 59 private String VTS_NATIVE_TEST_DIR = "DATA/nativetest64/"; 60 private String TARGET_NATIVE_TEST_DIR = "/data/nativetest64/"; 61 private String TEST_HAL_ADAPTER_BINARY = "android.hardware.foo (at) 1.0-adapter"; 62 private String TEST_HAL_PACKAGE = "android.hardware.foo (at) 1.1"; 63 64 private class TestCmdUtil extends CmdUtil { 65 public boolean mCmdSuccess = true; 66 @Override 67 public boolean waitCmdResultWithDelay(ITestDevice device, String cmd, 68 Predicate<String> predicate) throws DeviceNotAvailableException { 69 return mCmdSuccess; 70 } 71 72 @Override 73 public boolean retry(ITestDevice device, String cmd, String validation_cmd, 74 Predicate<String> predicate, int retry_count) throws DeviceNotAvailableException { 75 device.executeShellCommand(cmd); 76 return mCmdSuccess; 77 } 78 79 @Override 80 public boolean retry(ITestDevice device, Vector<String> cmds, String validation_cmd, 81 Predicate<String> predicate) throws DeviceNotAvailableException { 82 for (String cmd : cmds) { 83 device.executeShellCommand(cmd); 84 } 85 return mCmdSuccess; 86 } 87 88 @Override 89 public void restartFramework(ITestDevice device) throws DeviceNotAvailableException {} 90 91 @Override 92 public void setSystemProperty(ITestDevice device, String name, String value) 93 throws DeviceNotAvailableException {} 94 } 95 private TestCmdUtil mCmdUtil = new TestCmdUtil(); 96 VtsCompatibilityInvocationHelper mMockHelper = null; 97 private class TestPreparer extends VtsHalAdapterPreparer { 98 @Override 99 VtsCompatibilityInvocationHelper createVtsHelper() { 100 return mMockHelper; 101 } 102 } 103 File mTestDir = null; 104 105 @Mock private IBuildInfo mBuildInfo; 106 @Mock private ITestDevice mDevice; 107 @Mock private IAbi mAbi; 108 @InjectMocks private TestPreparer mPreparer = new TestPreparer(); 109 110 @Before 111 public void setUp() throws Exception { 112 MockitoAnnotations.initMocks(this); 113 // Create the base dirs 114 mTestDir = FileUtil.createTempDir("vts-hal-adapter-preparer-unit-tests"); 115 new File(mTestDir, VTS_NATIVE_TEST_DIR).mkdirs(); 116 mMockHelper = new VtsCompatibilityInvocationHelper() { 117 @Override 118 public File getTestsDir() throws FileNotFoundException { 119 return mTestDir; 120 } 121 }; 122 mPreparer.setCmdUtil(mCmdUtil); 123 OptionSetter setter = new OptionSetter(mPreparer); 124 setter.setOptionValue("adapter-binary-name", TEST_HAL_ADAPTER_BINARY); 125 setter.setOptionValue("hal-package-name", TEST_HAL_PACKAGE); 126 doReturn("64").when(mAbi).getBitness(); 127 } 128 129 @After 130 public void tearDown() throws Exception { 131 FileUtil.recursiveDelete(mTestDir); 132 } 133 134 @Test 135 public void testOnSetUpAdapterSingleInstance() throws Exception { 136 File testAdapter = createTestAdapter(); 137 String output = "android.hardware.foo (at) 1.1::IFoo/default"; 138 doReturn(output).doReturn("").when(mDevice).executeShellCommand( 139 String.format(LIST_HAL_CMD, TEST_HAL_PACKAGE)); 140 141 mPreparer.setUp(mDevice, mBuildInfo); 142 verify(mDevice, times(1)) 143 .pushFile(eq(testAdapter), eq(TARGET_NATIVE_TEST_DIR + TEST_HAL_ADAPTER_BINARY)); 144 String adapterCmd = String.format("%s /data/nativetest64/%s %s %s %d", SCRIPT_PATH, 145 TEST_HAL_ADAPTER_BINARY, "IFoo", "default", THREAD_COUNT_DEFAULT); 146 verify(mDevice, times(1)).executeShellCommand(eq(adapterCmd)); 147 } 148 149 @Test 150 public void testOnSetUpAdapterMultipleInstance() throws Exception { 151 File testAdapter = createTestAdapter(); 152 String output = "android.hardware.foo (at) 1.1::IFoo/default\n" 153 + "android.hardware.foo (at) 1.1::IFoo/test\n" 154 + "android.hardware.foo (at) 1.1::IFooSecond/default\n" 155 + "android.hardware.foo (at) 1.1::IFooSecond/slot/1\n"; 156 doReturn(output).doReturn("").when(mDevice).executeShellCommand( 157 String.format(LIST_HAL_CMD, TEST_HAL_PACKAGE)); 158 159 mPreparer.setUp(mDevice, mBuildInfo); 160 161 List<String> adapterCmds = new ArrayList<String>(); 162 adapterCmds.add(String.format("%s /data/nativetest64/%s %s %s %d", SCRIPT_PATH, 163 TEST_HAL_ADAPTER_BINARY, "IFoo", "default", THREAD_COUNT_DEFAULT)); 164 adapterCmds.add(String.format("%s /data/nativetest64/%s %s %s %d", SCRIPT_PATH, 165 TEST_HAL_ADAPTER_BINARY, "IFoo", "test", THREAD_COUNT_DEFAULT)); 166 adapterCmds.add(String.format("%s /data/nativetest64/%s %s %s %d", SCRIPT_PATH, 167 TEST_HAL_ADAPTER_BINARY, "IFooSecond", "default", THREAD_COUNT_DEFAULT)); 168 adapterCmds.add(String.format("%s /data/nativetest64/%s %s %s %d", SCRIPT_PATH, 169 TEST_HAL_ADAPTER_BINARY, "IFooSecond", "slot/1", THREAD_COUNT_DEFAULT)); 170 171 for (String cmd : adapterCmds) { 172 verify(mDevice, times(1)).executeShellCommand(eq(cmd)); 173 } 174 } 175 176 @Test 177 public void testOnSetupAdapterNotFound() throws Exception { 178 try { 179 mPreparer.setUp(mDevice, mBuildInfo); 180 } catch (TargetSetupError e) { 181 assertEquals("Could not push adapter.", e.getMessage()); 182 return; 183 } 184 fail(); 185 } 186 187 @Test 188 public void testOnSetupServiceNotAvailable() throws Exception { 189 File testAdapter = createTestAdapter(); 190 doReturn("").when(mDevice).executeShellCommand( 191 String.format(LIST_HAL_CMD, TEST_HAL_PACKAGE)); 192 mPreparer.setUp(mDevice, mBuildInfo); 193 } 194 195 @Test 196 public void testOnSetUpAdapterFailed() throws Exception { 197 File testAdapter = createTestAdapter(); 198 String output = "android.hardware.foo (at) 1.1::IFoo/default"; 199 doReturn(output).when(mDevice).executeShellCommand( 200 String.format(LIST_HAL_CMD, TEST_HAL_PACKAGE)); 201 mCmdUtil.mCmdSuccess = false; 202 try { 203 mPreparer.setUp(mDevice, mBuildInfo); 204 } catch (TargetSetupError e) { 205 assertEquals("HAL adapter setup failed.", e.getMessage()); 206 return; 207 } 208 fail(); 209 } 210 211 @Test 212 public void testOnTearDownRestoreFailed() throws Exception { 213 mCmdUtil.mCmdSuccess = false; 214 try { 215 mPreparer.setCmdUtil(mCmdUtil); 216 mPreparer.addCommand("one"); 217 mPreparer.tearDown(mDevice, mBuildInfo, null); 218 } catch (AssertionError | DeviceNotAvailableException e) { 219 assertEquals("HAL restore failed.", e.getMessage()); 220 return; 221 } 222 fail(); 223 } 224 225 /** 226 * Helper method to create a test adapter under mTestDir. 227 * 228 * @return created test file. 229 */ 230 private File createTestAdapter() throws IOException { 231 File testAdapter = new File(mTestDir, VTS_NATIVE_TEST_DIR + TEST_HAL_ADAPTER_BINARY); 232 testAdapter.createNewFile(); 233 return testAdapter; 234 } 235 } 236