Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Squashed 0944d8a (check_timeout) into one commit #35

Closed
wants to merge 21 commits into from

3 participants

@ianheggie

Hi I have squashed it all into one commit - let me know which you prefer and I'll kill the other (I prefer this one if you don't care).

Once one or either is merged, then I have some fixes for the bugs that where found to submit.

Squashed 0944d8a (check_timeout) into one commit:

Simplify and expand tests, adding tests for:

  • timeout conditions
  • unreachable hosts and routes
  • open and unused ports

Added test/setup_ubuntu.bash to setup unreachable host, route,
and confirm we can still use telstra.com as a black hole
for pings and tests for ports 7, 22 and 1001

Refactored tests to line up with existing example in external

Add extra rvm versions;
Add IGNORE_HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG and
IGNORE_HTTP_PING_DOES_NOT_TIMEOUT_BUG to test those bugs with
failure allowed;

Reorganise .travis.yml to make intent clearer

Speed up timeout tests

Give more details on failing tests

and others added some commits
@djberg96 Commented out a test that usually failed. 3ccaee9
@ianheggie ianheggie Add travis tests for various ruby versions, with 1.8.7 and jruby mark…
…ed as allowed failures
e391c27
@ianheggie ianheggie Commented out jruby 18mode and 19mode till it is fixed so
we don't repeat three copies of it freezing for 10 minutes
1c0a6f7
@djberg96 Merge pull request #29 from ianheggie/add_travis_tests
Add travis tests for various ruby versions, with 1.8.7 and jruby marked ...
55d00af
@ianheggie ianheggie Add ICMP tests to travis-ci (refactored exclusions to work
with rvmsudo);
Drop ruby 1.8.7 test;
Fix ping of 192.168.0.1 to use 127.0.0.1 instead;
Move handling of EXCLUDE_ICMP_TEST to the same place as other excludes;
9a0748f
@ianheggie ianheggie Add to Known Issues in README based on testing results 6c59c70
@djberg96 Merge pull request #31 from ianheggie/add_icmp_tests_to_travis
Add icmp tests to travis, drop ruby 1.8.7, note known issues discovered in testing
c699a8a
@bcandrea bcandrea Fixed timeouts in Net::Ping::External
Using Timeout.timeout with Open3 is not an option with Ruby > 1.9.3
(see https://bugs.ruby-lang.org/issues/5487). Implemented a workaround
based on the code at https://gist.github.com/lpar/1032297
77fd65a
@bcandrea bcandrea Update travis.yml to trigger CI run
This update should trigger a Travis CI build on different Rubies.
6ae1b88
@djberg96 Merge pull request #32 from bcandrea/develop
Fixed timeouts in Net::Ping::External
61cd0a4
@djberg96 Don't use read_nonblock on Windows. bb83bc9
@bcandrea bcandrea Merge pull request #1 from djberg96/master
Merge back changes from djberg96
c034875
@bcandrea bcandrea Fix a typo in the Net::Ping::External test
This typo had the effect of setting the expected
ping duration to 5.5s instead of 1.5s. Since in Windows
the defaul ping timeout is 4s, the test always passed.
ef80ced
@bcandrea bcandrea Avoid implementing timeout on Windows
The -w option of ping on Windows has the same effect
of an external timeout.
9fe37ec
@bcandrea bcandrea Merge branch 'feature/fix-win32-external-ping' into develop c805658
@djberg96 Merge pull request #33 from bcandrea/develop
Fix a typo in the unit test for Net::Ping::External and change the implementation for Windows
a4ae2bd
@djberg96 Remove comment about win32-open3, add note about minimum Ruby version. 666474e
@ianheggie ianheggie Squashed 0944d8a (check_timeout) into one commit:
coSimplify and expand tests, adding tests for:
* timeout conditions
* unreachable hosts and routes
* open and unused ports

Added test/setup_ubuntu.bash to setup unreachable host, route,
and confirm we can still use telstra.com as a black hole
for pings and tests for ports 7, 22 and 1001

Refactored tests to line up with existing example in external

Add extra rvm versions;
Add IGNORE_HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG and
IGNORE_HTTP_PING_DOES_NOT_TIMEOUT_BUG to test those bugs with
failure allowed;

Reorganise .travis.yml to make intent clearer

Speed up timeout tests

Give more details on failing tests
9fdfc69
@djberg96
Owner

Upon further reflection, I've decided to go back to just using the command line arguments. I think I lost sight of the purpose of the External class, which is to just wrap an external timeout. If folks need more granular timeout control, I think they shouldn't use an external ping to begin with.

I don't want you to think I don't appreciate your contribution - I do! And you will still get credit for spotting the useless Timeout wrapper and providing a solution.

ianheggie added some commits
@ianheggie ianheggie Change UNREACHABLE_HOST to using a script to try and find an unreacha…
…ble host (using iptables doesn't work for host unreachable);

Don't enforce that icmp pings have to wait if they discover network unreachable early;
8453f54
@ianheggie ianheggie Merge remote-tracking branch 'upstream/master'
Conflicts: (resolved in favour of upstream changes):
	README
	lib/net/ping/external.rb
	test/test_net_ping_external.rb
f6bd725
@ianheggie

Just looked at your change to remove time-out from external - I like what you did. I note that you didn't keep the (timeout*1000).to_i.to_s for windows - I suspect that was to make sure there where no fractional milliseconds in the command line arg.

@ianheggie ianheggie referenced this pull request
Closed

Fix icmp enetunreach bug #36

@djberg96 djberg96 closed this
@ianheggie ianheggie deleted the ianheggie:extra_timeout_and_unreachable_tests branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 26, 2014
Commits on Mar 21, 2014
  1. @ianheggie
  2. @ianheggie

    Commented out jruby 18mode and 19mode till it is fixed so

    ianheggie authored
    we don't repeat three copies of it freezing for 10 minutes
Commits on Mar 22, 2014
  1. Merge pull request #29 from ianheggie/add_travis_tests

    authored
    Add travis tests for various ruby versions, with 1.8.7 and jruby marked ...
Commits on Mar 26, 2014
  1. @ianheggie

    Add ICMP tests to travis-ci (refactored exclusions to work

    ianheggie authored
    with rvmsudo);
    Drop ruby 1.8.7 test;
    Fix ping of 192.168.0.1 to use 127.0.0.1 instead;
    Move handling of EXCLUDE_ICMP_TEST to the same place as other excludes;
  2. @ianheggie
  3. Merge pull request #31 from ianheggie/add_icmp_tests_to_travis

    authored
    Add icmp tests to travis, drop ruby 1.8.7, note known issues discovered in testing
  4. @bcandrea

    Fixed timeouts in Net::Ping::External

    bcandrea authored
    Using Timeout.timeout with Open3 is not an option with Ruby > 1.9.3
    (see https://bugs.ruby-lang.org/issues/5487). Implemented a workaround
    based on the code at https://gist.github.com/lpar/1032297
  5. @bcandrea

    Update travis.yml to trigger CI run

    bcandrea authored
    This update should trigger a Travis CI build on different Rubies.
Commits on Mar 27, 2014
  1. Merge pull request #32 from bcandrea/develop

    authored
    Fixed timeouts in Net::Ping::External
  2. @bcandrea

    Merge pull request #1 from djberg96/master

    bcandrea authored
    Merge back changes from djberg96
  3. @bcandrea

    Fix a typo in the Net::Ping::External test

    bcandrea authored
    This typo had the effect of setting the expected
    ping duration to 5.5s instead of 1.5s. Since in Windows
    the defaul ping timeout is 4s, the test always passed.
  4. @bcandrea

    Avoid implementing timeout on Windows

    bcandrea authored
    The -w option of ping on Windows has the same effect
    of an external timeout.
  5. @bcandrea
Commits on Mar 28, 2014
  1. Merge pull request #33 from bcandrea/develop

    authored
    Fix a typo in the unit test for Net::Ping::External and change the implementation for Windows
Commits on Mar 30, 2014
  1. @ianheggie

    Squashed 0944d8a (check_timeout) into one commit:

    ianheggie authored
    coSimplify and expand tests, adding tests for:
    * timeout conditions
    * unreachable hosts and routes
    * open and unused ports
    
    Added test/setup_ubuntu.bash to setup unreachable host, route,
    and confirm we can still use telstra.com as a black hole
    for pings and tests for ports 7, 22 and 1001
    
    Refactored tests to line up with existing example in external
    
    Add extra rvm versions;
    Add IGNORE_HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG and
    IGNORE_HTTP_PING_DOES_NOT_TIMEOUT_BUG to test those bugs with
    failure allowed;
    
    Reorganise .travis.yml to make intent clearer
    
    Speed up timeout tests
    
    Give more details on failing tests
Commits on Apr 2, 2014
  1. @ianheggie

    Change UNREACHABLE_HOST to using a script to try and find an unreacha…

    ianheggie authored
    …ble host (using iptables doesn't work for host unreachable);
    
    Don't enforce that icmp pings have to wait if they discover network unreachable early;
  2. @ianheggie

    Merge remote-tracking branch 'upstream/master'

    ianheggie authored
    Conflicts: (resolved in favour of upstream changes):
    	README
    	lib/net/ping/external.rb
    	test/test_net_ping_external.rb
  3. @ianheggie

    Merge branch 'master' (and upstream) into extra_timeout_and_unreachab…

    ianheggie authored
    …le_tests
    
    Conflicts:
    	test/test_net_ping_external.rb
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -1 +1,2 @@
Gemfile.lock
+*.gem
View
46 .travis.yml
@@ -0,0 +1,46 @@
+language: ruby
+notifications:
+ email:
+ on_success: change
+ on_failure: always
+before_install:
+- gem update --system $RUBYGEMS_VERSION
+- gem --version
+- gem install bundler
+- bundle --version
+- bash test/setup_ubuntu.bash
+bundler_args: --binstubs
+script: rvmsudo_secure_path=1 rvmsudo UNREACHABLE_ROUTE=192.0.2.1 UNREACHABLE_HOST=`test/find_unreachable_host.bash` EXCLUDE=$EXCLUDE bin/rake test
+matrix:
+ include:
+ - rvm: 1.9.2
+ env: EXCLUDE=HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG,ICMP_ENETUNREACH_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: 1.9.3
+ env: EXCLUDE=ICMP_ENETUNREACH_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: 2.0.0
+ env: EXCLUDE=ICMP_ENETUNREACH_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: 2.1.0
+ env: EXCLUDE=ICMP_ENETUNREACH_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ # https://jira.codehaus.org/browse/JRUBY-5897 - jruby doesn't support raw sockets
+ # jruby timeout doesn't work with udp sockets
+ # jruby: tcp tests failing with Errno::ECONNREFUSED to localhost port 22
+ - rvm: jruby-19mode
+ env: EXCLUDE=udp,icmp,tcp,HTTP_PING_DOES_NOT_TIMEOUT_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: rbx-2.2.6
+ env: EXCLUDE=icmp,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ # show bugs
+ - rvm: ruby-head
+ env: TESTOPTS=-v
+ # upcoming releases
+ - rvm: ruby-head
+ env: TESTOPTS=-v EXCLUDE=ICMP_ENETUNREACH_BUG,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: rbx
+ env: TESTOPTS=-v EXCLUDE=icmp,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ - rvm: jruby-head
+ env: TESTOPTS=-v EXCLUDE=udp,icmp,tcp,TCP_UNREACHABLE_ROUTE_BUG,TCP_SERVICE_CHECK_UNUSED_PORT_BUG
+ allow_failures:
+ - rvm: ruby-head
+ - rvm: ruby-head
+ - rvm: rbx
+ - rvm: jruby-head
+
View
2  Gemfile
@@ -1,2 +1,2 @@
-source 'http://rubygems.org'
+source 'https://rubygems.org'
gemspec
View
15 README
@@ -7,6 +7,7 @@
* fakeweb (test only)
* test-unit (test only)
+ Ruby 1.9 or later.
JRuby users should use JRuby 1.6.7 or later.
== Installation
@@ -30,19 +31,29 @@
The former has the advantage of being easier to remember and all inclusive,
not to mention backwards compatible. The latter has the advantage of
reducing your memory footprint.
-
+
== Known Issues
+
+ **UDP**
Older versions of Ruby 1.9.x may not work with UDP pings.
Older versions of JRuby will return false positives in UDP pings
because of an incorrect error class being raised. See JRuby-4896.
+ Jruby hangs if UDP ping doesn't receive a return packet. See JRuby-6974.
+
+ **External**
JRuby 1.6.7 or later is required for external pings because of a bug
in earlier versions with open3 and stream handling.
+ **ICMP**
+ ICMP pings are not supported by JRuby or Rubinus. See JRuby-5897
ICMP pings are not thread safe. See https://www.ruby-forum.com/topic/146116.
Patches welcome.
+ **TCP**
+ TCP pings are not supported under JRuby: they fail with Errno::ECONNREFUSED.
+
== License
Artistic 2.0
@@ -51,7 +62,7 @@
setup a gittip if used by your company professionally.
http://www.gittip.com/djberg96/
-
+
== More documentation
If you installed this library via Rubygems, you can view the inline
documentation via ri or fire up 'gem server', and point your browser at
View
76 lib/net/ping/external.rb
@@ -8,6 +8,9 @@ module Net
# The Ping::External class encapsulates methods for external (system) pings.
class Ping::External < Ping
+
+ ERR_MSG_SIZE = 4096
+
# Pings the host using your system's ping utility and checks for any
# errors or warnings. Returns true if successful, or false if not.
#
@@ -31,6 +34,7 @@ def ping(host = @host)
when /hpux/i
pcmd += [host, '-n1', '-m', @timeout.to_s]
when /win32|windows|msdos|mswin|cygwin|mingw/i
+ #pcmd += ['-n', '1', '-w', (1000 * @timeout).to_i.to_s, host]
pcmd += ['-n', '1', '-w', (@timeout * 1000).to_s, host]
else
pcmd += [host]
@@ -77,6 +81,78 @@ def ping(host = @host)
bool
end
+ # Runs a specified shell command in a separate thread.
+ # If it exceeds the given timeout in seconds, kills it.
+ # Returns a list of the form
+ #
+ # [exit_code, err]
+ #
+ # which contains any output sent by the command to stderr as a String.
+ # Uses Kernel.select to wait up to the timeout length (in seconds)
+ # before killing the process spawned by Open3.
+ #
+ def run_with_timeout(*command)
+ err = nil
+ exit_code = nil
+
+ begin
+ # Start task in another thread, which spawns a process
+ stdin, stdout, stderr, thread = Open3.popen3(*command)
+ # Get the pid of the spawned process
+ pid = thread[:pid]
+ start = Time.now
+
+ while (Time.now - start) < timeout and thread.alive?
+ begin
+ err = stderr.read_nonblock(ERR_MSG_SIZE)
+ rescue IO::WaitReadable
+ IO.select([stderr], nil, nil, @timeout)
+ next
+ rescue EOFError
+ # Command has completed, not really an error...
+ break
+ end
+ end
+
+ if thread.alive?
+ # We need to kill the process, because killing the thread leaves
+ # the process alive but detached, annoyingly enough.
+ begin
+ Process.kill("TERM", pid)
+ err = "execution expired"
+ rescue Errno::ESRCH
+ # The process already exited, ignoring
+ end
+ end
+ # Join the thread and get its exit status
+ exit_code = thread.value.exitstatus
+ ensure
+ stdin.close if stdin
+ stdout.close if stdout
+ stderr.close if stderr
+ end
+ err ||= ''
+ return [exit_code, err]
+ end
+
+ # Runs a command without a timeout. The returned value
+ # is a list of the form
+ #
+ # [exit_code, err]
+ #
+ def run_no_timeout(*command)
+ out, err, exit_code = Open3.capture3(*command)
+ if exit_code != 0 and (err.nil? or err.empty?)
+ out.each_line do |line|
+ if line =~ /(timed out|could not find host)/i
+ err = line
+ break
+ end
+ end
+ end
+ return [exit_code, err]
+ end
+
alias ping? ping
alias pingecho ping
end
View
12 test/find_unreachable_host.bash
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+#
+# Find last unreachable host listed
+
+rm -f /tmp/t$$
+netstat -rn | egrep '255\.255\.255\.0' | while [ ! -s /tmp/t$$ ] && read network ignore_rest
+do
+ echo Checking $network >&2
+ fping -A -g $network/24 -a 2>&1 | grep 'ICMP Host Unreachable ' | tail -1 > /tmp/t$$
+ cat /tmp/t$$ >&2
+done
+sed 's/.* //' /tmp/t$$ | grep .
View
38 test/setup_ubuntu.bash
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+#
+# . test/setup_ubuntu.bash # will setup env variables after testing
+
+set -x
+if ping -n -c2 192.0.2.1 2>&1 | egrep -i network.*unreachable
+then
+ echo Unreachable network is already setup
+else
+ sudo ip route add unreachable 192.0.2.1
+fi
+if egrep '#disable' /etc/xinetd.d/echo
+then
+ echo echo service is setup
+else
+ sudo apt-get update -qq
+ sudo apt-get install xinetd fping -y -qq
+ sudo sed -i.bak -e 's/disable/#disable/' /etc/xinetd.d/echo
+ sudo service xinetd restart
+fi
+
+echo Port 1001 should not be in LISTEN mode
+netstat -l -n | egrep ':1001 '
+
+set -e
+echo Checking everything is working as expected ...
+
+ping -n -c2 192.0.2.1 2>&1 | egrep -i network.*unreachable
+sh -c '! ping -n -c2 144.140.108.23'
+sh -c '! (ping -n -c2 144.140.108.23 2>&1 | egrep -i unreachable )'
+netstat -l -n | egrep ':7 '
+netstat -l -n | egrep ':22 '
+
+export UNREACHABLE_ROUTE=192.0.2.1
+set +e
+export UNREACHABLE_HOST=`test/find_unreachable_host.bash`
+
+set +x
View
43 test/slow_server.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+#
+# Returns a response after 20 seconds
+#
+require 'socket'
+
+host = '127.0.0.1'
+server = TCPServer.new(host, 0)
+if server.respond_to? :addr
+ port = server.addr[1]
+else
+ # rbx doesn't have local_address
+ port = 12345
+ server = TcpSocket.new(host, port)
+end
+delay = (ARGV.first || 20).to_i
+STDOUT.puts port
+STDERR.puts "Slow server waiting for tcp or http://127.0.0.1:#{port} connection."
+STDOUT.flush
+STDERR.flush
+
+client = server.accept()
+STDERR.puts "Got request, Delaying #{delay} seconds"
+STDERR.flush
+Signal.trap("TERM") do
+ STDERR.puts "Terminating as requested..."
+ exit 2
+end
+sleep(delay)
+resp = "Delayed #{delay} seconds\n"
+header = ["HTTP/1.0 200 OK",
+ "Server: Ruby",
+ "Content-Type: text/plain",
+ "Content-Length: #{resp.length}",
+ '',
+ ''].join("\r\n")
+STDERR.puts "Responding with #{header.length} chars in header and #{resp.length} in body ..."
+STDERR.flush
+client.write header
+client.write resp
+client.close
+STDERR.puts "slow_server finished"
+STDERR.flush
View
62 test/test_helper.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+
+# Defaults
+
+class TestHelper
+
+ DEFAULT_BLACKHOLE_IP = '144.140.108.23' # telstra.com - aussie ISP that drops packets
+ DEFAULT_BLACKHOLE_PORT = 1001
+
+ def self.local_tcp_port
+ (ENV['LOCAL_TCP_PORT'] || 22).to_i
+ end
+
+ #def self.local_udp_port
+ # ENV['LOCAL_UDP_PORT'].to_i if ENV['LOCAL_UDP_PORT']
+ #end
+
+ def self.unreachable_host
+ ENV['UNREACHABLE_HOST'] != '' && ENV['UNREACHABLE_HOST']
+ end
+
+ def self.unreachable_route
+ ENV['UNREACHABLE_ROUTE'] != '' && ENV['UNREACHABLE_ROUTE']
+ end
+
+ def self.blackhole_ip
+ if ENV['BLACKHOLE_IP'].to_s =~ /(\S+)/
+ $1
+ else
+ DEFAULT_BLACKHOLE_IP
+ end
+ end
+
+ def self.blackhole_port
+ if ENV['BLACKHOLE_PORT'].to_s =~ /(\d+)/
+ $1.to_i
+ else
+ DEFAULT_BLACKHOLE_PORT
+ end
+ end
+
+ def self.unreachable_host_url
+ 'http://%s:%d/' % [TestHelper.unreachable_host, TestHelper.blackhole_port] if TestHelper.unreachable_host
+ end
+
+ def self.unreachable_route_url
+ 'http://%s:%d/' % [TestHelper.unreachable_route, TestHelper.blackhole_port] if TestHelper.unreachable_route
+ end
+
+ def self.blackhole_url
+ 'http://%s:%d/' % [TestHelper.blackhole_ip, TestHelper.blackhole_port] if TestHelper.blackhole_ip
+ end
+
+ def self.allow_net_connect
+ res = '127.0.0.1'
+ res << '|' << self.unreachable_host if self.unreachable_host
+ res << '|' << self.unreachable_route if self.unreachable_route
+ res << '|' << self.blackhole_ip if self.blackhole_ip
+ %r[^https?://(#{res.gsub('.', '\.')})]
+ end
+
+end
View
55 test/test_net_ping.rb
@@ -4,32 +4,53 @@
# Test suite for all the Ping subclasses. Note that the Ping::ICMP
# class test won't be run unless this is run as a privileged process.
######################################################################
-require 'test_net_ping_external'
-require 'test_net_ping_http'
-require 'test_net_ping_tcp'
-require 'test_net_ping_udp'
-require 'fakeweb'
-if File::ALT_SEPARATOR
- require 'win32/security'
+require File.expand_path('../test_helper.rb', __FILE__)
+$LOAD_PATH << File.expand_path('../', __FILE__)
- if Win32::Security.elevated_security?
- require 'test_net_ping_icmp'
- end
-else
- if Process.euid == 0
- require 'test_net_ping_icmp'
+unless ENV['EXCLUDE'].to_s =~ /external/
+ # ruby 1.8.7 doesn't return thread from popen3, so exitstatus can't be checked
+ require 'test_net_ping_external'
+end
+unless ENV['EXCLUDE'].to_s =~ /http/
+ require 'test_net_ping_http'
+end
+unless ENV['EXCLUDE'].to_s =~ /tcp/
+ require 'test_net_ping_tcp'
+end
+unless ENV['EXCLUDE'].to_s =~ /udp/
+ # http://jira.codehaus.org/browse/JRUBY-6974 - Timeout.timeout not working using UDPSocket
+ require 'test_net_ping_udp'
+end
+
+unless ENV['EXCLUDE'].to_s =~ /icmp/
+ if File::ALT_SEPARATOR
+ require 'win32/security'
+
+ if Win32::Security.elevated_security?
+ require 'test_net_ping_icmp'
+ end
+ else
+ if Process.euid == 0
+ require 'test_net_ping_icmp'
+ end
end
end
-if File::ALT_SEPARATOR
- require 'test_net_ping_wmi'
+unless ENV['EXCLUDE'].to_s =~ /wmi/
+ if File::ALT_SEPARATOR
+ require 'test_net_ping_wmi'
+ end
end
class TC_Net_Ping < Test::Unit::TestCase
def test_net_ping_version
assert_equal('1.7.2', Net::Ping::VERSION)
end
-end
-FakeWeb.allow_net_connect = false
+ def test_test_helper_allow_net_connect
+ assert_true( !!("http://127.0.0.1/fred" =~ TestHelper.allow_net_connect))
+ assert_false( !! ("http://127.0.1.1/fred" =~ TestHelper.allow_net_connect))
+ end
+
+end
View
85 test/test_net_ping_external.rb 100644 → 100755
@@ -10,13 +10,17 @@
#########################################################################
require 'test-unit'
require 'net/ping/external'
+require File.expand_path('../test_helper.rb', __FILE__)
class TC_Net_Ping_External < Test::Unit::TestCase
def setup
- @host = 'localhost'
- @bogus = 'foo.bar.baz'
- @pe = Net::Ping::External.new(@host)
- @bad = Net::Ping::External.new(@bogus)
+ @host = 'localhost'
+ @bogus = 'foo.bar.baz'
+ @pe = Net::Ping::External.new(@host)
+ @bad = Net::Ping::External.new(@bogus)
+ @blackhole = Net::Ping::External.new(TestHelper.blackhole_ip)
+ @unreachable_host = TestHelper.unreachable_host && Net::Ping::External.new(TestHelper.unreachable_host)
+ @unreachable_route = TestHelper.unreachable_route && Net::Ping::External.new(TestHelper.unreachable_route)
end
test "ping basic functionality" do
@@ -120,6 +124,69 @@ def setup
assert_nil(@pe.warning)
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 unreacable 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
+
test "timing out causes expected result" do
ext = Net::Ping::External.new('foo.bar.baz', nil, 1)
start = Time.now
@@ -130,9 +197,11 @@ def setup
end
def teardown
- @host = nil
- @bogus = nil
- @pe = nil
- @bad = nil
+ @host = nil
+ @bogus = nil
+ @bcast_ip = nil
+ @pe = nil
+ @bad = nil
+ @unreachable = nil
end
end
View
112 test/test_net_ping_http.rb 100644 → 100755
@@ -7,6 +7,7 @@
require 'test-unit'
require 'fakeweb'
require 'net/ping/http'
+require File.expand_path('../test_helper.rb', __FILE__)
class TC_Net_Ping_HTTP < Test::Unit::TestCase
def setup
@@ -14,7 +15,7 @@ def setup
@uri = 'http://www.google.com/index.html'
@uri_https = 'https://encrypted.google.com'
@proxy = 'http://username:password@proxymoxie:3128'
- FakeWeb.allow_net_connect = false
+ FakeWeb.allow_net_connect = TestHelper.allow_net_connect
FakeWeb.register_uri(:get, @uri, :body => "PONG")
FakeWeb.register_uri(:head, @uri, :body => "PONG")
@@ -30,8 +31,13 @@ def setup
:body => "",
:status => ["502", "Bad Gateway"])
+
@http = Net::Ping::HTTP.new(@uri, 80, 30)
@bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
+
+ @blackhole = Net::Ping::HTTP.new(TestHelper.blackhole_url)
+ @unreachable_host = TestHelper.unreachable_host_url && Net::Ping::HTTP.new(TestHelper.unreachable_host_url)
+ @unreachable_route = TestHelper.unreachable_route_url && Net::Ping::HTTP.new(TestHelper.unreachable_route_url)
end
test 'ping basic functionality' do
@@ -60,6 +66,23 @@ def setup
assert_false(@bad.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 'duration basic functionality' do
assert_respond_to(@http, :duration)
assert_nothing_raised{ @http.ping }
@@ -105,6 +128,93 @@ def setup
assert_equal(5, @bad.timeout)
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 unreacable 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 black hole 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 black hole 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 slow_server
+ echo_cmd = File.expand_path('../slow_server.rb', __FILE__)
+ echo_process = IO.popen('ruby %s' % echo_cmd, 'r')
+ port = echo_process.gets.to_i
+ [ echo_process, port ]
+ end
+
+ test "pinging a slow host returns after the timeout" do
+ omit_if(ENV['EXCLUDE'].to_s =~ /HTTP_PING_DOES_NOT_TIMEOUT_BUG/, 'HTTP_PING_DOES_NOT_TIMEOUT_BUG excluded')
+ echo_process, port = slow_server
+ slow = Net::Ping::HTTP.new('http://127.0.0.1/', port)
+ slow.timeout = 1
+ tolerance = 0.5
+ start_time = Time.now
+ res = slow.ping
+ elapsed = Time.now - start_time
+ Process.kill('TERM', echo_process.pid)
+ echo_process.close
+ assert_true(elapsed < slow.timeout + tolerance,
+ 'Expected elapsed (%1.1f) to be < timeout (%d) + tolerance (%1.1f), ping = %s, exception = %s' %
+ [elapsed, slow.timeout, tolerance, res, slow.exception.inspect])
+ end
+
+ test "pinging a slow host waits for timeout" do
+ omit_if(ENV['EXCLUDE'].to_s =~ /HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG/, 'HTTP_PING_DOES_NOT_WAITS_FOR_RESPONSE_BUG excluded')
+ echo_process, port = slow_server
+ slow = Net::Ping::HTTP.new('http://127.0.0.1/', port)
+ slow.timeout = 3
+ tolerance = 0.5
+ start_time = Time.now
+ res= slow.ping
+ elapsed = Time.now - start_time
+ Process.kill('TERM', echo_process.pid)
+ echo_process.close
+ assert_true(elapsed > slow.timeout - tolerance,
+ 'Expected elapsed (%1.1f) to be > timeout (%d) - tolerance (%1.1f), ping = %s, exception = %s' %
+ [elapsed, slow.timeout, tolerance, res, slow.exception.inspect])
+ end
+
+
test 'exception attribute basic functionality' do
assert_respond_to(@http, :exception)
assert_nil(@http.exception)
View
83 test/test_net_ping_icmp.rb 100644 → 100755
@@ -7,6 +7,7 @@
#######################################################################
require 'test-unit'
require 'net/ping/icmp'
+require File.expand_path('../test_helper.rb', __FILE__)
if File::ALT_SEPARATOR
require 'win32/security'
@@ -24,6 +25,13 @@ 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
@@ -42,7 +50,7 @@ def setup
test "icmp ping of local host is successful" do
assert_true(Net::Ping::ICMP.new(@host).ping?)
- assert_true(Net::Ping::ICMP.new('192.168.0.1').ping?)
+ assert_true(Net::Ping::ICMP.new('127.0.0.1').ping?)
end
test "ping? is an alias for ping" do
@@ -79,8 +87,8 @@ def setup
test "host setter method basic functionality" do
assert_respond_to(@icmp, :host=)
- assert_nothing_raised{ @icmp.host = '192.168.0.1' }
- assert_equal(@icmp.host, '192.168.0.1')
+ assert_nothing_raised{ @icmp.host = '127.0.0.1' }
+ assert_equal(@icmp.host, '127.0.0.1')
end
test "port method basic functionality" do
@@ -132,8 +140,77 @@ def setup
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
View
142 test/test_net_ping_tcp.rb 100644 → 100755
@@ -7,12 +7,18 @@
require 'test-unit'
require 'net/ping/tcp'
include Net
+require File.expand_path('../test_helper.rb', __FILE__)
class TC_PingTCP < Test::Unit::TestCase
def setup
+ Ping::TCP.service_check = false
@host = 'localhost'
- @port = 22
+ @port = TestHelper.local_tcp_port
@tcp = Ping::TCP.new(@host, @port)
+ @tcp_unused_port = Ping::TCP.new(@host, TestHelper.blackhole_port)
+ @blackhole = Net::Ping::TCP.new(TestHelper.blackhole_ip, TestHelper.blackhole_port)
+ @unreachable_host = TestHelper.unreachable_host && Net::Ping::TCP.new(TestHelper.unreachable_host, TestHelper.blackhole_port)
+ @unreachable_route = TestHelper.unreachable_route && Net::Ping::TCP.new(TestHelper.unreachable_route, TestHelper.blackhole_port)
end
def test_ping
@@ -30,37 +36,53 @@ def test_ping_aliases
assert_nothing_raised{ @tcp.pingecho(@host) }
end
- def test_ping_service_check_false
- msg = "+this test may fail depending on your network environment+"
- Ping::TCP.service_check = false
- @tcp = Ping::TCP.new('localhost')
- assert_false(@tcp.ping?, msg)
- assert_false(@tcp.exception.nil?, "Bad exception data")
+ # Decided to comment this out, pretty much always failed. Need a better test.
+ #def test_ping_service_check_false
+ # msg = "+this test may fail depending on your network environment+"
+ # Ping::TCP.service_check = false
+ # @tcp = Ping::TCP.new('localhost')
+ # assert_false(@tcp.ping?, msg)
+ # assert_false(@tcp.exception.nil?, "Bad exception data")
+ #end
+
+ def test_ping_service_check_true_on_open_port
+ msg = "checks localhost port #{@tcp.port} is alive +this test will fail if that port is not open on this system+"
+ Ping::TCP.service_check = true
+ res = @tcp.ping?
+ assert_true(res, msg + ", exception = #{@tcp.exception}")
end
- def test_ping_service_check_true
- msg = "+this test may fail depending on your network environment+"
+ def test_ping_service_check_true_on_unused_port
+ omit_if(ENV['EXCLUDE'].to_s =~ /TCP_SERVICE_CHECK_UNUSED_PORT_BUG/)
+ msg = "checks localhost port #{@tcp_unused_port.port} is NOT alive +this test will fail if that port IS open on this system+"
Ping::TCP.service_check = true
- assert_true(@tcp.ping?, msg)
+ res = @tcp_unused_port.ping?
+ assert_false(res, msg + ", exception = #{@tcp_unused_port.exception}")
+ end
+
+ def test_ping_service_check_false_on_unused_port
+ msg = "checks localhost port #{@tcp_unused_port.port}, but should return true even if it is closed"
+ res = @tcp_unused_port.ping?
+ assert_true(res, msg + ", exception = #{@tcp_unused_port.exception}")
end
-
+
def test_service_check
assert_respond_to(Ping::TCP, :service_check)
- assert_respond_to(Ping::TCP, :service_check=)
+ assert_respond_to(Ping::TCP, :service_check=)
end
-
+
# These will be removed eventually
def test_service_check_aliases
assert_respond_to(Ping::TCP, :econnrefused)
assert_respond_to(Ping::TCP, :econnrefused=)
assert_respond_to(Ping::TCP, :ecr)
- assert_respond_to(Ping::TCP, :ecr=)
+ assert_respond_to(Ping::TCP, :ecr=)
end
-
+
def test_service_check_expected_errors
assert_raises(ArgumentError){ Ping::TCP.service_check = "blah" }
end
-
+
# If the ping failed, the duration will be nil
def test_duration
assert_nothing_raised{ @tcp.ping }
@@ -87,6 +109,94 @@ def test_timeout
assert_equal(5, @tcp.timeout)
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 && ENV['EXCLUDE'].to_s !~ /TCP_UNREACHABLE_ROUTE_BUG/)
+ @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 unreacable 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 black hole 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 black hole 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 slow_server
+ echo_cmd = File.expand_path('../slow_server.rb', __FILE__)
+ echo_process = IO.popen('ruby %s' % echo_cmd, 'r')
+ port = echo_process.gets.to_i
+ [ echo_process, port ]
+ end
+
+ test "pinging a slow host returns without waiting for timeout" do
+ echo_process, port = slow_server
+ slow = Net::Ping::TCP.new('127.0.0.1', port)
+ slow.timeout = 3
+ tolerance = 0.9
+ start_time = Time.now
+ res = slow.ping
+ elapsed = Time.now - start_time
+ Process.kill('TERM', echo_process.pid)
+ echo_process.close
+ assert_true(elapsed < slow.timeout + tolerance,
+ 'Expected elapsed (%1.1f) to be < tolerance (%1.1f), ping = %s, exception = %s' %
+ [elapsed, tolerance, res, slow.exception.inspect])
+ end
+
def test_exception
msg = "+this test may fail depending on your network environment+"
assert_respond_to(@tcp, :exception)
View
93 test/test_net_ping_udp.rb 100644 → 100755
@@ -9,12 +9,17 @@
########################################################################
require 'test-unit'
require 'net/ping/udp'
+require File.expand_path('../test_helper.rb', __FILE__)
class TC_Net_Ping_UDP < Test::Unit::TestCase
def setup
Net::Ping::UDP.service_check = false
@host = '127.0.0.1'
@udp = Net::Ping::UDP.new(@host)
+ @udp_unused_port = Net::Ping::UDP.new(@host, TestHelper.blackhole_port)
+ @blackhole = Net::Ping::UDP.new(TestHelper.blackhole_ip, TestHelper.blackhole_port)
+ @unreachable_host = TestHelper.unreachable_host && Net::Ping::UDP.new(TestHelper.unreachable_host, TestHelper.blackhole_port)
+ @unreachable_route = TestHelper.unreachable_route && Net::Ping::UDP.new(TestHelper.unreachable_route, TestHelper.blackhole_port)
end
test "ping basic functionality" do
@@ -112,6 +117,94 @@ def setup
assert_raise(ArgumentError){ Net::Ping::UDP.service_check = 1 }
end
+ def test_ping_service_check_true_on_open_port
+ msg = "checks localhost port #{@udp.port} is alive +this test will fail if that port is not open on this system+"
+ Ping::UDP.service_check = true
+ res = @udp.ping?
+ assert_true(res, msg + ", exception = #{@udp.exception}")
+ end
+
+ def test_ping_service_check_true_on_closed_port
+ msg = "checks localhost port #{@udp_unused_port.port} is NOT alive +this test will fail if that port IS open on this system+"
+ Ping::UDP.service_check = true
+ res = @udp_unused_port.ping?
+ assert_false(res, msg + ", exception = #{@udp_unused_port.exception}")
+ end
+
+ def test_ping_service_check_false_on_closed_port
+ msg = "checks localhost port #{@udp_unused_port.port}, but should return true even if it is closed"
+ res = @udp_unused_port.ping?
+ assert_true(res, msg + ", exception = #{@udp_unused_port.exception}")
+ 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 unreacable 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 black hole 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 black hole 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
@udp = nil
View
0  test/test_net_ping_wmi.rb 100644 → 100755
File mode changed
Something went wrong with that request. Please try again.