LibNativeBridge: Add testing

Change PreInitializeNativeBridge to have a different path for
the host to allow testing. Add a test (needs root privileges).

Add a test for NeedsNativeBridge.

Change error reporting to use strerror.

Change-Id: Id2d488f0484ff8b0438863b48ef43770e784505f
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
index c19aba9..d460f6f 100644
--- a/libnativebridge/native_bridge.cc
+++ b/libnativebridge/native_bridge.cc
@@ -205,6 +205,10 @@
 
 
 bool NeedsNativeBridge(const char* instruction_set) {
+  if (instruction_set == nullptr) {
+    ALOGE("Null instruction set in NeedsNativeBridge.");
+    return false;
+  }
   return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0;
 }
 
@@ -240,11 +244,16 @@
   // mount command will fail, so we safe the extra file existence check...
   char cpuinfo_path[1024];
 
-  snprintf(cpuinfo_path, 1024, "/system/lib"
+#ifdef HAVE_ANDROID_OS
+  snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib"
 #ifdef __LP64__
-  "64"
+      "64"
+#endif  // __LP64__
+      "/%s/cpuinfo", instruction_set);
+#else   // !HAVE_ANDROID_OS
+  // To be able to test on the host, we hardwire a relative path.
+  snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo");
 #endif
-    "/%s/cpuinfo", instruction_set);
 
   // Bind-mount.
   if (TEMP_FAILURE_RETRY(mount(cpuinfo_path,        // Source.
@@ -252,7 +261,7 @@
                                nullptr,             // FS type.
                                MS_BIND,             // Mount flags: bind mount.
                                nullptr)) == -1) {   // "Data."
-    ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %d", cpuinfo_path, errno);
+    ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
   }
 #else
   UNUSED(instruction_set);