17c2fbfb3SApril Chin#
27c2fbfb3SApril Chin# CDDL HEADER START
37c2fbfb3SApril Chin#
47c2fbfb3SApril Chin# The contents of this file are subject to the terms of the
57c2fbfb3SApril Chin# Common Development and Distribution License (the "License").
67c2fbfb3SApril Chin# You may not use this file except in compliance with the License.
77c2fbfb3SApril Chin#
87c2fbfb3SApril Chin# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c2fbfb3SApril Chin# or http://www.opensolaris.org/os/licensing.
107c2fbfb3SApril Chin# See the License for the specific language governing permissions
117c2fbfb3SApril Chin# and limitations under the License.
127c2fbfb3SApril Chin#
137c2fbfb3SApril Chin# When distributing Covered Code, include this CDDL HEADER in each
147c2fbfb3SApril Chin# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c2fbfb3SApril Chin# If applicable, add the following below this CDDL HEADER, with the
167c2fbfb3SApril Chin# fields enclosed by brackets "[]" replaced with your own identifying
177c2fbfb3SApril Chin# information: Portions Copyright [yyyy] [name of copyright owner]
187c2fbfb3SApril Chin#
197c2fbfb3SApril Chin# CDDL HEADER END
207c2fbfb3SApril Chin#
217c2fbfb3SApril Chin
227c2fbfb3SApril Chin#
233e14f97fSRoger A. Faulkner# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
247c2fbfb3SApril Chin#
257c2fbfb3SApril Chin
267c2fbfb3SApril Chin#
277c2fbfb3SApril Chin# Test whether CR #6766246 ("bug in pattern matching") has been fixed.
28*b30d1939SAndy Fiddaman#
29*b30d1939SAndy Fiddaman# Quote from CR #6766246:
307c2fbfb3SApril Chin# ---- snip ----
317c2fbfb3SApril Chin# The bootstrap script of pkgsrc contains this code
327c2fbfb3SApril Chin# checkarg_sane_absolute_path() {
337c2fbfb3SApril Chin#   case "$1" in
347c2fbfb3SApril Chin#     "") ;; # the default value will be used.
357c2fbfb3SApril Chin#     *[!-A-Za-z0-9_./]*)
367c2fbfb3SApril Chin#       die "ERROR: Invalid characters in path $1 (from $2)." ;;
377c2fbfb3SApril Chin#     /*) ;;
387c2fbfb3SApril Chin#     *) die "ERROR: The argument to $2 must be an absolute path." ;;
397c2fbfb3SApril Chin#   esac
407c2fbfb3SApril Chin# }
417c2fbfb3SApril Chin# It turns out, the leading "!" in the pattern is not interpreted
427c2fbfb3SApril Chin# as negation, and the first "-" not as a literal. Instead the
437c2fbfb3SApril Chin# character range  "! to A" is constructed. Paths containing "%"
447c2fbfb3SApril Chin# or "@" are accepted, but paths containing "-" are rejected.
457c2fbfb3SApril Chin# Note that this interpretation makes the whole pattern
467c2fbfb3SApril Chin# syntactically wrong, which isn't noticed either.
47*b30d1939SAndy Fiddaman#
487c2fbfb3SApril Chin# Test case:
497c2fbfb3SApril Chin# -- snip --
507c2fbfb3SApril Chin# !/bin/sh
517c2fbfb3SApril Chin# case "$1" in
527c2fbfb3SApril Chin# *[!-A-Za-z0-9_./]*)
537c2fbfb3SApril Chin#         echo invalid characters used in $1
547c2fbfb3SApril Chin#         ;;
557c2fbfb3SApril Chin# *)
567c2fbfb3SApril Chin#         echo only valid characters used in $1
577c2fbfb3SApril Chin#         ;;
587c2fbfb3SApril Chin# esac
597c2fbfb3SApril Chin# -- snip --
607c2fbfb3SApril Chin# Expected Result:
617c2fbfb3SApril Chin#    strings containing a "-" should be accepted, strings containing
627c2fbfb3SApril Chin#    a "@" should be rejected
637c2fbfb3SApril Chin# Actual Result:
647c2fbfb3SApril Chin#    strings containing a "-" are rejected, strings containing a
657c2fbfb3SApril Chin#    "@" are accepted
667c2fbfb3SApril Chin# Workaround
677c2fbfb3SApril Chin#    The pattern "*[!A-Za-z0-9_./-]*" (i.e. shifting the dash to
687c2fbfb3SApril Chin#    the end) works as expected.
697c2fbfb3SApril Chin# ---- snip ----
707c2fbfb3SApril Chin
717c2fbfb3SApril Chin
7234f9b3eeSRoland Mainz# test setup
737c2fbfb3SApril Chinfunction err_exit
747c2fbfb3SApril Chin{
757c2fbfb3SApril Chin	print -u2 -n "\t"
767c2fbfb3SApril Chin	print -u2 -r ${Command}[$1]: "${@:2}"
773e14f97fSRoger A. Faulkner	(( Errors < 127 && Errors++ ))
787c2fbfb3SApril Chin}
797c2fbfb3SApril Chinalias err_exit='err_exit $LINENO'
807c2fbfb3SApril Chin
8134f9b3eeSRoland Mainzset -o nounset
8234f9b3eeSRoland MainzCommand=${0##*/}
837c2fbfb3SApril Chininteger Errors=0
847c2fbfb3SApril Chin
857c2fbfb3SApril Chin
867c2fbfb3SApril Chin## test 1 (based on the bug report):
877c2fbfb3SApril Chin
887c2fbfb3SApril Chinfunction do_match
897c2fbfb3SApril Chin{
907c2fbfb3SApril Chin	case "$1" in
917c2fbfb3SApril Chin		*[!-A-Za-z0-9_./]*)
927c2fbfb3SApril Chin			print "match"
937c2fbfb3SApril Chin			;;
947c2fbfb3SApril Chin		*)
957c2fbfb3SApril Chin			print "nomatch"
967c2fbfb3SApril Chin			;;
977c2fbfb3SApril Chin	esac
987c2fbfb3SApril Chin	return 0
997c2fbfb3SApril Chin}
1007c2fbfb3SApril Chin
1017c2fbfb3SApril Chintypeset pat
1027c2fbfb3SApril Chin
1037c2fbfb3SApril Chinpat="foo-bar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1047c2fbfb3SApril Chinpat="foo+bar" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1057c2fbfb3SApril Chinpat="foo/bar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1067c2fbfb3SApril Chinpat="foo_bar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1077c2fbfb3SApril Chinpat="foo@bar" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1087c2fbfb3SApril Chinpat="foobar-" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1097c2fbfb3SApril Chinpat="foobar+" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1107c2fbfb3SApril Chinpat="foobar/" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1117c2fbfb3SApril Chinpat="foobar_" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1127c2fbfb3SApril Chinpat="foobar@" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1137c2fbfb3SApril Chinpat="-foobar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1147c2fbfb3SApril Chinpat="+foobar" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1157c2fbfb3SApril Chinpat="/foobar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1167c2fbfb3SApril Chinpat="_foobar" ; [[ "$(do_match "${pat}")" == "nomatch" ]] || err_exit "${pat} matched."
1177c2fbfb3SApril Chinpat="@foobar" ; [[ "$(do_match "${pat}")" == "match"   ]] || err_exit "${pat} not matched."
1187c2fbfb3SApril Chin
1197c2fbfb3SApril Chin
1207c2fbfb3SApril Chin## test 2 (gsf's test chain):
1217c2fbfb3SApril Chin
1227c2fbfb3SApril Chin# Make sure LC_COLLATE has a value
123*b30d1939SAndy Fiddamanif [[ ! -v LC_COLLATE ]] ; then
124*b30d1939SAndy Fiddaman	if [[ -v LANG && ! -v LC_ALL ]]; then
1257c2fbfb3SApril Chin		LC_COLLATE="${LANG}"
1267c2fbfb3SApril Chin	fi
1277c2fbfb3SApril Chinfi
1287c2fbfb3SApril Chin
129*b30d1939SAndy Fiddamanif [[ -v LC_ALL ]] ; then
1307c2fbfb3SApril Chin	LC_COLLATE="${LC_ALL}"
1317c2fbfb3SApril Chinfi
1327c2fbfb3SApril Chin
133*b30d1939SAndy Fiddaman[[ -v LC_COLLATE ]] || LC_COLLATE=C
1347c2fbfb3SApril Chin
1357c2fbfb3SApril Chinset -- \
1367c2fbfb3SApril Chin        'A'   0 1 1   0 1 1      1 0 0   1 0 0   \
1377c2fbfb3SApril Chin        'Z'   0 1 1   0 1 1      1 0 0   1 0 0   \
1387c2fbfb3SApril Chin        '/'   0 0 0   0 0 0      1 1 1   1 1 1   \
1397c2fbfb3SApril Chin        '.'   0 0 0   0 0 0      1 1 1   1 1 1   \
1407c2fbfb3SApril Chin        '_'   0 0 0   0 0 0      1 1 1   1 1 1   \
1417c2fbfb3SApril Chin        '-'   1 1 1   1 1 1      0 0 0   0 0 0   \
1427c2fbfb3SApril Chin        '%'   0 0 0   0 0 0      1 1 1   1 1 1   \
1437c2fbfb3SApril Chin        '@'   0 0 0   0 0 0      1 1 1   1 1 1   \
1447c2fbfb3SApril Chin        '!'   0 0 0   0 0 0      1 1 1   1 1 1   \
1457c2fbfb3SApril Chin        '^'   0 0 0   0 0 0      1 1 1   1 1 1   \
1467c2fbfb3SApril Chin        # retain this line #
1477c2fbfb3SApril Chinwhile (( $# >= 13 )) ; do
1487c2fbfb3SApril Chin	c=$1
1497c2fbfb3SApril Chin	shift
1507c2fbfb3SApril Chin	for p in \
1517c2fbfb3SApril Chin		'[![.-.]]' \
1527c2fbfb3SApril Chin		'[![.-.][:upper:]]' \
1537c2fbfb3SApril Chin		'[![.-.]A-Z]' \
1547c2fbfb3SApril Chin		'[!-]' \
1557c2fbfb3SApril Chin		'[!-[:upper:]]' \
1567c2fbfb3SApril Chin		'[!-A-Z]' \
1577c2fbfb3SApril Chin		'[[.-.]]' \
1587c2fbfb3SApril Chin		'[[.-.][:upper:]]' \
1597c2fbfb3SApril Chin		'[[.-.]A-Z]' \
1607c2fbfb3SApril Chin		'[-]' \
1617c2fbfb3SApril Chin		'[-[:upper:]]' \
1627c2fbfb3SApril Chin		'[-A-Z]' \
1637c2fbfb3SApril Chin		# retain this line #
1647c2fbfb3SApril Chin	do      e=$1
1657c2fbfb3SApril Chin		shift
1667c2fbfb3SApril Chin		[[ $c == $p ]]
1677c2fbfb3SApril Chin		g=$?
1687c2fbfb3SApril Chin		[[ $g == $e ]] || err_exit "[[ '$c' == $p ]] for LC_COLLATE=$l failed -- expected $e, got $g"
1697c2fbfb3SApril Chin	done
1707c2fbfb3SApril Chindone
1717c2fbfb3SApril Chin
17234f9b3eeSRoland Mainz
1737c2fbfb3SApril Chin# tests done
1747c2fbfb3SApril Chinexit $((Errors))
175