1#!/usr/bin/perl 2# Test "mktemp". 3 4# Copyright (C) 2007-2023 Free Software Foundation, Inc. 5 6# This program is free software: you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation, either version 3 of the License, or 9# (at your option) any later version. 10 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <https://www.gnu.org/licenses/>. 18 19use strict; 20 21(my $ME = $0) =~ s|.*/||; 22 23sub check_tmp($$) 24{ 25 my ($file, $file_or_dir) = @_; 26 27 my (undef, undef, $mode, undef) = stat $file 28 or die "$ME: failed to stat $file: $!\n"; 29 my $required_mode; 30 if ($file_or_dir eq 'D') { 31 -d $file or die "$ME: $file isn't a directory\n"; 32 -x $file or die "$ME: $file isn't owner-searchable\n"; 33 $required_mode = 0700; 34 } elsif ($file_or_dir eq 'F') { 35 -f $file or die "$ME: $file isn't a regular file\n"; 36 $required_mode = 0600; 37 } 38 -r $file or die "$ME: $file isn't owner-readable\n"; 39 -w $file or die "$ME: $file isn't owner-writable\n"; 40 ($mode & 0777) == $required_mode 41 or die "$ME: $file doesn't have required permissions\n"; 42 43 $file_or_dir eq 'D' 44 and do { rmdir $file or die "$ME: failed to rmdir $file: $!\n" }; 45 $file_or_dir eq 'F' 46 and do { unlink $file or die "$ME: failed to unlink $file: $!\n" }; 47} 48 49# Turn off localization of executable's output. 50@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; 51my $prog = 'mktemp'; 52my $bad_dir = 'no/such/dir'; 53 54my @Tests = 55 ( 56 # test-name, [option, option, ...] {OUT=>"expected-output"} 57 # 58 ['too-many', '-q a b', 59 {ERR=>"$prog: too many templates\n" 60 . "Try '$prog --help' for more information.\n"}, {EXIT => 1} ], 61 62 ['too-few-x', '-q foo.XX', {EXIT => 1}, 63 {ERR=>"$prog: too few X's in template 'foo.XX'\n"}], 64 65 ['1f', 'bar.XXXX', {OUT => "bar.ZZZZ\n"}, 66 {OUT_SUBST => 's,\.....$,.ZZZZ,'}, 67 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 68 check_tmp $f, 'F'; }} 69 ], 70 71 ['2f', '-- -XXXX', {OUT => "-ZZZZ\n"}, 72 {OUT_SUBST => 's,-....$,-ZZZZ,'}, 73 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 74 check_tmp $f, 'F'; }} 75 ], 76 77 # Create a temporary directory. 78 ['1d', '-d f.XXXX', {OUT => "f.ZZZZ\n"}, 79 {OUT_SUBST => 's,\.....$,.ZZZZ,'}, 80 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 81 check_tmp $f, 'D'; }} 82 ], 83 84 # Use a template consisting solely of X's 85 ['1d-allX', '-d XXXX', {OUT => "ZZZZ\n"}, 86 {PRE => sub {mkdir 'XXXX',0755 or die "XXXX: $!\n"}}, 87 {OUT_SUBST => 's,^....$,ZZZZ,'}, 88 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 89 check_tmp $f, 'D'; rmdir 'XXXX' or die "rmdir XXXX: $!\n"; }} 90 ], 91 92 # Test -u 93 ['uf', '-u f.XXXX', {OUT => "f.ZZZZ\n"}, 94 {OUT_SUBST => 's,\.....$,.ZZZZ,'}, 95 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 96 -e $f and die "dry-run created file"; }}], 97 ['ud', '-d --dry-run d.XXXX', {OUT => "d.ZZZZ\n"}, 98 {OUT_SUBST => 's,\.....$,.ZZZZ,'}, 99 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 100 -e $f and die "dry-run created directory"; }}], 101 102 # Test bad templates 103 ['invalid-tl', '-t a/bXXXX', 104 {ERR=>"$prog: invalid template, 'a/bXXXX', " 105 . "contains directory separator\n"}, {EXIT => 1} ], 106 107 ['invalid-t2', '--tmpdir=a /bXXXX', 108 {ERR=>"$prog: invalid template, '/bXXXX'; " 109 . "with --tmpdir, it may not be absolute\n"}, {EXIT => 1} ], 110 111 # Suffix after X. 112 ['suffix1f', 'aXXXXb', {OUT=>"aZZZZb\n"}, 113 {OUT_SUBST=>'s,a....b,aZZZZb,'}, 114 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 115 check_tmp $f, 'F'; }}], 116 ['suffix1d', '-d aXXXXb', {OUT=>"aZZZZb\n"}, 117 {OUT_SUBST=>'s,a....b,aZZZZb,'}, 118 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 119 check_tmp $f, 'D'; }}], 120 ['suffix1u', '-u aXXXXb', {OUT=>"aZZZZb\n"}, 121 {OUT_SUBST=>'s,a....b,aZZZZb,'}, 122 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 123 -e $f and die "dry-run created file"; }}], 124 125 ['suffix2f', 'aXXXXaaXXXXa', {OUT=>"aXXXXaaZZZZa\n"}, 126 {OUT_SUBST=>'s,a....a$,aZZZZa,'}, 127 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 128 check_tmp $f, 'F'; }}], 129 ['suffix2d', '-d --suffix= aXXXXaaXXXX', {OUT=>"aXXXXaaZZZZ\n"}, 130 {OUT_SUBST=>'s,a....$,aZZZZ,'}, 131 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 132 check_tmp $f, 'D'; }}], 133 134 ['suffix3f', '--suffix=b aXXXX', {OUT=>"aZZZZb\n"}, 135 {OUT_SUBST=>'s,a....b,aZZZZb,'}, 136 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 137 check_tmp $f, 'F'; }}], 138 139 ['suffix4f', '--suffix=X aXXXX', {OUT=>"aZZZZX\n"}, 140 {OUT_SUBST=>'s,^a....,aZZZZ,'}, 141 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 142 check_tmp $f, 'F'; }}], 143 144 ['suffix5f', '--suffix /b aXXXX', {EXIT=>1}, 145 {ERR=>"$prog: invalid suffix '/b', contains directory separator\n"}], 146 147 ['suffix6f', 'aXXXX/b', {EXIT=>1}, 148 {ERR=>"$prog: invalid suffix '/b', contains directory separator\n"}], 149 150 ['suffix7f', '--suffix= aXXXXb', {EXIT=>1}, 151 {ERR=>"$prog: with --suffix, template 'aXXXXb' must end in X\n"}], 152 ['suffix7d', '-d --suffix=aXXXXb ""', {EXIT=>1}, 153 {ERR=>"$prog: with --suffix, template '' must end in X\n"}], 154 155 ['suffix8f', 'aXXXX --suffix=b', {OUT=>"aZZZZb\n"}, 156 {OUT_SUBST=>'s,^a....,aZZZZ,'}, 157 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 158 check_tmp $f, 'F'; }}], 159 160 ['suffix9f', 'aXXXX --suffix=b', {EXIT=>1}, 161 {ENV=>"POSIXLY_CORRECT=1"}, 162 {ERR=>"$prog: too many templates\n" 163 . "Try '$prog --help' for more information.\n"}], 164 165 ['suffix10f', 'aXXb', {EXIT => 1}, 166 {ERR=>"$prog: too few X's in template 'aXXb'\n"}], 167 ['suffix10d', '-d --suffix=X aXX', {EXIT => 1}, 168 {ERR=>"$prog: too few X's in template 'aXXX'\n"}], 169 170 ['suffix11f', '--suffix=.txt', {OUT=>"./tmp.ZZZZZZZZZZ.txt\n"}, 171 {ENV=>"TMPDIR=."}, 172 {OUT_SUBST=>'s,\..{10}\.,.ZZZZZZZZZZ.,'}, 173 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 174 check_tmp $f, 'F'; }}], 175 176 177 # Test template with subdirectory 178 ['tmp-w-slash', '--tmpdir=. a/bXXXX', 179 {PRE => sub {mkdir 'a',0755 or die "a: $!\n"}}, 180 {OUT_SUBST => 's,b....$,bZZZZ,'}, 181 {OUT => "./a/bZZZZ\n"}, 182 {POST => sub { my ($f) = @_; defined $f or return; chomp $f; 183 check_tmp $f, 'F'; unlink $f; rmdir 'a' or die "rmdir a: $!\n" }} 184 ], 185 186 ['priority-t-tmpdir', "-t -p $bad_dir foo.XXX", 187 {ENV=>"TMPDIR=."}, 188 {OUT_SUBST => 's,....$,.ZZZ,'}, 189 {OUT => "./foo.ZZZ\n"}, 190 ], 191 192 ['pipe-bad-tmpdir', 193 {ENV => "TMPDIR=$bad_dir"}, 194 {ERR_SUBST => "s,($bad_dir/)[^']+': .*,\$1...,"}, 195 {ERR => "$prog: failed to create file via template '$bad_dir/...\n"}, 196 {EXIT => 1}], 197 ['pipe-bad-tmpdir-u', '-u', {OUT => "$bad_dir/tmp.ZZZZZZZZZZ\n"}, 198 {ENV => "TMPDIR=$bad_dir"}, 199 {OUT_SUBST => 's,\..{10}$,.ZZZZZZZZZZ,'}], 200 ); 201 202my $save_temps = $ENV{DEBUG}; 203my $verbose = $ENV{VERBOSE}; 204 205my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose); 206exit $fail; 207