test: proper positioning of USE_PSA_INIT + added missing exit labels

Signed-off-by: valerio <valerio.setti@nordicsemi.no>
diff --git a/tests/suites/test_suite_x509parse.function b/tests/suites/test_suite_x509parse.function
index 5b93b12..5abd085 100644
--- a/tests/suites/test_suite_x509parse.function
+++ b/tests/suites/test_suite_x509parse.function
@@ -428,13 +428,14 @@
     mbedtls_x509_crt crt;
     int expected_result = ext_type & has_ext_type;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     crt.ext_types = ext_type;
 
     TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
 
+exit:
     mbedtls_x509_crt_free(&crt);
     USE_PSA_DONE();
 }
@@ -464,8 +465,8 @@
     char *p = buf;
     size_t n = sizeof(buf);
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
     memset(buf, 0, 2000);
 
     TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
@@ -493,7 +494,6 @@
     TEST_ASSERT(strcmp(buf, result_str) == 0);
 
 exit:
-
     mbedtls_x509_crt_free(&crt);
     USE_PSA_DONE();
 }
@@ -506,8 +506,8 @@
     char buf[2000];
     int res;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@@ -531,8 +531,8 @@
     char buf[2000];
     int res;
 
-    USE_PSA_INIT();
     mbedtls_x509_crl_init(&crl);
+    USE_PSA_INIT();
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
@@ -555,8 +555,8 @@
     mbedtls_x509_crl   crl;
     char buf[2000];
 
-    USE_PSA_INIT();
     mbedtls_x509_crl_init(&crl);
+    USE_PSA_INIT();
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
@@ -574,8 +574,8 @@
     char buf[2000];
     int res;
 
-    USE_PSA_INIT();
     mbedtls_x509_csr_init(&csr);
+    USE_PSA_INIT();
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
@@ -606,6 +606,8 @@
     TEST_ASSERT(res >= 0);
 
     TEST_ASSERT(strcmp(buf, result_str) == 0);
+
+exit:
     USE_PSA_DONE();
 }
 /* END_CASE */
@@ -630,10 +632,10 @@
      * - x509_verify() for server5 -> test-ca2:             ~ 18800
      * - x509_verify() for server10 -> int-ca3 -> int-ca2:  ~ 25500
      */
-    MD_OR_USE_PSA_INIT();
     mbedtls_x509_crt_restart_init(&rs_ctx);
     mbedtls_x509_crt_init(&crt);
     mbedtls_x509_crt_init(&ca);
+    MD_OR_USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
@@ -682,10 +684,10 @@
     char *cn_name = NULL;
     const mbedtls_x509_crt_profile *profile;
 
-    MD_OR_USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
     mbedtls_x509_crt_init(&ca);
     mbedtls_x509_crl_init(&crl);
+    MD_OR_USE_PSA_INIT();
 
     if (strcmp(cn_name_str, "NULL") != 0) {
         cn_name = cn_name_str;
@@ -767,9 +769,9 @@
     mbedtls_x509_crt ca;
     uint32_t flags = 0;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
     mbedtls_x509_crt_init(&ca);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
@@ -801,9 +803,10 @@
     uint32_t flags = 0;
     verify_print_context vrfy_ctx;
 
-    MD_OR_USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
     mbedtls_x509_crt_init(&ca);
+    MD_OR_USE_PSA_INIT();
+
     verify_print_init(&vrfy_ctx);
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@@ -838,8 +841,9 @@
     char buf[2000];
     int res = 0;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
+
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@@ -868,8 +872,9 @@
     char buf[2000];
     int res = 0;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
+
     memset(buf, 0, 2000);
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@@ -914,6 +919,7 @@
 
     TEST_EQUAL(ret, exp_ret);
 
+exit:
     mbedtls_free(name);
     USE_PSA_DONE();
 }
@@ -992,8 +998,8 @@
 {
     mbedtls_x509_crt   crt;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
 
@@ -1016,8 +1022,8 @@
 {
     mbedtls_x509_crt   crt;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
 
@@ -1040,8 +1046,8 @@
 {
     mbedtls_x509_crt crt;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
 
@@ -1062,8 +1068,8 @@
     ((void) result_str);
 #endif
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
 #if !defined(MBEDTLS_X509_REMOVE_INFO)
@@ -1147,12 +1153,12 @@
     ((void) result_str);
 #endif
 
-    USE_PSA_INIT();
     oid.tag = MBEDTLS_ASN1_OID;
     oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
     oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
 
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
                                                        &oid) == (result));
@@ -1197,8 +1203,9 @@
     unsigned char output[2000];
     int res;
 
-    USE_PSA_INIT();
     mbedtls_x509_crl_init(&crl);
+    USE_PSA_INIT();
+
     memset(output, 0, 2000);
 
 
@@ -1225,8 +1232,9 @@
     char my_out[1000];
     int my_ret;
 
-    USE_PSA_INIT();
     mbedtls_x509_csr_init(&csr);
+    USE_PSA_INIT();
+
     memset(my_out, 0, sizeof(my_out));
 
     my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
@@ -1251,8 +1259,9 @@
     char my_out[1000];
     int my_ret;
 
-    USE_PSA_INIT();
     mbedtls_x509_csr_init(&csr);
+    USE_PSA_INIT();
+
     memset(my_out, 0, sizeof(my_out));
 
     my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
@@ -1276,8 +1285,8 @@
     mbedtls_x509_crt chain, *cur;
     int i;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&chain);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
 
@@ -1309,9 +1318,9 @@
      * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
      * with NN.crt signed by NN-1.crt
      */
-    MD_OR_USE_PSA_INIT();
     mbedtls_x509_crt_init(&trusted);
     mbedtls_x509_crt_init(&chain);
+    MD_OR_USE_PSA_INIT();
 
     /* Load trusted root */
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
@@ -1347,9 +1356,9 @@
     mbedtls_x509_crt trusted, chain;
     const mbedtls_x509_crt_profile *profile = NULL;
 
-    MD_OR_USE_PSA_INIT();
     mbedtls_x509_crt_init(&chain);
     mbedtls_x509_crt_init(&trusted);
+    MD_OR_USE_PSA_INIT();
 
     while ((act = mystrsep(&chain_paths, " ")) != NULL) {
         TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
@@ -1389,6 +1398,7 @@
     int ret;
 
     USE_PSA_INIT();
+
     oid.tag = MBEDTLS_ASN1_OID;
     oid.p   = buf->x;
     oid.len   = buf->len;
@@ -1403,6 +1413,8 @@
         TEST_ASSERT(desc != NULL);
         TEST_ASSERT(strcmp(desc, ref_desc) == 0);
     }
+
+exit:
     USE_PSA_DONE();
 }
 /* END_CASE */
@@ -1414,6 +1426,7 @@
     char num_buf[100];
 
     USE_PSA_INIT();
+
     memset(num_buf, 0x2a, sizeof(num_buf));
 
     oid.tag = MBEDTLS_ASN1_OID;
@@ -1428,6 +1441,8 @@
         TEST_ASSERT(num_buf[ret] == 0);
         TEST_ASSERT(strcmp(num_buf, numstr) == 0);
     }
+
+exit:
     USE_PSA_DONE();
 }
 /* END_CASE */
@@ -1437,8 +1452,8 @@
 {
     mbedtls_x509_crt crt;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
 
@@ -1456,8 +1471,8 @@
 {
     mbedtls_x509_crt crt;
 
-    USE_PSA_INIT();
     mbedtls_x509_crt_init(&crt);
+    USE_PSA_INIT();
 
     TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
 
@@ -1497,6 +1512,7 @@
         TEST_ASSERT(min  == time.min);
         TEST_ASSERT(sec  == time.sec);
     }
+exit:
     USE_PSA_DONE();
 }
 /* END_CASE */