Commit | Line | Data |
c8570720 |
1 | #!./perl -w |
2 | |
3 | BEGIN { |
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/; |
14 | require $libnet_t; |
15 | |
16 | print "1..12\n"; |
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' ); |
21 | |
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' ); |
26 | |
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' ); |
32 | |
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' ); |
39 | |
40 | # inet_daytime |
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' ); |
44 | |
45 | # magic numbers defined in Net::Time |
46 | my $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 |
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()' ); |
55 | |
56 | |
57 | my %fail; |
58 | |
59 | sub make_fail { |
60 | my ($pack, $func, $num) = @_; |
61 | $num = 1 unless defined $num; |
62 | |
63 | $fail{$pack}{$func} = $num; |
64 | } |
65 | |
66 | package IO::Socket::INET; |
67 | |
68 | $fail{'IO::Socket::INET'} = { |
69 | new => 0, |
70 | 'send' => 0, |
71 | }; |
72 | |
73 | sub new { |
74 | my $class = shift; |
75 | return if $fail{$class}{new} and $fail{$class}{new}--; |
76 | bless( { @_ }, $class ); |
77 | } |
78 | |
79 | sub 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 | |
86 | my $msg; |
87 | sub set_message { |
88 | if (ref($_[0])) { |
89 | $_[0]->{msg} = $_[1]; |
90 | } else { |
91 | $msg = shift; |
92 | } |
93 | } |
94 | |
95 | sub do_recv { |
96 | my ($len, $msg) = @_[1,2]; |
97 | $_[0] .= substr($msg, 0, $len); |
98 | } |
99 | |
100 | sub 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 | |
111 | package IO::Select; |
112 | |
113 | sub new { |
114 | my $class = shift; |
115 | return if defined $fail{$class}{new} and $fail{$class}{new}--; |
116 | bless({sock => shift}, $class); |
117 | } |
118 | |
119 | sub 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 | |
127 | 1; |