diff mbox series

[v4,4/8] examples/fips_validation: allow json file as input

Message ID 20220429161559.415645-5-blo@iol.unh.edu (mailing list archive)
State Superseded
Delegated to: akhil goyal
Headers show
Series Add JSON vector set support to fips validation | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Brandon Lo April 29, 2022, 4:15 p.m. UTC
Added the ability to use the json format as the input
and output of the example application.

Signed-off-by: Brandon Lo <blo@iol.unh.edu>
---
v3:
* fix checkpatch warnings

v2:
* remove use_json variable

 examples/fips_validation/main.c | 200 +++++++++++++++++++++++++++++++-
 1 file changed, 196 insertions(+), 4 deletions(-)

Comments

Gowrishankar Muthukrishnan May 18, 2022, 3:45 p.m. UTC | #1
Verified the functionality changes and looks good to me.

Acked-by: Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>

Thanks.

> -----Original Message-----
> From: Brandon Lo <blo@iol.unh.edu>
> Sent: Friday, April 29, 2022 9:46 PM
> To: roy.fan.zhang@intel.com; ciara.power@intel.com
> Cc: dev@dpdk.org; Brandon Lo <blo@iol.unh.edu>
> Subject: [EXT] [PATCH v4 4/8] examples/fips_validation: allow json file as
> input
> 
> External Email
> 
> ----------------------------------------------------------------------
> Added the ability to use the json format as the input and output of the
> example application.
> 
> Signed-off-by: Brandon Lo <blo@iol.unh.edu>
> ---
> v3:
> * fix checkpatch warnings
> 
> v2:
> * remove use_json variable
> 
>  examples/fips_validation/main.c | 200
> +++++++++++++++++++++++++++++++-
>  1 file changed, 196 insertions(+), 4 deletions(-)
> 
> diff --git a/examples/fips_validation/main.c
> b/examples/fips_validation/main.c index e06ae37567..65f736f3cb 100644
> --- a/examples/fips_validation/main.c
> +++ b/examples/fips_validation/main.c
> @@ -34,11 +34,17 @@ enum {
>  	OPT_CRYPTODEV_BK_ID_NUM,
>  #define OPT_CRYPTODEV_BK_DIR_KEY    "broken-test-dir"
>  	OPT_CRYPTODEV_BK_DIR_KEY_NUM,
> +#define OPT_USE_JSON                "use-json"
> +	OPT_USE_JSON_NUM,
>  };
> 
>  struct fips_test_vector vec;
>  struct fips_test_interim_info info;
> 
> +#ifdef RTE_HAS_JANSSON
> +struct fips_test_json_info json_info;
> +#endif /* RTE_HAS_JANSSON */
> +
>  struct cryptodev_fips_validate_env {
>  	const char *req_path;
>  	const char *rsp_path;
> @@ -166,6 +172,11 @@ cryptodev_fips_validate_app_uninit(void)
>  static int
>  fips_test_one_file(void);
> 
> +#ifdef RTE_HAS_JANSSON
> +static int
> +fips_test_one_json_file(void);
> +#endif /* RTE_HAS_JANSSON */
> +
>  static int
>  parse_cryptodev_arg(char *arg)
>  {
> @@ -389,6 +400,7 @@ int
>  main(int argc, char *argv[])
>  {
>  	int ret;
> +	char use_json;
> 
>  	ret = rte_eal_init(argc, argv);
>  	if (ret < 0) {
> @@ -424,9 +436,17 @@ main(int argc, char *argv[])
>  					ret, env.req_path);
>  			goto exit;
>  		}
> -
> -
> +		use_json = info.file_type == FIPS_TYPE_JSON;
> +
> +#ifdef RTE_HAS_JANSSON
> +		ret = info.file_type == FIPS_TYPE_JSON ?
> +			fips_test_one_json_file() : fips_test_one_file();
> +		if (use_json)
> +			json_decref(json_info.json_root);
> +#else /* RTE_HAS_JANSSON */
>  		ret = fips_test_one_file();
> +#endif /* RTE_HAS_JANSSON */
> +
>  		if (ret < 0) {
>  			RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
>  					ret, env.req_path);
> @@ -480,8 +500,19 @@ main(int argc, char *argv[])
>  						ret, req_path);
>  				break;
>  			}
> +			use_json = info.file_type == FIPS_TYPE_JSON;
> 
> +#ifdef RTE_HAS_JANSSON
> +			if (use_json) {
> +				ret = fips_test_one_json_file();
> +				json_decref(json_info.json_root);
> +			} else {
> +				ret = fips_test_one_file();
> +			}
> +#else /* RTE_HAS_JANSSON */
>  			ret = fips_test_one_file();
> +#endif /* RTE_HAS_JANSSON */
> +
>  			if (ret < 0) {
>  				RTE_LOG(ERR, USER1, "Error %i: Failed test
> %s\n",
>  						ret, req_path);
> @@ -1219,7 +1250,8 @@ fips_generic_test(void)
>  	struct fips_val val = {NULL, 0};
>  	int ret;
> 
> -	fips_test_write_one_case();
> +	if (info.file_type != FIPS_TYPE_JSON)
> +		fips_test_write_one_case();
> 
>  	ret = fips_run_test();
>  	if (ret < 0) {
> @@ -1238,6 +1270,7 @@ fips_generic_test(void)
>  	switch (info.file_type) {
>  	case FIPS_TYPE_REQ:
>  	case FIPS_TYPE_RSP:
> +	case FIPS_TYPE_JSON:
>  		if (info.parse_writeback == NULL)
>  			return -EPERM;
>  		ret = info.parse_writeback(&val);
> @@ -1253,7 +1286,8 @@ fips_generic_test(void)
>  		break;
>  	}
> 
> -	fprintf(info.fp_wr, "\n");
> +	if (info.file_type != FIPS_TYPE_JSON)
> +		fprintf(info.fp_wr, "\n");
>  	free(val.val);
> 
>  	return 0;
> @@ -1844,3 +1878,161 @@ fips_test_one_file(void)
> 
>  	return ret;
>  }
> +
> +#ifdef RTE_HAS_JANSSON
> +static int
> +fips_test_json_init_writeback(void)
> +{
> +	json_t *session_info, *session_write;
> +	session_info = json_array_get(json_info.json_root, 0);
> +	session_write = json_object();
> +	json_info.json_write_root = json_array();
> +
> +	json_object_set(session_write, "jwt",
> +		json_object_get(session_info, "jwt"));
> +	json_object_set(session_write, "url",
> +		json_object_get(session_info, "url"));
> +	json_object_set(session_write, "isSample",
> +		json_object_get(session_info, "isSample"));
> +
> +	json_info.is_sample = json_boolean_value(
> +		json_object_get(session_info, "isSample"));
> +
> +	json_array_append_new(json_info.json_write_root,
> session_write);
> +	return 0;
> +}
> +
> +static int
> +fips_test_one_test_case(void)
> +{
> +	int ret;
> +
> +	ret = fips_test_parse_one_json_case();
> +
> +	switch (ret) {
> +	case 0:
> +		ret = test_ops.test();
> +		if (ret == 0)
> +			break;
> +		RTE_LOG(ERR, USER1, "Error %i: test block\n",
> +				ret);
> +		break;
> +	case 1:
> +		break;
> +	default:
> +		RTE_LOG(ERR, USER1, "Error %i: Parse block\n",
> +				ret);
> +	}
> +	return 0;
> +}
> +
> +static int
> +fips_test_one_test_group(void)
> +{
> +	int ret;
> +	json_t *tests, *write_tests;
> +	size_t test_idx, tests_size;
> +
> +	write_tests = json_array();
> +	json_info.json_write_group = json_object();
> +	json_object_set(json_info.json_write_group, "tgId",
> +		json_object_get(json_info.json_test_group, "tgId"));
> +	json_object_set_new(json_info.json_write_group, "tests",
> write_tests);
> +
> +	switch (info.algo) {
> +	case FIPS_TEST_ALGO_AES_GCM:
> +		ret = parse_test_gcm_init();
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = fips_test_parse_one_json_group();
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = init_test_ops();
> +	if (ret < 0)
> +		return ret;
> +
> +	tests = json_object_get(json_info.json_test_group, "tests");
> +	tests_size = json_array_size(tests);
> +	for (test_idx = 0; test_idx < tests_size; test_idx++) {
> +		json_info.json_test_case = json_array_get(tests, test_idx);
> +		fips_test_one_test_case();
> +		json_array_append_new(write_tests,
> json_info.json_write_case);
> +	}
> +
> +	return 0;
> +}
> +
> +static int
> +fips_test_one_vector_set(void)
> +{
> +	int ret;
> +	json_t *test_groups, *write_groups, *write_version, *write_set;
> +	size_t group_idx, num_groups;
> +
> +	test_groups = json_object_get(json_info.json_vector_set,
> "testGroups");
> +	num_groups = json_array_size(test_groups);
> +
> +	json_info.json_write_set = json_array();
> +	write_version = json_object();
> +	json_object_set_new(write_version, "acvVersion",
> json_string(ACVVERSION));
> +	json_array_append_new(json_info.json_write_set, write_version);
> +
> +	write_set = json_object();
> +	json_array_append_new(json_info.json_write_set, write_set);
> +	write_groups = json_array();
> +
> +	json_object_set(write_set, "vsId",
> +		json_object_get(json_info.json_vector_set, "vsId"));
> +	json_object_set(write_set, "algorithm",
> +		json_object_get(json_info.json_vector_set, "algorithm"));
> +	json_object_set(write_set, "revision",
> +		json_object_get(json_info.json_vector_set, "revision"));
> +	json_object_set_new(write_set, "isSample",
> +		json_boolean(json_info.is_sample));
> +	json_object_set_new(write_set, "testGroups", write_groups);
> +
> +	ret = fips_test_parse_one_json_vector_set();
> +	if (ret < 0) {
> +		RTE_LOG(ERR, USER1, "Error: Unsupported or invalid vector
> set algorithm: %s\n",
> +
> 	json_string_value(json_object_get(json_info.json_vector_set,
> "algorithm")));
> +		return ret;
> +	}
> +
> +	for (group_idx = 0; group_idx < num_groups; group_idx++) {
> +		json_info.json_test_group = json_array_get(test_groups,
> group_idx);
> +		ret = fips_test_one_test_group();
> +		json_array_append_new(write_groups,
> json_info.json_write_group);
> +	}
> +
> +	return 0;
> +}
> +
> +static int
> +fips_test_one_json_file(void)
> +{
> +	size_t vector_set_idx, root_size;
> +
> +	root_size = json_array_size(json_info.json_root);
> +	fips_test_json_init_writeback();
> +
> +	for (vector_set_idx = 1; vector_set_idx < root_size;
> vector_set_idx++) {
> +		/* Vector set index starts at 1, the 0th index contains test
> session
> +		 * information.
> +		 */
> +		json_info.json_vector_set =
> json_array_get(json_info.json_root, vector_set_idx);
> +		fips_test_one_vector_set();
> +		json_array_append_new(json_info.json_write_root,
> json_info.json_write_set);
> +	}
> +
> +	json_dumpf(json_info.json_write_root, info.fp_wr,
> JSON_INDENT(4));
> +	json_decref(json_info.json_write_root);
> +
> +	return 0;
> +}
> +#endif /* RTE_HAS_JANSSON */
> --
> 2.25.1
Gowrishankar Muthukrishnan May 19, 2022, 5:30 a.m. UTC | #2
Please find comments inline from CI point of view.

> -----Original Message-----
....
....
>  main(int argc, char *argv[])
>  {
>  	int ret;
> +	char use_json;
> 
>  	ret = rte_eal_init(argc, argv);
>  	if (ret < 0) {
> @@ -424,9 +436,17 @@ main(int argc, char *argv[])
>  					ret, env.req_path);
>  			goto exit;
>  		}
> -
> -
> +		use_json = info.file_type == FIPS_TYPE_JSON;

use_json is not required apart from setting info.file_type. Some CI build fails
for unused use_json (when !RTE_HAS_JANSSON). Can this be removed ?.

Thanks,
Gowrishankar
Gowrishankar Muthukrishnan May 19, 2022, 9:12 a.m. UTC | #3
+lylavoie@iol.unh.edu

> -----Original Message-----
> From: Gowrishankar Muthukrishnan
> Sent: Thursday, May 19, 2022 11:00 AM
> To: Brandon Lo <blo@iol.unh.edu>; roy.fan.zhang@intel.com;
> ciara.power@intel.com
> Cc: dev@dpdk.org
> Subject: RE: [EXT] [PATCH v4 4/8] examples/fips_validation: allow json file as
> input
> 
> Please find comments inline from CI point of view.
> 
> > -----Original Message-----
> ....
> ....
> >  main(int argc, char *argv[])
> >  {
> >  	int ret;
> > +	char use_json;
> >
> >  	ret = rte_eal_init(argc, argv);
> >  	if (ret < 0) {
> > @@ -424,9 +436,17 @@ main(int argc, char *argv[])
> >  					ret, env.req_path);
> >  			goto exit;
> >  		}
> > -
> > -
> > +		use_json = info.file_type == FIPS_TYPE_JSON;
> 
> use_json is not required apart from setting info.file_type. Some CI build fails
> for unused use_json (when !RTE_HAS_JANSSON). Can this be removed ?.
> 
> Thanks,
> Gowrishankar
diff mbox series

Patch

diff --git a/examples/fips_validation/main.c b/examples/fips_validation/main.c
index e06ae37567..65f736f3cb 100644
--- a/examples/fips_validation/main.c
+++ b/examples/fips_validation/main.c
@@ -34,11 +34,17 @@  enum {
 	OPT_CRYPTODEV_BK_ID_NUM,
 #define OPT_CRYPTODEV_BK_DIR_KEY    "broken-test-dir"
 	OPT_CRYPTODEV_BK_DIR_KEY_NUM,
+#define OPT_USE_JSON                "use-json"
+	OPT_USE_JSON_NUM,
 };
 
 struct fips_test_vector vec;
 struct fips_test_interim_info info;
 
+#ifdef RTE_HAS_JANSSON
+struct fips_test_json_info json_info;
+#endif /* RTE_HAS_JANSSON */
+
 struct cryptodev_fips_validate_env {
 	const char *req_path;
 	const char *rsp_path;
@@ -166,6 +172,11 @@  cryptodev_fips_validate_app_uninit(void)
 static int
 fips_test_one_file(void);
 
+#ifdef RTE_HAS_JANSSON
+static int
+fips_test_one_json_file(void);
+#endif /* RTE_HAS_JANSSON */
+
 static int
 parse_cryptodev_arg(char *arg)
 {
@@ -389,6 +400,7 @@  int
 main(int argc, char *argv[])
 {
 	int ret;
+	char use_json;
 
 	ret = rte_eal_init(argc, argv);
 	if (ret < 0) {
@@ -424,9 +436,17 @@  main(int argc, char *argv[])
 					ret, env.req_path);
 			goto exit;
 		}
-
-
+		use_json = info.file_type == FIPS_TYPE_JSON;
+
+#ifdef RTE_HAS_JANSSON
+		ret = info.file_type == FIPS_TYPE_JSON ?
+			fips_test_one_json_file() : fips_test_one_file();
+		if (use_json)
+			json_decref(json_info.json_root);
+#else /* RTE_HAS_JANSSON */
 		ret = fips_test_one_file();
+#endif /* RTE_HAS_JANSSON */
+
 		if (ret < 0) {
 			RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
 					ret, env.req_path);
@@ -480,8 +500,19 @@  main(int argc, char *argv[])
 						ret, req_path);
 				break;
 			}
+			use_json = info.file_type == FIPS_TYPE_JSON;
 
+#ifdef RTE_HAS_JANSSON
+			if (use_json) {
+				ret = fips_test_one_json_file();
+				json_decref(json_info.json_root);
+			} else {
+				ret = fips_test_one_file();
+			}
+#else /* RTE_HAS_JANSSON */
 			ret = fips_test_one_file();
+#endif /* RTE_HAS_JANSSON */
+
 			if (ret < 0) {
 				RTE_LOG(ERR, USER1, "Error %i: Failed test %s\n",
 						ret, req_path);
@@ -1219,7 +1250,8 @@  fips_generic_test(void)
 	struct fips_val val = {NULL, 0};
 	int ret;
 
-	fips_test_write_one_case();
+	if (info.file_type != FIPS_TYPE_JSON)
+		fips_test_write_one_case();
 
 	ret = fips_run_test();
 	if (ret < 0) {
@@ -1238,6 +1270,7 @@  fips_generic_test(void)
 	switch (info.file_type) {
 	case FIPS_TYPE_REQ:
 	case FIPS_TYPE_RSP:
+	case FIPS_TYPE_JSON:
 		if (info.parse_writeback == NULL)
 			return -EPERM;
 		ret = info.parse_writeback(&val);
@@ -1253,7 +1286,8 @@  fips_generic_test(void)
 		break;
 	}
 
-	fprintf(info.fp_wr, "\n");
+	if (info.file_type != FIPS_TYPE_JSON)
+		fprintf(info.fp_wr, "\n");
 	free(val.val);
 
 	return 0;
@@ -1844,3 +1878,161 @@  fips_test_one_file(void)
 
 	return ret;
 }
+
+#ifdef RTE_HAS_JANSSON
+static int
+fips_test_json_init_writeback(void)
+{
+	json_t *session_info, *session_write;
+	session_info = json_array_get(json_info.json_root, 0);
+	session_write = json_object();
+	json_info.json_write_root = json_array();
+
+	json_object_set(session_write, "jwt",
+		json_object_get(session_info, "jwt"));
+	json_object_set(session_write, "url",
+		json_object_get(session_info, "url"));
+	json_object_set(session_write, "isSample",
+		json_object_get(session_info, "isSample"));
+
+	json_info.is_sample = json_boolean_value(
+		json_object_get(session_info, "isSample"));
+
+	json_array_append_new(json_info.json_write_root, session_write);
+	return 0;
+}
+
+static int
+fips_test_one_test_case(void)
+{
+	int ret;
+
+	ret = fips_test_parse_one_json_case();
+
+	switch (ret) {
+	case 0:
+		ret = test_ops.test();
+		if (ret == 0)
+			break;
+		RTE_LOG(ERR, USER1, "Error %i: test block\n",
+				ret);
+		break;
+	case 1:
+		break;
+	default:
+		RTE_LOG(ERR, USER1, "Error %i: Parse block\n",
+				ret);
+	}
+	return 0;
+}
+
+static int
+fips_test_one_test_group(void)
+{
+	int ret;
+	json_t *tests, *write_tests;
+	size_t test_idx, tests_size;
+
+	write_tests = json_array();
+	json_info.json_write_group = json_object();
+	json_object_set(json_info.json_write_group, "tgId",
+		json_object_get(json_info.json_test_group, "tgId"));
+	json_object_set_new(json_info.json_write_group, "tests", write_tests);
+
+	switch (info.algo) {
+	case FIPS_TEST_ALGO_AES_GCM:
+		ret = parse_test_gcm_init();
+		break;
+	default:
+		return -EINVAL;
+	}
+	if (ret < 0)
+		return ret;
+
+	ret = fips_test_parse_one_json_group();
+	if (ret < 0)
+		return ret;
+
+	ret = init_test_ops();
+	if (ret < 0)
+		return ret;
+
+	tests = json_object_get(json_info.json_test_group, "tests");
+	tests_size = json_array_size(tests);
+	for (test_idx = 0; test_idx < tests_size; test_idx++) {
+		json_info.json_test_case = json_array_get(tests, test_idx);
+		fips_test_one_test_case();
+		json_array_append_new(write_tests, json_info.json_write_case);
+	}
+
+	return 0;
+}
+
+static int
+fips_test_one_vector_set(void)
+{
+	int ret;
+	json_t *test_groups, *write_groups, *write_version, *write_set;
+	size_t group_idx, num_groups;
+
+	test_groups = json_object_get(json_info.json_vector_set, "testGroups");
+	num_groups = json_array_size(test_groups);
+
+	json_info.json_write_set = json_array();
+	write_version = json_object();
+	json_object_set_new(write_version, "acvVersion", json_string(ACVVERSION));
+	json_array_append_new(json_info.json_write_set, write_version);
+
+	write_set = json_object();
+	json_array_append_new(json_info.json_write_set, write_set);
+	write_groups = json_array();
+
+	json_object_set(write_set, "vsId",
+		json_object_get(json_info.json_vector_set, "vsId"));
+	json_object_set(write_set, "algorithm",
+		json_object_get(json_info.json_vector_set, "algorithm"));
+	json_object_set(write_set, "revision",
+		json_object_get(json_info.json_vector_set, "revision"));
+	json_object_set_new(write_set, "isSample",
+		json_boolean(json_info.is_sample));
+	json_object_set_new(write_set, "testGroups", write_groups);
+
+	ret = fips_test_parse_one_json_vector_set();
+	if (ret < 0) {
+		RTE_LOG(ERR, USER1, "Error: Unsupported or invalid vector set algorithm: %s\n",
+			json_string_value(json_object_get(json_info.json_vector_set, "algorithm")));
+		return ret;
+	}
+
+	for (group_idx = 0; group_idx < num_groups; group_idx++) {
+		json_info.json_test_group = json_array_get(test_groups, group_idx);
+		ret = fips_test_one_test_group();
+		json_array_append_new(write_groups, json_info.json_write_group);
+	}
+
+	return 0;
+}
+
+static int
+fips_test_one_json_file(void)
+{
+	size_t vector_set_idx, root_size;
+
+	root_size = json_array_size(json_info.json_root);
+	fips_test_json_init_writeback();
+
+	for (vector_set_idx = 1; vector_set_idx < root_size; vector_set_idx++) {
+		/* Vector set index starts at 1, the 0th index contains test session
+		 * information.
+		 */
+		json_info.json_vector_set = json_array_get(json_info.json_root, vector_set_idx);
+		fips_test_one_vector_set();
+		json_array_append_new(json_info.json_write_root, json_info.json_write_set);
+	}
+
+	json_dumpf(json_info.json_write_root, info.fp_wr, JSON_INDENT(4));
+	json_decref(json_info.json_write_root);
+
+	return 0;
+}
+#endif /* RTE_HAS_JANSSON */