all.sh: add new component to run test suites with psasim

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh
index 3bff3a8..eb9589a 100755
--- a/tests/scripts/all.sh
+++ b/tests/scripts/all.sh
@@ -217,6 +217,8 @@
     # defined in this script whose name starts with "component_".
     ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
 
+    PSASIM_PATH='tests/psa-client-server/psasim/'
+
     # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
     # the commands set by the environment
 }
@@ -356,6 +358,24 @@
     done
 }
 
+# This is a helper function to be used in psasim builds. It is meant to clean
+# up the library's workspace after the server build and before the client
+# build. Built libraries (mbedcrypto, mbedx509 and mbedtls) are supposed to be
+# already copied to psasim folder at this point.
+cleanup_before_psasim_client() {
+    # Clean up library files
+    make -C library clean
+    # Clean up intermediate files that were used to build the server
+    make -C $PSASIM_PATH clean_server_intermediate_files
+    # Restore files that were backup before building library files. This
+    # includes $CONFIG_H and $CRYPTO_CONFIG_H.
+    for x in $files_to_back_up; do
+        if [[ -e "$x$backup_suffix" ]]; then
+            cp -p "$x$backup_suffix" "$x"
+        fi
+    done
+}
+
 # Final cleanup when this script exits (except when exiting on a failure
 # in non-keep-going mode).
 final_cleanup () {
@@ -948,11 +968,11 @@
     make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include -I../tests -I../../tests -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_TEST_LIBTESTDRIVER1 $loc_accel_flags" LDFLAGS="-ltestdriver1 $ASAN_CFLAGS" "$@"
 }
 
-# $1: target which can be "client" or "server"
-helper_crypto_client_build() {
+# Set some default values $CONFIG_H in order to build server or client sides
+# in PSASIM. There is only 1 mandatory parameter:
+# - $1: target which can be "client" or "server"
+helper_psasim_base_config() {
     TARGET=$1
-    shift
-    TARGET_LIB=libpsa$TARGET
 
     if [ "$TARGET" == "client" ]; then
         scripts/config.py full
@@ -976,8 +996,23 @@
         # Also ensure MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER not set (to match client)
         scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
     fi
+}
 
-    make -C tests/psa-client-server/psasim/ CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@"
+# Helper to build the libraries for client/server in PSASIM. If the server is
+# being built, then it builds also the final executable.
+# There is only 1 mandatory parameter:
+# - $1: target which can be "client" or "server"
+helper_psasim_build() {
+    TARGET=$1
+    shift
+    TARGET_LIB=${TARGET}_libs
+
+    make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@"
+
+    # Build also the server application after its libraries have been built.
+    if [ "$TARGET" == "server" ]; then
+        make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_server
+    fi
 }
 
 ################################################################
@@ -1037,6 +1072,24 @@
     echo "$loc_list"
 }
 
+# Helper function for controlling (start & stop) the psasim server.
+helper_psasim_server() {
+    OPERATION=$1
+    if [ "$OPERATION" == "start" ]; then
+    (
+        cd tests
+        msg "start server"
+        psa-client-server/psasim/test/start_server.sh
+    )
+    else
+    (
+        cd tests
+        msg "terminate server and cleanup"
+        psa-client-server/psasim//test/kill_server.sh
+    )
+    fi
+}
+
 ################################################################
 #### Basic checks
 ################################################################
@@ -6029,20 +6082,16 @@
 }
 
 component_test_psasim() {
-    msg "build library for server"
+    msg "build server library and application"
     scripts/config.py crypto
-    helper_crypto_client_build server
+    helper_psasim_base_config server
+    helper_psasim_build server
 
-    msg "build server"
-    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_partition
-
-    # cleanup() will restore some backed-up files which include $CONFIG_H and
-    # $CRYPTO_CONFIG_H. Built libraries were already copied to psasim at this
-    # point.
-    cleanup
+    cleanup_before_psasim_client
 
     msg "build library for client"
-    helper_crypto_client_build client
+    helper_psasim_base_config client
+    helper_psasim_build client
 
     msg "build basic psasim client"
     make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_client_base
@@ -6057,6 +6106,33 @@
     make -C tests/psa-client-server/psasim clean
 }
 
+component_test_suite_with_psasim()
+{
+    msg "build server library and application"
+    helper_psasim_base_config server
+    # Modify server's library configuration here (if needed)
+    helper_psasim_build server
+
+    cleanup_before_psasim_client
+
+    msg "build client library"
+    helper_psasim_base_config client
+    # PAKE functions are still unsupported from PSASIM
+    scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_JPAKE
+    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
+    helper_psasim_build client
+
+    msg "build test suites"
+    make PSASIM=1 CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" tests
+
+    helper_psasim_server start
+
+    msg "run test suites"
+    make PSASIM=1 test
+
+    helper_psasim_server kill
+}
+
 ################################################################
 #### Termination
 ################################################################