forked from djberg96/net-ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_net_ping_icmp.rb
executable file
·216 lines (180 loc) · 6.74 KB
/
test_net_ping_icmp.rb
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#######################################################################
# test_net_ping_icmp.rb
#
# Test case for the Net::PingICMP class. You must run this test case
# with root privileges on UNIX systems. This should be run via the
# 'test' or 'test:icmp' Rake task.
#######################################################################
require 'test-unit'
require 'net/ping/icmp'
require File.expand_path('../test_helper.rb', __FILE__)
if File::ALT_SEPARATOR
require 'win32/security'
unless Win32::Security.elevated_security?
raise "The test:icmp task must be run with elevated security rights"
end
else
unless Process.euid == 0
raise "The test:icmp task must be run with root privileges"
end
end
class TC_PingICMP < Test::Unit::TestCase
def setup
@host = '127.0.0.1' # 'localhost'
@icmp = Net::Ping::ICMP.new(@host)
@blackhole = Net::Ping::ICMP.new(TestHelper.blackhole_ip)
if ENV['EXCLUDE'].to_s =~ /ICMP_ENETUNREACH_BUG/
@unreachable_host = @unreachable_route = nil
else
@unreachable_host = TestHelper.unreachable_host && Net::Ping::ICMP.new(TestHelper.unreachable_host)
@unreachable_route = TestHelper.unreachable_route && Net::Ping::ICMP.new(TestHelper.unreachable_route)
end
end
test "icmp ping basic functionality" do
assert_respond_to(@icmp, :ping)
assert_nothing_raised{ @icmp.ping }
end
test "icmp ping accepts a host" do
assert_nothing_raised{ @icmp.ping(@host) }
end
test "icmp ping returns a boolean" do
assert_boolean(@icmp.ping)
assert_boolean(@icmp.ping(@host))
end
test "icmp ping of local host is successful" do
assert_true(Net::Ping::ICMP.new(@host).ping?)
assert_true(Net::Ping::ICMP.new('127.0.0.1').ping?)
end
test "ping? is an alias for ping" do
assert_respond_to(@icmp, :ping?)
assert_alias_method(@icmp, :ping?, :ping)
end
test "pingecho is an alias for ping" do
assert_respond_to(@icmp, :pingecho)
assert_alias_method(@icmp, :pingecho, :ping)
end
test "icmp ping fails if host is invalid" do
assert_false(Net::Ping::ICMP.new('bogus').ping?)
assert_false(Net::Ping::ICMP.new('http://www.asdfhjklasdfhlkj.com').ping?)
end
test "bind method basic functionality" do
assert_respond_to(@icmp, :bind)
assert_nothing_raised{ @icmp.bind(Socket.gethostname) }
assert_nothing_raised{ @icmp.bind(Socket.gethostname, 80) }
end
test "duration method basic functionality" do
assert_nothing_raised{ @icmp.ping }
assert_respond_to(@icmp, :duration)
assert_kind_of(Float, @icmp.duration)
end
test "host getter method basic functionality" do
assert_respond_to(@icmp, :host)
assert_equal(@host, @icmp.host)
end
test "host setter method basic functionality" do
assert_respond_to(@icmp, :host=)
assert_nothing_raised{ @icmp.host = '127.0.0.1' }
assert_equal(@icmp.host, '127.0.0.1')
end
test "port method basic functionality" do
assert_respond_to(@icmp, :port)
assert_equal(nil, @icmp.port)
end
test "timeout getter method basic functionality" do
assert_respond_to(@icmp, :timeout)
assert_equal(5, @icmp.timeout)
end
test "timeout setter method basic functionality" do
assert_respond_to(@icmp, :timeout=)
assert_nothing_raised{ @icmp.timeout = 7 }
assert_equal(7, @icmp.timeout)
end
test "exception method basic functionality" do
assert_respond_to(@icmp, :exception)
assert_nothing_raised{ @icmp.ping }
end
test "exception method returns nil if no ping has happened yet" do
assert_nil(@icmp.exception)
end
test "warning method basic functionality" do
assert_respond_to(@icmp, :warning)
end
test "data_size getter method basic functionality" do
assert_respond_to(@icmp, :data_size)
assert_nothing_raised{ @icmp.data_size }
assert_kind_of(Numeric, @icmp.data_size)
end
test "data_size returns expected value" do
assert_equal(56, @icmp.data_size)
end
test "data_size setter method basic functionality" do
assert_respond_to(@icmp, :data_size=)
assert_nothing_raised{ @icmp.data_size = 22 }
end
test "setting an odd data_size is valid" do
assert_nothing_raised{ @icmp.data_size = 57 }
assert_boolean(@icmp.ping)
end
test 'ping should fail for an unreachable website' do
omit_unless(@unreachable_host)
@unreachable_host.timeout = 3
assert_false(@unreachable_host.ping?)
end
test 'ping should fail for a website on an unreachable route' do
omit_unless(@unreachable_route)
@unreachable_route.timeout = 3
assert_false(@unreachable_route.ping?)
end
test 'ping should fail for a black hole' do
@blackhole.timeout = 3
assert_false(@blackhole.ping?)
end
test "pinging an unreachable host returns after the timeout" do
omit_unless(@unreachable_host)
@unreachable_host.timeout = 1
tolerance = 0.5
start_time = Time.now
res = @unreachable_host.ping
elapsed = Time.now - start_time
assert_true(elapsed < @unreachable_host.timeout + tolerance,
'Expected elapsed (%1.1f) to be < timeout (%d) + tolerance (%1.1f), ping = %s, exception = %s' %
[elapsed, @unreachable_host.timeout, tolerance, res, @unreachable_host.exception.inspect])
end
test "pinging a host on an unreachable network returns after the timeout" do
omit_unless(@unreachable_route)
@unreachable_route.timeout = 1
tolerance = 0.5
start_time = Time.now
res = @unreachable_route.ping
elapsed = Time.now - start_time
assert_true(elapsed < @unreachable_route.timeout + tolerance,
'Expected elapsed (%1.1f) to be < timeout (%d) + tolerance (%1.1f), ping = %s, exception = %s' %
[elapsed, @unreachable_route.timeout, tolerance, res, @unreachable_route.exception.inspect])
end
test "pinging a blackhole returns after the timeout" do
@blackhole.timeout = 1
tolerance = 0.5
start_time = Time.now
res = @blackhole.ping
elapsed = Time.now - start_time
assert_true(elapsed < @blackhole.timeout + tolerance,
'Expected elapsed (%1.1f) to be < timeout (%d) + tolerance (%1.1f), ping = %s, exception = %s' %
[elapsed, @blackhole.timeout, tolerance, res, @blackhole.exception.inspect])
end
test "pinging a blackhole waits for timeout" do
@blackhole.timeout = 3
tolerance = 0.5
start_time = Time.now
res = @blackhole.ping
elapsed = Time.now - start_time
assert_true(elapsed > @blackhole.timeout - tolerance,
'Expected elapsed (%1.1f) to be > timeout (%d) - tolerance (%1.1f), ping = %s, exception = %s' %
[elapsed, @blackhole.timeout, tolerance, res, @blackhole.exception.inspect])
end
def teardown
@host = nil
@icmp = nil
@unreachable = nil
@reserved_for_docs = nil
end
end