From c4ce40d95dc27d10b6bb6f4a23d26cfd359387ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Thu, 25 Jul 2013 22:52:24 +0200 Subject: tests: table: test json parsing support Test the functions for parsing tables in JSON Support Signed-off-by: Alvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- tests/nft-parsing-test.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests/nft-parsing-test.c') diff --git a/tests/nft-parsing-test.c b/tests/nft-parsing-test.c index 4fe60c3..83a627c 100644 --- a/tests/nft-parsing-test.c +++ b/tests/nft-parsing-test.c @@ -14,6 +14,47 @@ #include #endif +#ifdef JSON_PARSING +#include +#endif + +static int test_json(const char *filename) +{ +#ifdef JSON_PARSING + int ret = -1; + struct nft_table *t = NULL; + json_t *root; + json_error_t error; + char *json = NULL; + + root = json_load_file(filename, 0, &error); + if (!root) { + printf("Error on the line %d : %s", error.line, error.text); + return -1; + } + + if (root == NULL) + 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_TABLE_PARSE_JSON, json) == 0) + ret = 0; + + nft_table_free(t); + } + } + + return ret; +#else + errno = EOPNOTSUPP; + return -1; +#endif +} + static int test_xml(const char *filename) { #ifdef XML_PARSING @@ -104,6 +145,14 @@ int main(int argc, char *argv[]) else 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 + printf("\033[32mOK\e[0m\n"); + } } closedir(d); -- cgit v1.2.3