psasim: merge all AUT programs into a single executable

This makes both building and testing much faster.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/psa-client-server/psasim/Makefile b/tests/psa-client-server/psasim/Makefile
index a7e22e1..4b0c46e 100644
--- a/tests/psa-client-server/psasim/Makefile
+++ b/tests/psa-client-server/psasim/Makefile
@@ -1,5 +1,3 @@
-MAIN ?= src/client.c
-
 CFLAGS += -Wall -Werror -std=c99 -D_XOPEN_SOURCE=1 -D_POSIX_C_SOURCE=200809L
 
 ifeq ($(DEBUG),1)
@@ -16,11 +14,15 @@
 					include/psa_manifest/pid.h \
 					include/psa_manifest/sid.h
 
-PSA_CLIENT_SRC = src/psa_ff_client.c \
-		 $(MAIN) \
+PSA_CLIENT_COMMON_SRC = src/psa_ff_client.c \
 		 src/psa_sim_crypto_client.c \
 		 src/psa_sim_serialise.c
 
+PSA_CLIENT_BASE_SRC = $(PSA_CLIENT_COMMON_SRC) src/client.c
+
+PSA_CLIENT_FULL_SRC = $(PSA_CLIENT_COMMON_SRC) \
+				$(wildcard src/aut_*.c)
+
 PARTITION_SERVER_BOOTSTRAP = src/psa_ff_bootstrap_TEST_PARTITION.c
 
 PSA_SERVER_SRC = $(PARTITION_SERVER_BOOTSTRAP) \
@@ -35,8 +37,11 @@
 test/seedfile:
 	dd if=/dev/urandom of=./test/seedfile bs=64 count=1
 
-test/psa_client: $(PSA_CLIENT_SRC) $(GENERATED_H_FILES)
-	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_SRC) $(LIBPSACLIENT) $(LDFLAGS) -o $@
+test/psa_client_base: $(PSA_CLIENT_BASE_SRC) $(GENERATED_H_FILES)
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_BASE_SRC) $(LIBPSACLIENT) $(LDFLAGS) -o $@
+
+test/psa_client_full: $(PSA_CLIENT_FULL_SRC) $(GENERATED_H_FILES)
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_FULL_SRC) $(LIBPSACLIENT) $(LDFLAGS) -o $@
 
 test/psa_partition: $(PSA_SERVER_SRC) $(GENERATED_H_FILES) test/seedfile
 	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_SERVER_SRC) $(LIBPSASERVER) $(LDFLAGS) -o $@
@@ -56,7 +61,7 @@
 	$(MAKE) -C $(MBEDTLS_ROOT_PATH) clean
 
 clean:
-	rm -f test/psa_client test/psa_partition
+	rm -f test/psa_client_base test/psa_client_full test/psa_partition
 	rm -f $(PARTITION_SERVER_BOOTSTRAP)
 	rm -rf libpsaclient libpsaserver
 	rm -rf include/psa_manifest
diff --git a/tests/psa-client-server/psasim/src/aut_main.c b/tests/psa-client-server/psasim/src/aut_main.c
new file mode 100644
index 0000000..e1012a5
--- /dev/null
+++ b/tests/psa-client-server/psasim/src/aut_main.c
@@ -0,0 +1,53 @@
+/**
+ * This is the base AUT that exectues all other AUTs meant to test PSA APIs
+ * through PSASIM.
+ */
+
+/*
+ *  Copyright The Mbed TLS Contributors
+ *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+ */
+
+/* First include Mbed TLS headers to get the Mbed TLS configuration and
+ * platform definitions that we'll use in this program. Also include
+ * standard C headers for functions we'll use here. */
+#include "mbedtls/build_info.h"
+
+#include "psa/crypto.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+int psa_hash_compute_main(void);
+int psa_hash_main(void);
+int psa_aead_main(char *cipher_name);
+
+#define TEST_MODULE(main_func) \
+    do {    \
+        char title[128] = { 0 }; \
+        char separator[128] = { 0 }; \
+        int title_len = snprintf(title, sizeof(title), "=== Test: %s ===", #main_func); \
+        memset(separator, '=', title_len); \
+        printf("%s\n%s\n%s\n", separator, title, separator); \
+        ret = main_func; \
+        if (ret != 0) { \
+            goto exit; \
+        } \
+    } while (0)
+
+int main()
+{
+    int ret;
+
+    TEST_MODULE(psa_hash_compute_main());
+    TEST_MODULE(psa_hash_main());
+
+    TEST_MODULE(psa_aead_main("aes128-gcm"));
+    TEST_MODULE(psa_aead_main("aes256-gcm"));
+    TEST_MODULE(psa_aead_main("aes128-gcm_8"));
+    TEST_MODULE(psa_aead_main("chachapoly"));
+
+exit:
+    return (ret != 0) ? 1 : 0;
+}
diff --git a/tests/psa-client-server/psasim/src/aut_psa_aead_demo.c b/tests/psa-client-server/psasim/src/aut_psa_aead.c
similarity index 97%
rename from tests/psa-client-server/psasim/src/aut_psa_aead_demo.c
rename to tests/psa-client-server/psasim/src/aut_psa_aead.c
index 4a46c40..aa9dfb0 100644
--- a/tests/psa-client-server/psasim/src/aut_psa_aead_demo.c
+++ b/tests/psa-client-server/psasim/src/aut_psa_aead.c
@@ -46,7 +46,7 @@
     !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
     !defined(MBEDTLS_CHACHAPOLY_C) || \
     defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER))
-int main(void)
+int psa_aead_main(void)
 {
     printf("MBEDTLS_PSA_CRYPTO_CLIENT or "
            "MBEDTLS_PSA_CRYPTO_C and/or "
@@ -257,21 +257,15 @@
 /*
  * Main function
  */
-int main(int argc, char **argv)
+int psa_aead_main(char *cipher_name)
 {
     psa_status_t status = PSA_SUCCESS;
 
-    /* Check usage */
-    if (argc != 2) {
-        puts(usage);
-        return EXIT_FAILURE;
-    }
-
     /* Initialize the PSA crypto library. */
     PSA_CHECK(psa_crypto_init());
 
     /* Run the demo */
-    PSA_CHECK(aead_demo(argv[1]));
+    PSA_CHECK(aead_demo(cipher_name));
 
     /* Deinitialize the PSA crypto library. */
     mbedtls_psa_crypto_free();
diff --git a/tests/psa-client-server/psasim/src/aut_psa_hash.c b/tests/psa-client-server/psasim/src/aut_psa_hash.c
index 6c2c07e..0446e7a 100644
--- a/tests/psa-client-server/psasim/src/aut_psa_hash.c
+++ b/tests/psa-client-server/psasim/src/aut_psa_hash.c
@@ -1,13 +1,4 @@
 /*
- *  Example computing a SHA-256 hash using the PSA Crypto API
- *
- *  The example computes the SHA-256 hash of a test string using the
- *  one-shot API call psa_hash_compute() and the using multi-part
- *  operation, which requires psa_hash_setup(), psa_hash_update() and
- *  psa_hash_finish(). The multi-part operation is popular on embedded
- *  devices where a rolling hash needs to be computed.
- *
- *
  *  Copyright The Mbed TLS Contributors
  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  */
@@ -20,33 +11,13 @@
 #include "mbedtls/build_info.h"
 #include "mbedtls/platform.h"
 
-/* Information about hashing with the PSA API can be
- * found here:
- * https://arm-software.github.io/psa-api/crypto/1.1/api/ops/hashes.html
- *
- * The algorithm used by this demo is SHA 256.
- * Please see include/psa/crypto_values.h to see the other
- * algorithms that are supported by Mbed TLS.
- * If you switch to a different algorithm you will need to update
- * the hash data in the EXAMPLE_HASH_VALUE macro below. */
-
-#if !defined(MBEDTLS_PSA_CRYPTO_CLIENT) && \
-    (!defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256))
-int main(void)
-{
-    mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256"
-                   "not defined, and not MBEDTLS_PSA_CRYPTO_CLIENT.\r\n");
-    return EXIT_SUCCESS;
-}
-#else
-
 #define HASH_ALG PSA_ALG_SHA_256
 
-const uint8_t sample_message[] = "Hello World!";
+static const uint8_t sample_message[] = "Hello World!";
 /* sample_message is terminated with a null byte which is not part of
  * the message itself so we make sure to subtract it in order to get
  * the message length. */
-const size_t sample_message_length = sizeof(sample_message) - 1;
+static const size_t sample_message_length = sizeof(sample_message) - 1;
 
 #define EXPECTED_HASH_VALUE {                                                    \
         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \
@@ -54,10 +25,10 @@
         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \
 }
 
-const uint8_t expected_hash[] = EXPECTED_HASH_VALUE;
-const size_t expected_hash_len = sizeof(expected_hash);
+static const uint8_t expected_hash[] = EXPECTED_HASH_VALUE;
+static const size_t expected_hash_len = sizeof(expected_hash);
 
-int main(void)
+int psa_hash_main(void)
 {
     psa_status_t status;
     uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)];
@@ -157,4 +128,3 @@
     psa_hash_abort(&cloned_hash_operation);
     return EXIT_FAILURE;
 }
-#endif /* !MBEDTLS_PSA_CRYPTO_C || !PSA_WANT_ALG_SHA_256 */
diff --git a/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c b/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c
index 70c3e5b..959e0c3 100644
--- a/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c
+++ b/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c
@@ -1,15 +1,4 @@
 /*
- *  API(s) under test: psa_hash_compute()
- *
- *  Taken from programs/psa/psa_hash.c, and calls to all hash APIs
- *  but psa_hash_compute() removed.
- *
- *  Example computing a SHA-256 hash using the PSA Crypto API
- *
- *  The example computes the SHA-256 hash of a test string using the
- *  one-shot API call psa_hash_compute().
- *
- *
  *  Copyright The Mbed TLS Contributors
  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  */
@@ -22,33 +11,13 @@
 #include "mbedtls/build_info.h"
 #include "mbedtls/platform.h"
 
-/* Information about hashing with the PSA API can be
- * found here:
- * https://arm-software.github.io/psa-api/crypto/1.1/api/ops/hashes.html
- *
- * The algorithm used by this demo is SHA 256.
- * Please see include/psa/crypto_values.h to see the other
- * algorithms that are supported by Mbed TLS.
- * If you switch to a different algorithm you will need to update
- * the hash data in the EXAMPLE_HASH_VALUE macro below. */
-
-#if !defined(MBEDTLS_PSA_CRYPTO_CLIENT) && \
-    (!defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256))
-int main(void)
-{
-    mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256"
-                   "not defined, and not MBEDTLS_PSA_CRYPTO_CLIENT.\r\n");
-    return EXIT_SUCCESS;
-}
-#else
-
 #define HASH_ALG PSA_ALG_SHA_256
 
-const uint8_t sample_message[] = "Hello World!";
+static const uint8_t sample_message[] = "Hello World!";
 /* sample_message is terminated with a null byte which is not part of
  * the message itself so we make sure to subtract it in order to get
  * the message length. */
-const size_t sample_message_length = sizeof(sample_message) - 1;
+static const size_t sample_message_length = sizeof(sample_message) - 1;
 
 #define EXPECTED_HASH_VALUE {                                                    \
         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \
@@ -56,10 +25,10 @@
         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \
 }
 
-const uint8_t expected_hash[] = EXPECTED_HASH_VALUE;
-const size_t expected_hash_len = sizeof(expected_hash);
+static const uint8_t expected_hash[] = EXPECTED_HASH_VALUE;
+static const size_t expected_hash_len = sizeof(expected_hash);
 
-int main(void)
+int psa_hash_compute_main(void)
 {
     psa_status_t status;
     uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)];
@@ -110,4 +79,3 @@
 cleanup:
     return EXIT_FAILURE;
 }
-#endif /* !MBEDTLS_PSA_CRYPTO_C || !PSA_WANT_ALG_SHA_256 */
diff --git a/tests/psa-client-server/psasim/test/run_test.sh b/tests/psa-client-server/psasim/test/run_test.sh
index 45a317a..7c1011e 100755
--- a/tests/psa-client-server/psasim/test/run_test.sh
+++ b/tests/psa-client-server/psasim/test/run_test.sh
@@ -13,6 +13,9 @@
 
 cd "$(dirname "$0")"
 
+CLIENT_BIN=$1
+shift
+
 function clean_run() {
     rm -f psa_notify_*
     pkill psa_partition || true
@@ -30,8 +33,9 @@
 
 clean_run
 
-./psa_partition -k &
-SERV_PID=$!
+./psa_partition &
 wait_for_server_startup
-./psa_client "$@"
-wait $SERV_PID
+./$CLIENT_BIN "$@"
+
+# Kill server once client exited
+pkill psa_partition
diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh
index 1ea70ff..5093d9a 100755
--- a/tests/scripts/all.sh
+++ b/tests/scripts/all.sh
@@ -6080,47 +6080,16 @@
     msg "build library for client"
     helper_crypto_client_build client
 
-    msg "build psasim to test psa_client"
-    rm -f tests/psa-client-server/psasim/test/psa_client        # In case left behind
-    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_client
+    msg "build basic psasim client"
+    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_client_base
+    msg "test basic psasim client"
+    tests/psa-client-server/psasim/test/run_test.sh psa_client_base
 
-    msg "test psasim"
-    tests/psa-client-server/psasim/test/run_test.sh
+    msg "build full psasim client"
+    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_client_full
+    msg "test full psasim client"
+    tests/psa-client-server/psasim/test/run_test.sh psa_client_full
 
-
-    msg "build psasim to test psa_hash_compute"
-    # Delete the executable to ensure we build using the right MAIN
-    rm tests/psa-client-server/psasim/test/psa_client
-    # API under test: psa_hash_compute()
-    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" MAIN="src/aut_psa_hash_compute.c" test/psa_client
-
-    msg "test psasim running psa_hash_compute"
-    tests/psa-client-server/psasim/test/run_test.sh
-
-
-    # Next APIs under test: psa_hash_*(). Use our copy of the PSA hash example.
-    msg "build psasim to test all psa_hash_* APIs"
-    # Delete the executable to ensure we build using the right MAIN
-    rm tests/psa-client-server/psasim/test/psa_client
-    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" MAIN="src/aut_psa_hash.c" test/psa_client
-
-    msg "test psasim running psa_hash sample"
-    tests/psa-client-server/psasim/test/run_test.sh
-
-
-    # Next APIs under test: psa_aead_*(). Use our copy of the PSA aead example.
-    msg "build psasim to test all psa_aead_* APIs"
-    # Delete the executable to ensure we build using the right MAIN
-    rm tests/psa-client-server/psasim/test/psa_client
-    make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" MAIN="src/aut_psa_aead_demo.c" test/psa_client
-
-    msg "test psasim running psa_aead_demo sample"
-    tests/psa-client-server/psasim/test/run_test.sh aes128-gcm
-    tests/psa-client-server/psasim/test/run_test.sh aes256-gcm
-    tests/psa-client-server/psasim/test/run_test.sh aes128-gcm_8
-    tests/psa-client-server/psasim/test/run_test.sh chachapoly
-
-    msg "clean psasim"
     make -C tests/psa-client-server/psasim clean
 }