]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
tests: table: test json parsing support
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>
Thu, 25 Jul 2013 20:52:24 +0000 (22:52 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 25 Jul 2013 21:03:20 +0000 (23:03 +0200)
Test the functions for parsing tables in JSON Support

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tests/Makefile.am
tests/jsonfiles/01-table.json [new file with mode: 0644]
tests/jsonfiles/02-table.json [new file with mode: 0644]
tests/nft-parsing-test.c

index 6941c3c27ad36adf9e72f89618da163ca0dafb39..cfa4e8e5951f010031d42187830cdd5989ae107a 100644 (file)
@@ -3,4 +3,4 @@ include $(top_srcdir)/Make_global.am
 check_PROGRAMS = nft-parsing-test
 
 nft_parsing_test_SOURCES = nft-parsing-test.c
-nft_parsing_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} ${LIBXML_LIBS}
+nft_parsing_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} ${LIBXML_LIBS} ${LIBJSON_LIBS}
diff --git a/tests/jsonfiles/01-table.json b/tests/jsonfiles/01-table.json
new file mode 100644 (file)
index 0000000..ec496b9
--- /dev/null
@@ -0,0 +1 @@
+{"table" : {"name" : "filter","version" : 0,"properties" : {"family" : "ip","table_flags" : 0}}}
diff --git a/tests/jsonfiles/02-table.json b/tests/jsonfiles/02-table.json
new file mode 100644 (file)
index 0000000..03f4d5a
--- /dev/null
@@ -0,0 +1 @@
+{"table" : {"name" : "filter2","version" : 0,"properties" : {"family" : "ip6","table_flags" : 0}}}
index 4fe60c3cbdb5ec366461a66e222bb32e78500dd4..83a627c874deb388a8ef3fbe57c757e3b79fbf76 100644 (file)
 #include <mxml.h>
 #endif
 
+#ifdef JSON_PARSING
+#include <jansson.h>
+#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);