11 if (!eval { require Socket }) {
12 plan skip_all => "no Socket";
14 elsif (ord('A') == 193 && !eval { require Convert::EBCDIC }) {
15 plan skip_all => "EBCDIC but no Convert::EBCDIC";
21 $INC{'IO/Socket.pm'} = 1;
22 $INC{'IO/Select.pm'} = 1;
23 $INC{'IO/Socket/INET.pm'} = 1;
26 # cannot use(), otherwise it will use IO::Socket and IO::Select
27 eval{ require Net::Time; };
28 ok( !$@, 'should be able to require() Net::Time safely' );
29 ok( exists $INC{'Net/Time.pm'}, 'should be able to use Net::Time' );
31 # force the socket to fail
32 make_fail('IO::Socket::INET', 'new');
33 my $badsock = Net::Time::_socket('foo', 1, 'bar', 'baz');
34 is( $badsock, undef, '_socket() should fail if Socket creation fails' );
36 # if socket is created with protocol UDP (default), it will send a newline
37 my $sock = Net::Time::_socket('foo', 2, 'bar');
38 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
39 is( $sock->{sent}, "\n", 'should send \n with UDP protocol set' );
40 is( $sock->{timeout}, 120, 'timeout should default to 120' );
42 # now try it with a custom timeout and a different protocol
43 $sock = Net::Time::_socket('foo', 3, 'bar', 'tcp', 11);
44 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
45 is( $sock->{sent}, undef, '_socket() should send nothing unless UDP protocol' );
46 is( $sock->{PeerAddr}, 'bar', '_socket() should set PeerAddr in socket' );
47 is( $sock->{timeout}, 11, '_socket() should respect custom timeout value' );
50 # check for correct args (daytime, 13)
51 IO::Socket::INET::set_message('z');
52 is( Net::Time::inet_daytime('bob'), 'z', 'inet_daytime() should receive data' );
54 # magic numbers defined in Net::Time
55 my $offset = $^O eq 'MacOS' ?
56 (4 * 31536000) : (70 * 31536000 + 17 * 86400);
58 # check for correct args (time, 13)
59 # pretend it is only six seconds since the offset, create a fake message
61 IO::Socket::INET::set_message(pack("N", $offset + 6));
62 is( Net::Time::inet_time('foo'), 6,
63 'inet_time() should calculate time since offset for time()' );
69 my ($pack, $func, $num) = @_;
70 $num = 1 unless defined $num;
72 $fail{$pack}{$func} = $num;
75 package IO::Socket::INET;
77 $fail{'IO::Socket::INET'} = {
84 return if $fail{$class}{new} and $fail{$class}{new}--;
85 bless( { @_ }, $class );
90 my $class = ref($self);
91 return if $fail{$class}{'send'} and $fail{$class}{'send'}--;
92 $self->{sent} .= shift;
105 my ($len, $msg) = @_[1,2];
106 $_[0] .= substr($msg, 0, $len);
110 my ($self, $buf, $length, $flags) = @_;
111 my $message = exists $self->{msg} ?
114 if (defined($message)) {
115 do_recv($_[1], $length, $message);
124 return if defined $fail{$class}{new} and $fail{$class}{new}--;
125 bless({sock => shift}, $class);
129 my ($self, $timeout) = @_;
130 my $class = ref($self);
131 return if defined $fail{$class}{can_read} and $fail{class}{can_read}--;
132 $self->{sock}{timeout} = $timeout;