JSON updates

This commit is contained in:
Olof hagsand 2019-01-20 19:13:40 +01:00
parent 19f26e6838
commit 34d7f60ca4
6 changed files with 81 additions and 17 deletions

View file

@ -311,7 +311,6 @@ api_data_get2(clicon_handle h,
FCGX_FPrintF(r->out, "\r\n"); FCGX_FPrintF(r->out, "\r\n");
FCGX_FPrintF(r->out, "%s", cbx?cbuf_get(cbx):""); FCGX_FPrintF(r->out, "%s", cbx?cbuf_get(cbx):"");
FCGX_FPrintF(r->out, "\r\n\r\n"); FCGX_FPrintF(r->out, "\r\n\r\n");
clicon_debug(1, "%s Z", __FUNCTION__);
ok: ok:
retval = 0; retval = 0;
done: done:

View file

@ -193,7 +193,8 @@ EOF
if [ -z "$ret" -a -z "$expect" ]; then if [ -z "$ret" -a -z "$expect" ]; then
return return
fi 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 # -o only matching
# Two variants: -EZo and -Fxq # Two variants: -EZo and -Fxq
# match=`echo "$ret" | grep -FZo "$expect"` # match=`echo "$ret" | grep -FZo "$expect"`

View file

@ -1,15 +1,22 @@
#!/bin/bash #!/bin/bash
# Test: JSON parser tests # 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="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_json"
PROG=../util/clixon_util_json PROG=../util/clixon_util_json
# include err() and new() functions and creates $dir # include err() and new() functions and creates $dir
. ./lib.sh . ./lib.sh
new "json parse" new "json parse to xml"
expecteof "$PROG" 0 '{"foo": -23}' "^<foo>-23</foo>$" expecteofx "$PROG" 0 '{"foo": -23}' "<foo>-23</foo>"
new "json parse list" new "json parse to json" # should be {"foo": -23}
expecteof "$PROG" 0 '{"a":[0,1,2,3]}' "^<a>0</a><a>1</a><a>2</a><a>3</a>$" expecteofx "$PROG -j" 0 '{"foo": -23}' '{"foo": "-23"}'
new "json parse list xml"
expecteofx "$PROG" 0 '{"a":[0,1,2,3]}' "<a>0</a><a>1</a><a>2</a><a>3</a>"
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 rm -rf $dir

View file

@ -44,6 +44,23 @@ module example{
type string; 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 EOF
@ -150,6 +167,13 @@ expectfn 'curl -s -X PUT -d {"interface":{"name":"TEST","type":"eth0"}} http://l
new "restconf PUT change key error" 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"}}}' 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" new "Kill restconf daemon"
sudo pkill -u www-data -f "/www-data/clixon_restconf" sudo pkill -u www-data -f "/www-data/clixon_restconf"

View file

@ -70,7 +70,13 @@
static int static int
usage(char *argv0) 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 <level> \tDebug\n"
"\t-j \t\tOutput as JSON\n"
"\t-l <s|e|o> \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n",
argv0);
exit(0); exit(0);
} }
@ -78,25 +84,52 @@ int
main(int argc, main(int argc,
char **argv) char **argv)
{ {
int retval = -1;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xc; cxobj *xc;
cbuf *cb = cbuf_new(); cbuf *cb = cbuf_new();
int c;
int logdst = CLICON_LOG_STDERR;
int json = 0;
if (argc != 1){ optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, "hD:jl:")) != -1)
switch (c) {
case 'h':
usage(argv[0]); usage(argv[0]);
return 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__, LOG_INFO, CLICON_LOG_STDERR); clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, logdst);
if (json_parse_file(0, NULL, &xt) < 0) if (json_parse_file(0, NULL, &xt) < 0)
goto done; goto done;
xc = NULL; xc = NULL;
while ((xc = xml_child_each(xt, xc, -1)) != NULL) while ((xc = xml_child_each(xt, xc, -1)) != NULL)
if (json)
xml2json_cbuf(cb, xc, 0); /* print xml */
else
clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */ clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */
fprintf(stdout, "%s", cbuf_get(cb)); fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);
retval = 0;
done: done:
if (xt) if (xt)
xml_free(xt); xml_free(xt);
if (cb) if (cb)
cbuf_free(cb); cbuf_free(cb);
return 0; return retval;
} }

View file

@ -72,7 +72,7 @@ usage(char *argv0)
"where options are\n" "where options are\n"
"\t-h \t\tHelp\n" "\t-h \t\tHelp\n"
"\t-D <level> \tDebug\n" "\t-D <level> \tDebug\n"
"\t-j \tOutput as JSON\n" "\t-j \t\tOutput as JSON\n"
"\t-l <s|e|o> \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n", "\t-l <s|e|o> \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n",
argv0); argv0);
exit(0); exit(0);
@ -82,10 +82,10 @@ int
main(int argc, main(int argc,
char **argv) char **argv)
{ {
int retval = -1;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xc; cxobj *xc;
cbuf *cb = cbuf_new(); cbuf *cb = cbuf_new();
int retval = -1;
int c; int c;
int logdst = CLICON_LOG_STDERR; int logdst = CLICON_LOG_STDERR;
int json = 0; int json = 0;
@ -112,7 +112,7 @@ main(int argc,
usage(argv[0]); usage(argv[0]);
break; 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, "</config>", NULL, &xt) < 0){ if (xml_parse_file(0, "</config>", NULL, &xt) < 0){
fprintf(stderr, "xml parse error %s\n", clicon_err_reason); fprintf(stderr, "xml parse error %s\n", clicon_err_reason);
goto done; goto done;