Skip to contentSkip to conversation

test: return EXIT_FAILURE if some error was found

PATCH Applied Arturo Borrero Gonzalez 2 participants 12 messages Oct 31, 2013 – Nov 3, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 test: return EXIT_FAILURE if some error was found

Before this patch, 0 was returned unconditionally.

---
 tests/nft-parsing-test.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
tests/nft-parsing-test.c +11 -4
index 866c985..70028a0 100644
@@ -234,7 +234,7 @@ static int test_json(const char *filename)
 			if (nft_ruleset_parse(rs, NFT_RULESET_PARSE_JSON, json) == 0)
 				ret = compare_test(TEST_JSON_RULESET, rs, filename);
 			else
-				ret = -1;
+				goto failparsing;
 
 			nft_ruleset_free(rs);
 			}
@@ -329,7 +329,7 @@ static int test_xml(const char *filename)
 				ret = compare_test(TEST_XML_RULESET, rs,
 						   filename);
 			else
-				ret = -1;
+				goto failparsing;
 
 			nft_ruleset_free(rs);
 		}
@@ -352,6 +352,7 @@ int main(int argc, char *argv[])
 	DIR *d;
 	struct dirent *dent;
 	char path[PATH_MAX];
+	int ret = 0, exit_code = 0;
 
 	if (argc != 2) {
 		fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
@@ -374,19 +375,25 @@ 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) {
-			if (test_xml(path) == 0) {
+			if ((ret = test_xml(path)) == 0) {
 				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 (test_json(path) == 0) {
+			if ((ret = test_json(path)) == 0) {
 				printf("parsing and validating %s: ", path);
 				printf("\033[32mOK\e[0m\n");
 			}
+			exit_code += ret;
 		}
 	}
 
 	closedir(d);
+
+	if (exit_code != 0)
+		exit(EXIT_FAILURE);
+
 	return 0;
 }
#1 lore raw

PATCH 2/6 test: fix memleak in XML testing

Ensure the tree is freed when done.

---
 tests/nft-parsing-test.c |    2 ++
 1 file changed, 2 insertions(+)
tests/nft-parsing-test.c +2 -0
index 70028a0..5eaaa1e 100644
@@ -335,9 +335,11 @@ static int test_xml(const char *filename)
 		}
 	}
 
+	mxmlDelete(tree);
 	return ret;
 
 failparsing:
+	mxmlDelete(tree);
 	printf("parsing %s: ", filename);
 	printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));
 	return -1;
maintainer Applied
#2 lore raw

Re: [libnftables PATCH 2/6] test: fix memleak in XML testing

Quoted 1 line · Arturo Borrero Gonzalez

Ensure the tree is freed when done.

Applied, thanks.

#3 lore raw

PATCH 3/6 test: use constants for coloured strings

In reply to Arturo Borrero Gonzalez

Use constants for coloured strings, which have a ugly format.

---
 tests/nft-parsing-test.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tests/nft-parsing-test.c +9 -8
index 5eaaa1e..5333dbd 100644
@@ -33,6 +33,9 @@ enum {
 	TEST_JSON_RULESET,
 };
 
+#define FAILED_STR "\033[31mFAILED\e[0m"
+#define OK_STR "\033[32mOK\e[0m"
+
 #if defined(XML_PARSING) || defined(JSON_PARSING)
 static void print_detail_error(char *a, char *b)
 {
@@ -160,8 +163,7 @@ static int compare_test(uint32_t type, void *input, const char *filename)
 	if (strncmp(orig, out, strlen(out)) == 0)
 		return 0;
 
-	printf("validating %s: ", filename);
-	printf("\033[31mFAILED\e[0m\n");
+	printf("parsing and validating %s: %s\n", filename, FAILED_STR);
 	print_detail_error(orig, out);
 	return -1;
 }
@@ -245,8 +247,7 @@ static int test_json(const char *filename)
 	return ret;
 
 failparsing:
-	printf("parsing %s: ", filename);
-	printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));
+	printf("parsing %s: %s (%s)\n", filename, FAILED_STR, strerror(errno));
 	free(json);
 	json_decref(root);
 	return -1;
@@ -340,8 +341,8 @@ static int test_xml(const char *filename)
 
 failparsing:
 	mxmlDelete(tree);
-	printf("parsing %s: ", filename);
-	printf("\033[31mFAILED\e[0m (%s)\n", strerror(errno));
+	printf("parsing and validating %s: %s (%s)\n", filename, FAILED_STR,
+	       strerror(errno));
 	return -1;
 #else
 	errno = EOPNOTSUPP;
@@ -379,14 +380,14 @@ int main(int argc, char *argv[])
 		if (strcmp(&dent->d_name[len-4], ".xml") == 0) {
 			if ((ret = test_xml(path)) == 0) {
 				printf("parsing and validating %s: ", path);
-				printf("\033[32mOK\e[0m\n");
+				printf("%s\n", OK_STR);
 			}
 			exit_code += ret;
 		}
 		if (strcmp(&dent->d_name[len-5], ".json") == 0) {
 			if ((ret = test_json(path)) == 0) {
 				printf("parsing and validating %s: ", path);
-				printf("\033[32mOK\e[0m\n");
+				printf("%s\n", OK_STR);
 			}
 			exit_code += ret;
 		}
#4 lore raw

Re: [libnftables PATCH 3/6] test: use constants for coloured strings

Quoted 12 lines · Arturo Borrero Gonzalez

Use constants for coloured strings, which have a ugly format.

---
 tests/nft-parsing-test.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tests/nft-parsing-test.c +0 -0
index 5eaaa1e..5333dbd 100644

tests/nft-parsing-test.c enum { · line 37

Quoted 5 lines
tests/nft-parsing-test.c +2 -0
@@ -33,6 +33,9 @@ enum {
 	TEST_JSON_RULESET,
 };
 
+#define FAILED_STR "\033[31mFAILED\e[0m"
+#define OK_STR "\033[32mOK\e[0m"

I don't like this macro thing, sorry.

#5 lore raw

PATCH 4/6 test: report errors building XML tree

In reply to Arturo Borrero Gonzalez

Report error when building XML tree.

---
 tests/nft-parsing-test.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tests/nft-parsing-test.c +8 -2
index 5333dbd..3d26c3a 100644
@@ -274,12 +274,18 @@ static int test_xml(const char *filename)
 	tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
 	fclose(fp);
 
-	if (tree == NULL)
+	if (tree == NULL) {
+		printf("unable to build XML tree from file %s %s\n", filename,
+		       FAILED_STR);
 		return -1;
+	}
 
 	xml = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
-	if (xml == NULL)
+	if (xml == NULL) {
+		printf("unable to alloc string from XML tree from %s %s\n",
+		       filename, FAILED_STR);
 		return -1;
+	}
 
 	/* Check what parsing should be done */
 	if (strcmp(tree->value.opaque, "table") == 0) {
maintainer Applied
#6 lore raw

Re: [libnftables PATCH 4/6] test: report errors building XML tree

Quoted 1 line · Arturo Borrero Gonzalez

Report error when building XML tree.

Applied, thanks.

#7 lore raw

PATCH 5/6 test: style cleanup

In reply to Arturo Borrero Gonzalez

Some style cleanups.

---
 tests/nft-parsing-test.c |   34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
tests/nft-parsing-test.c +22 -12
index 3d26c3a..e147e79 100644
@@ -193,8 +193,10 @@ static int test_json(const char *filename)
 	if (json_object_get(root, "table") != NULL) {
 		t = nft_table_alloc();
 		if (t != NULL) {
-			if (nft_table_parse(t, NFT_TABLE_PARSE_JSON, json) == 0)
-				ret = compare_test(TEST_JSON_TABLE, t, filename);
+			if (nft_table_parse(t,
+					    NFT_TABLE_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_TABLE,
+						   t, filename);
 			else
 				goto failparsing;
 
@@ -203,8 +205,10 @@ static int test_json(const char *filename)
 	} else if (json_object_get(root, "chain") != NULL) {
 		c = nft_chain_alloc();
 		if (c != NULL) {
-			if (nft_chain_parse(c, NFT_CHAIN_PARSE_JSON, json) == 0)
-				ret = compare_test(TEST_JSON_CHAIN, c, filename);
+			if (nft_chain_parse(c,
+					    NFT_CHAIN_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_CHAIN,
+						   c, filename);
 			else
 				goto failparsing;
 
@@ -213,8 +217,10 @@ static int test_json(const char *filename)
 	} else if (json_object_get(root, "rule") != NULL) {
 		r = nft_rule_alloc();
 		if (r != NULL) {
-			if (nft_rule_parse(r, NFT_RULE_PARSE_JSON, json) == 0)
-				ret = compare_test(TEST_JSON_RULE, r, filename);
+			if (nft_rule_parse(r,
+					   NFT_RULE_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_RULE,
+						   r, filename);
 			else
 				goto failparsing;
 
@@ -229,17 +235,19 @@ static int test_json(const char *filename)
 				goto failparsing;
 
 			nft_set_free(s);
-			}
+		}
 	} else if (json_object_get(root, "nftables") != NULL) {
 		rs = nft_ruleset_alloc();
 		if (rs != NULL) {
-			if (nft_ruleset_parse(rs, NFT_RULESET_PARSE_JSON, json) == 0)
-				ret = compare_test(TEST_JSON_RULESET, rs, filename);
+			if (nft_ruleset_parse(rs,
+					      NFT_RULESET_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_RULESET,
+						   rs, filename);
 			else
 				goto failparsing;
 
 			nft_ruleset_free(rs);
-			}
+		}
 	}
 
 	free(json);
@@ -292,7 +300,8 @@ 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 = compare_test(TEST_XML_TABLE, t, filename);
+				ret = compare_test(TEST_XML_TABLE,
+						   t, filename);
 			else
 				goto failparsing;
 
@@ -302,7 +311,8 @@ 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 = compare_test(TEST_XML_CHAIN, c, filename);
+				ret = compare_test(TEST_XML_CHAIN,
+						   c, filename);
 			else
 				goto failparsing;
 
#8 lore raw

Re: [libnftables PATCH 5/6] test: style cleanup

Quoted 13 lines · Arturo Borrero Gonzalez

Some style cleanups.

---
 tests/nft-parsing-test.c |   34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
tests/nft-parsing-test.c +0 -0
index 3d26c3a..e147e79 100644
@@ -193,8 +193,10 @@ static int test_json(const char *filename)
 	if (json_object_get(root, "table") != NULL) {

tests/nft-parsing-test.c static int test_json(const char *filename) · line 201

Quoted 10 lines
tests/nft-parsing-test.c +4 -2
 		t = nft_table_alloc();
 		if (t != NULL) {
-			if (nft_table_parse(t, NFT_TABLE_PARSE_JSON, json) == 0)
-				ret = compare_test(TEST_JSON_TABLE, t, filename);
+			if (nft_table_parse(t,
+					    NFT_TABLE_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_TABLE,
+						   t, filename);
 			else
 				goto failparsing;

If you need to break lines like this, it usually means that this code needs to be split in smaller functions. Please, send me a clean up by encapsulating the tests in new smaller functions, eg. test_json_table().

#9 lore raw

PATCH 6/6 test: report compilation without support

In reply to Arturo Borrero Gonzalez

Print a message when there is no support for some parser.

Remove those EOPNOTSUPPs because they are unused.

---
 tests/nft-parsing-test.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/nft-parsing-test.c +2 -2
index e147e79..32c8837 100644
@@ -260,7 +260,7 @@ failparsing:
 	json_decref(root);
 	return -1;
 #else
-	errno = EOPNOTSUPP;
+	printf("Compiled without support for JSON.\n");
 	return -1;
 #endif
 }
@@ -361,7 +361,7 @@ failparsing:
 	       strerror(errno));
 	return -1;
 #else
-	errno = EOPNOTSUPP;
+	printf("Compiled without support for XML.\n");
 	return -1;
 #endif
 }
maintainer Applied
#10 lore raw

Re: [libnftables PATCH 6/6] test: report compilation without support

Quoted 3 lines · Arturo Borrero Gonzalez

Print a message when there is no support for some parser.

Remove those EOPNOTSUPPs because they are unused.

Applied, thanks.