net-ssh 2.9.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.gitignore +6 -0
  4. data/.rubocop.yml +5 -0
  5. data/.rubocop_todo.yml +1129 -0
  6. data/.travis.yml +41 -5
  7. data/CHANGES.txt +133 -1
  8. data/Gemfile +13 -0
  9. data/Gemfile.norbnacl +10 -0
  10. data/Gemfile.norbnacl.lock +41 -0
  11. data/ISSUE_TEMPLATE.md +30 -0
  12. data/README.rdoc +26 -81
  13. data/Rakefile +63 -45
  14. data/appveyor.yml +51 -0
  15. data/lib/net/ssh/authentication/agent.rb +174 -14
  16. data/lib/net/ssh/authentication/ed25519.rb +137 -0
  17. data/lib/net/ssh/authentication/ed25519_loader.rb +21 -0
  18. data/lib/net/ssh/authentication/key_manager.rb +36 -30
  19. data/lib/net/ssh/authentication/methods/abstract.rb +4 -0
  20. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +16 -9
  21. data/lib/net/ssh/authentication/methods/password.rb +17 -4
  22. data/lib/net/ssh/authentication/pageant.rb +166 -45
  23. data/lib/net/ssh/authentication/session.rb +3 -2
  24. data/lib/net/ssh/buffer.rb +49 -10
  25. data/lib/net/ssh/buffered_io.rb +17 -12
  26. data/lib/net/ssh/config.rb +39 -8
  27. data/lib/net/ssh/connection/channel.rb +42 -20
  28. data/lib/net/ssh/connection/event_loop.rb +114 -0
  29. data/lib/net/ssh/connection/keepalive.rb +2 -2
  30. data/lib/net/ssh/connection/session.rb +120 -34
  31. data/lib/net/ssh/errors.rb +6 -6
  32. data/lib/net/ssh/key_factory.rb +49 -43
  33. data/lib/net/ssh/known_hosts.rb +49 -3
  34. data/lib/net/ssh/prompt.rb +47 -78
  35. data/lib/net/ssh/proxy/command.rb +31 -5
  36. data/lib/net/ssh/proxy/http.rb +15 -11
  37. data/lib/net/ssh/proxy/https.rb +49 -0
  38. data/lib/net/ssh/proxy/socks4.rb +2 -1
  39. data/lib/net/ssh/proxy/socks5.rb +3 -2
  40. data/lib/net/ssh/ruby_compat.rb +2 -29
  41. data/lib/net/ssh/service/forward.rb +2 -2
  42. data/lib/net/ssh/test/channel.rb +7 -0
  43. data/lib/net/ssh/test/extensions.rb +17 -0
  44. data/lib/net/ssh/test/kex.rb +4 -4
  45. data/lib/net/ssh/test/packet.rb +18 -2
  46. data/lib/net/ssh/test/script.rb +16 -2
  47. data/lib/net/ssh/test/socket.rb +1 -1
  48. data/lib/net/ssh/test.rb +5 -5
  49. data/lib/net/ssh/transport/algorithms.rb +92 -75
  50. data/lib/net/ssh/transport/cipher_factory.rb +19 -26
  51. data/lib/net/ssh/transport/ctr.rb +7 -9
  52. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +20 -9
  53. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +5 -3
  54. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +1 -1
  55. data/lib/net/ssh/transport/key_expander.rb +1 -0
  56. data/lib/net/ssh/transport/openssl.rb +1 -1
  57. data/lib/net/ssh/transport/packet_stream.rb +11 -3
  58. data/lib/net/ssh/transport/server_version.rb +13 -6
  59. data/lib/net/ssh/transport/session.rb +20 -10
  60. data/lib/net/ssh/transport/state.rb +1 -1
  61. data/lib/net/ssh/verifiers/secure.rb +8 -10
  62. data/lib/net/ssh/version.rb +4 -4
  63. data/lib/net/ssh.rb +62 -14
  64. data/net-ssh-public_cert.pem +19 -18
  65. data/net-ssh.gemspec +34 -194
  66. data/support/arcfour_check.rb +1 -1
  67. data/support/ssh_tunnel_bug.rb +1 -1
  68. data.tar.gz.sig +0 -0
  69. metadata +125 -109
  70. metadata.gz.sig +0 -0
  71. data/Rudyfile +0 -96
  72. data/lib/net/ssh/authentication/agent/java_pageant.rb +0 -85
  73. data/lib/net/ssh/authentication/agent/socket.rb +0 -178
  74. data/setup.rb +0 -1585
  75. data/test/README.txt +0 -47
  76. data/test/authentication/methods/common.rb +0 -28
  77. data/test/authentication/methods/test_abstract.rb +0 -51
  78. data/test/authentication/methods/test_hostbased.rb +0 -114
  79. data/test/authentication/methods/test_keyboard_interactive.rb +0 -100
  80. data/test/authentication/methods/test_none.rb +0 -41
  81. data/test/authentication/methods/test_password.rb +0 -95
  82. data/test/authentication/methods/test_publickey.rb +0 -148
  83. data/test/authentication/test_agent.rb +0 -224
  84. data/test/authentication/test_key_manager.rb +0 -227
  85. data/test/authentication/test_session.rb +0 -107
  86. data/test/common.rb +0 -108
  87. data/test/configs/auth_off +0 -5
  88. data/test/configs/auth_on +0 -4
  89. data/test/configs/empty +0 -0
  90. data/test/configs/eqsign +0 -3
  91. data/test/configs/exact_match +0 -8
  92. data/test/configs/host_plus +0 -10
  93. data/test/configs/multihost +0 -4
  94. data/test/configs/negative_match +0 -6
  95. data/test/configs/nohost +0 -19
  96. data/test/configs/numeric_host +0 -4
  97. data/test/configs/send_env +0 -2
  98. data/test/configs/substitutes +0 -8
  99. data/test/configs/wild_cards +0 -14
  100. data/test/connection/test_channel.rb +0 -467
  101. data/test/connection/test_session.rb +0 -543
  102. data/test/known_hosts/github +0 -1
  103. data/test/manual/test_forward.rb +0 -285
  104. data/test/manual/test_pageant.rb +0 -37
  105. data/test/start/test_connection.rb +0 -53
  106. data/test/start/test_options.rb +0 -43
  107. data/test/start/test_transport.rb +0 -28
  108. data/test/test_all.rb +0 -11
  109. data/test/test_buffer.rb +0 -433
  110. data/test/test_buffered_io.rb +0 -63
  111. data/test/test_config.rb +0 -221
  112. data/test/test_key_factory.rb +0 -191
  113. data/test/test_known_hosts.rb +0 -13
  114. data/test/transport/hmac/test_md5.rb +0 -41
  115. data/test/transport/hmac/test_md5_96.rb +0 -27
  116. data/test/transport/hmac/test_none.rb +0 -34
  117. data/test/transport/hmac/test_ripemd160.rb +0 -36
  118. data/test/transport/hmac/test_sha1.rb +0 -36
  119. data/test/transport/hmac/test_sha1_96.rb +0 -27
  120. data/test/transport/hmac/test_sha2_256.rb +0 -37
  121. data/test/transport/hmac/test_sha2_256_96.rb +0 -27
  122. data/test/transport/hmac/test_sha2_512.rb +0 -37
  123. data/test/transport/hmac/test_sha2_512_96.rb +0 -27
  124. data/test/transport/kex/test_diffie_hellman_group14_sha1.rb +0 -13
  125. data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +0 -146
  126. data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +0 -92
  127. data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +0 -34
  128. data/test/transport/kex/test_ecdh_sha2_nistp256.rb +0 -161
  129. data/test/transport/kex/test_ecdh_sha2_nistp384.rb +0 -38
  130. data/test/transport/kex/test_ecdh_sha2_nistp521.rb +0 -38
  131. data/test/transport/test_algorithms.rb +0 -324
  132. data/test/transport/test_cipher_factory.rb +0 -443
  133. data/test/transport/test_hmac.rb +0 -34
  134. data/test/transport/test_identity_cipher.rb +0 -40
  135. data/test/transport/test_packet_stream.rb +0 -1761
  136. data/test/transport/test_server_version.rb +0 -78
  137. data/test/transport/test_session.rb +0 -331
  138. data/test/transport/test_state.rb +0 -181
@@ -1,224 +0,0 @@
1
- require 'common'
2
- require 'net/ssh/authentication/agent'
3
-
4
- module Authentication
5
-
6
- class TestAgent < Test::Unit::TestCase
7
-
8
- SSH2_AGENT_REQUEST_VERSION = 1
9
- SSH2_AGENT_REQUEST_IDENTITIES = 11
10
- SSH2_AGENT_IDENTITIES_ANSWER = 12
11
- SSH2_AGENT_SIGN_REQUEST = 13
12
- SSH2_AGENT_SIGN_RESPONSE = 14
13
- SSH2_AGENT_FAILURE = 30
14
- SSH2_AGENT_VERSION_RESPONSE = 103
15
-
16
- SSH_COM_AGENT2_FAILURE = 102
17
-
18
- SSH_AGENT_REQUEST_RSA_IDENTITIES = 1
19
- SSH_AGENT_RSA_IDENTITIES_ANSWER = 2
20
- SSH_AGENT_FAILURE = 5
21
-
22
- def setup
23
- @original, ENV['SSH_AUTH_SOCK'] = ENV['SSH_AUTH_SOCK'], "/path/to/ssh.agent.sock"
24
- end
25
-
26
- def teardown
27
- ENV['SSH_AUTH_SOCK'] = @original
28
- end
29
-
30
- def test_connect_should_use_agent_factory_to_determine_connection_type
31
- factory.expects(:open).with("/path/to/ssh.agent.sock").returns(socket)
32
- agent(false).connect!
33
- end
34
-
35
- def test_connect_should_raise_error_if_connection_could_not_be_established
36
- factory.expects(:open).raises(SocketError)
37
- assert_raises(Net::SSH::Authentication::AgentNotAvailable) { agent(false).connect! }
38
- end
39
-
40
- def test_negotiate_should_raise_error_if_ssh2_agent_response_recieved
41
- socket.expect do |s, type, buffer|
42
- assert_equal SSH2_AGENT_REQUEST_VERSION, type
43
- assert_equal Net::SSH::Transport::ServerVersion::PROTO_VERSION, buffer.read_string
44
- s.return(SSH2_AGENT_VERSION_RESPONSE)
45
- end
46
- assert_raises(Net::SSH::Authentication::AgentNotAvailable) { agent.negotiate! }
47
- end
48
-
49
- def test_negotiate_should_raise_error_if_response_was_unexpected
50
- socket.expect do |s, type, buffer|
51
- assert_equal SSH2_AGENT_REQUEST_VERSION, type
52
- s.return(255)
53
- end
54
- assert_raises(Net::SSH::Authentication::AgentNotAvailable) { agent.negotiate! }
55
- end
56
-
57
- def test_negotiate_should_be_successful_with_expected_response
58
- socket.expect do |s, type, buffer|
59
- assert_equal SSH2_AGENT_REQUEST_VERSION, type
60
- s.return(SSH_AGENT_RSA_IDENTITIES_ANSWER)
61
- end
62
- assert_nothing_raised { agent(:connect).negotiate! }
63
- end
64
-
65
- def test_identities_should_fail_if_SSH_AGENT_FAILURE_recieved
66
- socket.expect do |s, type, buffer|
67
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
68
- s.return(SSH_AGENT_FAILURE)
69
- end
70
- assert_raises(Net::SSH::Authentication::AgentError) { agent.identities }
71
- end
72
-
73
- def test_identities_should_fail_if_SSH2_AGENT_FAILURE_recieved
74
- socket.expect do |s, type, buffer|
75
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
76
- s.return(SSH2_AGENT_FAILURE)
77
- end
78
- assert_raises(Net::SSH::Authentication::AgentError) { agent.identities }
79
- end
80
-
81
- def test_identities_should_fail_if_SSH_COM_AGENT2_FAILURE_recieved
82
- socket.expect do |s, type, buffer|
83
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
84
- s.return(SSH_COM_AGENT2_FAILURE)
85
- end
86
- assert_raises(Net::SSH::Authentication::AgentError) { agent.identities }
87
- end
88
-
89
- def test_identities_should_fail_if_response_is_not_SSH2_AGENT_IDENTITIES_ANSWER
90
- socket.expect do |s, type, buffer|
91
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
92
- s.return(255)
93
- end
94
- assert_raises(Net::SSH::Authentication::AgentError) { agent.identities }
95
- end
96
-
97
- def test_identities_should_augment_identities_with_comment_field
98
- key1 = key
99
- key2 = OpenSSL::PKey::DSA.new(512)
100
-
101
- socket.expect do |s, type, buffer|
102
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
103
- s.return(SSH2_AGENT_IDENTITIES_ANSWER, :long, 2, :string, Net::SSH::Buffer.from(:key, key1), :string, "My favorite key", :string, Net::SSH::Buffer.from(:key, key2), :string, "Okay, but not the best")
104
- end
105
-
106
- result = agent.identities
107
- assert_equal key1.to_blob, result.first.to_blob
108
- assert_equal key2.to_blob, result.last.to_blob
109
- assert_equal "My favorite key", result.first.comment
110
- assert_equal "Okay, but not the best", result.last.comment
111
- end
112
-
113
- def test_identities_should_ignore_unimplemented_ones
114
- key1 = key
115
- key2 = OpenSSL::PKey::DSA.new(512)
116
- key2.to_blob[0..5]='badkey'
117
- key3 = OpenSSL::PKey::DSA.new(512)
118
-
119
- socket.expect do |s, type, buffer|
120
- assert_equal SSH2_AGENT_REQUEST_IDENTITIES, type
121
- s.return(SSH2_AGENT_IDENTITIES_ANSWER, :long, 3, :string, Net::SSH::Buffer.from(:key, key1), :string, "My favorite key", :string, Net::SSH::Buffer.from(:key, key2), :string, "bad", :string, Net::SSH::Buffer.from(:key, key3), :string, "Okay, but not the best")
122
- end
123
-
124
- result = agent.identities
125
- assert_equal 2,result.size
126
- assert_equal key1.to_blob, result.first.to_blob
127
- assert_equal key3.to_blob, result.last.to_blob
128
- assert_equal "My favorite key", result.first.comment
129
- assert_equal "Okay, but not the best", result.last.comment
130
- end
131
-
132
- def test_close_should_close_socket
133
- socket.expects(:close)
134
- agent.close
135
- end
136
-
137
- def test_sign_should_fail_if_response_is_SSH_AGENT_FAILURE
138
- socket.expect { |s,| s.return(SSH_AGENT_FAILURE) }
139
- assert_raises(Net::SSH::Authentication::AgentError) { agent.sign(key, "hello world") }
140
- end
141
-
142
- def test_sign_should_fail_if_response_is_SSH2_AGENT_FAILURE
143
- socket.expect { |s,| s.return(SSH2_AGENT_FAILURE) }
144
- assert_raises(Net::SSH::Authentication::AgentError) { agent.sign(key, "hello world") }
145
- end
146
-
147
- def test_sign_should_fail_if_response_is_SSH_COM_AGENT2_FAILURE
148
- socket.expect { |s,| s.return(SSH_COM_AGENT2_FAILURE) }
149
- assert_raises(Net::SSH::Authentication::AgentError) { agent.sign(key, "hello world") }
150
- end
151
-
152
- def test_sign_should_fail_if_response_is_not_SSH2_AGENT_SIGN_RESPONSE
153
- socket.expect { |s,| s.return(255) }
154
- assert_raises(Net::SSH::Authentication::AgentError) { agent.sign(key, "hello world") }
155
- end
156
-
157
- def test_sign_should_return_signed_data_from_agent
158
- socket.expect do |s,type,buffer|
159
- assert_equal SSH2_AGENT_SIGN_REQUEST, type
160
- assert_equal key.to_blob, Net::SSH::Buffer.new(buffer.read_string).read_key.to_blob
161
- assert_equal "hello world", buffer.read_string
162
- assert_equal 0, buffer.read_long
163
-
164
- s.return(SSH2_AGENT_SIGN_RESPONSE, :string, "abcxyz123")
165
- end
166
-
167
- assert_equal "abcxyz123", agent.sign(key, "hello world")
168
- end
169
-
170
- private
171
-
172
- class MockSocket
173
- def initialize
174
- @expectation = nil
175
- @buffer = Net::SSH::Buffer.new
176
- end
177
-
178
- def expect(&block)
179
- @expectation = block
180
- end
181
-
182
- def return(type, *args)
183
- data = Net::SSH::Buffer.from(*args)
184
- @buffer.append([data.length+1, type, data.to_s].pack("NCA*"))
185
- end
186
-
187
- def send(data, flags)
188
- raise "got #{data.inspect} but no packet was expected" unless @expectation
189
- buffer = Net::SSH::Buffer.new(data)
190
- buffer.read_long # skip the length
191
- type = buffer.read_byte
192
- @expectation.call(self, type, buffer)
193
- @expectation = nil
194
- end
195
-
196
- def read(length)
197
- @buffer.read(length)
198
- end
199
- end
200
-
201
- def key
202
- @key ||= OpenSSL::PKey::RSA.new(512)
203
- end
204
-
205
- def socket
206
- @socket ||= MockSocket.new
207
- end
208
-
209
- def factory
210
- @factory ||= stub("socket factory", :open => socket)
211
- end
212
-
213
- def agent(auto=:connect)
214
- @agent ||= begin
215
- agent = Net::SSH::Authentication::Agent.new
216
- agent.stubs(:agent_socket_factory).returns(factory)
217
- agent.connect! if auto == :connect
218
- agent
219
- end
220
- end
221
-
222
- end
223
-
224
- end
@@ -1,227 +0,0 @@
1
- require 'common'
2
- require 'net/ssh/authentication/key_manager'
3
-
4
- module Authentication
5
-
6
- class TestKeyManager < Test::Unit::TestCase
7
- def test_key_files_and_known_identities_are_empty_by_default
8
- assert manager.key_files.empty?
9
- assert manager.known_identities.empty?
10
- end
11
-
12
- def test_assume_agent_is_available_by_default
13
- assert manager.use_agent?
14
- end
15
-
16
- def test_add_ensures_list_is_unique
17
- manager.add "/first"
18
- manager.add "/second"
19
- manager.add "/third"
20
- manager.add "/second"
21
- assert_equal 3, manager.key_files.length
22
- final_files = manager.key_files.map {|item| item.split('/').last}
23
- assert_equal %w(first second third), final_files
24
- end
25
-
26
- def test_use_agent_should_be_set_to_false_if_agent_could_not_be_found
27
- Net::SSH::Authentication::Agent.expects(:connect).raises(Net::SSH::Authentication::AgentNotAvailable)
28
- assert manager.use_agent?
29
- assert_nil manager.agent
30
- assert !manager.use_agent?
31
- end
32
-
33
- def test_agent_should_be_used_by_default
34
- assert manager().use_agent?
35
- end
36
-
37
- def test_agent_should_not_be_used_with_no_agent
38
- assert !manager(:use_agent => false).use_agent?
39
- end
40
-
41
- def test_each_identity_should_load_from_key_files
42
- manager.stubs(:agent).returns(nil)
43
- first = File.expand_path("/first")
44
- second = File.expand_path("/second")
45
- stub_file_private_key first, rsa
46
- stub_file_private_key second, dsa
47
-
48
- identities = []
49
- manager.each_identity { |identity| identities << identity }
50
-
51
- assert_equal 2, identities.length
52
- assert_equal rsa.to_blob, identities.first.to_blob
53
- assert_equal dsa.to_blob, identities.last.to_blob
54
-
55
- assert_equal({:from => :file, :file => first, :key => rsa}, manager.known_identities[rsa])
56
- assert_equal({:from => :file, :file => second, :key => dsa}, manager.known_identities[dsa])
57
- end
58
-
59
- def test_identities_should_load_from_agent
60
- manager.stubs(:agent).returns(agent)
61
-
62
- identities = []
63
- manager.each_identity { |identity| identities << identity }
64
-
65
- assert_equal 2, identities.length
66
- assert_equal rsa.to_blob, identities.first.to_blob
67
- assert_equal dsa.to_blob, identities.last.to_blob
68
-
69
- assert_equal({:from => :agent}, manager.known_identities[rsa])
70
- assert_equal({:from => :agent}, manager.known_identities[dsa])
71
- end
72
-
73
- if defined?(OpenSSL::PKey::EC)
74
- def test_identities_with_ecdsa_should_load_from_agent
75
- manager.stubs(:agent).returns(agent_with_ecdsa_keys)
76
-
77
- identities = []
78
- manager.each_identity { |identity| identities << identity }
79
- assert_equal 5, identities.length
80
-
81
- assert_equal rsa.to_blob, identities[0].to_blob
82
- assert_equal dsa.to_blob, identities[1].to_blob
83
- assert_equal ecdsa_sha2_nistp256.to_blob, identities[2].to_blob
84
- assert_equal ecdsa_sha2_nistp384.to_blob, identities[3].to_blob
85
- assert_equal ecdsa_sha2_nistp521.to_blob, identities[4].to_blob
86
-
87
- assert_equal({:from => :agent}, manager.known_identities[rsa])
88
- assert_equal({:from => :agent}, manager.known_identities[dsa])
89
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp256])
90
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp384])
91
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp521])
92
- end
93
- end
94
-
95
- def test_only_identities_with_key_files_should_load_from_agent_of_keys_only_set
96
- manager(:keys_only => true).stubs(:agent).returns(agent)
97
-
98
- first = File.expand_path("/first")
99
- stub_file_private_key first, rsa
100
-
101
- identities = []
102
- manager.each_identity { |identity| identities << identity }
103
-
104
- assert_equal 1, identities.length
105
- assert_equal rsa.to_blob, identities.first.to_blob
106
-
107
- assert_equal({:from => :agent}, manager.known_identities[rsa])
108
- assert manager.use_agent?
109
- end
110
-
111
- def test_identities_without_public_key_files_should_not_be_touched_if_identity_loaded_from_agent
112
- manager.stubs(:agent).returns(agent)
113
-
114
- first = File.expand_path("/first")
115
- stub_file_public_key first, rsa
116
- second = File.expand_path("/second")
117
- stub_file_private_key second, dsa, :passphrase => :should_not_be_asked
118
-
119
- identities = []
120
- manager.each_identity do |identity|
121
- identities << identity
122
- break if manager.known_identities[identity][:from] == :agent
123
- end
124
-
125
- assert_equal 1, identities.length
126
- assert_equal rsa.to_blob, identities.first.to_blob
127
- end
128
-
129
- def test_sign_with_agent_originated_key_should_request_signature_from_agent
130
- manager.stubs(:agent).returns(agent)
131
- manager.each_identity { |identity| } # preload the known_identities
132
- agent.expects(:sign).with(rsa, "hello, world").returns("abcxyz123")
133
- assert_equal "abcxyz123", manager.sign(rsa, "hello, world")
134
- end
135
-
136
- def test_sign_with_file_originated_key_should_load_private_key_and_sign_with_it
137
- manager.stubs(:agent).returns(nil)
138
- first = File.expand_path("/first")
139
- stub_file_private_key first, rsa(512)
140
- rsa.expects(:ssh_do_sign).with("hello, world").returns("abcxyz123")
141
- manager.each_identity { |identity| } # preload the known_identities
142
- assert_equal "\0\0\0\assh-rsa\0\0\0\011abcxyz123", manager.sign(rsa, "hello, world")
143
- end
144
-
145
- def test_sign_with_file_originated_key_should_raise_key_manager_error_if_unloadable
146
- manager.known_identities[rsa] = { :from => :file, :file => "/first" }
147
-
148
- Net::SSH::KeyFactory.expects(:load_private_key).raises(OpenSSL::PKey::RSAError)
149
-
150
- assert_raises Net::SSH::Authentication::KeyManagerError do
151
- manager.sign(rsa, "hello, world")
152
- end
153
- end
154
-
155
- private
156
-
157
- def stub_file_private_key(name, key, options = {})
158
- manager.add(name)
159
- File.stubs(:readable?).with(name).returns(true)
160
- File.stubs(:readable?).with(name + ".pub").returns(false)
161
-
162
- case options.fetch(:passphrase, :indifferently)
163
- when :should_be_asked
164
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, false).raises(OpenSSL::PKey::RSAError).at_least_once
165
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, true).returns(key).at_least_once
166
- when :should_not_be_asked
167
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, false).raises(OpenSSL::PKey::RSAError).at_least_once
168
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, true).never
169
- else # :indifferently
170
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, any_of(true, false)).returns(key).at_least_once
171
- end
172
-
173
- # do not override OpenSSL::PKey::EC#public_key
174
- # (it will be called in transport/openssl.rb.)
175
- unless defined?(OpenSSL::PKey::EC) && key.public_key.kind_of?(OpenSSL::PKey::EC::Point)
176
- key.stubs(:public_key).returns(key)
177
- end
178
- end
179
-
180
- def stub_file_public_key(name, key)
181
- manager.add(name)
182
- File.stubs(:readable?).with(name).returns(false)
183
- File.stubs(:readable?).with(name + ".pub").returns(true)
184
-
185
- Net::SSH::KeyFactory.expects(:load_public_key).with(name + ".pub").returns(key).at_least_once
186
- end
187
-
188
- def rsa(size=512)
189
- @rsa ||= OpenSSL::PKey::RSA.new(size)
190
- end
191
-
192
- def dsa
193
- @dsa ||= OpenSSL::PKey::DSA.new(512)
194
- end
195
-
196
- if defined?(OpenSSL::PKey::EC)
197
- def ecdsa_sha2_nistp256
198
- @ecdsa_sha2_nistp256 ||= OpenSSL::PKey::EC.new("prime256v1").generate_key
199
- end
200
-
201
- def ecdsa_sha2_nistp384
202
- @ecdsa_sha2_nistp384 ||= OpenSSL::PKey::EC.new("secp384r1").generate_key
203
- end
204
-
205
- def ecdsa_sha2_nistp521
206
- @ecdsa_sha2_nistp521 ||= OpenSSL::PKey::EC.new("secp521r1").generate_key
207
- end
208
- end
209
-
210
- def agent
211
- @agent ||= stub("agent", :identities => [rsa, dsa])
212
- end
213
-
214
- def agent_with_ecdsa_keys
215
- @agent ||= stub("agent", :identities => [rsa, dsa,
216
- ecdsa_sha2_nistp256,
217
- ecdsa_sha2_nistp384,
218
- ecdsa_sha2_nistp521])
219
- end
220
-
221
- def manager(options = {})
222
- @manager ||= Net::SSH::Authentication::KeyManager.new(nil, options)
223
- end
224
-
225
- end
226
-
227
- end
@@ -1,107 +0,0 @@
1
- require 'common'
2
- require 'net/ssh/authentication/session'
3
-
4
- module Authentication
5
-
6
- class TestSession < Test::Unit::TestCase
7
- include Net::SSH::Transport::Constants
8
- include Net::SSH::Authentication::Constants
9
-
10
- def test_constructor_should_set_defaults
11
- assert_equal %w(none publickey password keyboard-interactive), session.auth_methods
12
- assert_equal session.auth_methods, session.allowed_auth_methods
13
- end
14
-
15
- def test_authenticate_should_continue_if_method_disallowed
16
- transport.expect do |t, packet|
17
- assert_equal SERVICE_REQUEST, packet.type
18
- assert_equal "ssh-userauth", packet.read_string
19
- t.return(SERVICE_ACCEPT)
20
- end
21
-
22
- Net::SSH::Authentication::Methods::Publickey.any_instance.expects(:authenticate).with("next service", "username", "password").raises(Net::SSH::Authentication::DisallowedMethod)
23
- Net::SSH::Authentication::Methods::Password.any_instance.expects(:authenticate).with("next service", "username", "password").returns(true)
24
- Net::SSH::Authentication::Methods::None.any_instance.expects(:authenticate).with("next service", "username", "password").returns(false)
25
-
26
- assert session.authenticate("next service", "username", "password")
27
- end
28
-
29
- def test_authenticate_should_raise_error_if_service_request_fails
30
- transport.expect do |t, packet|
31
- assert_equal SERVICE_REQUEST, packet.type
32
- assert_equal "ssh-userauth", packet.read_string
33
- t.return(255)
34
- end
35
-
36
- assert_raises(Net::SSH::Exception) { session.authenticate("next service", "username", "password") }
37
- end
38
-
39
- def test_authenticate_should_return_false_if_all_auth_methods_fail
40
- transport.expect do |t, packet|
41
- assert_equal SERVICE_REQUEST, packet.type
42
- assert_equal "ssh-userauth", packet.read_string
43
- t.return(SERVICE_ACCEPT)
44
- end
45
-
46
- Net::SSH::Authentication::Methods::Publickey.any_instance.expects(:authenticate).with("next service", "username", "password").returns(false)
47
- Net::SSH::Authentication::Methods::Password.any_instance.expects(:authenticate).with("next service", "username", "password").returns(false)
48
- Net::SSH::Authentication::Methods::KeyboardInteractive.any_instance.expects(:authenticate).with("next service", "username", "password").returns(false)
49
- Net::SSH::Authentication::Methods::None.any_instance.expects(:authenticate).with("next service", "username", "password").returns(false)
50
-
51
- assert_equal false, session.authenticate("next service", "username", "password")
52
- end
53
-
54
- def test_next_message_should_silently_handle_USERAUTH_BANNER_packets
55
- transport.return(USERAUTH_BANNER, :string, "Howdy, folks!")
56
- transport.return(SERVICE_ACCEPT)
57
- assert_equal SERVICE_ACCEPT, session.next_message.type
58
- end
59
-
60
- def test_next_message_should_understand_USERAUTH_FAILURE
61
- transport.return(USERAUTH_FAILURE, :string, "a,b,c", :bool, false)
62
- packet = session.next_message
63
- assert_equal USERAUTH_FAILURE, packet.type
64
- assert_equal %w(a b c), session.allowed_auth_methods
65
- end
66
-
67
- (60..79).each do |type|
68
- define_method("test_next_message_should_return_packets_of_type_#{type}") do
69
- transport.return(type)
70
- assert_equal type, session.next_message.type
71
- end
72
- end
73
-
74
- def test_next_message_should_understand_USERAUTH_SUCCESS
75
- transport.return(USERAUTH_SUCCESS)
76
- assert !transport.hints[:authenticated]
77
- assert_equal USERAUTH_SUCCESS, session.next_message.type
78
- assert transport.hints[:authenticated]
79
- end
80
-
81
- def test_next_message_should_raise_error_on_unrecognized_packet_types
82
- transport.return(1)
83
- assert_raises(Net::SSH::Exception) { session.next_message }
84
- end
85
-
86
- def test_expect_message_should_raise_exception_if_next_packet_is_not_expected_type
87
- transport.return(SERVICE_ACCEPT)
88
- assert_raises(Net::SSH::Exception) { session.expect_message(USERAUTH_BANNER) }
89
- end
90
-
91
- def test_expect_message_should_return_packet_if_next_packet_is_expected_type
92
- transport.return(SERVICE_ACCEPT)
93
- assert_equal SERVICE_ACCEPT, session.expect_message(SERVICE_ACCEPT).type
94
- end
95
-
96
- private
97
-
98
- def session(options={})
99
- @session ||= Net::SSH::Authentication::Session.new(transport(options), options)
100
- end
101
-
102
- def transport(options={})
103
- @transport ||= MockTransport.new(options)
104
- end
105
- end
106
-
107
- end
data/test/common.rb DELETED
@@ -1,108 +0,0 @@
1
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
- gem "test-unit" # http://rubyforge.org/pipermail/test-unit-tracker/2009-July/000075.html
3
- gem 'mocha'
4
- require 'test/unit'
5
- require 'mocha/setup'
6
- require 'net/ssh/buffer'
7
- require 'net/ssh/config'
8
- require 'net/ssh/loggable'
9
- require 'net/ssh/packet'
10
- require 'net/ssh/transport/session'
11
- require 'ostruct'
12
-
13
- # clear the default files out so that tests don't get confused by existing
14
- # SSH config files.
15
- $original_config_default_files = Net::SSH::Config.default_files.dup
16
- Net::SSH::Config.default_files.clear
17
-
18
- def P(*args)
19
- Net::SSH::Packet.new(Net::SSH::Buffer.from(*args))
20
- end
21
-
22
- class MockTransport < Net::SSH::Transport::Session
23
- class BlockVerifier
24
- def initialize(block)
25
- @block = block
26
- end
27
-
28
- def verify(data)
29
- @block.call(data)
30
- end
31
- end
32
-
33
- attr_reader :host_key_verifier
34
- attr_accessor :host_as_string
35
- attr_accessor :server_version
36
-
37
- attr_reader :client_options
38
- attr_reader :server_options
39
- attr_reader :hints, :queue
40
-
41
- attr_accessor :mock_enqueue
42
-
43
- def initialize(options={})
44
- self.logger = options[:logger]
45
- self.host_as_string = "net.ssh.test,127.0.0.1"
46
- self.server_version = OpenStruct.new(:version => "SSH-2.0-Ruby/Net::SSH::Test")
47
- @expectation = nil
48
- @queue = []
49
- @hints = {}
50
- @socket = options[:socket]
51
- @algorithms = OpenStruct.new(:session_id => "abcxyz123")
52
- verifier { |data| true }
53
- end
54
-
55
- def send_message(message)
56
- buffer = Net::SSH::Buffer.new(message.to_s)
57
- if @expectation.nil?
58
- raise "got #{message.to_s.inspect} but was not expecting anything"
59
- else
60
- block, @expectation = @expectation, nil
61
- block.call(self, Net::SSH::Packet.new(buffer))
62
- end
63
- end
64
-
65
- def enqueue_message(message)
66
- if mock_enqueue
67
- send_message(message)
68
- else
69
- super
70
- end
71
- end
72
-
73
- def poll_message
74
- @queue.shift
75
- end
76
-
77
- def next_message
78
- @queue.shift or raise "expected a message from the server but nothing was ready to send"
79
- end
80
-
81
- def return(type, *args)
82
- @queue << P(:byte, type, *args)
83
- end
84
-
85
- def expect(&block)
86
- @expectation = block
87
- end
88
-
89
- def expect!
90
- expect {}
91
- end
92
-
93
- def verifier(&block)
94
- @host_key_verifier = BlockVerifier.new(block)
95
- end
96
-
97
- def configure_client(options)
98
- @client_options = options
99
- end
100
-
101
- def configure_server(options)
102
- @server_options = options
103
- end
104
-
105
- def hint(name, value=true)
106
- @hints[name] = value
107
- end
108
- end
@@ -1,5 +0,0 @@
1
- HostBasedAuthentication no
2
- PasswordAuthentication no
3
- PubKeyAuthentication no
4
- ChallengeResponseAuthentication no
5
- KbdInteractiveAuthentication no
data/test/configs/auth_on DELETED
@@ -1,4 +0,0 @@
1
- HostBasedAuthentication yes
2
- PasswordAuthentication yes
3
- PubKeyAuthentication yes
4
- ChallengeResponseAuthentication yes
data/test/configs/empty DELETED
File without changes
data/test/configs/eqsign DELETED
@@ -1,3 +0,0 @@
1
- Host=test.test
2
- Port =1234
3
- Compression yes
@@ -1,8 +0,0 @@
1
- Host other.host
2
- Compression no
3
- Port 1231
4
-
5
- Host test.host
6
- Compression yes
7
- ForwardAgent yes
8
- Port 1234