diff options
author | Jamis Buck <jamis@37signals.com> | 2007-08-14 22:32:03 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2007-08-14 22:32:03 +0000 |
commit | 73f4b6abb31b178efd186f40c3aede52245693b9 (patch) | |
tree | e62ec930f4caa2faabc8a029b5b1dda2f262edff /test/authentication/methods | |
parent | 48654ab602e29e5dda1cc467cca80194a7a1e096 (diff) | |
download | net-ssh-73f4b6abb31b178efd186f40c3aede52245693b9.tar.gz |
tests for keyboard-interactive authmethod
git-svn-id: http://svn.jamisbuck.org/net-ssh/branches/v2@178 1d2a57f2-1ded-0310-ad52-83097a15a5de
Diffstat (limited to 'test/authentication/methods')
-rw-r--r-- | test/authentication/methods/common.rb | 28 | ||||
-rw-r--r-- | test/authentication/methods/test_abstract.rb | 10 | ||||
-rw-r--r-- | test/authentication/methods/test_hostbased.rb | 3 | ||||
-rw-r--r-- | test/authentication/methods/test_keyboard_interactive.rb | 99 |
4 files changed, 131 insertions, 9 deletions
diff --git a/test/authentication/methods/common.rb b/test/authentication/methods/common.rb new file mode 100644 index 0000000..735836d --- /dev/null +++ b/test/authentication/methods/common.rb @@ -0,0 +1,28 @@ +module Authentication; module Methods + + module Common + include Net::SSH::Authentication::Constants + + private + + def socket(options={}) + @socket ||= stub("socket", :client_name => "me.ssh.test") + end + + def transport(options={}) + @transport ||= MockTransport.new(options.merge(:socket => socket)) + end + + def session(options={}) + @session ||= begin + sess = stub("auth-session", :logger => nil, :transport => transport(options)) + def sess.next_message + transport.next_message + end + sess + end + end + + end + +end; end
\ No newline at end of file diff --git a/test/authentication/methods/test_abstract.rb b/test/authentication/methods/test_abstract.rb index 45709b2..e9f5051 100644 --- a/test/authentication/methods/test_abstract.rb +++ b/test/authentication/methods/test_abstract.rb @@ -1,10 +1,12 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../..").uniq! require 'common' +require 'authentication/methods/common' require 'net/ssh/authentication/methods/abstract' module Authentication; module Methods class TestAbstract < Test::Unit::TestCase + include Common def test_constructor_should_set_defaults assert_nil subject.key_manager @@ -45,14 +47,6 @@ module Authentication; module Methods @subject ||= Net::SSH::Authentication::Methods::Abstract.new(session(options), options) end - def transport(options={}) - @transport ||= MockTransport.new(options) - end - - def session(options={}) - @session ||= stub("auth-session", :logger => nil, :transport => transport(options)) - end - end end; end
\ No newline at end of file diff --git a/test/authentication/methods/test_hostbased.rb b/test/authentication/methods/test_hostbased.rb index 400acd1..cd1df57 100644 --- a/test/authentication/methods/test_hostbased.rb +++ b/test/authentication/methods/test_hostbased.rb @@ -1,11 +1,12 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../..").uniq! require 'common' require 'net/ssh/authentication/methods/hostbased' +require 'authentication/methods/common' module Authentication; module Methods class TestHostbased < Test::Unit::TestCase - include Net::SSH::Authentication::Constants + include Common def test_authenticate_should_return_false_when_no_key_manager_has_been_set assert_equal false, subject(:key_manager => nil).authenticate("ssh-connection", "jamis") diff --git a/test/authentication/methods/test_keyboard_interactive.rb b/test/authentication/methods/test_keyboard_interactive.rb new file mode 100644 index 0000000..9706a9c --- /dev/null +++ b/test/authentication/methods/test_keyboard_interactive.rb @@ -0,0 +1,99 @@ +$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../..").uniq! +require 'common' +require 'net/ssh/authentication/methods/keyboard_interactive' +require 'authentication/methods/common' + +module Authentication; module Methods + + class TestKeyboardInteractive < Test::Unit::TestCase + include Common + + USERAUTH_INFO_REQUEST = 60 + USERAUTH_INFO_RESPONSE = 61 + + def test_authenticate_should_be_false_when_server_does_not_support_this_method + transport.expect do |t,packet| + assert_equal USERAUTH_REQUEST, packet.type + assert_equal "jamis", packet.read_string + assert_equal "ssh-connection", packet.read_string + assert_equal "keyboard-interactive", packet.read_string + assert_equal "", packet.read_string # language tags + assert_equal "", packet.read_string # submethods + + t.return(USERAUTH_FAILURE, :string, "password") + end + + assert_equal false, subject.authenticate("ssh-connection", "jamis") + end + + def test_authenticate_should_be_false_if_given_password_is_not_accepted + transport.expect do |t,packet| + assert_equal USERAUTH_REQUEST, packet.type + t.return(USERAUTH_INFO_REQUEST, :string, "", :string, "", :string, "", :long, 1, :string, "Password:", :bool, false) + t.expect do |t,packet| + assert_equal USERAUTH_INFO_RESPONSE, packet.type + assert_equal 1, packet.read_long + assert_equal "the-password", packet.read_string + t.return(USERAUTH_FAILURE, :string, "publickey") + end + end + + assert_equal false, subject.authenticate("ssh-connection", "jamis", "the-password") + end + + def test_authenticate_should_be_true_if_given_password_is_accepted + transport.expect do |t,packet| + assert_equal USERAUTH_REQUEST, packet.type + t.return(USERAUTH_INFO_REQUEST, :string, "", :string, "", :string, "", :long, 1, :string, "Password:", :bool, false) + t.expect do |t,packet| + assert_equal USERAUTH_INFO_RESPONSE, packet.type + t.return(USERAUTH_SUCCESS) + end + end + + assert subject.authenticate("ssh-connection", "jamis", "the-password") + end + + def test_authenticate_should_duplicate_password_as_needed_to_fill_request + transport.expect do |t,packet| + assert_equal USERAUTH_REQUEST, packet.type + t.return(USERAUTH_INFO_REQUEST, :string, "", :string, "", :string, "", :long, 2, :string, "Password:", :bool, false, :string, "Again:", :bool, false) + t.expect do |t,packet| + assert_equal USERAUTH_INFO_RESPONSE, packet.type + assert_equal 2, packet.read_long + assert_equal "the-password", packet.read_string + assert_equal "the-password", packet.read_string + t.return(USERAUTH_SUCCESS) + end + end + + assert subject.authenticate("ssh-connection", "jamis", "the-password") + end + + def test_authenticate_should_prompt_for_input_when_password_is_not_given + subject.expects(:prompt).with("Name:", true).returns("name") + subject.expects(:prompt).with("Password:", false).returns("password") + + transport.expect do |t,packet| + assert_equal USERAUTH_REQUEST, packet.type + t.return(USERAUTH_INFO_REQUEST, :string, "", :string, "", :string, "", :long, 2, :string, "Name:", :bool, true, :string, "Password:", :bool, false) + t.expect do |t,packet| + assert_equal USERAUTH_INFO_RESPONSE, packet.type + assert_equal 2, packet.read_long + assert_equal "name", packet.read_string + assert_equal "password", packet.read_string + t.return(USERAUTH_SUCCESS) + end + end + + assert subject.authenticate("ssh-connection", "jamis", nil) + end + + private + + def subject(options={}) + @subject ||= Net::SSH::Authentication::Methods::KeyboardInteractive.new(session(options), options) + end + end + +end; end
\ No newline at end of file |