Cleaning up code for xml insignificant whitespace removal

Experimenal explicit index search code
This commit is contained in:
Olof hagsand 2020-02-07 14:59:57 +01:00
parent c7d6f69a85
commit a674af6f2c
32 changed files with 493 additions and 180 deletions

View file

@ -70,7 +70,7 @@ testname=
# for starting a backend
: ${BETIMEOUT:=10}
# If set, enable debugging (of backend)
# If set, enable debugging (of backend and restconf daemons)
: ${DBG:=0}
# Where to log restconf. Some systems may not have syslog,

View file

@ -15,8 +15,11 @@ s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
# Number of list/leaf-list entries
: ${nr:=100}
# Number of tests to generate XML for
max=7
# XML file (alt provide it in stdin after xpath)
for (( i=1; i<7; i++ )); do
for (( i=1; i<$max; i++ )); do
eval xml$i=$dir/xml$i.xml
done
ydir=$dir/yang

View file

@ -113,9 +113,11 @@ expectfn "$clixon_cli -1 -f $cfg -l o load /tmp/foo" 0 "^$"
new "cli check load"
expectfn "$clixon_cli -1 -f $cfg -l o show conf cli" 0 "interfaces interface eth/0/0 ipv4 enabled true"
new "cli debug"
new "cli debug set"
expectfn "$clixon_cli -1 -f $cfg -l o debug level 1" 0 "^$"
# How to test this?
new "cli debug reset"
expectfn "$clixon_cli -1 -f $cfg -l o debug level 0" 0 "^$"
new "cli rpc"

View file

@ -9,8 +9,11 @@ s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
# Number of list/leaf-list entries
: ${nr:=100}
# Number of tests to generate XML for
max=9
# XML file (alt provide it in stdin after xpath)
for (( i=1; i<9; i++ )); do
for (( i=1; i<$max; i++ )); do
eval xml$i=$dir/xml$i.xml
done
ydir=$dir/yang
@ -19,7 +22,7 @@ if [ ! -d $ydir ]; then
mkdir $ydir
fi
# XPATH binary search in ordered-by system lists
# Instance-id PATH binary search in ordered-by system lists
cat <<EOF > $ydir/moda.yang
module moda{
namespace "urn:example:a";

View file

@ -63,6 +63,7 @@ module leafref{
}
EOF
# This is state data writte to file that backend reads from (on request)
cat <<EOF > $fstate
<sender-state xmlns="urn:example:example">
<ref>x</ref>
@ -83,7 +84,7 @@ if [ $BE -ne 0 ]; then
wait_backend
fi
# Test top-level, default prefix, wring leafref prefix and typedef path
# Test top-level, default prefix, wrong leafref prefix and typedef path
XML=$(cat <<EOF
<sender-config xmlns="urn:example:example">
<name>x</name>
@ -127,6 +128,24 @@ expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get content="nonconfig"><filter ty
new "netconf get /sender-config config-only"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get content="config"><filter type="xpath" select="/sender-config" xmlns="urn:example:example"/></get></rpc>]]>]]>' '^<rpc-reply><data><sender-config xmlns="urn:example:example"><name>x</name></sender-config></data></rpc-reply>]]>]]>$'
# Negative tests,
# Double xmlns attribute
cat <<EOF > $fstate
<sender-config xmlns="urn:example:example">
<name>x</name>
</sender-config>
EOF
new "Merge same tree - check double xmlns attribute"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get content="all"><filter type="xpath" select="/"/></get></rpc>]]>]]>' '^<rpc-reply><data><sender-config xmlns="urn:example:example"><name>x</name></sender-config></data></rpc-reply>]]>]]>$'
# Back to original
cat <<EOF > $fstate
<sender-state xmlns="urn:example:example">
<ref>x</ref>
</sender-state>
EOF
# delete x, add y
XML=$(cat <<EOF
<sender-config xmlns="urn:example:example" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">

154
test/test_netconf_whitespace.sh Executable file
View file

@ -0,0 +1,154 @@
#!/usr/bin/env bash
# Test netconf / xml whitespaces
# Insignicant and significant
# See https://www.w3.org/TR/xml-c14n
# where "clean", "dirty" and "mixed" whitespaces are defined
# <clean> </clean>
# <dirty> A B </dirty>
# <mixed>
# A
# <clean> </clean>
# B
# <dirty> A B </dirty>
# C
# </mixed>
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
APPNAME=example
cfg=$dir/conf_yang.xml
fyang=$dir/whitespace.yang
cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_XMLDB_DIR>$dir</CLICON_XMLDB_DIR>
</clixon-config>
EOF
cat <<EOF > $fyang
module whitespace{
yang-version 1.1;
namespace "urn:example:whitespace";
prefix fi;
container x{
presence true;
list y {
key a;
leaf a{
type string;
}
container b{
presence true;
}
}
}
}
EOF
new "test params: -f $cfg"
echo '<config><ex:x xmlns:ex="urn:example:whitespace">
<ex:y> <ex:a>foo</ex:a>\n <ex:b> </ex:b></ex:y> </ex:x></config></edit-config></rpc>]]>]]>$start</config>' > $dir/startup_db
if [ $BE -ne 0 ]; then
new "kill old backend"
sudo clixon_backend -zf $cfg -s startup
if [ $? -ne 0 ]; then
err
fi
new "start backend -s startup -f $cfg"
start_backend -s startup -f $cfg
fi
new "waiting"
wait_backend
new "get startup"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>' '^<rpc-reply><data><ex:x xmlns:ex="urn:example:whitespace"><ex:y><ex:a>foo</ex:a><ex:b/></ex:y></ex:x></data></rpc-reply>]]>]]>$'
new "remove"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:whitespace" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete"/></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "commit"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><commit/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "add insignificant 'dirty' whitespaces"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:whitespace"> \t \ <y> <a>foo</a>\n </y> </x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "get stripped whitespace"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>' '^<rpc-reply><data><x xmlns="urn:example:whitespace"><y><a>foo</a></y></x></data></rpc-reply>]]>]]>$'
new "validate"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "add insignificant 'dirty' whitespaces with prefixes"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><ex:x xmlns:ex="urn:example:whitespace"> \t \ <ex:y> <ex:a>foo</ex:a>\n </ex:y> </ex:x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "get stripped whitespace"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>' '^<rpc-reply><data><ex:x xmlns:ex="urn:example:whitespace"><ex:y><ex:a>foo</ex:a></ex:y></ex:x></data></rpc-reply>]]>]]>$'
new "validate"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "add insignificant 'clean' whitespaces"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:whitespace"> \n \t </x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "add more insignificant 'clean' whitespaces"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:whitespace"><y><a>foo</a><b> </b></y></x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "get stripped whitespace"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>' '^<rpc-reply><data><x xmlns="urn:example:whitespace"><y><a>foo</a><b/></y></x></data></rpc-reply>]]>]]>'
new "validate"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "add significant whitespaces"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:whitespace"><y><a> foo
bar </a></y></x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "get significant whitespace"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>' '^<rpc-reply><data><x xmlns="urn:example:whitespace"><y><a> foo
bar </a></y></x></data></rpc-reply>]]>]]>'
new "validate"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
if [ $BE -eq 0 ]; then
exit # BE
fi
new "Kill backend"
# Check if premature kill
pid=$(pgrep -u root -f clixon_backend)
if [ -z "$pid" ]; then
err "backend already dead"
fi
# kill backend
stop_backend -f $cfg
rm -rf $dir

View file

@ -163,7 +163,7 @@ XML=$(cat <<EOF
</html:html>
EOF
)
expecteof "$clixon_util_xml -o" 0 "$XML" "$XML"
expecteof "$clixon_util_xml -o" 0 "$XML" '^<html:html xmlns:html="http://www.w3.org/1999/xhtml"><html:head><html:title>Frobnostication</html:title></html:head><html:body><html:p><html:a href="http://frob.example.com">here.</html:a></html:p></html:body></html:html>$'
new "Second example 6.1 from https://www.w3.org/TR/2009/REC-xml-names-20091208"
XML=$(cat <<EOF
@ -176,8 +176,8 @@ XML=$(cat <<EOF
</bk:book>
EOF
)
expecteof "$clixon_util_xml -o" 0 "$XML" "$XML"
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>$'
rm -rf $dir
# unset conditional parameters