warn_collector_test: make it actually work

The warn_collector_test.c wrapper which calls system() assumes that the
return value of system() is the exit code when it's really a wait status.
That means we'd often get back values like 256 which, when masked with
0377, we get back 0 and so this test would always "pass".

Once we fix that, we see that the script has been failing for all boards
other than link because of hardcoded paths in the setup:
warn_collector: error while loading shared libraries: libmetrics.so:
	cannot open shared object file: No such file or directory

Analysis of these vars show that we haven't even been testing the local
compiled binary.  Instead, we've been trying to test the one installed
into the $SYSROOT due to the $PATH munging.  I cleaned that up as well
as generally tidied up this test script.

Once we fix that, we then see that the collection doesn't work properly
and fails with:
expected 2 lines in warn-test-log, found this:
6c1b181c

This last bug is due to the regex matching less than was expected:
<PRE_WARN>[^ ]+\n         if (WarnStart()) {  /* yytext is file:line */
But the actual line looks like:
Apr 31 25:25:25 localhost kernel: [117959.231100] WARNING: at /mnt/host/source/src/third_party/kernel/files/drivers/gpu/drm/i915/intel_dp.c:351 intel_dp_check_edp+0x6b/0xb9()

You can see there is a space between the <file> and the <symbol> which
means we pass in to the start function in this (*not* the file:line):
intel_dp_check_edp+0x6b/0xb9()

When the test script runs, it tries to use sed to mung the error message
to get a new file path, but the collector only looks at the func name,
so the calculated hashes continue to match.

We can fix this latter part in two ways: change the test to mung the
func name, or change the lex to pass in the file+func name.  I've gone
the latter route as that seems to have been the intention.

BUG=chromium:217382
TEST=`cros_run_unit_tests --board=x86-alex -p crash-reporter` passes

Change-Id: I5b3490c56280bb5260198af930b6744cea585c46
Reviewed-on: https://gerrit.chromium.org/gerrit/56305
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
diff --git a/crash_reporter/warn_collector.l b/crash_reporter/warn_collector.l
index 2f7f8ac..ee3657e 100644
--- a/crash_reporter/warn_collector.l
+++ b/crash_reporter/warn_collector.l
@@ -48,7 +48,7 @@
 
 ^{CUT_HERE}\n{WARNING}          BEGIN(PRE_WARN);
 .|\n                            /* ignore all other input in state 0 */
-<PRE_WARN>[^ ]+\n               if (WarnStart()) {  /* yytext is file:line */
+<PRE_WARN>[^ ]+.[^ ]+\n         if (WarnStart()) {  /* yytext is file:line func+offset/offset() */
                                   BEGIN(WARN); ECHO;
                                 } else {
                                   BEGIN(0);
diff --git a/crash_reporter/warn_collector_test.c b/crash_reporter/warn_collector_test.c
index 67f3455..76707b2 100644
--- a/crash_reporter/warn_collector_test.c
+++ b/crash_reporter/warn_collector_test.c
@@ -10,5 +10,6 @@
 
 int main(int ac, char **av)
 {
-  return system("$SRC/warn_collector_test.sh");
+  int status = system("exec \"${SRC}\"/warn_collector_test.sh");
+  return status < 0 ? EXIT_FAILURE : WEXITSTATUS(status);
 }
diff --git a/crash_reporter/warn_collector_test.sh b/crash_reporter/warn_collector_test.sh
index e1f8a29..0790181 100755
--- a/crash_reporter/warn_collector_test.sh
+++ b/crash_reporter/warn_collector_test.sh
@@ -9,12 +9,20 @@
 
 set -e
 
-SYSROOT=/build/link
-LD_LIBRARY_PATH=$SYSROOT/lib64:$SYSROOT/usr/lib64
-PATH=.:$SYSROOT/bin:$SYSROOT/usr/bin:$PATH
-TESTLOG=warn-test-log
+fail() {
+  printf '[ FAIL ] %b\n' "$*"
+  exit 1
+}
 
-trap cleanup EXIT
+if [[ -z ${SYSROOT} ]]; then
+  fail "SYSROOT must be set for this test to work"
+fi
+: ${OUT:=${PWD}}
+cd "${OUT}"
+PATH=${OUT}:${PATH}
+TESTLOG="${OUT}/warn-test-log"
+
+echo "Testing: $(which warn_collector)"
 
 cleanup() {
   # Kill daemon (if started) on exit
@@ -22,32 +30,30 @@
 }
 
 check_log() {
-  n_expected=$1
-  if [ ! -f $TESTLOG ]; then
-    echo $TESTLOG was not created
-    exit 1
+  local n_expected=$1
+  if [[ ! -f ${TESTLOG} ]]; then
+    fail "${TESTLOG} was not created"
   fi
-  if [ "$(wc -l < $TESTLOG)" -ne $n_expected ]; then
-    echo expected $n_expected lines in $TESTLOG, found this:
-    cat $TESTLOG
-    exit 1
+  if [[ $(wc -l < "${TESTLOG}") -ne ${n_expected} ]]; then
+    fail "expected ${n_expected} lines in ${TESTLOG}, found this instead:
+$(<"${TESTLOG}")"
   fi
-  if egrep -qv '^[0-9a-f]{8}$' $TESTLOG; then
-    echo found bad lines in $TESTLOG:
-    cat $TESTLOG
-    exit 1
+  if egrep -qv '^[0-9a-f]{8}$' "${TESTLOG}"; then
+    fail "found bad lines in ${TESTLOG}:
+$(<"${TESTLOG}")"
   fi
 }
 
-rm -f warn-test-log
-cp $SRC/warn_collector_test_reporter.sh .
-cp $SRC/TEST_WARNING .
+rm -f "${TESTLOG}"
+cp "${SRC}/warn_collector_test_reporter.sh" .
+cp "${SRC}/TEST_WARNING" .
 cp TEST_WARNING messages
 
 # Start the collector daemon.  With the --test option, the daemon reads input
 # from ./messages, writes the warning into ./warning, and invokes
 # ./warn_collector_test_reporter.sh to report the warning.
 warn_collector --test &
+trap cleanup EXIT
 
 # After a while, check that the first warning has been collected.
 sleep 1