Merge "Shrank refocus test and reference image size"
diff --git a/tests/tests/jni/libjnitest/android_jni_cts_LinkerNamespacesTest.cpp b/tests/tests/jni/libjnitest/android_jni_cts_LinkerNamespacesTest.cpp
index d38bdd2..3ffabc3 100644
--- a/tests/tests/jni/libjnitest/android_jni_cts_LinkerNamespacesTest.cpp
+++ b/tests/tests/jni/libjnitest/android_jni_cts_LinkerNamespacesTest.cpp
@@ -20,9 +20,11 @@
 
 #include <dirent.h>
 #include <dlfcn.h>
+#include <fcntl.h>
 #include <jni.h>
 #include <JNIHelp.h>
 #include <libgen.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -32,17 +34,18 @@
 #include <unordered_set>
 #include <vector>
 
-static std::vector<std::string> kDefaultLibraryPaths = {
-#if defined(__LP64__)
-    "/system/lib64",
-    "/vendor/lib64",
-#else
-    "/system/lib",
-    "/vendor/lib",
-#endif
-  };
+#include "ScopedLocalRef.h"
+#include "ScopedUtfChars.h"
 
-static std::unordered_set<std::string> kPublicLibraries = {
+#if defined(__LP64__)
+static const std::string kSystemLibraryPath = "/system/lib64";
+static const std::string kVendorLibraryPath = "/vendor/lib64";
+#else
+static const std::string kSystemLibraryPath = "/system/lib";
+static const std::string kVendorLibraryPath = "/vendor/lib";
+#endif
+
+static std::unordered_set<std::string> kSystemPublicLibraries = {
     "libandroid.so",
     "libc.so",
     "libdl.so",
@@ -89,10 +92,12 @@
   return true;
 }
 
-static bool should_be_accessible(const std::string& path) {
+static bool should_be_accessible(const std::string& public_library_path,
+                                 const std::unordered_set<std::string>& public_libraries,
+                                 const std::string& path) {
   std::string name = basename(path.c_str());
-  return (kPublicLibraries.find(name) != kPublicLibraries.end()) &&
-         (kDefaultLibraryPaths.front() + "/" + name == path);
+  return (public_libraries.find(name) != public_libraries.end()) &&
+         (public_library_path + "/" + name == path);
 }
 
 static bool is_directory(const std::string path) {
@@ -105,17 +110,19 @@
 }
 
 static bool is_libdl(const std::string path) {
-  return kDefaultLibraryPaths.front() + "/libdl.so" == path;
+  return kSystemLibraryPath + "/libdl.so" == path;
 }
 
-extern "C" JNIEXPORT jstring JNICALL Java_android_jni_cts_LinkerNamespacesHelper_runAccessibilityTest(
-        JNIEnv* env, jclass clazz __attribute__((unused))) {
-  std::list<std::string> dirs(kDefaultLibraryPaths.begin(), kDefaultLibraryPaths.end());
+static bool check_libs(const std::string& public_library_path,
+                       const std::unordered_set<std::string>& public_libraries,
+                       std::string* error) {
+  std::list<std::string> dirs;
+  dirs.push_back(public_library_path);
+
   while (!dirs.empty()) {
     const auto dir = dirs.front();
     dirs.pop_front();
-    std::string error;
-    bool success = for_each_file(dir, [&dirs,&dir](const char* name, std::string* error_msg) {
+    bool success = for_each_file(dir, [&](const char* name, std::string* error_msg) {
       std::string path = dir + "/" + name;
       if (is_directory(path)) {
         dirs.push_back(path);
@@ -132,7 +139,7 @@
 
       auto dlcloser = [](void* handle) { dlclose(handle); };
       std::unique_ptr<void, decltype(dlcloser)> handle(dlopen(path.c_str(), RTLD_NOW), dlcloser);
-      if (should_be_accessible(path)) {
+      if (should_be_accessible(public_library_path, public_libraries, path)) {
         if (handle.get() == nullptr) {
           *error_msg = "The library \"" + path + "\" should be accessible but isn't: " + dlerror();
           return false;
@@ -151,13 +158,44 @@
         }
       }
       return true;
-    }, &error);
+    }, error);
 
     if (!success) {
-      return env->NewStringUTF(error.c_str());
+      return false;
     }
   }
 
+  return true;
+}
+
+static void load_vendor_libraries(JNIEnv* env,
+                                  jobjectArray java_vendor_public_libraries,
+                                  std::unordered_set<std::string>* libraries) {
+  size_t size = env->GetArrayLength(java_vendor_public_libraries);
+  for (size_t i = 0; i<size; ++i) {
+    ScopedLocalRef<jstring> java_soname(
+        env, (jstring) env->GetObjectArrayElement(java_vendor_public_libraries, i));
+
+    ScopedUtfChars soname(env, java_soname.get());
+    libraries->insert(soname.c_str());
+  }
+}
+
+extern "C" JNIEXPORT jstring JNICALL
+    Java_android_jni_cts_LinkerNamespacesHelper_runAccessibilityTestImpl(
+        JNIEnv* env,
+        jclass clazz __attribute__((unused)),
+        jobjectArray java_vendor_public_libraries) {
+  std::string error;
+
+  std::unordered_set<std::string> vendor_public_libraries;
+  load_vendor_libraries(env, java_vendor_public_libraries, &vendor_public_libraries);
+
+  if (!check_libs(kSystemLibraryPath, kSystemPublicLibraries, &error) ||
+      !check_libs(kVendorLibraryPath, vendor_public_libraries, &error)) {
+    return env->NewStringUTF(error.c_str());
+  }
+
   return nullptr;
 }
 
diff --git a/tests/tests/jni/src/android/jni/cts/JniStaticTest.java b/tests/tests/jni/src/android/jni/cts/JniStaticTest.java
index 8a91c44..91ebe73 100644
--- a/tests/tests/jni/src/android/jni/cts/JniStaticTest.java
+++ b/tests/tests/jni/src/android/jni/cts/JniStaticTest.java
@@ -16,6 +16,7 @@
 
 package android.jni.cts;
 
+import java.io.IOException;
 
 /**
  * Basic static method tests. The "nonce" class being tested by this
@@ -34,7 +35,7 @@
      * Test library accessibility. Internal platform libraries should not
      * be accessible from the jni code.
      */
-    public void test_linker_namespaces() {
+    public void test_linker_namespaces() throws IOException {
         String error = LinkerNamespacesHelper.runAccessibilityTest();
         if (error != null) {
             fail(error);
diff --git a/tests/tests/jni/src/android/jni/cts/LinkerNamespacesHelper.java b/tests/tests/jni/src/android/jni/cts/LinkerNamespacesHelper.java
index 2dff019..21bd71a 100644
--- a/tests/tests/jni/src/android/jni/cts/LinkerNamespacesHelper.java
+++ b/tests/tests/jni/src/android/jni/cts/LinkerNamespacesHelper.java
@@ -16,6 +16,31 @@
 
 package android.jni.cts;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.File;
+import java.io.FileReader;
+import java.util.ArrayList;
+import java.util.List;
+
 class LinkerNamespacesHelper {
-    public static native String runAccessibilityTest();
+    private final static String VENDOR_CONFIG_FILE = "/vendor/etc/public.libraries.txt";
+    public static String runAccessibilityTest() throws IOException {
+        List<String> libs = new ArrayList<>();
+        File file = new File(VENDOR_CONFIG_FILE);
+        if (file.exists()) {
+            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
+                String line;
+                while ((line = br.readLine()) != null) {
+                    line = line.trim();
+                    if (line.isEmpty() || line.startsWith("#")) {
+                        continue;
+                    }
+                    libs.add(line);
+                }
+            }
+        }
+        return runAccessibilityTestImpl(libs.toArray(new String[libs.size()]));
+    }
+    private static native String runAccessibilityTestImpl(String[] publicVendorLibs);
 }
diff --git a/tests/tests/libcorefileio/Android.mk b/tests/tests/libcorefileio/Android.mk
new file mode 100644
index 0000000..29226bf
--- /dev/null
+++ b/tests/tests/libcorefileio/Android.mk
@@ -0,0 +1,33 @@
+# Copyright (C) 2016 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 := optional
+# and when built explicitly put it in the data partition
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := CtsLibcoreFileIOTestCases
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+include $(BUILD_CTS_PACKAGE)
diff --git a/tests/tests/libcorefileio/AndroidManifest.xml b/tests/tests/libcorefileio/AndroidManifest.xml
new file mode 100644
index 0000000..4557eec
--- /dev/null
+++ b/tests/tests/libcorefileio/AndroidManifest.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 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.libcorefileio.cts">
+
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+
+    <application>
+        <uses-library android:name="android.test.runner"/>
+        <service android:name="android.cts.LockHoldingService"
+                 android:process=":lockHoldingService"
+                 android:permission="android.permission.WRITE_EXTERNAL_STORAGE"
+        />
+        <receiver android:name="android.cts.FileChannelTryLockTest$IntentReceiver">
+
+            <intent-filter>
+                <action android:name="android.cts.CtsLibcoreFileIOTestCases">
+                </action>
+            </intent-filter>
+
+        </receiver>
+    </application>
+
+    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="android.libcorefileio.cts">
+        <meta-data android:name="listener"
+                   android:value="com.android.cts.runner.CtsTestRunListener"/>
+    </instrumentation>
+
+</manifest>
+
diff --git a/tests/tests/libcorefileio/AndroidTest.xml b/tests/tests/libcorefileio/AndroidTest.xml
new file mode 100644
index 0000000..9baa713
--- /dev/null
+++ b/tests/tests/libcorefileio/AndroidTest.xml
@@ -0,0 +1,23 @@
+<!-- Copyright (C) 2016 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 Legacy Libcore test cases">
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.ApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="CtsLibcoreFileIOTestCases.apk" />
+    </target_preparer>
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+        <option name="package" value="android.libcorefileio.cts" />
+    </test>
+</configuration>
\ No newline at end of file
diff --git a/tests/tests/libcorefileio/src/android/cts/FileChannelTryLockTest.java b/tests/tests/libcorefileio/src/android/cts/FileChannelTryLockTest.java
new file mode 100644
index 0000000..61a59ca
--- /dev/null
+++ b/tests/tests/libcorefileio/src/android/cts/FileChannelTryLockTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2016 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.cts;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Environment;
+import android.test.AndroidTestCase;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.channels.FileLock;
+import java.nio.channels.OverlappingFileLockException;
+
+@SuppressWarnings("deprecation")
+public class FileChannelTryLockTest extends AndroidTestCase {
+
+    final static String dirName = "CtsFIleIOTest";
+
+    final static String sharedFileName = "sharedFile";
+
+    public void testFileLockWithMultipleProcess() throws InterruptedException, IOException {
+        IntentReceiver receiver = new IntentReceiver();
+        getContext().startService(new Intent(getContext(), LockHoldingService.class));
+        synchronized (receiver.notifier) {
+            receiver.notifier.wait(10000);
+        }
+        File sharedFile = createFileInDir(dirName, sharedFileName);
+        assertNull(new FileOutputStream(sharedFile).getChannel().tryLock());
+        getContext().stopService(new Intent(getContext(), LockHoldingService.class));
+    }
+
+    public void testFileLockWithSingleProcess() throws IOException {
+        File file = createFileInDir(dirName, "sharedFileForSingleProcess");
+        FileLock fileLock1 = new FileOutputStream(file).getChannel().tryLock();
+        try {
+            new FileOutputStream(file).getChannel().tryLock();
+            fail();
+        } catch (OverlappingFileLockException expected) {
+        }
+    }
+
+    static File createFileInDir(String dirName, String fileName) throws IOException {
+        File dir = new File(Environment.getExternalStorageDirectory(), dirName);
+        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+            throw new IOException("External storage is not mounted");
+        } else if (!dir.mkdirs() && !dir.isDirectory()) {
+            throw new IOException("Cannot create directory for device info files");
+        } else {
+            return new File(dir, fileName);
+        }
+    }
+
+    static void deleteDir() {
+        File dir = new File(Environment.getExternalStorageDirectory(), dirName);
+        if (dir.isDirectory()) {
+            String[] children = dir.list();
+            for (int i = 0; i < children.length; i++) {
+                new File(dir, children[i]).delete();
+            }
+            dir.delete();
+        }
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        getContext().stopService(new Intent(getContext(), LockHoldingService.class));
+        deleteDir();
+    }
+
+    public static class IntentReceiver extends BroadcastReceiver {
+
+        public final static Object notifier = new Object();
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            synchronized (notifier) {
+                notifier.notify();
+            }
+        }
+    }
+}
+
diff --git a/tests/tests/libcorefileio/src/android/cts/LockHoldingService.java b/tests/tests/libcorefileio/src/android/cts/LockHoldingService.java
new file mode 100644
index 0000000..be6a352
--- /dev/null
+++ b/tests/tests/libcorefileio/src/android/cts/LockHoldingService.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 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.cts;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.IBinder;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.channels.FileLock;
+
+public class LockHoldingService extends Service {
+
+    final String TAG = "CtsLibcoreFileIOTestCases";
+
+    File file = null;
+
+    FileLock fileLock = null;
+
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startID) {
+        try {
+            this.file = FileChannelTryLockTest.createFileInDir(FileChannelTryLockTest.dirName,
+                    FileChannelTryLockTest.sharedFileName);
+            try {
+                this.fileLock = new FileOutputStream(file).getChannel().tryLock();
+            } catch (IOException e) {
+                Log.e(TAG, e.getMessage());
+            }
+        } catch (IOException e) {
+            Log.e(TAG, e.getMessage());
+        }
+        sendBroadcast(new Intent().setAction("android.cts.CtsLibcoreFileIOTestCases"));
+        return START_STICKY;
+    }
+
+    @Override
+    public void onDestroy() {
+        try {
+            fileLock.close();
+        } catch (IOException e) {
+            Log.e(TAG, e.getMessage());
+        }
+        FileChannelTryLockTest.deleteDir();
+    }
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/kernel_input.rs b/tests/tests/renderscript/src/android/renderscript/cts/kernel_input.rs
index 2eaca92..a4d919f 100644
--- a/tests/tests/renderscript/src/android/renderscript/cts/kernel_input.rs
+++ b/tests/tests/renderscript/src/android/renderscript/cts/kernel_input.rs
@@ -35,6 +35,12 @@
  * functions check their input against a global variable.
  */
 
+// The clear_input_* kernels have rsDebug calls so that the writes they make to
+// their parameters don't get optimized away.  To avoid logspam from the
+// rsDebug, guard calls to it with a runtime test that is guaranteed to be
+// false.
+volatile int gDummy = 0;
+
 // For clear_input_* kernels, we use a volatile qualified input argument
 // to try to inhibit any optimizations that would result in the write to
 // the input argument being optimized out by the compiler.
@@ -42,9 +48,13 @@
 #define COMMON_TEST_CODE(type)                           \
   type initial_value_##type;                             \
   type RS_KERNEL clear_input_##type(volatile type in) {  \
-    rsDebug(#type, in);                                  \
+    if (gDummy == 500) {                                 \
+      rsDebug(#type, in);                                \
+    }                                                    \
     in -= in;                                            \
-    rsDebug(#type, in);                                  \
+    if (gDummy == 500) {                                 \
+      rsDebug(#type, in);                                \
+    }                                                    \
     return in;                                           \
   }
 
@@ -174,9 +184,13 @@
 
 // See comment on volatile above.
 small RS_KERNEL clear_input_small(volatile small in) {
-  rsDebug("in.x", in.x[0]);
+  if (gDummy == 500) {
+    rsDebug("in.x", in.x[0]);
+  }
   in.x[0] = 0;
-  rsDebug("in.x", in.x[0]);
+  if (gDummy == 500) {
+    rsDebug("in.x", in.x[0]);
+  }
   return in;
 }
 
@@ -190,9 +204,13 @@
 // See comment on volatile above.
 big RS_KERNEL clear_input_big(volatile big in) {
   for (size_t i = 0; i < 100; ++i) {
-    rsDebug("in.x", in.x[i]);
+    if (gDummy == 500) {
+      rsDebug("in.x", in.x[i]);
+    }
     in.x[i] = 0;
-    rsDebug("in.x", in.x[i]);
+    if (gDummy == 500) {
+      rsDebug("in.x", in.x[i]);
+    }
   }
   return in;
 }