clixon/test/test_json.sh
Olof hagsand 19e21be0bc Very large commit for upcoming 4.4 release
Major New features

* New and updated search functions using xpath, api-path and instance-id
  * New search functions using api-path and instance_id:
    * C search functions: `clixon_find_instance_id()` and `clixon_find_api_path()`
  * Binary search optimization in lists for indexed leafs in all three formats.
    * This improves search performance to O(logN) which is drastical improvements for large lists.
  * You can also register explicit indexes for making binary search (not only list keys)
  * For more info, see docs at [paths](https://clixon-docs.readthedocs.io/en/latest/paths.html) and
[search](https://clixon-docs.readthedocs.io/en/latest/xml.html#searching-in-xml)

API changes on existing features (you may need to change your code)
* On failed validation of leafrefs, error message changed from: `No such leaf` to `No leaf <name> matching path <path>`.
* CLI Error message (clicon_rpc_generate_error()) changed when backend returns netconf error to be more descriptive:
  * Original: `Config error: Validate failed. Edit and try again or discard changes: Invalid argument`
  * New (example): `Netconf error: application operation-failed Identityref validation failed, undefined not derived from acl-base . Validate failed. Edit and try again or discard changes"

Minor changes

* Test framework
  * Added `-- -S <file>` command-line to main example to be able to return any state to main example.
  * Added `test/cicd` test scripts for running on a set of other hosts
* C-code restructuring
  * clixon_yang.c partitioned and moved code into clixon_yang_parse_lib.c and clixon_yang_module.c and move back some code from clixon_yang_type.c.
    * partly to reduce size, but most important to limit code that accesses internal yang structures, only clixon_yang.c does this now.
2020-02-02 15:52:30 +01:00

135 lines
3.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Test: JSON parser tests. See RFC7951
# - Multi-line + pretty-print
# - Empty values
# Note that members should not be quoted. See test_restconf2.sh for typed
#PROG="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_json"
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
: ${clixon_util_json:=clixon_util_json}
: ${clixon_util_xml:=clixon_util_xml}
fyang=$dir/json.yang
cat <<EOF > $fyang
module json{
prefix ex;
namespace "urn:example:clixon";
identity genre {
description
"From RFC8040 jukebox example.
Identity prefixes are translated from module-name to xml prefix";
}
identity blues {
base genre;
}
typedef gtype{
type identityref{
base genre;
}
}
leaf a{
type int32;
}
container c{
leaf a{
type int32;
}
leaf s{
type string;
}
}
leaf g1 {
description "direct type";
type identityref { base genre; }
}
leaf g2 {
description "indirect type";
type gtype;
}
}
EOF
new "test params: -y $fyang"
# No yang
new "json parse to xml"
expecteofx "$clixon_util_json" 0 '{"foo": -23}' "<foo>-23</foo>"
new "json parse to json" # should be {"foo": -23}
expecteofx "$clixon_util_json -j" 0 '{"foo": -23}' '{"foo":"-23"}'
new "json parse list xml"
expecteofx "$clixon_util_json" 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 "$clixon_util_json -j" 0 '{"a":[0,1,2,3]}' '{"a":"0"}{"a":"1"}{"a":"2"}{"a":"3"}'
# Multi-line JOSN not pretty-print
JSON='{"json:c":{"a":42,"s":"string"}}'
# Same with pretty-print
JSONP='{
"json:c": {
"a": 42,
"s": "string"
}
}'
new "json no pp in/out"
expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" "$JSON"
new "json pp in/out"
expecteofeq "$clixon_util_json -jpy $fyang" 0 "$JSONP" "$JSONP"
new "json pp in/ no pp out"
expecteofeq "$clixon_util_json -jy $fyang" 0 "$JSONP" "$JSON"
new "json no pp in/ pp out"
expecteofeq "$clixon_util_json -jpy $fyang" 0 "$JSON" "$JSONP"
JSON='{"json:a":-23}'
new "json leaf back to json"
expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" "$JSON"
JSON='{"json:c":{"a":937}}'
new "json parse container back to json"
expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" "$JSON"
# identities translation json -> xml is tricky wrt prefixes, json uses module
# name, xml uses xml namespace prefixes (or default)
JSON='{"json:g1":"json:blues"}'
new "json identity to xml"
expecteofx "$clixon_util_json -y $fyang" 0 "$JSON" '<g1 xmlns="urn:example:clixon">blues</g1>'
new "json identity back to json"
expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" '{"json:g1":"blues"}'
new "xml identity with explicit ns to json"
expecteofx "$clixon_util_xml -ovjy $fyang" 0 '<g1 xmlns="urn:example:clixon" xmlns:ex="urn:example:clixon">ex:blues</g1>' '{"json:g1":"blues"}'
# Same with indirect type
JSON='{"json:g2":"json:blues"}'
new "json indirect identity to xml"
expecteofx "$clixon_util_json -y $fyang" 0 "$JSON" '<g2 xmlns="urn:example:clixon">blues</g2>'
new "json indirect identity back to json"
expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" '{"json:g2":"blues"}'
new "xml indirect identity with explicit ns to json"
expecteofx "$clixon_util_xml -ojvy $fyang" 0 '<g2 xmlns="urn:example:clixon" xmlns:ex="urn:example:clixon">ex:blues</g2>' '{"json:g2":"blues"}'
# XXX CDATA translation, should work bit does not
if false; then
JSON='{"json:c": {"s": "<![CDATA[ z > x & x < y ]]>"}}'
new "json parse cdata xml"
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
fi
rm -rf $dir
# unset conditional parameters
unset clixon_util_json
unset clixon_util_xml