DPDK patches and discussions
 help / color / mirror / Atom feed
From: Ibtisam Tariq <ibtisam.tariq@emumba.com>
To: marko.kovacevic@intel.com, konstantin.ananyev@intel.com,
	reshma.pattan@intel.com, john.mcnamara@intel.com,
	cristian.dumitrescu@intel.com, jasvinder.singh@intel.com,
	chenbo.xia@intel.com, maxime.coquelin@redhat.com,
	xiaoyun.li@intel.com
Cc: dev@dpdk.org, Ibtisam Tariq <ibtisam.tariq@emumba.com>
Subject: [dpdk-dev] [PATCH 1/8] examples/fips_validation: enhance getopt_long usage
Date: Thu, 29 Oct 2020 12:53:32 +0000
Message-ID: <20201029125339.30916-1-ibtisam.tariq@emumba.com> (raw)

Instead of using getopt_long return value, strcmp was used to
compare the input parameters with the struct option array. This
patch get rid of all those strcmp by directly binding each longopt
with an int enum.

Bugzilla ID: 238
Fixes: 3d0fad56b74 ("examples/fips_validation: add crypto FIPS application"}
Cc: marko.kovacevic@intel.com

Reported-by: David Marchand <david.marchand@redhat.com>
Signed-off-by: Ibtisam Tariq <ibtisam.tariq@emumba.com>
---
 examples/fips_validation/main.c | 241 +++++++++++++++++++-------------
 1 file changed, 143 insertions(+), 98 deletions(-)

diff --git a/examples/fips_validation/main.c b/examples/fips_validation/main.c
index 07532c956..5fb76b421 100644
--- a/examples/fips_validation/main.c
+++ b/examples/fips_validation/main.c
@@ -15,17 +15,31 @@
 #include "fips_validation.h"
 #include "fips_dev_self_test.h"
 
+enum{
 #define REQ_FILE_PATH_KEYWORD	"req-file"
+	/* first long only option value must be >= 256, so that we won't
+	 * conflict with short options
+	 */
+	REQ_FILE_PATH_KEYWORD_NUM = 256,
 #define RSP_FILE_PATH_KEYWORD	"rsp-file"
+	RSP_FILE_PATH_KEYWORD_NUM,
 #define MBUF_DATAROOM_KEYWORD	"mbuf-dataroom"
+	MBUF_DATAROOM_KEYWORD_NUM,
 #define FOLDER_KEYWORD		"path-is-folder"
+	FOLDER_KEYWORD_NUM,
 #define CRYPTODEV_KEYWORD	"cryptodev"
+	CRYPTODEV_KEYWORD_NUM,
 #define CRYPTODEV_ID_KEYWORD	"cryptodev-id"
+	CRYPTODEV_ID_KEYWORD_NUM,
 #define CRYPTODEV_ST_KEYWORD	"self-test"
+	CRYPTODEV_ST_KEYWORD_NUM,
 #define CRYPTODEV_BK_ID_KEYWORD	"broken-test-id"
+	CRYPTODEV_BK_ID_KEYWORD_NUM,
 #define CRYPTODEV_BK_DIR_KEY	"broken-test-dir"
+	CRYPTODEV_BK_DIR_KEY_NUM,
 #define CRYPTODEV_ENC_KEYWORD	"enc"
 #define CRYPTODEV_DEC_KEYWORD	"dec"
+};
 
 struct fips_test_vector vec;
 struct fips_test_interim_info info;
@@ -226,15 +240,24 @@ cryptodev_fips_validate_parse_args(int argc, char **argv)
 	char **argvopt;
 	int option_index;
 	struct option lgopts[] = {
-			{REQ_FILE_PATH_KEYWORD, required_argument, 0, 0},
-			{RSP_FILE_PATH_KEYWORD, required_argument, 0, 0},
-			{FOLDER_KEYWORD, no_argument, 0, 0},
-			{MBUF_DATAROOM_KEYWORD, required_argument, 0, 0},
-			{CRYPTODEV_KEYWORD, required_argument, 0, 0},
-			{CRYPTODEV_ID_KEYWORD, required_argument, 0, 0},
-			{CRYPTODEV_ST_KEYWORD, no_argument, 0, 0},
-			{CRYPTODEV_BK_ID_KEYWORD, required_argument, 0, 0},
-			{CRYPTODEV_BK_DIR_KEY, required_argument, 0, 0},
+			{REQ_FILE_PATH_KEYWORD, required_argument,
+					NULL, REQ_FILE_PATH_KEYWORD_NUM},
+			{RSP_FILE_PATH_KEYWORD, required_argument,
+					NULL, RSP_FILE_PATH_KEYWORD_NUM},
+			{FOLDER_KEYWORD, no_argument,
+					NULL, FOLDER_KEYWORD_NUM},
+			{MBUF_DATAROOM_KEYWORD, required_argument,
+					NULL, MBUF_DATAROOM_KEYWORD_NUM},
+			{CRYPTODEV_KEYWORD, required_argument,
+					NULL, CRYPTODEV_KEYWORD_NUM},
+			{CRYPTODEV_ID_KEYWORD, required_argument,
+					NULL, CRYPTODEV_ID_KEYWORD_NUM},
+			{CRYPTODEV_ST_KEYWORD, no_argument,
+					NULL, CRYPTODEV_ST_KEYWORD_NUM},
+			{CRYPTODEV_BK_ID_KEYWORD, required_argument,
+					NULL, CRYPTODEV_BK_ID_KEYWORD_NUM},
+			{CRYPTODEV_BK_DIR_KEY, required_argument,
+					NULL, CRYPTODEV_BK_DIR_KEY_NUM},
 			{NULL, 0, 0, 0}
 	};
 
@@ -251,105 +274,127 @@ cryptodev_fips_validate_parse_args(int argc, char **argv)
 	while ((opt = getopt_long(argc, argvopt, "s:",
 				  lgopts, &option_index)) != EOF) {
 
+		if (opt == '?') {
+			cryptodev_fips_validate_usage(prgname);
+			return -1;
+		}
+
 		switch (opt) {
-		case 0:
-			if (strcmp(lgopts[option_index].name,
-					REQ_FILE_PATH_KEYWORD) == 0)
-				env.req_path = optarg;
-			else if (strcmp(lgopts[option_index].name,
-					RSP_FILE_PATH_KEYWORD) == 0)
-				env.rsp_path = optarg;
-			else if (strcmp(lgopts[option_index].name,
-					FOLDER_KEYWORD) == 0)
-				env.is_path_folder = 1;
-			else if (strcmp(lgopts[option_index].name,
-					CRYPTODEV_KEYWORD) == 0) {
-				ret = parse_cryptodev_arg(optarg);
-				if (ret < 0) {
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
-			} else if (strcmp(lgopts[option_index].name,
-					CRYPTODEV_ID_KEYWORD) == 0) {
-				ret = parse_cryptodev_id_arg(optarg);
-				if (ret < 0) {
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
-			} else if (strcmp(lgopts[option_index].name,
-					CRYPTODEV_ST_KEYWORD) == 0) {
-				env.self_test = 1;
-			} else if (strcmp(lgopts[option_index].name,
-					CRYPTODEV_BK_ID_KEYWORD) == 0) {
-				if (!env.broken_test_config) {
-					env.broken_test_config = rte_malloc(
-						NULL,
-						sizeof(*env.broken_test_config),
-						0);
-					if (!env.broken_test_config)
-						return -ENOMEM;
-
-					env.broken_test_config->expect_fail_dir =
-						self_test_dir_enc_auth_gen;
-				}
+		case REQ_FILE_PATH_KEYWORD_NUM:
+		{
+			env.req_path = optarg;
+			break;
+		}
+		case RSP_FILE_PATH_KEYWORD_NUM:
+		{
+			env.rsp_path = optarg;
+			break;
+		}
+		case FOLDER_KEYWORD_NUM:
+		{
+			env.is_path_folder = 1;
+			break;
+		}
+		case CRYPTODEV_KEYWORD_NUM:
+		{
+			ret = parse_cryptodev_arg(optarg);
+			if (ret < 0) {
+				cryptodev_fips_validate_usage(prgname);
+				return -EINVAL;
+			}
 
-				if (parser_read_uint32(
-					&env.broken_test_config->expect_fail_test_idx,
-						optarg) < 0) {
-					rte_free(env.broken_test_config);
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
-			} else if (strcmp(lgopts[option_index].name,
-					CRYPTODEV_BK_DIR_KEY) == 0) {
-				if (!env.broken_test_config) {
-					env.broken_test_config = rte_malloc(
-						NULL,
-						sizeof(*env.broken_test_config),
-						0);
-					if (!env.broken_test_config)
-						return -ENOMEM;
-
-					env.broken_test_config->
-						expect_fail_test_idx = 0;
-				}
+			break;
+		}
+		case CRYPTODEV_ID_KEYWORD_NUM:
+		{
+			ret = parse_cryptodev_id_arg(optarg);
+			if (ret < 0) {
+				cryptodev_fips_validate_usage(prgname);
+				return -EINVAL;
+			}
+			break;
+		}
+		case CRYPTODEV_ST_KEYWORD_NUM:
+		{
+			env.self_test = 1;
+			break;
+		}
+		case CRYPTODEV_BK_ID_KEYWORD_NUM:
+		{
+			if (!env.broken_test_config) {
+				env.broken_test_config = rte_malloc(
+					NULL,
+					sizeof(*env.broken_test_config),
+					0);
+				if (!env.broken_test_config)
+					return -ENOMEM;
+
+				env.broken_test_config->expect_fail_dir =
+					self_test_dir_enc_auth_gen;
+			}
 
-				if (strcmp(optarg, CRYPTODEV_ENC_KEYWORD) == 0)
-					env.broken_test_config->expect_fail_dir =
-						self_test_dir_enc_auth_gen;
-				else if (strcmp(optarg, CRYPTODEV_DEC_KEYWORD)
-						== 0)
-					env.broken_test_config->expect_fail_dir =
-						self_test_dir_dec_auth_verify;
-				else {
-					rte_free(env.broken_test_config);
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
-			} else if (strcmp(lgopts[option_index].name,
-					MBUF_DATAROOM_KEYWORD) == 0) {
-				uint32_t data_room_size;
-
-				if (parser_read_uint32(&data_room_size,
-						optarg) < 0) {
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
+			if (parser_read_uint32(
+				&env.broken_test_config->expect_fail_test_idx,
+					optarg) < 0) {
+				rte_free(env.broken_test_config);
+				cryptodev_fips_validate_usage(prgname);
+				return -EINVAL;
+			}
+			break;
+		}
+		case CRYPTODEV_BK_DIR_KEY_NUM:
+		{
+			if (!env.broken_test_config) {
+				env.broken_test_config = rte_malloc(
+					NULL,
+					sizeof(*env.broken_test_config),
+					0);
+				if (!env.broken_test_config)
+					return -ENOMEM;
+
+				env.broken_test_config->
+					expect_fail_test_idx = 0;
+			}
 
-				if (data_room_size == 0 ||
-						data_room_size > UINT16_MAX) {
-					cryptodev_fips_validate_usage(prgname);
-					return -EINVAL;
-				}
+			if (strcmp(optarg, CRYPTODEV_ENC_KEYWORD) == 0)
+				env.broken_test_config->expect_fail_dir =
+					self_test_dir_enc_auth_gen;
+			else if (strcmp(optarg, CRYPTODEV_DEC_KEYWORD)
+					== 0)
+				env.broken_test_config->expect_fail_dir =
+					self_test_dir_dec_auth_verify;
+			else {
+				rte_free(env.broken_test_config);
+				cryptodev_fips_validate_usage(prgname);
+				return -EINVAL;
+			}
+			break;
+		}
+		case MBUF_DATAROOM_KEYWORD_NUM:
+		{
+			uint32_t data_room_size;
 
-				env.mbuf_data_room = data_room_size;
-			} else {
+			if (parser_read_uint32(&data_room_size,
+					optarg) < 0) {
 				cryptodev_fips_validate_usage(prgname);
 				return -EINVAL;
 			}
+
+			if (data_room_size == 0 ||
+					data_room_size > UINT16_MAX) {
+				cryptodev_fips_validate_usage(prgname);
+				return -EINVAL;
+			}
+
+			env.mbuf_data_room = data_room_size;
+
 			break;
+		}
 		default:
-			return -1;
+		{
+			cryptodev_fips_validate_usage(prgname);
+			return -EINVAL;
+		}
 		}
 	}
 
-- 
2.17.1


             reply	other threads:[~2020-10-29 12:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-29 12:53 Ibtisam Tariq [this message]
2020-10-29 12:53 ` [dpdk-dev] [PATCH 2/8] examples/l3fwd-acl: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 3/8] examples/packet_ordering: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 4/8] examples/performance-thread/l3fwd-thread: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 5/8] examples/qos_sched: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 6/8] examples/vhost: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 7/8] examples/vhost_crypto: " Ibtisam Tariq
2020-10-29 12:53 ` [dpdk-dev] [PATCH 8/8] examples/tep_termination: " Ibtisam Tariq
2020-10-29 13:16   ` David Marchand
2020-11-02  8:18     ` Ibtisam Tariq
2020-10-29 22:07 ` [dpdk-dev] [PATCH 1/8] examples/fips_validation: " David Marchand
2020-11-02  8:32   ` Ibtisam Tariq
2020-11-04 10:00     ` Ibtisam Tariq
2020-11-05  8:59       ` David Marchand
2020-11-10  6:10         ` Ibtisam Tariq
2020-11-10  8:23           ` David Marchand
2020-11-10  9:03             ` Ibtisam Tariq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201029125339.30916-1-ibtisam.tariq@emumba.com \
    --to=ibtisam.tariq@emumba.com \
    --cc=chenbo.xia@intel.com \
    --cc=cristian.dumitrescu@intel.com \
    --cc=dev@dpdk.org \
    --cc=jasvinder.singh@intel.com \
    --cc=john.mcnamara@intel.com \
    --cc=konstantin.ananyev@intel.com \
    --cc=marko.kovacevic@intel.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=reshma.pattan@intel.com \
    --cc=xiaoyun.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

DPDK patches and discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.dpdk.org/dev/0 dev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dev dev/ https://inbox.dpdk.org/dev \
		dev@dpdk.org
	public-inbox-index dev

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.dev


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git