diff options
Diffstat (limited to 'test/authentication/methods/test_publickey.rb')
-rw-r--r-- | test/authentication/methods/test_publickey.rb | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/test/authentication/methods/test_publickey.rb b/test/authentication/methods/test_publickey.rb index 663be81..8f2cc73 100644 --- a/test/authentication/methods/test_publickey.rb +++ b/test/authentication/methods/test_publickey.rb @@ -2,44 +2,44 @@ require 'common' require 'net/ssh/authentication/methods/publickey' require 'authentication/methods/common' -module Authentication +module Authentication module Methods class TestPublickey < NetSSHTest 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") end - + def test_authenticate_should_return_false_when_key_manager_has_no_keys assert_equal false, subject(keys: []).authenticate("ssh-connection", "jamis") end - + def test_authenticate_should_return_false_if_no_keys_can_authenticate transport.expect do |t, packet| assert_equal USERAUTH_REQUEST, packet.type assert verify_userauth_request_packet(packet, keys.first, false) t.return(USERAUTH_FAILURE, :string, "hostbased,password") - + t.expect do |t2, packet2| assert_equal USERAUTH_REQUEST, packet2.type assert verify_userauth_request_packet(packet2, keys.last, false) t2.return(USERAUTH_FAILURE, :string, "hostbased,password") end end - + assert_equal false, subject.authenticate("ssh-connection", "jamis") end - + def test_authenticate_should_raise_if_publickey_disallowed key_manager.expects(:sign).with(&signature_parameters(keys.first)).returns("sig-one") - + transport.expect do |t, packet| assert_equal USERAUTH_REQUEST, packet.type assert verify_userauth_request_packet(packet, keys.first, false) t.return(USERAUTH_PK_OK, :string, keys.first.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.first)) - + t.expect do |t2,packet2| assert_equal USERAUTH_REQUEST, packet2.type assert verify_userauth_request_packet(packet2, keys.first, true) @@ -47,32 +47,32 @@ module Authentication t2.return(USERAUTH_FAILURE, :string, "hostbased,password") end end - + assert_raises Net::SSH::Authentication::DisallowedMethod do subject.authenticate("ssh-connection", "jamis") end end - + def test_authenticate_should_return_false_if_signature_exchange_fails key_manager.expects(:sign).with(&signature_parameters(keys.first)).returns("sig-one") key_manager.expects(:sign).with(&signature_parameters(keys.last)).returns("sig-two") - + transport.expect do |t, packet| assert_equal USERAUTH_REQUEST, packet.type assert verify_userauth_request_packet(packet, keys.first, false) t.return(USERAUTH_PK_OK, :string, keys.first.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.first)) - + t.expect do |t2,packet2| assert_equal USERAUTH_REQUEST, packet2.type assert verify_userauth_request_packet(packet2, keys.first, true) assert_equal "sig-one", packet2.read_string t2.return(USERAUTH_FAILURE, :string, "publickey") - + t2.expect do |t3, packet3| assert_equal USERAUTH_REQUEST, packet3.type assert verify_userauth_request_packet(packet3, keys.last, false) t3.return(USERAUTH_PK_OK, :string, keys.last.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.last)) - + t3.expect do |t4,packet4| assert_equal USERAUTH_REQUEST, packet4.type assert verify_userauth_request_packet(packet4, keys.last, true) @@ -82,18 +82,18 @@ module Authentication end end end - + assert !subject.authenticate("ssh-connection", "jamis") end - + def test_authenticate_should_return_true_if_any_key_can_authenticate key_manager.expects(:sign).with(&signature_parameters(keys.first)).returns("sig-one") - + transport.expect do |t, packet| assert_equal USERAUTH_REQUEST, packet.type assert verify_userauth_request_packet(packet, keys.first, false) t.return(USERAUTH_PK_OK, :string, keys.first.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.first)) - + t.expect do |t2,packet2| assert_equal USERAUTH_REQUEST, packet2.type assert verify_userauth_request_packet(packet2, keys.first, true) @@ -101,12 +101,12 @@ module Authentication t2.return(USERAUTH_SUCCESS) end end - + assert subject.authenticate("ssh-connection", "jamis") end - + private - + def signature_parameters(key) Proc.new do |given_key, data| next false unless given_key.to_blob == key.to_blob @@ -116,7 +116,7 @@ module Authentication verify_userauth_request_packet(buffer, key, true) end end - + def verify_userauth_request_packet(packet, key, has_sig) packet.read_string == "jamis" && # user-name packet.read_string == "ssh-connection" && # next service @@ -125,12 +125,12 @@ module Authentication packet.read_string == key.ssh_type && # ssh key type packet.read_buffer.read_key.to_blob == key.to_blob # key end - + @@keys = nil def keys @@keys ||= [OpenSSL::PKey::RSA.new(512), OpenSSL::PKey::DSA.new(512)] end - + def key_manager(options={}) @key_manager ||= begin manager = stub("key_manager") @@ -138,11 +138,11 @@ module Authentication manager end end - + def subject(options={}) options[:key_manager] = key_manager(options) unless options.key?(:key_manager) @subject ||= Net::SSH::Authentication::Methods::Publickey.new(session(options), options) end - end - -end; end + end + end +end |