8 if (!eval "require Socket") {
9 print "1..0 # no Socket\n"; exit 0;
11 if (ord('A') == 193 && !eval "require Convert::EBCDIC") {
12 print "1..0 # EBCDIC but no Convert::EBCDIC\n"; exit 0;
14 $INC{'IO/Socket.pm'} = 1;
15 $INC{'IO/Select.pm'} = 1;
16 $INC{'IO/Socket/INET.pm'} = 1;
19 (my $libnet_t = __FILE__) =~ s/time.t/libnet_t.pl/;
23 # cannot use(), otherwise it will use IO::Socket and IO::Select
24 eval{ require Net::Time; };
25 ok( !$@, 'should be able to require() Net::Time safely' );
26 ok( exists $INC{'Net/Time.pm'}, 'should be able to use Net::Time' );
28 # force the socket to fail
29 make_fail('IO::Socket::INET', 'new');
30 my $badsock = Net::Time::_socket('foo', 1, 'bar', 'baz');
31 is( $badsock, undef, '_socket() should fail if Socket creation fails' );
33 # if socket is created with protocol UDP (default), it will send a newline
34 my $sock = Net::Time::_socket('foo', 2, 'bar');
35 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
36 is( $sock->{sent}, "\n", 'should send \n with UDP protocol set' );
37 is( $sock->{timeout}, 120, 'timeout should default to 120' );
39 # now try it with a custom timeout and a different protocol
40 $sock = Net::Time::_socket('foo', 3, 'bar', 'tcp', 11);
41 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
42 is( $sock->{sent}, undef, '_socket() should send nothing unless UDP protocol' );
43 is( $sock->{PeerAddr}, 'bar', '_socket() should set PeerAddr in socket' );
44 is( $sock->{timeout}, 11, '_socket() should respect custom timeout value' );
47 # check for correct args (daytime, 13)
48 IO::Socket::INET::set_message('z');
49 is( Net::Time::inet_daytime('bob'), 'z', 'inet_daytime() should receive data' );
51 # magic numbers defined in Net::Time
52 my $offset = $^O eq 'MacOS' ?
53 (4 * 31536000) : (70 * 31536000 + 17 * 86400);
55 # check for correct args (time, 13)
56 # pretend it is only six seconds since the offset, create a fake message
58 IO::Socket::INET::set_message(pack("N", $offset + 6));
59 is( Net::Time::inet_time('foo'), 6,
60 'inet_time() should calculate time since offset for time()' );
66 my ($pack, $func, $num) = @_;
67 $num = 1 unless defined $num;
69 $fail{$pack}{$func} = $num;
72 package IO::Socket::INET;
74 $fail{'IO::Socket::INET'} = {
81 return if $fail{$class}{new} and $fail{$class}{new}--;
82 bless( { @_ }, $class );
87 my $class = ref($self);
88 return if $fail{$class}{'send'} and $fail{$class}{'send'}--;
89 $self->{sent} .= shift;
102 my ($len, $msg) = @_[1,2];
103 $_[0] .= substr($msg, 0, $len);
107 my ($self, $buf, $length, $flags) = @_;
108 my $message = exists $self->{msg} ?
111 if (defined($message)) {
112 do_recv($_[1], $length, $message);
121 return if defined $fail{$class}{new} and $fail{$class}{new}--;
122 bless({sock => shift}, $class);
126 my ($self, $timeout) = @_;
127 my $class = ref($self);
128 return if defined $fail{$class}{can_read} and $fail{class}{can_read}--;
129 $self->{sock}{timeout} = $timeout;