blob: 4637cf0adee877d31b4f18aa21b91aeacd36c8d7 (
plain)
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
|
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
gem "test-unit" # http://rubyforge.org/pipermail/test-unit-tracker/2009-July/000075.html
require 'test/unit'
require 'mocha'
require 'net/ssh/buffer'
require 'net/ssh/config'
require 'net/ssh/loggable'
require 'net/ssh/packet'
require 'net/ssh/transport/session'
require 'ostruct'
# clear the default files out so that tests don't get confused by existing
# SSH config files.
$original_config_default_files = Net::SSH::Config.default_files.dup
Net::SSH::Config.default_files.clear
def P(*args)
Net::SSH::Packet.new(Net::SSH::Buffer.from(*args))
end
class MockTransport < Net::SSH::Transport::Session
class BlockVerifier
def initialize(block)
@block = block
end
def verify(data)
@block.call(data)
end
end
attr_reader :host_key_verifier
attr_accessor :host_as_string
attr_accessor :server_version
attr_reader :client_options
attr_reader :server_options
attr_reader :hints, :queue
attr_accessor :mock_enqueue
def initialize(options={})
self.logger = options[:logger]
self.host_as_string = "net.ssh.test,127.0.0.1"
self.server_version = OpenStruct.new(:version => "SSH-2.0-Ruby/Net::SSH::Test")
@expectation = nil
@queue = []
@hints = {}
@socket = options[:socket]
@algorithms = OpenStruct.new(:session_id => "abcxyz123")
verifier { |data| true }
end
def send_message(message)
buffer = Net::SSH::Buffer.new(message.to_s)
if @expectation.nil?
raise "got #{message.to_s.inspect} but was not expecting anything"
else
block, @expectation = @expectation, nil
block.call(self, Net::SSH::Packet.new(buffer))
end
end
def enqueue_message(message)
if mock_enqueue
send_message(message)
else
super
end
end
def poll_message
@queue.shift
end
def next_message
@queue.shift or raise "expected a message from the server but nothing was ready to send"
end
def return(type, *args)
@queue << P(:byte, type, *args)
end
def expect(&block)
@expectation = block
end
def expect!
expect {}
end
def verifier(&block)
@host_key_verifier = BlockVerifier.new(block)
end
def configure_client(options)
@client_options = options
end
def configure_server(options)
@server_options = options
end
def hint(name, value=true)
@hints[name] = value
end
end
|