Fixed: [JSON backslash string decoding/encoding not correct](https://github.com/clicon/clixon/issues/453)

Added unicode BMP support for JSON strings
Test: encoding/decoding tests for UTF-8
This commit is contained in:
Olof hagsand 2023-09-19 12:28:58 +02:00
parent 1a43a32770
commit 45f41e3e4d
12 changed files with 254 additions and 35 deletions

View file

@ -2,6 +2,7 @@
# Test: JSON parser tests. See RFC7951
# - Multi-line + pretty-print
# - Empty values
# - JSON string encode/decode
# 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)
@ -138,6 +139,41 @@ JSON='{"json:c":{"s":"<![CDATA[ z > x & x < y ]]>"}}'
new "json parse cdata xml"
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
JSON='{"text":"ruled over the shores of the Hreiðsea"}'
new "json utf-8"
expecteofx "$clixon_util_json -j" 0 "$JSON" "$JSON"
new "json utf-8 xml out"
expecteofx "$clixon_util_json" 0 "$JSON" "<text>ruled over the shores of the Hreiðsea</text>"
# see https://github.com/clicon/clixon/issues/453
JSON="{\"text\":\"one
two\"}"
new "json not escaped \n expect fail"
expecteofx "$clixon_util_json -j" 255 "$JSON" 2> /dev/null
new "json not escaped \" expect fail"
expecteofx "$clixon_util_json -j" 255 "quote:\"" 2> /dev/null
JSON='{"text":"cr:\nquote:\"tab:\tend"}'
new "json escaping \n\"\t to json"
expecteofx "$clixon_util_json -j" 0 "$JSON" "$JSON"
new "json escaping \n\"\t to xml"
expecteofx "$clixon_util_json" 0 "$JSON" "quote:\"tab: end</text>"
JSON='{"text":"bmp:\u005E"}'
new "json escaping \u BMP circumflex"
#expecteofx "$clixon_util_json -j -D $DBG" 0 "$JSON" '{"text":"bmp:^"}'
JSON='{"text":"bmp:\u00F0"}'
new "json escaping \u BMP latin eth"
expecteofx "$clixon_util_json -j -D $DBG" 0 "$JSON" '{"text":"bmp:ð"}'
JSON='{"text":"bmp:\uFAIL"}'
new "json escaping unicode BMP fail"
expecteofx "$clixon_util_json -j -D $DBG" 255 "$JSON" 2> /dev/null
rm -rf $dir
new "endtest"

View file

@ -294,6 +294,7 @@ function testrun()
new "start restconf daemon"
# inline of start_restconf, cant make quotes to work
echo "sudo -u $wwwstartuser -s $clixon_restconf $RCLOG -D $DBG -f $cfg -R $RESTCONFIG1"
STTYSETTINGS=$(stty -g) # reset in wait_restconf
sudo -u $wwwstartuser -s $clixon_restconf $RCLOG -D $DBG -f $cfg -R "$RESTCONFIG1" </dev/null &>/dev/null &
if [ $? -ne 0 ]; then
err1 "expected 0" "$?"
@ -308,14 +309,13 @@ function testrun()
else
HVER=2
fi
new "wait restconf"
wait_restconf
new "restconf root discovery. RFC 8040 3.1 (xml+xrd)"
echo "curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta"
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta)" 0 "HTTP/$HVER 200" "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
echo "fcgi or native+http/1 or native+http/1+http/2"
new "restconf GET http/1.1"
expectpart "$(curl $CURLOPTS --http1.1 -X GET $proto://$addr/.well-known/host-meta)" 0 'HTTP/1.1 200 OK' "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
@ -334,7 +334,7 @@ function testrun()
new "restconf GET https/2 prior-knowledge"
expectpart "$(curl $CURLOPTS --http2-prior-knowledge -X GET $proto://$addr/.well-known/host-meta)" 0 "HTTP/$HVER 200" "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
fi
# Wrong protocol http when https or vice versa
if [ $proto = http ]; then # see (2) https to http port in restconf_main_native.c
new "Wrong proto=https on http port, expect err 35 wrong version number"
@ -354,10 +354,8 @@ function testrun()
wait_restconf
new "restconf root discovery. RFC 8040 3.1 (xml+xrd)"
echo "curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta"
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta)" 0 "HTTP/$HVER 200" "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
echo "native + http/2 only"
# Important here is robustness of restconf daemon, not a meaningful reply
if [ $proto = http ]; then # see (2) https to http port in restconf_main_native.c
# http protocol mismatch can just close the socket if assumed its http/2
@ -405,10 +403,8 @@ function testrun()
wait_restconf
new "restconf root discovery. RFC 8040 3.1 (xml+xrd)"
echo "curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta"
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/.well-known/host-meta)" 0 "HTTP/$HVER 200" "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
echo "fcgi or native+http/1 or native+http/1+http/2"
if [ "${WITH_RESTCONF}" = "native" ]; then # XXX does not work with nginx
new "restconf GET http/1.0 - returns 1.0"
expectpart "$(curl $CURLOPTS --http1.0 -X GET $proto://$addr/.well-known/host-meta)" 0 'HTTP/1.0 200 OK' "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" "<Link rel='restconf' href='/restconf'/>" "</XRD>"
@ -439,7 +435,6 @@ function testrun()
expectpart "$(curl $CURLOPTS -X GET http://$addr:443/.well-known/host-meta)" 0 "HTTP/" "400"
fi
fi # HTTP/2
# Exact match
new "restconf get restconf resource. RFC 8040 3.3 (json)"
expectpart "$(curl $CURLOPTS -X GET -H "Accept: application/yang-data+json" $proto://$addr/restconf)" 0 "HTTP/$HVER 200" '{"ietf-restconf:restconf":{"data":{},"operations":{},"yang-library-version":"2019-01-04"}}'
@ -653,6 +648,24 @@ function testrun()
new "restconf Add subtree with too many keys (expected error)"
expectpart "$(curl $CURLOPTS -X PUT -H "Content-Type: application/yang-data+json" -d '{"ietf-interfaces:interface":{"name":"eth/0/0","type":"clixon-example:eth","enabled":true}}' $proto://$addr/restconf/data/ietf-interfaces:interfaces/interface=a,b)" 0 "HTTP/$HVER 400" '{"ietf-restconf:errors":{"error":{"error-type":"rpc","error-tag":"malformed-message","error-severity":"error","error-message":"List key interface length mismatch"}}}'
cat <<EOF > $dir/input.json
{"clixon-example:parameter":{"name":"x","value":"foo
bar"}}
EOF
new "restconf JSON escape encoding with explicit \n expect fail"
expectpart "$(curl $CURLOPTS -X POST -H "Accept: application/yang-data+json" -H "Content-Type: application/yang-data+json" --data-binary @$dir/input.json $proto://$addr/restconf/data/clixon-example:table)" 0 "HTTP/$HVER 400" "malformed-message"
JSON='{"clixon-example:parameter":[{"name":"x","value":"foo\nbar"}]}'
new "restconf JSON escape encoding"
expectpart "$(curl $CURLOPTS -X POST -H "Accept: application/yang-data+json" -H "Content-Type: application/yang-data+json" --data-binary "$JSON" $proto://$addr/restconf/data/clixon-example:table)" 0 "HTTP/$HVER 201" "Location: $proto://$addr/restconf/data/clixon-example:table/parameter=x"
new "Check restconf JSON escape encoding"
expectpart "$(curl $CURLOPTS -X GET -H "Accept: application/yang-data+json" $proto://$addr/restconf/data/clixon-example:table/parameter=x)" 0 "HTTP/$HVER 200" '{"clixon-example:parameter":\[{"name":"x","value":"foo\\nbar"}\]}'
new "Check restconf JSON escape encoding in XML"
expectpart "$(curl $CURLOPTS -X GET -H "Accept: application/yang-data+xml" $proto://$addr/restconf/data/clixon-example:table/parameter=x)" 0 "HTTP/$HVER 200" "<parameter xmlns=\"urn:example:clixon\"><name>x</name><value>foo" "bar</value></parameter>"
if [ $RC -ne 0 ]; then
new "Kill restconf daemon"
stop_restconf

View file

@ -212,6 +212,19 @@ EOF
)
expecteof "$clixon_util_xml -o" 0 "$XML" '^<bk:book xmlns:bk="urn:loc.gov:books" xmlns:isbn="urn:ISBN:0-395-36341-6"><bk:title>Cheaper by the Dozen</bk:title><isbn:number>1568491379</isbn:number></bk:book>$'
XML=$(cat <<EOF
<?xml version="1.0" encoding='utf-8'?>
<text>
Theodoric the bold
chief of sea-warriors
ruled over the shores of the Hreiðsea
</text>
EOF
)
new "utf-8 string"
expecteof "$clixon_util_xml -o" 0 "$XML" "^ruled over the shores of the Hreiðsea$"
rm -rf $dir
new "endtest"