From 31ba47cf1d78a9275c8991104e65bb10273c1c90 Mon Sep 17 00:00:00 2001 From: Kaspar Schiess Date: Thu, 18 Mar 2010 08:44:58 +0100 Subject: [PATCH] ! A proper fix for the error I've found yesterday. We should further look for empty rescue statements, as they may sometimes catch stuff that we don't want to be caught. --- lib/net/ldap.rb | 7 +++--- spec/spec_helper.rb | 2 ++ spec/unit/ldap_spec.rb | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 spec/unit/ldap_spec.rb diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index a31790f..193786e 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -1,6 +1,5 @@ require 'openssl' require 'ostruct' - require 'socket' require 'net/ber' @@ -1122,8 +1121,10 @@ module Net def initialize server begin @conn = TCPSocket.new( server[:host], server[:port] ) - rescue - raise LdapError.new( "no connection to server" ) + rescue SocketError + raise LdapError, "No such address or other socket error." + rescue Errno::ECONNREFUSED + raise LdapError, "Server #{server[:host]} refused connection on port #{server[:port]}." end if server[:encryption] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2e9a845..9e4a19f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +require 'net/ldap' + Spec::Runner.configure do |config| config.mock_with :flexmock end \ No newline at end of file diff --git a/spec/unit/ldap_spec.rb b/spec/unit/ldap_spec.rb new file mode 100644 index 0000000..1edb5c9 --- /dev/null +++ b/spec/unit/ldap_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe Net::LDAP::Connection do + describe "initialize" do + context "when host is not responding" do + before(:each) do + flexmock(TCPSocket). + should_receive(:new).and_raise(Errno::ECONNREFUSED) + end + + it "should raise LdapError" do + lambda { + Net::LDAP::Connection.new( + :server => 'test.mocked.com', + :port => 636) + }.should raise_error(Net::LDAP::LdapError) + end + end + context "when host is blocking the port" do + before(:each) do + flexmock(TCPSocket). + should_receive(:new).and_raise(SocketError) + end + + it "should raise LdapError" do + lambda { + Net::LDAP::Connection.new( + :server => 'test.mocked.com', + :port => 636) + }.should raise_error(Net::LDAP::LdapError) + end + end + context "on other exceptions" do + before(:each) do + flexmock(TCPSocket). + should_receive(:new).and_raise(NameError) + end + + it "should rethrow the exception" do + lambda { + Net::LDAP::Connection.new( + :server => 'test.mocked.com', + :port => 636) + }.should raise_error(NameError) + end + end + end +end \ No newline at end of file