#!/usr/bin/env bash # Test: XML parser tests and JSON translation # @see https://www.w3.org/TR/2008/REC-xml-20081126 # https://www.w3.org/TR/2009/REC-xml-names-20091208 # Note CDATA to JSON: in earlier versions, CDATA was stripped when converting to JSON # but this has been changed so that the CDATA is a part of the payload, eg shows up also in # JSON strings # 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_xml:="clixon_util_xml"} : ${clixon_util_json:="clixon_util_json"} new "xml parse" expecteof "$clixon_util_xml -o" 0 "" "^$" # Note dont know what b is. new "xml parse to json" expecteof "$clixon_util_xml -oj" 0 "" '{"a":{"b":{}}}' new "xml parse strange names" expecteof "$clixon_util_xml -o" 0 "<_->" "<_->" new "xml parse name errors" expecteof "$clixon_util_xml -o" 255 "<-a/>" "" new "xml parse name errors" expecteof "$clixon_util_xml -o" 255 "<9/>" "" new "xml parse name errors" expecteof "$clixon_util_xml -o" 255 "" "" LF=' ' new "xml parse content with CR LF -> LF, CR->LF (see https://www.w3.org/TR/REC-xml/#sec-line-ends)" ret=$(echo "a b${LF}c ${LF}d" | $clixon_util_xml -o) if [ "$ret" != "a${LF}b${LF}c${LF}d" ]; then err 'a$LFb$LFc' "$ret" fi new "xml simple CDATA" expecteofx "$clixon_util_xml -o" 0 '' '' new "xml CDATA right square bracket: ]" expecteofx "$clixon_util_xml -o" 0 "" "" new "xml simple CDATA to json" expecteofx "$clixon_util_xml -o -j" 0 '' '{"a":""}' # Example partly from https://www.w3resource.com/xml/attribute.php # XML complex CDATA (with comments for debug):; DUMMY=$(cat <An example of escaped CENDs y" so I guess that means that z > x ]]> ]]> ]]> EOF ) # without comments XML=$(cat <An example of escaped CENDs y" so I guess that means that z > x ]]> ]]> ]]> EOF ) XML=$(cat <<'EOF' An example of escaped CENDs y" so I guess that means that z > x ]]>]]>]]> EOF ) new "complex CDATA xml to xml" expecteof "$clixon_util_xml -o" 0 "$XML" "^$XML $" JSON=$(cat < y\" so I guess that means that z > x ]]>","data":"]]>","alternative":"]]>"}} EOF ) new "complex CDATA xml to json" expecteofx "$clixon_util_xml -oj" 0 "$XML" "$JSON" # reverse new "complex CDATA json to json" expecteofx "$clixon_util_json -j" 0 "$JSON" "$JSON" # reverse new "complex CDATA json to xml" expecteofx "$clixon_util_json" 0 "$JSON" "$XML" XML=$(cat <Less than: < , greater than: > ampersand: & EOF ) new "xml encode <>&" expecteof "$clixon_util_xml -o" 0 "$XML" "$XML" new "xml encode <>& to json" expecteof "$clixon_util_xml -oj" 0 "$XML" '{"message":"Less than: < , greater than: > ampersand: & "}' XML=$(cat <single-quote character ' represented as ' and double-quote character as " EOF ) new "xml single and double quote" expecteof "$clixon_util_xml -o" 0 "$XML" "single-quote character ' represented as ' and double-quote character as \"" JSON=$(cat <a\b" "a\b" new "xml backspace to json" expecteofx "$clixon_util_xml -oj" 0 "a\b" '{"a":"a\\b"}' new "Double quotes for attributes" expecteof "$clixon_util_xml -o" 0 '' '' new "Single quotes for attributes (returns double quotes but at least parses right)" expecteof "$clixon_util_xml -o" 0 "" '' new "Mixed quotes" expecteof "$clixon_util_xml -o" 0 "" '' new "XMLdecl version" expecteof "$clixon_util_xml -o" 0 '' '' new "XMLdecl version, single quotes" expecteof "$clixon_util_xml -o" 0 "" '' new "XMLdecl version no element" expecteof "$clixon_util_xml -o" 255 '' '' 2> /dev/null new "XMLdecl no version" expecteof "$clixon_util_xml -o" 255 '' '' 2> /dev/null new "XMLdecl misspelled version" expecteof "$clixon_util_xml -ol o" 255 '' '' 2> /dev/null new "XMLdecl version + encoding" expecteof "$clixon_util_xml -o" 0 '' '' # XML processors SHOULD match character encoding names in a case-insensitive way new "XMLdecl encoding case-insensitive" expecteof "$clixon_util_xml -o" 0 '' '' new "XMLdecl version + wrong encoding" expecteof "$clixon_util_xml -o" 255 '' '' 2> /dev/null new "XMLdecl version + misspelled encoding" expecteof "$clixon_util_xml -ol o" 255 '' 'syntax error: at or before: e' 2> /dev/null new "XMLdecl version + standalone" expecteof "$clixon_util_xml -o" 0 '' '' new "PI - Processing instruction empty" expecteof "$clixon_util_xml -o" 0 '' '' new "PI some content" expecteof "$clixon_util_xml -o" 0 '' '' new "prolog element misc*" expecteof "$clixon_util_xml -o" 0 '' '' # We allow it as an internal necessity for parsing of xml fragments #new "double element error" #expecteof "$clixon_util_xml" 255 '' '' new "namespace: DefaultAttName" expecteof "$clixon_util_xml -o" 0 'hello' 'hello' new "namespace: PrefixedAttName" expecteof "$clixon_util_xml -o" 0 'hello' '^hello$' new "First example 6.1 from https://www.w3.org/TR/2009/REC-xml-names-20091208" XML=$(cat < Frobnostication Moved to here. EOF ) expecteof "$clixon_util_xml -o" 0 "$XML" '^Frobnosticationhere.$' new "Second example 6.1 from https://www.w3.org/TR/2009/REC-xml-names-20091208" XML=$(cat < Cheaper by the Dozen 1568491379 EOF ) expecteof "$clixon_util_xml -o" 0 "$XML" '^Cheaper by the Dozen1568491379$' rm -rf $dir # unset conditional parameters unset clixon_util_xml new "endtest" endtest