Merge pull request #9558 from gilles-peskine-arm/run-test-suites-on-subproject

Run test suites on subproject
diff --git a/tests/Makefile b/tests/Makefile
index 63df02c..66bb1cd 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -281,7 +281,6 @@
 # Test suites caught by SKIP_TEST_SUITES are built but not executed.
 check: $(BINARIES) $(CRYPTO_BINARIES)
 	perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
-	cd ../tf-psa-crypto/tests && perl ../../tests/scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
 
 test: check
 
diff --git a/tests/scripts/basic-build-test.sh b/tests/scripts/basic-build-test.sh
index b850147..4adae9a 100755
--- a/tests/scripts/basic-build-test.sh
+++ b/tests/scripts/basic-build-test.sh
@@ -35,8 +35,6 @@
     exit 1
 fi
 
-MBEDTLS_ROOT_DIR="$PWD"
-
 : ${OPENSSL:="openssl"}
 : ${GNUTLS_CLI:="gnutls-cli"}
 : ${GNUTLS_SERV:="gnutls-serv"}
@@ -81,26 +79,21 @@
 
 # Step 2 - Execute the tests
 TEST_OUTPUT=out_${PPID}
-cd $MBEDTLS_ROOT_DIR/tests
+cd tests
 if [ ! -f "seedfile" ]; then
     dd if=/dev/urandom of="seedfile" bs=64 count=1
 fi
-cd $MBEDTLS_ROOT_DIR/tf-psa-crypto/tests
-if [ ! -f "seedfile" ]; then
-    dd if=/dev/urandom of="seedfile" bs=64 count=1
+if [ ! -f "../tf-psa-crypto/tests/seedfile" ]; then
+    cp "seedfile" "../tf-psa-crypto/tests/seedfile"
 fi
 echo
 
 # Step 2a - Unit Tests (keep going even if some tests fail)
 echo '################ Unit tests ################'
-cd $MBEDTLS_ROOT_DIR/tests
-perl scripts/run-test-suites.pl -v 2 |tee tls-x509-unit-test-$TEST_OUTPUT
-cd $MBEDTLS_ROOT_DIR/tf-psa-crypto/tests
-perl $MBEDTLS_ROOT_DIR/tests/scripts/run-test-suites.pl -v 2 |tee ../../tests/crypto-unit-test-$TEST_OUTPUT
+perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
 echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
 echo
 
-cd $MBEDTLS_ROOT_DIR/tests
 # Step 2b - System Tests (keep going even if some tests fail)
 echo
 echo '################ ssl-opt.sh ################'
@@ -151,13 +144,13 @@
 
     cd tests
 
-    # Step 4a - TLS and x509 unit tests
-    echo "TLS and x509 unit tests - tests/scripts/run-test-suites.pl"
+    # Step 4a - Unit tests
+    echo "Unit tests - tests/scripts/run-test-suites.pl"
 
-    PASSED_TESTS=$(tail -n6 tls-x509-unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
-    SKIPPED_TESTS=$(tail -n6 tls-x509-unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
-    TOTAL_SUITES=$(tail -n6 tls-x509-unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
-    FAILED_TESTS=$(tail -n6 tls-x509-unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
+    PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
+    SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
+    TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
+    FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
 
     echo "No test suites     : $TOTAL_SUITES"
     echo "Passed             : $PASSED_TESTS"
@@ -173,29 +166,7 @@
     TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
     TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
 
-    # Step 4b - Crypto unit tests
-    echo "Crypto unit tests - tests/scripts/run-test-suites.pl"
-
-    PASSED_TESTS=$(tail -n6 crypto-unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
-    SKIPPED_TESTS=$(tail -n6 crypto-unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
-    TOTAL_SUITES=$(tail -n6 crypto-unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
-    FAILED_TESTS=$(tail -n6 crypto-unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
-
-    echo "No test suites     : $TOTAL_SUITES"
-    echo "Passed             : $PASSED_TESTS"
-    echo "Failed             : $FAILED_TESTS"
-    echo "Skipped            : $SKIPPED_TESTS"
-    echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
-    echo "Total avail tests  : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
-    echo
-
-    TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
-    TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
-    TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
-    TOTAL_AVAIL=$(($TOTAL_AVAIL + $PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
-    TOTAL_EXED=$(($TOTAL_EXED + $PASSED_TESTS + $FAILED_TESTS))
-
-    # Step 4c - TLS Options tests
+    # Step 4b - TLS Options tests
     echo "TLS Options tests - tests/ssl-opt.sh"
 
     PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
@@ -217,7 +188,7 @@
     TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
 
 
-    # Step 4d - System Compatibility tests
+    # Step 4c - System Compatibility tests
     echo "System/Compatibility tests - tests/compat.sh"
 
     PASSED_TESTS=$(cat compat-test-$TEST_OUTPUT | sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p' | awk 'BEGIN{ s = 0 } { s += $1 } END{ print s }')
@@ -239,7 +210,7 @@
     TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
 
 
-    # Step 4e - Grand totals
+    # Step 4d - Grand totals
     echo "-------------------------------------------------------------------------"
     echo "Total tests"
 
@@ -251,13 +222,12 @@
     echo
 
 
-    # Step 4f - Coverage report
+    # Step 4e - Coverage report
     echo "Coverage statistics:"
     sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
     echo
 
-    rm tls-x509-unit-test-$TEST_OUTPUT
-    rm crypto-unit-test-$TEST_OUTPUT
+    rm unit-test-$TEST_OUTPUT
     rm sys-test-$TEST_OUTPUT
     rm compat-test-$TEST_OUTPUT
     rm cov-$TEST_OUTPUT
diff --git a/tests/scripts/run-test-suites.pl b/tests/scripts/run-test-suites.pl
index 408deaf..e01d44f 100755
--- a/tests/scripts/run-test-suites.pl
+++ b/tests/scripts/run-test-suites.pl
@@ -28,6 +28,7 @@
 use utf8;
 use open qw(:std utf8);
 
+use Cwd qw(getcwd);
 use Getopt::Long qw(:config auto_help gnu_compat);
 use Pod::Usage;
 
@@ -40,7 +41,8 @@
 
 # All test suites = executable files with a .datax file.
 my @suites = ();
-for my $data_file (glob 'test_suite_*.datax') {
+my @test_dirs = qw(../tf-psa-crypto/tests .);
+for my $data_file (map {glob "$_/test_suite_*.datax"} @test_dirs) {
     (my $base = $data_file) =~ s/\.datax$//;
     push @suites, $base if -x $base;
     push @suites, "$base.exe" if -e "$base.exe";
@@ -59,15 +61,8 @@
       ')(\z|\.)' );
 
 # in case test suites are linked dynamically
-if (-d '../../tf-psa-crypto') {
-    $ENV{'LD_LIBRARY_PATH'} = '../../library';
-    $ENV{'DYLD_LIBRARY_PATH'} = '../../library';
-}
-else
-{
-    $ENV{'LD_LIBRARY_PATH'} = '../library';
-    $ENV{'DYLD_LIBRARY_PATH'} = '../library';
-}
+$ENV{'LD_LIBRARY_PATH'} = getcwd() . "/../library";
+$ENV{'DYLD_LIBRARY_PATH'} = $ENV{'LD_LIBRARY_PATH'}; # For macOS
 
 my $prefix = $^O eq "MSWin32" ? '' : './';
 
@@ -82,8 +77,13 @@
     print $padchar x( $padlen ), " $string ", $padchar x( $padlen ), "\n";
 }
 
-for my $suite (@suites)
+for my $suite_path (@suites)
 {
+    my ($dir, $suite) = ('.', $suite_path);
+    if ($suite =~ m!(.*)/([^/]*)!) {
+        $dir = $1;
+        $suite = $2;
+    }
     print "$suite ", "." x ( 72 - length($suite) - 2 - 4 ), " ";
     if( $suite =~ /$skip_re/o ) {
         print "SKIP\n";
@@ -91,7 +91,7 @@
         next;
     }
 
-    my $command = "$prefix$suite";
+    my $command = "cd $dir && $prefix$suite";
     if( $verbose ) {
         $command .= ' -v';
     }