net-ping 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+ require 'ping'
3
+
4
+ module Net
5
+ class Ping::UDP < Ping
6
+ MAX_DATA = 64
7
+
8
+ # The data to send to the remote host. By default this is 'ping'.
9
+ # This should be MAX_DATA size characters or less.
10
+ #
11
+ attr_reader :data
12
+
13
+ # Creates and returns a new Ping::UDP object. This is effectively
14
+ # identical to its superclass constructor.
15
+ #
16
+ def initialize(host=nil, port=nil, timeout=5)
17
+ @data = 'ping'
18
+
19
+ super(host, port, timeout)
20
+
21
+ @bind_host = nil
22
+ @bind_port = nil
23
+ end
24
+
25
+ # Sets the data string sent to the remote host. This value cannot have
26
+ # a size greater than MAX_DATA.
27
+ #
28
+ def data=(string)
29
+ if string.size > MAX_DATA
30
+ err = "cannot set data string larger than #{MAX_DATA} characters"
31
+ raise ArgumentError, err
32
+ end
33
+
34
+ @data = string
35
+ end
36
+
37
+ # Associates the local end of the UDP connection with the given +host+
38
+ # and +port+. This is essentially a wrapper for UDPSocket#bind.
39
+ #
40
+ def bind(host, port)
41
+ @bind_host = host
42
+ @bind_port = port
43
+ end
44
+
45
+ # Sends a simple text string to the host and checks the return string. If
46
+ # the string sent and the string returned are a match then the ping was
47
+ # successful and true is returned. Otherwise, false is returned.
48
+ #
49
+ def ping(host = @host)
50
+ super(host)
51
+
52
+ bool = false
53
+ udp = UDPSocket.open
54
+ array = []
55
+
56
+ if @bind_host
57
+ udp.bind(@bind_host, @bind_port)
58
+ end
59
+
60
+ start_time = Time.now
61
+
62
+ begin
63
+ Timeout.timeout(@timeout){
64
+ udp.connect(host, @port)
65
+ udp.send(@data, 0)
66
+ array = udp.recvfrom(MAX_DATA)
67
+ }
68
+ rescue Exception => err
69
+ @exception = err
70
+ else
71
+ if array[0] == @data
72
+ bool = true
73
+ end
74
+ ensure
75
+ udp.close if udp
76
+ end
77
+
78
+ # There is no duration if the ping failed
79
+ @duration = Time.now - start_time if bool
80
+
81
+ bool
82
+ end
83
+
84
+ alias ping? ping
85
+ alias pingecho ping
86
+ end
87
+
88
+ # Class alias for backwards compatibility
89
+ PingUDP = Ping::UDP
90
+ end
@@ -11,26 +11,26 @@ if base == "test" || base =~ /net-pingsimple.*/
11
11
  end
12
12
 
13
13
  require "test/unit"
14
- require "net/ping"
14
+ require "net/ping/external"
15
15
  include Net
16
16
 
17
17
  class TC_PingExternal < Test::Unit::TestCase
18
18
  def setup
19
19
  @host = "www.ruby-lang.org"
20
20
  @bogus = "foo.bar.baz"
21
- @pe = PingExternal.new(@host)
22
- @bad = PingExternal.new(@bogus)
21
+ @pe = Ping::External.new(@host)
22
+ @bad = Ping::External.new(@bogus)
23
23
  end
24
24
 
25
25
  def test_version
26
- assert_equal("1.1.1", PingExternal::VERSION, "Bad version constant")
26
+ assert_equal("1.2.0", PingExternal::VERSION)
27
27
  end
28
28
 
29
29
  def test_ping
30
30
  assert_respond_to(@pe, :ping)
31
31
  assert_respond_to(@pe, :ping?)
32
32
  assert_nothing_raised{ @pe.ping }
33
- assert_raises(ArgumentError){ @pe.ping(@host) }
33
+ assert_nothing_raised{ @pe.ping(@host) }
34
34
  end
35
35
 
36
36
  def test_good_ping
@@ -42,14 +42,39 @@ class TC_PingExternal < Test::Unit::TestCase
42
42
  assert_equal(false, @bad.exception.nil?, "Bad exception data")
43
43
  end
44
44
 
45
- def test_accessors
45
+ def test_duration
46
+ assert_nothing_raised{ @pe.ping }
47
+ assert_respond_to(@pe, :duration)
48
+ assert_kind_of(Float, @pe.duration)
49
+ end
50
+
51
+ def test_host
46
52
  assert_respond_to(@pe, :host)
47
53
  assert_respond_to(@pe, :host=)
54
+ assert_equal('www.ruby-lang.org', @pe.host)
55
+ end
56
+
57
+ def test_port
48
58
  assert_respond_to(@pe, :port)
49
59
  assert_respond_to(@pe, :port=)
60
+ assert_equal(7, @pe.port)
61
+ end
62
+
63
+ def test_timeout
50
64
  assert_respond_to(@pe, :timeout)
51
65
  assert_respond_to(@pe, :timeout=)
66
+ assert_equal(5, @pe.timeout)
67
+ end
68
+
69
+ def test_exception
52
70
  assert_respond_to(@pe, :exception)
71
+ assert_nothing_raised{ @pe.ping }
72
+ assert_nothing_raised{ @bad.ping }
73
+ assert_nil(@pe.exception)
74
+ assert_not_nil(@bad.exception)
75
+ end
76
+
77
+ def test_warning
53
78
  assert_respond_to(@pe, :warning)
54
79
  end
55
80
 
data/test/tc_pinghttp.rb CHANGED
@@ -11,18 +11,18 @@ if base == "test" || base =~ /net-pingsimple.*/
11
11
  end
12
12
 
13
13
  require "test/unit"
14
- require "net/ping"
14
+ require "net/ping/http"
15
15
  include Net
16
16
 
17
17
  class TC_PingHTTP < Test::Unit::TestCase
18
18
  def setup
19
19
  @uri = "http://www.pragmaticprogrammer.com/index.html"
20
- @http = PingHTTP.new(@uri)
21
- @bad = PingHTTP.new("http://www.blabfoobarurgh.com")
20
+ @http = Ping::HTTP.new(@uri)
21
+ @bad = Ping::HTTP.new("http://www.blabfoobarurgh.com") # One hopes not
22
22
  end
23
23
 
24
24
  def test_version
25
- assert_equal("1.1.1", PingHTTP::VERSION, "Bad version constant")
25
+ assert_equal("1.2.0", PingHTTP::VERSION)
26
26
  end
27
27
 
28
28
  def test_ping
@@ -38,17 +38,42 @@ class TC_PingHTTP < Test::Unit::TestCase
38
38
  assert_not_nil(@bad.exception)
39
39
  end
40
40
 
41
- def test_accessors
41
+ def test_duration
42
+ assert_nothing_raised{ @http.ping }
43
+ assert_respond_to(@http, :duration)
44
+ assert_kind_of(Float, @http.duration)
45
+ end
46
+
47
+ def test_host
42
48
  assert_respond_to(@http, :host)
43
49
  assert_respond_to(@http, :host=)
50
+ assert_respond_to(@http, :uri) # Alias
51
+ assert_respond_to(@http, :uri=) # Alias
52
+ assert_equal('http://www.pragmaticprogrammer.com/index.html', @http.host)
53
+ end
54
+
55
+ def test_port
44
56
  assert_respond_to(@http, :port)
45
57
  assert_respond_to(@http, :port=)
58
+ assert_equal(80, @http.port)
59
+ end
60
+
61
+ def test_timeout
46
62
  assert_respond_to(@http, :timeout)
47
63
  assert_respond_to(@http, :timeout=)
64
+ assert_equal(5, @http.timeout)
65
+ end
66
+
67
+ def test_exception
48
68
  assert_respond_to(@http, :exception)
69
+ assert_nothing_raised{ @http.ping }
70
+ assert_nothing_raised{ @bad.ping }
71
+ assert_nil(@http.exception)
72
+ assert_not_nil(@bad.exception)
73
+ end
74
+
75
+ def test_warning
49
76
  assert_respond_to(@http, :warning)
50
- assert_respond_to(@http, :uri)
51
- assert_respond_to(@http, :uri=)
52
77
  end
53
78
 
54
79
  def teardown
@@ -0,0 +1,84 @@
1
+ #######################################################################
2
+ # tc_pingicmp.rb
3
+ #
4
+ # Test case for the Net::PingICMP class. You must run this test case
5
+ # with root privileges on UNIX systems.
6
+ #######################################################################
7
+ base = File.basename(Dir.pwd)
8
+
9
+ if base == "test" || base =~ /net-pingsimple.*/
10
+ Dir.chdir("..") if base == "test"
11
+ $LOAD_PATH.unshift(Dir.pwd + "/lib")
12
+ end
13
+
14
+ require "test/unit"
15
+ require "net/ping/icmp"
16
+ include Net
17
+
18
+ class TC_PingICMP < Test::Unit::TestCase
19
+ def setup
20
+ @host = "www.ruby-lang.org"
21
+ @icmp = Ping::ICMP.new(@host)
22
+ end
23
+
24
+ def test_version
25
+ assert_equal("1.2.0", PingICMP::VERSION)
26
+ end
27
+
28
+ def test_ping
29
+ assert_respond_to(@icmp, :ping)
30
+ assert_respond_to(@icmp, :ping?)
31
+ assert_nothing_raised{ @icmp.ping }
32
+ assert_nothing_raised{ @icmp.ping(@host) }
33
+ end
34
+
35
+ def test_bind
36
+ assert_respond_to(@icmp, :bind)
37
+ assert_nothing_raised{ @icmp.bind(Socket.gethostname) }
38
+ assert_nothing_raised{ @icmp.bind(Socket.gethostname, 80) }
39
+ end
40
+
41
+ def test_duration
42
+ assert_nothing_raised{ @icmp.ping }
43
+ assert_respond_to(@icmp, :duration)
44
+ assert_kind_of(Float, @icmp.duration)
45
+ end
46
+
47
+ def test_duration
48
+ assert_nothing_raised{ @icmp.ping }
49
+ assert_respond_to(@icmp, :duration)
50
+ assert_kind_of(Float, @icmp.duration)
51
+ end
52
+
53
+ def test_host
54
+ assert_respond_to(@icmp, :host)
55
+ assert_respond_to(@icmp, :host=)
56
+ assert_equal('www.ruby-lang.org', @icmp.host)
57
+ end
58
+
59
+ def test_port
60
+ assert_respond_to(@icmp, :port)
61
+ assert_equal(nil, @icmp.port)
62
+ end
63
+
64
+ def test_timeout
65
+ assert_respond_to(@icmp, :timeout)
66
+ assert_respond_to(@icmp, :timeout=)
67
+ assert_equal(5, @icmp.timeout)
68
+ end
69
+
70
+ def test_exception
71
+ assert_respond_to(@icmp, :exception)
72
+ assert_nothing_raised{ @icmp.ping }
73
+ assert_nil(@icmp.exception)
74
+ end
75
+
76
+ def test_warning
77
+ assert_respond_to(@icmp, :warning)
78
+ end
79
+
80
+ def teardown
81
+ @host = nil
82
+ @icmp = nil
83
+ end
84
+ end
data/test/tc_pingtcp.rb CHANGED
@@ -11,24 +11,24 @@ if base == "test" || base =~ /net-pingsimple.*/
11
11
  end
12
12
 
13
13
  require "test/unit"
14
- require "net/ping"
14
+ require "net/ping/tcp"
15
15
  include Net
16
16
 
17
17
  class TC_PingTCP < Test::Unit::TestCase
18
18
  def setup
19
19
  @host = "www.ruby-lang.org"
20
- @tcp = PingTCP.new(@host, "http")
20
+ @tcp = Ping::TCP.new(@host)
21
21
  end
22
22
 
23
23
  def test_version
24
- assert_equal("1.1.1", PingTCP::VERSION, "Bad version constant")
24
+ assert_equal("1.2.0", PingTCP::VERSION, "Bad version constant")
25
25
  end
26
26
 
27
27
  def test_ping
28
28
  assert_respond_to(@tcp, :ping)
29
29
  assert_respond_to(@tcp, :ping?)
30
30
  assert_nothing_raised{ @tcp.ping }
31
- assert_raises(ArgumentError){ @tcp.ping(@host) }
31
+ assert_nothing_raised{ @tcp.ping(@host) }
32
32
  end
33
33
 
34
34
  def test_ping_ecr_false
@@ -52,14 +52,38 @@ class TC_PingTCP < Test::Unit::TestCase
52
52
  assert_raises(ArgumentError){ PingTCP.ecr = "blah" }
53
53
  end
54
54
 
55
- def test_accessors
55
+ def test_duration
56
+ assert_nothing_raised{ @tcp.ping }
57
+ assert_respond_to(@tcp, :duration)
58
+ assert_kind_of(Float, @tcp.duration)
59
+ end
60
+
61
+ def test_host
56
62
  assert_respond_to(@tcp, :host)
57
63
  assert_respond_to(@tcp, :host=)
64
+ assert_equal('www.ruby-lang.org', @tcp.host)
65
+ end
66
+
67
+ def test_port
58
68
  assert_respond_to(@tcp, :port)
59
69
  assert_respond_to(@tcp, :port=)
70
+ assert_equal(7, @tcp.port)
71
+ end
72
+
73
+ def test_timeout
60
74
  assert_respond_to(@tcp, :timeout)
61
75
  assert_respond_to(@tcp, :timeout=)
76
+ assert_equal(5, @tcp.timeout)
77
+ end
78
+
79
+ def test_exception
80
+ msg = "+this test may fail depending on your network environment+"
62
81
  assert_respond_to(@tcp, :exception)
82
+ assert_nothing_raised{ @tcp.ping }
83
+ assert_nil(@tcp.exception, msg)
84
+ end
85
+
86
+ def test_warning
63
87
  assert_respond_to(@tcp, :warning)
64
88
  end
65
89
 
data/test/tc_pingudp.rb CHANGED
@@ -14,41 +14,67 @@ if base == "test" || base =~ /net-pingsimple.*/
14
14
  end
15
15
 
16
16
  require "test/unit"
17
- require "net/ping"
17
+ require "net/ping/udp"
18
18
  include Net
19
19
 
20
20
  class TC_PingUDP < Test::Unit::TestCase
21
21
  def setup
22
22
  @host = "www.ruby-lang.org"
23
- @udp = PingUDP.new(@host)
23
+ @udp = Ping::UDP.new(@host)
24
24
  end
25
25
 
26
26
  def test_version
27
- assert_equal("1.1.1", PingUDP::VERSION, "Bad version constant")
27
+ assert_equal("1.2.0", PingUDP::VERSION)
28
28
  end
29
29
 
30
30
  def test_ping
31
31
  assert_respond_to(@udp, :ping)
32
32
  assert_respond_to(@udp, :ping?)
33
33
  assert_nothing_raised{ @udp.ping }
34
- assert_raises(ArgumentError){ @udp.ping(@host) }
34
+ assert_nothing_raised{ @udp.ping(@host) }
35
35
  end
36
36
 
37
37
  def test_ping_standard
38
38
  assert_equal(false, @udp.ping?)
39
39
  assert_equal(false, @udp.exception.nil?, "Bad exception data")
40
40
  end
41
+
42
+ def test_bind
43
+ assert_respond_to(@udp, :bind)
44
+ assert_nothing_raised{ @udp.bind('www.ruby-lang.org', 80) }
45
+ end
41
46
 
42
- def test_accessors
47
+ def test_duration
48
+ assert_nothing_raised{ @udp.ping }
49
+ assert_respond_to(@udp, :duration)
50
+ assert_kind_of(Float, @udp.duration)
51
+ end
52
+
53
+ def test_host
43
54
  assert_respond_to(@udp, :host)
44
55
  assert_respond_to(@udp, :host=)
56
+ assert_equal('www.ruby-lang.org', @udp.host)
57
+ end
58
+
59
+ def test_port
45
60
  assert_respond_to(@udp, :port)
46
61
  assert_respond_to(@udp, :port=)
62
+ assert_equal(7, @udp.port)
63
+ end
64
+
65
+ def test_timeout
47
66
  assert_respond_to(@udp, :timeout)
48
67
  assert_respond_to(@udp, :timeout=)
49
- assert_respond_to(@udp, :data)
50
- assert_respond_to(@udp, :data=)
68
+ assert_equal(5, @udp.timeout)
69
+ end
70
+
71
+ def test_exception
51
72
  assert_respond_to(@udp, :exception)
73
+ assert_nothing_raised{ @udp.ping }
74
+ assert_nil(@udp.exception)
75
+ end
76
+
77
+ def test_warning
52
78
  assert_respond_to(@udp, :warning)
53
79
  end
54
80