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, "%s", cbx?cbuf_get(cbx):"");
FCGX_FPrintF(r->out, "\r\n\r\n");
clicon_debug(1, "%s Z", __FUNCTION__);
ok:
retval = 0;
done:

View file

@ -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"`

View file

@ -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}' "^<foo>-23</foo>$"
new "json parse to xml"
expecteofx "$PROG" 0 '{"foo": -23}' "<foo>-23</foo>"
new "json parse list"
expecteof "$PROG" 0 '{"a":[0,1,2,3]}' "^<a>0</a><a>1</a><a>2</a><a>3</a>$"
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]}' "<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

View file

@ -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"

View file

@ -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 <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);
}
@ -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){
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, "hD:jl:")) != -1)
switch (c) {
case 'h':
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)
goto done;
xc = 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 */
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;
}

View file

@ -72,7 +72,7 @@ usage(char *argv0)
"where options are\n"
"\t-h \t\tHelp\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",
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, "</config>", NULL, &xt) < 0){
fprintf(stderr, "xml parse error %s\n", clicon_err_reason);
goto done;