1#!/usr/bin/env sh
2
3GPSBABEL_FREEZE_TIME=y
4export GPSBABEL_FREEZE_TIME
5
6# Turn on GNU libc instrumentation.
7MALLOC_CHECK_=2
8export MALLOC_CHECK_
9
10# Testcases are written for English/US locale (by default)
11LC_ALL=en_US.UTF-8
12export LC_ALL
13
14BASEPATH=`dirname $0`
15PNAME=${PNAME:-${BASEPATH}/gpsbabel}
16DIFF=${DIFF:-diff}
17REFERENCE=${BASEPATH}/reference
18# OD=${OD:-od -Ax -txC -v}
19if [ -x /usr/bin/hexdump ] ; then
20	OD=${OD:-hexdump -v -C}
21else
22	OD=${OD:-od -Ax -txC -v}
23fi
24XMLWF=xmlwf
25# set up a variable so we can skip extra tests when running valgrind
26echo "${PNAME}" | grep -q valgrind
27RUNNINGVALGRIND=$?
28
29TMPDIR=${GBTEMP:-/tmp}/gpsbabel.$$
30mkdir -p $TMPDIR
31trap "rm -fr $TMPDIR" 0 1 2 3 15
32
33bincompare()
34{
35	rm -f ${TMPDIR}/bc1
36		rm -f ${TMPDIR}/bc2
37		${OD} $1 >${TMPDIR}/bc1
38		${OD} $2 >${TMPDIR}/bc2
39		${DIFF} ${TMPDIR}/bc1 ${TMPDIR}/bc2 || {
40		echo ERROR binary comparing $*
41		errorcount=`expr $errorcount + 1`
42		#exit 1
43	}
44}
45
46compare()
47{
48	${DIFF} -u -b -w $* ||  {
49		echo ERROR comparing $*
50		errorcount=`expr $errorcount + 1`
51		#exit 1
52	}
53}
54
55compare_nole()
56{
57	${DIFF} -u --strip-trailing-cr $* ||  {
58		echo ERROR comparing $*
59		errorcount=`expr $errorcount + 1`
60		#exit 1
61	}
62}
63
64compare_with_alternate()
65{
66	${DIFF} -u -b -w $1 $3 1>${TMPDIR}/compare.log ||  {
67		${DIFF} -u -b -w $2 $3 || {
68			echo ERROR comparing with alternate $2 to $3
69			cat ${TMPDIR}/compare.log
70			rm -f ${TMPDIR}/compare.log
71			echo ERROR comparing with alternate $1 to $3
72			errorcount=`expr $errorcount + 1`
73			#exit 1
74		}
75	}
76}
77
78sort_and_compare()
79{
80	sort $1 > $TMPDIR/s1
81	sort $2 > $TMPDIR/s2
82	compare $TMPDIR/s1 $TMPDIR/s2
83}
84
85gpsbabel()
86{
87	${PNAME} "$@" || {
88		echo "$PNAME returned error $?"
89		echo "($PNAME $@)"
90		errorcount=`expr $errorcount + 1`
91		#exit 1
92	}
93}
94
95utf8bomcheck()
96{
97  if [ ${RUNNINGVALGRIND} -ne  0 ]; then
98    if [ "$(dd if=$1 bs=1 count=3 2>/dev/null)" = $'\xef\xbb\xbf' ]; then
99      echo "ERROR: UTF-8 BOM found in $1"
100      errorcount=`expr $errorcount + 1`
101    fi
102  fi
103}
104
105xmlwfcheck()
106{
107  if [ ${RUNNINGVALGRIND} -ne 0 ]; then
108    if which ${XMLWF} >/dev/null 2>&1; then
109      # xmlwf is a bit lame, exit status is always 0
110      rm -f ${TMPDIR}/xmlwf.out
111      ${XMLWF} $1 2>&1 | tee ${TMPDIR}/xmlwf.out
112      if [ -s ${TMPDIR}/xmlwf.out ]; then
113        echo "ERROR: xml is not well-formed in $1"
114        errorcount=`expr $errorcount + 1`
115      fi
116    fi
117  fi
118}
119
120# Some formats are just too boring to test.   The ones that
121# are xcsv include
122# garmin301
123# garmin_poi
124# gpsdrivetrack
125# nima
126# mapconverter
127# geonet
128# saplus
129# s_and_t
130# xmap2006
131# cambridge
132# cup
133
134errorcount=0
135
136if [ $# -ge 1 ]; then
137  while [ $# -ge 1 ];
138  do
139    t=${BASEPATH}/testo.d/$1.test
140    echo Running $t
141    . $t
142    shift;
143  done
144else
145  for i in ${BASEPATH}/testo.d/*.test
146  do
147    echo Running $i
148    . $i
149  done
150fi
151
152# Multiple tests produce these files in addition to gpx.test and kml.test,
153# e.g. tpo.test has produced problematic gpx and kml files.
154# For this reason this check isn't incorporated into each test.
155
156# reference/basecamp.gpx has a UTF-8 BOM which is useful for verifying this test.
157#cp reference/basecamp.gpx ${TMPDIR}
158#cp LineStyles.gpx.badchar ${TMPDIR}/bad.gpx
159XMLS=$(find ${TMPDIR} -name "*.gpx" -o -name "*.kml")
160
161if [ ${RUNNINGVALGRIND} -ne  0 ]; then
162  echo "Running UTF-8 BOM test"
163  for i in ${XMLS}
164  do
165    utf8bomcheck $i
166  done
167fi
168
169if [ ${RUNNINGVALGRIND} -ne  0 ]; then
170  if which ${XMLWF} >/dev/null 2>&1; then
171    echo "Running well-formed XML test"
172    for i in ${XMLS}
173    do
174      xmlwfcheck $i
175    done
176  else
177    echo "Skipping well-formed XML test"
178  fi
179fi
180
181exit $errorcount
182