net-ssh 4.0.0.alpha1 → 4.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gitignore +6 -0
  5. data/.travis.yml +10 -7
  6. data/CHANGES.txt +4 -0
  7. data/Gemfile +2 -15
  8. data/README.rdoc +6 -37
  9. data/Rakefile +5 -57
  10. data/lib/net/ssh/connection/session.rb +13 -0
  11. data/lib/net/ssh/ruby_compat.rb +2 -24
  12. data/lib/net/ssh/version.rb +1 -1
  13. data/net-ssh.gemspec +34 -225
  14. metadata +26 -100
  15. metadata.gz.sig +0 -0
  16. data/test/README.txt +0 -18
  17. data/test/authentication/methods/common.rb +0 -28
  18. data/test/authentication/methods/test_abstract.rb +0 -51
  19. data/test/authentication/methods/test_hostbased.rb +0 -114
  20. data/test/authentication/methods/test_keyboard_interactive.rb +0 -121
  21. data/test/authentication/methods/test_none.rb +0 -41
  22. data/test/authentication/methods/test_password.rb +0 -95
  23. data/test/authentication/methods/test_publickey.rb +0 -148
  24. data/test/authentication/test_agent.rb +0 -224
  25. data/test/authentication/test_ed25519.rb +0 -77
  26. data/test/authentication/test_key_manager.rb +0 -240
  27. data/test/authentication/test_session.rb +0 -107
  28. data/test/common.rb +0 -109
  29. data/test/configs/auth_off +0 -5
  30. data/test/configs/auth_on +0 -4
  31. data/test/configs/empty +0 -0
  32. data/test/configs/eqsign +0 -3
  33. data/test/configs/exact_match +0 -8
  34. data/test/configs/host_plus +0 -10
  35. data/test/configs/multihost +0 -4
  36. data/test/configs/negative_match +0 -6
  37. data/test/configs/nohost +0 -19
  38. data/test/configs/numeric_host +0 -4
  39. data/test/configs/proxy_remote_user +0 -2
  40. data/test/configs/send_env +0 -2
  41. data/test/configs/substitutes +0 -8
  42. data/test/configs/wild_cards +0 -14
  43. data/test/connection/test_channel.rb +0 -487
  44. data/test/connection/test_session.rb +0 -563
  45. data/test/integration/README.md +0 -18
  46. data/test/integration/Vagrantfile +0 -12
  47. data/test/integration/common.rb +0 -65
  48. data/test/integration/playbook.yml +0 -57
  49. data/test/integration/test_ed25519_pkeys.rb +0 -70
  50. data/test/integration/test_forward.rb +0 -532
  51. data/test/integration/test_id_rsa_keys.rb +0 -96
  52. data/test/integration/test_proxy.rb +0 -93
  53. data/test/known_hosts/github +0 -1
  54. data/test/known_hosts/github_hash +0 -1
  55. data/test/manual/test_pageant.rb +0 -37
  56. data/test/start/test_connection.rb +0 -53
  57. data/test/start/test_options.rb +0 -57
  58. data/test/start/test_transport.rb +0 -28
  59. data/test/start/test_user_nil.rb +0 -27
  60. data/test/test_all.rb +0 -12
  61. data/test/test_buffer.rb +0 -433
  62. data/test/test_buffered_io.rb +0 -63
  63. data/test/test_config.rb +0 -268
  64. data/test/test_key_factory.rb +0 -191
  65. data/test/test_known_hosts.rb +0 -66
  66. data/test/transport/hmac/test_md5.rb +0 -41
  67. data/test/transport/hmac/test_md5_96.rb +0 -27
  68. data/test/transport/hmac/test_none.rb +0 -34
  69. data/test/transport/hmac/test_ripemd160.rb +0 -36
  70. data/test/transport/hmac/test_sha1.rb +0 -36
  71. data/test/transport/hmac/test_sha1_96.rb +0 -27
  72. data/test/transport/hmac/test_sha2_256.rb +0 -37
  73. data/test/transport/hmac/test_sha2_256_96.rb +0 -27
  74. data/test/transport/hmac/test_sha2_512.rb +0 -37
  75. data/test/transport/hmac/test_sha2_512_96.rb +0 -27
  76. data/test/transport/kex/test_diffie_hellman_group14_sha1.rb +0 -13
  77. data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +0 -150
  78. data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +0 -96
  79. data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +0 -19
  80. data/test/transport/kex/test_ecdh_sha2_nistp256.rb +0 -161
  81. data/test/transport/kex/test_ecdh_sha2_nistp384.rb +0 -38
  82. data/test/transport/kex/test_ecdh_sha2_nistp521.rb +0 -38
  83. data/test/transport/test_algorithms.rb +0 -328
  84. data/test/transport/test_cipher_factory.rb +0 -324
  85. data/test/transport/test_hmac.rb +0 -34
  86. data/test/transport/test_identity_cipher.rb +0 -40
  87. data/test/transport/test_packet_stream.rb +0 -1186
  88. data/test/transport/test_server_version.rb +0 -74
  89. data/test/transport/test_session.rb +0 -331
  90. data/test/transport/test_state.rb +0 -181
  91. data/test/verifiers/test_secure.rb +0 -40
@@ -1,77 +0,0 @@
1
- require 'common'
2
- require 'net/ssh/authentication/ed25519'
3
- require 'base64'
4
-
5
- module Authentication
6
-
7
- class TestED25519 < Test::Unit::TestCase
8
- def test_no_pwd_key
9
- pub = Net::SSH::Buffer.new(Base64.decode64(public_key_no_pwd.split(' ')[1]))
10
- _type = pub.read_string
11
- pub_data = pub.read_string
12
- priv = private_key_no_pwd
13
-
14
- pub_key = ED25519::PubKey.new(pub_data)
15
- priv_key = ED25519::PrivKey.new(priv,nil)
16
-
17
- shared_secret = "Hello"
18
- signed = priv_key.ssh_do_sign(shared_secret)
19
- self.assert_equal(true,pub_key.ssh_do_verify(signed,shared_secret))
20
- self.assert_equal(priv_key.public_key.fingerprint, pub_key.fingerprint)
21
- end
22
-
23
- def test_pwd_key
24
- if defined?(JRUBY_VERSION)
25
- puts "Skipping password protected ED25519 for JRuby"
26
- return
27
- end
28
- pub = Net::SSH::Buffer.new(Base64.decode64(public_key_pwd.split(' ')[1]))
29
- _type = pub.read_string
30
- pub_data = pub.read_string
31
- priv = private_key_pwd
32
-
33
- pub_key = ED25519::PubKey.new(pub_data)
34
- priv_key = ED25519::PrivKey.new(priv,'pwd')
35
-
36
- shared_secret = "Hello"
37
- signed = priv_key.ssh_do_sign(shared_secret)
38
- self.assert_equal(true,pub_key.ssh_do_verify(signed,shared_secret))
39
- self.assert_equal(priv_key.public_key.fingerprint, pub_key.fingerprint)
40
- end
41
-
42
- def private_key_pwd
43
- @pwd_key = <<-EOF
44
- -----BEGIN OPENSSH PRIVATE KEY-----
45
- b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jYmMAAAAGYmNyeXB0AAAAGAAAABBxwCvr3V
46
- /8pWhC/xvTnGJhAAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAICaHkFaGXqYhUVFc
47
- aZ10TPUbkIvmaFXwYRoOS5qE8MciAAAAsNUAhbNQKwNcOr0eNq3nhtjoyeVyH8hRrpWsiY
48
- 46vPiECi6R6OdYGSd7W3fdzUDeyOYCY9ZVIjAzENG+9FsygYzMi6XCuw00OuDFLUp4fL4K
49
- i/coUIVqouB4TPQAmsCVXiIRVTWQtRG0kWfFaV3qRt/bc22ZCvCT6ZZ1UmtulqqfUhSlKM
50
- oPcTikV1iWH5Xc+GxRFRRGTN/6HvBf0AKDB1kMXlDhGnBnHGeNH1pk44xG
51
- -----END OPENSSH PRIVATE KEY-----
52
- EOF
53
- end
54
-
55
- def public_key_pwd
56
- 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICaHkFaGXqYhUVFcaZ10TPUbkIvmaFXwYRoOS5qE8Mci vagrant@vagrant-ubuntu-trusty-64'
57
- end
58
-
59
- def private_key_no_pwd
60
- @anonymous_key = <<-EOF
61
- -----BEGIN OPENSSH PRIVATE KEY-----
62
- b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
63
- QyNTUxOQAAACAwdjQYeBiTz1DdZFzzLvG+t913L+eVqCgtzpAYxQG8yQAAAKjlHzLo5R8y
64
- 6AAAAAtzc2gtZWQyNTUxOQAAACAwdjQYeBiTz1DdZFzzLvG+t913L+eVqCgtzpAYxQG8yQ
65
- AAAEBPrD+n4901Y+NYJ2sry+EWRdltGFhMISvp91TywJ//mTB2NBh4GJPPUN1kXPMu8b63
66
- 3Xcv55WoKC3OkBjFAbzJAAAAIHZhZ3JhbnRAdmFncmFudC11YnVudHUtdHJ1c3R5LTY0AQ
67
- IDBAU=
68
- -----END OPENSSH PRIVATE KEY-----
69
- EOF
70
- end
71
-
72
- def public_key_no_pwd
73
- 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDB2NBh4GJPPUN1kXPMu8b633Xcv55WoKC3OkBjFAbzJ vagrant@vagrant-ubuntu-trusty-64'
74
- end
75
- end
76
-
77
- end
@@ -1,240 +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_each_identity_should_not_prompt_for_passphrase_in_non_interactive_mode
60
- manager(:non_interactive => true).stubs(:agent).returns(nil)
61
- first = File.expand_path("/first")
62
- stub_file_private_key first, rsa, :passphrase => :should_not_be_asked
63
- identities = []
64
- manager.each_identity { |identity| identities << identity }
65
- assert_equal(identities, [])
66
- end
67
-
68
- def test_identities_should_load_from_agent
69
- manager.stubs(:agent).returns(agent)
70
-
71
- identities = []
72
- manager.each_identity { |identity| identities << identity }
73
-
74
- assert_equal 2, identities.length
75
- assert_equal rsa.to_blob, identities.first.to_blob
76
- assert_equal dsa.to_blob, identities.last.to_blob
77
-
78
- assert_equal({:from => :agent}, manager.known_identities[rsa])
79
- assert_equal({:from => :agent}, manager.known_identities[dsa])
80
- end
81
-
82
- if defined?(OpenSSL::PKey::EC)
83
- def test_identities_with_ecdsa_should_load_from_agent
84
- manager.stubs(:agent).returns(agent_with_ecdsa_keys)
85
-
86
- identities = []
87
- manager.each_identity { |identity| identities << identity }
88
- assert_equal 5, identities.length
89
-
90
- assert_equal rsa.to_blob, identities[0].to_blob
91
- assert_equal dsa.to_blob, identities[1].to_blob
92
- assert_equal ecdsa_sha2_nistp256.to_blob, identities[2].to_blob
93
- assert_equal ecdsa_sha2_nistp384.to_blob, identities[3].to_blob
94
- assert_equal ecdsa_sha2_nistp521.to_blob, identities[4].to_blob
95
-
96
- assert_equal({:from => :agent}, manager.known_identities[rsa])
97
- assert_equal({:from => :agent}, manager.known_identities[dsa])
98
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp256])
99
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp384])
100
- assert_equal({:from => :agent}, manager.known_identities[ecdsa_sha2_nistp521])
101
- end
102
- end
103
-
104
- def test_only_identities_with_key_files_should_load_from_agent_of_keys_only_set
105
- manager(:keys_only => true).stubs(:agent).returns(agent)
106
-
107
- first = File.expand_path("/first")
108
- stub_file_private_key first, rsa
109
-
110
- identities = []
111
- manager.each_identity { |identity| identities << identity }
112
-
113
- assert_equal 1, identities.length
114
- assert_equal rsa.to_blob, identities.first.to_blob
115
-
116
- assert_equal({:from => :agent}, manager.known_identities[rsa])
117
- assert manager.use_agent?
118
- end
119
-
120
- def test_identities_without_public_key_files_should_not_be_touched_if_identity_loaded_from_agent
121
- manager.stubs(:agent).returns(agent)
122
-
123
- first = File.expand_path("/first")
124
- stub_file_public_key first, rsa
125
- second = File.expand_path("/second")
126
- stub_file_private_key second, dsa, :passphrase => :should_not_be_asked
127
-
128
- identities = []
129
- manager.each_identity do |identity|
130
- identities << identity
131
- break if manager.known_identities[identity][:from] == :agent
132
- end
133
-
134
- assert_equal 1, identities.length
135
- assert_equal rsa.to_blob, identities.first.to_blob
136
- end
137
-
138
- def test_sign_with_agent_originated_key_should_request_signature_from_agent
139
- manager.stubs(:agent).returns(agent)
140
- manager.each_identity { |identity| } # preload the known_identities
141
- agent.expects(:sign).with(rsa, "hello, world").returns("abcxyz123")
142
- assert_equal "abcxyz123", manager.sign(rsa, "hello, world")
143
- end
144
-
145
- def test_sign_with_file_originated_key_should_load_private_key_and_sign_with_it
146
- manager.stubs(:agent).returns(nil)
147
- first = File.expand_path("/first")
148
- stub_file_private_key first, rsa(512)
149
- rsa.expects(:ssh_do_sign).with("hello, world").returns("abcxyz123")
150
- manager.each_identity { |identity| } # preload the known_identities
151
- assert_equal "\0\0\0\assh-rsa\0\0\0\011abcxyz123", manager.sign(rsa, "hello, world")
152
- end
153
-
154
- def test_sign_with_file_originated_key_should_raise_key_manager_error_if_unloadable
155
- manager.known_identities[rsa] = { :from => :file, :file => "/first" }
156
-
157
- Net::SSH::KeyFactory.expects(:load_private_key).raises(OpenSSL::PKey::RSAError)
158
-
159
- assert_raises Net::SSH::Authentication::KeyManagerError do
160
- manager.sign(rsa, "hello, world")
161
- end
162
- end
163
-
164
- private
165
-
166
- def stub_file_private_key(name, key, options = {})
167
- manager.add(name)
168
- File.stubs(:file?).with(name).returns(true)
169
- File.stubs(:readable?).with(name).returns(true)
170
- File.stubs(:file?).with(name + ".pub").returns(true)
171
- File.stubs(:readable?).with(name + ".pub").returns(false)
172
-
173
- case options.fetch(:passphrase, :indifferently)
174
- when :should_be_asked
175
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, false).raises(OpenSSL::PKey::RSAError).at_least_once
176
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, true).returns(key).at_least_once
177
- when :should_not_be_asked
178
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, false).raises(OpenSSL::PKey::RSAError).at_least_once
179
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, true).never
180
- else # :indifferently
181
- Net::SSH::KeyFactory.expects(:load_private_key).with(name, nil, any_of(true, false)).returns(key).at_least_once
182
- end
183
-
184
- # do not override OpenSSL::PKey::EC#public_key
185
- # (it will be called in transport/openssl.rb.)
186
- unless defined?(OpenSSL::PKey::EC) && key.public_key.kind_of?(OpenSSL::PKey::EC::Point)
187
- key.stubs(:public_key).returns(key)
188
- end
189
- end
190
-
191
- def stub_file_public_key(name, key)
192
- manager.add(name)
193
- File.stubs(:file?).with(name).returns(true)
194
- File.stubs(:readable?).with(name).returns(true)
195
- File.stubs(:file?).with(name + ".pub").returns(true)
196
- File.stubs(:readable?).with(name + ".pub").returns(true)
197
-
198
- Net::SSH::KeyFactory.expects(:load_public_key).with(name + ".pub").returns(key).at_least_once
199
- end
200
-
201
- def rsa(size=512)
202
- @rsa ||= OpenSSL::PKey::RSA.new(size)
203
- end
204
-
205
- def dsa
206
- @dsa ||= OpenSSL::PKey::DSA.new(512)
207
- end
208
-
209
- if defined?(OpenSSL::PKey::EC)
210
- def ecdsa_sha2_nistp256
211
- @ecdsa_sha2_nistp256 ||= OpenSSL::PKey::EC.new("prime256v1").generate_key
212
- end
213
-
214
- def ecdsa_sha2_nistp384
215
- @ecdsa_sha2_nistp384 ||= OpenSSL::PKey::EC.new("secp384r1").generate_key
216
- end
217
-
218
- def ecdsa_sha2_nistp521
219
- @ecdsa_sha2_nistp521 ||= OpenSSL::PKey::EC.new("secp521r1").generate_key
220
- end
221
- end
222
-
223
- def agent
224
- @agent ||= stub("agent", :identities => [rsa, dsa])
225
- end
226
-
227
- def agent_with_ecdsa_keys
228
- @agent ||= stub("agent", :identities => [rsa, dsa,
229
- ecdsa_sha2_nistp256,
230
- ecdsa_sha2_nistp384,
231
- ecdsa_sha2_nistp521])
232
- end
233
-
234
- def manager(options = {})
235
- @manager ||= Net::SSH::Authentication::KeyManager.new(nil, options)
236
- end
237
-
238
- end
239
-
240
- 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
@@ -1,109 +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
- @options = options
45
- self.logger = options[:logger]
46
- self.host_as_string = "net.ssh.test,127.0.0.1"
47
- self.server_version = OpenStruct.new(:version => "SSH-2.0-Ruby/Net::SSH::Test")
48
- @expectation = nil
49
- @queue = []
50
- @hints = {}
51
- @socket = options[:socket]
52
- @algorithms = OpenStruct.new(:session_id => "abcxyz123")
53
- verifier { |data| true }
54
- end
55
-
56
- def send_message(message)
57
- buffer = Net::SSH::Buffer.new(message.to_s)
58
- if @expectation.nil?
59
- raise "got #{message.to_s.inspect} but was not expecting anything"
60
- else
61
- block, @expectation = @expectation, nil
62
- block.call(self, Net::SSH::Packet.new(buffer))
63
- end
64
- end
65
-
66
- def enqueue_message(message)
67
- if mock_enqueue
68
- send_message(message)
69
- else
70
- super
71
- end
72
- end
73
-
74
- def poll_message
75
- @queue.shift
76
- end
77
-
78
- def next_message
79
- @queue.shift or raise "expected a message from the server but nothing was ready to send"
80
- end
81
-
82
- def return(type, *args)
83
- @queue << P(:byte, type, *args)
84
- end
85
-
86
- def expect(&block)
87
- @expectation = block
88
- end
89
-
90
- def expect!
91
- expect {}
92
- end
93
-
94
- def verifier(&block)
95
- @host_key_verifier = BlockVerifier.new(block)
96
- end
97
-
98
- def configure_client(options)
99
- @client_options = options
100
- end
101
-
102
- def configure_server(options)
103
- @server_options = options
104
- end
105
-
106
- def hint(name, value=true)
107
- @hints[name] = value
108
- end
109
- end