net-ping 1.6.2-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGES +287 -0
- data/Gemfile +2 -0
- data/MANIFEST +25 -0
- data/README +54 -0
- data/Rakefile +94 -0
- data/doc/ping.txt +274 -0
- data/examples/example_pingexternal.rb +16 -0
- data/examples/example_pinghttp.rb +22 -0
- data/examples/example_pingtcp.rb +16 -0
- data/examples/example_pingudp.rb +12 -0
- data/lib/net/ping.rb +17 -0
- data/lib/net/ping/external.rb +122 -0
- data/lib/net/ping/helper.rb +33 -0
- data/lib/net/ping/http.rb +159 -0
- data/lib/net/ping/icmp.rb +179 -0
- data/lib/net/ping/ping.rb +89 -0
- data/lib/net/ping/tcp.rb +83 -0
- data/lib/net/ping/udp.rb +119 -0
- data/lib/net/ping/wmi.rb +118 -0
- data/net-ping.gemspec +43 -0
- data/test/test_net_ping.rb +35 -0
- data/test/test_net_ping_external.rb +129 -0
- data/test/test_net_ping_http.rb +210 -0
- data/test/test_net_ping_icmp.rb +139 -0
- data/test/test_net_ping_tcp.rb +105 -0
- data/test/test_net_ping_udp.rb +119 -0
- data/test/test_net_ping_wmi.rb +81 -0
- metadata +145 -0
@@ -0,0 +1,210 @@
|
|
1
|
+
#################################################################################
|
2
|
+
# test_net_ping_http.rb
|
3
|
+
#
|
4
|
+
# Test case for the Net::PingHTTP class. This should be run via the 'test' or
|
5
|
+
# 'test:http' Rake task.
|
6
|
+
#################################################################################
|
7
|
+
require 'test-unit'
|
8
|
+
require 'fakeweb'
|
9
|
+
require 'net/ping/http'
|
10
|
+
|
11
|
+
class TC_Net_Ping_HTTP < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
ENV['http_proxy'] = ENV['https_proxy'] = ENV['no_proxy'] = nil
|
14
|
+
@uri = 'http://www.google.com/index.html'
|
15
|
+
@uri_https = 'https://encrypted.google.com'
|
16
|
+
@proxy = 'http://username:password@proxymoxie:3128'
|
17
|
+
FakeWeb.allow_net_connect = false
|
18
|
+
|
19
|
+
FakeWeb.register_uri(:get, @uri, :body => "PONG")
|
20
|
+
FakeWeb.register_uri(:head, @uri, :body => "PONG")
|
21
|
+
FakeWeb.register_uri(:head, @uri_https, :body => "PONG")
|
22
|
+
FakeWeb.register_uri(:get, @uri_https, :body => "PONG")
|
23
|
+
FakeWeb.register_uri(:head, "http://jigsaw.w3.org/HTTP/300/302.html",
|
24
|
+
:body => "PONG",
|
25
|
+
:location => "#{@uri}",
|
26
|
+
:status => ["302", "Found"])
|
27
|
+
|
28
|
+
FakeWeb.register_uri(:any, 'http://www.blabfoobarurghxxxx.com', :exception => SocketError)
|
29
|
+
|
30
|
+
@http = Net::Ping::HTTP.new(@uri, 80, 30)
|
31
|
+
@bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
|
32
|
+
end
|
33
|
+
|
34
|
+
test 'ping basic functionality' do
|
35
|
+
assert_respond_to(@http, :ping)
|
36
|
+
assert_nothing_raised{ @http.ping }
|
37
|
+
end
|
38
|
+
|
39
|
+
test 'ping returns a boolean value' do
|
40
|
+
assert_boolean(@http.ping?)
|
41
|
+
assert_boolean(@bad.ping?)
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'ping? is an alias for ping' do
|
45
|
+
assert_alias_method(@http, :ping?, :ping)
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'pingecho is an alias for ping' do
|
49
|
+
assert_alias_method(@http, :pingecho, :ping)
|
50
|
+
end
|
51
|
+
|
52
|
+
test 'ping should succeed for a valid website' do
|
53
|
+
assert_true(@http.ping?)
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'ping should fail for an invalid website' do
|
57
|
+
assert_false(@bad.ping?)
|
58
|
+
end
|
59
|
+
|
60
|
+
test 'duration basic functionality' do
|
61
|
+
assert_respond_to(@http, :duration)
|
62
|
+
assert_nothing_raised{ @http.ping }
|
63
|
+
end
|
64
|
+
|
65
|
+
test 'duration returns a float value on a successful ping' do
|
66
|
+
assert_true(@http.ping)
|
67
|
+
assert_kind_of(Float, @http.duration)
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'duration is nil on an unsuccessful ping' do
|
71
|
+
assert_false(@bad.ping)
|
72
|
+
assert_nil(@http.duration)
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'host attribute basic functionality' do
|
76
|
+
assert_respond_to(@http, :host)
|
77
|
+
assert_respond_to(@http, :host=)
|
78
|
+
assert_equal('http://www.google.com/index.html', @http.host)
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'uri is an alias for host' do
|
82
|
+
assert_alias_method(@http, :uri, :host)
|
83
|
+
assert_alias_method(@http, :uri=, :host=)
|
84
|
+
end
|
85
|
+
|
86
|
+
test 'port attribute basic functionality' do
|
87
|
+
assert_respond_to(@http, :port)
|
88
|
+
assert_respond_to(@http, :port=)
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'port attribute expected value' do
|
92
|
+
assert_equal(80, @http.port)
|
93
|
+
end
|
94
|
+
|
95
|
+
test 'timeout attribute basic functionality' do
|
96
|
+
assert_respond_to(@http, :timeout)
|
97
|
+
assert_respond_to(@http, :timeout=)
|
98
|
+
end
|
99
|
+
|
100
|
+
test 'timeout attribute expected values' do
|
101
|
+
assert_equal(30, @http.timeout)
|
102
|
+
assert_equal(5, @bad.timeout)
|
103
|
+
end
|
104
|
+
|
105
|
+
test 'exception attribute basic functionality' do
|
106
|
+
assert_respond_to(@http, :exception)
|
107
|
+
assert_nil(@http.exception)
|
108
|
+
end
|
109
|
+
|
110
|
+
test 'exception attribute is nil if the ping is successful' do
|
111
|
+
assert_true(@http.ping)
|
112
|
+
assert_nil(@http.exception)
|
113
|
+
end
|
114
|
+
|
115
|
+
test 'exception attribute is not nil if the ping is unsuccessful' do
|
116
|
+
assert_false(@bad.ping)
|
117
|
+
assert_not_nil(@bad.exception)
|
118
|
+
end
|
119
|
+
|
120
|
+
test 'warning attribute basic functionality' do
|
121
|
+
assert_respond_to(@http, :warning)
|
122
|
+
assert_nil(@http.warning)
|
123
|
+
end
|
124
|
+
|
125
|
+
test 'user_agent accessor is defined' do
|
126
|
+
assert_respond_to(@http, :user_agent)
|
127
|
+
assert_respond_to(@http, :user_agent=)
|
128
|
+
end
|
129
|
+
|
130
|
+
test 'user_agent defaults to nil' do
|
131
|
+
assert_nil(@http.user_agent)
|
132
|
+
end
|
133
|
+
|
134
|
+
test 'ping with user agent' do
|
135
|
+
@http.user_agent = "KDDI-CA32"
|
136
|
+
assert_true(@http.ping)
|
137
|
+
end
|
138
|
+
|
139
|
+
test 'redirect_limit accessor is defined' do
|
140
|
+
assert_respond_to(@http, :redirect_limit)
|
141
|
+
assert_respond_to(@http, :redirect_limit=)
|
142
|
+
end
|
143
|
+
|
144
|
+
test 'redirect_limit defaults to 5' do
|
145
|
+
assert_equal(5, @http.redirect_limit)
|
146
|
+
end
|
147
|
+
|
148
|
+
test 'redirects succeed by default' do
|
149
|
+
@http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
|
150
|
+
assert_true(@http.ping)
|
151
|
+
end
|
152
|
+
|
153
|
+
test 'redirect fail if follow_redirect is set to false' do
|
154
|
+
@http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
|
155
|
+
@http.follow_redirect = false
|
156
|
+
assert_false(@http.ping)
|
157
|
+
end
|
158
|
+
|
159
|
+
test 'ping with redirect limit set to zero fails' do
|
160
|
+
@http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
|
161
|
+
@http.redirect_limit = 0
|
162
|
+
assert_false(@http.ping)
|
163
|
+
assert_equal("Redirect limit exceeded", @http.exception)
|
164
|
+
end
|
165
|
+
|
166
|
+
test 'ping against https site defaults to port 443' do
|
167
|
+
@http = Net::Ping::HTTP.new(@uri_https)
|
168
|
+
assert_equal(443, @http.port)
|
169
|
+
end
|
170
|
+
|
171
|
+
test 'ping against https site works as expected' do
|
172
|
+
@http = Net::Ping::HTTP.new(@uri_https)
|
173
|
+
assert_true(@http.ping)
|
174
|
+
end
|
175
|
+
|
176
|
+
test 'ping with get option' do
|
177
|
+
@http = Net::Ping::HTTP.new(@uri)
|
178
|
+
@http.get_request = true
|
179
|
+
assert_true(@http.ping)
|
180
|
+
end
|
181
|
+
|
182
|
+
test 'ping with http proxy' do
|
183
|
+
ENV['http_proxy'] = "http://proxymoxie:3128"
|
184
|
+
@http = Net::Ping::HTTP.new(@uri)
|
185
|
+
@http.get_request = true
|
186
|
+
assert_true(@http.ping)
|
187
|
+
assert_true(@http.proxied)
|
188
|
+
end
|
189
|
+
|
190
|
+
test 'ping with https proxy' do
|
191
|
+
ENV['https_proxy'] = "http://proxymoxie:3128"
|
192
|
+
@http = Net::Ping::HTTP.new(@uri_https)
|
193
|
+
@http.get_request = true
|
194
|
+
assert_true(@http.ping)
|
195
|
+
assert_true(@http.proxied)
|
196
|
+
end
|
197
|
+
|
198
|
+
test 'ping with no_proxy' do
|
199
|
+
ENV['no_proxy'] = "google.com"
|
200
|
+
@http = Net::Ping::HTTP.new(@uri)
|
201
|
+
@http.get_request = true
|
202
|
+
assert_true(@http.ping)
|
203
|
+
assert_false(@http.proxied)
|
204
|
+
end
|
205
|
+
|
206
|
+
def teardown
|
207
|
+
@uri = nil
|
208
|
+
@http = nil
|
209
|
+
end
|
210
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#######################################################################
|
2
|
+
# test_net_ping_icmp.rb
|
3
|
+
#
|
4
|
+
# Test case for the Net::PingICMP class. You must run this test case
|
5
|
+
# with root privileges on UNIX systems. This should be run via the
|
6
|
+
# 'test' or 'test:icmp' Rake task.
|
7
|
+
#######################################################################
|
8
|
+
require 'test-unit'
|
9
|
+
require 'net/ping/icmp'
|
10
|
+
|
11
|
+
if File::ALT_SEPARATOR
|
12
|
+
require 'win32/security'
|
13
|
+
|
14
|
+
unless Win32::Security.elevated_security?
|
15
|
+
raise "The test:icmp task must be run with elevated security rights"
|
16
|
+
end
|
17
|
+
else
|
18
|
+
unless Process.euid == 0
|
19
|
+
raise "The test:icmp task must be run with root privileges"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class TC_PingICMP < Test::Unit::TestCase
|
24
|
+
def setup
|
25
|
+
@host = '127.0.0.1' # 'localhost'
|
26
|
+
@icmp = Net::Ping::ICMP.new(@host)
|
27
|
+
end
|
28
|
+
|
29
|
+
test "icmp ping basic functionality" do
|
30
|
+
assert_respond_to(@icmp, :ping)
|
31
|
+
assert_nothing_raised{ @icmp.ping }
|
32
|
+
end
|
33
|
+
|
34
|
+
test "icmp ping accepts a host" do
|
35
|
+
assert_nothing_raised{ @icmp.ping(@host) }
|
36
|
+
end
|
37
|
+
|
38
|
+
test "icmp ping returns a boolean" do
|
39
|
+
assert_boolean(@icmp.ping)
|
40
|
+
assert_boolean(@icmp.ping(@host))
|
41
|
+
end
|
42
|
+
|
43
|
+
test "icmp ping of local host is successful" do
|
44
|
+
assert_true(Net::Ping::ICMP.new(@host).ping?)
|
45
|
+
assert_true(Net::Ping::ICMP.new('192.168.0.1').ping?)
|
46
|
+
end
|
47
|
+
|
48
|
+
test "ping? is an alias for ping" do
|
49
|
+
assert_respond_to(@icmp, :ping?)
|
50
|
+
assert_alias_method(@icmp, :ping?, :ping)
|
51
|
+
end
|
52
|
+
|
53
|
+
test "pingecho is an alias for ping" do
|
54
|
+
assert_respond_to(@icmp, :pingecho)
|
55
|
+
assert_alias_method(@icmp, :pingecho, :ping)
|
56
|
+
end
|
57
|
+
|
58
|
+
test "icmp ping fails if host is invalid" do
|
59
|
+
assert_false(Net::Ping::ICMP.new('bogus').ping?)
|
60
|
+
assert_false(Net::Ping::ICMP.new('http://www.asdfhjklasdfhlkj.com').ping?)
|
61
|
+
end
|
62
|
+
|
63
|
+
test "bind method basic functionality" do
|
64
|
+
assert_respond_to(@icmp, :bind)
|
65
|
+
assert_nothing_raised{ @icmp.bind(Socket.gethostname) }
|
66
|
+
assert_nothing_raised{ @icmp.bind(Socket.gethostname, 80) }
|
67
|
+
end
|
68
|
+
|
69
|
+
test "duration method basic functionality" do
|
70
|
+
assert_nothing_raised{ @icmp.ping }
|
71
|
+
assert_respond_to(@icmp, :duration)
|
72
|
+
assert_kind_of(Float, @icmp.duration)
|
73
|
+
end
|
74
|
+
|
75
|
+
test "host getter method basic functionality" do
|
76
|
+
assert_respond_to(@icmp, :host)
|
77
|
+
assert_equal(@host, @icmp.host)
|
78
|
+
end
|
79
|
+
|
80
|
+
test "host setter method basic functionality" do
|
81
|
+
assert_respond_to(@icmp, :host=)
|
82
|
+
assert_nothing_raised{ @icmp.host = '192.168.0.1' }
|
83
|
+
assert_equal(@icmp.host, '192.168.0.1')
|
84
|
+
end
|
85
|
+
|
86
|
+
test "port method basic functionality" do
|
87
|
+
assert_respond_to(@icmp, :port)
|
88
|
+
assert_equal(nil, @icmp.port)
|
89
|
+
end
|
90
|
+
|
91
|
+
test "timeout getter method basic functionality" do
|
92
|
+
assert_respond_to(@icmp, :timeout)
|
93
|
+
assert_equal(5, @icmp.timeout)
|
94
|
+
end
|
95
|
+
|
96
|
+
test "timeout setter method basic functionality" do
|
97
|
+
assert_respond_to(@icmp, :timeout=)
|
98
|
+
assert_nothing_raised{ @icmp.timeout = 7 }
|
99
|
+
assert_equal(7, @icmp.timeout)
|
100
|
+
end
|
101
|
+
|
102
|
+
test "exception method basic functionality" do
|
103
|
+
assert_respond_to(@icmp, :exception)
|
104
|
+
assert_nothing_raised{ @icmp.ping }
|
105
|
+
end
|
106
|
+
|
107
|
+
test "exception method returns nil if no ping has happened yet" do
|
108
|
+
assert_nil(@icmp.exception)
|
109
|
+
end
|
110
|
+
|
111
|
+
test "warning method basic functionality" do
|
112
|
+
assert_respond_to(@icmp, :warning)
|
113
|
+
end
|
114
|
+
|
115
|
+
test "data_size getter method basic functionality" do
|
116
|
+
assert_respond_to(@icmp, :data_size)
|
117
|
+
assert_nothing_raised{ @icmp.data_size }
|
118
|
+
assert_kind_of(Numeric, @icmp.data_size)
|
119
|
+
end
|
120
|
+
|
121
|
+
test "data_size returns expected value" do
|
122
|
+
assert_equal(56, @icmp.data_size)
|
123
|
+
end
|
124
|
+
|
125
|
+
test "data_size setter method basic functionality" do
|
126
|
+
assert_respond_to(@icmp, :data_size=)
|
127
|
+
assert_nothing_raised{ @icmp.data_size = 22 }
|
128
|
+
end
|
129
|
+
|
130
|
+
test "setting an odd data_size is valid" do
|
131
|
+
assert_nothing_raised{ @icmp.data_size = 57 }
|
132
|
+
assert_boolean(@icmp.ping)
|
133
|
+
end
|
134
|
+
|
135
|
+
def teardown
|
136
|
+
@host = nil
|
137
|
+
@icmp = nil
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
#####################################################################
|
2
|
+
# test_net_ping_tcp.rb
|
3
|
+
#
|
4
|
+
# Test case for the Net::PingTCP class. This test should be run via
|
5
|
+
# the 'test' or 'test:tcp' Rake task.
|
6
|
+
#####################################################################
|
7
|
+
require 'test-unit'
|
8
|
+
require 'net/ping/tcp'
|
9
|
+
include Net
|
10
|
+
|
11
|
+
class TC_PingTCP < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
@host = 'localhost'
|
14
|
+
@port = 22
|
15
|
+
@tcp = Ping::TCP.new(@host, @port)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_ping
|
19
|
+
assert_respond_to(@tcp, :ping)
|
20
|
+
assert_nothing_raised{ @tcp.ping }
|
21
|
+
assert_nothing_raised{ @tcp.ping(@host) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_ping_aliases
|
25
|
+
assert_respond_to(@tcp, :ping?)
|
26
|
+
assert_respond_to(@tcp, :pingecho)
|
27
|
+
assert_nothing_raised{ @tcp.ping? }
|
28
|
+
assert_nothing_raised{ @tcp.ping?(@host) }
|
29
|
+
assert_nothing_raised{ @tcp.pingecho }
|
30
|
+
assert_nothing_raised{ @tcp.pingecho(@host) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_ping_service_check_false
|
34
|
+
msg = "+this test may fail depending on your network environment+"
|
35
|
+
Ping::TCP.service_check = false
|
36
|
+
@tcp = Ping::TCP.new('localhost')
|
37
|
+
assert_false(@tcp.ping?, msg)
|
38
|
+
assert_false(@tcp.exception.nil?, "Bad exception data")
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_ping_service_check_true
|
42
|
+
msg = "+this test may fail depending on your network environment+"
|
43
|
+
Ping::TCP.service_check = true
|
44
|
+
assert_true(@tcp.ping?, msg)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_service_check
|
48
|
+
assert_respond_to(Ping::TCP, :service_check)
|
49
|
+
assert_respond_to(Ping::TCP, :service_check=)
|
50
|
+
end
|
51
|
+
|
52
|
+
# These will be removed eventually
|
53
|
+
def test_service_check_aliases
|
54
|
+
assert_respond_to(Ping::TCP, :econnrefused)
|
55
|
+
assert_respond_to(Ping::TCP, :econnrefused=)
|
56
|
+
assert_respond_to(Ping::TCP, :ecr)
|
57
|
+
assert_respond_to(Ping::TCP, :ecr=)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_service_check_expected_errors
|
61
|
+
assert_raises(ArgumentError){ Ping::TCP.service_check = "blah" }
|
62
|
+
end
|
63
|
+
|
64
|
+
# If the ping failed, the duration will be nil
|
65
|
+
def test_duration
|
66
|
+
assert_nothing_raised{ @tcp.ping }
|
67
|
+
assert_respond_to(@tcp, :duration)
|
68
|
+
omit_if(@tcp.duration.nil?, 'ping failed, skipping')
|
69
|
+
assert_kind_of(Float, @tcp.duration)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_host
|
73
|
+
assert_respond_to(@tcp, :host)
|
74
|
+
assert_respond_to(@tcp, :host=)
|
75
|
+
assert_equal(@host, @tcp.host)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_port
|
79
|
+
assert_respond_to(@tcp, :port)
|
80
|
+
assert_respond_to(@tcp, :port=)
|
81
|
+
assert_equal(22, @tcp.port)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_timeout
|
85
|
+
assert_respond_to(@tcp, :timeout)
|
86
|
+
assert_respond_to(@tcp, :timeout=)
|
87
|
+
assert_equal(5, @tcp.timeout)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_exception
|
91
|
+
msg = "+this test may fail depending on your network environment+"
|
92
|
+
assert_respond_to(@tcp, :exception)
|
93
|
+
assert_nothing_raised{ @tcp.ping }
|
94
|
+
assert_nil(@tcp.exception, msg)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_warning
|
98
|
+
assert_respond_to(@tcp, :warning)
|
99
|
+
end
|
100
|
+
|
101
|
+
def teardown
|
102
|
+
@host = nil
|
103
|
+
@tcp = nil
|
104
|
+
end
|
105
|
+
end
|