diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index f1a70b18..91fde253 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -62,7 +62,7 @@ def open_connection(server) end return rescue Net::LDAP::Error, SocketError, SystemCallError, - OpenSSL::SSL::SSLError => e + OpenSSL::SSL::SSLError, IO::TimeoutError => e # Ensure the connection is closed in the event a setup failure. close errors << [e, host, port] diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index fdfa418c..91cbe278 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -363,6 +363,28 @@ def setup @connection = Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) end + def capture_stderr + stderr, $stderr = $stderr, StringIO.new + yield + $stderr.string + ensure + $stderr = stderr + end + + # Fake socket for testing + # + # FakeTCPSocket.new("success", 636) + # FakeTCPSocket.new("fail.SocketError", 636) # raises SocketError + class FakeTCPSocket + def initialize(host, port, socket_opts = {}) + parts = host.split(".") + status = parts.shift + error = parts.join(".") + + raise Object.const_get(error) if status == "fail" + end + end + def test_error_failed_operation ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeUnwillingToPerform, "", "The provided password value was rejected by a password validator: The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The minimum number of characters from that set that must be present in user passwords is 1"]) ber.ber_identifier = Net::LDAP::PDU::ModifyResponse @@ -382,6 +404,22 @@ def test_no_error_on_success assert result.success?, "should be success" assert_equal "", result.error_message end + + def test_connection_timeout_io_timeout_error + return if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("4.0.0") + + connection = Net::LDAP::Connection.new( + :host => "fail.IO::TimeoutError", + :port => 636, + :socket_class => FakeTCPSocket, + ) + + capture_stderr do + assert_raise Net::LDAP::Error do + connection.socket + end + end + end end class TestLDAPConnectionInstrumentation < Test::Unit::TestCase