Sync and Async crypto tests are almost identical and have a lot
duplicated code.
Move unique configuration into testsuite's init function and reuse Async
test code for both Sync and Async.

Signed-off-by: Taras Kondratiuk <taras.kondrat...@linaro.org>
---
 test/validation/Makefile.am                        |   3 +-
 ...ypto_test_async_inp.c => odp_crypto_test_inp.c} |  49 +++-
 ...ypto_test_async_inp.h => odp_crypto_test_inp.h} |   8 +-
 test/validation/crypto/odp_crypto_test_sync_inp.c  | 271 ---------------------
 test/validation/crypto/odp_crypto_test_sync_inp.h  |  17 --
 test/validation/odp_crypto.c                       |   9 +-
 6 files changed, 50 insertions(+), 307 deletions(-)
 rename test/validation/crypto/{odp_crypto_test_async_inp.c => 
odp_crypto_test_inp.c} (90%)
 rename test/validation/crypto/{odp_crypto_test_async_inp.h => 
odp_crypto_test_inp.h} (62%)
 delete mode 100644 test/validation/crypto/odp_crypto_test_sync_inp.c
 delete mode 100644 test/validation/crypto/odp_crypto_test_sync_inp.h

diff --git a/test/validation/Makefile.am b/test/validation/Makefile.am
index 0639205..a9e1a43 100644
--- a/test/validation/Makefile.am
+++ b/test/validation/Makefile.am
@@ -41,8 +41,7 @@ dist_odp_classification_SOURCES = 
classification/odp_classification_tests.c \
                                classification/odp_classification_basic.c \
                                odp_classification.c $(ODP_CU_COMMON)
 odp_crypto_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/crypto
-dist_odp_crypto_SOURCES = crypto/odp_crypto_test_async_inp.c \
-                         crypto/odp_crypto_test_sync_inp.c \
+dist_odp_crypto_SOURCES = crypto/odp_crypto_test_inp.c \
                          odp_crypto.c $(ODP_CU_COMMON)
 dist_odp_init_SOURCES  = odp_init.c
 dist_odp_init_abort_SOURCES = odp_init_abort.c
diff --git a/test/validation/crypto/odp_crypto_test_async_inp.c 
b/test/validation/crypto/odp_crypto_test_inp.c
similarity index 90%
rename from test/validation/crypto/odp_crypto_test_async_inp.c
rename to test/validation/crypto/odp_crypto_test_inp.c
index 494a68e..38e84e0 100644
--- a/test/validation/crypto/odp_crypto_test_async_inp.c
+++ b/test/validation/crypto/odp_crypto_test_inp.c
@@ -6,8 +6,16 @@
 
 #include <odp.h>
 #include <CUnit/Basic.h>
-#include <CUnit/TestDB.h>
 #include "test_vectors.h"
+#include "odp_crypto_test_inp.h"
+
+struct suite_context_s {
+       enum odp_crypto_op_mode pref_mode;
+       odp_pool_t pool;
+       odp_queue_t queue;
+};
+
+static struct suite_context_s suite_context;
 
 /* Basic algorithm run function for async inplace mode.
  * Creates a session from input parameters and runs one operation
@@ -38,21 +46,16 @@ static void alg_test(enum odp_crypto_op op,
        odp_crypto_compl_t compl_event;
        odp_crypto_op_result_t result;
 
-       odp_queue_t compl_queue = odp_queue_lookup("crypto-out");
-       CU_ASSERT(compl_queue != ODP_QUEUE_INVALID);
-       odp_pool_t pool = odp_pool_lookup("packet_pool");
-       CU_ASSERT(pool != ODP_POOL_INVALID);
-
        /* Create a crypto session */
        odp_crypto_session_params_t ses_params;
        memset(&ses_params, 0, sizeof(ses_params));
        ses_params.op = op;
        ses_params.auth_cipher_text = false;
-       ses_params.pref_mode = ODP_CRYPTO_ASYNC;
+       ses_params.pref_mode = suite_context.pref_mode;
        ses_params.cipher_alg = cipher_alg;
        ses_params.auth_alg = auth_alg;
-       ses_params.compl_queue = compl_queue;
-       ses_params.output_pool = pool;
+       ses_params.compl_queue = suite_context.queue;
+       ses_params.output_pool = suite_context.pool;
        ses_params.cipher_key = cipher_key;
        ses_params.iv = ses_iv;
        ses_params.auth_key = auth_key;
@@ -62,7 +65,7 @@ static void alg_test(enum odp_crypto_op op,
        CU_ASSERT(status == ODP_CRYPTO_SES_CREATE_ERR_NONE);
 
        /* Prepare input data */
-       odp_packet_t pkt = odp_packet_alloc(pool, input_vec_len);
+       odp_packet_t pkt = odp_packet_alloc(suite_context.pool, input_vec_len);
        CU_ASSERT(pkt != ODP_PACKET_INVALID);
        uint8_t *data_addr = odp_packet_data(pkt);
        memcpy(data_addr, input_vec, input_vec_len);
@@ -291,7 +294,31 @@ static void alg_hmac_md5(void)
        }
 }
 
-CU_TestInfo test_array_async[] = {
+int suite_sync_inp_init(void)
+{
+       suite_context.pool = odp_pool_lookup("packet_pool");
+       if (suite_context.pool == ODP_POOL_INVALID)
+               return -1;
+
+       suite_context.queue = ODP_QUEUE_INVALID;
+       suite_context.pref_mode = ODP_CRYPTO_SYNC;
+       return 0;
+}
+
+int suite_async_inp_init(void)
+{
+       suite_context.pool = odp_pool_lookup("packet_pool");
+       if (suite_context.pool == ODP_POOL_INVALID)
+               return -1;
+       suite_context.queue = odp_queue_lookup("crypto-out");
+       if (suite_context.queue == ODP_QUEUE_INVALID)
+               return -1;
+
+       suite_context.pref_mode = ODP_CRYPTO_ASYNC;
+       return 0;
+}
+
+CU_TestInfo test_array_inp[] = {
        {ASYNC_INP_ENC_ALG_3DES_CBC, enc_alg_3des_cbc },
        {ASYNC_INP_DEC_ALG_3DES_CBC, dec_alg_3des_cbc },
        {ASYNC_INP_ENC_ALG_3DES_CBC_OVR_IV, enc_alg_3des_cbc_ovr_iv },
diff --git a/test/validation/crypto/odp_crypto_test_async_inp.h 
b/test/validation/crypto/odp_crypto_test_inp.h
similarity index 62%
rename from test/validation/crypto/odp_crypto_test_async_inp.h
rename to test/validation/crypto/odp_crypto_test_inp.h
index ffad83c..a3a5d8d 100644
--- a/test/validation/crypto/odp_crypto_test_async_inp.h
+++ b/test/validation/crypto/odp_crypto_test_inp.h
@@ -8,10 +8,14 @@
 
 #include "CUnit/TestDB.h"
 
-/* Suite name */
+/* Suite names */
 #define ODP_CRYPTO_ASYNC_INP   "odp_crypto_async_inp"
+#define ODP_CRYPTO_SYNC_INP    "odp_crypto_sync_inp"
 
 /* Suite test array */
-CU_TestInfo test_array_async[1];
+extern CU_TestInfo test_array_inp[];
+
+int suite_sync_inp_init(void);
+int suite_async_inp_init(void);
 
 #endif
diff --git a/test/validation/crypto/odp_crypto_test_sync_inp.c 
b/test/validation/crypto/odp_crypto_test_sync_inp.c
deleted file mode 100644
index 477ae54..0000000
--- a/test/validation/crypto/odp_crypto_test_sync_inp.c
+++ /dev/null
@@ -1,271 +0,0 @@
-#include <odp.h>
-#include <CUnit/Basic.h>
-#include <CUnit/TestDB.h>
-#include "test_vectors.h"
-
-/* Basic algorithm run function for sync inplace.
- * Creates a session from input parameters and runs one operation
- * on input_vec. Checks the output of the crypto operation against
- * output_vec.
- */
-static void alg_test(enum odp_crypto_op op,
-                    enum odp_cipher_alg cipher_alg,
-                    odp_crypto_iv_t ses_iv,
-                    uint8_t *op_iv_ptr,
-                    odp_crypto_key_t cipher_key,
-                    enum odp_auth_alg auth_alg,
-                    odp_crypto_key_t auth_key,
-                    uint8_t *input_vec,
-                    unsigned int input_vec_len,
-                    uint8_t *output_vec,
-                    unsigned int output_vec_len)
-{
-       odp_crypto_session_t session;
-       int rc;
-       enum odp_crypto_ses_create_err status;
-       odp_bool_t posted;
-       odp_crypto_op_result_t result;
-
-       odp_queue_t compl_queue = odp_queue_lookup("crypto-out");
-       CU_ASSERT(compl_queue != ODP_QUEUE_INVALID);
-       odp_pool_t pool = odp_pool_lookup("packet_pool");
-       CU_ASSERT(pool != ODP_POOL_INVALID);
-
-       /* Create a crypto session */
-       odp_crypto_session_params_t ses_params;
-       memset(&ses_params, 0, sizeof(ses_params));
-       ses_params.op = op;
-       ses_params.auth_cipher_text = false;
-       ses_params.pref_mode = ODP_CRYPTO_SYNC;
-       ses_params.cipher_alg = cipher_alg;
-       ses_params.auth_alg = auth_alg;
-       ses_params.compl_queue = ODP_QUEUE_INVALID;
-       ses_params.output_pool = pool;
-       ses_params.cipher_key = cipher_key;
-       ses_params.iv = ses_iv;
-       ses_params.auth_key = auth_key;
-
-       /* TEST : odp_crypto_session_create */
-       rc = odp_crypto_session_create(&ses_params, &session, &status);
-       CU_ASSERT(!rc);
-       CU_ASSERT(status == ODP_CRYPTO_SES_CREATE_ERR_NONE);
-
-       /* Prepare input data */
-       odp_packet_t pkt = odp_packet_alloc(pool, input_vec_len);
-       CU_ASSERT(pkt != ODP_PACKET_INVALID);
-       uint8_t *data_addr = odp_packet_data(pkt);
-       memcpy(data_addr, input_vec, input_vec_len);
-       const int data_off = 0;
-
-       /* Prepare input/output params */
-       odp_crypto_op_params_t op_params;
-       memset(&op_params, 0, sizeof(op_params));
-       op_params.session = session;
-       op_params.pkt = pkt;
-       op_params.out_pkt = pkt;
-       op_params.ctx = (void *)0xdeadbeef;
-       if (cipher_alg != ODP_CIPHER_ALG_NULL &&
-           auth_alg == ODP_AUTH_ALG_NULL) {
-               op_params.cipher_range.offset = data_off;
-               op_params.cipher_range.length = input_vec_len;
-               if (op_iv_ptr)
-                       op_params.override_iv_ptr = op_iv_ptr;
-       } else if (cipher_alg == ODP_CIPHER_ALG_NULL &&
-                auth_alg != ODP_AUTH_ALG_NULL) {
-               op_params.auth_range.offset = data_off;
-               op_params.auth_range.length = input_vec_len;
-               op_params.hash_result_offset = data_off;
-       } else {
-               CU_FAIL("%s : not implemented for combined alg mode\n");
-       }
-
-       /* TEST : odp_crypto_operation */
-       rc = odp_crypto_operation(&op_params, &posted, &result);
-       CU_ASSERT(!rc);
-       /* indication that the operation completed */
-       CU_ASSERT(!posted);
-
-       /* TEST: results were ok */
-       CU_ASSERT(result.ok);
-       CU_ASSERT(result.auth_status.alg_err == ODP_CRYPTO_ALG_ERR_NONE);
-       CU_ASSERT(result.auth_status.hw_err == ODP_CRYPTO_HW_ERR_NONE);
-       CU_ASSERT(result.cipher_status.alg_err == ODP_CRYPTO_ALG_ERR_NONE);
-       CU_ASSERT(result.cipher_status.hw_err == ODP_CRYPTO_HW_ERR_NONE);
-
-       /* TEST : operation output was correct */
-       CU_ASSERT(!memcmp(data_addr, output_vec, output_vec_len));
-
-       CU_ASSERT(result.ctx == (void *)0xdeadbeef);
-
-       rc = odp_crypto_session_destroy(session);
-       CU_ASSERT(!rc);
-
-       odp_packet_free(pkt);
-}
-
-#define SYNC_INP_ENC_ALG_3DES_CBC      "ENC_ALG_3DES_CBC"
-static void enc_alg_3des_cbc(void)
-{
-       odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 },
-                        auth_key   = { .data = NULL, .length = 0 };
-       odp_crypto_iv_t iv;
-       unsigned int test_vec_num = (sizeof(tdes_cbc_reference_length)/
-                                    sizeof(tdes_cbc_reference_length[0]));
-       unsigned int i;
-       for (i = 0; i < test_vec_num; i++) {
-               cipher_key.data = tdes_cbc_reference_key[i];
-               cipher_key.length = sizeof(tdes_cbc_reference_key[i]);
-               iv.data = tdes_cbc_reference_iv[i];
-               iv.length = sizeof(tdes_cbc_reference_iv[i]);
-
-               alg_test(ODP_CRYPTO_OP_ENCODE,
-                        ODP_CIPHER_ALG_3DES_CBC,
-                        iv,
-                        NULL,
-                        cipher_key,
-                        ODP_AUTH_ALG_NULL,
-                        auth_key,
-                        tdes_cbc_reference_plaintext[i],
-                        tdes_cbc_reference_length[i],
-                        tdes_cbc_reference_ciphertext[i],
-                        tdes_cbc_reference_length[i]);
-       }
-}
-
-/* This test verifies the correctness of decode (ciphertext -> plaintext)
- * operation for 3DES_CBC algorithm. IV for the operation is the session IV
- * */
-#define SYNC_INP_DEC_ALG_3DES_CBC      "DEC_ALG_3DES_CBC"
-static void dec_alg_3des_cbc(void)
-{
-       odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 },
-                        auth_key   = { .data = NULL, .length = 0 };
-       odp_crypto_iv_t iv = { .data = NULL, .length = 0 };
-       unsigned int test_vec_num = (sizeof(tdes_cbc_reference_length)/
-                                    sizeof(tdes_cbc_reference_length[0]));
-
-       unsigned int i;
-       for (i = 0; i < test_vec_num; i++) {
-               cipher_key.data = tdes_cbc_reference_key[i];
-               cipher_key.length = sizeof(tdes_cbc_reference_key[i]);
-               iv.data = tdes_cbc_reference_iv[i];
-               iv.length = sizeof(tdes_cbc_reference_iv[i]);
-
-               alg_test(ODP_CRYPTO_OP_DECODE,
-                        ODP_CIPHER_ALG_3DES_CBC,
-                        iv,
-                        NULL,
-                        cipher_key,
-                        ODP_AUTH_ALG_NULL,
-                        auth_key,
-                        tdes_cbc_reference_ciphertext[i],
-                        tdes_cbc_reference_length[i],
-                        tdes_cbc_reference_plaintext[i],
-                        tdes_cbc_reference_length[i]);
-       }
-}
-
-/* This test verifies the correctness of HMAC_MD5 digest operation.
- * The output check length is truncated to 12 bytes (96 bits) as
- * returned by the crypto operation API call.
- * Note that hash digest is a one-way operation.
- * */
-#define SYNC_INP_ALG_HMAC_MD5  "ALG_HMAC_MD5"
-static void alg_hmac_md5(void)
-{
-       odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 },
-                        auth_key   = { .data = NULL, .length = 0 };
-       odp_crypto_iv_t iv = { .data = NULL, .length = 0 };
-
-       unsigned int test_vec_num = (sizeof(hmac_md5_reference_length)/
-                                    sizeof(hmac_md5_reference_length[0]));
-
-       unsigned int i;
-       for (i = 0; i < test_vec_num; i++) {
-               auth_key.data = hmac_md5_reference_key[i];
-               auth_key.length = sizeof(hmac_md5_reference_key[i]);
-
-               alg_test(ODP_CRYPTO_OP_ENCODE,
-                        ODP_CIPHER_ALG_NULL,
-                        iv,
-                        iv.data,
-                        cipher_key,
-                        ODP_AUTH_ALG_MD5_96,
-                        auth_key,
-                        hmac_md5_reference_plaintext[i],
-                        hmac_md5_reference_length[i],
-                        hmac_md5_reference_digest[i],
-                        HMAC_MD5_96_CHECK_LEN);
-       }
-}
-
-/* This test verifies the correctness of encode (plaintext -> ciphertext)
- * operation for 3DES_CBC algorithm. IV for the operation is the operation IV.
- * */
-#define SYNC_INP_ENC_ALG_3DES_CBC_OVR_IV       "ENC_ALG_3DES_CBC_OVR_IV"
-static void enc_alg_3des_cbc_ovr_iv(void)
-{
-       odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 },
-                        auth_key   = { .data = NULL, .length = 0 };
-       odp_crypto_iv_t iv = { .data = NULL, .length = TDES_CBC_IV_LEN };
-       unsigned int test_vec_num = (sizeof(tdes_cbc_reference_length)/
-                                    sizeof(tdes_cbc_reference_length[0]));
-
-       unsigned int i;
-       for (i = 0; i < test_vec_num; i++) {
-               cipher_key.data = tdes_cbc_reference_key[i];
-               cipher_key.length = sizeof(tdes_cbc_reference_key[i]);
-
-               alg_test(ODP_CRYPTO_OP_ENCODE,
-                        ODP_CIPHER_ALG_3DES_CBC,
-                        iv,
-                        tdes_cbc_reference_iv[i],
-                        cipher_key,
-                        ODP_AUTH_ALG_NULL,
-                        auth_key,
-                        tdes_cbc_reference_plaintext[i],
-                        tdes_cbc_reference_length[i],
-                        tdes_cbc_reference_ciphertext[i],
-                        tdes_cbc_reference_length[i]);
-       }
-}
-
-/* This test verifies the correctness of decode (ciphertext -> plaintext)
- * operation for 3DES_CBC algorithm. IV for the operation is the session IV.
- * */
-#define SYNC_INP_DEC_ALG_3DES_CBC_OVR_IV       "DEC_ALG_3DES_CBC_OVR_IV"
-static void dec_alg_3des_cbc_ovr_iv(void)
-{
-       odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 },
-                        auth_key   = { .data = NULL, .length = 0 };
-       odp_crypto_iv_t iv = { .data = NULL, .length = TDES_CBC_IV_LEN };
-       unsigned int test_vec_num = (sizeof(tdes_cbc_reference_length)/
-                                    sizeof(tdes_cbc_reference_length[0]));
-
-       unsigned int i;
-       for (i = 0; i < test_vec_num; i++) {
-               cipher_key.data = tdes_cbc_reference_key[i];
-               cipher_key.length = sizeof(tdes_cbc_reference_key[i]);
-
-               alg_test(ODP_CRYPTO_OP_DECODE,
-                        ODP_CIPHER_ALG_3DES_CBC,
-                        iv,
-                        tdes_cbc_reference_iv[i],
-                        cipher_key,
-                        ODP_AUTH_ALG_NULL,
-                        auth_key,
-                        tdes_cbc_reference_ciphertext[i],
-                        tdes_cbc_reference_length[i],
-                        tdes_cbc_reference_plaintext[i],
-                        tdes_cbc_reference_length[i]);
-       }
-}
-
-CU_TestInfo test_array_sync[] = {
-       {SYNC_INP_ENC_ALG_3DES_CBC, enc_alg_3des_cbc },
-       {SYNC_INP_DEC_ALG_3DES_CBC, dec_alg_3des_cbc },
-       {SYNC_INP_ENC_ALG_3DES_CBC_OVR_IV, enc_alg_3des_cbc_ovr_iv },
-       {SYNC_INP_DEC_ALG_3DES_CBC_OVR_IV, dec_alg_3des_cbc_ovr_iv },
-       {SYNC_INP_ALG_HMAC_MD5, alg_hmac_md5 },
-       CU_TEST_INFO_NULL,
-};
diff --git a/test/validation/crypto/odp_crypto_test_sync_inp.h 
b/test/validation/crypto/odp_crypto_test_sync_inp.h
deleted file mode 100644
index 6a8511d..0000000
--- a/test/validation/crypto/odp_crypto_test_sync_inp.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* Copyright (c) 2014, Linaro Limited
- * All rights reserved.
- *
- * SPDX-License-Identifier:    BSD-3-Clause
- */
-#ifndef ODP_CRYPTO_TEST_SYNC_INP_
-#define ODP_CRYPTO_TEST_SYNC_INP_
-
-#include "CUnit/TestDB.h"
-
-/* Suite name */
-#define ODP_CRYPTO_SYNC_INP    "odp_crypto_sync_inp"
-
-/* Suite test array */
-CU_TestInfo test_array_sync[1];
-
-#endif
diff --git a/test/validation/odp_crypto.c b/test/validation/odp_crypto.c
index 4126ac1..f364b1a 100644
--- a/test/validation/odp_crypto.c
+++ b/test/validation/odp_crypto.c
@@ -6,8 +6,7 @@
 
 #include <odp.h>
 #include "odp_cunit_common.h"
-#include "odp_crypto_test_async_inp.h"
-#include "odp_crypto_test_sync_inp.h"
+#include "odp_crypto_test_inp.h"
 
 #define SHM_PKT_POOL_SIZE      (512*2048*2)
 #define SHM_PKT_POOL_BUF_SIZE  (1024 * 32)
@@ -16,8 +15,10 @@
 #define SHM_COMPL_POOL_BUF_SIZE        128
 
 CU_SuiteInfo odp_testsuites[] = {
-       {ODP_CRYPTO_SYNC_INP, NULL, NULL, NULL, NULL, test_array_sync },
-       {ODP_CRYPTO_ASYNC_INP, NULL, NULL, NULL, NULL, test_array_async },
+       {ODP_CRYPTO_SYNC_INP, suite_sync_inp_init, NULL, NULL, NULL,
+                       test_array_inp},
+       {ODP_CRYPTO_ASYNC_INP, suite_async_inp_init, NULL, NULL, NULL,
+                       test_array_inp},
        CU_SUITE_INFO_NULL,
 };
 
-- 
1.9.1


_______________________________________________
lng-odp mailing list
lng-odp@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to