kselftest: Parse kTAP for test results

This stops mangling test output for humans and performs a best-effort
kTAP parsing for the kselftest output (including recursive kTAP output).

Signed-off-by: Kees Cook <keescook@chromium.org>
diff --git a/automated/linux/kselftest/kselftest.sh b/automated/linux/kselftest/kselftest.sh
index 9522a6f..229292f 100755
--- a/automated/linux/kselftest/kselftest.sh
+++ b/automated/linux/kselftest/kselftest.sh
@@ -116,7 +116,34 @@
 
 
 parse_output() {
-    grep "^selftests:" "${LOGFILE}" > "${RESULT_FILE}"
+    perl -ne '
+    if (m|^# selftests: (.*)$|) {
+	$testdir = $1;
+	$testdir =~ s|[:/]\s*|.|g;
+    } elsif (m|^(?:# )*(not )?ok (?:\d+) ([^#]+)(# (SKIP)?)?|) {
+        $not = $1;
+        $test = $2;
+        $skip = $4;
+        $test =~ s|\s+$||;
+        # If the test name starts with "selftests: " it is "fully qualified".
+        if ($test =~ /selftests: (.*)/) {
+            $test = $1;
+	    $test =~ s|[:/]\s*|.|g;
+        } else {
+            # Otherwise, it likely needs the testdir prepended.
+            $test = "$testdir.$test";
+        }
+        # Any appearance of the SKIP is a skip.
+        if ($skip eq "SKIP") {
+            $result="skip";
+        } elsif ($not eq "not ") {
+            $result="fail";
+        } else {
+            $result="pass";
+        }
+	print "$test $result\n";
+    }
+' "${LOGFILE}" > "${RESULT_FILE}"
 }
 
 install() {
@@ -187,5 +214,5 @@
 rm -f "${skips}"
 
 # run_kselftest.sh file generated by kselftest Makefile and included in tarball
-./run_kselftest.sh 2>&1 | tee "${LOGFILE}" | sed 's/^not ok/[FAIL]/'|sed 's/^ok/[PASS]/'|sed 's/://g'|awk '{if ($0 ~ "# SKIP") {$1 = "[SKIP]"} print $0"\n"$3 ": " $4"_"$5 " " $1}'
+./run_kselftest.sh 2>&1 | tee "${LOGFILE}"
 parse_output