* [libnftables PATCH 1/2] src: expr: use the function base2str in payload.
@ 2013-08-10 19:40 Alvaro Neira
2013-08-10 19:40 ` [libnftables PATCH 2/2] test: compare content parsing with original file content Alvaro Neira
0 siblings, 1 reply; 3+ messages in thread
From: Alvaro Neira @ 2013-08-10 19:40 UTC (permalink / raw)
To: netfilter-devel; +Cc: eric
From: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Use base2str to refactor code in the function snprintf in payload.
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
src/expr/payload.c | 80 ++++++++++++++++------------------------------------
1 file changed, 24 insertions(+), 56 deletions(-)
diff --git a/src/expr/payload.c b/src/expr/payload.c
index d00988a..340a0d8 100644
--- a/src/expr/payload.c
+++ b/src/expr/payload.c
@@ -149,31 +149,31 @@ nft_rule_expr_payload_parse(struct nft_rule_expr *e, struct nlattr *attr)
return 0;
}
+static char *base2str_array[NFT_PAYLOAD_TRANSPORT_HEADER+1] = {
+ [NFT_PAYLOAD_LL_HEADER] = "link",
+ [NFT_PAYLOAD_NETWORK_HEADER] = "network",
+ [NFT_PAYLOAD_TRANSPORT_HEADER] = "transport",
+};
+
+static const char *base2str(enum nft_payload_bases base)
+{
+ if (base > NFT_PAYLOAD_TRANSPORT_HEADER)
+ return "unknown";
+
+ return base2str_array[base];
+}
+
static int
nft_rule_expr_payload_snprintf_json(char *buf, size_t len, uint32_t flags,
struct nft_expr_payload *p)
{
int size = len, offset = 0, ret;
- ret = snprintf(buf, len, "\"dreg\" : %u, \"offset\" : %u, \"len\" : %u, ",
- p->dreg, p->offset, p->len);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
- switch (p->base) {
- case NFT_PAYLOAD_LL_HEADER:
- ret = snprintf(buf+offset, len, "\"base\" : \"link\"");
- break;
- case NFT_PAYLOAD_NETWORK_HEADER:
- ret = snprintf(buf+offset, len, "\"base\" : \"network\"");
- break;
- case NFT_PAYLOAD_TRANSPORT_HEADER:
- ret = snprintf(buf+offset, len, "\"base\" : \"transport\"");
- break;
- default:
- ret = snprintf(buf+offset, len, "\"base\" : \"unknown\"");
- break;
- }
-
+ ret = snprintf(buf, len, "\"dreg\" : %u, "
+ "\"offset\" : %u, "
+ "\"len\" : %u, "
+ "\"base\" : \"%s\"",
+ p->dreg, p->offset, p->len, base2str(p->base));
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
return offset;
@@ -243,48 +243,16 @@ nft_rule_expr_payload_snprintf_xml(char *buf, size_t len, uint32_t flags,
{
int size = len, offset = 0, ret;
- ret = snprintf(buf, len, "<dreg>%u</dreg><offset>%u</offset>"
- "<len>%u</len>", p->dreg, p->offset, p->len);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
- /* A default option is not provided.
- * The <base> node will be missing; Is not mandatory.
- */
-
- switch (p->base) {
- case NFT_PAYLOAD_LL_HEADER:
- ret = snprintf(buf+offset, len, "<base>link</base>");
- break;
- case NFT_PAYLOAD_NETWORK_HEADER:
- ret = snprintf(buf+offset, len, "<base>network</base>");
- break;
- case NFT_PAYLOAD_TRANSPORT_HEADER:
- ret = snprintf(buf+offset, len, "<base>transport</base>");
- break;
- default:
- ret = snprintf(buf+offset, len, "<base>unknown</base>");
- break;
- }
-
+ ret = snprintf(buf, len, "<dreg>%u</dreg>"
+ "<offset>%u</offset>"
+ "<len>%u</len>"
+ "<base>%s</base>",
+ p->dreg, p->offset, p->len, base2str(p->base));
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
return offset;
}
-static char *base2str_array[NFT_PAYLOAD_TRANSPORT_HEADER+1] = {
- [NFT_PAYLOAD_LL_HEADER] = "link",
- [NFT_PAYLOAD_NETWORK_HEADER] = "network",
- [NFT_PAYLOAD_TRANSPORT_HEADER] = "transport",
-};
-
-static const char *base2str(enum nft_payload_bases base)
-{
- if (base > NFT_PAYLOAD_TRANSPORT_HEADER)
- return "unknown";
-
- return base2str_array[base];
-}
-
static int
nft_rule_expr_payload_snprintf(char *buf, size_t len, uint32_t type,
uint32_t flags, struct nft_rule_expr *e)
--
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 related [flat|nested] 3+ messages in thread
* [libnftables PATCH 2/2] test: compare content parsing with original file content.
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
2013-08-11 7:47 ` Pablo Neira Ayuso
0 siblings, 1 reply; 3+ messages in thread
From: Alvaro Neira @ 2013-08-10 19:40 UTC (permalink / raw)
To: netfilter-devel; +Cc: eric
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [libnftables PATCH 2/2] test: compare content parsing with original file content.
2013-08-10 19:40 ` [libnftables PATCH 2/2] test: compare content parsing with original file content Alvaro Neira
@ 2013-08-11 7:47 ` Pablo Neira Ayuso
0 siblings, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2013-08-11 7:47 UTC (permalink / raw)
To: Alvaro Neira; +Cc: netfilter-devel, eric
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-08-11 7:47 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-10 19:40 [libnftables PATCH 1/2] src: expr: use the function base2str in payload Alvaro Neira
2013-08-10 19:40 ` [libnftables PATCH 2/2] test: compare content parsing with original file content Alvaro Neira
2013-08-11 7:47 ` Pablo Neira Ayuso
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).