1#!/bin/sh
2
3#
4# extgetvariable_testrunner.sh, container-emulated, automated
5#     GetVariable plugin function test generator
6#
7# Copyright (C) 2015 Free Software Foundation, Inc.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22#
23#
24# Original author: Nutchanon Wetchasit <Nutchanon.Wetchasit@gmail.com>
25#
26# This test runner checks Gnash for:
27#  * GetVariable() datatype issues (bug #42395)
28#        <https://savannah.gnu.org/bugs/?42395>
29#  * Timeline variable declaration issue (bug #45840) in SWF5 environment
30#        <https://savannah.gnu.org/bugs/?45840>
31#
32# Usage:
33#     ./extgetvariable_testrunner.sh <builddir> <srcdir> <swfversion> <swf>
34#
35# Generated test runner's exit codes:
36#     0         if tester ran completely
37#     non-zero  if tester encountered an error
38#
39# Note:
40#     The generated test file requires a filesystem that supports named pipes.
41#
42
43# Check for generation parameters
44while getopts "" name
45do
46	case $name in
47		?)
48			echo "Usage: $0 <builddir> <srcdir> <swfversion> <swf>" >&2
49			exit 1;;
50	esac
51done
52shift $(($OPTIND - 1))
53if [ "$#" -ne 4 ]
54then
55	echo "Usage: $0 <builddir> <srcdir> <swfversion> <swf>" >&2
56	exit 1
57fi
58
59# Load generation parameters
60top_builddir=$1
61shift
62top_srcdir=$1
63shift
64swfversion=$1
65shift
66swf=$1
67
68# Generate the test runner
69echo "#!/bin/sh"
70echo
71
72echo "# Environment variables"
73env | grep '^GNASH' | while read reply
74do
75	echo "export \"${reply}\""
76done
77
78cat << EOF
79
80# Filenames and constants
81LOGFILE=${top_builddir}/testoutlog.\$\$
82PIPE2CONTAINER=${top_builddir}/tocontainer.\$\$
83PIPE2PLAYER=${top_builddir}/toplayer.\$\$
84READTIMEOUT=5
85
86# Test counts
87TESTED=0
88FAILED=0
89PASSED=0
90
91# check_equals(\$op1, \$op2, \$msg)
92# Equality checker and counter
93check_equals() {
94	if [ "\$1" = "\$2" ]
95	then
96		echo "PASSED: \$3"
97		PASSED=\`expr "\$PASSED" + 1\`
98	else
99		echo "FAILED: \$3 (\"\$1\" != \"\$2\")"
100		FAILED=\`expr "\$FAILED" + 1\`
101	fi
102	TESTED=\`expr "\$TESTED" + 1\`
103}
104
105# xcheck_equals(\$op1, \$op2, \$msg)
106# Equality checker and counter (for expected failure)
107xcheck_equals() {
108	if [ "\$1" = "\$2" ]
109	then
110		echo "XPASSED: \$3"
111		PASSED=\`expr "\$PASSED" + 1\`
112	else
113		echo "XFAILED: \$3 (\"\$1\" != \"\$2\")"
114		FAILED=\`expr "\$FAILED" + 1\`
115	fi
116	TESTED=\`expr "\$TESTED" + 1\`
117}
118
119# check_totals(\$op, \$msg)
120# Test count checker
121check_totals() {
122	check_equals "\$TESTED" "\$1" "\$2"
123}
124
125# check_error(\$bool, \$msg)
126# Assert \$bool is 0; if not, flag error in the test, and exit
127check_error() {
128	if [ "\$1" -ne 0 ]
129	then
130		echo "ERROR: \$2" >&2
131		exit 1
132	fi
133}
134
135# read_timeout(\$varname, \$timeout)
136# Read one line from standard input, with a specified timeout (in seconds)
137read_timeout() {
138	trap 'trap - USR1; return 142' USR1
139	(sleep "\$2" && kill -USR1 "\$\$" > /dev/null 2>&1) &
140	TIMEOUTPID=\$!
141	read "\$1"
142	READERROR=\$?
143	kill "\$TIMEOUTPID" > /dev/null 2>&1
144	trap - USR1
145	return \$READERROR
146}
147
148# Create required named pipes
149if [ \! -p "\$PIPE2CONTAINER" ]
150then
151	mkfifo "\$PIPE2CONTAINER"
152	check_error "\$?" "Failed to create a named pipe: \$PIPE2CONTAINER"
153fi
154if [ \! -p "\$PIPE2PLAYER" ]
155then
156	mkfifo "\$PIPE2PLAYER"
157	check_error "\$?" "Failed to create a named pipe: \$PIPE2PLAYER"
158fi
159
160# Open player-to-host pipe
161exec 3<> "\$PIPE2CONTAINER"
162check_error \$? "Failed to open a named pipe: \$PIPE2CONTAINER"
163
164# Open host-to-player pipe
165exec 4<> "\$PIPE2PLAYER"
166check_error \$? "Failed to open a named pipe: \$PIPE2PLAYER"
167
168# Start player
169"${top_builddir}/gui/gnash" -r 0 -vv -F 3:4 "${swf}" > "\$LOGFILE" 2>&1 &
170GNASHPID=\$!
171
172# Wait until the SWF code finish running, by loop-checking logfile
173STARTCOUNTDOWN=\$READTIMEOUT
174while [ \$STARTCOUNTDOWN -gt 0 ]
175do
176	if grep "TRACE: ENDOFTEST" "\$LOGFILE" 2>&1 > /dev/null
177	then
178		break
179	fi
180	sleep 1
181	STARTCOUNTDOWN=\`expr \$STARTCOUNTDOWN - 1\`
182done
183
184[ \$STARTCOUNTDOWN -ne 0 ]
185check_equals \$? 0 "Gnash-side ActionScript code should be successfully run"
186
187# Call string-returning GetVariable() on string variable
188echo '<invoke name="GetVariable" returntype="xml"><arguments><string>string_variable</string></arguments></invoke>' >&4
189
190# Read for value statement
191read_timeout LINE \$READTIMEOUT <&3
192check_equals "\$LINE" '<string>This is a string</string>' "Gnash should return a correct value from GetVariable call on string"
193
194# Call string-returning GetVariable() on integer variable
195echo '<invoke name="GetVariable" returntype="xml"><arguments><string>integer_variable</string></arguments></invoke>' >&4
196
197# Read for return value statement
198read_timeout LINE \$READTIMEOUT <&3
199check_equals "\$LINE" '<string>9876</string>' "Gnash should return a correct value from GetVariable call on integer"
200
201# Call string-returning GetVariable() on floating-point variable
202echo '<invoke name="GetVariable" returntype="xml"><arguments><string>float_variable</string></arguments></invoke>' >&4
203
204# Read for return value statement
205read_timeout LINE \$READTIMEOUT <&3
206check_equals "\$LINE" '<string>9876.5432</string>' "Gnash should return a correct value from GetVariable call on floating point"
207
208# Call string-returning GetVariable() on positive infinite
209# floating-point variable
210echo '<invoke name="GetVariable" returntype="xml"><arguments><string>infinite_variable</string></arguments></invoke>' >&4
211
212# Read for return value statement
213read_timeout LINE \$READTIMEOUT <&3
214check_equals "\$LINE" '<string>Infinity</string>' "Gnash should return a correct value from GetVariable call on infinity floating point"
215
216# Call string-returning GetVariable() on negative infinite
217# floating-point variable
218echo '<invoke name="GetVariable" returntype="xml"><arguments><string>neginfinite_variable</string></arguments></invoke>' >&4
219
220# Read for return value statement
221read_timeout LINE \$READTIMEOUT <&3
222check_equals "\$LINE" '<string>-Infinity</string>' "Gnash should return a correct value from GetVariable call on negative infinity floating point"
223
224# Call string-returning GetVariable() on non-number floating-point variable
225echo '<invoke name="GetVariable" returntype="xml"><arguments><string>nan_variable</string></arguments></invoke>' >&4
226
227# Read for return value statement
228read_timeout LINE \$READTIMEOUT <&3
229check_equals "\$LINE" '<string>NaN</string>' "Gnash should return a correct value from GetVariable call on non-number floating point"
230
231# Call string-returning GetVariable() on boolean variable
232echo '<invoke name="GetVariable" returntype="xml"><arguments><string>boolean_variable</string></arguments></invoke>' >&4
233
234# Read for return value statement
235read_timeout LINE \$READTIMEOUT <&3
236check_equals "\$LINE" '<string>true</string>' "Gnash should return a correct value from GetVariable call on boolean"
237
238# Call string-returning GetVariable() on null variable
239echo '<invoke name="GetVariable" returntype="xml"><arguments><string>null_variable</string></arguments></invoke>' >&4
240
241# Read for return value statement
242read_timeout LINE \$READTIMEOUT <&3
243check_equals "\$LINE" '<string>null</string>' "Gnash should return a correct value from GetVariable call on null"
244
245# Call string-returning GetVariable() on unassigned variable
246echo '<invoke name="GetVariable" returntype="xml"><arguments><string>unassigned_variable</string></arguments></invoke>' >&4
247
248# Read for return value statement
249read_timeout LINE \$READTIMEOUT <&3
250if [ "${swfversion}" -gt 6 ]
251then
252	check_equals "\$LINE" '<string>undefined</string>' "Gnash should return a correct value from GetVariable call on unassigned variable"
253else
254	check_equals "\$LINE" '<string></string>' "Gnash should return a correct value from GetVariable call on unassigned variable"
255fi
256
257# Call string-returning GetVariable() on variable with undefined value
258echo '<invoke name="GetVariable" returntype="xml"><arguments><string>undefined_variable</string></arguments></invoke>' >&4
259
260# Read for return value statement
261read_timeout LINE \$READTIMEOUT <&3
262if [ "${swfversion}" -gt 6 ]
263then
264	check_equals "\$LINE" '<string>undefined</string>' "Gnash should return a correct value from GetVariable call on variable with undefined value"
265else
266	check_equals "\$LINE" '<string></string>' "Gnash should return a correct value from GetVariable call on variable with undefined value"
267fi
268
269# Call string-returning GetVariable() on non-existent variable
270echo '<invoke name="GetVariable" returntype="xml"><arguments><string>nonexistent_variable</string></arguments></invoke>' >&4
271
272# Read for return value statement
273read_timeout LINE \$READTIMEOUT <&3
274check_equals "\$LINE" '<null/>' "Gnash should return a correct value from GetVariable call on non-existent variable"
275
276# Call string-returning GetVariable() on string array variable
277echo '<invoke name="GetVariable" returntype="xml"><arguments><string>array_variable</string></arguments></invoke>' >&4
278
279# Read for return value statement
280read_timeout LINE \$READTIMEOUT <&3
281check_equals "\$LINE" '<string>The,quick,brown,fox,jumps,over,the,lazy,dog</string>' "Gnash should return a correct value from GetVariable call on array variable"
282
283# Call string-returning GetVariable() on object variable
284echo '<invoke name="GetVariable" returntype="xml"><arguments><string>object_variable</string></arguments></invoke>' >&4
285
286# Read for return value statement
287read_timeout LINE \$READTIMEOUT <&3
288check_equals "\$LINE" '<string>[object Object]</string>' "Gnash should return a correct value from GetVariable call on object variable"
289
290# Call string-returning GetVariable() on object variable
291# with custom toString() method
292echo '<invoke name="GetVariable" returntype="xml"><arguments><string>object_variable_customstring</string></arguments></invoke>' >&4
293
294# Read for return value statement
295read_timeout LINE \$READTIMEOUT <&3
296check_equals "\$LINE" '<string>This is a custom Object.toString()</string>' "Gnash should return a correct value from GetVariable call on object variable with custom toString()"
297
298# Call string-returning GetVariable() on function variable
299echo '<invoke name="GetVariable" returntype="xml"><arguments><string>function_variable</string></arguments></invoke>' >&4
300
301# Read for return value statement
302read_timeout LINE \$READTIMEOUT <&3
303check_equals "\$LINE" '<string>[type Function]</string>' "Gnash should return a correct value from GetVariable call on function variable"
304
305# Close pipes
306exec 3<&-
307exec 4<&-
308
309# Force Gnash to exit
310kill \$GNASHPID
311wait \$GNASHPID
312
313# Check for total number of test run
314check_totals "16" "There should be 16 tests run"
315
316# Remove temporary files
317rm "\$LOGFILE"
318rm "\$PIPE2CONTAINER"
319rm "\$PIPE2PLAYER"
320EOF
321