| 1 |
use strict; |
| 2 |
|
| 3 |
use Test::More 1; |
| 4 |
END{ done_testing() unless caller } |
| 5 |
|
| 6 |
use Config; |
| 7 |
use File::Temp qw(tempdir); |
| 8 |
|
| 9 |
sub is_cygwin () { scalar grep { lc($^O) eq $_ or lc($Config{osname}) eq $_ } qw( cygwin ) } |
| 10 |
|
| 11 |
sub is_msys () { scalar grep { lc($^O) eq $_ or lc($Config{osname}) eq $_ } qw( msys msys2 ) } |
| 12 |
|
| 13 |
sub is_win32 () { $^O eq 'MSWin32' } |
| 14 |
|
| 15 |
sub is_unix_superuser () { |
| 16 |
( not is_win32() and ( $> == 0 or $< == 0 ) ) |
| 17 |
or |
| 18 |
( is_cygwin() and grep { $_ == 544 } split /\s+/, `/usr/bin/id -G` ) |
| 19 |
} |
| 20 |
|
| 21 |
my $dir = tempdir( CLEANUP => 0 ) or BAIL_OUT( "Could not setup temp directory" ); |
| 22 |
print "Temp dir in <$dir>\n"; |
| 23 |
|
| 24 |
unless( -d $dir ) { |
| 25 |
mkdir 'test_files', 0700 |
| 26 |
or BAIL_OUT( "Could not make directory! $!" ); |
| 27 |
} |
| 28 |
|
| 29 |
chdir $dir or BAIL_OUT( "Could not change directory! $!" ); |
| 30 |
|
| 31 |
my @files = qw( |
| 32 |
max_file non_zero_file not_readable readable zero_file |
| 33 |
executable min_file not_executable not_writable writable |
| 34 |
mtime_file |
| 35 |
); |
| 36 |
|
| 37 |
foreach my $file ( @files ) { |
| 38 |
open FH, "> $file"; |
| 39 |
close FH; |
| 40 |
} |
| 41 |
|
| 42 |
{ |
| 43 |
my $count = chmod 0644, @files; |
| 44 |
is( $count, scalar @files ) or BAIL_OUT( "Could not make files readable" ); |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
my $count = chmod 0400, 'readable', 'not_writable', 'not_executable'; |
| 49 |
is( $count, 3 ) or BAIL_OUT( "Could not make files readable" ); |
| 50 |
} |
| 51 |
|
| 52 |
{ |
| 53 |
my $count = chmod 0200, 'writable', 'not_readable', |
| 54 |
'zero_file', 'max_file', 'non_zero_file'; |
| 55 |
is( $count, 5 ) or BAIL_OUT( "Could not make files writable" ); |
| 56 |
if( is_win32() ) { |
| 57 |
system 'attrib', '+', 'not_readable'; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
{ |
| 62 |
my $count = chmod 0100, 'executable'; |
| 63 |
is( $count, 1 ) or BAIL_OUT( "Could not make files executable" ); |
| 64 |
} |
| 65 |
|
| 66 |
truncate 'zero_file', 0; |
| 67 |
truncate 'max_file', 10; |
| 68 |
truncate 'min_file', 0; |
| 69 |
|
| 70 |
{ |
| 71 |
open FH, '> min_file' or BAIL_OUT( "Could not write to min_file: $!" ); |
| 72 |
binmode FH; #, Windows, yo! |
| 73 |
print FH 'x' x 40, $/, 'x' x 11, $/; |
| 74 |
close FH; |
| 75 |
} |
| 76 |
is( -s 'min_file', 51 + 2 * length( $/ ) ); |
| 77 |
|
| 78 |
mkdir 'sub_dir', 0755 or BAIL_OUT( "Could not cerate subdir: $!" ); |