Ensure that run-test preserves exit status

Run-test was squashing the exit status of test programs. This made it
less robust then it should be. We also changed several tests that
relied on this behavior to instead explicitly check for an expected
exit value.

Test: ./test.py --host -j50

Change-Id: Ic4455db1ce5f46cf7446f2f29909241d55beb144
diff --git a/test/030-bad-finalizer/expected.txt b/test/030-bad-finalizer/expected.txt
index 74e208c..0aca6f2 100644
--- a/test/030-bad-finalizer/expected.txt
+++ b/test/030-bad-finalizer/expected.txt
@@ -2,3 +2,4 @@
 Finalizer started and spinning...
 Finalizer done spinning.
 Finalizer sleeping forever now.
+exit status: 2
diff --git a/test/030-bad-finalizer/run b/test/030-bad-finalizer/run
new file mode 100755
index 0000000..7a0d0d0
--- /dev/null
+++ b/test/030-bad-finalizer/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright 2017 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.
+
+# Squash the exit status and put it in expected
+./default-run "$@"
+echo "exit status:" $?
diff --git a/test/034-call-null/expected.txt b/test/034-call-null/expected.txt
index 4e0281e..11aefde 100644
--- a/test/034-call-null/expected.txt
+++ b/test/034-call-null/expected.txt
@@ -1,2 +1,3 @@
 Exception in thread "main" java.lang.NullPointerException: Attempt to invoke direct method 'void Main.doStuff(int, int[][], java.lang.String, java.lang.String[][])' on a null object reference
 	at Main.main(Main.java:26)
+exit status: 1
diff --git a/test/034-call-null/run b/test/034-call-null/run
new file mode 100755
index 0000000..7a0d0d0
--- /dev/null
+++ b/test/034-call-null/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright 2017 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.
+
+# Squash the exit status and put it in expected
+./default-run "$@"
+echo "exit status:" $?
diff --git a/test/038-inner-null/expected.txt b/test/038-inner-null/expected.txt
index 2e92564..d148eff 100644
--- a/test/038-inner-null/expected.txt
+++ b/test/038-inner-null/expected.txt
@@ -2,3 +2,4 @@
 Exception in thread "main" java.lang.NullPointerException: Attempt to invoke virtual method 'void Main$Blort.repaint()' on a null object reference
 	at Main$Special.callInner(Main.java:31)
 	at Main.main(Main.java:20)
+exit status: 1
diff --git a/test/038-inner-null/run b/test/038-inner-null/run
new file mode 100755
index 0000000..7a0d0d0
--- /dev/null
+++ b/test/038-inner-null/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright 2017 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.
+
+# Squash the exit status and put it in expected
+./default-run "$@"
+echo "exit status:" $?
diff --git a/test/054-uncaught/expected.txt b/test/054-uncaught/expected.txt
index 7d7f03c..878260a 100644
--- a/test/054-uncaught/expected.txt
+++ b/test/054-uncaught/expected.txt
@@ -18,3 +18,4 @@
 java.lang.NullPointerException: Hi diddly-ho, neighborino.
 	at Main.catchTheUncaught(Main.java:63)
 	at Main.main(Main.java:26)
+exit status: 1
diff --git a/test/054-uncaught/run b/test/054-uncaught/run
new file mode 100755
index 0000000..7a0d0d0
--- /dev/null
+++ b/test/054-uncaught/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright 2017 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.
+
+# Squash the exit status and put it in expected
+./default-run "$@"
+echo "exit status:" $?
diff --git a/test/714-invoke-custom-lambda-metafactory/expected.txt b/test/714-invoke-custom-lambda-metafactory/expected.txt
index cbe9840..54b6c24 100644
--- a/test/714-invoke-custom-lambda-metafactory/expected.txt
+++ b/test/714-invoke-custom-lambda-metafactory/expected.txt
@@ -2,3 +2,4 @@
 	at Main.main(Main.java:25)
 Caused by: java.lang.NullPointerException: Bootstrap method returned null
 	... 1 more
+exit status: 1
diff --git a/test/714-invoke-custom-lambda-metafactory/run b/test/714-invoke-custom-lambda-metafactory/run
new file mode 100755
index 0000000..7a0d0d0
--- /dev/null
+++ b/test/714-invoke-custom-lambda-metafactory/run
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Copyright 2017 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.
+
+# Squash the exit status and put it in expected
+./default-run "$@"
+echo "exit status:" $?
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index 055cffb..59c9022 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -886,11 +886,14 @@
       adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
     fi
 
+    exit_status=0
     if [ "$DRY_RUN" != "y" ]; then
       adb shell sh $DEX_LOCATION/cmdline.sh
+      exit_status=$?
     fi
 
     rm -f $cmdfile
+    exit $exit_status
 else
     # Host run.
     export ANDROID_PRINTF_LOG=brief
@@ -996,10 +999,12 @@
         trap 'kill -INT -$pid' INT
         $cmdline "$@" 2>&1 & pid=$!
         wait $pid
+        exit_value=$?
         # Add extra detail if time out is enabled.
-        if [ ${PIPESTATUS[0]} = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
+        if [ $exit_value = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
           echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
         fi
+        exit $exit_value
       else
         # With a thread dump that uses gdb if a timeout.
         trap 'kill -INT -$pid' INT
@@ -1022,6 +1027,7 @@
             # The test timed out.
             echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
           fi
+          exit $test_exit_status
         fi
       fi
     fi