The hash/hmac tests iterate over all support algorithms testing
each one in turn, but this is not visible from the test output.
Register one test per algorithm so the test result directly
reflect what algorithms were tested. This is useful information
since different crypto backends support different subsets of
algorithms.

Signed-off-by: Daniel P. Berrangé <[email protected]>
---
 tests/unit/test-crypto-hash.c | 315 ++++++++++++++++------------------
 tests/unit/test-crypto-hmac.c | 268 ++++++++++++++---------------
 2 files changed, 271 insertions(+), 312 deletions(-)

diff --git a/tests/unit/test-crypto-hash.c b/tests/unit/test-crypto-hash.c
index 1813e77b5a5..fbdae46fd64 100644
--- a/tests/unit/test-crypto-hash.c
+++ b/tests/unit/test-crypto-hash.c
@@ -92,218 +92,191 @@ static const int expected_lens[] = {
 static const char hex[] = "0123456789abcdef";
 
 /* Test with dynamic allocation */
-static void test_hash_alloc(void)
+static void test_hash_alloc(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        uint8_t *result = NULL;
-        size_t resultlen = 0;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
-
-        ret = qcrypto_hash_bytes(i,
-                                 INPUT_TEXT,
-                                 strlen(INPUT_TEXT),
-                                 &result,
-                                 &resultlen,
-                                 &error_fatal);
-        g_assert(ret == 0);
-        g_assert(resultlen == expected_lens[i]);
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    uint8_t *result = NULL;
+    size_t resultlen = 0;
+    int ret;
+    size_t j;
+
+    ret = qcrypto_hash_bytes(alg,
+                             INPUT_TEXT,
+                             strlen(INPUT_TEXT),
+                             &result,
+                             &resultlen,
+                             &error_fatal);
+    g_assert(ret == 0);
+    g_assert(resultlen == expected_lens[alg]);
 
-        for (j = 0; j < resultlen; j++) {
-            g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 
0xf]);
-            g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
-        }
-        g_free(result);
+    for (j = 0; j < resultlen; j++) {
+        g_assert(expected_outputs[alg][j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(expected_outputs[alg][j * 2 + 1] == hex[result[j] & 0xf]);
     }
+    g_free(result);
 }
 
 /* Test with caller preallocating */
-static void test_hash_prealloc(void)
+static void test_hash_prealloc(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        uint8_t *result, *origresult;
-        size_t resultlen;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
-
-        resultlen = expected_lens[i];
-        origresult = result = g_new0(uint8_t, resultlen);
-
-        ret = qcrypto_hash_bytes(i,
-                                 INPUT_TEXT,
-                                 strlen(INPUT_TEXT),
-                                 &result,
-                                 &resultlen,
-                                 &error_fatal);
-        g_assert(ret == 0);
-        /* Validate that our pre-allocated pointer was not replaced */
-        g_assert(result == origresult);
-        g_assert(resultlen == expected_lens[i]);
-        for (j = 0; j < resultlen; j++) {
-            g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 
0xf]);
-            g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
-        }
-        g_free(result);
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    uint8_t *result, *origresult;
+    size_t resultlen;
+    int ret;
+    size_t j;
+
+    resultlen = expected_lens[alg];
+    origresult = result = g_new0(uint8_t, resultlen);
+
+    ret = qcrypto_hash_bytes(alg,
+                             INPUT_TEXT,
+                             strlen(INPUT_TEXT),
+                             &result,
+                             &resultlen,
+                             &error_fatal);
+    g_assert(ret == 0);
+    /* Validate that our pre-allocated pointer was not replaced */
+    g_assert(result == origresult);
+    g_assert(resultlen == expected_lens[alg]);
+    for (j = 0; j < resultlen; j++) {
+        g_assert(expected_outputs[alg][j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(expected_outputs[alg][j * 2 + 1] == hex[result[j] & 0xf]);
     }
+    g_free(result);
 }
 
 
 /* Test with dynamic allocation */
-static void test_hash_iov(void)
+static void test_hash_iov(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        struct iovec iov[3] = {
-            { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) 
},
-            { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) 
},
-            { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) 
},
-        };
-        uint8_t *result = NULL;
-        size_t resultlen = 0;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
-
-        ret = qcrypto_hash_bytesv(i,
-                                  iov, 3,
-                                  &result,
-                                  &resultlen,
-                                  &error_fatal);
-        g_assert(ret == 0);
-        g_assert(resultlen == expected_lens[i]);
-        for (j = 0; j < resultlen; j++) {
-            g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 
0xf]);
-            g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
-        }
-        g_free(result);
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    struct iovec iov[3] = {
+        { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
+        { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
+        { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
+    };
+    uint8_t *result = NULL;
+    size_t resultlen = 0;
+    int ret;
+    size_t j;
+
+    ret = qcrypto_hash_bytesv(alg,
+                              iov, 3,
+                              &result,
+                              &resultlen,
+                              &error_fatal);
+    g_assert(ret == 0);
+    g_assert(resultlen == expected_lens[alg]);
+    for (j = 0; j < resultlen; j++) {
+        g_assert(expected_outputs[alg][j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(expected_outputs[alg][j * 2 + 1] == hex[result[j] & 0xf]);
     }
+    g_free(result);
 }
 
 
 /* Test with printable hashing */
-static void test_hash_digest(void)
+static void test_hash_digest(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        int ret;
-        char *digest;
-        size_t digestsize;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    int ret;
+    char *digest;
+    size_t digestsize;
 
-        digestsize = qcrypto_hash_digest_len(i);
+    digestsize = qcrypto_hash_digest_len(alg);
 
-        g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
+    g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[alg]));
 
-        ret = qcrypto_hash_digest(i,
-                                  INPUT_TEXT,
-                                  strlen(INPUT_TEXT),
-                                  &digest,
-                                  &error_fatal);
-        g_assert(ret == 0);
-        g_assert_cmpstr(digest, ==, expected_outputs[i]);
-        g_free(digest);
-    }
+    ret = qcrypto_hash_digest(alg,
+                              INPUT_TEXT,
+                              strlen(INPUT_TEXT),
+                              &digest,
+                              &error_fatal);
+    g_assert(ret == 0);
+    g_assert_cmpstr(digest, ==, expected_outputs[alg]);
+    g_free(digest);
 }
 
 /* Test with base64 encoding */
-static void test_hash_base64(void)
+static void test_hash_base64(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        int ret;
-        char *digest;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
-
-        ret = qcrypto_hash_base64(i,
-                                  INPUT_TEXT,
-                                  strlen(INPUT_TEXT),
-                                  &digest,
-                                  &error_fatal);
-        g_assert(ret == 0);
-        g_assert_cmpstr(digest, ==, expected_outputs_b64[i]);
-        g_free(digest);
-    }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    int ret;
+    char *digest;
+
+    ret = qcrypto_hash_base64(alg,
+                              INPUT_TEXT,
+                              strlen(INPUT_TEXT),
+                              &digest,
+                              &error_fatal);
+    g_assert(ret == 0);
+    g_assert_cmpstr(digest, ==, expected_outputs_b64[alg]);
+    g_free(digest);
 }
 
-static void test_hash_accumulate(void)
+static void test_hash_accumulate(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
-        g_autoptr(QCryptoHash) hash = NULL;
-        struct iovec iov[] = {
-            { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) 
},
-            { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) 
},
-            { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) 
},
-        };
-        g_autofree uint8_t *result = NULL;
-        size_t resultlen = 0;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hash_supports(i)) {
-            continue;
-        }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    g_autoptr(QCryptoHash) hash = NULL;
+    struct iovec iov[] = {
+        { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
+        { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
+        { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
+    };
+    g_autofree uint8_t *result = NULL;
+    size_t resultlen = 0;
+    int ret;
+    size_t j;
+
+    hash = qcrypto_hash_new(alg, &error_fatal);
+    g_assert(hash != NULL);
+
+    /* Add each iovec to the hash context separately */
+    for (j = 0; j < G_N_ELEMENTS(iov); j++) {
+        ret = qcrypto_hash_updatev(hash,
+                                   &iov[j], 1,
+                                   &error_fatal);
 
-        hash = qcrypto_hash_new(i, &error_fatal);
-        g_assert(hash != NULL);
+        g_assert(ret == 0);
+    }
 
-        /* Add each iovec to the hash context separately */
-        for (j = 0; j < G_N_ELEMENTS(iov); j++) {
-            ret = qcrypto_hash_updatev(hash,
-                                      &iov[j], 1,
+    ret = qcrypto_hash_finalize_bytes(hash, &result, &resultlen,
                                       &error_fatal);
 
-            g_assert(ret == 0);
-        }
-
-        ret = qcrypto_hash_finalize_bytes(hash, &result, &resultlen,
-                                          &error_fatal);
-
-        g_assert(ret == 0);
-        g_assert(resultlen == expected_lens[i]);
-        for (j = 0; j < resultlen; j++) {
-            g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 
0xf]);
-            g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
-        }
+    g_assert(ret == 0);
+    g_assert(resultlen == expected_lens[alg]);
+    for (j = 0; j < resultlen; j++) {
+        g_assert(expected_outputs[alg][j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(expected_outputs[alg][j * 2 + 1] == hex[result[j] & 0xf]);
     }
 }
 
 int main(int argc, char **argv)
 {
+    size_t i;
     int ret = qcrypto_init(&error_fatal);
     g_assert(ret == 0);
 
     g_test_init(&argc, &argv, NULL);
-    g_test_add_func("/crypto/hash/iov", test_hash_iov);
-    g_test_add_func("/crypto/hash/alloc", test_hash_alloc);
-    g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc);
-    g_test_add_func("/crypto/hash/digest", test_hash_digest);
-    g_test_add_func("/crypto/hash/base64", test_hash_base64);
-    g_test_add_func("/crypto/hash/accumulate", test_hash_accumulate);
+
+#define ADD_TEST(alg, prefix, meth) \
+    { \
+        g_autofree char *name = g_strdup_printf( \
+            "%s/%s", prefix, QCryptoHashAlgo_str(alg)); \
+        g_test_add_data_func(name, GINT_TO_POINTER(alg), meth); \
+    }
+
+    for (i = 0; i < QCRYPTO_HASH_ALGO__MAX ; i++) {
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
+        ADD_TEST(i, "/crypto/hash/iov", test_hash_iov);
+        ADD_TEST(i, "/crypto/hash/alloc", test_hash_alloc);
+        ADD_TEST(i, "/crypto/hash/prealloc", test_hash_prealloc);
+        ADD_TEST(i, "/crypto/hash/digest", test_hash_digest);
+        ADD_TEST(i, "/crypto/hash/base64", test_hash_base64);
+        ADD_TEST(i, "/crypto/hash/accumulate", test_hash_accumulate);
+    }
     return g_test_run();
 }
diff --git a/tests/unit/test-crypto-hmac.c b/tests/unit/test-crypto-hmac.c
index f7158621908..9c2ee8c17f8 100644
--- a/tests/unit/test-crypto-hmac.c
+++ b/tests/unit/test-crypto-hmac.c
@@ -86,177 +86,163 @@ static QCryptoHmacTestData test_data[] = {
 
 static const char hex[] = "0123456789abcdef";
 
-static void test_hmac_alloc(void)
+static void test_hmac_alloc(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
-        QCryptoHmacTestData *data = &test_data[i];
-        QCryptoHmac *hmac = NULL;
-        uint8_t *result = NULL;
-        size_t resultlen = 0;
-        const char *exp_output = NULL;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hmac_supports(data->alg)) {
-            return;
-        }
-
-        exp_output = data->hex_digest;
-
-        hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
-                                strlen(KEY), &error_fatal);
-        g_assert(hmac != NULL);
-
-        ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
-                                 strlen(INPUT_TEXT), &result,
-                                 &resultlen, &error_fatal);
-        g_assert(ret == 0);
-
-        for (j = 0; j < resultlen; j++) {
-            g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
-            g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
-        }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    QCryptoHmacTestData *data = &test_data[alg];
+    QCryptoHmac *hmac = NULL;
+    uint8_t *result = NULL;
+    size_t resultlen = 0;
+    const char *exp_output = NULL;
+    int ret;
+    size_t j;
+
+    exp_output = data->hex_digest;
+
+    hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
+                            strlen(KEY), &error_fatal);
+    g_assert(hmac != NULL);
+
+    ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
+                             strlen(INPUT_TEXT), &result,
+                             &resultlen, &error_fatal);
+    g_assert(ret == 0);
+
+    for (j = 0; j < resultlen; j++) {
+        g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
+    }
 
-        qcrypto_hmac_free(hmac);
+    qcrypto_hmac_free(hmac);
 
-        g_free(result);
-    }
+    g_free(result);
 }
 
-static void test_hmac_prealloc(void)
+static void test_hmac_prealloc(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
-        QCryptoHmacTestData *data = &test_data[i];
-        QCryptoHmac *hmac = NULL;
-        uint8_t *result = NULL, *origresult = NULL;
-        size_t resultlen = 0;
-        const char *exp_output = NULL;
-        int ret;
-        size_t j;
-
-        if (!qcrypto_hmac_supports(data->alg)) {
-            return;
-        }
-
-        exp_output = data->hex_digest;
-
-        resultlen = strlen(exp_output) / 2;
-        origresult = result = g_new0(uint8_t, resultlen);
-
-        hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
-                                strlen(KEY), &error_fatal);
-        g_assert(hmac != NULL);
-
-        ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
-                                 strlen(INPUT_TEXT), &result,
-                                 &resultlen, &error_fatal);
-        g_assert(ret == 0);
-        /* Validate that our pre-allocated pointer was not replaced */
-        g_assert(result == origresult);
-
-        exp_output = data->hex_digest;
-        for (j = 0; j < resultlen; j++) {
-            g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
-            g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
-        }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    QCryptoHmacTestData *data = &test_data[alg];
+    QCryptoHmac *hmac = NULL;
+    uint8_t *result = NULL, *origresult = NULL;
+    size_t resultlen = 0;
+    const char *exp_output = NULL;
+    int ret;
+    size_t j;
+
+    exp_output = data->hex_digest;
+
+    resultlen = strlen(exp_output) / 2;
+    origresult = result = g_new0(uint8_t, resultlen);
+
+    hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
+                            strlen(KEY), &error_fatal);
+    g_assert(hmac != NULL);
+
+    ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
+                             strlen(INPUT_TEXT), &result,
+                             &resultlen, &error_fatal);
+    g_assert(ret == 0);
+    /* Validate that our pre-allocated pointer was not replaced */
+    g_assert(result == origresult);
+
+    exp_output = data->hex_digest;
+    for (j = 0; j < resultlen; j++) {
+        g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
+    }
 
-        qcrypto_hmac_free(hmac);
+    qcrypto_hmac_free(hmac);
 
-        g_free(result);
-    }
+    g_free(result);
 }
 
-static void test_hmac_iov(void)
+static void test_hmac_iov(const void *opaque)
 {
-    size_t i;
-
-    for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
-        QCryptoHmacTestData *data = &test_data[i];
-        QCryptoHmac *hmac = NULL;
-        uint8_t *result = NULL;
-        size_t resultlen = 0;
-        const char *exp_output = NULL;
-        int ret;
-        size_t j;
-        struct iovec iov[3] = {
-            { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) 
},
-            { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) 
},
-            { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) 
},
-        };
-
-        if (!qcrypto_hmac_supports(data->alg)) {
-            return;
-        }
-
-        exp_output = data->hex_digest;
-
-        hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
-                                strlen(KEY), &error_fatal);
-        g_assert(hmac != NULL);
-
-        ret = qcrypto_hmac_bytesv(hmac, iov, 3, &result,
-                                  &resultlen, &error_fatal);
-        g_assert(ret == 0);
-
-        for (j = 0; j < resultlen; j++) {
-            g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
-            g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
-        }
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    QCryptoHmacTestData *data = &test_data[alg];
+    QCryptoHmac *hmac = NULL;
+    uint8_t *result = NULL;
+    size_t resultlen = 0;
+    const char *exp_output = NULL;
+    int ret;
+    size_t j;
+    struct iovec iov[3] = {
+        { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
+        { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
+        { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
+    };
+
+    exp_output = data->hex_digest;
+
+    hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
+                            strlen(KEY), &error_fatal);
+    g_assert(hmac != NULL);
+
+    ret = qcrypto_hmac_bytesv(hmac, iov, 3, &result,
+                              &resultlen, &error_fatal);
+    g_assert(ret == 0);
+
+    for (j = 0; j < resultlen; j++) {
+        g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
+        g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
+    }
 
-        qcrypto_hmac_free(hmac);
+    qcrypto_hmac_free(hmac);
 
-        g_free(result);
-    }
+    g_free(result);
 }
 
-static void test_hmac_digest(void)
+static void test_hmac_digest(const void *opaque)
 {
-    size_t i;
+    QCryptoHashAlgo alg = GPOINTER_TO_INT(opaque);
+    QCryptoHmacTestData *data = &test_data[alg];
+    QCryptoHmac *hmac = NULL;
+    uint8_t *result = NULL;
+    const char *exp_output = NULL;
+    int ret;
 
-    for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
-        QCryptoHmacTestData *data = &test_data[i];
-        QCryptoHmac *hmac = NULL;
-        uint8_t *result = NULL;
-        const char *exp_output = NULL;
-        int ret;
+    exp_output = data->hex_digest;
 
-        if (!qcrypto_hmac_supports(data->alg)) {
-            return;
-        }
-
-        exp_output = data->hex_digest;
-
-        hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
-                                strlen(KEY), &error_fatal);
-        g_assert(hmac != NULL);
+    hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
+                            strlen(KEY), &error_fatal);
+    g_assert(hmac != NULL);
 
-        ret = qcrypto_hmac_digest(hmac, (const char *)INPUT_TEXT,
-                                  strlen(INPUT_TEXT), (char **)&result,
-                                  &error_fatal);
-        g_assert(ret == 0);
+    ret = qcrypto_hmac_digest(hmac, (const char *)INPUT_TEXT,
+                              strlen(INPUT_TEXT), (char **)&result,
+                              &error_fatal);
+    g_assert(ret == 0);
 
-        g_assert_cmpstr((const char *)result, ==, exp_output);
+    g_assert_cmpstr((const char *)result, ==, exp_output);
 
-        qcrypto_hmac_free(hmac);
+    qcrypto_hmac_free(hmac);
 
-        g_free(result);
-    }
+    g_free(result);
 }
 
 int main(int argc, char **argv)
 {
+    size_t i;
     g_test_init(&argc, &argv, NULL);
 
     g_assert(qcrypto_init(NULL) == 0);
 
-    g_test_add_func("/crypto/hmac/iov", test_hmac_iov);
-    g_test_add_func("/crypto/hmac/alloc", test_hmac_alloc);
-    g_test_add_func("/crypto/hmac/prealloc", test_hmac_prealloc);
-    g_test_add_func("/crypto/hmac/digest", test_hmac_digest);
+#define ADD_TEST(alg, prefix, meth) \
+    { \
+        g_autofree char *name = g_strdup_printf( \
+            "%s/%s", prefix, QCryptoHashAlgo_str(alg)); \
+        g_test_add_data_func(name, GINT_TO_POINTER(alg), meth); \
+    }
+
+    for (i = 0; i < QCRYPTO_HASH_ALGO__MAX ; i++) {
+        if (!qcrypto_hmac_supports(i)) {
+            continue;
+        }
+
+        ADD_TEST(i, "/crypto/hmac/iov", test_hmac_iov);
+        ADD_TEST(i, "/crypto/hmac/alloc", test_hmac_alloc);
+        ADD_TEST(i, "/crypto/hmac/prealloc", test_hmac_prealloc);
+        ADD_TEST(i, "/crypto/hmac/digest", test_hmac_digest);
+    }
 
     return g_test_run();
 }
-- 
2.56.0


Reply via email to