summaryrefslogtreecommitdiff
path: root/dist/Net-Ping/t/450_service.t
blob: dd1bd4ca317289a13aff3d901a0b6711b1a9db05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Testing service_check method using tcp and syn protocols.

use Config;

BEGIN {
  unless (eval "require IO::Socket") {
    print "1..0 \# Skip: no IO::Socket\n";
    exit;
  }
  unless (getservbyname('echo', 'tcp')) {
    print "1..0 \# Skip: no echo port\n";
    exit;
  }
  unless ($Config{d_getpbyname}) {
    print "1..0 \# Skip: no getprotobyname\n";
    exit;
  }
}

use strict;
use Test::More tests => 26;
BEGIN {use_ok('Net::Ping')};

# I'm lazy so I'll just use IO::Socket
# for the TCP Server stuff instead of doing
# all that direct socket() junk manually.

my $sock1 = new IO::Socket::INET
  LocalAddr => "127.0.0.1",
  Proto => "tcp",
  Listen => 8,
  or warn "bind: $!";

isa_ok($sock1, 'IO::Socket::INET',
       'Start a TCP listen server on ephemeral port');

# Start listening on another ephemeral port
my $sock2 = new IO::Socket::INET
  LocalAddr => "127.0.0.1",
  Proto => "tcp",
  Listen => 8,
  or warn "bind: $!";

isa_ok($sock2, 'IO::Socket::INET',
       'Start a second TCP listen server on ephemeral port');

my $port1 = $sock1->sockport;
cmp_ok($port1, '>', 0);

my $port2 = $sock2->sockport;
cmp_ok($port2, '>', 0);

# 
isnt($port1, $port2, 'Make sure the servers are listening on different ports');

$sock2->close;

# This is how it should be:
# 127.0.0.1:$port1 - service ON
# 127.0.0.1:$port2 - service OFF

#####
# First, we test using the "tcp" protocol.
# (2 seconds should be long enough to connect to loopback.)
my $p = new Net::Ping "tcp", 2;

isa_ok($p, 'Net::Ping', 'new() worked');

# Disable service checking
$p->service_check(0);

# Try on the first port
$p->{port_num} = $port1;

is($p->ping("127.0.0.1"), 1, 'first port is reachable');

# Try on the other port
$p->{port_num} = $port2;

{
    local $TODO = "Believed not to work on $^O" if $^O =~ /^(?:hpux|MSWin32|os390)$/;
    is($p->ping("127.0.0.1"), 1, 'second port is reachable');
}

# Enable service checking
$p->service_check(1);

# Try on the first port
$p->{port_num} = $port1;

is($p->ping("127.0.0.1"), 1, 'first service is on');

# Try on the other port
$p->{port_num} = $port2;

isnt($p->ping("127.0.0.1"), 2, 'second service is off');

# test 11 just finished.

#####
# Lastly, we test using the "syn" protocol.
$p = new Net::Ping "syn", 2;

isa_ok($p, 'Net::Ping', 'new() worked');

# Disable service checking
$p->service_check(0);

# Try on the first port
$p->{port_num} = $port1;

is($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!");

is($p->ack(), '127.0.0.1', 'IP should be reachable');
is($p->ack(), undef, 'No more sockets');

###
# Get a fresh object
$p = new Net::Ping "syn", 2;

isa_ok($p, 'Net::Ping', 'new() worked');

# Disable service checking
$p->service_check(0);

# Try on the other port
$p->{port_num} = $port2;

SKIP: {
  skip "no localhost resolver on $^O", 2
    unless Socket::getaddrinfo('localhost', &Socket::AF_INET);
  is($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!");

  {
    local $TODO = "Believed not to work on $^O"
      if $^O =~ /^(?:hpux|MSWin32|os390)$/;
    is($p->ack(), '127.0.0.1', 'IP should be reachable');
  }
}
is($p->ack(), undef, 'No more sockets');


###
# Get a fresh object
$p = new Net::Ping "syn", 2;

isa_ok($p, 'Net::Ping', 'new() worked');

# Enable service checking
$p->service_check(1);

# Try on the first port
$p->{port_num} = $port1;

is($p->ping("127.0.0.1"), 1, "send SYN to first port") or diag ("ERRNO: $!");

is($p->ack(), '127.0.0.1', 'IP should be reachable');
is($p->ack(), undef, 'No more sockets');


###
# Get a fresh object
$p = new Net::Ping "syn", 2;

isa_ok($p, 'Net::Ping', 'new() worked');

# Enable service checking
$p->service_check(1);

# Try on the other port
$p->{port_num} = $port2;

is($p->ping("127.0.0.1"), 1, "send SYN to second port") or diag ("ERRNO: $!");

is($p->ack(), undef, 'No sockets should have service on');