Move TLS PSK setup and cleanup into a common function instead of using
hooks. Hooks are for when the test needs to access the QTestState.

This primarily moves setup of TLS PSK tests from ->start_hook time
earlier into test function call time, which brings the migrate_set_*
calls within earlier, where they can be replaced in subsequent patches
with the new config setup.

Signed-off-by: Fabiano Rosas <[email protected]>
---
 tests/qtest/migration/tls-tests.c | 149 +++++++++++++++++-------------
 1 file changed, 83 insertions(+), 66 deletions(-)

diff --git a/tests/qtest/migration/tls-tests.c 
b/tests/qtest/migration/tls-tests.c
index 2eeed1fc5b..aade57f7de 100644
--- a/tests/qtest/migration/tls-tests.c
+++ b/tests/qtest/migration/tls-tests.c
@@ -23,12 +23,12 @@
 #endif /* CONFIG_TASN1 */
 
 
-struct TestMigrateTLSPSKData {
+typedef struct {
     char *workdir;
     char *workdiralt;
     char *pskfile;
     char *pskfilealt;
-};
+} TestMigrateTLSPSKData;
 
 typedef struct {
     bool mismatch;
@@ -44,59 +44,62 @@ static TestMigrateTLSPSK tls_psk_mismatch = {
 
 static char *tmpfs;
 
-static void *
-migrate_hook_start_tls_psk_common(QTestState *from,
-                                  QTestState *to,
-                                  void *opaque)
+static void *migrate_hook_start_tls_psk_common(QTestState *from,
+                                               QTestState *to,
+                                               void *opaque)
 {
     TestMigrateTLSPSK *args = opaque;
-    struct TestMigrateTLSPSKData *data =
-        g_new0(struct TestMigrateTLSPSKData, 1);
+    g_autofree char *workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
+    g_autofree char *workdiralt = NULL;
 
+    if (args->mismatch) {
+        workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
+    }
+
+    qtest_qmp_assert_success(from,
+                             "{ 'execute': 'object-add',"
+                             "  'arguments': { 'qom-type': 'tls-creds-psk',"
+                             "                 'id': 'tlscredspsk0',"
+                             "                 'endpoint': 'client',"
+                             "                 'dir': %s,"
+                             "                 'username': 'qemu'} }",
+                             workdir);
+
+    qtest_qmp_assert_success(to,
+                             "{ 'execute': 'object-add',"
+                             "  'arguments': { 'qom-type': 'tls-creds-psk',"
+                             "                 'id': 'tlscredspsk0',"
+                             "                 'endpoint': 'server',"
+                             "                 'dir': %s } }",
+                             args->mismatch ? workdiralt : workdir);
+
+    migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
+    migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
+
+    return NULL;
+}
+
+static void migrate_tls_psk_init(MigrateCommon *args,
+                                 TestMigrateTLSPSK *test_args,
+                                 TestMigrateTLSPSKData *data)
+{
     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
                                     QCRYPTO_TLS_CREDS_PSKFILE);
     g_mkdir_with_parents(data->workdir, 0700);
     test_tls_psk_init(data->pskfile);
 
-    if (args->mismatch) {
+    if (test_args->mismatch) {
         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
                                            QCRYPTO_TLS_CREDS_PSKFILE);
         g_mkdir_with_parents(data->workdiralt, 0700);
         test_tls_psk_init_alt(data->pskfilealt);
     }
-
-    qtest_qmp_assert_success(from,
-                             "{ 'execute': 'object-add',"
-                             "  'arguments': { 'qom-type': 'tls-creds-psk',"
-                             "                 'id': 'tlscredspsk0',"
-                             "                 'endpoint': 'client',"
-                             "                 'dir': %s,"
-                             "                 'username': 'qemu'} }",
-                             data->workdir);
-
-    qtest_qmp_assert_success(to,
-                             "{ 'execute': 'object-add',"
-                             "  'arguments': { 'qom-type': 'tls-creds-psk',"
-                             "                 'id': 'tlscredspsk0',"
-                             "                 'endpoint': 'server',"
-                             "                 'dir': %s } }",
-                             args->mismatch ? data->workdiralt : 
data->workdir);
-
-    migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
-    migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
-
-    return data;
 }
 
-static void
-migrate_hook_end_tls_psk(QTestState *from,
-                         QTestState *to,
-                         void *opaque)
+static void migrate_tls_psk_cleanup(TestMigrateTLSPSKData *data)
 {
-    struct TestMigrateTLSPSKData *data = opaque;
-
     test_tls_psk_cleanup(data->pskfile);
     if (data->pskfilealt) {
         test_tls_psk_cleanup(data->pskfilealt);
@@ -113,6 +116,36 @@ migrate_hook_end_tls_psk(QTestState *from,
     g_free(data);
 }
 
+static void test_precopy_tls_psk_common(MigrateCommon *args,
+                                        TestMigrateTLSPSK *test_args)
+{
+    TestMigrateTLSPSKData *data = g_new0(TestMigrateTLSPSKData, 1);
+
+    migrate_tls_psk_init(args, test_args, data);
+    test_precopy_common(args);
+    migrate_tls_psk_cleanup(data);
+}
+
+static void test_postcopy_tls_psk_common(MigrateCommon *args,
+                                        TestMigrateTLSPSK *test_args)
+{
+    TestMigrateTLSPSKData *data = g_new0(TestMigrateTLSPSKData, 1);
+
+    migrate_tls_psk_init(args, test_args, data);
+    test_postcopy_common(args);
+    migrate_tls_psk_cleanup(data);
+}
+
+static void test_postcopy_recovery_tls_psk_common(MigrateCommon *args,
+                                                  TestMigrateTLSPSK *test_args)
+{
+    TestMigrateTLSPSKData *data = g_new0(TestMigrateTLSPSKData, 1);
+
+    migrate_tls_psk_init(args, test_args, data);
+    test_postcopy_recovery_common(args);
+    migrate_tls_psk_cleanup(data);
+}
+
 #ifdef CONFIG_TASN1
 typedef struct {
     char *workdir;
@@ -340,29 +373,26 @@ static void test_postcopy_tls_psk(char *name, 
MigrateCommon *args)
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
-    test_postcopy_common(args);
+    test_postcopy_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_postcopy_preempt_tls_psk(char *name, MigrateCommon *args)
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
     args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT] = true;
 
-    test_postcopy_common(args);
+    test_postcopy_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_postcopy_recovery_tls_psk(char *name, MigrateCommon *args)
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
-    test_postcopy_recovery_common(args);
+    test_postcopy_recovery_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_multifd_postcopy_recovery_tls_psk(char *name,
@@ -370,11 +400,10 @@ static void test_multifd_postcopy_recovery_tls_psk(char 
*name,
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
     args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
 
-    test_postcopy_recovery_common(args);
+    test_postcopy_recovery_tls_psk_common(args, &tls_psk_match);
 }
 
 /* This contains preempt+recovery+tls test altogether */
@@ -382,11 +411,10 @@ static void test_postcopy_preempt_all(char *name, 
MigrateCommon *args)
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
     args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT] = true;
 
-    test_postcopy_recovery_common(args);
+    test_postcopy_recovery_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_multifd_postcopy_preempt_recovery_tls_psk(char *name,
@@ -394,12 +422,11 @@ static void 
test_multifd_postcopy_preempt_recovery_tls_psk(char *name,
 {
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
     args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
     args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT] = true;
 
-    test_postcopy_recovery_common(args);
+    test_postcopy_recovery_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_precopy_unix_tls_psk(char *name, MigrateCommon *args)
@@ -410,9 +437,8 @@ static void test_precopy_unix_tls_psk(char *name, 
MigrateCommon *args)
     args->listen_uri = uri;
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_match);
 }
 
 #ifdef CONFIG_TASN1
@@ -451,9 +477,8 @@ static void test_precopy_tcp_tls_psk_match(char *name, 
MigrateCommon *args)
     args->listen_uri = "tcp:127.0.0.1:0";
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_precopy_tcp_tls_psk_mismatch(char *name, MigrateCommon *args)
@@ -461,30 +486,25 @@ static void test_precopy_tcp_tls_psk_mismatch(char *name, 
MigrateCommon *args)
     args->listen_uri = "tcp:127.0.0.1:0";
     args->start_hook_full = migrate_hook_start_tls_psk_common;
     args->start_hook_data = &tls_psk_mismatch;
-    args->end_hook = migrate_hook_end_tls_psk;
     args->result = MIG_TEST_FAIL;
 
     args->start.hide_stderr = true;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_mismatch);
 }
 
 static void *migrate_hook_start_no_tls(QTestState *from, QTestState *to)
 {
-    struct TestMigrateTLSPSKData *data =
-        g_new0(struct TestMigrateTLSPSKData, 1);
-
     migrate_set_parameter_null(from, "tls-creds");
     migrate_set_parameter_null(to, "tls-creds");
 
-    return data;
+    return NULL;
 }
 
 static void test_precopy_tcp_no_tls(char *name, MigrateCommon *args)
 {
     args->listen_uri = "tcp:127.0.0.1:0";
     args->start_hook = migrate_hook_start_no_tls;
-    args->end_hook = migrate_hook_end_tls_psk;
 
     test_precopy_common(args);
 }
@@ -657,19 +677,17 @@ 
migrate_hook_start_multifd_tls_x509_reject_anon_client(QTestState *from,
 static void test_multifd_tcp_tls_psk_match(char *name, MigrateCommon *args)
 {
     args->start_hook = migrate_hook_start_multifd_tcp_tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
     args->listen_uri = "tcp:127.0.0.1:0";
 
     args->start.incoming_defer = true;
     args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_match);
 }
 
 static void test_multifd_tcp_tls_psk_mismatch(char *name, MigrateCommon *args)
 {
     args->start_hook = migrate_hook_start_multifd_tcp_tls_psk_mismatch;
-    args->end_hook = migrate_hook_end_tls_psk;
     args->result = MIG_TEST_FAIL;
     args->listen_uri = "tcp:127.0.0.1:0";
 
@@ -677,21 +695,20 @@ static void test_multifd_tcp_tls_psk_mismatch(char *name, 
MigrateCommon *args)
     args->start.incoming_defer = true;
     args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_mismatch);
 }
 
 static void test_multifd_postcopy_tcp_tls_psk_match(char *name,
                                                     MigrateCommon *args)
 {
     args->start_hook = migrate_hook_start_multifd_tcp_tls_psk_match;
-    args->end_hook = migrate_hook_end_tls_psk;
     args->listen_uri = "tcp:127.0.0.1:0";
 
     args->start.incoming_defer = true;
     args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
     args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] = true;
 
-    test_precopy_common(args);
+    test_precopy_tls_psk_common(args, &tls_psk_match);
 }
 
 #ifdef CONFIG_TASN1
-- 
2.51.0


Reply via email to