Patchwork [libnftables,2/2] test: compare content parsing with original file content.

login
register
mail settings
Submitter Alvaro Neira
Date Aug. 10, 2013, 7:40 p.m.
Message ID <20130810194051.18629.33864.stgit@Ph0enix>
Download mbox | patch
Permalink /patch/266271/
State Accepted
Headers show

Comments

Alvaro Neira - Aug. 10, 2013, 7:40 p.m.
From: Álvaro Neira Ayuso <alvaroneay@gmail.com>

Before, we have tested only if we have parsed some file but we hadn't tested if the content is OK.
Now, we can test if we can parse some file and if the object's info is the same than the content
in the test files.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
 tests/nft-parsing-test.c |  186 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 170 insertions(+), 16 deletions(-)


--
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
Pablo Neira - Aug. 11, 2013, 7:47 a.m.
On Sat, Aug 10, 2013 at 09:40:51PM +0200, Alvaro Neira wrote:
> From: Álvaro Neira Ayuso <alvaroneay@gmail.com>
> 
> Before, we have tested only if we have parsed some file but we
> hadn't tested if the content is OK.  Now, we can test if we can
> parse some file and if the object's info is the same than the
> content in the test files.

Applied this minor glitches, see below.

> diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c
> index e111fd0..f9cfcdf 100644
> --- a/tests/nft-parsing-test.c
> +++ b/tests/nft-parsing-test.c
[...]
> +	printf("matching %s: ", filename);

renamed this to "validating" instead of matching.

> +	printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));

removed strerr there. If validation fails, ie. strcmp(original,
output) are not equal, errno does not contain any relevant
information.

Thanks.
--
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

Patch

diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c
index e111fd0..f9cfcdf 100644
--- a/tests/nft-parsing-test.c
+++ b/tests/nft-parsing-test.c
@@ -19,6 +19,138 @@ 
 #include <jansson.h>
 #endif
 
+enum {
+	TEST_XML_TABLE = 0,
+	TEST_XML_CHAIN,
+	TEST_XML_RULE,
+	TEST_XML_SET,
+	TEST_JSON_TABLE,
+	TEST_JSON_CHAIN,
+	TEST_JSON_RULE,
+	TEST_JSON_SET,
+};
+
+#if defined(XML_PARSING) || defined(JSON_PARSING)
+static void print_detail_error(char *a, char *b)
+{
+	int i;
+	int from = -1;
+
+	for (i = 0; i < strlen(b); i++) {
+		if (from == -1 && a[i] != b[i]) {
+			from = i;
+			break;
+
+		}
+	}
+
+	if (from != -1) {
+		int k = from - 10;
+
+		if (k < 0)
+			k = 0;
+
+		fprintf(stderr, "from file:     ");
+		for (i = k; i < from + 10; i++)
+			fprintf(stderr, "%c", a[i]);
+
+		fprintf(stderr, "\nfrom snprintf: ");
+		for (i = k; i < from + 10; i++)
+			fprintf(stderr, "%c", b[i]);
+
+		/* Don't look twice below this comment ;-) */
+		fprintf(stderr, "\n               ");
+		for (i = k; i < from + 10; i++) {
+			if (i == from)
+				fprintf(stderr, "^");
+			else
+				fprintf(stderr, " ");
+		}
+		fprintf(stderr, "\n");
+	}
+}
+
+static int compare_test(uint32_t type, void *input, const char *filename)
+{
+	struct nft_table *t = NULL;
+	struct nft_chain *c = NULL;
+	struct nft_rule *r = NULL;
+	struct nft_set *s = NULL;
+	char orig[4096];
+	char out[4096];
+	FILE *fp;
+
+	switch (type) {
+	case TEST_XML_TABLE:
+	case TEST_JSON_TABLE:
+		t = (struct nft_table *)input;
+		break;
+	case TEST_XML_CHAIN:
+	case TEST_JSON_CHAIN:
+		c = (struct nft_chain *)input;
+		break;
+	case TEST_XML_RULE:
+	case TEST_JSON_RULE:
+		r = (struct nft_rule *)input;
+		break;
+	case TEST_XML_SET:
+	case TEST_JSON_SET:
+		s = (struct nft_set *)input;
+		break;
+	default:
+		errno = EINVAL;
+		return -1;
+	}
+
+	switch (type) {
+	case TEST_XML_TABLE:
+		nft_table_snprintf(out, sizeof(out), t, NFT_TABLE_O_XML, 0);
+		break;
+	case TEST_JSON_TABLE:
+		nft_table_snprintf(out, sizeof(out), t, NFT_TABLE_O_JSON, 0);
+		break;
+	case TEST_XML_CHAIN:
+		nft_chain_snprintf(out, sizeof(out), c, NFT_CHAIN_O_XML, 0);
+		break;
+	case TEST_JSON_CHAIN:
+		nft_chain_snprintf(out, sizeof(out), c, NFT_CHAIN_O_JSON, 0);
+		break;
+	case TEST_XML_RULE:
+		nft_rule_snprintf(out, sizeof(out), r, NFT_RULE_O_XML, 0);
+		break;
+	case TEST_JSON_RULE:
+		nft_rule_snprintf(out, sizeof(out), r, NFT_RULE_O_JSON, 0);
+		break;
+	case TEST_XML_SET:
+		nft_set_snprintf(out, sizeof(out), s, NFT_SET_O_XML, 0);
+		break;
+	case TEST_JSON_SET:
+		nft_set_snprintf(out, sizeof(out), s, NFT_SET_O_JSON, 0);
+		break;
+	default:
+		errno = EINVAL;
+		return -1;
+	}
+
+	fp = fopen(filename, "r");
+	if (fp == NULL) {
+		perror("open");
+		exit(EXIT_FAILURE);
+	}
+
+	fgets(orig, sizeof(orig), fp);
+	fclose(fp);
+
+	if (strncmp(orig, out, strlen(out)) == 0)
+		return 0;
+
+	printf("matching %s: ", filename);
+	printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));
+	print_detail_error(orig, out);
+	return -1;
+}
+#endif
+
 static int test_json(const char *filename)
 {
 #ifdef JSON_PARSING
@@ -44,7 +176,9 @@  static int test_json(const char *filename)
 		t = nft_table_alloc();
 		if (t != NULL) {
 			if (nft_table_parse(t, NFT_TABLE_PARSE_JSON, json) == 0)
-				ret = 0;
+				ret = compare_test(TEST_JSON_TABLE, t, filename);
+			else
+				goto failparsing;
 
 			nft_table_free(t);
 		}
@@ -52,13 +186,23 @@  static int test_json(const char *filename)
 		c = nft_chain_alloc();
 		if (c != NULL) {
 			if (nft_chain_parse(c, NFT_CHAIN_PARSE_JSON, json) == 0)
-				ret = 0;
+				ret = compare_test(TEST_JSON_CHAIN, c, filename);
+			else
+				goto failparsing;
 
 			nft_chain_free(c);
 		}
 	}
 
+	free(root);
 	return ret;
+
+failparsing:
+	printf("parsing %s: ", filename);
+	printf("\033[31mFAILED\e[0m (%s)\n",
+	strerror(errno));
+	free(root);
+	return -1;
 #else
 	errno = EOPNOTSUPP;
 	return -1;
@@ -93,7 +237,9 @@  static int test_xml(const char *filename)
 		t = nft_table_alloc();
 		if (t != NULL) {
 			if (nft_table_parse(t, NFT_TABLE_PARSE_XML, xml) == 0)
-				ret = 0;
+				ret = compare_test(TEST_XML_TABLE, t, filename);
+			else
+				goto failparsing;
 
 			nft_table_free(t);
 		}
@@ -101,7 +247,9 @@  static int test_xml(const char *filename)
 		c = nft_chain_alloc();
 		if (c != NULL) {
 			if (nft_chain_parse(c, NFT_CHAIN_PARSE_XML, xml) == 0)
-				ret = 0;
+				ret = compare_test(TEST_XML_CHAIN, c, filename);
+			else
+				goto failparsing;
 
 			nft_chain_free(c);
 		}
@@ -109,7 +257,9 @@  static int test_xml(const char *filename)
 		r = nft_rule_alloc();
 		if (r != NULL) {
 			if (nft_rule_parse(r, NFT_RULE_PARSE_XML, xml) == 0)
-				ret = 0;
+				ret = compare_test(TEST_XML_RULE, r, filename);
+			else
+				goto failparsing;
 
 			nft_rule_free(r);
 		}
@@ -117,13 +267,21 @@  static int test_xml(const char *filename)
 		s = nft_set_alloc();
 		if (s != NULL) {
 			if (nft_set_parse(s, NFT_SET_PARSE_XML, xml) == 0)
-				ret = 0;
+				ret = compare_test(TEST_XML_SET, s, filename);
+			else
+				goto failparsing;
 
 			nft_set_free(s);
 		}
 	}
 
 	return ret;
+
+failparsing:
+	printf("parsing %s: ", filename);
+	printf("\033[31mFAILED\e[0m (%s)\n",
+	strerror(errno));
+	return -1;
 #else
 	errno = EOPNOTSUPP;
 	return -1;
@@ -157,20 +315,16 @@  int main(int argc, char *argv[])
 		snprintf(path, sizeof(path), "%s/%s", argv[1], dent->d_name);
 
 		if (strcmp(&dent->d_name[len-4], ".xml") == 0) {
-			printf("parsing %s: ", path);
-			if (test_xml(path) < 0)
-				printf("\033[31mFAILED\e[0m (%s)\n",
-					strerror(errno));
-			else
+			if (test_xml(path) == 0) {
+				printf("parsing and matching %s: ", path);
 				printf("\033[32mOK\e[0m\n");
+			}
 		}
 		if (strcmp(&dent->d_name[len-5], ".json") == 0) {
-			printf("parsing %s: ", path);
-			if (test_json(path) < 0)
-				printf("\033[31mFAILED\e[0m (%s)\n",
-					strerror(errno));
-			else
+			if (test_json(path) == 0) {
+				printf("parsing and matching %s: ", path);
 				printf("\033[32mOK\e[0m\n");
+			}
 		}
 	}