-
Notifications
You must be signed in to change notification settings - Fork 9
fix: portable errno constants (ELOOP, ENOTEMPTY) + WRITE validation #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Koan-Bot
wants to merge
2
commits into
cpanel:master
Choose a base branch
from
atoomic:koan.atoomic/fix-portability-errno
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−13
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #!/usr/bin/perl -w | ||
|
|
||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Test2::Bundle::Extended; | ||
| use Test2::Tools::Explain; | ||
|
|
||
| use Errno qw/ELOOP ENOTEMPTY ENOENT/; | ||
| use Fcntl; | ||
|
|
||
| use Test::MockFile qw< nostrict >; | ||
|
|
||
| subtest "sysopen O_NOFOLLOW on symlink sets ELOOP" => sub { | ||
| my $target = Test::MockFile->file( '/tmp/real_file', 'data' ); | ||
| my $link = Test::MockFile->symlink( '/tmp/real_file', '/tmp/link_to_file' ); | ||
|
|
||
| # O_NOFOLLOW on a regular file should work | ||
| $! = 0; | ||
| ok( sysopen( my $fh, '/tmp/real_file', O_RDONLY | O_NOFOLLOW ), "sysopen O_NOFOLLOW on regular file succeeds" ); | ||
| close $fh if $fh; | ||
|
|
||
| # O_NOFOLLOW on a symlink should fail with ELOOP | ||
| $! = 0; | ||
| ok( !sysopen( my $fh2, '/tmp/link_to_file', O_RDONLY | O_NOFOLLOW ), "sysopen O_NOFOLLOW on symlink fails" ); | ||
| is( $! + 0, ELOOP, "\$! is ELOOP (not hardcoded 40)" ) or diag "Got errno: " . ( $! + 0 ) . " ($!)"; | ||
| }; | ||
|
|
||
| subtest "rmdir non-empty directory sets ENOTEMPTY" => sub { | ||
| my $dir = Test::MockFile->dir('/tmp/test_dir'); | ||
| my $file = Test::MockFile->file( '/tmp/test_dir/child', 'content' ); | ||
|
|
||
| mkdir('/tmp/test_dir'); | ||
|
|
||
| $! = 0; | ||
| ok( !rmdir('/tmp/test_dir'), "rmdir on non-empty directory fails" ); | ||
| is( $! + 0, ENOTEMPTY, "\$! is ENOTEMPTY (not hardcoded 39)" ) or diag "Got errno: " . ( $! + 0 ) . " ($!)"; | ||
| }; | ||
|
|
||
| subtest "syswrite with non-numeric length warns" => sub { | ||
| my $mock = Test::MockFile->file('/tmp/write_test'); | ||
| sysopen( my $fh, '/tmp/write_test', O_WRONLY | O_CREAT | O_TRUNC ) or die; | ||
|
|
||
| my @warnings; | ||
| local $SIG{__WARN__} = sub { push @warnings, $_[0] }; | ||
|
|
||
| my $ret = syswrite( $fh, "hello", "abc" ); | ||
| is( $ret, 0, "syswrite with non-numeric len returns 0" ); | ||
| ok( scalar @warnings >= 1, "got a warning" ); | ||
| like( $warnings[0], qr/isn't numeric/, "warning mentions non-numeric argument" ) if @warnings; | ||
|
|
||
| close $fh; | ||
| }; | ||
|
|
||
| subtest "syswrite with negative length warns" => sub { | ||
| my $mock = Test::MockFile->file('/tmp/write_test2'); | ||
| sysopen( my $fh, '/tmp/write_test2', O_WRONLY | O_CREAT | O_TRUNC ) or die; | ||
|
|
||
| my @warnings; | ||
| local $SIG{__WARN__} = sub { push @warnings, $_[0] }; | ||
|
|
||
| my $ret = syswrite( $fh, "hello", -1 ); | ||
| is( $ret, 0, "syswrite with negative length returns 0" ); | ||
| ok( scalar @warnings >= 1, "got a warning" ); | ||
| like( $warnings[0], qr/Negative length/, "warning mentions negative length" ) if @warnings; | ||
|
|
||
| close $fh; | ||
| }; | ||
|
|
||
| subtest "syswrite with offset outside string warns" => sub { | ||
| my $mock = Test::MockFile->file('/tmp/write_test3'); | ||
| sysopen( my $fh, '/tmp/write_test3', O_WRONLY | O_CREAT | O_TRUNC ) or die; | ||
|
|
||
| my @warnings; | ||
| local $SIG{__WARN__} = sub { push @warnings, $_[0] }; | ||
|
|
||
| my $ret = syswrite( $fh, "hello", 2, 100 ); | ||
| is( $ret, 0, "syswrite with offset beyond string returns 0" ); | ||
| ok( scalar @warnings >= 1, "got a warning" ); | ||
| like( $warnings[0], qr/Offset outside string/, "warning mentions offset" ) if @warnings; | ||
|
|
||
| close $fh; | ||
| }; | ||
|
|
||
| subtest "syswrite with valid negative offset works" => sub { | ||
| my $mock = Test::MockFile->file('/tmp/write_test4'); | ||
| sysopen( my $fh, '/tmp/write_test4', O_WRONLY | O_CREAT | O_TRUNC ) or die; | ||
|
|
||
| # -3 from end of "hello" (len 5) = position 2, write 2 chars = "ll" | ||
| is( syswrite( $fh, "hello", 2, -3 ), 2, "syswrite with negative offset returns correct byte count" ); | ||
| is( $mock->contents, "ll", "correct substring written with negative offset" ); | ||
|
|
||
| close $fh; | ||
| }; | ||
|
|
||
| subtest "syswrite with too-negative offset warns" => sub { | ||
| my $mock = Test::MockFile->file('/tmp/write_test5'); | ||
| sysopen( my $fh, '/tmp/write_test5', O_WRONLY | O_CREAT | O_TRUNC ) or die; | ||
|
|
||
| my @warnings; | ||
| local $SIG{__WARN__} = sub { push @warnings, $_[0] }; | ||
|
|
||
| my $ret = syswrite( $fh, "hello", 2, -10 ); | ||
| is( $ret, 0, "syswrite with offset before start of string returns 0" ); | ||
| ok( scalar @warnings >= 1, "got a warning" ); | ||
| like( $warnings[0], qr/Offset outside string/, "warning mentions offset" ) if @warnings; | ||
|
|
||
| close $fh; | ||
| }; | ||
|
|
||
| done_testing(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs discussion