Add --error-test option to test error detection and reporting

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh
index b6f39e9..20a20a3 100755
--- a/tests/scripts/all.sh
+++ b/tests/scripts/all.sh
@@ -231,6 +231,8 @@
                         Prefix for a cross-compiler for arm-none-eabi
                         (default: "${ARM_NONE_EABI_GCC_PREFIX}")
      --armcc            Run ARM Compiler builds (on by default).
+     --error-test       Error test mode: run a failing function in addition
+                        to any specified component.
      --except           Exclude the COMPONENTs listed on the command line,
                         instead of running only those.
      --no-append-outcome    Write a new outcome file and analyze it (default).
@@ -378,6 +380,7 @@
 pre_parse_command_line () {
     COMMAND_LINE_COMPONENTS=
     all_except=0
+    error_test=0
     no_armcc=
 
     # Note that legacy options are ignored instead of being omitted from this
@@ -390,6 +393,7 @@
             --armcc) no_armcc=;;
             --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
             --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
+            --error-test) error_test=$((error_test + 1));;
             --except) all_except=1;;
             --force|-f) FORCE=1;;
             --gnutls-cli) shift; GNUTLS_CLI="$1";;
@@ -2636,6 +2640,19 @@
 #### Run all the things
 ################################################################
 
+# Function invoked by --error-test to test error reporting.
+pseudo_component_error_test () {
+    msg "Testing error reporting $error_test"
+    if [ $KEEP_GOING -ne 0 ]; then
+        echo "Expect three failing commands."
+    fi
+    error_test='this should not be used since the component runs in a subshell'
+    grep non_existent /dev/null
+    not grep -q . "$0"
+    make unknown_target
+    false "this should not be executed"
+}
+
 # Run one component and clean up afterwards.
 run_component () {
     # Back up the configuration in case the component modifies it.
@@ -2685,6 +2702,10 @@
 pre_generate_files
 
 # Run the requested tests.
+while [ $error_test -gt 0 ]; do
+    run_component pseudo_component_error_test
+    error_test=$((error_test - 1))
+done
 for component in $RUN_COMPONENTS; do
     run_component "component_$component"
 done