8 $INC{'IO/Socket.pm'} = 1;
9 $INC{'IO/Select.pm'} = 1;
10 $INC{'IO/Socket/INET.pm'} = 1;
13 (my $libnet_t = __FILE__) =~ s/time.t/libnet_t.pl/;
17 # cannot use(), otherwise it will use IO::Socket and IO::Select
18 eval{ require Net::Time; };
19 ok( !$@, 'should be able to require() Net::Time safely' );
20 ok( exists $INC{'Net/Time.pm'}, 'should be able to use Net::Time' );
22 # force the socket to fail
23 make_fail('IO::Socket::INET', 'new');
24 my $badsock = Net::Time::_socket('foo', 1, 'bar', 'baz');
25 is( $badsock, undef, '_socket() should fail if Socket creation fails' );
27 # if socket is created with protocol UDP (default), it will send a newline
28 my $sock = Net::Time::_socket('foo', 2, 'bar');
29 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
30 is( $sock->{sent}, "\n", 'should send \n with UDP protocol set' );
31 is( $sock->{timeout}, 120, 'timeout should default to 120' );
33 # now try it with a custom timeout and a different protocol
34 $sock = Net::Time::_socket('foo', 3, 'bar', 'tcp', 11);
35 ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
36 is( $sock->{sent}, undef, '_socket() should send nothing unless UDP protocol' );
37 is( $sock->{PeerAddr}, 'bar', '_socket() should set PeerAddr in socket' );
38 is( $sock->{timeout}, 11, '_socket() should respect custom timeout value' );
41 # check for correct args (daytime, 13)
42 IO::Socket::INET::set_message('z');
43 is( Net::Time::inet_daytime('bob'), 'z', 'inet_daytime() should receive data' );
45 # magic numbers defined in Net::Time
46 my $offset = $^O eq 'MacOS' ?
47 (4 * 31536000) : (70 * 31536000 + 17 * 86400);
49 # check for correct args (time, 13)
50 # pretend it is only six seconds since the offset, create a fake message
52 IO::Socket::INET::set_message(pack("N", $offset + 6));
53 is( Net::Time::inet_time('foo'), 6,
54 'inet_time() should calculate time since offset for time()' );
60 my ($pack, $func, $num) = @_;
61 $num = 1 unless defined $num;
63 $fail{$pack}{$func} = $num;
66 package IO::Socket::INET;
68 $fail{'IO::Socket::INET'} = {
75 return if $fail{$class}{new} and $fail{$class}{new}--;
76 bless( { @_ }, $class );
81 my $class = ref($self);
82 return if $fail{$class}{'send'} and $fail{$class}{'send'}--;
83 $self->{sent} .= shift;
96 my ($len, $msg) = @_[1,2];
97 $_[0] .= substr($msg, 0, $len);
101 my ($self, $buf, $length, $flags) = @_;
102 my $message = exists $self->{msg} ?
105 if (defined($message)) {
106 do_recv($_[1], $length, $message);
115 return if defined $fail{$class}{new} and $fail{$class}{new}--;
116 bless({sock => shift}, $class);
120 my ($self, $timeout) = @_;
121 my $class = ref($self);
122 return if defined $fail{$class}{can_read} and $fail{class}{can_read}--;
123 $self->{sock}{timeout} = $timeout;