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
|
#--
# =============================================================================
# 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 'test/unit'
require 'net/ssh/connection/driver'
require 'net/ssh/errors'
require 'net/ssh/util/buffer'
class TC_Driver < Test::Unit::TestCase
class Buffers
def writer
Net::SSH::Util::WriterBuffer.new
end
end
class Log
attr_reader :messages
def initialize
@messages = []
end
def debug?
true
end
def debug( msg )
@messages << msg
end
end
class Session
attr_reader :events
attr_reader :script
def initialize
@events = []
@script = []
end
def send_message( msg )
@events << msg.to_s
end
def wait_for_message
@script.shift
end
end
class Channel
attr_reader :local_id
attr_reader :remote_id
attr_reader :data
attr_reader :window_size
attr_reader :packet_size
attr_reader :type
attr_reader :block
attr_reader :events
def self.open( type, data )
new( type, data, nil, nil, nil )
end
def self.create( type, remote_id, window_size, packet_size )
new( type, nil, remote_id, window_size, packet_size )
end
def initialize( type, data, remote_id, winsize, packsize )
@type = type
@data = data
@remote_id = remote_id
@window_size = winsize
@packet_size = packsize
@local_id = 1
@events = []
end
def close( value )
@events << [ :close, value ]
end
def on_confirm_open( &block )
@block = block
end
def self.event( name )
define_method( "do_#{name}".to_sym ) do |*args|
@events << [ name, *args ]
end
end
event :confirm_failed
event :confirm_open
event :window_adjust
event :data
event :extended_data
event :eof
event :request
event :success
event :failure
end
def reader( text )
Net::SSH::Util::ReaderBuffer.new( text )
end
def setup
@session = Session.new
@log = Log.new
@driver = Net::SSH::Connection::Driver.new( @session, @log,
Buffers.new,
:open => proc { |t,d| Channel.open(t,d) },
:create => proc { |t,r,w,p| Channel.create(t,r,w,p) } )
end
def test_open_channel
channel = @driver.open_channel( "test", "data" ) { |a| a }
assert_equal "hello", channel.block.call( "hello" )
assert_equal "test", channel.type
assert_equal "data", channel.data
assert_equal [ channel ], @driver.channels
end
def test_remove_channel
channel = @driver.open_channel( "test", "data" ) { |a| a }
assert_equal [channel], @driver.channels
@driver.remove_channel( channel )
assert_equal [], @driver.channels
end
def test_allocate_channel_id
assert_equal 1, @driver.allocate_channel_id
10.times { @driver.allocate_channel_id }
assert_equal 12, @driver.allocate_channel_id
end
def test_global_request
@driver.global_request "test", "foobar"
assert_equal [ "\120\0\0\0\4test\1foobar" ], @session.events
end
def test_request_success
@driver.global_request( "test", "foobar" ) do |a,b|
assert a
assert_equal "response", b
end
@driver.do_request_success "response"
end
def test_request_failure
@driver.global_request( "test", "foobar" ) do |a,b|
assert !a
assert_equal "response", b
end
@driver.do_request_failure "response"
end
def test_channel_open_no_handler
assert_raise( Net::SSH::Exception ) do
@driver.do_channel_open(
reader( "\0\0\0\4test\0\0\0\4\0\0\0\0\0\0\0\1" ) )
end
end
def test_channel_open_with_handler
@driver.add_channel_open_handler( "test" ) do |conn,chan,resp|
assert_equal @driver, conn
assert_equal "test", chan.type
assert_equal 4, chan.remote_id
assert_equal 0, chan.window_size
assert_equal 1, chan.packet_size
assert_equal "hello world", resp.read
end
@driver.do_channel_open(
reader( "\0\0\0\4test\0\0\0\4\0\0\0\0\0\0\0\1hello world" ) )
assert_equal 1, @driver.channels.length
assert_equal [ "\133\0\0\0\4\0\0\0\1\x7f\xff\xff\xff\x7f\xff\xff\xff" ],
@session.events
end
def test_channel_open_failure
channel = @driver.open_channel( "test", "data" )
assert_equal 1, @driver.channels.length
@driver.do_channel_open_failure(
reader( "\0\0\0\1\0\0\0\2\0\0\0\4test\0\0\0\2en" ) )
assert_equal 0, @driver.channels.length
assert_equal [ [ :confirm_failed, 2, "test", "en" ] ], channel.events
end
def test_channel_open_confirmation
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_open_confirmation(
reader( "\0\0\0\1\0\0\0\2\0\0\0\3\0\0\0\4" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :confirm_open, 2, 3, 4 ] ], channel.events
end
def test_channel_window_adjust
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_window_adjust( reader( "\0\0\0\1\0\0\0\12" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :window_adjust, 10 ] ], channel.events
end
def test_channel_data
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_data( reader( "\0\0\0\1\0\0\0\4test" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :data, "test" ] ], channel.events
end
def test_channel_extended_data
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_extended_data( reader( "\0\0\0\1\0\0\0\2\0\0\0\4test" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :extended_data, 2, "test" ] ], channel.events
end
def test_channel_eof
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_eof( reader( "\0\0\0\1" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :eof ] ], channel.events
end
def test_channel_close
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_close( reader( "\0\0\0\1" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :close, false ] ], channel.events
end
def test_channel_request
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_request( reader( "\0\0\0\1\0\0\0\4test\1foobarbaz" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :request, "test", true, reader("foobarbaz") ] ],
channel.events
end
def test_channel_success
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_success( reader( "\0\0\0\1" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :success ] ], channel.events
end
def test_channel_failure
channel = @driver.open_channel( "test", "data" )
@driver.do_channel_failure( reader( "\0\0\0\1" ) )
assert_equal 1, @driver.channels.length
assert_equal [ [ :failure ] ], channel.events
end
ProcessItem = Struct.new( :name, :script, :events )
def self.i( name, script, events )
ProcessItem.new( name, script, events )
end
def test_process_bad
@session.script << [ 0, reader("") ]
assert_raise( Net::SSH::Exception ) do
@driver.process
end
end
end
|