line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Net::Service; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Test::Net::Service - test different network services |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $net_service = Test::Net::Service->new( |
10
|
|
|
|
|
|
|
'host' => 'camel.cle.sk', |
11
|
|
|
|
|
|
|
'proto' => 'tcp', |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
eval { |
15
|
|
|
|
|
|
|
$net_service->test( |
16
|
|
|
|
|
|
|
'port' => 22, |
17
|
|
|
|
|
|
|
'service' => 'ssh', |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This should a collection of basic test for network services. Check the list |
24
|
|
|
|
|
|
|
and the description L. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
1
|
|
83241
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
66
|
|
30
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
1256
|
use IO::Socket::INET (); |
|
1
|
|
|
|
|
55135
|
|
|
1
|
|
|
|
|
26
|
|
33
|
1
|
|
|
1
|
|
1086
|
use Carp::Clan 'croak'; |
|
1
|
|
|
|
|
4925
|
|
|
1
|
|
|
|
|
7
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
1
|
|
187
|
use base 'Class::Accessor::Fast'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
900
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 PROPERTIES |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
All optional for constructor. Will be used as defaults if set. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
host |
45
|
|
|
|
|
|
|
socket |
46
|
|
|
|
|
|
|
proto |
47
|
|
|
|
|
|
|
port |
48
|
|
|
|
|
|
|
service |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw{ |
53
|
|
|
|
|
|
|
host |
54
|
|
|
|
|
|
|
socket |
55
|
|
|
|
|
|
|
proto |
56
|
|
|
|
|
|
|
port |
57
|
|
|
|
|
|
|
service |
58
|
|
|
|
|
|
|
}); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 METHODS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 new() |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Constructor. You set any property and it will be used as defaults for C<<->test()>> |
66
|
|
|
|
|
|
|
method. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub new { |
71
|
1
|
|
|
1
|
1
|
18
|
my $class = shift; |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
19
|
return $class->SUPER::new({ @_ }); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 test() |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Perform the service test. Add any additional || different parameters to the default |
80
|
|
|
|
|
|
|
ones as function arguments. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub test { |
85
|
1
|
|
|
1
|
1
|
68
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
15
|
my %args = (%$self, @_); |
88
|
1
|
|
|
|
|
7
|
my $socket = $self->connect(%args); |
89
|
1
|
|
|
|
|
3
|
my $service = 'test_'.$args{'service'}; |
90
|
|
|
|
|
|
|
|
91
|
1
|
50
|
|
|
|
5
|
croak 'failed to connect' |
92
|
|
|
|
|
|
|
if not defined $socket; |
93
|
|
|
|
|
|
|
|
94
|
1
|
50
|
|
|
|
16
|
croak 'do not know how to test '.$service |
95
|
|
|
|
|
|
|
if not $self->can($service); |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
6
|
$self->$service(%args, 'socket' => $socket); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 connect() |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
INTERNAL methd to connect to the host&port if needed. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub connect { |
109
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
110
|
1
|
|
|
|
|
4
|
my %args = @_; |
111
|
|
|
|
|
|
|
|
112
|
1
|
50
|
|
|
|
9
|
return $args{'socket'} |
113
|
|
|
|
|
|
|
if $args{'socket'}; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
0
|
return IO::Socket::INET->new( |
116
|
|
|
|
|
|
|
PeerAddr => $args{'host'}, |
117
|
|
|
|
|
|
|
PeerPort => $args{'port'}, |
118
|
|
|
|
|
|
|
Proto => $args{'proto'}, |
119
|
|
|
|
|
|
|
); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 Services |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head3 test_dummy() |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Will aways succeed if the connection is sucesfull. Additionaly |
129
|
|
|
|
|
|
|
it will return hash ref of all the arguments that will be used |
130
|
|
|
|
|
|
|
to connect and test. Can be used when you want to always pass |
131
|
|
|
|
|
|
|
the test or for debugging. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub test_dummy { |
136
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
137
|
1
|
|
|
|
|
5
|
my %args = @_; |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
7
|
return \%args; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head3 test_ssh() |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Will check for SSH string in the first line returned by server after |
146
|
|
|
|
|
|
|
connection. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub test_ssh { |
151
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
152
|
0
|
|
|
|
|
|
my %args = @_; |
153
|
0
|
|
|
|
|
|
my $socket = $args{'socket'}; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
my $reqexp_match = qr/SSH/; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my $reply = <$socket>; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
return if $reply =~ $reqexp_match; |
160
|
0
|
|
|
|
|
|
die 'reply "', $reply, '" does not match ', $reqexp_match, "\n"; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head3 test_http() |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Need 'host' to be passed. Will make GET http request for this host. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Checks if the first line of the server response beginns with 'HTTP'. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub test_http { |
173
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
174
|
0
|
|
|
|
|
|
my %args = @_; |
175
|
0
|
|
|
|
|
|
my $socket = $args{'socket'}; |
176
|
0
|
|
|
|
|
|
my $host = $args{'host'}; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my $reqexp_match = qr{^HTTP/}; |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
print $socket "GET / HTTP/1.1\nHost: $host\n\n"; |
181
|
0
|
|
|
|
|
|
my $reply = <$socket>; |
182
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
return 1 if $reply =~ $reqexp_match; |
184
|
0
|
|
|
|
|
|
die 'reply "', $reply, '" does not match ', $reqexp_match, "\n"; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head3 test_https() |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
TODO |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub test_https { |
195
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
196
|
0
|
|
|
|
|
|
my %args = @_; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# TODO |
199
|
0
|
|
|
|
|
|
return; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
__END__ |