Permalink
Browse files

switched from #readchar to #readpartial(1) in lib/net/ssh/transport/s…

…erver_version.rb, so that closed sockets are recognized
  • Loading branch information...
1 parent 63fda96 commit 2fc2d203ed731888ed393449e6ececbc2ad3dfdd aaalex committed with delano Nov 4, 2009
Showing with 18 additions and 8 deletions.
  1. +4 −3 lib/net/ssh/transport/server_version.rb
  2. +14 −5 test/transport/test_server_version.rb
@@ -44,9 +44,10 @@ def negotiate!(socket)
@version = ""
loop do
# b = socket.recv(1)
- b = socket.readchar
-
- if b.nil?
+ begin
+ b = socket.readpartial(1)
+ raise Net::SSH::Disconnect, "connection closed by remote host" if b.nil?
+ rescue EOFError => e
raise Net::SSH::Disconnect, "connection closed by remote host"
end
@version << b
@@ -28,6 +28,10 @@ def test_unacceptible_server_version_should_raise_exception
assert_raises(Net::SSH::Exception) { subject(socket(false, "SSH-1.4-Testing_1.0\r\n")) }
end
+ def test_unexpected_server_close_should_raise_exception
+ assert_raises(Net::SSH::Disconnect) { subject(socket(false, "\r\nDestination server does not have Ssh activated.\r\nContact Cisco Systems, Inc to purchase a\r\nlicense key to activate Ssh.\r\n", true)) }
+ end
+
def test_header_lines_should_be_accumulated
s = subject(socket(true, "Welcome\r\nAnother line\r\nSSH-2.0-Testing_1.0\r\n"))
assert_equal "Welcome\r\nAnother line\r\n", s.header
@@ -40,16 +44,21 @@ def test_server_disconnect_should_raise_exception
private
- def socket(good, version_header)
+ def socket(good, version_header, raise_eot=false)
socket = mock("socket")
data = version_header.split('')
recv_times = data.length
- if data[-1] != "\n"
- recv_times += 1
- end
+ recv_times += 1 if data[-1] != "\n"
+
+ unless raise_eot
+
# socket.expects(:recv).with(1).times(recv_times).returns(*data).then.returns(nil)
- socket.expects(:readchar).times(recv_times).returns(*data).then.returns(nil)
+# socket.expects(:readchar).times(recv_times).returns(*data).then.returns(nil)
+ socket.expects(:readpartial).with(1).times(recv_times).returns(*data).then.returns(nil)
+ else
+ socket.expects(:readpartial).with(1).times(recv_times+1).returns(*data).then.raises(EOFError, "end of file reached")
+ end
if good
socket.expects(:write).with("#{Net::SSH::Transport::ServerVersion::PROTO_VERSION}\r\n")

0 comments on commit 2fc2d20

Please sign in to comment.