| 1 |
elmex |
1.1 |
#!perl |
| 2 |
|
|
use strict; |
| 3 |
|
|
use Test::More; |
| 4 |
|
|
use AnyEvent::Impl::Perl; |
| 5 |
|
|
use AnyEvent; |
| 6 |
|
|
use Net::IRC3::Connection; |
| 7 |
|
|
use Net::IRC3::Client::Connection; |
| 8 |
|
|
use Socket; |
| 9 |
|
|
use IO::Handle; |
| 10 |
|
|
|
| 11 |
|
|
sub prepare_cl_srv { |
| 12 |
|
|
my $srv = Net::IRC3::Connection->new; |
| 13 |
|
|
my $cl = Net::IRC3::Client::Connection->new; |
| 14 |
|
|
|
| 15 |
|
|
socketpair (my $s1, my $s2, AF_UNIX, SOCK_STREAM, PF_UNSPEC) |
| 16 |
|
|
or die "socketpair: $!"; |
| 17 |
|
|
$s1 or die "couldn't make socketpair: $!"; |
| 18 |
|
|
$srv->use_socket ('localhost', '65533', $s1); |
| 19 |
|
|
$cl->use_socket ('localhost', '6667', $s2); |
| 20 |
|
|
|
| 21 |
|
|
($srv, $cl) |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
|
|
plan tests => 5; |
| 25 |
|
|
|
| 26 |
|
|
{ |
| 27 |
|
|
my $con = Net::IRC3::Connection->new; |
| 28 |
|
|
|
| 29 |
|
|
# TEST 1: resolve failure (blocking) |
| 30 |
|
|
SKIP: { |
| 31 |
|
|
skip "don't wait for timeout", 1; |
| 32 |
|
|
|
| 33 |
|
|
diag ("Trying to connect to non-existent domain... waiting for resolver timeout"); |
| 34 |
|
|
eval { |
| 35 |
|
|
$con->connect ('nosuchdomain.exists.here', 12345); |
| 36 |
|
|
}; |
| 37 |
|
|
like ($@, qr/couldn't connect to irc server 'nosuchdomain\.exists\.here:12345'/, |
| 38 |
|
|
"connection error with resolution"); |
| 39 |
|
|
}; |
| 40 |
|
|
|
| 41 |
|
|
# TEST 2: connection refused |
| 42 |
|
|
|
| 43 |
|
|
my $connect_error = 0; |
| 44 |
|
|
my $c = AnyEvent->condvar; |
| 45 |
|
|
|
| 46 |
|
|
$con->reg_cb ( |
| 47 |
|
|
connect_error => sub { |
| 48 |
|
|
$c->broadcast; |
| 49 |
|
|
$connect_error = $_[1]; |
| 50 |
|
|
0 |
| 51 |
|
|
} |
| 52 |
|
|
); |
| 53 |
|
|
|
| 54 |
|
|
$con->connect ('127.0.0.1', 65533); |
| 55 |
|
|
|
| 56 |
|
|
$c->wait; |
| 57 |
|
|
like ($connect_error, qr/refused/, "does connection refused error"); |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
{ |
| 61 |
|
|
my $c = AnyEvent->condvar; |
| 62 |
|
|
|
| 63 |
|
|
my ($srv, $cl) = prepare_cl_srv; |
| 64 |
|
|
my ($nick, $user, $real) = (test => testbot => "I'm a Net::IRC3 test"); |
| 65 |
|
|
my $ss = {}; # server state |
| 66 |
|
|
|
| 67 |
|
|
$srv->reg_cb ( |
| 68 |
|
|
irc_nick => sub { |
| 69 |
|
|
my ($srv, $p) = @_; |
| 70 |
|
|
$ss->{nick} = $p->{params}->[0]; |
| 71 |
|
|
1 |
| 72 |
|
|
}, |
| 73 |
|
|
irc_user => sub { |
| 74 |
|
|
my ($srv, $p) = @_; |
| 75 |
|
|
$ss->{user} = $p->{params}->[0]; |
| 76 |
|
|
$ss->{real} = $p->{params}->[3]; |
| 77 |
|
|
is ($ss->{nick}, $nick, "server got nick"); |
| 78 |
|
|
is ($ss->{user}, $user, "server got user"); |
| 79 |
|
|
is ($ss->{real}, $real, "server got real"); |
| 80 |
|
|
$c->broadcast; |
| 81 |
|
|
1 |
| 82 |
|
|
}, |
| 83 |
|
|
); |
| 84 |
|
|
$cl->register ($nick, $user, $real); |
| 85 |
|
|
|
| 86 |
|
|
$c->wait; |
| 87 |
|
|
} |