Just sorting to guarantee order is not enough.
[p5sagit/p5-mst-13.2.git] / lib / Net / t / time.t
CommitLineData
c8570720 1#!./perl -w
2
3BEGIN {
4 if ($ENV{PERL_CORE}) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
8 $INC{'IO/Socket.pm'} = 1;
9 $INC{'IO/Select.pm'} = 1;
10 $INC{'IO/Socket/INET.pm'} = 1;
11}
12
13(my $libnet_t = __FILE__) =~ s/time.t/libnet_t.pl/;
14require $libnet_t;
15
16print "1..12\n";
17# cannot use(), otherwise it will use IO::Socket and IO::Select
18eval{ require Net::Time; };
19ok( !$@, 'should be able to require() Net::Time safely' );
20ok( exists $INC{'Net/Time.pm'}, 'should be able to use Net::Time' );
21
22# force the socket to fail
23make_fail('IO::Socket::INET', 'new');
24my $badsock = Net::Time::_socket('foo', 1, 'bar', 'baz');
25is( $badsock, undef, '_socket() should fail if Socket creation fails' );
26
27# if socket is created with protocol UDP (default), it will send a newline
28my $sock = Net::Time::_socket('foo', 2, 'bar');
29ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
30is( $sock->{sent}, "\n", 'should send \n with UDP protocol set' );
31is( $sock->{timeout}, 120, 'timeout should default to 120' );
32
33# now try it with a custom timeout and a different protocol
34$sock = Net::Time::_socket('foo', 3, 'bar', 'tcp', 11);
35ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
36is( $sock->{sent}, undef, '_socket() should send nothing unless UDP protocol' );
37is( $sock->{PeerAddr}, 'bar', '_socket() should set PeerAddr in socket' );
38is( $sock->{timeout}, 11, '_socket() should respect custom timeout value' );
39
40# inet_daytime
41# check for correct args (daytime, 13)
42IO::Socket::INET::set_message('z');
43is( Net::Time::inet_daytime('bob'), 'z', 'inet_daytime() should receive data' );
44
45# magic numbers defined in Net::Time
46my $offset = $^O eq 'MacOS' ?
47 (4 * 31536000) : (70 * 31536000 + 17 * 86400);
48
49# check for correct args (time, 13)
50# pretend it is only six seconds since the offset, create a fake message
51# inet_time
52IO::Socket::INET::set_message(pack("N", $offset + 6));
53is( Net::Time::inet_time('foo'), 6,
54 'inet_time() should calculate time since offset for time()' );
55
56
57my %fail;
58
59sub make_fail {
60 my ($pack, $func, $num) = @_;
61 $num = 1 unless defined $num;
62
63 $fail{$pack}{$func} = $num;
64}
65
66package IO::Socket::INET;
67
68$fail{'IO::Socket::INET'} = {
69 new => 0,
70 'send' => 0,
71};
72
73sub new {
74 my $class = shift;
75 return if $fail{$class}{new} and $fail{$class}{new}--;
76 bless( { @_ }, $class );
77}
78
79sub send {
80 my $self = shift;
81 my $class = ref($self);
82 return if $fail{$class}{'send'} and $fail{$class}{'send'}--;
83 $self->{sent} .= shift;
84}
85
86my $msg;
87sub set_message {
88 if (ref($_[0])) {
89 $_[0]->{msg} = $_[1];
90 } else {
91 $msg = shift;
92 }
93}
94
95sub do_recv {
96 my ($len, $msg) = @_[1,2];
97 $_[0] .= substr($msg, 0, $len);
98}
99
100sub recv {
101 my ($self, $buf, $length, $flags) = @_;
102 my $message = exists $self->{msg} ?
103 $self->{msg} : $msg;
104
105 if (defined($message)) {
106 do_recv($_[1], $length, $message);
107 }
108 1;
109}
110
111package IO::Select;
112
113sub new {
114 my $class = shift;
115 return if defined $fail{$class}{new} and $fail{$class}{new}--;
116 bless({sock => shift}, $class);
117}
118
119sub can_read {
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;
124 1;
125}
126
1271;