1#! /usr/bin/env bash
2# EXPAT TEST SCRIPT FOR W3C XML TEST SUITE
3#
4# This script can be used to exercise Expat against the
5# w3c.org xml test suite, available from
6# http://www.w3.org/XML/Test/xmlts20020606.zip.
7#
8# To run this script, first set XMLWF below so that xmlwf can be
9# found, then set the output directory with OUTPUT.
10#
11# The script lists all test cases where Expat shows a discrepancy
12# from the expected result. Test cases where only the canonical
13# output differs are prefixed with "Output differs:", and a diff file
14# is generated in the appropriate subdirectory under $OUTPUT.
15#
16# If there are output files provided, the script will use
17# output from xmlwf and compare the desired output against it.
18# However, one has to take into account that the canonical output
19# produced by xmlwf conforms to an older definition of canonical XML
20# and does not generate notation declarations.
21#
22#                          __  __            _
23#                       ___\ \/ /_ __   __ _| |_
24#                      / _ \\  /| '_ \ / _` | __|
25#                     |  __//  \| |_) | (_| | |_
26#                      \___/_/\_\ .__/ \__,_|\__|
27#                               |_| XML parser
28#
29# Copyright (c) 2002-2004 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
30# Copyright (c) 2002      Karl Waclawek <karl@waclawek.net>
31# Copyright (c) 2008-2019 Sebastian Pipping <sebastian@pipping.org>
32# Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
33# Licensed under the MIT license:
34#
35# Permission is  hereby granted,  free of charge,  to any  person obtaining
36# a  copy  of  this  software   and  associated  documentation  files  (the
37# "Software"),  to  deal in  the  Software  without restriction,  including
38# without  limitation the  rights  to use,  copy,  modify, merge,  publish,
39# distribute, sublicense, and/or sell copies of the Software, and to permit
40# persons  to whom  the Software  is  furnished to  do so,  subject to  the
41# following conditions:
42#
43# The above copyright  notice and this permission notice  shall be included
44# in all copies or substantial portions of the Software.
45#
46# THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
47# EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
48# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
49# NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
50# DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
51# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
52# USE OR OTHER DEALINGS IN THE SOFTWARE.
53
54shopt -s nullglob
55
56# Note: OUTPUT must terminate with the directory separator.
57OUTPUT="$PWD/tests/out/"
58TS="$PWD/tests/"
59
60MYDIR="`dirname \"$0\"`"
61cd "$MYDIR"
62MYDIR="`pwd`"
63#XMLWF="${1:-`dirname \"$MYDIR\"`/xmlwf/xmlwf}"
64XMLWF=/usr/bin/xmlwf
65# Unicode-aware diff utility
66DIFF="${MYDIR}/udiffer.py"
67
68
69# RunXmlwfNotWF file reldir
70# reldir includes trailing slash
71RunXmlwfNotWF() {
72  file="$1"
73  reldir="$2"
74  if $XMLWF -p "$file" > /dev/null; then
75      echo "Expected not well-formed: $reldir$file"
76      return 1
77  else
78      return 0
79  fi 
80}
81
82# RunXmlwfWF file reldir
83# reldir includes trailing slash
84RunXmlwfWF() {
85  file="$1"
86  reldir="$2"
87  $XMLWF -p -N -d "$OUTPUT$reldir" "$file" > outfile || return $?
88  read outdata < outfile 
89  if test "$outdata" = "" ; then 
90      if [ -f "out/$file" ] ; then 
91          $DIFF "$OUTPUT$reldir$file" "out/$file" > outfile 
92          if [ -s outfile ] ; then 
93              cp outfile "$OUTPUT$reldir$file.diff"
94              echo "Output differs: $reldir$file"
95              return 1
96          fi 
97      fi 
98      return 0
99  else 
100      echo "In $reldir: $outdata"
101      return 1
102  fi 
103}
104
105SUCCESS=0
106ERROR=0
107
108UpdateStatus() {
109  if [ "$1" -eq 0 ] ; then
110    SUCCESS=`expr $SUCCESS + 1`
111  else
112    ERROR=`expr $ERROR + 1`
113  fi
114}
115
116##########################
117# well-formed test cases #
118##########################
119
120cd "$TS/xmlconf"
121for xmldir in ibm/valid/P* \
122              ibm/invalid/P* \
123              xmltest/valid/ext-sa \
124              xmltest/valid/not-sa \
125              xmltest/invalid \
126              xmltest/invalid/not-sa \
127              xmltest/valid/sa \
128              sun/valid \
129              sun/invalid ; do
130  cd "$TS/xmlconf/$xmldir"
131  mkdir -p "$OUTPUT$xmldir"
132  for xmlfile in $(ls -1 *.xml | sort -d) ; do
133      [[ -f "$xmlfile" ]] || continue
134      RunXmlwfWF "$xmlfile" "$xmldir/"
135      UpdateStatus $?
136  done
137  rm -f outfile
138done
139
140cd "$TS/xmlconf/oasis"
141mkdir -p "$OUTPUT"oasis
142for xmlfile in *pass*.xml ; do
143    RunXmlwfWF "$xmlfile" "oasis/"
144    UpdateStatus $?
145done
146rm outfile
147
148##############################
149# not well-formed test cases #
150##############################
151
152cd "$TS/xmlconf"
153for xmldir in ibm/not-wf/P* \
154              ibm/not-wf/p28a \
155              ibm/not-wf/misc \
156              xmltest/not-wf/ext-sa \
157              xmltest/not-wf/not-sa \
158              xmltest/not-wf/sa \
159              sun/not-wf ; do
160  cd "$TS/xmlconf/$xmldir"
161  for xmlfile in *.xml ; do
162      RunXmlwfNotWF "$xmlfile" "$xmldir/"
163      UpdateStatus $?
164  done
165done
166
167cd "$TS/xmlconf/oasis"
168for xmlfile in *fail*.xml ; do
169    RunXmlwfNotWF "$xmlfile" "oasis/"
170    UpdateStatus $?
171done
172
173echo "Passed: $SUCCESS"
174echo "Failed: $ERROR"
175