Merge "Add sepolicy_tests.TestVendorTypeViolators to CTS."
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/ResultReporter.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/ResultReporter.java
index 51d59a1..328148c 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/ResultReporter.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/ResultReporter.java
@@ -19,6 +19,7 @@
 import com.android.compatibility.common.tradefed.testtype.CompatibilityTest;
 import com.android.compatibility.common.tradefed.util.RetryType;
 import com.android.compatibility.common.util.ChecksumReporter;
+import com.android.compatibility.common.util.DeviceInfo;
 import com.android.compatibility.common.util.ICaseResult;
 import com.android.compatibility.common.util.IInvocationResult;
 import com.android.compatibility.common.util.IModuleResult;
@@ -567,20 +568,42 @@
             mMasterResultReporter.testLog(name, type, stream);
             return;
         }
-        try {
-            File logFile = null;
-            if (mCompressLogs) {
-                try (InputStream inputStream = stream.createInputStream()) {
-                    logFile = mTestLogSaver.saveAndGZipLogData(name, type, inputStream);
+        if (name.endsWith(DeviceInfo.FILE_SUFFIX)) {
+            // Handle device info file case
+            testLogDeviceInfo(name, stream);
+        } else {
+            // Handle default case
+            try {
+                File logFile = null;
+                if (mCompressLogs) {
+                    try (InputStream inputStream = stream.createInputStream()) {
+                        logFile = mTestLogSaver.saveAndGZipLogData(name, type, inputStream);
+                    }
+                } else {
+                    try (InputStream inputStream = stream.createInputStream()) {
+                        logFile = mTestLogSaver.saveLogData(name, type, inputStream);
+                    }
                 }
-            } else {
-                try (InputStream inputStream = stream.createInputStream()) {
-                    logFile = mTestLogSaver.saveLogData(name, type, inputStream);
-                }
+                debug("Saved logs for %s in %s", name, logFile.getAbsolutePath());
+            } catch (IOException e) {
+                warn("Failed to write log for %s", name);
+                CLog.e(e);
             }
-            debug("Saved logs for %s in %s", name, logFile.getAbsolutePath());
+        }
+    }
+
+    /* Write device-info files to the result, invoked only by the master result reporter */
+    private void testLogDeviceInfo(String name, InputStreamSource stream) {
+        try {
+            File ediDir = new File(mResultDir, DeviceInfo.RESULT_DIR_NAME);
+            ediDir.mkdirs();
+            File ediFile = new File(ediDir, name);
+            if (!ediFile.exists()) {
+                // only write this file to the results if not already present
+                FileUtil.writeToFile(stream.createInputStream(), ediFile);
+            }
         } catch (IOException e) {
-            warn("Failed to write log for %s", name);
+            warn("Failed to write device info %s to result", name);
             CLog.e(e);
         }
     }
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java
index d48e9f6..8a86fe9 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/DeviceInfoCollector.java
@@ -16,27 +16,30 @@
 
 package com.android.compatibility.common.tradefed.targetprep;
 
-import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 import com.android.compatibility.common.tradefed.testtype.CompatibilityTest;
-import com.android.compatibility.common.tradefed.util.CollectorUtil;
+import com.android.compatibility.common.util.DeviceInfo;
 import com.android.compatibility.common.util.DevicePropertyInfo;
 import com.android.tradefed.build.IBuildInfo;
 import com.android.tradefed.config.Option;
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.ITestLogger;
 import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.result.FileInputStreamSource;
+import com.android.tradefed.result.ITestLoggerReceiver;
+import com.android.tradefed.result.LogDataType;
 import com.android.tradefed.targetprep.BuildError;
 import com.android.tradefed.targetprep.TargetSetupError;
 import com.android.tradefed.util.FileUtil;
 
 import java.io.File;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.Map.Entry;
 
 /**
  * An {@link ApkInstrumentationPreparer} that collects device info.
  */
-public class DeviceInfoCollector extends ApkInstrumentationPreparer {
+public class DeviceInfoCollector extends ApkInstrumentationPreparer implements ITestLoggerReceiver {
 
     private static final String ABI = "ro.product.cpu.abi";
     private static final String ABI2 = "ro.product.cpu.abi2";
@@ -72,19 +75,16 @@
     private String mSrcDir;
 
     @Option(name = "dest-dir", description = "The directory under the result to store the files")
-    private String mDestDir;
+    private String mDestDir = DeviceInfo.RESULT_DIR_NAME;
 
+    @Deprecated
     @Option(name = "temp-dir", description = "The directory containing host-side device info files")
     private String mTempDir;
 
-    // Temp directory for host-side device info files.
-    private File mHostDir;
-
-    // Destination result directory for all device info files.
-    private File mResultDir;
+    private ITestLogger mLogger;
 
     public DeviceInfoCollector() {
-        mWhen = When.BOTH;
+        mWhen = When.BEFORE;
     }
 
     @Override
@@ -104,58 +104,30 @@
         if (mSkipDeviceInfo) {
             return;
         }
-
-        createTempHostDir();
-        createResultDir(buildInfo);
         run(device, buildInfo);
-        getDeviceInfoFiles(device);
+        File deviceInfoDir = null;
+        try {
+            deviceInfoDir = FileUtil.createTempDir(DeviceInfo.RESULT_DIR_NAME);
+            if (device.pullDir(mSrcDir, deviceInfoDir)) {
+                for (File deviceInfoFile : deviceInfoDir.listFiles()) {
+                    FileInputStreamSource source = new FileInputStreamSource(deviceInfoFile);
+                    mLogger.testLog(deviceInfoFile.getName(), LogDataType.TEXT, source);
+                    source.close();
+                }
+            } else {
+                CLog.e("Failed to pull device-info files from device %s", device.getSerialNumber());
+            }
+        } catch (IOException e) {
+            CLog.e("Failed to pull device-info files from device %s", device.getSerialNumber());
+            CLog.e(e);
+        } finally {
+            FileUtil.recursiveDelete(deviceInfoDir);
+        }
     }
 
     @Override
-    public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) {
-        if (mSkipDeviceInfo) {
-            return;
-        }
-        if (mHostDir != null && mHostDir.isDirectory() &&
-                mResultDir != null && mResultDir.isDirectory()) {
-            CollectorUtil.pullFromHost(mHostDir, mResultDir);
-        }
-    }
-
-    private void createTempHostDir() {
-        try {
-            mHostDir = FileUtil.createNamedTempDir(mTempDir);
-            if (!mHostDir.isDirectory()) {
-                CLog.e("%s is not a directory", mHostDir.getAbsolutePath());
-                return;
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    private void createResultDir(IBuildInfo buildInfo) {
-        CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
-        try {
-            mResultDir = buildHelper.getResultDir();
-            if (mDestDir != null) {
-                mResultDir = new File(mResultDir, mDestDir);
-            }
-            mResultDir.mkdirs();
-            if (!mResultDir.isDirectory()) {
-                CLog.e("%s is not a directory", mResultDir.getAbsolutePath());
-                return;
-            }
-        } catch (FileNotFoundException fnfe) {
-            fnfe.printStackTrace();
-        }
-    }
-
-    private void getDeviceInfoFiles(ITestDevice device) {
-        if (mResultDir != null && mResultDir.isDirectory()) {
-            String mResultPath = mResultDir.getAbsolutePath();
-            CollectorUtil.pullFromDevice(device, mSrcDir, mResultPath);
-        }
+    public void setTestLogger(ITestLogger testLogger) {
+        mLogger = testLogger;
     }
 
     private static String nullToEmpty(String value) {
diff --git a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/result/ResultReporterTest.java b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/result/ResultReporterTest.java
index 7a1fe7f..c24e9df 100644
--- a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/result/ResultReporterTest.java
+++ b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/result/ResultReporterTest.java
@@ -18,6 +18,7 @@
 
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildProvider;
+import com.android.compatibility.common.util.DeviceInfo;
 import com.android.compatibility.common.util.ICaseResult;
 import com.android.compatibility.common.util.IInvocationResult;
 import com.android.compatibility.common.util.IModuleResult;
@@ -475,4 +476,26 @@
         // actual logs
         assertEquals(2, mBuildHelper.getLogsDir().listFiles()[0].listFiles()[0].list().length);
     }
+
+    /**
+     * Ensure that when {@link ResultReporter#testLog(String, LogDataType, InputStreamSource)} is
+     * called for host-side device info, a device info file is created in the result
+     */
+    public void testTestLogWithDeviceInfo() throws Exception {
+        InputStreamSource fakeData = new ByteArrayInputStreamSource("test".getBytes());
+        String deviceInfoName = String.format("Test%s", DeviceInfo.FILE_SUFFIX);
+        mReporter.invocationStarted(mContext);
+        mReporter.testLog(deviceInfoName, LogDataType.TEXT, fakeData);
+        File deviceInfoFolder = new File(mBuildHelper.getResultDir(), DeviceInfo.RESULT_DIR_NAME);
+        // assert device info folder was created
+        assertTrue(deviceInfoFolder.exists());
+        File[] deviceInfoFiles = deviceInfoFolder.listFiles();
+        // assert that one file was written to the folder
+        assertEquals(1, deviceInfoFiles.length);
+        File deviceInfoFile = deviceInfoFiles[0];
+        // assert device info file has been named correctly
+        assertEquals(deviceInfoName, deviceInfoFile.getName());
+        // assert contents of the file
+        assertEquals("test", FileUtil.readStringFromFile(deviceInfoFile));
+    }
 }
diff --git a/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java b/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java
index ef05813..5d303e5 100644
--- a/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java
+++ b/common/host-side/util/src/com/android/compatibility/common/util/DeviceInfo.java
@@ -15,36 +15,80 @@
  */
 package com.android.compatibility.common.util;
 
+import static org.junit.Assert.fail;
+
 import com.android.compatibility.common.util.HostInfoStore;
-import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.result.FileInputStreamSource;
+import com.android.tradefed.result.LogDataType;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
+import com.android.tradefed.testtype.IDeviceTest;
 import com.android.tradefed.util.FileUtil;
+import com.android.tradefed.util.StreamUtil;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 import java.io.File;
 
 /**
  * Collect device information from host and write to a JSON file.
  */
-public abstract class DeviceInfo extends DeviceTestCase {
+@RunWith(DeviceJUnit4ClassRunner.class)
+public abstract class DeviceInfo implements IDeviceTest {
 
-    // Temporary folder must match the temp-dir value configured in DeviceInfoCollector target
-    // preparer in cts/tools/cts-tradefed/res/config/cts-preconditions.xml
-    private static final String TEMPORARY_REPORT_FOLDER = "temp-device-info-files/";
+    // Default name of the directory for storing device info files within the result directory
+    public static final String RESULT_DIR_NAME = "device-info-files";
+
+    public static final String FILE_SUFFIX = ".deviceinfo.json";
+
+    /** A reference to the device under test. */
+    protected ITestDevice mDevice;
 
     private HostInfoStore mStore;
 
+    @Rule
+    public TestLogData mLogger = new TestLogData();
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public ITestDevice getDevice() {
+        return mDevice;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setDevice(ITestDevice device) {
+        mDevice = device;
+    }
+
+    @Test
     public void testCollectDeviceInfo() throws Exception {
-        String collectionName = getClass().getSimpleName();
+        String deviceInfoName = getClass().getSimpleName() + FILE_SUFFIX;
+        File jsonFile = null;
+        FileInputStreamSource source = null;
         try {
-            final File dir = FileUtil.createNamedTempDir(TEMPORARY_REPORT_FOLDER);
-            File jsonFile = new File(dir, collectionName + ".deviceinfo.json");
+            jsonFile = FileUtil.createTempFile(getClass().getSimpleName(), FILE_SUFFIX);
             mStore = new HostInfoStore(jsonFile);
             mStore.open();
             collectDeviceInfo(mStore);
             mStore.close();
+            source = new FileInputStreamSource(jsonFile);
+            mLogger.addTestLog(deviceInfoName, LogDataType.TEXT, source);
         } catch (Exception e) {
-            e.printStackTrace();
+            CLog.e(e);
             fail(String.format("Failed to collect device info (%s): %s",
-                    collectionName, e.getMessage()));
+                    deviceInfoName, e.getMessage()));
+        } finally {
+            FileUtil.deleteFile(jsonFile);
+            StreamUtil.close(source);
         }
     }
 
@@ -52,4 +96,4 @@
      * Method to collect device information.
      */
     protected abstract void collectDeviceInfo(HostInfoStore store) throws Exception;
-}
\ No newline at end of file
+}
diff --git a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteMultiViewTest.java b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteMultiViewTest.java
index db1c721..e48c319 100644
--- a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteMultiViewTest.java
+++ b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteMultiViewTest.java
@@ -21,10 +21,11 @@
 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.PACKAGE_READ;
 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertFileReadWriteAccess;
 
+import android.os.SystemClock;
 import android.system.Os;
 
 import android.test.AndroidTestCase;
-
+import android.text.format.DateUtils;
 
 import android.util.Log;
 
@@ -65,6 +66,9 @@
 
         Os.rename(ourTestDir.getAbsolutePath(), otherTestDir.getAbsolutePath());
 
+        // Sit around long enough for VFS cache to expire
+        SystemClock.sleep(15 * DateUtils.SECOND_IN_MILLIS);
+
         assertNotEqual(Os.getuid(), Os.stat(otherCache.getAbsolutePath()).st_uid);
         assertNotEqual(Os.getuid(), Os.stat(otherTestDir.getAbsolutePath()).st_uid);
         assertNotEqual(Os.getuid(), Os.stat(afterFile.getAbsolutePath()).st_uid);
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
index 6b7d89b..d5aadd0 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
@@ -114,6 +114,14 @@
             ASSIST_APP_PKG + "/.MyInteractionService";
 
     private static final String ARG_ALLOW_FAILURE = "allowFailure";
+
+    private static final String RESTRICT_BACKGROUND_GET_CMD =
+        "cmd netpolicy get restrict-background";
+    private static final String RESTRICT_BACKGROUND_ON_CMD =
+        "cmd netpolicy set restrict-background true";
+    private static final String RESTRICT_BACKGROUND_OFF_CMD =
+        "cmd netpolicy set restrict-background false";
+
     // ID of the user all tests are run as. For device owner this will be the primary user, for
     // profile owner it is the user id of the created profile.
     protected int mUserId;
@@ -267,7 +275,7 @@
             return;
         }
         installAppAsUser(VPN_APP_APK, mUserId);
-        executeDeviceTestClass(".AlwaysOnVpnTest");
+        executeDeviceTestClassNoRestrictBackground(".AlwaysOnVpnTest");
     }
 
     @RequiresDevice
@@ -818,6 +826,22 @@
         runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, mUserId);
     }
 
+    /**
+     * Executes a test class on device. Prior to running, turn off background data usage
+     * restrictions, and restore the original restrictions after the test.
+     */
+    protected void executeDeviceTestClassNoRestrictBackground(String className) throws Exception {
+        boolean originalRestriction = ensureRestrictBackgroundPolicyOff();
+        try {
+            executeDeviceTestClass(className);
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            // if the test throws exception, still restore the policy
+            restoreRestrictBackgroundPolicyTo(originalRestriction);
+        }
+    }
+
     protected void executeDeviceTestMethod(String className, String testName) throws Exception {
         runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, testName, mUserId);
     }
@@ -995,4 +1019,22 @@
     protected void clearVoiceInteractionService() throws DeviceNotAvailableException {
         getDevice().executeShellCommand("settings delete secure voice_interaction_service");
     }
+
+    /**
+     * Ensure that restrict background policy is off.
+     * Returns the original status of restrict background policy.
+     */
+    private boolean ensureRestrictBackgroundPolicyOff() throws Exception {
+        String restriction = getDevice().executeShellCommand(RESTRICT_BACKGROUND_GET_CMD);
+        if (restriction.contains("enabled")) {
+            getDevice().executeShellCommand(RESTRICT_BACKGROUND_OFF_CMD);
+            return true;
+        }
+        return false;
+    }
+
+    private void restoreRestrictBackgroundPolicyTo(boolean restricted) throws Exception {
+        getDevice().executeShellCommand(
+                restricted ? RESTRICT_BACKGROUND_ON_CMD : RESTRICT_BACKGROUND_OFF_CMD);
+    }
 }
diff --git a/hostsidetests/jvmti/attaching/host/src/android/jvmti/cts/JvmtiAttachingHostTest.java b/hostsidetests/jvmti/attaching/host/src/android/jvmti/cts/JvmtiAttachingHostTest.java
index 2438f32..52be162 100644
--- a/hostsidetests/jvmti/attaching/host/src/android/jvmti/cts/JvmtiAttachingHostTest.java
+++ b/hostsidetests/jvmti/attaching/host/src/android/jvmti/cts/JvmtiAttachingHostTest.java
@@ -93,8 +93,28 @@
         return AbiUtils.getBaseArchForAbi(abi);
     }
 
+    private void runAttachTestCmd(ITestDevice device, String pkg, String agentParams)
+            throws Exception {
+        String attachCmd = "cmd activity start -S -W " + agentParams + " -n " + pkg
+                + "/android.jvmti.JvmtiActivity";
+
+        String attachReply = device.executeShellCommand(attachCmd);
+        // Don't try to parse the output. The test will time out anyways if this didn't
+        // work.
+        if (attachReply != null && !attachReply.trim().isEmpty()) {
+            CLog.e(attachReply);
+        }
+    }
+
+    private final static String AGENT = "libctsjvmtiattachagent.so";
+
     private void runAttachTest(ITestDevice device, String pkg, String apk) {
         try {
+            runAttachTestCmd(device, pkg, "--attach-agent-bind " + AGENT);
+        } catch (Exception e) {
+            throw new RuntimeException("Failed bind-time attaching", e);
+        }
+        try {
             String pwd = device.executeShellCommand("run-as " + pkg + " pwd");
             if (pwd == null) {
                 throw new RuntimeException("pwd failed");
@@ -104,30 +124,24 @@
                 throw new RuntimeException("pwd failed");
             }
 
+            // Give it a different name, so we do not have "contamination" from
+            // the test APK.
+            String libInDataData = AGENT.substring(0, AGENT.length() - ".so".length()) + "2.so";
             String agentInDataData =
-                    installLibToDataData(device, pkg, apk, pwd, "libctsjvmtiattachagent.so");
-
-            String attachCmd = "cmd activity start -S -W --attach-agent " + agentInDataData + " -n "
-                    + pkg + "/android.jvmti.JvmtiActivity";
-
-            String attachReply = device.executeShellCommand(attachCmd);
-            // Don't try to parse the output. The test will time out anyways if this didn't
-            // work.
-            if (attachReply != null && !attachReply.trim().isEmpty()) {
-                CLog.e(attachReply);
-            }
+                    installLibToDataData(device, pkg, apk, pwd, AGENT, libInDataData);
+            runAttachTestCmd(device, pkg, "--attach-agent " + agentInDataData);
         } catch (Exception e) {
-            throw new RuntimeException("Failed attaching", e);
+            throw new RuntimeException("Failed pre-bind attaching", e);
         }
     }
 
     String installLibToDataData(ITestDevice device, String pkg, String apk, String dataData,
-            String library) throws Exception {
+            String library, String newLibName) throws Exception {
         ZipFile zf = null;
         File tmpFile = null;
         String libInTmp = null;
         try {
-            String libInDataData = dataData + "/" + library;
+            String libInDataData = dataData + "/" + newLibName;
 
             File apkFile = mBuildHelper.getTestFile(apk);
             zf = new ZipFile(apkFile);
diff --git a/hostsidetests/services/activityandwindowmanager/activitymanager/src/android/server/cts/ActivityManagerAssistantStackTests.java b/hostsidetests/services/activityandwindowmanager/activitymanager/src/android/server/cts/ActivityManagerAssistantStackTests.java
index be2af68..abc3082 100644
--- a/hostsidetests/services/activityandwindowmanager/activitymanager/src/android/server/cts/ActivityManagerAssistantStackTests.java
+++ b/hostsidetests/services/activityandwindowmanager/activitymanager/src/android/server/cts/ActivityManagerAssistantStackTests.java
@@ -189,6 +189,7 @@
         mAmWmState.waitForFocusedStack(mDevice, ASSISTANT_STACK_ID);
         assertAssistantStackExists();
         if (!noHomeScreen()) {
+            mAmWmState.waitForHomeActivityVisible(mDevice);
             mAmWmState.assertHomeActivityVisible(true);
         }
 
diff --git a/run_unit_tests.sh b/run_unit_tests.sh
index 64ecf25..3f29832 100755
--- a/run_unit_tests.sh
+++ b/run_unit_tests.sh
@@ -44,7 +44,6 @@
     cts-tradefed-tests\
     compatibility-device-info-tests\
     compatibility-manifest-generator-tests
-    compatibility-host-media-preconditions-tests\
     CompatibilityTestApp"
 
 pushd ${CTS_DIR}/..
@@ -74,5 +73,3 @@
 ${CTS_DIR}/common/util/tests/run_tests.sh
 
 ${CTS_DIR}/tools/cts-tradefed/tests/run_tests.sh
-
-${CTS_DIR}/tests/tests/mediastress/preconditions/tests/run_tests.sh
diff --git a/tests/dram/Android.mk b/tests/dram/Android.mk
deleted file mode 100644
index 1d21cdc..0000000
--- a/tests/dram/Android.mk
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (C) 2012 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.
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-# don't include this package in any target
-LOCAL_MODULE_TAGS := tests
-
-# Include both the 32 and 64 bit versions
-LOCAL_MULTILIB := both
-
-LOCAL_STATIC_JAVA_LIBRARIES := compatibility-device-util ctstestrunner
-
-LOCAL_JNI_SHARED_LIBRARIES := libctsdram_jni
-
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_PACKAGE_NAME := CtsDramTestCases
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
-
-LOCAL_SDK_VERSION := 16
-
-include $(BUILD_CTS_PACKAGE)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/dram/AndroidManifest.xml b/tests/dram/AndroidManifest.xml
deleted file mode 100644
index d43fe79..0000000
--- a/tests/dram/AndroidManifest.xml
+++ /dev/null
@@ -1,29 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2012 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.
--->
-
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-        package="android.dram.cts">
-
-    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
-    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
-    <application>
-        <uses-library android:name="android.test.runner" />
-    </application>
-    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
-            android:targetPackage="android.dram.cts"
-            android:label="DRAM bandwidth measurement" />
-</manifest>
diff --git a/tests/dram/AndroidTest.xml b/tests/dram/AndroidTest.xml
deleted file mode 100644
index 636c8c1..0000000
--- a/tests/dram/AndroidTest.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 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.
--->
-<configuration description="Config for CTS Dram test cases">
-    <option name="config-descriptor:metadata" key="component" value="systems" />
-    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
-        <option name="cleanup-apks" value="true" />
-        <option name="test-file-name" value="CtsDramTestCases.apk" />
-    </target_preparer>
-    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
-        <option name="package" value="android.dram.cts" />
-        <option name="runtime-hint" value="8m" />
-    </test>
-</configuration>
diff --git a/tests/dram/jni/Android.mk b/tests/dram/jni/Android.mk
deleted file mode 100644
index 53b9f28..0000000
--- a/tests/dram/jni/Android.mk
+++ /dev/null
@@ -1,31 +0,0 @@
-# Copyright (C) 2012 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.
-#
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE    := libctsdram_jni
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_SRC_FILES := MemoryNativeJni.cpp
-
-LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
-
-LOCAL_SDK_VERSION := 14
-
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/dram/jni/MemoryNativeJni.cpp b/tests/dram/jni/MemoryNativeJni.cpp
deleted file mode 100644
index cbf145c..0000000
--- a/tests/dram/jni/MemoryNativeJni.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-#include <jni.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/time.h>
-
-double currentTimeMillis()
-{
-    struct timeval tv;
-    gettimeofday(&tv, (struct timezone *) NULL);
-    return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
-}
-
-extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemcpy(JNIEnv* env,
-        jclass clazz, jint bufferSize, jint repetition)
-{
-    char* src = new char[bufferSize];
-    char* dst = new char[bufferSize];
-    if ((src == NULL) || (dst == NULL)) {
-        delete[] src;
-        delete[] dst;
-        env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "No memory");
-        return -1;
-    }
-    memset(src, 0, bufferSize);
-    memset(dst, 0, bufferSize);
-    double start = currentTimeMillis();
-    for (int i = 0; i < repetition; i++) {
-        memcpy(dst, src, bufferSize);
-        src[bufferSize - 1] = i & 0xff;
-    }
-    double end = currentTimeMillis();
-    delete[] src;
-    delete[] dst;
-    return end - start;
-}
-
-extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemset(JNIEnv* env,
-        jclass clazz, jint bufferSize, jint repetition, jint c)
-{
-    char* dst = new char[bufferSize];
-    if (dst == NULL) {
-        delete[] dst;
-        env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "No memory");
-        return -1;
-    }
-    memset(dst, 0, bufferSize);
-    double start = currentTimeMillis();
-    for (int i = 0; i < repetition; i++) {
-        memset(dst, (c + i) & 0xff, bufferSize);
-    }
-    double end = currentTimeMillis();
-    delete[] dst;
-    return end - start;
-}
-
diff --git a/tests/dram/src/android/dram/cts/BandwidthTest.java b/tests/dram/src/android/dram/cts/BandwidthTest.java
deleted file mode 100644
index 8b54d90..0000000
--- a/tests/dram/src/android/dram/cts/BandwidthTest.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * Copyright (C) 2012 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 android.dram.cts;
-
-import android.content.Context;
-import android.graphics.Point;
-import android.util.Log;
-import android.view.WindowManager;
-
-import com.android.compatibility.common.util.CtsAndroidTestCase;
-import com.android.compatibility.common.util.DeviceReportLog;
-import com.android.compatibility.common.util.ResultType;
-import com.android.compatibility.common.util.ResultUnit;
-import com.android.compatibility.common.util.Stat;
-
-/**
- * check how many screens the memcpy function can copy in a sec.
- * Note that this does not represent the total memory bandwidth available in the system
- * as typically CPU cannot use the whole bandwidth.
- * Smaller buffers can fit into L1 or L2 cache, which can show big boost.
- */
-public class BandwidthTest extends CtsAndroidTestCase {
-    private static final String TAG = BandwidthTest.class.getSimpleName();
-    private static final String REPORT_LOG_NAME = "CtsDramTestCases";
-    // data length is odd to prevent rare cases that all data are rejected.
-    private static final int MEMCPY_REPETITION = 11;
-    private static final int MEMSET_REPETITION = 31;
-    private static final int REPEAT_IN_EACH_CALL = 100;
-    private static final int KB = 1024;
-    private static final int MB = 1024 * 1024;
-    private static final int MEMSET_CHAR = 0xa5;
-    // reject data outside +/- this value * median
-    private static final double OUTLIER_THRESHOLD = 0.1;
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        // warm-up
-        MemoryNative.runMemcpy(2 * MB, 100);
-    }
-
-    public void testMemcpyK004() {
-        doRunMemcpy(4 * KB);
-    }
-
-    public void testMemcpyK008() {
-        doRunMemcpy(8 * KB);
-    }
-
-    public void testMemcpyK016() {
-        doRunMemcpy(16 * KB);
-    }
-
-    public void testMemcpyK032() {
-        doRunMemcpy(32 * KB);
-    }
-
-    public void testMemcpyK064() {
-        doRunMemcpy(64 * KB);
-    }
-
-    public void testMemcpyK128() {
-        doRunMemcpy(128 * KB);
-    }
-
-    public void testMemcpyK256() {
-        doRunMemcpy(256 * KB);
-    }
-
-    public void testMemcpyK512() {
-        doRunMemcpy(512 * KB);
-    }
-
-    public void testMemcpyM001() {
-        doRunMemcpy(1 * MB);
-    }
-
-    public void testMemcpyM002() {
-        doRunMemcpy(2 * MB);
-    }
-
-    public void testMemcpyM004() {
-        doRunMemcpy(4 * MB);
-    }
-
-    public void testMemcpyM008() {
-        doRunMemcpy(8 * MB);
-    }
-
-    public void testMemcpyM016() {
-        doRunMemcpy(16 * MB);
-    }
-
-    public void testMemsetK004() {
-        doRunMemset(4 * KB);
-    }
-
-    public void testMemsetK008() {
-        doRunMemset(8 * KB);
-    }
-
-    public void testMemsetK016() {
-        doRunMemset(16 * KB);
-    }
-
-    public void testMemsetK032() {
-        doRunMemset(32 * KB);
-    }
-
-    public void testMemsetK064() {
-        doRunMemset(64 * KB);
-    }
-
-    public void testMemsetK128() {
-        doRunMemset(128 * KB);
-    }
-
-    public void testMemsetK256() {
-        doRunMemset(256 * KB);
-    }
-
-    public void testMemsetK512() {
-        doRunMemset(512 * KB);
-    }
-
-    public void testMemsetM001() {
-        doRunMemset(1 * MB);
-    }
-
-    public void testMemsetM002() {
-        doRunMemset(2 * MB);
-    }
-
-    public void testMemsetM004() {
-        doRunMemset(4 * MB);
-    }
-
-    public void testMemsetM008() {
-        doRunMemset(8 * MB);
-    }
-
-    public void testMemsetM016() {
-        doRunMemset(16 * MB);
-    }
-
-    private void doRunMemcpy(int bufferSize) {
-        double[] result = new double[MEMCPY_REPETITION];
-        int repeatInEachCall = REPEAT_IN_EACH_CALL;
-        if (bufferSize < (1 * MB)) {
-            // too small buffer size finishes too early to give accurate result.
-            repeatInEachCall *= (1 * MB / bufferSize);
-        }
-        for (int i = 0; i < MEMCPY_REPETITION; i++) {
-            result[i] = MemoryNative.runMemcpy(bufferSize, repeatInEachCall);
-        }
-        String streamName = "run_memcpy";
-        DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
-        report.addValue("buffer_size", bufferSize, ResultType.NEUTRAL, ResultUnit.NONE);
-        report.addValues("memcpy_time", result, ResultType.LOWER_BETTER, ResultUnit.MS);
-        double[] mbps = Stat.calcRatePerSecArray(
-                (double)bufferSize * repeatInEachCall / 1024.0 / 1024.0, result);
-        report.addValues("memcpy_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
-        Stat.StatResult stat = Stat.getStatWithOutlierRejection(mbps, OUTLIER_THRESHOLD);
-        if (stat.mDataCount != result.length) {
-            Log.w(TAG, "rejecting " + (result.length - stat.mDataCount) + " outliers");
-        }
-        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
-        Point size = new Point();
-        wm.getDefaultDisplay().getSize(size);
-        Log.i(TAG, " x " + size.x + " y " + size.y);
-        double pixels = size.x * size.y;
-        // now this represents how many times the whole screen can be copied in a sec.
-        double screensPerSecAverage = stat.mAverage / pixels * 1024.0 * 1024.0 / 4.0;
-        report.addValue("memcpy_in_fps", screensPerSecAverage, ResultType.HIGHER_BETTER,
-                ResultUnit.FPS);
-        report.setSummary("memcpy_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER,
-                ResultUnit.MBPS);
-        report.submit(getInstrumentation());
-    }
-
-    private void doRunMemset(int bufferSize) {
-        double[] result = new double[MEMSET_REPETITION];
-        int repeatInEachCall = REPEAT_IN_EACH_CALL;
-        if (bufferSize < (1 * MB)) {
-            // too small buffer size finishes too early to give accurate result.
-            repeatInEachCall *= (1 * MB / bufferSize);
-        }
-        for (int i = 0; i < MEMSET_REPETITION; i++) {
-            result[i] = MemoryNative.runMemset(bufferSize, repeatInEachCall, MEMSET_CHAR);
-        }
-        String streamName = "run_memset";
-        DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
-        report.addValue("buffer_size", bufferSize, ResultType.NEUTRAL, ResultUnit.NONE);
-        report.addValues("memset_time", result, ResultType.LOWER_BETTER, ResultUnit.MS);
-        double[] mbps = Stat.calcRatePerSecArray(
-                (double)bufferSize * repeatInEachCall / 1024.0 / 1024.0, result);
-        report.addValues("memset_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
-        Stat.StatResult stat = Stat.getStatWithOutlierRejection(mbps, OUTLIER_THRESHOLD);
-        if (stat.mDataCount != result.length) {
-            Log.w(TAG, "rejecting " + (result.length - stat.mDataCount) + " outliers");
-        }
-        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
-        Point size = new Point();
-        wm.getDefaultDisplay().getSize(size);
-        Log.i(TAG, " x " + size.x + " y " + size.y);
-        double pixels = size.x * size.y;
-        // now this represents how many times the whole screen can be copied in a sec.
-        double screensPerSecAverage = stat.mAverage / pixels * 1024.0 * 1024.0 / 4.0;
-        report.addValue("memset_in_fps", screensPerSecAverage, ResultType.HIGHER_BETTER,
-                ResultUnit.FPS);
-        report.setSummary("memset_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER,
-                ResultUnit.MBPS);
-        report.submit(getInstrumentation());
-    }
-}
diff --git a/tests/dram/src/android/dram/cts/MemoryNative.java b/tests/dram/src/android/dram/cts/MemoryNative.java
deleted file mode 100644
index 7fa4c46..0000000
--- a/tests/dram/src/android/dram/cts/MemoryNative.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2012 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 android.dram.cts;
-
-public class MemoryNative {
-    static {
-        System.loadLibrary("ctsdram_jni");
-    }
-    /**
-     * run memcpy for given number of repetition from a source to a destination buffers
-     * with each having the size of bufferSize.
-     * @param bufferSize
-     * @param repetition
-     * @return time spent in copying in ms.
-     */
-    public static native double runMemcpy(int bufferSize, int repetition);
-
-    /**
-     * run memset for given number of repetition from a source to a destination buffers
-     * with each having the size of bufferSize.
-     * @param bufferSize
-     * @param repetition
-     * @param c char to set. Only LSBs will be used to get char value.
-     * @return time spent in memset in ms.
-     */
-    public static native double runMemset(int bufferSize, int repetition, int c);
-}
diff --git a/tests/tests/bionic/Android.build.copy.libs.mk b/tests/tests/bionic/Android.build.copy.libs.mk
index 7db4b1d..2944911 100644
--- a/tests/tests/bionic/Android.build.copy.libs.mk
+++ b/tests/tests/bionic/Android.build.copy.libs.mk
@@ -84,6 +84,7 @@
   libtest_relo_check_dt_needed_order_1.so \
   libtest_relo_check_dt_needed_order_2.so \
   libtest_simple.so \
+  libtest_thread_local_dtor.so \
   libtest_two_parents_child.so \
   libtest_two_parents_parent1.so \
   libtest_two_parents_parent2.so \
diff --git a/tests/tests/carrierapi/AndroidManifest.xml b/tests/tests/carrierapi/AndroidManifest.xml
index 0338d99..95f55f4 100644
--- a/tests/tests/carrierapi/AndroidManifest.xml
+++ b/tests/tests/carrierapi/AndroidManifest.xml
@@ -17,6 +17,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.carrierapi.cts">
 
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
diff --git a/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java b/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java
new file mode 100644
index 0000000..63f8347
--- /dev/null
+++ b/tests/tests/carrierapi/src/android/carrierapi/cts/NetworkScanApiTest.java
@@ -0,0 +1,258 @@
+/*
+ * 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 android.carrierapi.cts;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Message;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.telephony.CellInfo;
+import android.telephony.CellInfoGsm;
+import android.telephony.CellInfoLte;
+import android.telephony.CellInfoWcdma;
+import android.telephony.NetworkScan;
+import android.telephony.NetworkScanRequest;
+import android.telephony.RadioAccessSpecifier;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.TelephonyManager;
+import android.telephony.TelephonyScanManager;
+import android.util.Log;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Build, install and run the tests by running the commands below:
+ *  make cts -j64
+ *  cts-tradefed run cts -m CtsCarrierApiTestCases --test android.carrierapi.cts.NetworkScanApiTest
+ */
+@RunWith(AndroidJUnit4.class)
+public class NetworkScanApiTest {
+    private TelephonyManager mTelephonyManager;
+    private static final String TAG = "NetworkScanApiTest";
+    private int mNetworkScanStatus;
+    private static final int EVENT_NETWORK_SCAN_START = 100;
+    private static final int EVENT_NETWORK_SCAN_RESULTS = 200;
+    private static final int EVENT_NETWORK_SCAN_ERROR = 300;
+    private static final int EVENT_NETWORK_SCAN_COMPLETED = 400;
+    private List<CellInfo> mScanResults = null;
+    private NetworkScanHandlerThread mTestHandlerThread;
+    private Handler mHandler;
+    private NetworkScanRequest mNetworkScanRequest;
+    private NetworkScanCallbackImpl mNetworkScanCallback;
+    private static final int MAX_INIT_WAIT_MS = 60000; // 60 seconds
+    private Object mLock = new Object();
+    private boolean mReady;
+    private int mErrorCode;
+
+    @Before
+    public void setUp() throws Exception {
+        mTelephonyManager = (TelephonyManager)
+                InstrumentationRegistry.getContext().getSystemService(Context.TELEPHONY_SERVICE);
+        mTestHandlerThread = new NetworkScanHandlerThread(TAG);
+        mTestHandlerThread.start();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mTestHandlerThread.quit();
+    }
+
+    private void waitUntilReady() {
+        synchronized (mLock) {
+            try {
+                mLock.wait(MAX_INIT_WAIT_MS);
+            } catch (InterruptedException ie) {
+            }
+
+            if (!mReady) {
+                fail("NetworkScanApiTest failed to initialize");
+            }
+        }
+    }
+
+    private void setReady(boolean ready) {
+        synchronized (mLock) {
+            mReady = ready;
+            mLock.notifyAll();
+        }
+    }
+
+    private class NetworkScanHandlerThread extends HandlerThread {
+
+        public NetworkScanHandlerThread(String name) {
+            super(name);
+        }
+
+        @Override
+        public void onLooperPrepared() {
+            /* create a custom handler for the Handler Thread */
+            mHandler = new Handler(mTestHandlerThread.getLooper()) {
+                @Override
+                public void handleMessage(Message msg) {
+                    switch (msg.what) {
+                        case EVENT_NETWORK_SCAN_START:
+                            Log.d(TAG, "request network scan");
+                            mTelephonyManager.requestNetworkScan(
+                                    mNetworkScanRequest, mNetworkScanCallback);
+                            break;
+                        default:
+                            Log.d(TAG, "Unknown Event " + msg.what);
+                    }
+                }
+            };
+        }
+    }
+
+    private class NetworkScanCallbackImpl extends TelephonyScanManager.NetworkScanCallback {
+        @Override
+        public void onResults(List<CellInfo> results) {
+            Log.d(TAG, "onResults: " + results.toString());
+            mNetworkScanStatus = EVENT_NETWORK_SCAN_RESULTS;
+            mScanResults = results;
+        }
+
+        @Override
+        public void onComplete() {
+            Log.d(TAG, "onComplete");
+            mNetworkScanStatus = EVENT_NETWORK_SCAN_COMPLETED;
+            setReady(true);
+        }
+
+        @Override
+        public void onError(int error) {
+            Log.d(TAG, "onError: " + String.valueOf(error));
+            mNetworkScanStatus = EVENT_NETWORK_SCAN_ERROR;
+            mErrorCode = error;
+            setReady(true);
+        }
+    }
+
+    private RadioAccessSpecifier getRadioAccessSpecifier(CellInfo cellInfo) {
+        RadioAccessSpecifier ras;
+        if (cellInfo instanceof CellInfoLte) {
+            int ranLte = AccessNetworkConstants.AccessNetworkType.EUTRAN;
+            int[] lteChannels = {((CellInfoLte) cellInfo).getCellIdentity().getEarfcn()};
+            ras = new RadioAccessSpecifier(ranLte, null /* bands */, lteChannels);
+            Log.d(TAG, "CellInfoLte channel: " + lteChannels[0]);
+        } else if (cellInfo instanceof CellInfoWcdma) {
+            int ranLte = AccessNetworkConstants.AccessNetworkType.UTRAN;
+            int[] wcdmaChannels = {((CellInfoWcdma) cellInfo).getCellIdentity().getUarfcn()};
+            ras = new RadioAccessSpecifier(ranLte, null /* bands */, wcdmaChannels);
+            Log.d(TAG, "CellInfoWcdma channel: " + wcdmaChannels[0]);
+        } else if (cellInfo instanceof CellInfoGsm) {
+            int ranGsm = AccessNetworkConstants.AccessNetworkType.GERAN;
+            int[] gsmChannels = {((CellInfoGsm) cellInfo).getCellIdentity().getArfcn()};
+            ras = new RadioAccessSpecifier(ranGsm, null /* bands */, gsmChannels);
+            Log.d(TAG, "CellInfoGsm channel: " + gsmChannels[0]);
+        } else {
+            ras = null;
+        }
+        return ras;
+    }
+
+    /**
+     * Tests that the device properly requests a network scan.
+     */
+    @Test
+    public void testRequestNetworkScan() throws InterruptedException {
+        if (!mTelephonyManager.hasCarrierPrivileges()) {
+            fail("This test requires a SIM card with carrier privilege rule on it.");
+        }
+
+        // Make sure that there should be at least one entry.
+        List<CellInfo> allCellInfo = mTelephonyManager.getAllCellInfo();
+        Log.d(TAG, "allCellInfo: " + allCellInfo.toString());
+        if (allCellInfo == null) {
+            fail("TelephonyManager.getAllCellInfo() returned NULL!");
+        }
+        if (allCellInfo.size() == 0) {
+            fail("TelephonyManager.getAllCellInfo() returned zero-length list!");
+        }
+
+        // Construct a NetworkScanRequest
+        List<RadioAccessSpecifier> radioAccessSpecifier = new ArrayList<>();
+        for (int i = 0; i < allCellInfo.size(); i++) {
+            RadioAccessSpecifier ras = getRadioAccessSpecifier(allCellInfo.get(i));
+            if (ras != null) {
+                radioAccessSpecifier.add(ras);
+            }
+        }
+        if (radioAccessSpecifier.size() == 0) {
+            RadioAccessSpecifier gsm = new RadioAccessSpecifier(
+                    AccessNetworkConstants.AccessNetworkType.GERAN,
+                    null /* bands */,
+                    null /* channels */);
+            RadioAccessSpecifier lte = new RadioAccessSpecifier(
+                    AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                    null /* bands */,
+                    null /* channels */);
+            RadioAccessSpecifier wcdma = new RadioAccessSpecifier(
+                    AccessNetworkConstants.AccessNetworkType.UTRAN,
+                    null /* bands */,
+                    null /* channels */);
+            radioAccessSpecifier.add(gsm);
+            radioAccessSpecifier.add(lte);
+            radioAccessSpecifier.add(wcdma);
+        }
+        RadioAccessSpecifier[] radioAccessSpecifierArray =
+                new RadioAccessSpecifier[radioAccessSpecifier.size()];
+        mNetworkScanRequest = new NetworkScanRequest(
+                NetworkScanRequest.SCAN_TYPE_ONE_SHOT /* scan type */,
+                radioAccessSpecifier.toArray(radioAccessSpecifierArray),
+                5 /* search periodicity */,
+                60 /* max search time */,
+                true /*enable incremental results*/,
+                5 /* incremental results periodicity */,
+                null /* List of PLMN ids (MCC-MNC) */);
+
+        mNetworkScanCallback = new NetworkScanCallbackImpl();
+        Message startNetworkScan = mHandler.obtainMessage(EVENT_NETWORK_SCAN_START);
+        setReady(false);
+        startNetworkScan.sendToTarget();
+        waitUntilReady();
+
+        Log.d(TAG, "mNetworkScanStatus: " + mNetworkScanStatus);
+        assertTrue("The final scan status is not ScanCompleted or ScanError with an error "
+                        + "code ERROR_MODEM_UNAVAILABLE or ERROR_UNSUPPORTED",
+                isScanStatusValid());
+    }
+
+    private boolean isScanStatusValid() {
+        // TODO(b/72162885): test the size of ScanResults is not zero after the blocking bug fixed.
+        if ((mNetworkScanStatus == EVENT_NETWORK_SCAN_COMPLETED) && (mScanResults != null)) {
+            // Scan complete.
+            return true;
+        }
+        if ((mNetworkScanStatus == EVENT_NETWORK_SCAN_ERROR)
+                && ((mErrorCode == NetworkScan.ERROR_MODEM_UNAVAILABLE)
+                || (mErrorCode == NetworkScan.ERROR_UNSUPPORTED))) {
+            // Scan error but the error type is allowed.
+            return true;
+        }
+        return false;
+    }
+}
diff --git a/tests/tests/hardware/src/android/hardware/cts/GlUtils.java b/tests/tests/hardware/src/android/hardware/cts/GlUtils.java
index 81628d4..bd5cecb 100644
--- a/tests/tests/hardware/src/android/hardware/cts/GlUtils.java
+++ b/tests/tests/hardware/src/android/hardware/cts/GlUtils.java
@@ -17,6 +17,7 @@
 package android.hardware.cts;
 
 import android.opengl.GLES20;
+import android.util.Pair;
 
 import java.util.Arrays;
 import java.util.regex.Matcher;
@@ -27,6 +28,10 @@
     }
 
     static int getMajorVersion() {
+        return getVersion().first;
+    }
+
+    static Pair<Integer, Integer> getVersion() {
         // Section 6.1.5 of the OpenGL ES specification indicates the GL version
         // string strictly follows this format:
         //
@@ -42,9 +47,10 @@
         Pattern pattern = Pattern.compile("OpenGL ES ([0-9]+)\\.([0-9]+)");
         Matcher matcher = pattern.matcher(version);
         if (matcher.find()) {
-            return Integer.parseInt(matcher.group(1));
+            return new Pair<>(
+                    Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)));
         }
-        return 2;
+        return new Pair<>(2, 0);
     }
 
     static String[] getExtensions() {
diff --git a/tests/tests/hardware/src/android/hardware/cts/HardwareBufferTest.java b/tests/tests/hardware/src/android/hardware/cts/HardwareBufferTest.java
index 5ea845a..6276959 100644
--- a/tests/tests/hardware/src/android/hardware/cts/HardwareBufferTest.java
+++ b/tests/tests/hardware/src/android/hardware/cts/HardwareBufferTest.java
@@ -24,6 +24,7 @@
 import android.opengl.EGLSurface;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
+import android.util.Pair;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -70,8 +71,10 @@
             }, 0);
             EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
 
-            sHasFloatBuffers = GlUtils.getMajorVersion() >= 3 ||
-                    GlUtils.hasExtensions("GL_OES_texture_half_float",
+            Pair<Integer, Integer> version = GlUtils.getVersion();
+            sHasFloatBuffers = (version.first >= 3 && version.second >= 2) ||
+                    GlUtils.hasExtensions(
+                            "GL_OES_texture_half_float",
                             "GL_OES_texture_half_float_linear");
 
             EGL14.eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
diff --git a/tests/tests/jvmti/attaching/Android.mk b/tests/tests/jvmti/attaching/Android.mk
index dc13854..56169d4 100644
--- a/tests/tests/jvmti/attaching/Android.mk
+++ b/tests/tests/jvmti/attaching/Android.mk
@@ -25,7 +25,10 @@
 LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner android-support-test
 
 LOCAL_MULTILIB := both
-LOCAL_JNI_SHARED_LIBRARIES := libjvmtiattachingtestagent
+LOCAL_JNI_SHARED_LIBRARIES := libjvmtiattachingtestagent1 \
+                              libjvmtiattachingtestagent2 \
+                              libjvmtiattachingtestagent3 \
+                              libjvmtiattachingtestagent4 \
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/tests/tests/jvmti/attaching/jni/Android.mk b/tests/tests/jvmti/attaching/jni/Android.mk
index 8f364e2..b0d8376 100644
--- a/tests/tests/jvmti/attaching/jni/Android.mk
+++ b/tests/tests/jvmti/attaching/jni/Android.mk
@@ -18,9 +18,10 @@
 
 LOCAL_PATH:= $(call my-dir)
 
+define jvmti-attaching-test-agent
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := libjvmtiattachingtestagent
+LOCAL_MODULE := libjvmtiattachingtestagent$1
 
 LOCAL_MODULE_TAGS := optional
 
@@ -29,8 +30,15 @@
 LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
 
 LOCAL_SDK_VERSION := current
-LOCAL_NDK_STL_VARIANT := c++_static
 
 LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
 
+LOCAL_CFLAGS += -DAGENT_NR=$1
+
 include $(BUILD_SHARED_LIBRARY)
+endef
+
+$(eval $(call jvmti-attaching-test-agent,1))
+$(eval $(call jvmti-attaching-test-agent,2))
+$(eval $(call jvmti-attaching-test-agent,3))
+$(eval $(call jvmti-attaching-test-agent,4))
diff --git a/tests/tests/jvmti/attaching/jni/agent.c b/tests/tests/jvmti/attaching/jni/agent.c
index 1b1d6df..3ebca31 100644
--- a/tests/tests/jvmti/attaching/jni/agent.c
+++ b/tests/tests/jvmti/attaching/jni/agent.c
@@ -22,9 +22,7 @@
 static bool sIsAttached = false;
 
 jint
-Agent_OnAttach(JavaVM* vm,
-               char* options,
-               void* reserved) {
+Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
     if (options != NULL && options[0] == 'a') {
         sIsAttached = true;
         return JNI_OK;
@@ -33,12 +31,18 @@
     }
 }
 
-JNIEXPORT jboolean JNICALL
-Java_android_jvmti_attaching_cts_AttachingTest_isAttached(JNIEnv *env,
-                                                          jclass klass) {
+#ifndef AGENT_NR
+#error "Missing AGENT_NR"
+#endif
+#define CONCAT(A,B) A ## B
+#define EVAL(A,B) CONCAT(A,B)
+#define NAME(BASE) EVAL(BASE,AGENT_NR)
+
+JNIEXPORT jboolean JNICALL NAME(Java_android_jvmti_attaching_cts_AttachingTest_isAttached) (
+        JNIEnv* env, jclass klass) {
     if (sIsAttached) {
         return JNI_TRUE;
     } else {
         return JNI_FALSE;
     }
-}
\ No newline at end of file
+}
diff --git a/tests/tests/jvmti/attaching/src/android.jvmti.attaching.cts/AttachingTest.java b/tests/tests/jvmti/attaching/src/android.jvmti.attaching.cts/AttachingTest.java
index 27f4131..994681f 100644
--- a/tests/tests/jvmti/attaching/src/android.jvmti.attaching.cts/AttachingTest.java
+++ b/tests/tests/jvmti/attaching/src/android.jvmti.attaching.cts/AttachingTest.java
@@ -23,9 +23,11 @@
 
 import dalvik.system.BaseDexClassLoader;
 
-import org.junit.BeforeClass;
+import org.junit.AfterClass;
 import org.junit.FixMethodOrder;
 import org.junit.Test;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
 import org.junit.runner.RunWith;
 import org.junit.runners.MethodSorters;
 
@@ -35,20 +37,110 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.concurrent.Callable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
-@RunWith(AndroidJUnit4.class)
+@RunWith(Parameterized.class)
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class AttachingTest {
-    private static String sAgentFile;
+    // Some static stored state, for final cleanup.
+    private static Set<File> createdFiles = new HashSet<>();
 
-    @BeforeClass
-    public static void copyAgentToFile() throws Exception {
+    // Parameters for a test instance.
+
+    // The string to pass as the agent parameter.
+    private String agentString;
+    // The classloader to pass.
+    private ClassLoader classLoader;
+    // The attach-success function for the last test.
+    private Callable<Boolean> isAttachedFn;
+
+    public AttachingTest(String agentString, ClassLoader classLoader,
+            Callable<Boolean> isAttachedFn) {
+        this.agentString = agentString;
+        this.classLoader = classLoader;
+        this.isAttachedFn = isAttachedFn;
+    }
+
+    @Parameters
+    public static Collection<Object[]> data() {
+        Collection<Object[]> params = new ArrayList<>();
+
+        try {
+            // Test that an absolute path works w/o given classloader.
+            File agentExtracted = copyAgentToFile("jvmtiattachingtestagent1");
+            Callable<Boolean> success = AttachingTest::isAttached1;
+            params.add(new Object[] {
+                agentExtracted.getAbsolutePath(),
+                null,
+                success,
+            });
+            createdFiles.add(agentExtracted);
+        } catch (Exception exc) {
+            throw new RuntimeException(exc);
+        }
+
+        try {
+            // Test that an absolute path works w/ given classloader.
+            File agentExtracted = copyAgentToFile("jvmtiattachingtestagent2");
+            Callable<Boolean> success = AttachingTest::isAttached2;
+            params.add(new Object[] {
+                agentExtracted.getAbsolutePath(),
+                AttachingTest.class.getClassLoader(),
+                success,
+            });
+            createdFiles.add(agentExtracted);
+        } catch (Exception exc) {
+            throw new RuntimeException(exc);
+        }
+
+        {
+            // Test that a relative path works w/ given classloader.
+            Callable<Boolean> success = AttachingTest::isAttached3;
+            params.add(new Object[] {
+                "libjvmtiattachingtestagent3.so",
+                AttachingTest.class.getClassLoader(),
+                success,
+            });
+        }
+
+        try {
+            // The name part of an extracted lib should not work.
+            File agentExtracted = copyAgentToFile("jvmtiattachingtestagent4");
+            String name = agentExtracted.getName();
+            Callable<Boolean> success = () -> {
+                try {
+                    isAttached4();
+                    // Any result is a failure.
+                    return false;
+                } catch (UnsatisfiedLinkError e) {
+                    return true;
+                }
+            };
+            params.add(new Object[] {
+                name,
+                AttachingTest.class.getClassLoader(),
+                success,
+            });
+            createdFiles.add(agentExtracted);
+        } catch (Exception exc) {
+            throw new RuntimeException(exc);
+        }
+
+        return params;
+    }
+
+    private static File copyAgentToFile(String lib) throws Exception {
         ClassLoader cl = AttachingTest.class.getClassLoader();
         assertTrue(cl instanceof BaseDexClassLoader);
 
         File copiedAgent = File.createTempFile("agent", ".so");
         try (InputStream is = new FileInputStream(
-                ((BaseDexClassLoader) cl).findLibrary("jvmtiattachingtestagent"))) {
+                ((BaseDexClassLoader) cl).findLibrary(lib))) {
             try (OutputStream os = new FileOutputStream(copiedAgent)) {
                 byte[] buffer = new byte[64 * 1024];
 
@@ -62,45 +154,70 @@
             }
         }
 
-        sAgentFile = copiedAgent.getAbsolutePath();
+        return copiedAgent;
     }
 
+    @AfterClass
+    public static void cleanupExtractedAgents() throws Exception {
+        for (File f : createdFiles) {
+            f.delete();
+        }
+        createdFiles.clear();
+    }
+
+    // Tests.
+
+    // This will be repeated unnecessarily, but that's OK.
     @Test(expected = IOException.class)
     public void a_attachInvalidAgent() throws Exception {
-        Debug.attachJvmtiAgent(File.createTempFile("badAgent", ".so").getAbsolutePath(), null);
+        File tmpFile = File.createTempFile("badAgent", ".so");
+        createdFiles.add(tmpFile);
+        Debug.attachJvmtiAgent(tmpFile.getAbsolutePath(), null, classLoader);
     }
 
     @Test(expected = IOException.class)
     public void a_attachInvalidPath() throws Exception {
-        Debug.attachJvmtiAgent(sAgentFile + ".invalid", null);
+        Debug.attachJvmtiAgent(agentString + ".invalid", null, classLoader);
     }
 
     @Test(expected = NullPointerException.class)
     public void a_attachNullAgent() throws Exception {
-        Debug.attachJvmtiAgent(null, null);
+        Debug.attachJvmtiAgent(null, null, classLoader);
     }
 
+    // This will be repeated unnecessarily, but that's OK.
     @Test(expected = IllegalArgumentException.class)
     public void a_attachWithEquals() throws Exception {
-        Debug.attachJvmtiAgent(File.createTempFile("=", ".so").getAbsolutePath(), null);
+        File tmpFile = File.createTempFile("=", ".so");
+        createdFiles.add(tmpFile);
+        Debug.attachJvmtiAgent(tmpFile.getAbsolutePath(), null, classLoader);
     }
 
     @Test(expected = IOException.class)
     public void a_attachWithNullOptions() throws Exception {
-        Debug.attachJvmtiAgent(sAgentFile, null);
+        Debug.attachJvmtiAgent(agentString, null, classLoader);
     }
 
     @Test(expected = IOException.class)
     public void a_attachWithBadOptions() throws Exception {
-        Debug.attachJvmtiAgent(sAgentFile, "b");
+        Debug.attachJvmtiAgent(agentString, "b", classLoader);
     }
 
     @Test
     public void b_attach() throws Exception {
-        Debug.attachJvmtiAgent(sAgentFile, "a");
+        try {
+            Debug.attachJvmtiAgent(agentString, "a", classLoader);
+        } catch (Throwable t) {
+            // Ignored.
+        }
 
-        assertTrue(isAttached());
+        assertTrue(isAttachedFn.call());
     }
 
-    native static boolean isAttached();
+    // Functions the agents can bind to.
+
+    native static boolean isAttached1();
+    native static boolean isAttached2();
+    native static boolean isAttached3();
+    native static boolean isAttached4();
 }
diff --git a/tests/tests/net/src/android/net/cts/IpSecManagerTest.java b/tests/tests/net/src/android/net/cts/IpSecManagerTest.java
index 8e5d256..0ef3792 100644
--- a/tests/tests/net/src/android/net/cts/IpSecManagerTest.java
+++ b/tests/tests/net/src/android/net/cts/IpSecManagerTest.java
@@ -35,7 +35,9 @@
 
 import java.io.FileDescriptor;
 import java.io.IOException;
+import java.net.DatagramPacket;
 import java.net.DatagramSocket;
+import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -61,7 +63,6 @@
 
     private static final InetAddress GOOGLE_DNS_4 = IpAddress("8.8.8.8");
     private static final InetAddress GOOGLE_DNS_6 = IpAddress("2001:4860:4860::8888");
-    private static final InetAddress LOOPBACK_4 = IpAddress("127.0.0.1");
 
     private static final InetAddress[] GOOGLE_DNS_LIST =
             new InetAddress[] {GOOGLE_DNS_4, GOOGLE_DNS_6};
@@ -110,20 +111,17 @@
     public void testAllocSpi() throws Exception {
         for (InetAddress addr : GOOGLE_DNS_LIST) {
             IpSecManager.SecurityParameterIndex randomSpi = null, droidSpi = null;
-            randomSpi = mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_OUT, addr);
+            randomSpi = mISM.allocateSecurityParameterIndex(addr);
             assertTrue(
                     "Failed to receive a valid SPI",
                     randomSpi.getSpi() != IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
 
-            droidSpi =
-                    mISM.allocateSecurityParameterIndex(
-                            IpSecTransform.DIRECTION_IN, addr, DROID_SPI);
-            assertTrue(
-                    "Failed to allocate specified SPI, " + DROID_SPI,
+            droidSpi = mISM.allocateSecurityParameterIndex(addr, DROID_SPI);
+            assertTrue("Failed to allocate specified SPI, " + DROID_SPI,
                     droidSpi.getSpi() == DROID_SPI);
 
             try {
-                mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_IN, addr, DROID_SPI);
+                mISM.allocateSecurityParameterIndex(addr, DROID_SPI);
                 fail("Duplicate SPI was allowed to be created");
             } catch (IpSecManager.SpiUnavailableException expected) {
                 // This is a success case because we expect a dupe SPI to throw
@@ -197,7 +195,8 @@
             localPort = getPort(udpSocket);
         }
 
-        mISM.applyTransportModeTransform(udpSocket, transform);
+        mISM.applyTransportModeTransform(udpSocket, IpSecManager.DIRECTION_IN, transform);
+        mISM.applyTransportModeTransform(udpSocket, IpSecManager.DIRECTION_OUT, transform);
 
         for (int i = 0; i < sendCount; i++) {
             byte[] in = new byte[TEST_DATA.length];
@@ -206,7 +205,7 @@
             assertArrayEquals("Encapsulated data did not match.", TEST_DATA, in);
         }
 
-        mISM.removeTransportModeTransform(udpSocket, transform);
+        mISM.removeTransportModeTransforms(udpSocket);
         Os.close(udpSocket);
     }
 
@@ -236,14 +235,17 @@
         Os.bind(server, local, 0);
         int port = ((InetSocketAddress) Os.getsockname(server)).getPort();
 
-        mISM.applyTransportModeTransform(client, transform);
-        mISM.applyTransportModeTransform(server, transform);
+        mISM.applyTransportModeTransform(client, IpSecManager.DIRECTION_IN, transform);
+        mISM.applyTransportModeTransform(client, IpSecManager.DIRECTION_OUT, transform);
+        mISM.applyTransportModeTransform(server, IpSecManager.DIRECTION_IN, transform);
+        mISM.applyTransportModeTransform(server, IpSecManager.DIRECTION_OUT, transform);
 
         Os.listen(server, 10);
         Os.connect(client, local, port);
         FileDescriptor accepted = Os.accept(server, null);
 
-        mISM.applyTransportModeTransform(accepted, transform);
+        mISM.applyTransportModeTransform(accepted, IpSecManager.DIRECTION_IN, transform);
+        mISM.applyTransportModeTransform(accepted, IpSecManager.DIRECTION_OUT, transform);
 
         // Wait for TCP handshake packets to be counted
         StatsChecker.waitForNumPackets(3); // (SYN, SYN+ACK, ACK)
@@ -271,9 +273,9 @@
             StatsChecker.waitForNumPackets(4 * (i + 1));
         }
 
-        mISM.removeTransportModeTransform(server, transform);
-        mISM.removeTransportModeTransform(client, transform);
-        mISM.removeTransportModeTransform(accepted, transform);
+        mISM.removeTransportModeTransforms(server);
+        mISM.removeTransportModeTransforms(client);
+        mISM.removeTransportModeTransforms(accepted);
 
         Os.close(server);
         Os.close(client);
@@ -291,54 +293,71 @@
      * send data (expect exception)
      */
     public void testCreateTransform() throws Exception {
-        InetAddress local = LOOPBACK_4;
-        IpSecManager.SecurityParameterIndex outSpi =
-                mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_OUT, local);
-
-        IpSecManager.SecurityParameterIndex inSpi =
-                mISM.allocateSecurityParameterIndex(
-                        IpSecTransform.DIRECTION_IN, local, outSpi.getSpi());
+        InetAddress localAddr = InetAddress.getByName(IPV4_LOOPBACK);
+        IpSecManager.SecurityParameterIndex spi =
+                mISM.allocateSecurityParameterIndex(localAddr);
 
         IpSecTransform transform =
                 new IpSecTransform.Builder(mContext)
-                        .setSpi(IpSecTransform.DIRECTION_OUT, outSpi)
                         .setEncryption(
-                                IpSecTransform.DIRECTION_OUT,
                                 new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY))
                         .setAuthentication(
-                                IpSecTransform.DIRECTION_OUT,
                                 new IpSecAlgorithm(
                                         IpSecAlgorithm.AUTH_HMAC_SHA256,
                                         AUTH_KEY,
                                         AUTH_KEY.length * 8))
-                        .setSpi(IpSecTransform.DIRECTION_IN, inSpi)
-                        .setEncryption(
-                                IpSecTransform.DIRECTION_IN,
-                                new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY))
-                        .setAuthentication(
-                                IpSecTransform.DIRECTION_IN,
-                                new IpSecAlgorithm(
-                                        IpSecAlgorithm.AUTH_HMAC_SHA256,
-                                        AUTH_KEY,
-                                        CRYPT_KEY.length * 8))
-                        .buildTransportModeTransform(local);
+                        .buildTransportModeTransform(localAddr, spi);
 
-        // Bind localSocket to a random available port.
-        DatagramSocket localSocket = new DatagramSocket(0);
-        int localPort = localSocket.getLocalPort();
-        localSocket.setSoTimeout(500);
-        ParcelFileDescriptor pin = ParcelFileDescriptor.fromDatagramSocket(localSocket);
-        FileDescriptor udpSocket = pin.getFileDescriptor();
-
-        mISM.applyTransportModeTransform(udpSocket, transform);
-        byte[] data = new String("Best test data ever!").getBytes("UTF-8");
+        final boolean [][] applyInApplyOut = {
+                {false, false}, {false, true}, {true, false}, {true,true}};
+        final byte[] data = new String("Best test data ever!").getBytes("UTF-8");
+        final DatagramPacket outPacket = new DatagramPacket(data, 0, data.length, localAddr, 0);
 
         byte[] in = new byte[data.length];
-        Os.sendto(udpSocket, data, 0, data.length, 0, local, localPort);
-        Os.read(udpSocket, in, 0, in.length);
-        assertTrue("Encapsulated data did not match.", Arrays.equals(data, in));
-        mISM.removeTransportModeTransform(udpSocket, transform);
-        Os.close(udpSocket);
+        DatagramPacket inPacket = new DatagramPacket(in, in.length);
+        DatagramSocket localSocket;
+        int localPort;
+
+        for(boolean[] io : applyInApplyOut) {
+            boolean applyIn = io[0];
+            boolean applyOut = io[1];
+            // Bind localSocket to a random available port.
+            localSocket = new DatagramSocket(0);
+            localPort = localSocket.getLocalPort();
+            localSocket.setSoTimeout(200);
+            outPacket.setPort(localPort);
+            if (applyIn) {
+                mISM.applyTransportModeTransform(
+                        localSocket, IpSecManager.DIRECTION_IN, transform);
+            }
+            if (applyOut) {
+                mISM.applyTransportModeTransform(
+                        localSocket, IpSecManager.DIRECTION_OUT, transform);
+            }
+            if (applyIn == applyOut) {
+                localSocket.send(outPacket);
+                localSocket.receive(inPacket);
+                assertTrue("Encapsulated data did not match.",
+                        Arrays.equals(outPacket.getData(), inPacket.getData()));
+                mISM.removeTransportModeTransforms(localSocket);
+                localSocket.close();
+            } else {
+                try {
+                    localSocket.send(outPacket);
+                    localSocket.receive(inPacket);
+                } catch (IOException e) {
+                    continue;
+                } finally {
+                    mISM.removeTransportModeTransforms(localSocket);
+                    localSocket.close();
+                }
+                // FIXME: This check is disabled because sockets currently receive data
+                // if there is a valid SA for decryption, even when the input policy is
+                // not applied to a socket.
+                //  fail("Data IO should fail on asymmetrical transforms! + Input="
+                //          + applyIn + " Output=" + applyOut);
+            }
+        }
         transform.close();
     }
 
@@ -476,20 +495,13 @@
         InetAddress local = InetAddress.getByName(localAddress);
 
         try (IpSecManager.UdpEncapsulationSocket encapSocket = mISM.openUdpEncapsulationSocket();
-                IpSecManager.SecurityParameterIndex outSpi =
-                        mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_OUT, local);
-                IpSecManager.SecurityParameterIndex inSpi =
-                        mISM.allocateSecurityParameterIndex(
-                                IpSecTransform.DIRECTION_IN, local, outSpi.getSpi())) {
+                IpSecManager.SecurityParameterIndex spi =
+                        mISM.allocateSecurityParameterIndex(local)) {
 
             IpSecTransform.Builder transformBuilder =
                     new IpSecTransform.Builder(mContext)
-                            .setSpi(IpSecTransform.DIRECTION_OUT, outSpi)
-                            .setEncryption(IpSecTransform.DIRECTION_OUT, crypt)
-                            .setAuthentication(IpSecTransform.DIRECTION_OUT, auth)
-                            .setSpi(IpSecTransform.DIRECTION_IN, inSpi)
-                            .setEncryption(IpSecTransform.DIRECTION_IN, crypt)
-                            .setAuthentication(IpSecTransform.DIRECTION_IN, auth);
+                            .setEncryption(crypt)
+                            .setAuthentication(auth);
 
             if (doUdpEncap) {
                 transformBuilder =
@@ -500,7 +512,8 @@
             int transportHdrLen = 0;
             int udpEncapLen = doUdpEncap ? UDP_HDRLEN : 0;
 
-            try (IpSecTransform transform = transformBuilder.buildTransportModeTransform(local)) {
+            try (IpSecTransform transform =
+                        transformBuilder.buildTransportModeTransform(local, spi)) {
                 if (protocol == IPPROTO_TCP) {
                     transportHdrLen = TCP_HDRLEN_WITH_OPTIONS;
                     checkTcp(transform, local, sendCount, useJavaSockets);
@@ -920,19 +933,16 @@
     }
 
     public void testUdpEncapsulation() throws Exception {
-        InetAddress local = LOOPBACK_4;
+        InetAddress local = InetAddress.getByName(IPV4_LOOPBACK);
 
         // TODO: Refactor to make this more representative of a normal application use case. (use
         // separate sockets for inbound and outbound)
         // Create SPIs, UDP encap socket
         try (IpSecManager.UdpEncapsulationSocket encapSocket = mISM.openUdpEncapsulationSocket();
-                IpSecManager.SecurityParameterIndex outSpi =
-                        mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_OUT, local);
-                IpSecManager.SecurityParameterIndex inSpi =
-                        mISM.allocateSecurityParameterIndex(
-                                IpSecTransform.DIRECTION_IN, local, outSpi.getSpi());
+                IpSecManager.SecurityParameterIndex spi =
+                        mISM.allocateSecurityParameterIndex(local);
                 IpSecTransform transform =
-                        buildIpSecTransform(mContext, inSpi, outSpi, encapSocket, local)) {
+                        buildIpSecTransform(mContext, spi, encapSocket, local)) {
 
             // Create user socket, apply transform to it
             FileDescriptor udpSocket = null;
@@ -940,7 +950,10 @@
                 udpSocket = getBoundUdpSocket(local);
                 int port = getPort(udpSocket);
 
-                mISM.applyTransportModeTransform(udpSocket, transform);
+                mISM.applyTransportModeTransform(
+                        udpSocket, IpSecManager.DIRECTION_IN, transform);
+                mISM.applyTransportModeTransform(
+                        udpSocket, IpSecManager.DIRECTION_OUT, transform);
 
                 // Send an ESP packet from this socket to itself. Since the inbound and
                 // outbound transforms match, we should receive the data we sent.
@@ -970,7 +983,7 @@
                         "Encap socket was unable to send/receive IKE data",
                         Arrays.equals(data, in));
 
-                mISM.removeTransportModeTransform(udpSocket, transform);
+                mISM.removeTransportModeTransforms(udpSocket);
             } finally {
                 if (udpSocket != null) {
                     Os.close(udpSocket);
@@ -980,26 +993,25 @@
     }
 
     public void testIke() throws Exception {
-        InetAddress local = LOOPBACK_4;
+        InetAddress localAddr = InetAddress.getByName(IPV4_LOOPBACK);
 
         // TODO: Refactor to make this more representative of a normal application use case. (use
         // separate sockets for inbound and outbound)
         try (IpSecManager.UdpEncapsulationSocket encapSocket = mISM.openUdpEncapsulationSocket();
-                IpSecManager.SecurityParameterIndex outSpi =
-                        mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_OUT, local);
-                IpSecManager.SecurityParameterIndex inSpi =
-                        mISM.allocateSecurityParameterIndex(IpSecTransform.DIRECTION_IN, local);
+                IpSecManager.SecurityParameterIndex spi =
+                        mISM.allocateSecurityParameterIndex(localAddr);
                 IpSecTransform transform =
-                        buildIpSecTransform(mContext, inSpi, outSpi, encapSocket, local)) {
+                        buildIpSecTransform(mContext, spi, encapSocket, localAddr)) {
 
             // Create user socket, apply transform to it
             FileDescriptor sock = null;
 
             try {
-                sock = getBoundUdpSocket(local);
+                sock = getBoundUdpSocket(localAddr);
                 int port = getPort(sock);
 
-                mISM.applyTransportModeTransform(sock, transform);
+                mISM.applyTransportModeTransform(sock, IpSecManager.DIRECTION_IN, transform);
+                mISM.applyTransportModeTransform(sock, IpSecManager.DIRECTION_OUT, transform);
 
                 // TODO: Find a way to set a timeout on the socket, and assert the ESP packet
                 // doesn't make it through. Setting sockopts currently throws EPERM (possibly
@@ -1010,7 +1022,7 @@
                 byte[] header = new byte[] {1, 1, 1, 1};
                 String message = "Sample ESP Packet";
                 byte[] data = (new String(header) + message).getBytes("UTF-8");
-                Os.sendto(sock, data, 0, data.length, 0, local, encapSocket.getPort());
+                Os.sendto(sock, data, 0, data.length, 0, localAddr, encapSocket.getPort());
 
                 // Send IKE packet from the encap socket to itself. Since IKE is not
                 // transformed in any way, this should succeed.
@@ -1023,7 +1035,7 @@
                         0,
                         data.length,
                         0,
-                        local,
+                        localAddr,
                         encapSocket.getPort());
 
                 // ESP data should be dropped, due to different input SPI (as opposed to being
@@ -1038,7 +1050,7 @@
                         "Encap socket received UDP-encap-ESP data despite invalid SPIs",
                         Arrays.equals(header, in));
 
-                mISM.removeTransportModeTransform(sock, transform);
+                mISM.removeTransportModeTransforms(sock);
             } finally {
                 if (sock != null) {
                     Os.close(sock);
@@ -1049,30 +1061,20 @@
 
     private static IpSecTransform buildIpSecTransform(
             Context mContext,
-            IpSecManager.SecurityParameterIndex inSpi,
-            IpSecManager.SecurityParameterIndex outSpi,
+            IpSecManager.SecurityParameterIndex spi,
             IpSecManager.UdpEncapsulationSocket encapSocket,
             InetAddress remoteAddr)
             throws Exception {
+            String localAddr = (remoteAddr instanceof Inet4Address)
+                    ? IPV4_LOOPBACK : IPV6_LOOPBACK;
         return new IpSecTransform.Builder(mContext)
-                .setSpi(IpSecTransform.DIRECTION_IN, inSpi)
-                .setSpi(IpSecTransform.DIRECTION_OUT, outSpi)
                 .setEncryption(
-                        IpSecTransform.DIRECTION_IN,
-                        new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY))
-                .setEncryption(
-                        IpSecTransform.DIRECTION_OUT,
                         new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY))
                 .setAuthentication(
-                        IpSecTransform.DIRECTION_IN,
-                        new IpSecAlgorithm(
-                                IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4))
-                .setAuthentication(
-                        IpSecTransform.DIRECTION_OUT,
                         new IpSecAlgorithm(
                                 IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4))
                 .setIpv4Encapsulation(encapSocket, encapSocket.getPort())
-                .buildTransportModeTransform(remoteAddr);
+                .buildTransportModeTransform(InetAddress.getByName(localAddr), spi);
     }
 
     private static int getPort(FileDescriptor sock) throws Exception {
diff --git a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
index 018e0f9..ae6123e 100644
--- a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
@@ -382,7 +382,14 @@
             byte bytes[] = new byte[SIZEOF_U64];
             ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder());
             int read = Os.read(pagemap, buf);
-            if (read != bytes.length)
+
+            if (read == 0)
+                // /proc/[pid]/maps may contain entries that are outside the process's VM space,
+                // like the [vectors] page on 32-bit ARM devices.  In this case, seek() succeeds but
+                // read() returns 0.  The kernel is telling us that there are no more pagemap
+                // entries to read, so we can stop here.
+                break;
+            else if (read != bytes.length)
                 throw new IOException("read(" + bytes.length + ") returned " + read);
 
             buf.position(0);
@@ -397,9 +404,6 @@
     @MediumTest
     public void testProcSelfPagemapSane() throws ErrnoException, IOException {
         FileDescriptor pagemap = null;
-        int dumpable = Os.prctl(OsConstants.PR_GET_DUMPABLE, 0, 0, 0, 0);
-        Os.prctl(OsConstants.PR_SET_DUMPABLE, 1, 0, 0, 0);
-
         try {
             pagemap = Os.open("/proc/self/pagemap", OsConstants.O_RDONLY, 0);
 
@@ -416,7 +420,6 @@
         } finally {
             if (pagemap != null)
                 Os.close(pagemap);
-            Os.prctl(OsConstants.PR_SET_DUMPABLE, dumpable, 0, 0, 0);
         }
     }
 
diff --git a/tests/tests/telephony/src/android/telephony/cts/TelephonyManagerTest.java b/tests/tests/telephony/src/android/telephony/cts/TelephonyManagerTest.java
index 79097e0..9803a45 100644
--- a/tests/tests/telephony/src/android/telephony/cts/TelephonyManagerTest.java
+++ b/tests/tests/telephony/src/android/telephony/cts/TelephonyManagerTest.java
@@ -51,8 +51,14 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.List;
 import java.util.regex.Pattern;
 
+/**
+ * Build, install and run the tests by running the commands below:
+ *  make cts -j64
+ *  cts-tradefed run cts -m CtsTelephonyTestCases --test android.telephony.cts.TelephonyManagerTest
+ */
 @RunWith(AndroidJUnit4.class)
 public class TelephonyManagerTest {
     private TelephonyManager mTelephonyManager;
@@ -602,4 +608,32 @@
     private static Context getContext() {
         return InstrumentationRegistry.getContext();
     }
+
+    /**
+     * Tests that the device properly sets the network selection mode to automatic.
+     * Expects a security exception since the caller does not have carrier privileges.
+     */
+    @Test
+    public void testSetNetworkSelectionModeAutomatic() {
+        try {
+            mTelephonyManager.setNetworkSelectionModeAutomatic();
+            fail("Expected SecurityException. App does not have carrier privileges.");
+        } catch (SecurityException expected) {
+        }
+    }
+
+    /**
+     * Tests that the device properly asks the radio to connect to the input network and change
+     * selection mode to manual.
+     * Expects a security exception since the caller does not have carrier privileges.
+     */
+    @Test
+    public void testSetNetworkSelectionModeManual() {
+        try {
+            mTelephonyManager.setNetworkSelectionModeManual(
+                    "" /* operatorNumeric */, false /* persistSelection */);
+            fail("Expected SecurityException. App does not have carrier privileges.");
+        } catch (SecurityException expected) {
+        }
+    }
 }
diff --git a/tests/tests/uidisolation/AndroidManifest.xml b/tests/tests/uidisolation/AndroidManifest.xml
index 0d27e37..822eeea 100644
--- a/tests/tests/uidisolation/AndroidManifest.xml
+++ b/tests/tests/uidisolation/AndroidManifest.xml
@@ -18,8 +18,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.uidisolation.cts">
 
-    <uses-library android:name="org.apache.http.legacy" android:required="false" />
-
     <application android:label="UidIsolationTest">
       <activity android:name="android.uidisolation.cts.ServiceRunnerActivity"
                 android:label="UidIsolationTest"/>
@@ -29,6 +27,7 @@
                android:process=":remote"
                android:isolatedProcess="true"/>
       <uses-library android:name="android.test.runner" />
+      <uses-library android:name="org.apache.http.legacy" android:required="false" />
     </application>
 
    <uses-permission android:name="android.permission.INTERNET"/>
diff --git a/tests/tests/view/src/android/view/cts/PixelCopyViewProducerActivity.java b/tests/tests/view/src/android/view/cts/PixelCopyViewProducerActivity.java
index ee1b921..69e1e7f 100644
--- a/tests/tests/view/src/android/view/cts/PixelCopyViewProducerActivity.java
+++ b/tests/tests/view/src/android/view/cts/PixelCopyViewProducerActivity.java
@@ -30,11 +30,14 @@
 import android.os.Bundle;
 import android.view.View;
 import android.view.ViewTreeObserver.OnDrawListener;
+import android.view.WindowInsets;
+import android.widget.FrameLayout;
 
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-public class PixelCopyViewProducerActivity extends Activity implements OnDrawListener {
+public class PixelCopyViewProducerActivity extends Activity implements OnDrawListener,
+        View.OnApplyWindowInsetsListener{
     private static final int[] ORIENTATIONS = {
             ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
             ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
@@ -44,6 +47,7 @@
     private int mCurrentOrientation = 0;
     private View mContent;
     private Rect mContentBounds = new Rect();
+    private Rect mOutsets = new Rect();
     private CountDownLatch mFence = new CountDownLatch(3);
     private boolean mSupportsRotation;
 
@@ -62,6 +66,7 @@
         mContent = new ColoredGrid(this);
         setContentView(mContent);
         mContent.getViewTreeObserver().addOnDrawListener(this);
+        mContent.setOnApplyWindowInsetsListener(this);
     }
 
     @Override
@@ -79,8 +84,9 @@
             // We pass mContentBounds here just as a throwaway rect, we don't care about
             // the visible rect just the global offset.
             mContent.getGlobalVisibleRect(mContentBounds, offset);
-            mContentBounds.set(offset.x, offset.y,
-                    offset.x + mContent.getWidth(), offset.y + mContent.getHeight());
+            mContentBounds.set(offset.x - mOutsets.left, offset.y - mOutsets.top,
+                    offset.x - mOutsets.left + mContent.getWidth(),
+                    offset.y - mOutsets.top + mContent.getHeight());
             mFence.countDown();
             if (mFence.getCount() > 0) {
                 mContent.invalidate();
@@ -88,6 +94,19 @@
         });
     }
 
+    @Override
+    public WindowInsets onApplyWindowInsets(View v, WindowInsets in) {
+        if (in.isRound()) {
+            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContent.getLayoutParams();
+            params.setMargins(in.getSystemWindowInsetLeft(), in.getSystemWindowInsetTop(),
+                    in.getSystemWindowInsetRight(), in.getSystemWindowInsetBottom());
+            mOutsets = new Rect(in.getSystemWindowInsetLeft(), in.getSystemWindowInsetTop(),
+                    in.getSystemWindowInsetRight(), in.getSystemWindowInsetBottom());
+            mContent.setLayoutParams(params);
+        }
+        return in;
+    }
+
     public void waitForFirstDrawCompleted(int timeout, TimeUnit unit) {
         try {
             if (!mFence.await(timeout, unit)) {