1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
# test compatibility-mode FH lookups, if enabled
# $Id: compat.t,v 1.6 1999/08/02 10:45:46 john Exp $
require 5.004;
use Net::Ident;
use Socket;
use FileHandle;
if ( ! grep { $_ eq "_export_hook_fh" } @Net::Ident::EXPORT ) {
# no default _export_hook_fh in @EXPORT, so we're not in compatibility mode
print "1..0\n";
exit 0;
}
# code below is most of the same as ident.t...
# turn on full debugging, but prepend ``# '' to output to make it disappear in
# non-verbose tests
# turn on full debugging, but prepend ``# '' to output to make it disappear in
# non-verbose tests
unless ( open(DEBUGFH, "|-") ) {
# child... do the stuff
$|++;
while ( <> ) {
s/^/# /;
print;
}
exit 0;
}
DEBUGFH->autoflush(1);
*Net::Ident::STDDBG = *DEBUGFH;
$Net::Ident::DEBUG = 2;
# find hosts file
my($hosts) = grep { -r } qw( t/hosts hosts ../t/hosts );
my @hosts;
if ( open(HOSTS, $hosts) ) {
@hosts = grep { !/^#/ } <HOSTS>;
chomp @hosts;
close HOSTS;
}
else {
@hosts = qw(127.0.0.1);
}
$SIG{ALRM} = sub { 0 };
$| = 1;
sub bomb ($) { die "# Aargh: @_\n1..1\nnot ok1\n" };
$tcpproto = (getprotobyname('tcp'))[2] || 6;
$identport = (getservbyname('ident', 'tcp'))[2] || 113;
foreach $host ( @hosts ) {
print "# trying to resolve $host...\n";
if ( $addr = inet_aton($host) ) {
$fh = new FileHandle;
socket($fh, PF_INET, SOCK_STREAM, $tcpproto) or bomb("socket: $!");
print "# connecting to " . inet_ntoa($addr) . ":$identport\n";
alarm(10);
if ( connect($fh, sockaddr_in($identport, $addr)) ) {
alarm(0);
print "# connected\n";
$connok ||= $fh;
$connokhost ||= $host;
}
else {
$e = $!;
alarm(0);
if ( $e =~ /connection refused/i ) {
print "# connection refused\n";
# try to make a connection to the telnet port instead,
# to give us some connected filehandle to try the
# ident lookup on.
print "# connecting to " . inet_ntoa($addr) . ":23\n";
$fh = new FileHandle;
socket($fh, PF_INET, SOCK_STREAM, $tcpproto) or
bomb("socket: $!");
alarm(10);
if ( connect($fh, sockaddr_in(23, $addr)) ) {
alarm(0);
print "# connected\n";
$connrefuse ||= $fh;
$connrefusehost ||= $host;
}
else {
print "# connect: $!\n";
alarm(0);
}
}
else {
print "# connect: $e\n";
}
}
last if $connok && $connrefuse;
}
}
$tests = 1;
if ( $connok ) {
print "# Will run regular ident lookups by connecting to $connokhost\n";
$tests++;
}
if ( $connrefuse ) {
print "# Will run ``connection refused'' tests by connecting to ",
$connrefusehost, "\n";
$tests++;
}
if ( ! $connok && ! $connrefuse ) {
print "# WARNING: not a lot of testing to do without an identd to use!\n";
}
print "1..$tests\n";
$i = 1;
if ( $connok ) {
print "# standard lookup test that will succeed, using FH->ident_lookup\n";
$username = $connok->ident_lookup(30);
print "not " unless $username;
print "ok $i\n"; $i++;
}
if ( $connrefuse ) {
print "# try to get a connection refused error\n";
($username, $opsys, $error) = $connrefuse->ident_lookup(30);
print "not " unless !defined $username && !defined $opsys &&
$error =~ /connection refused/i;
print "ok $i\n"; $i++;
print "# got: $error\n";
}
print "# try to get ident info on a something that's not a socket\n";
my($user, $opsys, $error) = STDERR->ident_lookup(30);
print "not " unless !defined $user && !defined $opsys && $error;
print "ok $i\n"; $i++;
for ($user, $opsys, $error) {
$_ = "<undef>" if ! defined;
}
print "# got: user=$user opsys=$opsys error=$error\n";
|