From: Nutchanon Wetchasit <Nutchanon.Wetchasit@gmail.com>
Subject: Add automated tests on unconventional target values in getURL().
Bug: https://savannah.gnu.org/bugs/?50393

See bug #50393 <https://savannah.gnu.org/bugs/?50393>

diff --git a/.gitignore b/.gitignore
index 5ebc15b..ea49f31 100644
--- a/.gitignore
+++ b/.gitignore
@@ -138,6 +138,7 @@ testsuite/misc-ming.all/*.swf
 testsuite/misc-ming.all/extgetvariable_testrunner_v*
 testsuite/misc-ming.all/hostcmd_testrunner_v*
 testsuite/misc-ming.all/hostcmd_htmltest_v*.html
+testsuite/misc-ming.all/hostcmd-geturl_testrunner_v*
 testsuite/misc-ming.all/XMLSocketTester
 testsuite/misc-ming.all/sound/*.swf
 testsuite/misc-mtasc.all/pp_*.as
diff --git a/testsuite/misc-ming.all/Makefile.am b/testsuite/misc-ming.all/Makefile.am
index 5ef927b..bea287b 100644
--- a/testsuite/misc-ming.all/Makefile.am
+++ b/testsuite/misc-ming.all/Makefile.am
@@ -35,7 +35,9 @@ DISTCLEANFILES = XMLSocketTester extgetvariable_testrunner_v5 \
                  hostcmd_testrunner_v6 hostcmd_testrunner_v7 \
                  hostcmd_testrunner_v8 hostcmd_htmltest_v5.html \
                  hostcmd_htmltest_v6.html hostcmd_htmltest_v7.html \
-                 hostcmd_htmltest_v8.html
+                 hostcmd_htmltest_v8.html hostcmd-geturl_testrunner_v4 \
+                 hostcmd-geturl_testrunner_v5 \ hostcmd-geturl_testrunner_v6 \
+                 hostcmd-geturl_testrunner_v7 \ hostcmd-geturl_testrunner_v8
 
 SUBDIRS = . loop loading displaylist_depths action_order register_class \
           init_action
@@ -80,7 +82,8 @@ EXTRA_DIST = \
 	XMLSocketTester.sh \
 	extgetvariable_testrunner.sh \
 	hostcmd_testrunner.sh \
-	hostcmd_htmltest.sh
+	hostcmd_htmltest.sh \
+	hostcmd-geturl_testrunner.sh
 
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/libbase \
@@ -246,6 +249,11 @@ check_SCRIPTS = \
 	hostcmd_htmltest_v6.html \
 	hostcmd_htmltest_v7.html \
 	hostcmd_htmltest_v8.html \
+	hostcmd-geturl_testrunner_v4 \
+	hostcmd-geturl_testrunner_v5 \
+	hostcmd-geturl_testrunner_v6 \
+	hostcmd-geturl_testrunner_v7 \
+	hostcmd-geturl_testrunner_v8 \
 	timeline_var_test-Runner \
 	place_object_testrunner \
 	place_object_test2runner \
@@ -1391,6 +1399,51 @@ hostcmd_htmltest_v7.html: hostcmd_htmltest.sh hostcmd_v7.swf
 hostcmd_htmltest_v8.html: hostcmd_htmltest.sh hostcmd_v8.swf
 	sh $(srcdir)/hostcmd_htmltest.sh 8 hostcmd_v8.swf > $@
 
+hostcmd-geturl_v4.swf: hostcmd-geturl.as
+	$(MAKESWF) $(MAKESWF_FLAGS) -c -1 -r 1 -v 4 -DOUTPUT_VERSION=4 -o $@ \
+		$(srcdir)/hostcmd-geturl.as
+
+hostcmd-geturl_v5.swf: hostcmd-geturl.as
+	$(MAKESWF) $(MAKESWF_FLAGS) -c -1 -r 1 -v 5 -DOUTPUT_VERSION=5 -o $@ \
+		$(srcdir)/hostcmd-geturl.as
+
+hostcmd-geturl_v6.swf: hostcmd-geturl.as
+	$(MAKESWF) $(MAKESWF_FLAGS) -r 1 -v 6 -DOUTPUT_VERSION=6 -o $@ \
+		$(srcdir)/hostcmd-geturl.as
+
+hostcmd-geturl_v7.swf: hostcmd-geturl.as
+	$(MAKESWF) $(MAKESWF_FLAGS) -r 1 -v 7 -DOUTPUT_VERSION=7 -o $@ \
+		$(srcdir)/hostcmd-geturl.as
+
+hostcmd-geturl_v8.swf: hostcmd-geturl.as
+	$(MAKESWF) $(MAKESWF_FLAGS) -r 1 -v 8 -DOUTPUT_VERSION=8 -o $@ \
+		$(srcdir)/hostcmd-geturl.as
+
+hostcmd-geturl_testrunner_v4: hostcmd-geturl_testrunner.sh hostcmd-geturl_v4.swf
+	sh $(srcdir)/hostcmd-geturl_testrunner.sh $(top_builddir) \
+		$(top_srcdir) 4 hostcmd-geturl_v4.swf > $@
+	chmod 755 $@
+
+hostcmd-geturl_testrunner_v5: hostcmd-geturl_testrunner.sh hostcmd-geturl_v5.swf
+	sh $(srcdir)/hostcmd-geturl_testrunner.sh $(top_builddir) \
+		$(top_srcdir) 5 hostcmd-geturl_v5.swf > $@
+	chmod 755 $@
+
+hostcmd-geturl_testrunner_v6: hostcmd-geturl_testrunner.sh hostcmd-geturl_v6.swf
+	sh $(srcdir)/hostcmd-geturl_testrunner.sh $(top_builddir) \
+		$(top_srcdir) 6 hostcmd-geturl_v6.swf > $@
+	chmod 755 $@
+
+hostcmd-geturl_testrunner_v7: hostcmd-geturl_testrunner.sh hostcmd-geturl_v7.swf
+	sh $(srcdir)/hostcmd-geturl_testrunner.sh $(top_builddir) \
+		$(top_srcdir) 7 hostcmd-geturl_v7.swf > $@
+	chmod 755 $@
+
+hostcmd-geturl_testrunner_v8: hostcmd-geturl_testrunner.sh hostcmd-geturl_v8.swf
+	sh $(srcdir)/hostcmd-geturl_testrunner.sh $(top_builddir) \
+		$(top_srcdir) 8 hostcmd-geturl_v8.swf > $@
+	chmod 755 $@
+
 GradientFillTest.swf: GradientFillTest.as 
 	$(MAKESWF) $(MAKESWF_FLAGS) -v 8 -r 1 -o $@  $(srcdir)/empty.as $(srcdir)/GradientFillTest.as
 
@@ -1662,6 +1715,11 @@ TEST_CASES = \
 	hostcmd_testrunner_v6 \
 	hostcmd_testrunner_v7 \
 	hostcmd_testrunner_v8 \
+	hostcmd-geturl_testrunner_v4 \
+	hostcmd-geturl_testrunner_v5 \
+	hostcmd-geturl_testrunner_v6 \
+	hostcmd-geturl_testrunner_v7 \
+	hostcmd-geturl_testrunner_v8 \
 	DrawingApiTestRunner \
 	TextSnapshotTest-Runner \
 	reverse_execute_PlaceObject2_test1runner \
diff --git a/testsuite/misc-ming.all/hostcmd-geturl.as b/testsuite/misc-ming.all/hostcmd-geturl.as
new file mode 100644
index 0000000..f8e1f54
--- /dev/null
+++ b/testsuite/misc-ming.all/hostcmd-geturl.as
@@ -0,0 +1,51 @@
+// hostcmd-geturl.as - getURL()/MovieClip.getURL() target tests
+//
+//   Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+//
+// Note:
+//     This SWF file does not run any test by itself as this is a test on
+//     plugin's host interface.
+//
+// Original author: Nutchanon Wetchasit <Nutchanon.Wetchasit@gmail.com>
+//
+
+// Once this Flash movie is run, it will issue `getURL()` call to host
+// container via a built-in `getURL(url,target)` function and
+// via `MovieClip.getURL(url,target)` with unconventional `target` values;
+// each issued call will be checked for correctness by the emulated
+// host container.
+//
+// In real browser environment, this test will not run in a useful manner
+// as the early `getURL()` call will redirect test page to other file
+// before the later tests could run.
+
+trace("STARTOFTEST");
+
+getURL("geturl-emptytarget.html", "");
+
+#if OUTPUT_VERSION >= 5
+getURL("geturl-notarget.html");
+getURL("geturl-undeftarget.html", undefined);
+getURL("geturl-nulltarget.html", null);
+
+this.getURL("mcgeturl-emptytarget.html", "");
+this.getURL("mcgeturl-notarget.html");
+this.getURL("mcgeturl-undeftarget.html", undefined);
+this.getURL("mcgeturl-nulltarget.html", null);
+#endif
+
+trace("ENDOFTEST");
diff --git a/testsuite/misc-ming.all/hostcmd-geturl_testrunner.sh b/testsuite/misc-ming.all/hostcmd-geturl_testrunner.sh
new file mode 100644
index 0000000..1b268a5
--- /dev/null
+++ b/testsuite/misc-ming.all/hostcmd-geturl_testrunner.sh
@@ -0,0 +1,262 @@
+#!/bin/sh
+
+#
+# hostcmd-geturl_testrunner.sh - container-emulated getURL()/MovieClip.getURL()
+#                                target test runner generator
+#
+# Copyright (C) 2017 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+#
+# Original author: Nutchanon Wetchasit <Nutchanon.Wetchasit@gmail.com>
+#
+# The generated test runner checks Gnash for:
+#  * Undefined and null target passing via getURL() and MovieClip.getURL()
+#    (bug #50393)
+#        <https://savannah.gnu.org/bugs/?50393>
+#
+# Usage:
+#     ./hostcmd-geturl_testrunner.sh <builddir> <srcdir> <swfversion> <swf>
+#
+# Generated test runner's exit codes:
+#     0         if tester ran completely
+#     non-zero  if tester encountered an error
+#
+# Note:
+#     The generated test file requires a filesystem that supports named pipes.
+#
+
+# Check for generation parameters
+while getopts "" name
+do
+	case $name in
+		?)
+			echo "Usage: $0 <builddir> <srcdir> <swfversion> <swf>" >&2
+			exit 1;;
+	esac
+done
+shift $(($OPTIND - 1))
+if [ "$#" -ne 4 ]
+then
+	echo "Usage: $0 <builddir> <srcdir> <swfversion> <swf>" >&2
+	exit 1
+fi
+
+# Load generation parameters
+top_builddir=$1
+shift
+top_srcdir=$1
+shift
+swfversion=$1
+shift
+swf=$1
+
+# Generate the test runner
+echo "#!/bin/sh"
+echo
+
+echo "# Environment variables"
+env | grep '^GNASH' | while read reply
+do
+	echo "export \"${reply}\""
+done
+
+cat << EOF
+
+# Filenames and constants
+LOGFILE=${top_builddir}/testoutlog.\$\$
+PIPE2CONTAINER=${top_builddir}/tocontainer.\$\$
+PIPE2PLAYER=${top_builddir}/toplayer.\$\$
+STARTTIMEOUT=10
+READTIMEOUT=5
+
+# Test counts
+TESTED=0
+FAILED=0
+PASSED=0
+
+# check_equals(\$op1, \$op2, \$msg)
+# Equality checker and counter
+check_equals() {
+	if [ "\$1" = "\$2" ]
+	then
+		echo "PASSED: \$3"
+		PASSED=\`expr "\$PASSED" + 1\`
+	else
+		echo "FAILED: \$3 (\"\$1\" != \"\$2\")"
+		FAILED=\`expr "\$FAILED" + 1\`
+	fi
+	TESTED=\`expr "\$TESTED" + 1\`
+}
+
+# xcheck_equals(\$op1, \$op2, \$msg)
+# Equality checker and counter (for expected failure)
+xcheck_equals() {
+	if [ "\$1" = "\$2" ]
+	then
+		echo "XPASSED: \$3"
+		PASSED=\`expr "\$PASSED" + 1\`
+	else
+		echo "XFAILED: \$3 (\"\$1\" != \"\$2\")"
+		FAILED=\`expr "\$FAILED" + 1\`
+	fi
+	TESTED=\`expr "\$TESTED" + 1\`
+}
+
+# check_totals(\$op, \$msg)
+# Test count checker
+check_totals() {
+	check_equals "\$TESTED" "\$1" "\$2"
+}
+
+# check_error(\$bool, \$msg)
+# Assert \$bool is 0; if not, flag error in the test, and exit
+check_error() {
+	if [ "\$1" -ne 0 ]
+	then
+		echo "ERROR: \$2" >&2
+		exit 1
+	fi
+}
+
+# read_timeout(\$varname, \$timeout)
+# Read one line from standard input, with a specified timeout (in seconds)
+read_timeout() {
+	trap 'trap - USR1; return 142' USR1
+	(sleep "\$2" && kill -USR1 "\$\$" > /dev/null 2>&1) &
+	TIMEOUTPID=\$!
+	read "\$1"
+	READERROR=\$?
+	kill "\$TIMEOUTPID" > /dev/null 2>&1
+	trap - USR1
+	return \$READERROR
+}
+
+# Create required named pipes
+if [ \! -p "\$PIPE2CONTAINER" ]
+then
+	mkfifo "\$PIPE2CONTAINER"
+	check_error "\$?" "Failed to create a named pipe: \$PIPE2CONTAINER"
+fi
+if [ \! -p "\$PIPE2PLAYER" ]
+then
+	mkfifo "\$PIPE2PLAYER"
+	check_error "\$?" "Failed to create a named pipe: \$PIPE2PLAYER"
+fi
+
+# Open player-to-host pipe
+exec 3<> "\$PIPE2CONTAINER"
+check_error \$? "Failed to open a named pipe: \$PIPE2CONTAINER"
+
+# Open host-to-player pipe
+exec 4<> "\$PIPE2PLAYER"
+check_error \$? "Failed to open a named pipe: \$PIPE2PLAYER"
+
+# Start player
+"${top_builddir}/gui/gnash" -r 0 -vv -F 3:4 "${swf}" > "\$LOGFILE" 2>&1 &
+GNASHPID=\$!
+
+# Wait until the SWF code start running, by loop-checking logfile
+STARTCOUNTDOWN=\$STARTTIMEOUT
+while [ \$STARTCOUNTDOWN -gt 0 ]
+do
+	if grep "TRACE: STARTOFTEST" "\$LOGFILE" 2>&1 > /dev/null
+	then
+		break
+	fi
+	sleep 1
+	STARTCOUNTDOWN=\`expr \$STARTCOUNTDOWN - 1\`
+done
+
+[ \$STARTCOUNTDOWN -ne 0 ]
+check_equals \$? 0 "Gnash-side ActionScript code should be successfully started"
+
+#
+# Built-in getURL target tests
+#
+
+# Read for empty-target getURL statement
+read_timeout LINE \$READTIMEOUT <&3
+check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>geturl-emptytarget.html</string><string>GET</string></arguments></invoke>' "Gnash should correctly pass built-in getURL call with empty target"
+
+if [ $swfversion -ge 5 ]
+then
+	# Read for no-target getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>geturl-notarget.html</string><string>GET</string></arguments></invoke>' "Gnash should correctly pass built-in getURL call with no target"
+
+	if [ $swfversion -ge 7 ]
+	then
+		# Read for undefined-target getURL statement (SWF7-above form)
+		read_timeout LINE \$READTIMEOUT <&3
+		check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>geturl-undeftarget.html</string><string>GET</string><string>undefined</string></arguments></invoke>' "Gnash should correctly pass built-in getURL call with undefined target"
+	else
+		# Read for undefined-target getURL statement (SWF6-below form)
+		read_timeout LINE \$READTIMEOUT <&3
+		check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>geturl-undeftarget.html</string><string>GET</string></arguments></invoke>' "Gnash should correctly pass built-in getURL call with undefined target"
+	fi
+
+	# Read for null-target getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>geturl-nulltarget.html</string><string>GET</string><string>null</string></arguments></invoke>' "Gnash should correctly pass built-in getURL call with null target"
+
+	#
+	# MovieClip-based getURL() target tests
+	#
+
+	# Read for empty-target MovieClip-based getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" "<invoke name=\"getURL\" returntype=\"xml\"><arguments><string>mcgeturl-emptytarget.html</string><string>GET</string></arguments></invoke>" "Gnash should correctly pass MovieClip-based getURL call with empty target"
+
+	# Read for no-target MovieClip-based getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>mcgeturl-notarget.html</string><string>GET</string></arguments></invoke>' "Gnash should correctly pass MovieClip-based getURL call with no target"
+
+	# Read for undefined-target MovieClip-based getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" '<invoke name="getURL" returntype="xml"><arguments><string>mcgeturl-undeftarget.html</string><string>GET</string></arguments></invoke>' "Gnash should correctly pass MovieClip-based getURL call with undefined target"
+
+	# Read for null-target MovieClip-based getURL statement
+	read_timeout LINE \$READTIMEOUT <&3
+	check_equals "\$LINE" "<invoke name=\"getURL\" returntype=\"xml\"><arguments><string>mcgeturl-nulltarget.html</string><string>GET</string><string>null</string></arguments></invoke>" "Gnash should correctly pass MovieClip-based getURL call with null target"
+fi
+
+# Close pipes
+exec 3<&-
+exec 4<&-
+
+# Force Gnash to exit
+kill \$GNASHPID
+wait \$GNASHPID
+
+# Show player-side output
+exec 5< "\$LOGFILE"
+cat <&5
+exec 5<&-
+
+# Check for total number of test run
+if [ $swfversion -ge 5 ]
+then
+	check_totals "9" "There should be 9 tests run"
+else
+	check_totals "2" "There should be 2 tests run"
+fi
+
+# Remove temporary files
+rm "\$LOGFILE"
+rm "\$PIPE2CONTAINER"
+rm "\$PIPE2PLAYER"
+EOF
