File: tc_driver.rb

package info (click to toggle)
libnet-ssh-ruby 1.1.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,472 kB
  • ctags: 2,465
  • sloc: ruby: 10,848; makefile: 17
file content (289 lines) | stat: -rw-r--r-- 8,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#--
# =============================================================================
# Copyright (c) 2004,2005 Jamis Buck (jamis@37signals.com)
# All rights reserved.
#
# This source file is distributed as part of the Net::SSH Secure Shell Client
# library for Ruby. This file (and the library as a whole) may be used only as
# allowed by either the BSD license, or the Ruby license (or, by association
# with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
# distribution for the texts of these licenses.
# -----------------------------------------------------------------------------
# net-ssh website : http://net-ssh.rubyforge.org
# project website: http://rubyforge.org/projects/net-ssh
# =============================================================================
#++

$:.unshift "#{File.dirname(__FILE__)}/../../../lib"

require 'net/ssh/service/forward/driver'
require 'net/ssh/util/buffer'
require 'test/unit'
require 'socket'

class TC_Forward_Driver < Test::Unit::TestCase

  class Buffers
    def writer
      Net::SSH::Util::WriterBuffer.new
    end
  end

  class Log
    def debug?
      true
    end
    def debug(msg)
    end
  end

  class MockObject
    attr_reader :events
    attr_reader :blocks
    attr_reader :returns

    def initialize
      @events = []
      @blocks = []
      @returns = []
    end
    def method_missing( sym, *args, &block )
      @blocks << block
      @events << [ sym, args, !block.nil? ]
      @returns << MockObject.new
      @returns.last
    end
    def method( sym )
      @events << [ :method, [ sym ], false ]
      lambda { || }
    end
    def respond_to?( sym )
      true
    end
  end

  def setup
    @connection = MockObject.new
    @handlers = { :local => MockObject.new, :remote => MockObject.new }

    @driver = Net::SSH::Service::Forward::Driver.new( @connection,
      Buffers.new, Log.new, @handlers )
  end

  def test_initialize
    assert_equal 0, @driver.direct_channel_count
    assert_equal 0, @driver.open_direct_channel_count
    assert_equal [ [ :add_channel_open_handler, [ "forwarded-tcpip" ], true ] ],
      @connection.events
  end

  def test_direct_channel
    handler = MockObject.new
    @driver.direct_channel( 1, "remote.host", 2, handler, 3, 4, 5 )

    assert_equal 1, @driver.direct_channel_count
    assert_equal 1, @driver.open_direct_channel_count

    assert_equal 2, @connection.events.length
    assert_equal [ :open_channel, [ "direct-tcpip",
      Net::SSH::Util::WriterBuffer.new(
        "\0\0\0\xbremote.host\0\0\0\2\0\0\0\x09127.0.0.1\0\0\0\1" ) ], true ],
      @connection.events.last
    assert_equal [ [ :on_confirm_failed, [], true ] ],
      @connection.returns.last.events

    channel = MockObject.new
    @connection.blocks.last.call( channel )

    assert_equal [ [ :method, [ :on_receive ], false ],
                   [ :method, [ :on_eof ], false ],
                   [ :confirm, [ channel, 1, "remote.host", 2, 3, 4, 5 ], false ],
                   [ :process, [ channel ], false ] ], handler.events

    assert_equal [ [ :on_data, [], true ],
                   [ :on_eof, [], true ],
                   [ :on_close, [], true ] ], channel.events

    channel.blocks.last.call( channel )
    assert_equal 0, @driver.open_direct_channel_count
    assert_equal [ :on_close, [ channel ], false ], handler.events.last
  end

  def test_local_bad_parm_count
    assert_raise( ArgumentError ) do
      @driver.local 1, 2
    end
    assert_raise( ArgumentError ) do
      @driver.local 1, 2, 3, 4, 5
    end
  end

  [
    [ :local_with_3, [ 12233, "test.host", 80 ] ],
    [ :local_with_4, [ "localhost", 12233, "test.host", 80 ] ]
  ].each do |name, args|
    define_method "test_#{name}" do
      assert_equal 0, @driver.open_direct_channel_count
      assert @driver.active_locals.empty?

      @driver.local(*args)

      address = '127.0.0.1'
      address = args.shift if args.first.is_a? String
      port = args.shift

      sleep 0.1
      socket = TCPSocket.new( address, port )
      sleep 0.1

      assert_equal 1, @driver.open_direct_channel_count
      assert !@driver.active_locals.empty?

      @driver.cancel_local( port, address )
      assert @driver.active_locals.empty?
    end
  end

  [
    [ :remote_with_2, [ 80 ] ],
    [ :remote_with_zero_port, [ 0 ] ],
    [ :remote_with_3, [ 80, 'localhost' ] ]
  ].each do |name, args|
    define_method "test_#{name}_success" do
      handler = MockObject.new

      port = args[0]
      host = args.last
      host = "127.0.0.1" unless host.is_a?( String )

      assert @driver.active_remotes.empty?
      assert_nothing_raised { @driver.remote( handler, *args ) }
      assert @driver.active_remotes.empty?

      assert_equal [ :global_request,
        [ "tcpip-forward", Net::SSH::Util::WriterBuffer.new(
          "\0\0\0#{host.length.chr}#{host}\0\0\0#{port.chr}" ) ], true ],
        @connection.events.last

      port = 0300 if port == 0
      response = Net::SSH::Util::ReaderBuffer.new( "\0\0\0#{port.chr}" )
      @connection.blocks.last.call( true, response )

      assert_equal [ port ], @driver.active_remotes

      assert_equal [ [ :setup, [ port ], false ] ], handler.events
    end

    define_method "test_#{name}_fail_handled" do
      handler = MockObject.new

      port = args[0]
      host = args.last
      host = "127.0.0.1" unless host.is_a?( String )

      assert @driver.active_remotes.empty?
      assert_nothing_raised { @driver.remote( handler, *args ) }
      assert @driver.active_remotes.empty?

      @connection.blocks.last.call( false, nil )

      assert_equal [ [ :error, [ "remote port #{port} could not be forwarded to local host" ], false ] ], handler.events
    end

    define_method "test_#{name}_fail_unhandled" do
      port = args[0]
      host = args.last
      host = "127.0.0.1" unless host.is_a?( String )

      assert @driver.active_remotes.empty?
      assert_nothing_raised { @driver.remote( nil, *args ) }
      assert @driver.active_remotes.empty?

      assert_raise( Net::SSH::Exception ) do
        @connection.blocks.last.call( false, nil )
      end
    end

    define_method "test_#{name}_duplicate" do
      handler = MockObject.new
      port = args[0]
      port = 0300 if port == 0
      @driver.remote( handler, *args )
      response = Net::SSH::Util::ReaderBuffer.new( "\0\0\0#{port.chr}" )
      @connection.blocks.last.call( true, response )

      args = args.dup
      args[0] = port
      assert_raise( Net::SSH::Exception ) do
        @driver.remote( handler, *args )
      end
    end
  end

  def test_remote_to
    @driver.remote_to( 1, "test.host", 2 )
    assert_equal [ [ :call, [ 1, "test.host" ], false ] ],
      @handlers[:remote].events
  end

  def test_cancel_remote_success
    @driver.remote_to( 1, "test.host", 2 )
    @connection.blocks.last.call( true, nil )
    assert_equal 1, @driver.active_remotes.length

    @driver.cancel_remote( 2 )
    assert_equal [ :global_request, [ "cancel-tcpip-forward",
      Net::SSH::Util::WriterBuffer.new( "\0\0\0\x09127.0.0.1\0\0\0\2" ) ],
      true ], @connection.events.last

    @connection.blocks.last.call( true, nil )
    assert_equal 0, @driver.active_remotes.length
  end

  def test_cancel_remote_failure
    @driver.remote_to( 1, "test.host", 2 )
    @connection.blocks.last.call( true, nil )
    assert_equal 1, @driver.active_remotes.length

    @driver.cancel_remote( 2 )
    assert_raise( Net::SSH::Exception ) do
      @connection.blocks.last.call( false, nil )
    end
  end

  def test_do_open_channel_valid
    handler = MockObject.new
    @driver.remote( handler, 80 )
    @connection.blocks.last.call( true, nil )
    assert_equal 1, @driver.active_remotes.length

    channel = MockObject.new
    data = Net::SSH::Util::ReaderBuffer.new(
      "\0\0\0\017123.456.789.012\0\0\0\120\0\0\0\017345.678.901.234\0\0\0\2" )

    @driver.do_open_channel( @connection, channel, data )

    assert_equal [
      [ :on_open, [ channel, "123.456.789.012", 80, "345.678.901.234", 2 ], false ],
      [ :method, [ :on_receive ], false ],
      [ :method, [ :on_close ], false ],
      [ :method, [ :on_eof ], false ] ], handler.events[1..-1]

    assert_equal [
      [ :on_data, [], true ],
      [ :on_close, [], true ],
      [ :on_eof, [], true ] ], channel.events
  end

  def test_do_open_channel_invalid
    handler = MockObject.new
    channel = MockObject.new
    data = Net::SSH::Util::ReaderBuffer.new(
      "\0\0\0\017123.456.789.012\0\0\0\120\0\0\0\017345.678.901.234\0\0\0\2" )

    assert_raise( Net::SSH::Exception ) do
      @driver.do_open_channel( @connection, channel, data )
    end
  end

end