Fix bitrot in the libnativebridge tests.

- Bitrot 1: NativeBridge2Signal_test stopped working when version 3 was
  added in https://r.android.com/234059.

- Bitrot 2: The code path that PreInitializeNativeBridgeFail1_test
  tested was removed completely when missing app data directory was
  allowed in https://r.android.com/1208629, so delete the test.

- Use cc_test_library for the dynamic test libs so they are installed in
  the test directory rather than in system/lib(64).

- Use test_per_src since native_bridge.cc doesn't support reloading the
  native bridge implementation lib after unloading.

- Add a custom script to run the tests and a preupload check that the
  script is run. (Configuring atest for these tests still TBD in
  b/189484095 - test_per_src is a complication.)

Test: art/libnativebridge/tests/runtests.sh
Bug: 122710865
Bug: 189484095
Change-Id: Ib2b387e1d858127ca4bb44c548a5105ea8b838d8
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
index 1dc9641..b8ee3c5 100644
--- a/PREUPLOAD.cfg
+++ b/PREUPLOAD.cfg
@@ -3,6 +3,10 @@
 
 hidden_api_txt_checksorted_hook = ${REPO_ROOT}/tools/platform-compat/hiddenapi/checksorted_sha.sh ${PREUPLOAD_COMMIT} ${REPO_ROOT}
 
+# TODO(b/189484095): Port libnativebridge tests to atest and enable in presubmit
+# so we don't need the custom runtests script and this check.
+check_libnativebridge_test_field = libnativebridge/tests/preupload_check_test_tag.sh ${PREUPLOAD_COMMIT_MESSAGE} ${PREUPLOAD_FILES}
+
 [Builtin Hooks]
 cpplint = true
 bpfmt = true
diff --git a/libnativebridge/tests/Android.bp b/libnativebridge/tests/Android.bp
index 9a81a0d..785ccd9 100644
--- a/libnativebridge/tests/Android.bp
+++ b/libnativebridge/tests/Android.bp
@@ -48,25 +48,25 @@
     cppflags: ["-fvisibility=protected"],
 }
 
-cc_library_shared {
+cc_test_library {
     name: "libnativebridge-test-case",
     srcs: ["NativeBridgeTestCase.cpp"],
     defaults: ["libnativebridge-test-case-defaults"],
 }
 
-cc_library_shared {
+cc_test_library {
     name: "libnativebridge2-test-case",
     srcs: ["NativeBridgeTestCase2.cpp"],
     defaults: ["libnativebridge-test-case-defaults"],
 }
 
-cc_library_shared {
+cc_test_library {
     name: "libnativebridge3-test-case",
     srcs: ["NativeBridgeTestCase3.cpp"],
     defaults: ["libnativebridge-test-case-defaults"],
 }
 
-cc_library_shared {
+cc_test_library {
     name: "libnativebridge6-test-case",
     srcs: ["NativeBridgeTestCase6.cpp"],
     defaults: ["libnativebridge-test-case-defaults"],
@@ -76,7 +76,7 @@
 }
 
 // A helper library to produce test-case side effect of PreZygoteForkNativeBridge.
-cc_library_shared {
+cc_test_library {
     name: "libnativebridge6prezygotefork",
     srcs: ["NativeBridge6PreZygoteFork_lib.cpp"],
     defaults: ["libnativebridge-test-case-defaults"],
@@ -102,6 +102,10 @@
         },
     },
 
+    // native_bridge.cc doesn't support reloading the native bridge after
+    // unloading, so each test needs to be its own process.
+    test_per_src: true,
+
     srcs: [
         "CodeCacheCreate_test.cpp",
         "CodeCacheExists_test.cpp",
@@ -112,7 +116,6 @@
         "NativeBridgeVersion_test.cpp",
         "NeedsNativeBridge_test.cpp",
         "PreInitializeNativeBridge_test.cpp",
-        "PreInitializeNativeBridgeFail1_test.cpp",
         "PreInitializeNativeBridgeFail2_test.cpp",
         "ReSetupNativeBridge_test.cpp",
         "UnavailableNativeBridge_test.cpp",
@@ -129,6 +132,9 @@
     shared_libs: [
         "liblog",
         "libnativebridge-test-case",
+        "libnativebridge2-test-case",
+        "libnativebridge3-test-case",
+        "libnativebridge6-test-case",
         "libnativebridge6prezygotefork",
     ],
     header_libs: ["libbase_headers"],
@@ -173,5 +179,5 @@
         "jni_headers",
         "libnativebridge-headers",
     ],
-    gtest:false,
+    gtest: false,
 }
diff --git a/libnativebridge/tests/CodeCacheCreate_test.cpp b/libnativebridge/tests/CodeCacheCreate_test.cpp
index 58270c4..1bd309c 100644
--- a/libnativebridge/tests/CodeCacheCreate_test.cpp
+++ b/libnativebridge/tests/CodeCacheCreate_test.cpp
@@ -23,9 +23,10 @@
 namespace android {
 
 // Tests that the bridge initialization creates the code_cache if it doesn't
-// exists.
+// exist.
 TEST_F(NativeBridgeTest, CodeCacheCreate) {
-    // Make sure that code_cache does not exists
+    // Make sure that code_cache does not exist
+    rmdir(kCodeCache);
     struct stat st;
     ASSERT_EQ(-1, stat(kCodeCache, &st));
     ASSERT_EQ(ENOENT, errno);
diff --git a/libnativebridge/tests/NativeBridge2Signal_test.cpp b/libnativebridge/tests/NativeBridge2Signal_test.cpp
index 59baca4..0573a5a 100644
--- a/libnativebridge/tests/NativeBridge2Signal_test.cpp
+++ b/libnativebridge/tests/NativeBridge2Signal_test.cpp
@@ -21,8 +21,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary2 = "libnativebridge2-test-case.so";
-
 TEST_F(NativeBridgeTest, V2_Signal) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary2, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3CreateNamespace_test.cpp b/libnativebridge/tests/NativeBridge3CreateNamespace_test.cpp
index 3bcc917..db7dd31 100644
--- a/libnativebridge/tests/NativeBridge3CreateNamespace_test.cpp
+++ b/libnativebridge/tests/NativeBridge3CreateNamespace_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_CreateNamespace) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3GetError_test.cpp b/libnativebridge/tests/NativeBridge3GetError_test.cpp
index b6b315d..afd0a7d 100644
--- a/libnativebridge/tests/NativeBridge3GetError_test.cpp
+++ b/libnativebridge/tests/NativeBridge3GetError_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_GetError) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3InitAnonymousNamespace_test.cpp b/libnativebridge/tests/NativeBridge3InitAnonymousNamespace_test.cpp
index b1855b4..f82c9e9 100644
--- a/libnativebridge/tests/NativeBridge3InitAnonymousNamespace_test.cpp
+++ b/libnativebridge/tests/NativeBridge3InitAnonymousNamespace_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_InitAnonymousNamespace) {
   // Init
   ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3IsPathSupported_test.cpp b/libnativebridge/tests/NativeBridge3IsPathSupported_test.cpp
index da9cf1c..4cbc0e8 100644
--- a/libnativebridge/tests/NativeBridge3IsPathSupported_test.cpp
+++ b/libnativebridge/tests/NativeBridge3IsPathSupported_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_IsPathSupported) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3LoadLibraryExt_test.cpp b/libnativebridge/tests/NativeBridge3LoadLibraryExt_test.cpp
index 9d037b5..3d66768 100644
--- a/libnativebridge/tests/NativeBridge3LoadLibraryExt_test.cpp
+++ b/libnativebridge/tests/NativeBridge3LoadLibraryExt_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_LoadLibraryExt) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge3UnloadLibrary_test.cpp b/libnativebridge/tests/NativeBridge3UnloadLibrary_test.cpp
index 041fc62..a366edc 100644
--- a/libnativebridge/tests/NativeBridge3UnloadLibrary_test.cpp
+++ b/libnativebridge/tests/NativeBridge3UnloadLibrary_test.cpp
@@ -18,8 +18,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
-
 TEST_F(NativeBridgeTest, V3_UnloadLibrary) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary3, nullptr));
diff --git a/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp b/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp
index 6522483..ba6a007 100644
--- a/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp
+++ b/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp
@@ -19,8 +19,6 @@
 
 namespace android {
 
-constexpr const char* kNativeBridgeLibrary6 = "libnativebridge6-test-case.so";
-
 TEST_F(NativeBridgeTest, V6_PreZygoteFork) {
     // Init
     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary6, nullptr));
diff --git a/libnativebridge/tests/NativeBridgeTest.h b/libnativebridge/tests/NativeBridgeTest.h
index cd0fd01..62509b8 100644
--- a/libnativebridge/tests/NativeBridgeTest.h
+++ b/libnativebridge/tests/NativeBridgeTest.h
@@ -27,6 +27,7 @@
 constexpr const char* kCodeCacheStatFail = "./code_cache/temp";
 constexpr const char* kNativeBridgeLibrary2 = "libnativebridge2-test-case.so";
 constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so";
+constexpr const char* kNativeBridgeLibrary6 = "libnativebridge6-test-case.so";
 
 namespace android {
 
@@ -36,4 +37,3 @@
 };  // namespace android
 
 #endif  // ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGETEST_H_
-
diff --git a/libnativebridge/tests/NativeBridgeTestCase2.cpp b/libnativebridge/tests/NativeBridgeTestCase2.cpp
index 3c75ebf..5164dec 100644
--- a/libnativebridge/tests/NativeBridgeTestCase2.cpp
+++ b/libnativebridge/tests/NativeBridgeTestCase2.cpp
@@ -46,8 +46,8 @@
 }
 
 extern "C" bool native_bridge2_is_compatible_compatible_with(uint32_t version) {
-  // For testing, allow 1 and 2, but disallow 3+.
-  return version <= 2;
+  // For testing, allow 1-3, but disallow later.
+  return version <= 3;
 }
 
 static bool native_bridge2_test_case_signal_handler(int, siginfo_t*, void*) {
@@ -73,4 +73,3 @@
   .isCompatibleWith = &native_bridge2_is_compatible_compatible_with,
   .getSignalHandler = &native_bridge2_get_signal_handler
 };
-
diff --git a/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp b/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp
deleted file mode 100644
index 5a2b0a1..0000000
--- a/libnativebridge/tests/PreInitializeNativeBridgeFail1_test.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2014 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 "NativeBridgeTest.h"
-
-#include <dlfcn.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-
-#include <cstdio>
-#include <cstring>
-
-#include <android/log.h>
-
-namespace android {
-
-TEST_F(NativeBridgeTest, PreInitializeNativeBridgeFail1) {
-  // Needs a valid application directory.
-  ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
-  ASSERT_FALSE(PreInitializeNativeBridge(nullptr, "isa"));
-  ASSERT_TRUE(NativeBridgeError());
-}
-
-}  // namespace android
diff --git a/libnativebridge/tests/preupload_check_test_tag.sh b/libnativebridge/tests/preupload_check_test_tag.sh
new file mode 100755
index 0000000..1c8cd4b
--- /dev/null
+++ b/libnativebridge/tests/preupload_check_test_tag.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+# A simple preupload check that the runtests.sh script has been run for the
+# libnativebridge tests if that library has been changed.
+# TODO(b/189484095): Port these tests to atest and enable in presubmit so we
+# don't need the custom script to run them.
+
+commit_message="$1"
+shift
+
+nativebridge_change=false
+for file; do
+  [[ $file = libnativebridge/* ]] && nativebridge_change=true
+done
+
+if $nativebridge_change; then
+  if grep '^Test: art/libnativebridge/tests/runtests.sh' <<< $commit_message ; then :; else
+    echo "Please run art/libnativebridge/tests/runtests.sh and add the tag:" 1>&2
+    echo "Test: art/libnativebridge/tests/runtests.sh [--skip-host|--skip-target]" 1>&2
+    exit 1
+  fi
+fi
diff --git a/libnativebridge/tests/runtests.sh b/libnativebridge/tests/runtests.sh
new file mode 100755
index 0000000..1eb232d
--- /dev/null
+++ b/libnativebridge/tests/runtests.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+set -e
+
+skip_build=
+skip_host=
+skip_target=
+skip_cleanup=
+for arg; do
+  case "$arg" in
+    --skip-build) skip_build=true ;;
+    --skip-host) skip_host=true ;;
+    --skip-target) skip_target=true ;;
+    --skip-cleanup) skip_cleanup=true ;;
+    *) break ;;
+  esac
+  shift
+done
+
+echo_and_run() {
+  echo "$@"
+  eval "$@"
+}
+
+device_test_root=/data/local/tmp/libnativebridge-test
+
+vars="$(build/soong/soong_ui.bash --dumpvars-mode --vars='HOST_OUT PRODUCT_OUT TARGET_ARCH')"
+# Assign to a variable and eval that, since bash ignores any error status
+# from the command substitution if it's directly on the eval line.
+eval $vars
+
+if [ -z "$skip_build" ]; then
+  rm -rf $HOST_OUT/nativetest{,64} $PRODUCT_OUT/data/nativetest{,64}/art/$TARGET_ARCH
+  echo_and_run build/soong/soong_ui.bash --make-mode MODULES-IN-art-libnativebridge-tests
+fi
+
+if [ -z "$skip_host" ]; then
+  for build_dir in $HOST_OUT/nativetest{,64}/ ; do
+    if [ ! -d $build_dir ]; then
+      echo "Skipping missing $build_dir"
+    else
+      for test_path in $build_dir/*/* ; do
+        test_rel_path=${test_path#${build_dir}/}
+        echo_and_run \( cd $build_dir \; $test_rel_path $* \)
+      done
+    fi
+  done
+fi
+
+if [ -z "$skip_target" ]; then
+  adb root
+  adb wait-for-device
+
+  for build_dir in $PRODUCT_OUT/data/nativetest{,64}/art/$TARGET_ARCH ; do
+    if [ ! -d $build_dir ]; then
+      echo "Skipping missing $build_dir"
+    else
+      test_dir=$device_test_root/$TARGET_ARCH
+
+      echo_and_run adb shell rm -rf $test_dir
+      echo_and_run adb push $build_dir $test_dir
+
+      for test_path in $build_dir/*/* ; do
+        test_rel_path=${test_path#${build_dir}/}
+        echo_and_run adb shell cd $test_dir '\;' LD_LIBRARY_PATH=. $test_rel_path $*
+      done
+    fi
+  done
+
+  if [ -z "$skip_cleanup" ]; then
+    echo_and_run adb shell rm -rf $device_test_root
+  fi
+fi
+
+echo "No errors"