1*0Sstevel@tonic-gate# -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*- 2*0Sstevel@tonic-gate#!/usr/local/bin/perl -w 3*0Sstevel@tonic-gate 4*0Sstevel@tonic-gateuse strict; 5*0Sstevel@tonic-gateuse lib qw(t/lib); 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate# Due to a bug in older versions of MakeMaker & Test::Harness, we must 8*0Sstevel@tonic-gate# ensure the blib's are in @INC, else we might use the core CGI.pm 9*0Sstevel@tonic-gateuse lib qw(blib/lib blib/arch); 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gateuse Test::More tests => 41; 12*0Sstevel@tonic-gateuse IO::Handle; 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gateBEGIN { use_ok('CGI::Carp') }; 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 17*0Sstevel@tonic-gate# Test id 18*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate# directly invoked 21*0Sstevel@tonic-gatemy $expect_f = __FILE__; 22*0Sstevel@tonic-gatemy $expect_l = __LINE__ + 1; 23*0Sstevel@tonic-gatemy ($file, $line, $id) = CGI::Carp::id(0); 24*0Sstevel@tonic-gateis($file, $expect_f, "file"); 25*0Sstevel@tonic-gateis($line, $expect_l, "line"); 26*0Sstevel@tonic-gateis($id, "carp.t", "id"); 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate# one level of indirection 29*0Sstevel@tonic-gatesub id1 { my $level = shift; return CGI::Carp::id($level); }; 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate$expect_l = __LINE__ + 1; 32*0Sstevel@tonic-gate($file, $line, $id) = id1(1); 33*0Sstevel@tonic-gateis($file, $expect_f, "file"); 34*0Sstevel@tonic-gateis($line, $expect_l, "line"); 35*0Sstevel@tonic-gateis($id, "carp.t", "id"); 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate# two levels of indirection 38*0Sstevel@tonic-gatesub id2 { my $level = shift; return id1($level); }; 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate$expect_l = __LINE__ + 1; 41*0Sstevel@tonic-gate($file, $line, $id) = id2(2); 42*0Sstevel@tonic-gateis($file, $expect_f, "file"); 43*0Sstevel@tonic-gateis($line, $expect_l, "line"); 44*0Sstevel@tonic-gateis($id, "carp.t", "id"); 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 47*0Sstevel@tonic-gate# Test stamp 48*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gatemy $stamp = "/^\\[ 51*0Sstevel@tonic-gate ([a-z]{3}\\s){2}\\s? 52*0Sstevel@tonic-gate [\\s\\d:]+ 53*0Sstevel@tonic-gate \\]\\s$id:/ix"; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gatelike(CGI::Carp::stamp(), 56*0Sstevel@tonic-gate $stamp, 57*0Sstevel@tonic-gate "Time in correct format"); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gatesub stamp1 {return CGI::Carp::stamp()}; 60*0Sstevel@tonic-gatesub stamp2 {return stamp1()}; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gatelike(stamp2(), $stamp, "Time in correct format"); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 65*0Sstevel@tonic-gate# Test warn and _warn 66*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate# set some variables to control what's going on. 69*0Sstevel@tonic-gate$CGI::Carp::WARN = 0; 70*0Sstevel@tonic-gate$CGI::Carp::EMIT_WARNINGS = 0; 71*0Sstevel@tonic-gatemy $q_file = quotemeta($file); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate# Test that realwarn is called 75*0Sstevel@tonic-gate{ 76*0Sstevel@tonic-gate local $^W = 0; 77*0Sstevel@tonic-gate eval "sub CGI::Carp::realwarn {return 'Called realwarn'};"; 78*0Sstevel@tonic-gate} 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate$expect_l = __LINE__ + 1; 81*0Sstevel@tonic-gateis(CGI::Carp::warn("There is a problem"), 82*0Sstevel@tonic-gate "Called realwarn", 83*0Sstevel@tonic-gate "CGI::Carp::warn calls CORE::warn"); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate# Test that message is constructed correctly 86*0Sstevel@tonic-gateeval 'sub CGI::Carp::realwarn {my $mess = shift; return $mess};'; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate$expect_l = __LINE__ + 1; 89*0Sstevel@tonic-gatelike(CGI::Carp::warn("There is a problem"), 90*0Sstevel@tonic-gate "/] $id: There is a problem at $q_file line $expect_l.".'$/', 91*0Sstevel@tonic-gate "CGI::Carp::warn builds correct message"); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate# Test that _warn is called at the correct time 94*0Sstevel@tonic-gate$CGI::Carp::WARN = 1; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gatemy $warn_expect_l = $expect_l = __LINE__ + 1; 97*0Sstevel@tonic-gatelike(CGI::Carp::warn("There is a problem"), 98*0Sstevel@tonic-gate "/] $id: There is a problem at $q_file line $expect_l.".'$/', 99*0Sstevel@tonic-gate "CGI::Carp::warn builds correct message"); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 102*0Sstevel@tonic-gate# Test ineval 103*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gateok(!CGI::Carp::ineval, 'ineval returns false when not in eval'); 106*0Sstevel@tonic-gateeval {ok(CGI::Carp::ineval, 'ineval returns true when in eval');}; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 109*0Sstevel@tonic-gate# Test die 110*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate# set some variables to control what's going on. 113*0Sstevel@tonic-gate$CGI::Carp::WRAP = 0; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate$expect_l = __LINE__ + 1; 116*0Sstevel@tonic-gateeval { CGI::Carp::die('There is a problem'); }; 117*0Sstevel@tonic-gatelike($@, 118*0Sstevel@tonic-gate '/^There is a problem/', 119*0Sstevel@tonic-gate 'CGI::Carp::die calls CORE::die without altering argument in eval'); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate# Test that realwarn is called 122*0Sstevel@tonic-gate{ 123*0Sstevel@tonic-gate local $^W = 0; 124*0Sstevel@tonic-gate eval 'sub CGI::Carp::realdie {my $mess = shift; return $mess};'; 125*0Sstevel@tonic-gate} 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gatelike(CGI::Carp::die('There is a problem'), 128*0Sstevel@tonic-gate $stamp, 129*0Sstevel@tonic-gate 'CGI::Carp::die calls CORE::die, but adds stamp'); 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 132*0Sstevel@tonic-gate# Test set_message 133*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gateis(CGI::Carp::set_message('My new Message'), 136*0Sstevel@tonic-gate 'My new Message', 137*0Sstevel@tonic-gate 'CGI::Carp::set_message returns new message'); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateis($CGI::Carp::CUSTOM_MSG, 140*0Sstevel@tonic-gate 'My new Message', 141*0Sstevel@tonic-gate 'CGI::Carp::set_message message set correctly'); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate# set the message back to the empty string so that the tests later 144*0Sstevel@tonic-gate# work properly. 145*0Sstevel@tonic-gateCGI::Carp::set_message(''), 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 148*0Sstevel@tonic-gate# Test set_progname 149*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gateimport CGI::Carp qw(name=new_progname); 152*0Sstevel@tonic-gateis($CGI::Carp::PROGNAME, 153*0Sstevel@tonic-gate 'new_progname', 154*0Sstevel@tonic-gate 'CGI::Carp::import set program name correctly'); 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateis(CGI::Carp::set_progname('newer_progname'), 157*0Sstevel@tonic-gate 'newer_progname', 158*0Sstevel@tonic-gate 'CGI::Carp::set_progname returns new program name'); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gateis($CGI::Carp::PROGNAME, 161*0Sstevel@tonic-gate 'newer_progname', 162*0Sstevel@tonic-gate 'CGI::Carp::set_progname program name set correctly'); 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate# set the message back to the empty string so that the tests later 165*0Sstevel@tonic-gate# work properly. 166*0Sstevel@tonic-gateis (CGI::Carp::set_progname(undef),undef,"CGI::Carp::set_progname returns unset name correctly"); 167*0Sstevel@tonic-gateis ($CGI::Carp::PROGNAME,undef,"CGI::Carp::set_progname program name unset correctly"); 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 170*0Sstevel@tonic-gate# Test warnings_to_browser 171*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gateCGI::Carp::warningsToBrowser(0); 174*0Sstevel@tonic-gateis($CGI::Carp::EMIT_WARNINGS, 0, "Warnings turned off"); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate# turn off STDOUT (prevents spurious warnings to screen 177*0Sstevel@tonic-gatetie *STDOUT, 'StoreStuff' or die "Can't tie STDOUT"; 178*0Sstevel@tonic-gateCGI::Carp::warningsToBrowser(1); 179*0Sstevel@tonic-gatemy $fake_out = join '', <STDOUT>; 180*0Sstevel@tonic-gateuntie *STDOUT; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gateopen(STDOUT, ">&REAL_STDOUT"); 183*0Sstevel@tonic-gatemy $fname = $0; 184*0Sstevel@tonic-gate$fname =~ tr/<>-/\253\273\255/; # _warn does this so we have to also 185*0Sstevel@tonic-gateis( $fake_out, "<!-- warning: There is a problem at $fname line $warn_expect_l. -->\n", 186*0Sstevel@tonic-gate 'warningsToBrowser() on' ); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gateis($CGI::Carp::EMIT_WARNINGS, 1, "Warnings turned off"); 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 191*0Sstevel@tonic-gate# Test fatals_to_browser 192*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gatepackage StoreStuff; 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gatesub TIEHANDLE { 197*0Sstevel@tonic-gate my $class = shift; 198*0Sstevel@tonic-gate bless [], $class; 199*0Sstevel@tonic-gate} 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gatesub PRINT { 202*0Sstevel@tonic-gate my $self = shift; 203*0Sstevel@tonic-gate push @$self, @_; 204*0Sstevel@tonic-gate} 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gatesub READLINE { 207*0Sstevel@tonic-gate my $self = shift; 208*0Sstevel@tonic-gate shift @$self; 209*0Sstevel@tonic-gate} 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gatepackage main; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gatetie *STDOUT, "StoreStuff"; 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate# do tests 216*0Sstevel@tonic-gatemy @result; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gateCGI::Carp::fatalsToBrowser(); 219*0Sstevel@tonic-gate$result[0] .= $_ while (<STDOUT>); 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gateCGI::Carp::fatalsToBrowser('Message to the world'); 222*0Sstevel@tonic-gate$result[1] .= $_ while (<STDOUT>); 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate$ENV{SERVER_ADMIN} = 'foo@bar.com'; 225*0Sstevel@tonic-gateCGI::Carp::fatalsToBrowser(); 226*0Sstevel@tonic-gate$result[2] .= $_ while (<STDOUT>); 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gateCGI::Carp::set_message('Override the message passed in'), 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gateCGI::Carp::fatalsToBrowser('Message to the world'); 231*0Sstevel@tonic-gate$result[3] .= $_ while (<STDOUT>); 232*0Sstevel@tonic-gateCGI::Carp::set_message(''), 233*0Sstevel@tonic-gatedelete $ENV{SERVER_ADMIN}; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate# now restore STDOUT 236*0Sstevel@tonic-gateuntie *STDOUT; 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gatelike($result[0], 240*0Sstevel@tonic-gate '/Content-type: text/html/', 241*0Sstevel@tonic-gate "Default string has header"); 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gateok($result[0] !~ /Message to the world/, "Custom message not in default string"); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gatelike($result[1], 246*0Sstevel@tonic-gate '/Message to the world/', 247*0Sstevel@tonic-gate "Custom Message appears in output"); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gateok($result[0] !~ /foo\@bar.com/, "Server Admin does not appear in default message"); 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gatelike($result[2], 252*0Sstevel@tonic-gate '/foo@bar.com/', 253*0Sstevel@tonic-gate "Server Admin appears in output"); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gatelike($result[3], 256*0Sstevel@tonic-gate '/Message to the world/', 257*0Sstevel@tonic-gate "Custom message not in result"); 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gatelike($result[3], 260*0Sstevel@tonic-gate '/Override the message passed in/', 261*0Sstevel@tonic-gate "Correct message in string"); 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 264*0Sstevel@tonic-gate# Test to_filehandle 265*0Sstevel@tonic-gate#----------------------------------------------------------------------------- 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gatesub buffer { 268*0Sstevel@tonic-gate CGI::Carp::to_filehandle (@_); 269*0Sstevel@tonic-gate} 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gatetie *STORE, "StoreStuff"; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gaterequire FileHandle; 274*0Sstevel@tonic-gatemy $fh = FileHandle->new; 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gateok( defined buffer(\*STORE), '\*STORE returns proper filehandle'); 277*0Sstevel@tonic-gateok( defined buffer( $fh ), '$fh returns proper filehandle'); 278*0Sstevel@tonic-gateok( defined buffer('::STDOUT'), 'STDIN returns proper filehandle'); 279*0Sstevel@tonic-gateok( defined buffer(*main::STDOUT), 'STDIN returns proper filehandle'); 280*0Sstevel@tonic-gateok(!defined buffer("WIBBLE"), '"WIBBLE" doesn\'t returns proper filehandle'); 281