From dc9733e097959f4e167244549f58cd3bef7af79b Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Thu, 27 Jun 2013 20:09:34 +0200 Subject: test: add testbench for XML This patch add a testbench for XML parsing, which may be extended to test JSON as well. To use it: $ cd test/ $ make nft-parsing-test $ ./nft-parsing-test xmlfiles/ This testbench supersedes old .sh test scripts, so they are deleted. [ I have mangled this patch to rename/mangle files, to colorize the test output and not to compile XML inconditionally --pablo ] Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- tests/nft-parsing-test.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/nft-parsing-test.c (limited to 'tests/nft-parsing-test.c') diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c new file mode 100644 index 0000000..55bb9ec --- /dev/null +++ b/tests/nft-parsing-test.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include + +#include /*nlmsghdr*/ +#include +#include +#include + +#ifdef XML_PARSING +#include +#endif + +static int test_xml(const char *filename) +{ +#ifdef XML_PARSING + int ret = -1; + struct nft_table *t = NULL; + struct nft_chain *c = NULL; + struct nft_rule *r = NULL; + FILE *fp; + mxml_node_t *tree = NULL;; + char *xml = NULL; + + fp = fopen(filename, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + if (tree == NULL) + return -1; + + xml = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + if (xml == NULL) + return -1; + + /* Check what parsing should be done */ + if (strcmp(tree->value.opaque, "table") == 0) { + t = nft_table_alloc(); + if (t != NULL) { + if (nft_table_parse(t, NFT_TABLE_PARSE_XML, xml) == 0) + ret = 0; + + nft_table_free(t); + } + } else if (strcmp(tree->value.opaque, "chain") == 0) { + c = nft_chain_alloc(); + if (c != NULL) { + if (nft_chain_parse(c, NFT_CHAIN_PARSE_XML, xml) == 0) + ret = 0; + + nft_chain_free(c); + } + } else if (strcmp(tree->value.opaque, "rule") == 0) { + r = nft_rule_alloc(); + if (r != NULL) { + if (nft_rule_parse(r, NFT_RULE_PARSE_XML, xml) == 0) + ret = 0; + + nft_rule_free(r); + } + } + + return ret; +#else + errno = EOPNOTSUPP; + return -1; +#endif +} + +int main(int argc, char *argv[]) +{ + DIR *d; + struct dirent *dent; + char path[PATH_MAX]; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + d = opendir(argv[1]); + if (d == NULL) { + perror("opendir"); + exit(EXIT_FAILURE); + } + + while ((dent = readdir(d)) != NULL) { + int len = strlen(dent->d_name); + + if (strcmp(dent->d_name, ".") == 0 || + strcmp(dent->d_name, "..") == 0) + continue; + + 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\033[37m (%s)\n", + strerror(errno)); + else + printf("\033[32mOK\033[37m \n"); + } + } + + closedir(d); + return 0; +} -- cgit v1.2.3