1*0Sstevel@tonic-gate#!./perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate chdir 't' if -d 't'; 5*0Sstevel@tonic-gate @INC = '../lib'; 6*0Sstevel@tonic-gate} 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gateuse strict; 9*0Sstevel@tonic-gateuse Config; 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gateuse Test::More tests => 15; 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gateuse_ok( 'sigtrap' ); 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gatepackage main; 16*0Sstevel@tonic-gatelocal %SIG; 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate# use a version of sigtrap.pm somewhat too high 19*0Sstevel@tonic-gateeval{ sigtrap->import(99999) }; 20*0Sstevel@tonic-gatelike( $@, qr/version 99999 required,/, 'import excessive version number' ); 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate# use an invalid signal name 23*0Sstevel@tonic-gateeval{ sigtrap->import('abadsignal') }; 24*0Sstevel@tonic-gatelike( $@, qr/^Unrecognized argument abadsignal/, 'send bad signame to import' ); 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gateeval{ sigtrap->import('handler') }; 27*0Sstevel@tonic-gatelike( $@, qr/^No argument specified/, 'send handler without subref' ); 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gatesigtrap->import('AFAKE'); 30*0Sstevel@tonic-gateis( $SIG{AFAKE}, \&sigtrap::handler_traceback, 'install normal handler' ); 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gatesigtrap->import('die', 'AFAKE', 'stack-trace', 'FAKE2'); 33*0Sstevel@tonic-gateis( $SIG{AFAKE}, \&sigtrap::handler_die, 'install the die handler' ); 34*0Sstevel@tonic-gateis( $SIG{FAKE2}, \&sigtrap::handler_traceback, 'install traceback handler' ); 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gatemy @normal = qw( HUP INT PIPE TERM ); 37*0Sstevel@tonic-gate@SIG{@normal} = 1 x @normal; 38*0Sstevel@tonic-gatesigtrap->import('normal-signals'); 39*0Sstevel@tonic-gateis( (grep { ref $_ } @SIG{@normal}), @normal, 'check normal-signals set' ); 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gatemy @error = qw( ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP ); 42*0Sstevel@tonic-gate@SIG{@error} = 1 x @error; 43*0Sstevel@tonic-gatesigtrap->import('error-signals'); 44*0Sstevel@tonic-gateis( (grep { ref $_ } @SIG{@error}), @error, 'check error-signals set' ); 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gatemy @old = qw( ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP ); 47*0Sstevel@tonic-gate@SIG{@old} = 1 x @old; 48*0Sstevel@tonic-gatesigtrap->import('old-interface-signals'); 49*0Sstevel@tonic-gateis( (grep { ref $_ } @SIG{@old}), @old, 'check old-interface-signals set' ); 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gatemy $handler = sub {}; 52*0Sstevel@tonic-gatesigtrap->import(handler => $handler, 'FAKE3'); 53*0Sstevel@tonic-gateis( $SIG{FAKE3}, $handler, 'install custom handler' ); 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate$SIG{FAKE} = 'IGNORE'; 56*0Sstevel@tonic-gatesigtrap->import('untrapped', 'FAKE'); 57*0Sstevel@tonic-gateis( $SIG{FAKE}, 'IGNORE', 'respect existing handler set to IGNORE' ); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gatemy $out = tie *STDOUT, 'TieOut'; 60*0Sstevel@tonic-gate$SIG{FAKE} = 'DEFAULT'; 61*0Sstevel@tonic-gate$sigtrap::Verbose = 1; 62*0Sstevel@tonic-gatesigtrap->import('any', 'FAKE'); 63*0Sstevel@tonic-gateis( $SIG{FAKE}, \&sigtrap::handler_traceback, 'should set default handler' ); 64*0Sstevel@tonic-gatelike( $out->read, qr/^Installing handler/, 'does it talk with $Verbose set?' ); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate# handler_die croaks with first argument 67*0Sstevel@tonic-gateeval { sigtrap::handler_die('FAKE') }; 68*0Sstevel@tonic-gatelike( $@, qr/^Caught a SIGFAKE/, 'does handler_die() croak?' ); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gatepackage TieOut; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gatesub TIEHANDLE { 73*0Sstevel@tonic-gate bless(\(my $scalar), $_[0]); 74*0Sstevel@tonic-gate} 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gatesub PRINT { 77*0Sstevel@tonic-gate my $self = shift; 78*0Sstevel@tonic-gate $$self .= join '', @_; 79*0Sstevel@tonic-gate} 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gatesub WRITE { 82*0Sstevel@tonic-gate my ($self, $msg, $length) = @_; 83*0Sstevel@tonic-gate $$self .= $msg; 84*0Sstevel@tonic-gate} 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gatesub read { 87*0Sstevel@tonic-gate my $self = shift; 88*0Sstevel@tonic-gate substr($$self, 0, length($$self), ''); 89*0Sstevel@tonic-gate} 90