1# $OpenBSD: OpenBSD-Pledge.t,v 1.4 2021/06/09 23:21:34 afresh1 Exp $ # 2## no critic 'version' 3## no critic 'package' 4# Before 'make install' is performed this script should be runnable with 5# 'make test'. After 'make install' it should work as 'perl OpenBSD-Pledge.t' 6 7######################### 8 9use strict; 10use warnings; 11 12use Fcntl qw( O_RDONLY O_WRONLY ); 13use File::Temp; 14 15use Config; 16my %sig_num; 17@sig_num{ split q{ }, $Config{sig_name} } = split q{ }, $Config{sig_num}; 18 19use Test::More; 20BEGIN { use_ok('OpenBSD::Pledge') } 21 22## no critic 'private' 23## no critic 'punctuation' 24######################### 25# _PLEDGE 26######################### 27 28sub xspledge_ok ($$) ## no critic 'prototypes' 29{ 30 my ( $name, $code ) = @_; 31 local $Test::Builder::Level = 32 $Test::Builder::Level + 1; ## no critic 'package variable' 33 34 my $ok = 0; 35 foreach my $pledge ( q{}, $name ) { 36 my $dir = File::Temp->newdir('OpenBSD-Pledge-XXXXXXXXX'); 37 my $pid = fork // die "Unable to fork for $name: $!\n"; 38 39 if ( !$pid ) { 40 chdir($dir); 41 OpenBSD::Pledge::_pledge( "abort" ); # non fatal 42 OpenBSD::Pledge::_pledge( "stdio $pledge" ) 43 || die "[$name] $!\n"; 44 $code->(); 45 exit; 46 } 47 48 waitpid $pid, 0; 49 50 if ($pledge) { 51 $ok += is $?, 0, "[$name] OK with pledge"; 52 } else { 53 ## no critic 'numbers' 54 $ok += is $? & 127, $sig_num{ABRT}, 55 "[$name] ABRT without pledge"; 56 } 57 } 58 return $ok == 2; 59} 60xspledge_ok rpath => sub { sysopen my $fh, '/dev/random', O_RDONLY }; 61xspledge_ok wpath => sub { sysopen my $fh, 'FOO', O_WRONLY }; 62xspledge_ok cpath => sub { mkdir q{/} }; 63 64######################### 65# PLEDGE 66######################### 67{ 68 my @calls; 69 no warnings 'redefine'; ## no critic 'warnings'; 70 local *OpenBSD::Pledge::_pledge = sub { push @calls, \@_; return 1 }; 71 use warnings 'redefine'; 72 73 OpenBSD::Pledge::pledge(qw( foo bar foo baz )); 74 OpenBSD::Pledge::pledge( qw( foo qux baz quux )); 75 76 is_deeply \@calls, 77 [ 78 [ "bar baz foo stdio" ], 79 [ "baz foo quux qux stdio" ], 80 ], 81 "Sorted and unique promises, plus stdio"; 82} 83 84######################### 85done_testing; 86 871; # to shut up critic 88