diff options
Diffstat (limited to 'tests/printf')
-rwxr-xr-x | tests/printf/printf-cov.pl | 106 | ||||
-rwxr-xr-x | tests/printf/printf-hex.sh | 29 | ||||
-rwxr-xr-x | tests/printf/printf-mb.sh | 64 | ||||
-rwxr-xr-x | tests/printf/printf-quote.sh | 57 | ||||
-rwxr-xr-x | tests/printf/printf-surprise.sh | 98 | ||||
-rwxr-xr-x | tests/printf/printf.sh | 128 |
6 files changed, 482 insertions, 0 deletions
diff --git a/tests/printf/printf-cov.pl b/tests/printf/printf-cov.pl new file mode 100755 index 0000000..988c2a7 --- /dev/null +++ b/tests/printf/printf-cov.pl @@ -0,0 +1,106 @@ +#!/usr/bin/perl +# improve printf.c test coverage + +# Copyright (C) 2008-2023 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, see <https://www.gnu.org/licenses/>. + +use strict; + +my $prog = 'printf'; +my $try = "Try '$prog --help' for more information.\n"; +my $pow_2_31 = 2**31; + +# Turn off localization of executable's output. +@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; + +my @Tests = +( + ['no-args', {EXIT=>1}, {ERR=>"$prog: missing operand\n$try"}], + ['no-arg2', '--', {EXIT=>1}, {ERR=>"$prog: missing operand\n$try"}], + ['escape-1', q('\a\b\f\n\r\t\v\z\c'), {OUT=>"\a\b\f\n\r\t\x0b\\z"}], + ['hex-ucX', '%X 999', {OUT=>"3E7"}], + ['hex-ucXw', '%4X 999', {OUT=>" 3E7"}], + ['hex-ucXp', '%.4X 999', {OUT=>"03E7"}], + ['hex-ucXwp', '%5.4X 999', {OUT=>" 03E7"}], + ['hex-vw', '%*X 4 42', {OUT=>" 2A"}], + ['hex-vp', '%.*X 4 42', {OUT=>"002A"}], + ['hex-vwvp', '%*.*X 3 2 15', {OUT=>" 0F"}], + ['b', q('nl\ntab\tx'), {OUT=>"nl\ntab\tx"}], + ['c1', '%c 123', {OUT=>"1"}], + ['cw', '%\*c 3 123', {OUT=>" 1"}], + ['d-ucXwp', '%5.4d 999', {OUT=>" 0999"}], + ['d-vw', '%*d 4 42', {OUT=>" 42"}], + ['d-vp', '%.*d 4 42', {OUT=>"0042"}], + ['d-vwvp', '%*.*d 3 2 15', {OUT=>" 15"}], + ['d-neg-prec', '%.*d -3 15', {OUT=>"15"}], + ['d-big-prec', "%.*d $pow_2_31 15", # INT_MAX + {EXIT=>1}, {ERR=>"$prog: invalid precision: '$pow_2_31'\n"}], + ['d-big-fwidth', "%*d $pow_2_31 15", # INT_MAX + {EXIT=>1}, {ERR=>"$prog: invalid field width: '$pow_2_31'\n"}], + ['F', '%F 1', {OUT=>"1.000000"}], + ['LF', '%LF 1', {OUT=>"1.000000"}], + ['E', '%E 2', {OUT=>"2.000000E+00"}], + ['LE', '%LE 2', {OUT=>"2.000000E+00"}], + ['s', '%s x', {OUT=>"x"}], + ['sw', '%\*s 2 x', {OUT=>" x"}], + ['sp', '%.\*s 2 abcd', {OUT=>"ab"}], + ['swp', '%\*.\*s 2 2 abcd', {OUT=>"ab"}], + ['sw-no-args', '%\*s'], + ['sw-no-args2', '%.\*s'], + ['G-ucXwp', '%5.4G 3', {OUT=>" 3"}], + ['G-vw', '%*G 4 42', {OUT=>" 42"}], + ['G-vp', '%.*G 4 42', {OUT=>"42"}], + ['G-vwvp', '%*.*G 5 3 15', {OUT=>" 15"}], + ['esc', q('\xaa\0377'), {OUT=>"\xaa\0377"}], + ['esc-bad-hex', q('\x'), {EXIT=>1}, + {ERR=>"$prog: missing hexadecimal number in escape\n"}], + ['u-bad-hex', q('\u00'), {EXIT=>1}, + {ERR=>"$prog: missing hexadecimal number in escape\n"}], + ['U-bad-hex', q('\U0000'), {EXIT=>1}, + {ERR=>"$prog: missing hexadecimal number in escape\n"}], + ['u4', q('\u0030'), {OUT=>"0"}], + ['U8', q('\U00000030'), {OUT=>"0"}], + ['u-invalid', q('\ud800'), {EXIT=>1}, + {ERR=>"$prog: invalid universal character name \\ud800\n"}], + ['u-missing', q('\u'), {EXIT=>1}, + {ERR=>"$prog: missing hexadecimal number in escape\n"}], + ['d-invalid', '%d no-num', {OUT=>'0'}, {EXIT=>1}, + # Depending on the strtol implementation we expect one of these: + # no-num: Invalid argument (FreeBSD6) + # no-num: expected a numeric value (glibc, Solaris 10) + {ERR_SUBST => 's/Invalid argument$/expected a numeric value/'}, + {ERR=>"$prog: 'no-num': expected a numeric value\n"}], + ['d-bad-suffix', '%d 9z', {OUT=>'9'}, {EXIT=>1}, + {ERR=>"$prog: '9z': value not completely converted\n"}], + ['d-out-of-range', '%d '.('9'x30), {EXIT=>1}, + {OUT=>"inaccurate"}, {OUT_SUBST => 's/\d+/inaccurate/'}, + {ERR=>"$prog: 9...9\n"}, {ERR_SUBST => "s/'9+.*/9...9/"}], + ['excess', 'B 1', {OUT=>'B'}, + {ERR=>"$prog: warning: ignoring excess arguments, starting with '1'\n"}], + ['percent', '%%', {OUT=>'%'}], + ['d-sp', q('% d' 33), {OUT=>' 33'}], + ['d-plus', q('%+d' 33), {OUT=>'+33'}], + ['d-minus', q('%-d' 33), {OUT=> '33'}], + ['d-zero', q('%02d' 1), {OUT=> '01'}], + ['d-quote', q("%'d" 3333), {OUT=> '3333'}, {OUT_SUBST => 'tr/3//c'}], + ['d-hash', q("%#d" 3333), {EXIT=>1}, + {ERR=>"$prog: %#d: invalid conversion specification\n"}], +); + +my $save_temps = $ENV{DEBUG}; +my $verbose = $ENV{VERBOSE}; + +my $fail = run_tests ($prog, \$prog, \@Tests, $save_temps, $verbose); +exit $fail; diff --git a/tests/printf/printf-hex.sh b/tests/printf/printf-hex.sh new file mode 100755 index 0000000..3aa9472 --- /dev/null +++ b/tests/printf/printf-hex.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# make sure that only two hex. digits are consumed in a \xHHH sequence + +# Copyright (C) 2002-2023 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, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ printf + +env printf '\x7e3\n' > out || fail=1 +cat <<\EOF > exp +~3 +EOF + +compare exp out || fail=1 + +Exit $fail diff --git a/tests/printf/printf-mb.sh b/tests/printf/printf-mb.sh new file mode 100755 index 0000000..418ad35 --- /dev/null +++ b/tests/printf/printf-mb.sh @@ -0,0 +1,64 @@ +#!/bin/sh +# tests for printing multi-byte values of characters + +# Copyright (C) 2022-2023 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, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ printf + +prog='env printf' + +unset LC_ALL +f=$LOCALE_FR_UTF8 +: ${LOCALE_FR_UTF8=none} +if test "$LOCALE_FR_UTF8" != "none"; then + ( + #valid multi-byte + LC_ALL=$f $prog '%04x\n' '"á' >>out 2>>err + #invalid multi-byte + LC_ALL=$f $prog '%04x\n' "'$($prog '\xe1')" >>out 2>>err + #uni-byte + LC_ALL=C $prog '%04x\n' "'$($prog '\xe1')" >>out 2>>err + #valid multi-byte, with trailing + LC_ALL=$f $prog '%04x\n' '"á=' >>out 2>>err + #invalid multi-byte, with trailing + LC_ALL=$f $prog '%04x\n' "'$($prog '\xe1')=" >>out 2>>err + ) + cat <<\EOF > exp || framework_failure_ +00e1 +00e1 +00e1 +00e1 +00e1 +EOF + compare exp out || fail=1 + + # Disparate LC_CTYPE and LC_MESSAGES problematic on macos, + # so just look for character in warning message, + # and normalize to LC_MESSAGES=C + C_WARNING='printf: '\ +'warning: =: character(s) following character constant have been ignored' + + sed "s/printf:.*=.*/$C_WARNING/" < err > c_err || framework_failure_ + + cat <<EOF > exp_err +$C_WARNING +$C_WARNING +EOF + compare exp_err c_err || fail=1 +fi + +Exit $fail diff --git a/tests/printf/printf-quote.sh b/tests/printf/printf-quote.sh new file mode 100755 index 0000000..d1671bd --- /dev/null +++ b/tests/printf/printf-quote.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# tests for printf %q + +# Copyright (C) 2015-2023 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, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ printf + +prog='env printf' + +# Equivalent output to ls --quoting=shell-escape +$prog '%q\n' '' "'" a 'a b' '~a' 'a~' "$($prog %b 'a\r')" > out +cat <<\EOF > exp || framework_failure_ +'' +"'" +a +'a b' +'~a' +a~ +'a'$'\r' +EOF +compare exp out || fail=1 + +unset LC_ALL +f=$LOCALE_FR_UTF8 +: ${LOCALE_FR_UTF8=none} +if test "$LOCALE_FR_UTF8" != "none"; then + ( + #printable multi-byte + LC_ALL=$f $prog '%q\n' 'áḃç' > out + #non-printable multi-byte + LC_ALL=$f $prog '%q\n' "$($prog '\xc2\x81')" >> out + #printable multi-byte in C locale + LC_ALL=C $prog '%q\n' 'áḃç' >> out + ) + cat <<\EOF > exp || framework_failure_ +áḃç +''$'\302\201' +''$'\303\241\341\270\203\303\247' +EOF + compare exp out || fail=1 +fi + +Exit $fail diff --git a/tests/printf/printf-surprise.sh b/tests/printf/printf-surprise.sh new file mode 100755 index 0000000..fc3c064 --- /dev/null +++ b/tests/printf/printf-surprise.sh @@ -0,0 +1,98 @@ +#!/bin/sh +# Detect printf(3) failure even when it doesn't set stream error indicator + +# Copyright (C) 2007-2023 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, see <https://www.gnu.org/licenses/>. + +prog=printf + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ printf + +vm=$(get_min_ulimit_v_ env $prog %20f 0) \ + || skip_ "this shell lacks ulimit support" + +# Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal +# error from glibc's printf(3) function whenever N was large relative to +# the size of available memory. As of Oct 2007, that internal stream- +# related failure was not reflected (for any libc I know of) in the usual +# stream error indicator that is tested by ferror. The result was that +# while the printf command obviously failed (generated no output), +# it mistakenly exited successfully (exit status of 0). + +# Testing it is tricky, because there is so much variance +# in quality for this corner of printf(3) implementations. +# Most implementations do attempt to allocate N bytes of storage. +# Using the maximum value for N (2^31-1) causes glibc-2.7 to try to +# allocate almost 2^64 bytes, while freeBSD 6.1's implementation +# correctly outputs almost 2GB worth of 0's, which takes too long. +# We want to test implementations that allocate N bytes, but without +# triggering the above extremes. + +# Some other versions of glibc-2.7 have a snprintf function that segfaults +# when an internal (technically unnecessary!) memory allocation fails. + +# The compromise is to limit virtual memory to something reasonable, +# and to make an N-byte-allocating-printf require more than that, thus +# triggering the printf(3) misbehavior -- which, btw, is required by ISO C99. + +mkfifo_or_skip_ fifo +trap_sigpipe_or_skip_ + +# Disable MALLOC_PERTURB_, to avoid triggering this bug +# https://bugs.debian.org/481543#77 +export MALLOC_PERTURB_=0 + +# Terminate any background process +cleanup_() { kill $pid 2>/dev/null && wait $pid; } + +head -c 10 fifo > out & pid=$! + +# Trigger large mem allocation failure +( trap '' PIPE && ulimit -v $vm && env $prog %20000000f 0 2>err-msg > fifo ) +exit=$? + +# Map this longer, and rarer, diagnostic to the common one. +# printf: cannot perform formatted output: Cannot allocate memory" +sed 's/cannot perform .*/write error/' err-msg > k && mv k err-msg +err_msg=$(tr '\n' : < err-msg) + +# By some bug, on Solaris 11 (5.11 snv_86), err_msg ends up +# containing '1> fifo:printf: write error:'. Recognize that, too. + +case $err_msg in + "$prog: write error:"*) diagnostic=y ;; + "1> fifo:$prog: write error:") diagnostic=y ;; + '') diagnostic=n ;; + *) diagnostic=unexpected ;; +esac +n_out=$(wc -c < out) + +case $n_out:$diagnostic:$exit in + 10:n:0) ;; # ok, succeeds w/no diagnostic: FreeBSD 6.1 + 10:y:1) ;; # ok, fails with EPIPE diagnostic: musl libc + 0:y:1) ;; # ok, glibc-2.8 and newer, when printf(3) fails with ENOMEM + + # With MALLOC_PERTURB_=0, this no longer happens. + # *:139) # segfault; known bug at least in debian unstable's libc6 2.7-11 + # echo 1>&2 "$0: bug in snprintf causes low-mem use of printf to segfault" + # fail=77;; + + # 10:y) ;; # Fail: doesn't happen: nobody succeeds with a diagnostic + # 0:n) ;; # Fail pre-patch: no output, no diag + *) fail=1;; +esac + +Exit $fail diff --git a/tests/printf/printf.sh b/tests/printf/printf.sh new file mode 100755 index 0000000..834012c --- /dev/null +++ b/tests/printf/printf.sh @@ -0,0 +1,128 @@ +#!/bin/sh +# basic tests for printf + +# Copyright (C) 2002-2023 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, see <https://www.gnu.org/licenses/>. + +prog='env printf' + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ printf + +getlimits_ + + +# Verify the 3 methods of specifying "Escape": +printf '%s\n' . . . | tr . '\033' > exp +$prog '\x1b\n\33\n\e\n' > out || fail=1 +compare exp out || fail=1 + +# This would fail (by printing the '--') for printf in sh-utils +# and in coreutils 4.5.1. +$prog -- 'foo\n' > out || fail=1 +cat <<\EOF > exp +foo +EOF + +compare exp out || fail=1 + +rm -f out exp +# Until coreutils-4.5.10, this would elicit a segfault. +$prog '1 %*sy\n' -3 x > out || fail=1 + +# Until coreutils 5.2.2, this would succeed. +if POSIXLY_CORRECT=1 $prog '2 \x' >/dev/null 2>&1; then + fail=1 +else + echo '2 failed, as expected' >> out +fi + +# Until coreutils-4.5.12, these would fail. +$prog '3 \x40\n' >> out || fail=1 +POSIXLY_CORRECT=1 \ +$prog '4 \x40\n' >> out || fail=1 +$prog '5 % +d\n' 234 >> out || fail=1 + +# This should print "6 !\n", but don't rely on '!' being the +# one-byte representation of octal 041. With printf prior to +# coreutils-5.0.1, it would print six bytes: "6 \41\n". +$prog '6 \41\n' | tr '\41' '!' >> out + +# Note that as of coreutils-5.0.1, printf with a format of '\0002y' +# prints a NUL byte followed by the digit '2' and a 'y'. +$prog '7 \2y \02y \002y \0002y\n' |tr '\0\2' '*=' >> out + +$prog '8 %b %b %b %b\n' '\1y' '\01y' '\001y' '\0001y'|tr '\1' = >> out + +$prog '9 %*dx\n' -2 0 >>out || fail=1 + +$prog '10 %.*dx\n' $INT_UFLOW 0 >>out || fail=1 +returns_ 1 $prog '%.*dx\n' $INT_OFLOW 0 >>out 2> /dev/null || fail=1 + +$prog '11 %*c\n' 2 x >>out || fail=1 + +returns_ 1 $prog '%#d\n' 0 >>out 2> /dev/null || fail=1 + +returns_ 1 $prog '%0s\n' 0 >>out 2> /dev/null || fail=1 + +returns_ 1 $prog '%.9c\n' 0 >>out 2> /dev/null || fail=1 + +returns_ 1 $prog '%'\''s\n' 0 >>out 2> /dev/null || fail=1 + +cat <<\EOF > exp +1 x y +2 failed, as expected +3 @ +4 @ +5 +234 +6 ! +7 =y =y =y *2y +8 =y =y =y =y +9 0 x +10 0x +11 x +EOF + +compare exp out || fail=1 + +# Verify handling of single quote chars (\' or \") + +$prog '%d\n' '"a' >out 2>err # valid +$prog '%d\n' '"a"' >>out 2>>err # invalid +$prog '%d\n' '"' >>out 2>>err # invalid +$prog '%d\n' 'a' >>out 2>>err # invalid + +cat <<EOF > exp +97 +97 +0 +0 +EOF + +# POSIX says strtoimax *may* set errno to EINVAL in the latter +# two cases. So far, that happens at least on MacOS X 10.5. +# Map that output to the more common expected output. +sed 's/: Invalid.*/: expected a numeric value/' err > k && mv k err + +cat <<EOF > exp_err +printf: warning: ": character(s) following character constant have been ignored +printf: '"': expected a numeric value +printf: 'a': expected a numeric value +EOF + +compare exp out || fail=1 +compare exp_err err || fail=1 + +Exit $fail |