Snap for 4507345 from 2d189315eca638ab626797e8dc6a0f006f7daac5 to pi-release

Change-Id: Ic4c044c221cd3b0c585de61a642595e643d72d29
diff --git a/JniInvocation.cpp b/JniInvocation.cpp
index 031dcb3..51cba6d 100644
--- a/JniInvocation.cpp
+++ b/JniInvocation.cpp
@@ -29,6 +29,28 @@
 #include <sys/system_properties.h>
 #endif
 
+template <typename T>
+void UNUSED(const T&) {}
+
+bool IsDebuggable() {
+#ifdef __ANDROID__
+  char debuggable[PROP_VALUE_MAX] = {0};
+  __system_property_get("ro.debuggable", debuggable);
+  return strcmp(debuggable, "1") == 0;
+#else
+  return false;
+#endif
+}
+
+int GetLibrarySystemProperty(char* buffer) {
+#ifdef __ANDROID__
+  return __system_property_get("persist.sys.dalvik.vm.lib.2", buffer);
+#else
+  UNUSED(buffer);
+  return 0;
+#endif
+}
+
 JniInvocation* JniInvocation::jni_invocation_ = NULL;
 
 JniInvocation::JniInvocation() :
@@ -48,22 +70,18 @@
   }
 }
 
-#ifdef __ANDROID__
-static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2";
-static const char* kDebuggableSystemProperty = "ro.debuggable";
-#endif
 static const char* kLibraryFallback = "libart.so";
 
-template<typename T> void UNUSED(const T&) {}
-
 const char* JniInvocation::GetLibrary(const char* library, char* buffer) {
+  return GetLibrary(library, buffer, &IsDebuggable, &GetLibrarySystemProperty);
+}
+
+const char* JniInvocation::GetLibrary(const char* library, char* buffer, bool (*is_debuggable)(),
+                                      int (*get_library_system_property)(char* buffer)) {
 #ifdef __ANDROID__
   const char* default_library;
 
-  char debuggable[PROP_VALUE_MAX];
-  __system_property_get(kDebuggableSystemProperty, debuggable);
-
-  if (strcmp(debuggable, "1") != 0) {
+  if (!is_debuggable()) {
     // Not a debuggable build.
     // Do not allow arbitrary library. Ignore the library parameter. This
     // will also ignore the default library, but initialize to fallback
@@ -75,7 +93,7 @@
     // Accept the library parameter. For the case it is NULL, load the default
     // library from the system property.
     if (buffer != NULL) {
-      if (__system_property_get(kLibrarySystemProperty, buffer) > 0) {
+      if (get_library_system_property(buffer) > 0) {
         default_library = buffer;
       } else {
         default_library = kLibraryFallback;
@@ -87,6 +105,8 @@
   }
 #else
   UNUSED(buffer);
+  UNUSED(is_debuggable);
+  UNUSED(get_library_system_property);
   const char* default_library = kLibraryFallback;
 #endif
   if (library == NULL) {
diff --git a/include/nativehelper/JniInvocation.h b/include/nativehelper/JniInvocation.h
index fc2ed0a..58beec5 100644
--- a/include/nativehelper/JniInvocation.h
+++ b/include/nativehelper/JniInvocation.h
@@ -45,6 +45,8 @@
   static const char* GetLibrary(const char* library, char* buffer);
 
  private:
+  static const char* GetLibrary(const char* library, char* buffer, bool (*is_debuggable)(),
+                                int (*get_library_system_property)(char* buffer));
 
   bool FindSymbol(void** pointer, const char* symbol);
 
@@ -64,6 +66,8 @@
   friend jint JNI_GetDefaultJavaVMInitArgs(void* vm_args);
   friend jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
   friend jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count);
+  friend class JNIInvocation_Debuggable_Test;
+  friend class JNIInvocation_NonDebuggable_Test;
 };
 
 #endif  // JNI_INVOCATION_H_included
diff --git a/tests/JniInvocation_test.cpp b/tests/JniInvocation_test.cpp
index 1467cb3..bb62e39 100644
--- a/tests/JniInvocation_test.cpp
+++ b/tests/JniInvocation_test.cpp
@@ -33,80 +33,33 @@
 // PROPERTY_VALUE_MAX.
 #include "cutils/properties.h"
 
-// Ability to have fake local system properties.
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-#include <sys/_system_properties.h>
-
-struct LocalPropertyTestState {
-    LocalPropertyTestState() : valid(false) {
-        const char* ANDROID_DATA = getenv("ANDROID_DATA");
-        char dir_template[PATH_MAX];
-        snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", ANDROID_DATA);
-        char* dirname = mkdtemp(dir_template);
-        if (!dirname) {
-            fprintf(stderr, "making temp file for test state failed (is %s writable?): %s",
-                    dir_template, strerror(errno));
-            return;
-        }
-
-        pa_dirname = dirname;
-        pa_filename = pa_dirname + "/__properties__";
-
-        __system_property_set_filename(pa_filename.c_str());
-        __system_property_area_init();
-        valid = true;
-    }
-
-    ~LocalPropertyTestState() {
-        if (!valid) {
-            return;
-        }
-
-        __system_property_set_filename(PROP_FILENAME);
-        __system_properties_init();
-        unlink(pa_filename.c_str());
-        rmdir(pa_dirname.c_str());
-    }
-public:
-    bool valid;
-private:
-    std::string pa_dirname;
-    std::string pa_filename;
-};
 #endif
 
-namespace android {
-
-class JNIInvocationTest : public testing::Test {
-};
-
 #ifdef HAVE_TEST_STUFF
-static const char* kDebuggableSystemProperty = "ro.debuggable";
-static const char* kIsDebuggableValue = "1";
-static const char* kIsNotDebuggableValue = "0";
-
-static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2";
 static const char* kTestNonNull = "libartd.so";
 static const char* kTestNonNull2 = "libartd2.so";
 static const char* kExpected = "libart.so";
 #endif
 
-TEST_F(JNIInvocationTest, Debuggable) {
+TEST(JNIInvocation, Debuggable) {
 #ifdef HAVE_TEST_STUFF
-    LocalPropertyTestState pa;
-    ASSERT_TRUE(pa.valid);
-    ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsDebuggableValue, 1));
-    ASSERT_EQ(0, __system_property_add(kLibrarySystemProperty, 27, kTestNonNull2, 11));
+    auto is_debuggable = []() { return true; };
+    auto get_library_system_property = [](char* buffer) -> int {
+        strcpy(buffer, kTestNonNull2);
+        return sizeof(kTestNonNull2);
+    };
 
     char buffer[PROPERTY_VALUE_MAX];
-    const char* result = JniInvocation::GetLibrary(NULL, buffer);
+    const char* result =
+        JniInvocation::GetLibrary(NULL, buffer, is_debuggable, get_library_system_property);
     EXPECT_FALSE(result == NULL);
     if (result != NULL) {
         EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0);
         EXPECT_FALSE(strcmp(result, kExpected) == 0);
     }
 
-    result = JniInvocation::GetLibrary(kTestNonNull, buffer);
+    result =
+        JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, get_library_system_property);
     EXPECT_FALSE(result == NULL);
     if (result != NULL) {
         EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
@@ -117,14 +70,12 @@
 #endif
 }
 
-TEST_F(JNIInvocationTest, NonDebuggable) {
+TEST(JNIInvocation, NonDebuggable) {
 #ifdef HAVE_TEST_STUFF
-    LocalPropertyTestState pa;
-    ASSERT_TRUE(pa.valid);
-    ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsNotDebuggableValue, 1));
+    auto is_debuggable = []() { return false; };
 
     char buffer[PROPERTY_VALUE_MAX];
-    const char* result = JniInvocation::GetLibrary(NULL, buffer);
+    const char* result = JniInvocation::GetLibrary(NULL, buffer, is_debuggable, nullptr);
     EXPECT_FALSE(result == NULL);
     if (result != NULL) {
         EXPECT_TRUE(strcmp(result, kExpected) == 0);
@@ -132,7 +83,7 @@
         EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
     }
 
-    result = JniInvocation::GetLibrary(kTestNonNull, buffer);
+    result = JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, nullptr);
     EXPECT_FALSE(result == NULL);
     if (result != NULL) {
         EXPECT_TRUE(strcmp(result, kExpected) == 0);
@@ -142,5 +93,3 @@
     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
 #endif
 }
-
-}  // namespace android