From: Alvaro Neira <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: eric@regit.org
Subject: [libnftables PATCH 2/2] test: compare content parsing with original file content.
Date: Sat, 10 Aug 2013 21:40:51 +0200 [thread overview]
Message-ID: <20130810194051.18629.33864.stgit@Ph0enix> (raw)
In-Reply-To: <20130810194039.18629.89549.stgit@Ph0enix>
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(-)
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");
+ }
}
}
--
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
next prev parent reply other threads:[~2013-08-10 19:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-10 19:40 [libnftables PATCH 1/2] src: expr: use the function base2str in payload Alvaro Neira
2013-08-10 19:40 ` Alvaro Neira [this message]
2013-08-11 7:47 ` [libnftables PATCH 2/2] test: compare content parsing with original file content Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130810194051.18629.33864.stgit@Ph0enix \
--to=alvaroneay@gmail.com \
--cc=eric@regit.org \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).