Do not reinitialize system properties to fake their values

Reinitializing system properties can result in crashes later in the
program, and is generally not recommended or even supported.  Instead,
this change uses a very rudamentary approach to mocking out the system
property functions.

Bug: 62197783
Test: unit tests
Change-Id: I99c57e1d87490926e4839ee154c862ee8a199e26
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) {