net-ping 1.7.2-universal-mingw32 → 1.7.3-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/net/ping/tcp.rb CHANGED
@@ -1,102 +1,102 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
-
3
- # The Net module serves as a namespace only.
4
- module Net
5
-
6
- # With a TCP ping simply try to open a connection. If we are successful,
7
- # assume success. In either case close the connection to be polite.
8
- #
9
- class Ping::TCP < Ping
10
- @@service_check = false
11
-
12
- # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
- # ping. The default is false.
14
- #
15
- def self.service_check
16
- @@service_check
17
- end
18
-
19
- # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
- # successful ping.
21
- #
22
- def self.service_check=(bool)
23
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
- raise ArgumentError, 'argument must be true or false'
25
- end
26
- @@service_check = bool
27
- end
28
-
29
- # This method attempts to ping a host and port using a TCPSocket with
30
- # the host, port and timeout values passed in the constructor. Returns
31
- # true if successful, or false otherwise.
32
- #
33
- # Note that, by default, an Errno::ECONNREFUSED return result will be
34
- # considered a failed ping. See the documentation for the
35
- # Ping::TCP.service_check= method if you wish to change this behavior.
36
- #
37
- def ping(host=@host)
38
- super(host)
39
-
40
- bool = false
41
- start_time = Time.now
42
-
43
- # Failure here most likely means bad host, so just bail.
44
- begin
45
- addr = Socket.getaddrinfo(host, port)
46
- rescue SocketError => err
47
- @exception = err
48
- return false
49
- end
50
-
51
- begin
52
- # Where addr[0][0] is likely AF_INET.
53
- sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
54
-
55
- # This may not be entirely necessary
56
- sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
57
-
58
- begin
59
- # Where addr[0][3] is an IP address
60
- sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
61
- rescue Errno::EINPROGRESS
62
- # No-op, continue below
63
- rescue Exception => err
64
- # Something has gone horribly wrong
65
- @exception = err
66
- return false
67
- end
68
-
69
- resp = IO.select(nil, [sock], nil, timeout)
70
-
71
- # Assume ECONNREFUSED at this point
72
- if resp.nil?
73
- if @@service_check
74
- bool = true
75
- else
76
- @exception = Errno::ECONNREFUSED
77
- end
78
- else
79
- bool = true
80
- end
81
- ensure
82
- sock.close if sock
83
- end
84
-
85
- # There is no duration if the ping failed
86
- @duration = Time.now - start_time if bool
87
-
88
- bool
89
- end
90
-
91
- alias ping? ping
92
- alias pingecho ping
93
-
94
- # Class method aliases. DEPRECATED.
95
- class << self
96
- alias econnrefused service_check
97
- alias econnrefused= service_check=
98
- alias ecr service_check
99
- alias ecr= service_check=
100
- end
101
- end
102
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+
6
+ # With a TCP ping simply try to open a connection. If we are successful,
7
+ # assume success. In either case close the connection to be polite.
8
+ #
9
+ class Ping::TCP < Ping
10
+ @@service_check = false
11
+
12
+ # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
+ # ping. The default is false.
14
+ #
15
+ def self.service_check
16
+ @@service_check
17
+ end
18
+
19
+ # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
+ # successful ping.
21
+ #
22
+ def self.service_check=(bool)
23
+ unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
+ raise ArgumentError, 'argument must be true or false'
25
+ end
26
+ @@service_check = bool
27
+ end
28
+
29
+ # This method attempts to ping a host and port using a TCPSocket with
30
+ # the host, port and timeout values passed in the constructor. Returns
31
+ # true if successful, or false otherwise.
32
+ #
33
+ # Note that, by default, an Errno::ECONNREFUSED return result will be
34
+ # considered a failed ping. See the documentation for the
35
+ # Ping::TCP.service_check= method if you wish to change this behavior.
36
+ #
37
+ def ping(host=@host)
38
+ super(host)
39
+
40
+ bool = false
41
+ start_time = Time.now
42
+
43
+ # Failure here most likely means bad host, so just bail.
44
+ begin
45
+ addr = Socket.getaddrinfo(host, port)
46
+ rescue SocketError => err
47
+ @exception = err
48
+ return false
49
+ end
50
+
51
+ begin
52
+ # Where addr[0][0] is likely AF_INET.
53
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
54
+
55
+ # This may not be entirely necessary
56
+ sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
57
+
58
+ begin
59
+ # Where addr[0][3] is an IP address
60
+ sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
61
+ rescue Errno::EINPROGRESS
62
+ # No-op, continue below
63
+ rescue Exception => err
64
+ # Something has gone horribly wrong
65
+ @exception = err
66
+ return false
67
+ end
68
+
69
+ resp = IO.select(nil, [sock], nil, timeout)
70
+
71
+ # Assume ECONNREFUSED at this point
72
+ if resp.nil?
73
+ if @@service_check
74
+ bool = true
75
+ else
76
+ @exception = Errno::ECONNREFUSED
77
+ end
78
+ else
79
+ bool = true
80
+ end
81
+ ensure
82
+ sock.close if sock
83
+ end
84
+
85
+ # There is no duration if the ping failed
86
+ @duration = Time.now - start_time if bool
87
+
88
+ bool
89
+ end
90
+
91
+ alias ping? ping
92
+ alias pingecho ping
93
+
94
+ # Class method aliases. DEPRECATED.
95
+ class << self
96
+ alias econnrefused service_check
97
+ alias econnrefused= service_check=
98
+ alias ecr service_check
99
+ alias ecr= service_check=
100
+ end
101
+ end
102
+ end
data/net-ping.gemspec CHANGED
@@ -1,40 +1,39 @@
1
- require 'rubygems'
2
- require 'rbconfig'
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = 'net-ping'
6
- spec.version = '1.7.2'
7
- spec.license = 'Artistic 2.0'
8
- spec.author = 'Daniel J. Berger'
9
- spec.email = 'djberg96@gmail.com'
10
- spec.homepage = 'https://github.com/djberg96/net-ping'
11
- spec.summary = 'A ping interface for Ruby.'
12
- spec.test_file = 'test/test_net_ping.rb'
13
- spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
-
15
- spec.rubyforge_project = 'shards'
16
- spec.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
17
-
18
- # The TCP Ping class requires this for non-blocking sockets.
19
- spec.required_ruby_version = ">= 1.9.1"
20
-
21
- spec.add_development_dependency('test-unit')
22
- spec.add_development_dependency('fakeweb')
23
- spec.add_development_dependency('rake')
24
-
25
- if File::ALT_SEPARATOR
26
- require 'rbconfig'
27
- arch = RbConfig::CONFIG['build_os']
28
- spec.platform = Gem::Platform.new(['universal', arch])
29
- spec.platform.version = nil
30
-
31
- # Used for icmp pings.
32
- spec.add_dependency('win32-security', '>= 0.2.0')
33
- end
34
-
35
- spec.description = <<-EOF
36
- The net-ping library provides a ping interface for Ruby. It includes
37
- separate TCP, HTTP, LDAP, ICMP, UDP, WMI (for Windows) and external ping
38
- classes.
39
- EOF
40
- end
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'net-ping'
6
+ spec.version = '1.7.3'
7
+ spec.license = 'Artistic 2.0'
8
+ spec.author = 'Daniel J. Berger'
9
+ spec.email = 'djberg96@gmail.com'
10
+ spec.homepage = 'https://github.com/djberg96/net-ping'
11
+ spec.summary = 'A ping interface for Ruby.'
12
+ spec.test_file = 'test/test_net_ping.rb'
13
+ spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
+
15
+ spec.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
16
+
17
+ # The TCP Ping class requires this for non-blocking sockets.
18
+ spec.required_ruby_version = ">= 1.9.3"
19
+
20
+ spec.add_development_dependency('test-unit')
21
+ spec.add_development_dependency('fakeweb')
22
+ spec.add_development_dependency('rake')
23
+
24
+ if File::ALT_SEPARATOR
25
+ require 'rbconfig'
26
+ arch = RbConfig::CONFIG['build_os']
27
+ spec.platform = Gem::Platform.new(['universal', arch])
28
+ spec.platform.version = nil
29
+
30
+ # Used for icmp pings.
31
+ spec.add_dependency('win32-security', '>= 0.2.0')
32
+ end
33
+
34
+ spec.description = <<-EOF
35
+ The net-ping library provides a ping interface for Ruby. It includes
36
+ separate TCP, HTTP, LDAP, ICMP, UDP, WMI (for Windows) and external ping
37
+ classes.
38
+ EOF
39
+ end
@@ -1,35 +1,35 @@
1
- ######################################################################
2
- # test_net_ping.rb
3
- #
4
- # Test suite for all the Ping subclasses. Note that the Ping::ICMP
5
- # class test won't be run unless this is run as a privileged process.
6
- ######################################################################
7
- require 'test_net_ping_external'
8
- require 'test_net_ping_http'
9
- require 'test_net_ping_tcp'
10
- require 'test_net_ping_udp'
11
- require 'fakeweb'
12
-
13
- if File::ALT_SEPARATOR
14
- require 'win32/security'
15
-
16
- if Win32::Security.elevated_security?
17
- require 'test_net_ping_icmp'
18
- end
19
- else
20
- if Process.euid == 0
21
- require 'test_net_ping_icmp'
22
- end
23
- end
24
-
25
- if File::ALT_SEPARATOR
26
- require 'test_net_ping_wmi'
27
- end
28
-
29
- class TC_Net_Ping < Test::Unit::TestCase
30
- def test_net_ping_version
31
- assert_equal('1.7.2', Net::Ping::VERSION)
32
- end
33
- end
34
-
35
- FakeWeb.allow_net_connect = false
1
+ ######################################################################
2
+ # test_net_ping.rb
3
+ #
4
+ # Test suite for all the Ping subclasses. Note that the Ping::ICMP
5
+ # class test won't be run unless this is run as a privileged process.
6
+ ######################################################################
7
+ require 'test_net_ping_external'
8
+ require 'test_net_ping_http'
9
+ require 'test_net_ping_tcp'
10
+ require 'test_net_ping_udp'
11
+ require 'fakeweb'
12
+
13
+ if File::ALT_SEPARATOR
14
+ require 'win32/security'
15
+
16
+ if Win32::Security.elevated_security?
17
+ require 'test_net_ping_icmp'
18
+ end
19
+ else
20
+ if Process.euid == 0
21
+ require 'test_net_ping_icmp'
22
+ end
23
+ end
24
+
25
+ if File::ALT_SEPARATOR
26
+ require 'test_net_ping_wmi'
27
+ end
28
+
29
+ class TC_Net_Ping < Test::Unit::TestCase
30
+ def test_net_ping_version
31
+ assert_equal('1.7.3', Net::Ping::VERSION)
32
+ end
33
+ end
34
+
35
+ FakeWeb.allow_net_connect = false
@@ -1,129 +1,138 @@
1
- #########################################################################
2
- # test_net_ping_external.rb
3
- #
4
- # Test case for the Net::PingExternal class. Run this via the 'test' or
5
- # 'test:external' rake task.
6
- #
7
- # WARNING: I've noticed that test failures will occur if you're using
8
- # OpenDNS. This is apparently caused by them messing with upstream
9
- # replies for advertising purposes.
10
- #########################################################################
11
- require 'test-unit'
12
- require 'net/ping/external'
13
-
14
- class TC_Net_Ping_External < Test::Unit::TestCase
15
- def setup
16
- @host = 'localhost'
17
- @bogus = 'foo.bar.baz'
18
- @pe = Net::Ping::External.new(@host)
19
- @bad = Net::Ping::External.new(@bogus)
20
- end
21
-
22
- test "ping basic functionality" do
23
- assert_respond_to(@pe, :ping)
24
- end
25
-
26
- test "ping with no arguments" do
27
- assert_nothing_raised{ @pe.ping }
28
- end
29
-
30
- test "ping accepts a hostname" do
31
- assert_nothing_raised{ @pe.ping(@host) }
32
- end
33
-
34
- test "ping returns a boolean" do
35
- assert_boolean(@pe.ping)
36
- assert_boolean(@bad.ping)
37
- end
38
-
39
- test "ping? alias" do
40
- assert_respond_to(@pe, :ping?)
41
- assert_alias_method(@pe, :ping?, :ping)
42
- end
43
-
44
- test "pingecho alias" do
45
- assert_nothing_raised{ @pe.pingecho }
46
- assert_alias_method(@pe, :pingecho, :ping)
47
- end
48
-
49
- test "pinging a good host returns true" do
50
- assert_true(@pe.ping?)
51
- end
52
-
53
- test "pinging a bogus host returns false" do
54
- assert_false(@bad.ping?)
55
- end
56
-
57
- test "duration basic functionality" do
58
- assert_nothing_raised{ @pe.ping }
59
- assert_respond_to(@pe, :duration)
60
- assert_kind_of(Float, @pe.duration)
61
- end
62
-
63
- test "duration is unset if a bad ping follows a good ping" do
64
- assert_nothing_raised{ @pe.ping }
65
- assert_not_nil(@pe.duration)
66
- assert_false(@pe.ping?(@bogus))
67
- assert_nil(@pe.duration)
68
- end
69
-
70
- test "host getter basic functionality" do
71
- assert_respond_to(@pe, :host)
72
- assert_equal('localhost', @pe.host)
73
- end
74
-
75
- test "host setter basic functionality" do
76
- assert_respond_to(@pe, :host=)
77
- assert_nothing_raised{ @pe.host = @bad }
78
- assert_equal(@bad, @pe.host)
79
- end
80
-
81
- test "port getter basic functionality" do
82
- assert_respond_to(@pe, :port)
83
- assert_equal(7, @pe.port)
84
- end
85
-
86
- test "port setter basic functionality" do
87
- assert_respond_to(@pe, :port=)
88
- assert_nothing_raised{ @pe.port = 90 }
89
- assert_equal(90, @pe.port)
90
- end
91
-
92
- test "timeout getter basic functionality" do
93
- assert_respond_to(@pe, :timeout)
94
- assert_equal(5, @pe.timeout)
95
- end
96
-
97
- test "timeout setter basic functionality" do
98
- assert_respond_to(@pe, :timeout=)
99
- assert_nothing_raised{ @pe.timeout = 30 }
100
- assert_equal(30, @pe.timeout)
101
- end
102
-
103
- test "exception method basic functionality" do
104
- assert_respond_to(@pe, :exception)
105
- assert_nil(@pe.exception)
106
- end
107
-
108
- test "pinging a bogus host stores exception data" do
109
- assert_nothing_raised{ @bad.ping? }
110
- assert_not_nil(@bad.exception)
111
- end
112
-
113
- test "pinging a good host results in no exception data" do
114
- assert_nothing_raised{ @pe.ping }
115
- assert_nil(@pe.exception)
116
- end
117
-
118
- test "warning basic functionality" do
119
- assert_respond_to(@pe, :warning)
120
- assert_nil(@pe.warning)
121
- end
122
-
123
- def teardown
124
- @host = nil
125
- @bogus = nil
126
- @pe = nil
127
- @bad = nil
128
- end
129
- end
1
+ #########################################################################
2
+ # test_net_ping_external.rb
3
+ #
4
+ # Test case for the Net::PingExternal class. Run this via the 'test' or
5
+ # 'test:external' rake task.
6
+ #
7
+ # WARNING: I've noticed that test failures will occur if you're using
8
+ # OpenDNS. This is apparently caused by them messing with upstream
9
+ # replies for advertising purposes.
10
+ #########################################################################
11
+ require 'test-unit'
12
+ require 'net/ping/external'
13
+
14
+ class TC_Net_Ping_External < Test::Unit::TestCase
15
+ def setup
16
+ @host = 'localhost'
17
+ @bogus = 'foo.bar.baz'
18
+ @pe = Net::Ping::External.new(@host)
19
+ @bad = Net::Ping::External.new(@bogus)
20
+ end
21
+
22
+ test "ping basic functionality" do
23
+ assert_respond_to(@pe, :ping)
24
+ end
25
+
26
+ test "ping with no arguments" do
27
+ assert_nothing_raised{ @pe.ping }
28
+ end
29
+
30
+ test "ping accepts a hostname" do
31
+ assert_nothing_raised{ @pe.ping(@host) }
32
+ end
33
+
34
+ test "ping returns a boolean" do
35
+ assert_boolean(@pe.ping)
36
+ assert_boolean(@bad.ping)
37
+ end
38
+
39
+ test "ping? alias" do
40
+ assert_respond_to(@pe, :ping?)
41
+ assert_alias_method(@pe, :ping?, :ping)
42
+ end
43
+
44
+ test "pingecho alias" do
45
+ assert_nothing_raised{ @pe.pingecho }
46
+ assert_alias_method(@pe, :pingecho, :ping)
47
+ end
48
+
49
+ test "pinging a good host returns true" do
50
+ assert_true(@pe.ping?)
51
+ end
52
+
53
+ test "pinging a bogus host returns false" do
54
+ assert_false(@bad.ping?)
55
+ end
56
+
57
+ test "duration basic functionality" do
58
+ assert_nothing_raised{ @pe.ping }
59
+ assert_respond_to(@pe, :duration)
60
+ assert_kind_of(Float, @pe.duration)
61
+ end
62
+
63
+ test "duration is unset if a bad ping follows a good ping" do
64
+ assert_nothing_raised{ @pe.ping }
65
+ assert_not_nil(@pe.duration)
66
+ assert_false(@pe.ping?(@bogus))
67
+ assert_nil(@pe.duration)
68
+ end
69
+
70
+ test "host getter basic functionality" do
71
+ assert_respond_to(@pe, :host)
72
+ assert_equal('localhost', @pe.host)
73
+ end
74
+
75
+ test "host setter basic functionality" do
76
+ assert_respond_to(@pe, :host=)
77
+ assert_nothing_raised{ @pe.host = @bad }
78
+ assert_equal(@bad, @pe.host)
79
+ end
80
+
81
+ test "port getter basic functionality" do
82
+ assert_respond_to(@pe, :port)
83
+ assert_equal(7, @pe.port)
84
+ end
85
+
86
+ test "port setter basic functionality" do
87
+ assert_respond_to(@pe, :port=)
88
+ assert_nothing_raised{ @pe.port = 90 }
89
+ assert_equal(90, @pe.port)
90
+ end
91
+
92
+ test "timeout getter basic functionality" do
93
+ assert_respond_to(@pe, :timeout)
94
+ assert_equal(5, @pe.timeout)
95
+ end
96
+
97
+ test "timeout setter basic functionality" do
98
+ assert_respond_to(@pe, :timeout=)
99
+ assert_nothing_raised{ @pe.timeout = 30 }
100
+ assert_equal(30, @pe.timeout)
101
+ end
102
+
103
+ test "exception method basic functionality" do
104
+ assert_respond_to(@pe, :exception)
105
+ assert_nil(@pe.exception)
106
+ end
107
+
108
+ test "pinging a bogus host stores exception data" do
109
+ assert_nothing_raised{ @bad.ping? }
110
+ assert_not_nil(@bad.exception)
111
+ end
112
+
113
+ test "pinging a good host results in no exception data" do
114
+ assert_nothing_raised{ @pe.ping }
115
+ assert_nil(@pe.exception)
116
+ end
117
+
118
+ test "warning basic functionality" do
119
+ assert_respond_to(@pe, :warning)
120
+ assert_nil(@pe.warning)
121
+ end
122
+
123
+ test "timing out causes expected result" do
124
+ ext = Net::Ping::External.new('foo.bar.baz', nil, 1)
125
+ start = Time.now
126
+ assert_false(ext.ping?)
127
+ elapsed = Time.now - start
128
+ assert_true(elapsed < 2.5, "Actual elapsed: #{elapsed}")
129
+ assert_not_nil(ext.exception)
130
+ end
131
+
132
+ def teardown
133
+ @host = nil
134
+ @bogus = nil
135
+ @pe = nil
136
+ @bad = nil
137
+ end
138
+ end