From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6299843BCF; Thu, 7 Mar 2024 14:09:44 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 96D7B42ED3; Thu, 7 Mar 2024 14:09:30 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 23D7C427E6 for ; Thu, 7 Mar 2024 14:09:25 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Tr8hn0np7zwPHm; Thu, 7 Mar 2024 21:07:05 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id CAD42144544; Thu, 7 Mar 2024 21:09:23 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 7 Mar 2024 21:09:23 +0800 From: Chengwen Feng To: , , Subject: [PATCH v2 5/5] test/argparse: refine testcases Date: Thu, 7 Mar 2024 13:07:42 +0000 Message-ID: <20240307130742.5578-6-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240307130742.5578-1-fengchengwen@huawei.com> References: <20240220131502.47510-1-fengchengwen@huawei.com> <20240307130742.5578-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Refine testcases, including: 1. add testcase comment. 2. argv[0] should set obj->prog_name. 3. set val_set as NULL in test_argparse_invalid_arg_flags, let it test to the specified code logic. 4. enable index verification in opt_callback_parse_int_of_no_val. Fixes: 6c5c6571601c ("argparse: verify argument config") Fixes: 31ed9f9f43bb ("argparse: parse parameters") Signed-off-by: Chengwen Feng --- app/test/test_argparse.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c index 7cecd7116c..09a6366b9f 100644 --- a/app/test/test_argparse.c +++ b/app/test/test_argparse.c @@ -199,11 +199,13 @@ test_argparse_invalid_has_val(void) uint32_t index; int ret; + /* test optional arg don't config has-value. */ obj = test_argparse_init_obj(); obj->args[0].flags &= ~TEST_ARGPARSE_FLAG_HAS_ARG_BITMASK; ret = rte_argparse_parse(obj, default_argc, default_argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); + /* test positional arg don't config required-value. */ for (index = 0; index < RTE_DIM(set_mask); index++) { obj = test_argparse_init_obj(); obj->args[0].name_long = "abc"; @@ -271,6 +273,7 @@ test_argparse_invalid_arg_flags(void) struct rte_argparse *obj; int ret; + /* test set unused bits. */ obj = test_argparse_init_obj(); obj->args[0].flags |= ~(TEST_ARGPARSE_FLAG_HAS_ARG_BITMASK | TEST_ARGPARSE_FLAG_VAL_TYPE_BITMASK | @@ -278,16 +281,18 @@ test_argparse_invalid_arg_flags(void) ret = rte_argparse_parse(obj, default_argc, default_argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); + /* test positional arg should not config multiple. */ obj = test_argparse_init_obj(); obj->args[0].name_long = "positional"; obj->args[0].name_short = NULL; obj->args[0].val_saver = (void *)1; - obj->args[0].val_set = (void *)1; + obj->args[0].val_set = NULL; obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT | RTE_ARGPARSE_ARG_SUPPORT_MULTI; ret = rte_argparse_parse(obj, default_argc, default_argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); + /* test optional arg enabled multiple but prased by autosave. */ obj = test_argparse_init_obj(); obj->args[0].flags |= RTE_ARGPARSE_ARG_SUPPORT_MULTI; ret = rte_argparse_parse(obj, default_argc, default_argv); @@ -325,13 +330,13 @@ test_argparse_invalid_option(void) int ret; obj = test_argparse_init_obj(); - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--invalid"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); obj = test_argparse_init_obj(); - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("invalid"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); @@ -355,7 +360,7 @@ test_argparse_opt_autosave_parse_int_of_no_val(void) obj->args[0].val_set = (void *)100; obj->args[0].flags = flags; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == 0, "Argparse parse expect success!"); @@ -387,7 +392,7 @@ test_argparse_opt_autosave_parse_int_of_required_val(void) obj->args[0].val_set = NULL; obj->args[0].flags = flags; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); argv[2] = test_strdup("100"); ret = rte_argparse_parse(obj, 3, argv); @@ -421,6 +426,7 @@ test_argparse_opt_autosave_parse_int_of_optional_val(void) char *argv[2]; int ret; + /* test without value. */ obj = test_argparse_init_obj(); obj->args[0].name_long = "--test-long"; obj->args[0].name_short = "-t"; @@ -428,7 +434,7 @@ test_argparse_opt_autosave_parse_int_of_optional_val(void) obj->args[0].val_set = (void *)100; obj->args[0].flags = flags; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == 0, "Argparse parse expect success!"); @@ -472,7 +478,8 @@ test_argparse_opt_autosave_parse_int_of_optional_val(void) static int opt_callback_parse_int_of_no_val(uint32_t index, const char *value, void *opaque) { - RTE_SET_USED(index); + if (index != 1) + return -EINVAL; if (value != NULL) return -EINVAL; *(int *)opaque = 100; @@ -493,10 +500,10 @@ test_argparse_opt_callback_parse_int_of_no_val(void) obj->args[0].name_long = "--test-long"; obj->args[0].name_short = "-t"; obj->args[0].val_saver = NULL; - obj->args[0].val_set = (void *)100; + obj->args[0].val_set = (void *)1; obj->args[0].flags = RTE_ARGPARSE_ARG_NO_VALUE; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == 0, "Argparse parse expect success!"); @@ -547,7 +554,7 @@ test_argparse_opt_callback_parse_int_of_required_val(void) obj->args[0].val_set = (void *)1; obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); argv[2] = test_strdup("100"); ret = rte_argparse_parse(obj, 3, argv); @@ -611,7 +618,7 @@ test_argparse_opt_callback_parse_int_of_optional_val(void) obj->args[0].val_set = (void *)1; obj->args[0].flags = RTE_ARGPARSE_ARG_OPTIONAL_VALUE; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("--test-long"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == 0, "Argparse parse expect success!"); @@ -656,6 +663,7 @@ test_argparse_pos_autosave_parse_int(void) char *argv[3]; int ret; + /* test positional autosave parse successful. */ obj = test_argparse_init_obj(); obj->args[0].name_long = "test-long"; obj->args[0].name_short = NULL; @@ -663,19 +671,20 @@ test_argparse_pos_autosave_parse_int(void) obj->args[0].val_set = NULL; obj->args[0].flags = flags; obj->args[1].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("100"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == 0, "Argparse parse expect success!"); TEST_ASSERT(val_saver == 100, "Argparse parse expect success!"); + /* test positional autosave parse failed. */ obj->args[0].flags = flags; val_saver = 0; argv[1] = test_strdup("100a"); ret = rte_argparse_parse(obj, 2, argv); TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!"); - /* test over position parameters. */ + /* test too much position parameters. */ obj->args[0].flags = flags; argv[1] = test_strdup("100"); argv[2] = test_strdup("200"); @@ -713,6 +722,7 @@ test_argparse_pos_callback_parse_int(void) char *argv[3]; int ret; + /* test positional callback parse successful. */ obj = test_argparse_init_obj(); obj->callback = pos_callback_parse_int; obj->opaque = (void *)val_saver; @@ -727,7 +737,7 @@ test_argparse_pos_callback_parse_int(void) obj->args[1].val_set = (void *)2; obj->args[1].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE; obj->args[2].name_long = NULL; - argv[0] = test_strdup(obj->usage); + argv[0] = test_strdup(obj->prog_name); argv[1] = test_strdup("100"); argv[2] = test_strdup("200"); ret = rte_argparse_parse(obj, 3, argv); @@ -735,7 +745,7 @@ test_argparse_pos_callback_parse_int(void) TEST_ASSERT(val_saver[1] == 100, "Argparse parse expect success!"); TEST_ASSERT(val_saver[2] == 200, "Argparse parse expect success!"); - /* test callback return failed. */ + /* test positional callback parse failed. */ obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE; obj->args[1].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE; argv[2] = test_strdup("200a"); -- 2.17.1