1#!/bin/sh
2# alive.test
3#
4# Copyright (C) 2010 Thien-Thi Nguyen
5# Copyright (C) 2018-2020 Free Software Foundation, Inc.
6#
7# This program is free software, licensed under the terms of the GNU
8# General Public License as published by the Free Software Foundation,
9# either version 3 of the License, or (at your option) any later version.
10# You should have received a copy of the GNU General Public License
11# along with this program.  If not, see <http://www.gnu.org/licenses/>.
12#
13# Author: Thien-Thi Nguyen
14# Modified: Reini Urban
15
16# Commentary:
17
18# This is a provisional test until something better comes along.
19# It checks "aliveness" and the program returning success, and not
20# correctness or performance.
21#
22# It relies on the TESTS_ENVIRONMENT Makefile.am variable to set
23# valid `PROGS'.  Each prog in PROGS is run against a set of example DWGs,
24# and if *not* successful the verbose output from -v2 is saved in a
25# file whose name is DWG-PROG.log.
26#
27# If there are no problems, exit successfully.  Otherwise display
28# the count of problem runs, the list of *.log files and exit with failure.
29# DATADIR can be set to support testing from a separate build dir.
30
31# Code:
32
33PROGS=${PROGS:-"dwgread dwgbmp dwg2dxf dwg2SVG dwglayers dwggrep dwgrewrite dxf2dwg dwgwrite"}
34[ -z "$DATADIR" ] && DATADIR="../../test/test-data"
35top_builddir=".."
36# artefact for "shellcheck"
37[ -z "$top_builddir" ] && echo $top_builddir
38CFLAGS="-g -O2"
39DISABLE_JSON=""
40DISABLE_WRITE="0"
41EXEEXT=""
42
43i=0
44rm -f ./*-dxf*.log ./*-read,.log ./*-write.log ./*-json.log ./*-rw.log
45rm -f ./*-bmp.log ./*-grep.log ./*-layers.log ./*-SVG.log ./*-ps.log ./*-svg2.log
46
47debugclasses=
48case "$CFLAGS" in
49    *-DDEBUG_CLASSES*) debugclasses=1 ;;
50    *) ;;
51esac
52
53#just check that it does not crash
54run ()
55{
56    prog="$1"
57    dwg="$2"
58    addarg="$3"
59
60    bprog="$(basename "$prog" .exe)"
61    bdwg="$(basename "$dwg" .dwg)"
62    bprog1=$(echo "$bprog" | sed -e's,^dwg2,,')
63    bprog1=$(echo "$bprog1" | sed -e's,^dwg,,')
64    if [ -n "$addarg" ]; then
65        out=$(echo "$addarg" | sed -e 's,--as=,,')
66        log="$bdwg-$out-$bprog1.log"
67    else
68        log="$bdwg-$bprog1.log"
69    fi
70    arg="${DATADIR}/$dwg.dwg"
71    rm "./$log" 2>/dev/null
72    case $bprog in
73    dwglayers)  ;;
74    dwggrep)    arg="-i tekst $arg" ;;
75    dwg2dxf)    if [ -n "$addarg" ]; then
76                    arg="-y $addarg -o $bdwg-$out.dxf $arg"
77                else
78                    arg="-y $arg"
79                fi
80                ;;
81    dwgread)    if [ -z "$DISABLE_JSON" ]; then
82                    if [ -n "$addarg" ]; then
83                        arg="-v2 $addarg -o $bdwg-$out.json $arg"
84                    else
85                        arg="-v2 -o $dwg.json $arg"
86                    fi
87                else
88                    arg="-v2 $arg"
89                fi
90                ;;
91    dwgwrite)   if [ -z "$DISABLE_JSON" ]; then
92                    if [ -n "$addarg" ]; then
93                        arg="-y -v2 $addarg -o $bdwg-$out.dwg $dwg.json"
94                    else
95                        arg="-y -v2 $dwg.json"
96                    fi
97                else
98                    if [ -n "$addarg" ]; then
99                        arg="-y -v2 $addarg -o $bdwg-$out.dwg $dwg.dxf"
100                    else
101                        arg="-y -v2 $dwg.dxf"
102                    fi
103                fi
104                ;;
105    dxfwrite)   if [ -z "$DISABLE_JSON" ]; then
106                    if [ -n "$addarg" ]; then
107                        arg="-y -v2 $addarg -o $bdwg-$out.dxf $dwg.json"
108                    else
109                        arg="-y -v2 $dwg.json"
110                    fi
111                else
112                    if [ -n "$addarg" ]; then
113                        arg="-y -v2 $addarg -o $bdwg-$out.dxf $dwg.dwg"
114                    else
115                        arg="-y -v2 $dwg.dwg"
116                    fi
117                fi
118                ;;
119    dxf2dwg)    if [ -n "$addarg" ]; then
120                    arg="-y -v2 $addarg -o $bdwg-$out.dwg ${DATADIR}/$dwg.dxf"
121                else
122                    arg="-y -v2 ${DATADIR}/$dwg.dxf"
123                fi
124                ;;
125    dwgrewrite) if [ -n "$addarg" ]; then
126                    arg="-v2 $addarg $arg"
127                else
128                    arg="-v2 $arg"
129                fi
130                ;;
131    dwgfuzz)    if [ -n "$DISABLE_WRITE" ]; then
132                    arg="-dwg $arg"
133                else
134                    arg="-rw $arg"
135                fi
136                ;;
137    dwgadd)     arg="-o dwgadd.dwg $DATADIR/../../examples/dwgadd.example"
138                ;;
139    esac
140    if [ -n "$VALGRIND" ] && [ -n "$LOG_COMPILER" ]; then
141        TESTPROG="$LOG_COMPILER $LOG_FLAGS"
142        log="$TEST_SUITE_LOG"
143    else
144        # libtool only if shared
145        TESTPROG="${top_builddir}/libtool --mode=execute"
146    fi
147    # indxf still leaks, GH #151
148    if test "$bprog" = "dxf2dwg" -a -n ""
149    then
150       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
151    fi
152    if test "$bprog" = "dwgfuzz" -a -n ""
153    then
154       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
155    fi
156    if test "$bprog" = "dwgadd" -a -n ""
157    then
158       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
159    fi
160    # rewrite leaking 3DSOLID
161    #if test $bprog = dwgrewrite -a -n "" -a "$bdwg" = example_2000
162    #then
163    #   TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
164    #fi
165    # injson leaks fixed, GH #197
166    #if test $bprog = dwgwrite -a -n ""
167    #then
168    #   TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
169    #fi
170    echo "$TESTPROG $prog $arg 2>$log"
171    # writers can hang
172    #if test $bprog = dwgrewrite -o $bprog = dwgwrite -o $bprog = dxf2dwg
173    #then
174    #    if test x$MINGW != x; then
175    #        OLDTESTPROG=$TESTPROG
176    #        TESTPROG=echo
177    #    else
178    #        (sleep 10s; killall $bprog 2>/dev/null) &
179    #    fi
180    #fi
181    # shellcheck disable=SC2086
182    if $TESTPROG ./$prog $arg >"$log" 2>&1
183    then
184        rm "./$log"
185    else
186        todo=
187        case $bprog in
188            dwgwrite)
189                [ -z $debugclasses ] || todo=1
190                case "$(uname -s)" in
191                    CYGWIN*) todo=1 ;;
192                    *) ;;
193                esac
194                ;;
195            dwgrewrite)
196                case "$(uname -s)" in
197                    MINGW*) test -f core && rm core
198                            todo=1
199                            ;;
200                    *) ;;
201                esac
202                ;;
203            *)  ;;
204        esac
205
206        if [ -z $todo ]
207        then
208            i=$((i+1))
209            echo "$prog failure $?"
210            tail -n100 "$log"
211        else
212            tmpnum=$((i+1))
213            echo "$(basename "$0"): TODO $prog $tmpnum failures"
214            ls -l "$log"
215        fi
216    fi
217    if [ x"$MINGW" != x ] && [ x"$OLDTESTPROG" != x ]; then
218        TESTPROG="$OLDTESTPROG"
219    fi
220}
221
222DATA="sample_2000 example_2000 example_2004 example_2007 example_2010"
223DATA="$DATA example_2013 example_2018 example_r14"
224#TODO="r11/ACEL10"
225
226for d in $DATA; do
227    b="$(basename "$d")"
228    rm "./$b.dxf" 2>/dev/null
229    rm "./$b.svg" 2>/dev/null
230    rm "./$b.ps" 2>/dev/null
231    rm "./$b.bmp" 2>/dev/null
232    rm "./$b-rewrite.dwg" 2>/dev/null
233    rm "./$b-dwgwrite.dwg" 2>/dev/null
234done
235
236for prog in $PROGS ; do
237    case $prog in
238    *dwgadd*)
239        run "$prog" "."
240        ;;
241    *)  for dwg in $DATA; do
242            run "$prog" "$dwg"
243        done
244        ;;
245    esac
246done
247
248# dwgread converts to json here, but has no --as yet
249# still disabled, see branch work/2004
250if false; then
251for prog in dwgwrite dwgrewrite dxf2dwg dwg2dxf dxfwrite ; do
252    for dwg in $DATA; do
253        for as in --as=r13 --as=r2000 --as=r2004 --as=r2010 --as=r2013 --as=r2018; do
254            run "$prog${EXEEXT}" "$dwg" "$as"
255        done
256    done
257done
258fi
259
260case $PROGS in
261    *dwggrep*)
262        echo "${top_builddir}/libtool --mode=execute dwggrep${EXEEXT} -i -c tekst ${DATADIR}/example_*.dwg"
263        n="$(${top_builddir}/libtool --mode=execute ./dwggrep${EXEEXT} -i -c tekst "${DATADIR}"/example_*.dwg | tr -d "\r")"
264        if test "$n" != "24"; then
265            i=$((i+1))
266            echo "dwggrep failure $?"
267            echo "result $n, expected 24"
268        fi
269esac
270
271#run dwgread r11/ACEL10
272
273# TODO: check generated DXF's against original DXF's
274# skip the run part if dwg2dxf is part of bin_PROGRAMS (i.e. being installed)
275# rather see the dxf.test roundtrip test
276# only in programs, not examples:
277if [ -f cmp_dxf.pl ] && [ -e dwg2dxf ]; then
278    for dwg in $DATA; do
279        run dwg2dxf${EXEEXT} "$dwg"
280        #if [ -f ${DATADIR}/$dwg.dxf ]; then
281        #    base="$(basename "$dwg" .dwg)"
282        #    perl filt_dxf.pl ${DATADIR}/$dwg.dxf > $base.dxf.orig
283        #    perl filt_dxf.pl $base.dxf > $base.dxf.new
284        #    diff -bu  $base.dxf.orig $base.dxf.new | perl cmp_dxf.pl
285        #    rm $base.dxf.orig $base.dxf.new 2>/dev/null
286        #fi
287    done
288fi
289
290if test "0" = "$i" ; then
291    exit 0
292else
293    echo "$(basename "$0"): $i failures"
294    ls -l ./*.log
295    exit 1
296fi
297
298# alive.test ends here
299