netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH 0/3] New features in nft-parsing-test
@ 2014-04-09  8:53 Ana Rey
  2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09  8:53 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

The following patchset contains new features in nft-parsing-test:

* Add the following command-line arguments:

-u/--update <directory>      Update test files from <directory>.
-f/--file <file>             Check test file <file>\n"

* Add an end of line in the exit of ruleset. It is necessary to write the
new generated testfiles.

Ana Rey (3):
  ruleset: Add an end of line in the exit of ruleset.
  tests: New tools to uptade xml and json testfiles.
  tests: Add support to check a json or xml testfile.

 src/ruleset.c            |  2 +-
 tests/nft-parsing-test.c | 88 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 83 insertions(+), 7 deletions(-)

-- 
1.9.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset.
  2014-04-09  8:53 [libnftnl PATCH 0/3] New features in nft-parsing-test Ana Rey
@ 2014-04-09  8:53 ` Ana Rey
  2014-04-09 10:11   ` Arturo Borrero Gonzalez
  2014-04-09 14:59   ` Ana Rey
  2014-04-09  8:54 ` [libnftnl PATCH 2/3] tests: New tools to uptade xml and json testfiles Ana Rey
  2014-04-09  8:54 ` [libnftnl PATCH 3/3] tests: Add support to check a json or xml testfile Ana Rey
  2 siblings, 2 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09  8:53 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

Add an end of line in the exit of ruleset.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/ruleset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/ruleset.c b/src/ruleset.c
index 3cbec09..181aeae 100644
--- a/src/ruleset.c
+++ b/src/ruleset.c
@@ -818,7 +818,7 @@ nft_ruleset_do_snprintf(char *buf, size_t size, const struct nft_ruleset *rs,
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
-	ret = snprintf(buf+offset, size, "%s", nft_ruleset_o_closetag(type));
+	ret = snprintf(buf+offset, size, "%s\n", nft_ruleset_o_closetag(type));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	return offset;
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 2/3] tests: New tools to uptade xml and json testfiles.
  2014-04-09  8:53 [libnftnl PATCH 0/3] New features in nft-parsing-test Ana Rey
  2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
@ 2014-04-09  8:54 ` Ana Rey
  2014-04-09  8:54 ` [libnftnl PATCH 3/3] tests: Add support to check a json or xml testfile Ana Rey
  2 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09  8:54 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

New tools to updata testfiles when the structure of xml or json files
are changed.

It adds a new command-line argument in nft-parsing-test.

With nft-parsing-test -u <directory>, you can update test files in
<directory> with a new structure of xml or json file.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 tests/nft-parsing-test.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c
index a35c064..3de343e 100644
--- a/tests/nft-parsing-test.c
+++ b/tests/nft-parsing-test.c
@@ -18,6 +18,8 @@ enum {
 	TEST_JSON_RULESET,
 };
 
+static bool update = false;
+
 static void print_detail_error(char *a, char *b)
 {
 	int i;
@@ -80,8 +82,24 @@ static int compare_test(uint32_t type, struct nft_ruleset *rs,
 	rewind(fp);
 	fgets(orig, sizeof(orig), fp);
 
-	if (strncmp(orig, out, strlen(out)) == 0)
+	if (strncmp(orig, out, strlen(out)) == 0) {
+		if (update)
+			printf("%s: No changes to update\n", filename);
+		return 0;
+	}
+	if (update) {
+		FILE *fout;
+		printf("%s: Updating test file\n", filename);
+		fout = fopen(filename, "w");
+		if (fout == NULL) {
+			printf("unable to open file %s: %s\n", filename,
+			strerror(errno));
+			return -1;
+		}
+		fwrite(out, strlen(out), 1, fout);
+		fclose(fout);
 		return 0;
+	}
 
 	printf("validating %s: ", filename);
 	printf("\033[31mFAILED\e[0m\n");
@@ -193,15 +211,21 @@ static int execute_test(const char *dir_name)
 
 		if (strcmp(&dent->d_name[len-4], ".xml") == 0) {
 			if ((ret = test_xml(path, err)) == 0) {
-				printf("parsing and validating %s: ", path);
-				printf("\033[32mOK\e[0m\n");
+				if (!update) {
+					printf("parsing and validating %s: ",
+					       path);
+					printf("\033[32mOK\e[0m\n");
+				}
 			}
 			exit_code += ret;
 		}
 		if (strcmp(&dent->d_name[len-5], ".json") == 0) {
 			if ((ret = test_json(path, err)) == 0) {
-				printf("parsing and validating %s: ", path);
-				printf("\033[32mOK\e[0m\n");
+				if (!update) {
+					printf("parsing and validating %s: ",
+					       path);
+					printf("\033[32mOK\e[0m\n");
+				}
 			}
 			exit_code += ret;
 		}
@@ -223,6 +247,7 @@ static void show_help(const char *name)
 "\n"
 "Options:\n"
 "  -d/--dir <directory>		Check test files from <directory>.\n"
+"  -u/--update <directory>	Update test files from <directory>.\n"
 "\n",
 	       name);
 }
@@ -234,6 +259,7 @@ int main(int argc, char *argv[])
 	int option_index = 0;
 	static struct option long_options[] = {
 		{ "dir", required_argument, 0, 'd' },
+		{ "update", required_argument, 0, 'u' },
 		{ 0 }
 	};
 
@@ -243,7 +269,7 @@ int main(int argc, char *argv[])
 	}
 
 	while (1) {
-		val = getopt_long(argc, argv, "d:", long_options,
+		val = getopt_long(argc, argv, "d:u:", long_options,
 				  &option_index);
 
 		if (val == -1)
@@ -253,6 +279,10 @@ int main(int argc, char *argv[])
 		case 'd':
 			ret = execute_test(optarg);
 			break;
+		case 'u':
+			update = true;
+			ret = execute_test(optarg);
+			break;
 		default:
 			show_help(argv[0]);
 			break;
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 3/3] tests: Add support to check a json or xml testfile.
  2014-04-09  8:53 [libnftnl PATCH 0/3] New features in nft-parsing-test Ana Rey
  2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
  2014-04-09  8:54 ` [libnftnl PATCH 2/3] tests: New tools to uptade xml and json testfiles Ana Rey
@ 2014-04-09  8:54 ` Ana Rey
  2 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09  8:54 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

Add new command-line argument "-f <file>" to check a json or xml
testfile.

./nft-parsing-test -f <file>  Check this testfile.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 tests/nft-parsing-test.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c
index 3de343e..9cc68dc 100644
--- a/tests/nft-parsing-test.c
+++ b/tests/nft-parsing-test.c
@@ -240,6 +240,47 @@ static int execute_test(const char *dir_name)
 	return 0;
 }
 
+static int execute_test_file(const char *filename)
+{
+	char path[PATH_MAX];
+	int ret = 0;
+	struct nft_parse_err *err;
+
+	err = nft_parse_err_alloc();
+	if (err == NULL) {
+		perror("error");
+		exit(EXIT_FAILURE);
+	}
+
+	snprintf(path, sizeof(path), "%s", filename);
+
+	int len = strlen(filename);
+	if (strcmp(&filename[len-4], ".xml") == 0) {
+		if ((ret = test_xml(path, err)) == 0) {
+			if (!update) {
+				printf("parsing and validating %s: ",
+				       path);
+				printf("\033[32mOK\e[0m\n");
+			}
+		}
+		exit(EXIT_FAILURE);
+	}
+	if (strcmp(&filename[len-5], ".json") == 0) {
+		if ((ret = test_json(path, err)) == 0) {
+			if (!update) {
+				printf("parsing and validating %s: ",
+				       path);
+				printf("\033[32mOK\e[0m\n");
+			}
+		}
+		exit(EXIT_FAILURE);
+	}
+
+	nft_parse_err_free(err);
+
+	return 0;
+}
+
 static void show_help(const char *name)
 {
 	printf(
@@ -248,6 +289,7 @@ static void show_help(const char *name)
 "Options:\n"
 "  -d/--dir <directory>		Check test files from <directory>.\n"
 "  -u/--update <directory>	Update test files from <directory>.\n"
+"  -f/--file <file>		Check test file <file>\n"
 "\n",
 	       name);
 }
@@ -260,6 +302,7 @@ int main(int argc, char *argv[])
 	static struct option long_options[] = {
 		{ "dir", required_argument, 0, 'd' },
 		{ "update", required_argument, 0, 'u' },
+		{ "file", required_argument, 0, 'f' },
 		{ 0 }
 	};
 
@@ -269,7 +312,7 @@ int main(int argc, char *argv[])
 	}
 
 	while (1) {
-		val = getopt_long(argc, argv, "d:u:", long_options,
+		val = getopt_long(argc, argv, "d:u:f:", long_options,
 				  &option_index);
 
 		if (val == -1)
@@ -283,6 +326,9 @@ int main(int argc, char *argv[])
 			update = true;
 			ret = execute_test(optarg);
 			break;
+		case 'f':
+			ret = execute_test_file(optarg);
+			break;
 		default:
 			show_help(argv[0]);
 			break;
-- 
1.9.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset.
  2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
@ 2014-04-09 10:11   ` Arturo Borrero Gonzalez
  2014-04-09 14:56     ` Ana Rey
  2014-04-09 14:59   ` Ana Rey
  1 sibling, 1 reply; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-04-09 10:11 UTC (permalink / raw)
  To: Ana Rey; +Cc: Netfilter Development Mailing list

On 9 April 2014 10:53, Ana Rey <anarey@gmail.com> wrote:
> Add an end of line in the exit of ruleset.
>
> Signed-off-by: Ana Rey <anarey@gmail.com>

Hi Ana,

Why do you need this here?

The policy was to don't add any format stuff (tabs, newlines..) to
XML|JSON formats and let the caller add the trailing \n if needed..

However, I see several inconsistencies already in the code:
src/rule.c: ret = snprintf(buf+offset, len, "\n");
src/rule.c: ret = snprintf(buf+offset, len, "]\n");
src/rule.c: ret = snprintf(buf+offset, len, " }\n");
src/set.c: ret = snprintf(buf+offset, len, "\n");

-- 
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset.
  2014-04-09 10:11   ` Arturo Borrero Gonzalez
@ 2014-04-09 14:56     ` Ana Rey
  0 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09 14:56 UTC (permalink / raw)
  To: Arturo Borrero Gonzalez; +Cc: Netfilter Development Mailing list

2014-04-09 12:11 GMT+02:00 Arturo Borrero Gonzalez
<arturo.borrero.glez@gmail.com>:
> On 9 April 2014 10:53, Ana Rey <anarey@gmail.com> wrote:
>> Add an end of line in the exit of ruleset.
>>
>> Signed-off-by: Ana Rey <anarey@gmail.com>
>
> Hi Ana,
>
> Why do you need this here?
>
> The policy was to don't add any format stuff (tabs, newlines..) to
> XML|JSON formats and let the caller add the trailing \n if needed..

Thank you for this advise. I'll remember it the next time.

I solved this problem in the tests/nft-parsing-test.c

(I'll send a new version of "[libnftnl PATCH 2/3] tests: New tools to
uptade xml and json testfiles." patch and I deleted it of tihs
patchset)

Thank you!

>
> However, I see several inconsistencies already in the code:
> src/rule.c: ret = snprintf(buf+offset, len, "\n");
> src/rule.c: ret = snprintf(buf+offset, len, "]\n");
> src/rule.c: ret = snprintf(buf+offset, len, " }\n");
> src/set.c: ret = snprintf(buf+offset, len, "\n");
>
> --
> Arturo Borrero González



-- 
Ana Rey (@anaRB)
http://about.me/anarey
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset.
  2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
  2014-04-09 10:11   ` Arturo Borrero Gonzalez
@ 2014-04-09 14:59   ` Ana Rey
  1 sibling, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-04-09 14:59 UTC (permalink / raw)
  To: netfilter-devel

Please, Fotget this patch.

2014-04-09 10:53 GMT+02:00 Ana Rey <anarey@gmail.com>:
> Add an end of line in the exit of ruleset.
>
> Signed-off-by: Ana Rey <anarey@gmail.com>
> ---
>  src/ruleset.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/ruleset.c b/src/ruleset.c
> index 3cbec09..181aeae 100644
> --- a/src/ruleset.c
> +++ b/src/ruleset.c
> @@ -818,7 +818,7 @@ nft_ruleset_do_snprintf(char *buf, size_t size, const struct nft_ruleset *rs,
>                 SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
>         }
>
> -       ret = snprintf(buf+offset, size, "%s", nft_ruleset_o_closetag(type));
> +       ret = snprintf(buf+offset, size, "%s\n", nft_ruleset_o_closetag(type));
>         SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
>
>         return offset;
> --
> 1.9.0
>



-- 
Ana Rey (@anaRB)
http://about.me/anarey

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-04-09 14:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-09  8:53 [libnftnl PATCH 0/3] New features in nft-parsing-test Ana Rey
2014-04-09  8:53 ` [libnftnl PATCH 1/3] ruleset: Add an end of line in the exit of ruleset Ana Rey
2014-04-09 10:11   ` Arturo Borrero Gonzalez
2014-04-09 14:56     ` Ana Rey
2014-04-09 14:59   ` Ana Rey
2014-04-09  8:54 ` [libnftnl PATCH 2/3] tests: New tools to uptade xml and json testfiles Ana Rey
2014-04-09  8:54 ` [libnftnl PATCH 3/3] tests: Add support to check a json or xml testfile Ana Rey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).