Skip to content

Commit

Permalink
In t/op/filetest.t, simplify the logic for testing read-only files.
Browse files Browse the repository at this point in the history
Only attempted to change the effective user ID if the test is running as
root. Don't bother consulting $Config to see whether assigning to $> is going
to work - just try it in an eval, and skip if it didn't. Only restore $> if
we know we changed it, and as we only change it from root, we already know
which value to restore it to.

The previous code to check $Config{d_seteuid} was incomplete, as it should
also have been checking for $Config{d_setreuid} and $Config{d_setresuid}, as
$> can use any of these.
  • Loading branch information
nwc10 committed May 31, 2012
1 parent 64f0b68 commit dcefe67
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions t/op/filetest.t
Expand Up @@ -9,7 +9,6 @@ BEGIN {
require './test.pl';
}

use Config;
plan(tests => 47 + 27*14);

ok( -d 'op' );
Expand All @@ -28,23 +27,26 @@ my $ro_file = tempfile();

chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";

$oldeuid = $>; # root can read and write anything
eval '$> = 1'; # so switch uid (may not be implemented)

print "# oldeuid = $oldeuid, euid = $>\n";

SKIP: {
if (!$Config{d_seteuid}) {
skip('no seteuid');
}
else {
ok( !-w $ro_file );
my $restore_root;
if ($> == 0) {
# root can read and write anything, so switch uid (may not be
# implemented)
eval '$> = 1';

skip("Can't drop root privs to test read-only files") if $> == 0;
note("Dropped root privs to test read-only files. \$> == $>");
++$restore_root;
}
}

# Scripts are not -x everywhere so cannot test that.
ok( !-w $ro_file );

eval '$> = $oldeuid'; # switch uid back (may not be implemented)
if ($restore_root) {
# If the previous assignment to $> worked, so should this:
$> = 0;
note("Restored root privs after testing read-only files. \$> == $>");
}
}

# these would fail for the euid 1
# (unless we have unpacked the source code as uid 1...)
Expand Down

0 comments on commit dcefe67

Please sign in to comment.