diff options
-rw-r--r-- | tests/Android.bp | 2 | ||||
-rw-r--r-- | tests/src/com/android/tests/apex/ApexE2EBaseHostTest.java | 128 | ||||
-rw-r--r-- | tests/src/com/android/tests/apex/ApexTestUtils.java | 143 |
3 files changed, 165 insertions, 108 deletions
diff --git a/tests/Android.bp b/tests/Android.bp index 2f077d7..908db9a 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -75,7 +75,7 @@ java_test_host { java_library_host { name: "apex_e2e_base_test", - srcs: ["src/**/ApexE2EBaseHostTest.java"], + srcs: ["src/**/ApexE2EBaseHostTest.java","src/**/ApexTestUtils.java"], libs: ["tradefed"], } diff --git a/tests/src/com/android/tests/apex/ApexE2EBaseHostTest.java b/tests/src/com/android/tests/apex/ApexE2EBaseHostTest.java index f385027..8625576 100644 --- a/tests/src/com/android/tests/apex/ApexE2EBaseHostTest.java +++ b/tests/src/com/android/tests/apex/ApexE2EBaseHostTest.java @@ -16,7 +16,6 @@ package com.android.tests.apex; -import com.android.tradefed.build.BuildInfoKey.BuildInfoFileKey; import com.android.tradefed.config.Option; import com.android.tradefed.config.Option.Importance; import com.android.tradefed.device.DeviceNotAvailableException; @@ -24,11 +23,6 @@ import com.android.tradefed.device.ITestDevice.ApexInfo; import com.android.tradefed.log.LogUtil.CLog; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; import com.android.tradefed.util.CommandResult; -import com.android.tradefed.util.CommandStatus; -import com.android.tradefed.util.FileUtil; -import com.android.tradefed.util.IRunUtil; -import com.android.tradefed.util.RunUtil; -import com.android.tradefed.util.SystemUtil.EnvVariable; import org.junit.After; import org.junit.Assert; @@ -44,27 +38,29 @@ import java.util.regex.Pattern; * Base test to check if Apex can be staged, activated and uninstalled successfully. */ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { - private static final String APEX_DATA_DIR = "/data/apex"; - private static final String STAGING_DATA_DIR = "/data/pkg_staging"; - private static final String OPTION_APEX_FILE_NAME = "apex_file_name"; - private static final String OPTION_BROADCASTAPP_APK_NAME = "broadcastapp_apk_name"; - private static final String BROADCASTAPP_PACKAGE_NAME = "android.apex.broadcastreceiver"; - private static final String APEX_INFO_EXTRACT_REGEX = - ".*package:\\sname='(\\S+)\\'\\sversionCode='(\\d+)'\\s.*"; - private final Pattern mAppPackageNamePattern = + protected static final String APEX_DATA_DIR = "/data/apex"; + protected static final String STAGING_DATA_DIR = "/data/pkg_staging"; + protected static final String OPTION_APEX_FILE_NAME = "apex_file_name"; + protected static final String OPTION_BROADCASTAPP_APK_NAME = "broadcastapp_apk_name"; + protected static final String BROADCASTAPP_PACKAGE_NAME = "android.apex.broadcastreceiver"; + + protected final Pattern mAppPackageNamePattern = Pattern.compile("appPackageName = com\\.android\\.apex\\.test;"); - private final Pattern mIsSessionReadyPattern = Pattern.compile("isStagedSessionReady = true"); - private final Pattern mIsSessionAppliedPattern = + protected final Pattern mIsSessionReadyPattern = Pattern.compile("isStagedSessionReady = true"); + protected final Pattern mIsSessionAppliedPattern = Pattern.compile("isStagedSessionApplied = true;"); - private final Pattern mSessionBroadcastReceiver = + protected final Pattern mSessionBroadcastReceiver = Pattern.compile("BroadcastReceiver: Action: android.content.pm.action.SESSION_UPDATED"); + /* protected so that derived tests can have access to test utils automatically */ + protected final ApexTestUtils mUtils = new ApexTestUtils(this); + @Option(name = OPTION_APEX_FILE_NAME, description = "The file name of the apex module.", importance = Importance.IF_UNSET, mandatory = true ) - private String mApexFileName = null; + protected String mApexFileName = null; @Option(name = OPTION_BROADCASTAPP_APK_NAME, description = "The APK file name of the BroadcastReceiver app.", @@ -73,8 +69,6 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { ) private String mBroadcastAppApkName = null; - private IRunUtil mRunUtil = new RunUtil(); - @Before public synchronized void setUp() throws Exception { getDevice().executeShellV2Command("rm -rf " + APEX_DATA_DIR + "/*"); @@ -82,7 +76,7 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { getDevice().reboot(); // for the above commands to take affect // Install broadcast receiver app String installResult = getDevice().installPackage( - getTestFile(mBroadcastAppApkName), false); + mUtils.getTestFile(mBroadcastAppApkName), false); Assert.assertNull( String.format("failed to install test app %s. Reason: %s", mBroadcastAppApkName, installResult), @@ -95,7 +89,7 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { public void doTestStageActivateUninstallApexPackage() throws DeviceNotAvailableException, IOException { - File testAppFile = getTestFile(mApexFileName); + File testAppFile = mUtils.getTestFile(mApexFileName); CLog.i("Found test apex file: " + testAppFile.getAbsoluteFile()); // Make MainActivity foreground service @@ -120,7 +114,7 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { e.printStackTrace(); } - ApexInfo testApexInfo = getApexInfo(testAppFile); + ApexInfo testApexInfo = mUtils.getApexInfo(testAppFile); Assert.assertNotNull(testApexInfo); // Assert isStagedSessionReady is true @@ -128,20 +122,20 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { Assert.assertEquals("", result.getStderr()); // TODO: Look into why appPackageInfo is null? or should it be null? // assertMatchesRegex(result.getStdout(), mAppPackageNamePattern); - assertMatchesRegex(result.getStdout(), mIsSessionReadyPattern); + mUtils.assertMatchesRegex(result.getStdout(), mIsSessionReadyPattern); // Assert session update broadcast was sent to apps listening to it. result = getDevice().executeShellV2Command("logcat -d"); - assertMatchesRegex(result.getStdout(), mSessionBroadcastReceiver); + mUtils.assertMatchesRegex(result.getStdout(), mSessionBroadcastReceiver); matcher = mIsSessionReadyPattern.matcher(result.getStdout()); - assertMatchesRegex(result.getStdout(), mIsSessionReadyPattern); + mUtils.assertMatchesRegex(result.getStdout(), mIsSessionReadyPattern); getDevice().reboot(); // This checks that the staged package was activated on reboot result = getDevice().executeShellV2Command("pm get-stagedsessions"); Assert.assertEquals("", result.getStderr()); - assertMatchesRegex(result.getStdout(), mIsSessionAppliedPattern); + mUtils.assertMatchesRegex(result.getStdout(), mIsSessionAppliedPattern); Set<ApexInfo> activatedApexes = getDevice().getActiveApexes(); Assert.assertTrue( @@ -152,84 +146,11 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { additionalCheck(); } - /* - * Retrieve package name and version code from test apex file. - */ - private ApexInfo getApexInfo(File apex) { - String aaptOutput = runCmd(String.format( - "aapt dump badging %s", apex.getAbsolutePath())); - String[] lines = aaptOutput.split("\n"); - Pattern p = Pattern.compile(APEX_INFO_EXTRACT_REGEX); - for (String l : lines) { - Matcher m = p.matcher(l); - if (m.matches()) { - ApexInfo apexInfo = new ApexInfo(m.group(1), Long.parseLong(m.group(2))); - return apexInfo; - } - } - return null; - } - - private String runCmd(String cmd) { - CLog.d("About to run command: %s", cmd); - CommandResult result = mRunUtil.runTimedCmd(1000 * 60 * 5, cmd.split("\\s+")); - Assert.assertNotNull(result); - Assert.assertTrue( - String.format("Command %s failed", cmd), - result.getStatus().equals(CommandStatus.SUCCESS)); - CLog.v("output:\n%s", result.getStdout()); - return result.getStdout(); - } - /** * Do some additional check, invoked by doTestStageActivateUninstallApexPackage. */ public abstract void additionalCheck(); - /** - * Helper method to get the test file. - */ - private File getTestFile(String testFileName) throws IOException { - File testFile = null; - - String testcasesPath = System.getenv(EnvVariable.ANDROID_HOST_OUT_TESTCASES.toString()); - if (testcasesPath != null) { - testFile = searchTestFile(new File(testcasesPath), testFileName); - } - if (testFile != null) { - return testFile; - } - - File hostLinkedDir = getBuild().getFile(BuildInfoFileKey.HOST_LINKED_DIR); - if (hostLinkedDir != null) { - testFile = searchTestFile(hostLinkedDir, testFileName); - } - if (testFile != null) { - return testFile; - } - - // Find the file in the buildinfo. - File tzdataFile = getBuild().getFile(testFileName); - if (tzdataFile != null) { - return tzdataFile; - } - - throw new IOException("Cannot find " + testFileName); - } - - /** - * Searches the file with the given name under the given directory, returns null if not found. - */ - private File searchTestFile(File baseSearchFile, String testFileName) { - if (baseSearchFile != null && baseSearchFile.isDirectory()) { - File testFile = FileUtil.findFile(baseSearchFile, testFileName); - if (testFile != null && testFile.isFile()) { - return testFile; - } - } - return null; - } - @After public void tearDown() throws DeviceNotAvailableException { getDevice().executeShellV2Command("rm -rf " + APEX_DATA_DIR + "/*"); @@ -237,11 +158,4 @@ public abstract class ApexE2EBaseHostTest extends BaseHostJUnit4Test { getDevice().uninstallPackage(BROADCASTAPP_PACKAGE_NAME); getDevice().reboot(); } - - private static void assertMatchesRegex(String text, Pattern pattern) { - Matcher matcher = pattern.matcher(text); - Assert.assertTrue( - String.format("Not true that '%s' matches regexp '%s'", text, pattern), - matcher.find()); - } } diff --git a/tests/src/com/android/tests/apex/ApexTestUtils.java b/tests/src/com/android/tests/apex/ApexTestUtils.java new file mode 100644 index 0000000..d30c167 --- /dev/null +++ b/tests/src/com/android/tests/apex/ApexTestUtils.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tests.apex; + +import com.android.tradefed.build.BuildInfoKey.BuildInfoFileKey; +import com.android.tradefed.build.IBuildInfo; +import com.android.tradefed.device.ITestDevice.ApexInfo; +import com.android.tradefed.log.LogUtil.CLog; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; +import com.android.tradefed.util.FileUtil; +import com.android.tradefed.util.IRunUtil; +import com.android.tradefed.util.RunUtil; +import com.android.tradefed.util.SystemUtil.EnvVariable; + +import org.junit.Assert; + +import java.io.File; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +class ApexTestUtils { + + private static final String APEX_INFO_EXTRACT_REGEX = + ".*package:\\sname='(\\S+)\\'\\sversionCode='(\\d+)'\\s.*"; + + private IRunUtil mRunUtil = new RunUtil(); + private BaseHostJUnit4Test mTest; + + private IBuildInfo getBuild() { + return mTest.getBuild(); + } + + ApexTestUtils(BaseHostJUnit4Test test) { + mTest = test; + } + + /** + * Retrieve package name and version code from test apex file. + * + * @param apex input apex file to retrieve the info from + */ + ApexInfo getApexInfo(File apex) { + String aaptOutput = runCmd(String.format( + "aapt dump badging %s", apex.getAbsolutePath())); + String[] lines = aaptOutput.split("\n"); + Pattern p = Pattern.compile(APEX_INFO_EXTRACT_REGEX); + for (String l : lines) { + Matcher m = p.matcher(l); + if (m.matches()) { + ApexInfo apexInfo = new ApexInfo(m.group(1), Long.parseLong(m.group(2))); + return apexInfo; + } + } + return null; + } + + /** + * Get the test file. + * + * @param testFileName name of the file + */ + File getTestFile(String testFileName) throws IOException { + File testFile = null; + + String testcasesPath = System.getenv(EnvVariable.ANDROID_HOST_OUT_TESTCASES.toString()); + if (testcasesPath != null) { + testFile = searchTestFile(new File(testcasesPath), testFileName); + } + if (testFile != null) { + return testFile; + } + + File hostLinkedDir = getBuild().getFile(BuildInfoFileKey.HOST_LINKED_DIR); + if (hostLinkedDir != null) { + testFile = searchTestFile(hostLinkedDir, testFileName); + } + if (testFile != null) { + return testFile; + } + + // Find the file in the buildinfo. + File tzdataFile = getBuild().getFile(testFileName); + if (tzdataFile != null) { + return tzdataFile; + } + + throw new IOException("Cannot find " + testFileName); + } + + /** + * Assert that a given string matches the a given regex. + * + * @param text the string + * @param pattern the regex + */ + void assertMatchesRegex(String text, Pattern pattern) { + Matcher matcher = pattern.matcher(text); + Assert.assertTrue( + String.format("Not true that '%s' matches regexp '%s'", text, pattern), + matcher.find()); + } + + private String runCmd(String cmd) { + CLog.d("About to run command: %s", cmd); + CommandResult result = mRunUtil.runTimedCmd(1000 * 60 * 5, cmd.split("\\s+")); + Assert.assertNotNull(result); + Assert.assertTrue( + String.format("Command %s failed", cmd), + result.getStatus().equals(CommandStatus.SUCCESS)); + CLog.v("output:\n%s", result.getStdout()); + return result.getStdout(); + } + + /** + * Searches the file with the given name under the given directory, returns null if not found. + */ + private File searchTestFile(File baseSearchFile, String testFileName) { + if (baseSearchFile != null && baseSearchFile.isDirectory()) { + File testFile = FileUtil.findFile(baseSearchFile, testFileName); + if (testFile != null && testFile.isFile()) { + return testFile; + } + } + return null; + } +} |