diff options
author | Álvaro Neira Ayuso <alvaroneay@gmail.com> | 2014-01-06 00:51:14 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2014-01-06 13:20:46 +0100 |
commit | e87d2f9ef8a4a298de5514b30ec2d43d3c90a644 (patch) | |
tree | b1379f466db57d1a8bf8c2e6048f8a3933ef9639 /tests | |
parent | b4a0d19dc1b16a614cdf0aa362f754e734486b38 (diff) |
src: new error reporting approach for XML/JSON parsers
I have added a new structure for reporting some errors in parser
that we can't cover with errno.
In this patch, we have three errors that we can't cover with errno:
NFT_PARSE_EBADINPUT : Bad XML/JSON format in the input
NFT_PARSE_EMISSINGNODE : Missing node in our input
NFT_PARSE_EBADTYPE : Wrong type value in a node
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/nft-parsing-test.c | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c index 6a5ab4d..558c849 100644 --- a/tests/nft-parsing-test.c +++ b/tests/nft-parsing-test.c @@ -167,7 +167,7 @@ static int compare_test(uint32_t type, void *input, const char *filename) } #endif -static int test_json(const char *filename) +static int test_json(const char *filename, struct nft_parse_err *err) { #ifdef JSON_PARSING int ret = -1; @@ -181,17 +181,15 @@ static int test_json(const char *filename) char *json; root = json_load_file(filename, 0, &error); - if (!root) { - printf("Error on the line %d : %s", error.line, error.text); + if (!root) return -1; - } json = json_dumps(root, JSON_INDENT(0)); if (json_object_get(root, "table") != NULL) { t = nft_table_alloc(); if (t != NULL) { - if (nft_table_parse(t, NFT_PARSE_JSON, json) == 0) + if (nft_table_parse(t, NFT_PARSE_JSON, json, err) == 0) ret = compare_test(TEST_JSON_TABLE, t, filename); else goto failparsing; @@ -201,7 +199,7 @@ 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_PARSE_JSON, json) == 0) + if (nft_chain_parse(c, NFT_PARSE_JSON, json, err) == 0) ret = compare_test(TEST_JSON_CHAIN, c, filename); else goto failparsing; @@ -211,7 +209,7 @@ 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_PARSE_JSON, json) == 0) + if (nft_rule_parse(r, NFT_PARSE_JSON, json, err) == 0) ret = compare_test(TEST_JSON_RULE, r, filename); else goto failparsing; @@ -221,7 +219,7 @@ static int test_json(const char *filename) } else if (json_object_get(root, "set") != NULL) { s = nft_set_alloc(); if (s != NULL) { - if (nft_set_parse(s, NFT_PARSE_JSON, json) == 0) + if (nft_set_parse(s, NFT_PARSE_JSON, json, err) == 0) ret = compare_test(TEST_JSON_SET, s, filename); else goto failparsing; @@ -231,7 +229,7 @@ static int test_json(const char *filename) } else if (json_object_get(root, "nftables") != NULL) { rs = nft_ruleset_alloc(); if (rs != NULL) { - if (nft_ruleset_parse(rs, NFT_PARSE_JSON, json) == 0) + if (nft_ruleset_parse(rs, NFT_PARSE_JSON, json, err) == 0) ret = compare_test(TEST_JSON_RULESET, rs, filename); else goto failparsing; @@ -256,7 +254,7 @@ failparsing: #endif } -static int test_xml(const char *filename) +static int test_xml(const char *filename, struct nft_parse_err *err) { #ifdef XML_PARSING int ret = -1; @@ -290,7 +288,7 @@ static int test_xml(const char *filename) if (strcmp(tree->value.opaque, "table") == 0) { t = nft_table_alloc(); if (t != NULL) { - if (nft_table_parse(t, NFT_PARSE_XML, xml) == 0) + if (nft_table_parse(t, NFT_PARSE_XML, xml, err) == 0) ret = compare_test(TEST_XML_TABLE, t, filename); else goto failparsing; @@ -300,7 +298,7 @@ static int test_xml(const char *filename) } else if (strcmp(tree->value.opaque, "chain") == 0) { c = nft_chain_alloc(); if (c != NULL) { - if (nft_chain_parse(c, NFT_PARSE_XML, xml) == 0) + if (nft_chain_parse(c, NFT_PARSE_XML, xml, err) == 0) ret = compare_test(TEST_XML_CHAIN, c, filename); else goto failparsing; @@ -310,7 +308,7 @@ static int test_xml(const char *filename) } else if (strcmp(tree->value.opaque, "rule") == 0) { r = nft_rule_alloc(); if (r != NULL) { - if (nft_rule_parse(r, NFT_PARSE_XML, xml) == 0) + if (nft_rule_parse(r, NFT_PARSE_XML, xml, err) == 0) ret = compare_test(TEST_XML_RULE, r, filename); else goto failparsing; @@ -320,7 +318,7 @@ static int test_xml(const char *filename) } else if (strcmp(tree->value.opaque, "set") == 0) { s = nft_set_alloc(); if (s != NULL) { - if (nft_set_parse(s, NFT_PARSE_XML, xml) == 0) + if (nft_set_parse(s, NFT_PARSE_XML, xml, err) == 0) ret = compare_test(TEST_XML_SET, s, filename); else goto failparsing; @@ -331,7 +329,7 @@ static int test_xml(const char *filename) rs = nft_ruleset_alloc(); if (rs != NULL) { if (nft_ruleset_parse(rs, NFT_PARSE_XML, - xml) == 0) + xml, err) == 0) ret = compare_test(TEST_XML_RULESET, rs, filename); else @@ -361,6 +359,7 @@ int main(int argc, char *argv[]) struct dirent *dent; char path[PATH_MAX]; int ret = 0, exit_code = 0; + struct nft_parse_err *err; if (argc != 2) { fprintf(stderr, "Usage: %s <directory>\n", argv[0]); @@ -373,6 +372,12 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + err = nft_parse_err_alloc(); + if (err == NULL) { + perror("error"); + exit(EXIT_FAILURE); + } + while ((dent = readdir(d)) != NULL) { int len = strlen(dent->d_name); @@ -383,14 +388,14 @@ 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 ((ret = test_xml(path)) == 0) { + if ((ret = test_xml(path, err)) == 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 ((ret = test_json(path)) == 0) { + if ((ret = test_json(path, err)) == 0) { printf("parsing and validating %s: ", path); printf("\033[32mOK\e[0m\n"); } @@ -399,6 +404,7 @@ int main(int argc, char *argv[]) } closedir(d); + nft_parse_err_free(err); if (exit_code != 0) exit(EXIT_FAILURE); |