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`"
63XMLWF="${1:-`dirname \"$MYDIR\"`/xmlwf/xmlwf}"
64# Unicode-aware diff utility
65DIFF="${MYDIR}/udiffer.py"
66
67
68# RunXmlwfNotWF file reldir
69# reldir includes trailing slash
70RunXmlwfNotWF() {
71  file="$1"
72  reldir="$2"
73  if $XMLWF -p "$file" > /dev/null; then
74      echo "Expected not well-formed: $reldir$file"
75      return 1
76  else
77      return 0
78  fi 
79}
80
81# RunXmlwfWF file reldir
82# reldir includes trailing slash
83RunXmlwfWF() {
84  file="$1"
85  reldir="$2"
86  $XMLWF -p -N -d "$OUTPUT$reldir" "$file" > outfile || return $?
87  read outdata < outfile 
88  if test "$outdata" = "" ; then 
89      if [ -f "out/$file" ] ; then 
90          $DIFF "$OUTPUT$reldir$file" "out/$file" > outfile 
91          if [ -s outfile ] ; then 
92              cp outfile "$OUTPUT$reldir$file.diff"
93              echo "Output differs: $reldir$file"
94              return 1
95          fi 
96      fi 
97      return 0
98  else 
99      echo "In $reldir: $outdata"
100      return 1
101  fi 
102}
103
104SUCCESS=0
105ERROR=0
106
107UpdateStatus() {
108  if [ "$1" -eq 0 ] ; then
109    SUCCESS=`expr $SUCCESS + 1`
110  else
111    ERROR=`expr $ERROR + 1`
112  fi
113}
114
115##########################
116# well-formed test cases #
117##########################
118
119cd "$TS/xmlconf"
120for xmldir in ibm/valid/P* \
121              ibm/invalid/P* \
122              xmltest/valid/ext-sa \
123              xmltest/valid/not-sa \
124              xmltest/invalid \
125              xmltest/invalid/not-sa \
126              xmltest/valid/sa \
127              sun/valid \
128              sun/invalid ; do
129  cd "$TS/xmlconf/$xmldir"
130  mkdir -p "$OUTPUT$xmldir"
131  for xmlfile in $(ls -1 *.xml | sort -d) ; do
132      [[ -f "$xmlfile" ]] || continue
133      RunXmlwfWF "$xmlfile" "$xmldir/"
134      UpdateStatus $?
135  done
136  rm -f outfile
137done
138
139cd "$TS/xmlconf/oasis"
140mkdir -p "$OUTPUT"oasis
141for xmlfile in *pass*.xml ; do
142    RunXmlwfWF "$xmlfile" "oasis/"
143    UpdateStatus $?
144done
145rm outfile
146
147##############################
148# not well-formed test cases #
149##############################
150
151cd "$TS/xmlconf"
152for xmldir in ibm/not-wf/P* \
153              ibm/not-wf/p28a \
154              ibm/not-wf/misc \
155              xmltest/not-wf/ext-sa \
156              xmltest/not-wf/not-sa \
157              xmltest/not-wf/sa \
158              sun/not-wf ; do
159  cd "$TS/xmlconf/$xmldir"
160  for xmlfile in *.xml ; do
161      RunXmlwfNotWF "$xmlfile" "$xmldir/"
162      UpdateStatus $?
163  done
164done
165
166cd "$TS/xmlconf/oasis"
167for xmlfile in *fail*.xml ; do
168    RunXmlwfNotWF "$xmlfile" "oasis/"
169    UpdateStatus $?
170done
171
172echo "Passed: $SUCCESS"
173echo "Failed: $ERROR"
174