net-ssh 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/doc/LICENSE-BSD +27 -0
  2. data/doc/LICENSE-GPL +280 -0
  3. data/doc/LICENSE-RUBY +56 -0
  4. data/doc/README +13 -0
  5. data/doc/manual-html/chapter-1.html +333 -0
  6. data/doc/manual-html/chapter-2.html +455 -0
  7. data/doc/manual-html/chapter-3.html +413 -0
  8. data/doc/manual-html/chapter-4.html +353 -0
  9. data/doc/manual-html/chapter-5.html +393 -0
  10. data/doc/manual-html/chapter-6.html +296 -0
  11. data/doc/manual-html/index.html +217 -0
  12. data/doc/manual-html/manual.css +192 -0
  13. data/doc/manual/chapter.erb +18 -0
  14. data/doc/manual/example.erb +18 -0
  15. data/doc/manual/index.erb +29 -0
  16. data/doc/manual/manual.css +192 -0
  17. data/doc/manual/manual.rb +240 -0
  18. data/doc/manual/manual.yml +67 -0
  19. data/doc/manual/page.erb +87 -0
  20. data/doc/manual/parts/channels_callbacks.txt +32 -0
  21. data/doc/manual/parts/channels_loop.txt +14 -0
  22. data/doc/manual/parts/channels_open.txt +20 -0
  23. data/doc/manual/parts/channels_operations.txt +15 -0
  24. data/doc/manual/parts/channels_types.txt +3 -0
  25. data/doc/manual/parts/channels_what_are.txt +7 -0
  26. data/doc/manual/parts/exec_channels.txt +28 -0
  27. data/doc/manual/parts/exec_open.txt +51 -0
  28. data/doc/manual/parts/exec_popen3.txt +35 -0
  29. data/doc/manual/parts/forward_direct.txt +37 -0
  30. data/doc/manual/parts/forward_handlers.txt +16 -0
  31. data/doc/manual/parts/forward_intro.txt +18 -0
  32. data/doc/manual/parts/forward_local.txt +18 -0
  33. data/doc/manual/parts/forward_remote.txt +14 -0
  34. data/doc/manual/parts/intro_author.txt +1 -0
  35. data/doc/manual/parts/intro_getting.txt +39 -0
  36. data/doc/manual/parts/intro_license.txt +6 -0
  37. data/doc/manual/parts/intro_support.txt +7 -0
  38. data/doc/manual/parts/intro_what_is.txt +7 -0
  39. data/doc/manual/parts/intro_what_is_not.txt +3 -0
  40. data/doc/manual/parts/proxy_http.txt +52 -0
  41. data/doc/manual/parts/proxy_intro.txt +1 -0
  42. data/doc/manual/parts/proxy_socks.txt +23 -0
  43. data/doc/manual/parts/session_key.txt +66 -0
  44. data/doc/manual/parts/session_options.txt +42 -0
  45. data/doc/manual/parts/session_session.txt +14 -0
  46. data/doc/manual/parts/session_start.txt +49 -0
  47. data/doc/manual/tutorial.erb +30 -0
  48. data/examples/channel-demo.rb +81 -0
  49. data/examples/port-forward.rb +51 -0
  50. data/examples/process-demo.rb +91 -0
  51. data/examples/remote-net-port-forward.rb +45 -0
  52. data/examples/remote-port-forward.rb +80 -0
  53. data/examples/tail-demo.rb +49 -0
  54. data/lib/net/ssh.rb +52 -0
  55. data/lib/net/ssh/connection/channel.rb +411 -0
  56. data/lib/net/ssh/connection/constants.rb +47 -0
  57. data/lib/net/ssh/connection/driver.rb +343 -0
  58. data/lib/net/ssh/connection/services.rb +72 -0
  59. data/lib/net/ssh/connection/term.rb +90 -0
  60. data/lib/net/ssh/errors.rb +27 -0
  61. data/lib/net/ssh/proxy/errors.rb +34 -0
  62. data/lib/net/ssh/proxy/http.rb +126 -0
  63. data/lib/net/ssh/proxy/socks4.rb +83 -0
  64. data/lib/net/ssh/proxy/socks5.rb +160 -0
  65. data/lib/net/ssh/service/forward/driver.rb +319 -0
  66. data/lib/net/ssh/service/forward/local-network-handler.rb +74 -0
  67. data/lib/net/ssh/service/forward/remote-network-handler.rb +81 -0
  68. data/lib/net/ssh/service/forward/services.rb +76 -0
  69. data/lib/net/ssh/service/process/driver.rb +153 -0
  70. data/lib/net/ssh/service/process/open.rb +193 -0
  71. data/lib/net/ssh/service/process/popen3.rb +160 -0
  72. data/lib/net/ssh/service/process/services.rb +66 -0
  73. data/lib/net/ssh/service/services.rb +44 -0
  74. data/lib/net/ssh/session.rb +242 -0
  75. data/lib/net/ssh/transport/algorithm-negotiator.rb +267 -0
  76. data/lib/net/ssh/transport/compress/compressor.rb +53 -0
  77. data/lib/net/ssh/transport/compress/decompressor.rb +53 -0
  78. data/lib/net/ssh/transport/compress/none-compressor.rb +39 -0
  79. data/lib/net/ssh/transport/compress/none-decompressor.rb +39 -0
  80. data/lib/net/ssh/transport/compress/services.rb +68 -0
  81. data/lib/net/ssh/transport/compress/zlib-compressor.rb +60 -0
  82. data/lib/net/ssh/transport/compress/zlib-decompressor.rb +52 -0
  83. data/lib/net/ssh/transport/constants.rb +66 -0
  84. data/lib/net/ssh/transport/errors.rb +47 -0
  85. data/lib/net/ssh/transport/identity-cipher.rb +61 -0
  86. data/lib/net/ssh/transport/kex/dh-gex.rb +106 -0
  87. data/lib/net/ssh/transport/kex/dh.rb +231 -0
  88. data/lib/net/ssh/transport/kex/services.rb +60 -0
  89. data/lib/net/ssh/transport/ossl/buffer-factory.rb +52 -0
  90. data/lib/net/ssh/transport/ossl/buffer.rb +87 -0
  91. data/lib/net/ssh/transport/ossl/cipher-factory.rb +98 -0
  92. data/lib/net/ssh/transport/ossl/digest-factory.rb +51 -0
  93. data/lib/net/ssh/transport/ossl/hmac-factory.rb +71 -0
  94. data/lib/net/ssh/transport/ossl/hmac/hmac.rb +62 -0
  95. data/lib/net/ssh/transport/ossl/hmac/md5-96.rb +44 -0
  96. data/lib/net/ssh/transport/ossl/hmac/md5.rb +46 -0
  97. data/lib/net/ssh/transport/ossl/hmac/none.rb +46 -0
  98. data/lib/net/ssh/transport/ossl/hmac/services.rb +68 -0
  99. data/lib/net/ssh/transport/ossl/hmac/sha1-96.rb +44 -0
  100. data/lib/net/ssh/transport/ossl/hmac/sha1.rb +45 -0
  101. data/lib/net/ssh/transport/ossl/key-factory.rb +113 -0
  102. data/lib/net/ssh/transport/ossl/services.rb +149 -0
  103. data/lib/net/ssh/transport/packet-stream.rb +210 -0
  104. data/lib/net/ssh/transport/services.rb +146 -0
  105. data/lib/net/ssh/transport/session.rb +296 -0
  106. data/lib/net/ssh/transport/version-negotiator.rb +73 -0
  107. data/lib/net/ssh/userauth/agent.rb +218 -0
  108. data/lib/net/ssh/userauth/constants.rb +35 -0
  109. data/lib/net/ssh/userauth/driver.rb +176 -0
  110. data/lib/net/ssh/userauth/methods/hostbased.rb +119 -0
  111. data/lib/net/ssh/userauth/methods/password.rb +70 -0
  112. data/lib/net/ssh/userauth/methods/publickey.rb +137 -0
  113. data/lib/net/ssh/userauth/methods/services.rb +63 -0
  114. data/lib/net/ssh/userauth/services.rb +126 -0
  115. data/lib/net/ssh/userauth/userkeys.rb +258 -0
  116. data/lib/net/ssh/util/buffer.rb +274 -0
  117. data/lib/net/ssh/util/openssl.rb +146 -0
  118. data/lib/net/ssh/util/prompter.rb +73 -0
  119. data/lib/net/ssh/version.rb +29 -0
  120. data/test/ALL-TESTS.rb +21 -0
  121. data/test/connection/tc_channel.rb +136 -0
  122. data/test/connection/tc_driver.rb +287 -0
  123. data/test/connection/tc_integration.rb +85 -0
  124. data/test/proxy/tc_http.rb +209 -0
  125. data/test/proxy/tc_socks4.rb +148 -0
  126. data/test/proxy/tc_socks5.rb +214 -0
  127. data/test/service/forward/tc_driver.rb +289 -0
  128. data/test/service/forward/tc_local_network_handler.rb +123 -0
  129. data/test/service/forward/tc_remote_network_handler.rb +108 -0
  130. data/test/service/process/tc_driver.rb +79 -0
  131. data/test/service/process/tc_integration.rb +117 -0
  132. data/test/service/process/tc_open.rb +179 -0
  133. data/test/service/process/tc_popen3.rb +164 -0
  134. data/test/tc_integration.rb +79 -0
  135. data/test/transport/compress/tc_none_compress.rb +41 -0
  136. data/test/transport/compress/tc_none_decompress.rb +45 -0
  137. data/test/transport/compress/tc_zlib_compress.rb +61 -0
  138. data/test/transport/compress/tc_zlib_decompress.rb +48 -0
  139. data/test/transport/kex/tc_dh.rb +304 -0
  140. data/test/transport/kex/tc_dh_gex.rb +70 -0
  141. data/test/transport/ossl/fixtures/dsa-encrypted +15 -0
  142. data/test/transport/ossl/fixtures/dsa-encrypted-bad +15 -0
  143. data/test/transport/ossl/fixtures/dsa-unencrypted +12 -0
  144. data/test/transport/ossl/fixtures/dsa-unencrypted-bad +12 -0
  145. data/test/transport/ossl/fixtures/dsa-unencrypted.pub +1 -0
  146. data/test/transport/ossl/fixtures/not-a-private-key +4 -0
  147. data/test/transport/ossl/fixtures/not-supported +2 -0
  148. data/test/transport/ossl/fixtures/rsa-encrypted +18 -0
  149. data/test/transport/ossl/fixtures/rsa-encrypted-bad +18 -0
  150. data/test/transport/ossl/fixtures/rsa-unencrypted +15 -0
  151. data/test/transport/ossl/fixtures/rsa-unencrypted-bad +15 -0
  152. data/test/transport/ossl/fixtures/rsa-unencrypted.pub +1 -0
  153. data/test/transport/ossl/hmac/tc_hmac.rb +58 -0
  154. data/test/transport/ossl/hmac/tc_md5.rb +50 -0
  155. data/test/transport/ossl/hmac/tc_md5_96.rb +50 -0
  156. data/test/transport/ossl/hmac/tc_none.rb +50 -0
  157. data/test/transport/ossl/hmac/tc_sha1.rb +50 -0
  158. data/test/transport/ossl/hmac/tc_sha1_96.rb +50 -0
  159. data/test/transport/ossl/tc_buffer.rb +97 -0
  160. data/test/transport/ossl/tc_buffer_factory.rb +67 -0
  161. data/test/transport/ossl/tc_cipher_factory.rb +84 -0
  162. data/test/transport/ossl/tc_digest_factory.rb +39 -0
  163. data/test/transport/ossl/tc_hmac_factory.rb +72 -0
  164. data/test/transport/ossl/tc_key_factory.rb +199 -0
  165. data/test/transport/tc_algorithm_negotiator.rb +169 -0
  166. data/test/transport/tc_identity_cipher.rb +52 -0
  167. data/test/transport/tc_integration.rb +110 -0
  168. data/test/transport/tc_packet_stream.rb +183 -0
  169. data/test/transport/tc_session.rb +283 -0
  170. data/test/transport/tc_version_negotiator.rb +86 -0
  171. data/test/userauth/methods/tc_hostbased.rb +136 -0
  172. data/test/userauth/methods/tc_password.rb +89 -0
  173. data/test/userauth/methods/tc_publickey.rb +167 -0
  174. data/test/userauth/tc_agent.rb +223 -0
  175. data/test/userauth/tc_driver.rb +190 -0
  176. data/test/userauth/tc_integration.rb +81 -0
  177. data/test/userauth/tc_userkeys.rb +265 -0
  178. data/test/util/tc_buffer.rb +217 -0
  179. metadata +256 -0
@@ -0,0 +1,283 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SSH Secure Shell Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-ssh website : http://net-ssh.rubyforge.org
13
+ # project website: http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../lib"
18
+
19
+ require 'net/ssh/transport/constants'
20
+ require 'net/ssh/transport/session'
21
+ require 'net/ssh/util/buffer'
22
+ require 'test/unit'
23
+ require 'ostruct'
24
+
25
+ class TC_Session < Test::Unit::TestCase
26
+ include Net::SSH::Transport::Constants
27
+
28
+ class Logger
29
+ attr_reader :msgs
30
+ def initialize
31
+ @msgs = []
32
+ end
33
+ def debug?; true; end
34
+ def debug(msg)
35
+ @msgs << "[D] #{msg}"
36
+ end
37
+ def info?; true; end
38
+ def info(msg)
39
+ @msgs << "[I] #{msg}"
40
+ end
41
+ def warn?; true; end
42
+ def warn(msg)
43
+ @msgs << "[W] #{msg}"
44
+ end
45
+ end
46
+
47
+ class VersionNegotiator
48
+ def negotiate( socket, version ); "A"; end
49
+ end
50
+
51
+ class AlgorithmNegotiator
52
+ def negotiate( session, options )
53
+ OpenStruct.new(
54
+ :server_packet => "A",
55
+ :client_packet => "B",
56
+ :kex => "C",
57
+ :host_key => "D",
58
+ :encryption_c2s => "E",
59
+ :encryption_s2c => "E",
60
+ :mac_c2s => "F",
61
+ :mac_s2c => "F",
62
+ :compression_c2s => "G",
63
+ :compression_s2c => "G",
64
+ :language_c2s => "",
65
+ :language_s2c => ""
66
+ )
67
+ end
68
+ end
69
+
70
+ class ScriptedSocket
71
+ attr_reader :replies
72
+
73
+ def initialize( script )
74
+ @replies = []
75
+ @script = script
76
+ end
77
+
78
+ def open( host, port )
79
+ @replies << "#{host}:#{port}"
80
+ self
81
+ end
82
+
83
+ def write( msg )
84
+ @replies << msg
85
+ end
86
+
87
+ def read
88
+ @script.shift
89
+ end
90
+ end
91
+
92
+ class PacketHandler
93
+ attr_writer :socket
94
+ def on_new_algos( &block )
95
+ @on_new_algos = block
96
+ end
97
+ def set_algorithms( *args )
98
+ @on_new_algos.call *args if @on_new_algos
99
+ end
100
+ end
101
+
102
+ class PacketSender < PacketHandler
103
+ def send( msg )
104
+ @socket.write msg
105
+ end
106
+ end
107
+
108
+ class PacketReceiver < PacketHandler
109
+ def get
110
+ @socket.read
111
+ end
112
+ end
113
+
114
+ class Ciphers
115
+ def get( *args )
116
+ args.first
117
+ end
118
+ def get_lengths( name )
119
+ [ 24, 8 ]
120
+ end
121
+ end
122
+
123
+ class HMACs
124
+ def get( *args )
125
+ args.first
126
+ end
127
+ def get_key_length( name )
128
+ 24
129
+ end
130
+ end
131
+
132
+ class Compressor
133
+ def initialize( *args )
134
+ end
135
+ end
136
+
137
+ class SSHAble; def to_ssh; ""; end; end
138
+
139
+ class Digester
140
+ def digest( text )
141
+ text
142
+ end
143
+ end
144
+
145
+ class Kex
146
+ def exchange_keys( session, info )
147
+ {
148
+ :shared_secret => SSHAble.new,
149
+ :session_id => "",
150
+ :server_key => "",
151
+ :hashing_algorithm => Digester.new
152
+ }
153
+ end
154
+ end
155
+
156
+ def self.method_added( name )
157
+ super
158
+
159
+ end
160
+
161
+ def reader(text)
162
+ Net::SSH::Util::ReaderBuffer.new( text )
163
+ end
164
+
165
+ def setup
166
+ @script = []
167
+ @logger = Logger.new
168
+ @socket = ScriptedSocket.new( @script )
169
+ @sender = PacketSender.new
170
+ @getter = PacketReceiver.new
171
+ end
172
+
173
+ def do_setup( host, opts={} )
174
+ @session = Net::SSH::Transport::Session.new( host, opts ) do |s|
175
+ s.logger = @logger
176
+ s.default_port = 22
177
+ s.version_negotiator = VersionNegotiator.new
178
+ s.algorithm_negotiator = AlgorithmNegotiator.new
179
+ s.socket_factory = @socket
180
+ s.packet_sender = @sender
181
+ s.packet_receiver = @getter
182
+ s.ciphers = Ciphers.new
183
+ s.hmacs = HMACs.new
184
+ s.kexs = { "C" => Kex.new }
185
+ s.compressors = { "G" => Compressor }
186
+ s.decompressors = { "G" => Compressor }
187
+ end
188
+ end
189
+
190
+ def test_bad_option
191
+ assert_raise( ArgumentError ) do
192
+ do_setup( "the-host", :bogus => "thing" )
193
+ end
194
+ end
195
+
196
+ def test_open
197
+ @sender.on_new_algos do |a,b,c|
198
+ assert_equal "E", a
199
+ assert_equal "F", b
200
+ assert_instance_of Compressor, c
201
+ end
202
+
203
+ @getter.on_new_algos do |a,b,c|
204
+ assert_equal "E", a
205
+ assert_equal "F", b
206
+ assert_instance_of Compressor, c
207
+ end
208
+
209
+ do_setup "the.host.com"
210
+
211
+ assert_equal [ "the.host.com:22" ], @socket.replies
212
+ end
213
+
214
+ def test_send_message
215
+ do_setup "the.host.com"
216
+ @session.send_message "sending"
217
+ assert_equal [ "the.host.com:22", "sending" ], @socket.replies
218
+ end
219
+
220
+ def test_wait_for_message
221
+ @script << reader( "\xFFhello" )
222
+ do_setup "the.host.com"
223
+ type, buffer = @session.wait_for_message
224
+ assert_equal 255, type
225
+ assert_equal "hello", buffer.remainder_as_buffer.content
226
+ end
227
+
228
+ def test_wait_for_disconnect
229
+ @script << reader( "#{DISCONNECT.chr}\0\0\0\1\0\0\0\1A\0\0\0\1B" )
230
+ do_setup "the.host.com"
231
+ assert_raise( Net::SSH::Transport::Disconnect ) do
232
+ @session.wait_for_message
233
+ end
234
+ end
235
+
236
+ def test_wait_for_ignore
237
+ @script << reader( "#{IGNORE.chr}\0\0\0\1A" )
238
+ @script << reader( "\xFFhello" )
239
+ do_setup "the.host.com"
240
+ type, buffer = @session.wait_for_message
241
+
242
+ assert_equal 255, type
243
+ assert_equal "hello", buffer.remainder_as_buffer.content
244
+
245
+ assert @logger.msgs.include?("[I] received IGNORE message (\"A\")")
246
+ end
247
+
248
+ def test_wait_for_debug_quiet
249
+ @script << reader( "#{DEBUG.chr}\0\0\0\0\1A\0\0\0\1B" )
250
+ @script << reader( "\xFFhello" )
251
+ do_setup "the.host.com"
252
+ type, buffer = @session.wait_for_message
253
+
254
+ assert_equal 255, type
255
+ assert_equal "hello", buffer.remainder_as_buffer.content
256
+
257
+ assert @logger.msgs.include?("[D] A (B)")
258
+ end
259
+
260
+ def test_wait_for_debug_verbose
261
+ @script << reader( "#{DEBUG.chr}\1\0\0\0\1A\0\0\0\1B" )
262
+ @script << reader( "\xFFhello" )
263
+ do_setup "the.host.com"
264
+ type, buffer = @session.wait_for_message
265
+
266
+ assert_equal 255, type
267
+ assert_equal "hello", buffer.remainder_as_buffer.content
268
+
269
+ assert @logger.msgs.include?("[W] A (B)")
270
+ end
271
+
272
+ def test_wait_for_kexinit
273
+ @script << reader( "#{KEXINIT.chr}\1\0\0\0\1A\0\0\0\1B" )
274
+ @script << reader( "\xFFhello" )
275
+ do_setup "the.host.com"
276
+ type, buffer = @session.wait_for_message
277
+
278
+ assert_equal 255, type
279
+ assert_equal "hello", buffer.remainder_as_buffer.content
280
+
281
+ assert @logger.msgs.include?("[I] re-key requested")
282
+ end
283
+ end
@@ -0,0 +1,86 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SSH Secure Shell Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-ssh website : http://net-ssh.rubyforge.org
13
+ # project website: http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../lib"
18
+
19
+ require 'net/ssh/transport/version-negotiator'
20
+ require 'test/unit'
21
+
22
+ class TC_VersionNegotiator < Test::Unit::TestCase
23
+
24
+ class MockLogger; def debug?; false; end; end
25
+
26
+ class ScriptedSocket
27
+ attr_reader :lines
28
+
29
+ def initialize( *script )
30
+ @lines = []
31
+ @script = script
32
+ end
33
+
34
+ def readline
35
+ @script.shift
36
+ end
37
+
38
+ def print( msg )
39
+ @lines << msg
40
+ end
41
+ end
42
+
43
+ def setup
44
+ logger = MockLogger.new
45
+ @negotiator = Net::SSH::Transport::VersionNegotiator.new( logger )
46
+ end
47
+
48
+ def test_negotiate_bad_version
49
+ socket = ScriptedSocket.new( "SSH-1.5-Bogus/Thing\n" )
50
+ assert_raise( Net::SSH::Exception ) do
51
+ @negotiator.negotiate( socket, "SSH-2.0-My/Version" )
52
+ end
53
+ end
54
+
55
+ def test_negotiate_compat_version
56
+ socket = ScriptedSocket.new( "SSH-1.99-Bogus/Thing\n" )
57
+ version = nil
58
+ assert_nothing_raised do
59
+ version = @negotiator.negotiate( socket, "SSH-2.0-My/Version" )
60
+ end
61
+ assert_equal "SSH-1.99-Bogus/Thing", version
62
+ assert_equal [ "SSH-2.0-My/Version\r\n" ], socket.lines
63
+ end
64
+
65
+ def test_negotiate_good_version
66
+ socket = ScriptedSocket.new( "SSH-2.0-Bogus/Thing\n" )
67
+ version = nil
68
+ assert_nothing_raised do
69
+ version = @negotiator.negotiate( socket, "SSH-2.0-My/Version" )
70
+ end
71
+ assert_equal "SSH-2.0-Bogus/Thing", version
72
+ assert_equal [ "SSH-2.0-My/Version\r\n" ], socket.lines
73
+ end
74
+
75
+ def test_header_lines
76
+ socket = ScriptedSocket.new( "First Line", "Second Line", "SSH-2.0-Bogus/Thing\n" )
77
+ version = nil
78
+ assert_nothing_raised do
79
+ version = @negotiator.negotiate( socket, "SSH-2.0-My/Version" )
80
+ end
81
+ assert_equal "SSH-2.0-Bogus/Thing", version
82
+ assert_equal [ "SSH-2.0-My/Version\r\n" ], socket.lines
83
+ assert_equal [ "First Line", "Second Line" ], @negotiator.header_lines
84
+ end
85
+
86
+ end
@@ -0,0 +1,136 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SSH Secure Shell Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-ssh website : http://net-ssh.rubyforge.org
13
+ # project website: http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../../lib"
18
+
19
+ require 'net/ssh/userauth/methods/hostbased'
20
+ require 'net/ssh/util/buffer'
21
+ require 'test/unit'
22
+ require 'ostruct'
23
+
24
+ class TC_Methods_HostBased < Test::Unit::TestCase
25
+
26
+ class Buffers
27
+ def writer
28
+ Net::SSH::Util::WriterBuffer.new
29
+ end
30
+ end
31
+
32
+ class Key < OpenStruct
33
+ def initialize( e, n )
34
+ super( :ssh_type => "ssh-rsa",
35
+ :e => OpenStruct.new( :to_ssh => [ e ].pack("N") ),
36
+ :n => OpenStruct.new( :to_ssh => [ n ].pack("N") ) )
37
+ end
38
+ end
39
+
40
+ class Messenger
41
+ attr_reader :data
42
+ attr_reader :messages
43
+
44
+ def initialize
45
+ @data = []
46
+ @messages = []
47
+ end
48
+
49
+ def send_message( msg )
50
+ @messages << msg.to_s
51
+ end
52
+
53
+ def wait_for_message
54
+ @data.shift
55
+ end
56
+ end
57
+
58
+ class KeyManager
59
+ attr_reader :host_identities
60
+ attr_reader :state
61
+ attr_reader :sigdata
62
+
63
+ def initialize( *identities )
64
+ @host_identities = identities.flatten
65
+ @state = :open
66
+ end
67
+
68
+ def sign( identity, data )
69
+ @sigdata = [ identity, data.to_s ]
70
+ "<signature>"
71
+ end
72
+
73
+ def finish
74
+ @state = :finished
75
+ end
76
+ end
77
+
78
+ def setup
79
+ ENV["USER"] = "test_client_user"
80
+ buffers = Buffers.new
81
+ @messenger = Messenger.new
82
+ @method = Net::SSH::UserAuth::Methods::HostBased.new( buffers )
83
+ @method.messenger = @messenger
84
+ @method.session_id = "test"
85
+ @method.hostname = "test.host"
86
+ end
87
+
88
+ def test_authenticate_no_key_manager
89
+ assert !@method.authenticate( "test", "test_user" )
90
+ assert @messenger.messages.empty?
91
+ end
92
+
93
+ def test_authenticate_no_identities
94
+ manager = KeyManager.new
95
+ assert !@method.authenticate( "test", "test_user", :key_manager => manager )
96
+ assert_equal :finished, manager.state
97
+ end
98
+
99
+ def test_authenticate_success
100
+ manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ) )
101
+ @messenger.data.concat [ OpenStruct.new( :message_type => 52 ) ]
102
+ assert @method.authenticate( "test", "test_user", :key_manager => manager )
103
+ assert_equal :finished, manager.state
104
+ assert_equal 1, @messenger.messages.length
105
+
106
+ sig = "#{50.chr}\0\0\0\11test_user\0\0\0\4test\0\0\0\11hostbased\0\0\0\7ssh-rsa" +
107
+ "\0\0\0\23\0\0\0\7ssh-rsa\1\1\1\1\2\2\2\2" +
108
+ "\0\0\0\12test.host.\0\0\0\20test_client_user"
109
+ packet1 = sig + "\0\0\0\13<signature>"
110
+ assert_equal packet1, @messenger.messages[0]
111
+
112
+ sigdata = manager.sigdata
113
+ assert_equal "\0\0\0\4test" + sig, sigdata[1]
114
+ end
115
+
116
+ def test_authenticate_fail
117
+ manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ),
118
+ Key.new( 0x03030303, 0x04040404 ) )
119
+ @messenger.data.concat [ OpenStruct.new( :message_type => 51 ),
120
+ OpenStruct.new( :message_type => 51 ) ]
121
+ assert !@method.authenticate( "test", "test_user", :key_manager => manager )
122
+ assert_equal :finished, manager.state
123
+ assert_equal 2, @messenger.messages.length
124
+ end
125
+
126
+ def test_authenticate_acceptible_identities_error
127
+ manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ) )
128
+ @messenger.data.concat [ OpenStruct.new( :message_type => 60 ),
129
+ OpenStruct.new( :message_type => 0 ) ]
130
+ assert_raise( Net::SSH::Exception ) do
131
+ @method.authenticate( "test", "test_user", :key_manager => manager )
132
+ end
133
+ assert_equal 1, @messenger.messages.length
134
+ end
135
+
136
+ end