Skip to contentSkip to conversation

fix uncallable unit tests (Bugzilla 1002)

PATCH Superseded Bruce Richardson 5 participants 16 messages May 20, 2022 – Jun 13, 2022

lore
Share & tools
lore
#0 lore raw

cover fix uncallable unit tests (Bugzilla 1002)

The dump* unit tests are uncallable from our test framework because the command validation in the unit test binary is incorrect. Rather than trying to fix this directly in the binary, a better approach is to add command validation to the cmdline library - which already has 99% of what we need already - and use that from the test app.

Bruce Richardson (2):
cmdline: add function to verify valid commands
test: use cmdline library to validate args

 app/test/commands.c         | 11 -----------
 app/test/test.c             | 25 ++++++++-----------------
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 5 files changed, 43 insertions(+), 33 deletions(-)

--
2.34.1

#1 lore raw

PATCH 1/2 cmdline: add function to verify valid commands

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
lib/cmdline/cmdline_parse.c +17 -3
index 349ec87bd7..75ea74db81 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char * buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;
@@ -284,7 +285,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 
 	/* call func */
 	if (f) {
-		f(result.buf, cl, data);
+		if (call_fn)
+			f(result.buf, cl, data);
 	}
 
 	/* no match */
@@ -296,6 +298,18 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char * buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char * buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +15 -2
index e4d802fff7..6dd210d843 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0)
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0).
+ * on successful parse
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] cmdline: add function to verify valid commands

Quoted 13 lines · Bruce Richardson

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

Quoted 6 lines
---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
#3 lore raw

PATCH 2/2 test: use cmdline library to validate args

In reply to Bruce Richardson

When passing in test names to run via either the DPDK_TEST environment variable or via extra argv parameters, the checks run on those commands can miss valid commands that are registered with the cmdline library in the initial context used to set it up. This is seen in the fact that the "dump_*" set of commands are not callable via argv parameters, but can be called manually.

To fix this, just use the commandline library to validate each command before executing it, stopping execution when an error is encountered. This also has the benefit of not having the test binrary drop to interactive mode if all commandline parameters given are invalid.

Fixes 9b848774a5dc ("test: use env variable to run tests")
Fixes ace2f054ed43 ("test: take test names from command line")

Bugzilla ID: 1002

---
 app/test/commands.c | 11 -----------
 app/test/test.c     | 25 ++++++++-----------------
 2 files changed, 8 insertions(+), 28 deletions(-)
app/test/commands.c +0 -11
index 887cabad64..31259e5c21 100644
@@ -378,14 +378,3 @@ int commands_init(void)
 	cmd_autotest_autotest.string_data.str = commands;
 	return 0;
 }
-
-int command_valid(const char *cmd)
-{
-	struct test_command *t;
-
-	TAILQ_FOREACH(t, &commands_list, next) {
-		if (strcmp(t->command, cmd) == 0)
-			return 1;
-	}
-	return 0;
-}
app/test/test.c +8 -17
index e69cae3eea..5b308ea6a3 100644
@@ -186,22 +186,10 @@ main(int argc, char **argv)
 #ifdef RTE_LIB_CMDLINE
 	char *dpdk_test = getenv("DPDK_TEST");
 
-	if (dpdk_test && strlen(dpdk_test) == 0)
-		dpdk_test = NULL;
-
-	if (dpdk_test && !command_valid(dpdk_test)) {
-		RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test);
-		dpdk_test = NULL;
-	}
-
-	if (dpdk_test)
+	if (dpdk_test && strlen(dpdk_test) > 0)
 		tests[test_count++] = dpdk_test;
-	for (i = 1; i < argc; i++) {
-		if (!command_valid(argv[i]))
-			RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]);
-		else
-			tests[test_count++] = argv[i];
-	}
+	for (i = 1; i < argc; i++)
+		tests[test_count++] = argv[i];
 
 	if (test_count > 0) {
 		char buf[1024];
@@ -214,9 +202,12 @@ main(int argc, char **argv)
 
 		for (i = 0; i < test_count; i++) {
 			snprintf(buf, sizeof(buf), "%s\n", tests[i]);
-			if (cmdline_in(cl, buf, strlen(buf)) < 0) {
+			if (cmdline_parse_check(cl, buf) < 0) {
+				printf("Error: invalid test command: '%s'\n", tests[i]);
+				ret = -1;
+			}
+			else if (cmdline_in(cl, buf, strlen(buf)) < 0) {
 				printf("error on cmdline input\n");
-
 				ret = -1;
 			} else
 				ret = last_test_result;
Signature · 1 line
2.34.1
#4 lore raw

v2 cover fix uncallable unit tests (Bugzilla 1002)

In reply to Bruce Richardson

The dump* unit tests are uncallable from our test framework because the command validation in the unit test binary is incorrect. Rather than trying to fix this directly in the binary, a better approach is to add command validation to the cmdline library - which already has 99% of what we need already - and use that from the test app.

V2:
* Fix checkpatch issues

Bruce Richardson (2):
cmdline: add function to verify valid commands
test: use cmdline library to validate args

 app/test/commands.c         | 11 -----------
 app/test/test.c             | 24 +++++++-----------------
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 5 files changed, 42 insertions(+), 33 deletions(-)

--
2.34.1

#5 lore raw

PATCH v2 1/2 cmdline: add function to verify valid commands

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
lib/cmdline/cmdline_parse.c +17 -3
index 349ec87bd7..b7fdc67ae5 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;
@@ -284,7 +285,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 
 	/* call func */
 	if (f) {
-		f(result.buf, cl, data);
+		if (call_fn)
+			f(result.buf, cl, data);
 	}
 
 	/* no match */
@@ -296,6 +298,18 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +15 -2
index e4d802fff7..6dd210d843 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0)
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0).
+ * on successful parse
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1
Tested-by on v2 1/2
#6 lore raw

RE: [PATCH v2 1/2] cmdline: add function to verify valid commands

Quoted 22 lines

-----Original Message-----
From: Richardson, Bruce <[email protected]>
Sent: Friday, May 20, 2022 11:13 PM
To: [email protected]
Cc: [email protected]; Li, WeiyuanX <[email protected]>; Richardson, Bruce <[email protected]>; Olivier Matz <[email protected]>; Ray Kinsella <[email protected]>
Subject: [PATCH v2 1/2] cmdline: add function to verify valid commands

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

Quoted 2 lines

---

Regards,
Li, Weiyuan

Acked-by on v2 1/2
#7 lore raw

Re: [PATCH v2 1/2] cmdline: add function to verify valid commands

In reply to Bruce Richardson

Hi Bruce,

Just few minor comments below.

Quoted 44 lines · Bruce Richardson

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
lib/cmdline/cmdline_parse.c +3 -2
index 349ec87bd7..b7fdc67ae5 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;

lib/cmdline/cmdline_parse.c cmdline_parse(struct cmdline *cl, const char * buf) · line 289

Quoted 6 lines
lib/cmdline/cmdline_parse.c +2 -1
@@ -284,7 +285,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 
 	/* call func */
 	if (f) {
-		f(result.buf, cl, data);
+		if (call_fn)
+			f(result.buf, cl, data);

Maybe nicer to test in one line:

if (f && call_fn)

Quoted 36 lines
lib/cmdline/cmdline_parse.c +12 -0
 	}
 
 	/* no match */
@@ -296,6 +298,18 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +2 -0
index e4d802fff7..6dd210d843 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 154

Quoted 6 lines
lib/cmdline/cmdline_parse.h +1 -2
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0)

Can we add a dot at the end?

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 165

Quoted 11 lines
lib/cmdline/cmdline_parse.h +8 -0
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0).
+ * on successful parse

Same here.

Quoted 23 lines
lib/cmdline/cmdline_parse.h +4 -0
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1

With these changes:

#8 lore raw

Re: [PATCH v2 1/2] cmdline: add function to verify valid commands

Quoted 58 lines · Olivier Matz

Hi Bruce,

Just few minor comments below.

Quoted 44 lines · Bruce Richardson

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
lib/cmdline/cmdline_parse.c +3 -2
index 349ec87bd7..b7fdc67ae5 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;

lib/cmdline/cmdline_parse.c cmdline_parse(struct cmdline *cl, const char * buf) · line 289

Quoted 6 lines
lib/cmdline/cmdline_parse.c +2 -1
@@ -284,7 +285,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 
 	/* call func */
 	if (f) {
-		f(result.buf, cl, data);
+		if (call_fn)
+			f(result.buf, cl, data);

Maybe nicer to test in one line:

Quoted 2 lines

if (f && call_fn)

If we do so, then we need to also change the "else" leg to "else if (!call_fn)" because we don't want to have the debug_printf being output in the case that we have call_fn == false. A better alternative is to slightly restructure the whole block, to have the error leg first, which removes the need for two condition checks before calling the function:

        /* no match */
        if (f == NULL) {
                debug_printf("No match err=%d\n", err);
                return err;
        }

        /* call func if requested*/
        if (call_fn)
                f(result.buf, cl, data);

        return linelen;

I think this latter option is better, so will implement in v3.

Quoted 31 lines
Quoted 31 lines
lib/cmdline/cmdline_parse.c +12 -0
 	}
 
 	/* no match */
@@ -296,6 +298,18 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +0 -0
index e4d802fff7..6dd210d843 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
Quoted 14 lines

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 154

Quoted 12 lines
lib/cmdline/cmdline_parse.h +3 -2
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0)

Can we add a dot at the end?

Ack.

Quoted 13 lines

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 165

Quoted 12 lines
lib/cmdline/cmdline_parse.h +8 -0
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0).
+ * on successful parse
Quoted 1 line

Same here.

Ack.

Quoted 28 lines
Quoted 24 lines
lib/cmdline/cmdline_parse.h +4 -0
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1

With these changes:

#9 lore raw

Re: [PATCH v2 1/2] cmdline: add function to verify valid commands

Quoted 59 lines · Bruce Richardson
Quoted 58 lines · Olivier Matz

Hi Bruce,

Just few minor comments below.

Quoted 44 lines · Bruce Richardson

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 20 +++++++++++++++++---
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 35 insertions(+), 5 deletions(-)
lib/cmdline/cmdline_parse.c +3 -2
index 349ec87bd7..b7fdc67ae5 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;

lib/cmdline/cmdline_parse.c cmdline_parse(struct cmdline *cl, const char * buf) · line 289

Quoted 6 lines
lib/cmdline/cmdline_parse.c +2 -1
@@ -284,7 +285,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 
 	/* call func */
 	if (f) {
-		f(result.buf, cl, data);
+		if (call_fn)
+			f(result.buf, cl, data);

Maybe nicer to test in one line:

Quoted 4 lines
Quoted 2 lines

if (f && call_fn)

If we do so, then we need to also change the "else" leg to "else if

Oh yes I missed the else part!

Quoted 17 lines

(!call_fn)" because we don't want to have the debug_printf being output in the case that we have call_fn == false. A better alternative is to slightly restructure the whole block, to have the error leg first, which removes the need for two condition checks before calling the function:

        /* no match */
        if (f == NULL) {
                debug_printf("No match err=%d\n", err);
                return err;
        }

        /* call func if requested*/
        if (call_fn)
                f(result.buf, cl, data);

        return linelen;
Quoted 1 line

I think this latter option is better, so will implement in v3.

Yes, it looks good to me, thanks!

Quoted 94 lines
Quoted 32 lines
Quoted 32 lines
lib/cmdline/cmdline_parse.c +12 -0
 	}
 
 	/* no match */
@@ -296,6 +298,18 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +0 -0
index e4d802fff7..6dd210d843 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
Quoted 14 lines

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 154

Quoted 12 lines
lib/cmdline/cmdline_parse.h +3 -2
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0)

Can we add a dot at the end?

Ack.

Quoted 13 lines

lib/cmdline/cmdline_parse.h typedef cmdline_parse_inst_t *cmdline_parse_ctx_t; · line 165

Quoted 12 lines
lib/cmdline/cmdline_parse.h +8 -0
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0).
+ * on successful parse
Quoted 1 line

Same here.

Ack.

Quoted 28 lines
Quoted 24 lines
lib/cmdline/cmdline_parse.h +4 -0
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1

With these changes:

#10 lore raw

PATCH v2 2/2 test: use cmdline library to validate args

In reply to Bruce Richardson

When passing in test names to run via either the DPDK_TEST environment variable or via extra argv parameters, the checks run on those commands can miss valid commands that are registered with the cmdline library in the initial context used to set it up. This is seen in the fact that the "dump_*" set of commands are not callable via argv parameters, but can be called manually.

To fix this, just use the commandline library to validate each command before executing it, stopping execution when an error is encountered. This also has the benefit of not having the test binrary drop to interactive mode if all commandline parameters given are invalid.

Fixes 9b848774a5dc ("test: use env variable to run tests")
Fixes ace2f054ed43 ("test: take test names from command line")

Bugzilla ID: 1002

---
 app/test/commands.c | 11 -----------
 app/test/test.c     | 24 +++++++-----------------
 2 files changed, 7 insertions(+), 28 deletions(-)
app/test/commands.c +0 -11
index 887cabad64..31259e5c21 100644
@@ -378,14 +378,3 @@ int commands_init(void)
 	cmd_autotest_autotest.string_data.str = commands;
 	return 0;
 }
-
-int command_valid(const char *cmd)
-{
-	struct test_command *t;
-
-	TAILQ_FOREACH(t, &commands_list, next) {
-		if (strcmp(t->command, cmd) == 0)
-			return 1;
-	}
-	return 0;
-}
app/test/test.c +7 -17
index e69cae3eea..ce25f468a6 100644
@@ -186,22 +186,10 @@ main(int argc, char **argv)
 #ifdef RTE_LIB_CMDLINE
 	char *dpdk_test = getenv("DPDK_TEST");
 
-	if (dpdk_test && strlen(dpdk_test) == 0)
-		dpdk_test = NULL;
-
-	if (dpdk_test && !command_valid(dpdk_test)) {
-		RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test);
-		dpdk_test = NULL;
-	}
-
-	if (dpdk_test)
+	if (dpdk_test && strlen(dpdk_test) > 0)
 		tests[test_count++] = dpdk_test;
-	for (i = 1; i < argc; i++) {
-		if (!command_valid(argv[i]))
-			RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]);
-		else
-			tests[test_count++] = argv[i];
-	}
+	for (i = 1; i < argc; i++)
+		tests[test_count++] = argv[i];
 
 	if (test_count > 0) {
 		char buf[1024];
@@ -214,9 +202,11 @@ main(int argc, char **argv)
 
 		for (i = 0; i < test_count; i++) {
 			snprintf(buf, sizeof(buf), "%s\n", tests[i]);
-			if (cmdline_in(cl, buf, strlen(buf)) < 0) {
+			if (cmdline_parse_check(cl, buf) < 0) {
+				printf("Error: invalid test command: '%s'\n", tests[i]);
+				ret = -1;
+			} else if (cmdline_in(cl, buf, strlen(buf)) < 0) {
 				printf("error on cmdline input\n");
-
 				ret = -1;
 			} else
 				ret = last_test_result;
Signature · 1 line
2.34.1
Acked-by on v2 2/2
#11 lore raw

Re: [PATCH v2 2/2] test: use cmdline library to validate args

Quoted 12 lines · Bruce Richardson

When passing in test names to run via either the DPDK_TEST environment variable or via extra argv parameters, the checks run on those commands can miss valid commands that are registered with the cmdline library in the initial context used to set it up. This is seen in the fact that the "dump_*" set of commands are not callable via argv parameters, but can be called manually.

To fix this, just use the commandline library to validate each command before executing it, stopping execution when an error is encountered. This also has the benefit of not having the test binrary drop to interactive mode if all commandline parameters given are invalid.

Quoted 5 lines
Fixes 9b848774a5dc ("test: use env variable to run tests")
Fixes ace2f054ed43 ("test: take test names from command line")

Bugzilla ID: 1002

#12 lore raw

v3 cover fix uncallable unit tests (Bugzilla 1002)

In reply to Bruce Richardson

The dump* unit tests are uncallable from our test framework because the command validation in the unit test binary is incorrect. Rather than trying to fix this directly in the binary, a better approach is to add command validation to the cmdline library - which already has 99% of what we need already - and use that from the test app.

Patch fix not CC'ed to stable, as fix requires new function in cmdline library.

V3:
* Fix feedback from Olivier's review
* Fix typo in spelling of "binary" (binrary) in patch 2 commit log

V2:
* Fix checkpatch issues

Bruce Richardson (2):
cmdline: add function to verify valid commands
test: use cmdline library to validate args

 app/test/commands.c         | 11 -----------
 app/test/test.c             | 24 +++++++-----------------
 lib/cmdline/cmdline_parse.c | 28 ++++++++++++++++++++--------
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 5 files changed, 45 insertions(+), 38 deletions(-)

--
2.34.1

#13 lore raw

PATCH v3 1/2 cmdline: add function to verify valid commands

The cmdline library cmdline_parse() function parses a command and executes the action automatically too. The cmdline_valid_buffer function also uses this function to validate commands, meaning that there is no function to validate a command as ok without executing it.

To fix this omission, we extract the body of cmdline_parse into a new static inline function with an extra parameter to indicate whether the action should be performed or not. Then we create two wrappers around that - a replacement for the existing cmdline_parse function where the extra parameter is "true" to execute the command, and a new function "cmdline_parse_check" which passes the parameter as "false" to perform cmdline validation only.

---
 lib/cmdline/cmdline_parse.c | 28 ++++++++++++++++++++--------
 lib/cmdline/cmdline_parse.h | 17 +++++++++++++++--
 lib/cmdline/version.map     |  3 +++
 3 files changed, 38 insertions(+), 10 deletions(-)
lib/cmdline/cmdline_parse.c +20 -8
index 349ec87bd7..ba7f4b80cb 100644
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include <rte_string_fns.h>
 
@@ -182,8 +183,8 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
 }
 
 
-int
-cmdline_parse(struct cmdline *cl, const char * buf)
+static inline int
+__cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
 {
 	unsigned int inst_num=0;
 	cmdline_parse_inst_t *inst;
@@ -282,20 +283,31 @@ cmdline_parse(struct cmdline *cl, const char * buf)
 		inst = ctx[inst_num];
 	}
 
-	/* call func */
-	if (f) {
-		f(result.buf, cl, data);
-	}
-
 	/* no match */
-	else {
+	if (f == NULL) {
 		debug_printf("No match err=%d\n", err);
 		return err;
 	}
 
+	/* call func if requested*/
+	if (call_fn)
+		f(result.buf, cl, data);
+
 	return linelen;
 }
 
+int
+cmdline_parse(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, true);
+}
+
+int
+cmdline_parse_check(struct cmdline *cl, const char *buf)
+{
+	return __cmdline_parse(cl, buf, false);
+}
+
 int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 		 char *dst, unsigned int size)
lib/cmdline/cmdline_parse.h +15 -2
index e4d802fff7..f4241a68fe 100644
@@ -7,6 +7,8 @@
 #ifndef _CMDLINE_PARSE_H_
 #define _CMDLINE_PARSE_H_
 
+#include <rte_compat.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -149,11 +151,22 @@ typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
  * argument buf must ends with "\n\0". The function returns
  * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
  * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
- * function (defined in the context) and returns 0
- * (CMDLINE_PARSE_SUCCESS).
+ * function (defined in the context) and returns the parsed line length (>= 0).
  */
 int cmdline_parse(struct cmdline *cl, const char *buf);
 
+/**
+ * Try to parse a buffer according to the specified context, but do not
+ * perform any function calls if parse is successful.
+ *
+ * The argument buf must ends with "\n\0".
+ * The function returns CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
+ * CMDLINE_PARSE_BAD_ARGS on error and returns the parsed line length (>=0)
+ * on successful parse.
+ */
+__rte_experimental
+int cmdline_parse_check(struct cmdline *cl, const char *buf);
+
 /**
  * complete() must be called with *state==0 (try to complete) or
  * with *state==-1 (just display choices), then called without
lib/cmdline/version.map +3 -0
index b9bbb87510..fc7fdd6ea4 100644
@@ -81,5 +81,8 @@ EXPERIMENTAL {
 	rdline_get_history_buffer_size;
 	rdline_get_opaque;
 
+	# added in 22.07
+	cmdline_parse_check;
+
 	local: *;
 };
Signature · 1 line
2.34.1
#14 lore raw

PATCH v3 2/2 test: use cmdline library to validate args

In reply to Bruce Richardson

When passing in test names to run via either the DPDK_TEST environment variable or via extra argv parameters, the checks run on those commands can miss valid commands that are registered with the cmdline library in the initial context used to set it up. This is seen in the fact that the "dump_*" set of commands are not callable via argv parameters, but can be called manually.

To fix this, just use the commandline library to validate each command before executing it, stopping execution when an error is encountered. This also has the benefit of not having the test binray drop to interactive mode if all commandline parameters given are invalid.

Fixes 9b848774a5dc ("test: use env variable to run tests")
Fixes ace2f054ed43 ("test: take test names from command line")

Bugzilla ID: 1002

---
 app/test/commands.c | 11 -----------
 app/test/test.c     | 24 +++++++-----------------
 2 files changed, 7 insertions(+), 28 deletions(-)
app/test/commands.c +0 -11
index 887cabad64..31259e5c21 100644
@@ -378,14 +378,3 @@ int commands_init(void)
 	cmd_autotest_autotest.string_data.str = commands;
 	return 0;
 }
-
-int command_valid(const char *cmd)
-{
-	struct test_command *t;
-
-	TAILQ_FOREACH(t, &commands_list, next) {
-		if (strcmp(t->command, cmd) == 0)
-			return 1;
-	}
-	return 0;
-}
app/test/test.c +7 -17
index 8a7dddde9b..fb073ff795 100644
@@ -186,22 +186,10 @@ main(int argc, char **argv)
 #ifdef RTE_LIB_CMDLINE
 	char *dpdk_test = getenv("DPDK_TEST");
 
-	if (dpdk_test && strlen(dpdk_test) == 0)
-		dpdk_test = NULL;
-
-	if (dpdk_test && !command_valid(dpdk_test)) {
-		RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test);
-		dpdk_test = NULL;
-	}
-
-	if (dpdk_test)
+	if (dpdk_test && strlen(dpdk_test) > 0)
 		tests[test_count++] = dpdk_test;
-	for (i = 1; i < argc; i++) {
-		if (!command_valid(argv[i]))
-			RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]);
-		else
-			tests[test_count++] = argv[i];
-	}
+	for (i = 1; i < argc; i++)
+		tests[test_count++] = argv[i];
 
 	if (test_count > 0) {
 		char buf[1024];
@@ -214,9 +202,11 @@ main(int argc, char **argv)
 
 		for (i = 0; i < test_count; i++) {
 			snprintf(buf, sizeof(buf), "%s\n", tests[i]);
-			if (cmdline_in(cl, buf, strlen(buf)) < 0) {
+			if (cmdline_parse_check(cl, buf) < 0) {
+				printf("Error: invalid test command: '%s'\n", tests[i]);
+				ret = -1;
+			} else if (cmdline_in(cl, buf, strlen(buf)) < 0) {
 				printf("error on cmdline input\n");
-
 				ret = -1;
 			} else
 				ret = last_test_result;
Signature · 1 line
2.34.1
#15 lore raw
In reply to Bruce Richardson
Quoted 8 lines · Bruce Richardson

The dump* unit tests are uncallable from our test framework because the command validation in the unit test binary is incorrect. Rather than trying to fix this directly in the binary, a better approach is to add command validation to the cmdline library - which already has 99% of what we need already - and use that from the test app.

Patch fix not CC'ed to stable, as fix requires new function in cmdline library.

Series applied, thanks Bruce.

Signature · 1 line
David Marchand