diff --git a/apps/restconf/restconf_methods.c b/apps/restconf/restconf_methods.c index 2ab68aee..46708588 100644 --- a/apps/restconf/restconf_methods.c +++ b/apps/restconf/restconf_methods.c @@ -311,7 +311,6 @@ api_data_get2(clicon_handle h, FCGX_FPrintF(r->out, "\r\n"); FCGX_FPrintF(r->out, "%s", cbx?cbuf_get(cbx):""); FCGX_FPrintF(r->out, "\r\n\r\n"); - clicon_debug(1, "%s Z", __FUNCTION__); ok: retval = 0; done: diff --git a/test/lib.sh b/test/lib.sh index 0c0d47dc..bb28e4ca 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -193,7 +193,8 @@ EOF if [ -z "$ret" -a -z "$expect" ]; then return fi - # -E for regexp (eg ^$). -Z for nul character, -x for implicit ^$ -q for quiet + # -G for basic regexp (eg ^$). -E for extended regular expression - differs in \ + # -Z for nul character, -x for implicit ^$ -q for quiet # -o only matching # Two variants: -EZo and -Fxq # match=`echo "$ret" | grep -FZo "$expect"` diff --git a/test/test_json.sh b/test/test_json.sh index b3f9e4d6..c20b3d6b 100755 --- a/test/test_json.sh +++ b/test/test_json.sh @@ -1,15 +1,22 @@ #!/bin/bash # Test: JSON parser tests +# Note that nmbers shouldnot be quoted. See test_restconf2.sh for typed #PROG="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_json" PROG=../util/clixon_util_json # include err() and new() functions and creates $dir . ./lib.sh -new "json parse" -expecteof "$PROG" 0 '{"foo": -23}' "^-23$" +new "json parse to xml" +expecteofx "$PROG" 0 '{"foo": -23}' "-23" -new "json parse list" -expecteof "$PROG" 0 '{"a":[0,1,2,3]}' "^0123$" +new "json parse to json" # should be {"foo": -23} +expecteofx "$PROG -j" 0 '{"foo": -23}' '{"foo": "-23"}' + +new "json parse list xml" +expecteofx "$PROG" 0 '{"a":[0,1,2,3]}' "0123" + +new "json parse list json" # should be {"a":[0,1,2,3]} +expecteofx "$PROG -j" 0 '{"a":[0,1,2,3]}' '{"a": "0"}{"a": "1"}{"a": "2"}{"a": "3"}' rm -rf $dir diff --git a/test/test_restconf2.sh b/test/test_restconf2.sh index 8010563f..31582531 100755 --- a/test/test_restconf2.sh +++ b/test/test_restconf2.sh @@ -44,6 +44,23 @@ module example{ type string; } } + container types{ + /* A couple of types to test quoting */ + leaf tint { + type int32; + } + leaf tdec64 { + type decimal64{ + fraction-digits 3; + } + } + leaf tbool { + type boolean; + } + leaf tstr { + type string; + } + } } EOF @@ -150,6 +167,13 @@ expectfn 'curl -s -X PUT -d {"interface":{"name":"TEST","type":"eth0"}} http://l new "restconf PUT change key error" expectfn 'curl -is -X PUT -d {"interface":{"name":"ALPHA","type":"eth0"}} http://localhost/restconf/data/example:cont1/interface=TEST' 0 '{"ietf-restconf:errors" : {"error": {"error-type": "protocol","error-tag": "operation-failed","error-severity": "error","error-message": "api-path keys do not match data keys"}}}' +#--------------- json type tests +new "restconf POST type x3" +expectfn 'curl -s -X POST -d {"example:types":{"tint":42,"tdec64":42.123,"tbool":false,"tstr":"str"}} http://localhost/restconf/data' 0 '' + +new "restconf POST type x3" +expectfn 'curl -s -X GET http://localhost/restconf/data/example:types' 0 '{"example:types": {"tint": 42,"tdec64": 42.123,"tbool": false,"tstr": "str"}}' + new "Kill restconf daemon" sudo pkill -u www-data -f "/www-data/clixon_restconf" diff --git a/util/clixon_util_json.c b/util/clixon_util_json.c index b60e2299..ac60ae20 100644 --- a/util/clixon_util_json.c +++ b/util/clixon_util_json.c @@ -70,7 +70,13 @@ static int usage(char *argv0) { - fprintf(stderr, "usage:%s.\n\tInput on stdin\n", argv0); + fprintf(stderr, "usage:%s [options]\n" + "where options are\n" + "\t-h \t\tHelp\n" + "\t-D \tDebug\n" + "\t-j \t\tOutput as JSON\n" + "\t-l \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n", + argv0); exit(0); } @@ -78,25 +84,52 @@ int main(int argc, char **argv) { + int retval = -1; cxobj *xt = NULL; cxobj *xc; cbuf *cb = cbuf_new(); + int c; + int logdst = CLICON_LOG_STDERR; + int json = 0; - if (argc != 1){ - usage(argv[0]); - return 0; - } - clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR); + optind = 1; + opterr = 0; + while ((c = getopt(argc, argv, "hD:jl:")) != -1) + switch (c) { + case 'h': + usage(argv[0]); + break; + case 'D': + if (sscanf(optarg, "%d", &debug) != 1) + usage(argv[0]); + break; + case 'j': + json++; + break; + case 'l': /* Log destination: s|e|o|f */ + if ((logdst = clicon_log_opt(optarg[0])) < 0) + usage(argv[0]); + break; + default: + usage(argv[0]); + break; + } + clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, logdst); if (json_parse_file(0, NULL, &xt) < 0) goto done; xc = NULL; while ((xc = xml_child_each(xt, xc, -1)) != NULL) - clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */ + if (json) + xml2json_cbuf(cb, xc, 0); /* print xml */ + else + clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */ fprintf(stdout, "%s", cbuf_get(cb)); + fflush(stdout); + retval = 0; done: if (xt) xml_free(xt); if (cb) cbuf_free(cb); - return 0; + return retval; } diff --git a/util/clixon_util_xml.c b/util/clixon_util_xml.c index c21091c4..1e7120f0 100644 --- a/util/clixon_util_xml.c +++ b/util/clixon_util_xml.c @@ -72,7 +72,7 @@ usage(char *argv0) "where options are\n" "\t-h \t\tHelp\n" "\t-D \tDebug\n" - "\t-j \tOutput as JSON\n" + "\t-j \t\tOutput as JSON\n" "\t-l \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n", argv0); exit(0); @@ -82,10 +82,10 @@ int main(int argc, char **argv) { + int retval = -1; cxobj *xt = NULL; cxobj *xc; cbuf *cb = cbuf_new(); - int retval = -1; int c; int logdst = CLICON_LOG_STDERR; int json = 0; @@ -112,7 +112,7 @@ main(int argc, usage(argv[0]); break; } - clicon_log_init("clixon_util_xml", debug?LOG_DEBUG:LOG_INFO, logdst); + clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, logdst); if (xml_parse_file(0, "", NULL, &xt) < 0){ fprintf(stderr, "xml parse error %s\n", clicon_err_reason); goto done;