net-ssh 4.1.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.gitignore +5 -0
- data/.rubocop.yml +8 -2
- data/.rubocop_todo.yml +405 -552
- data/.travis.yml +23 -22
- data/CHANGES.txt +112 -1
- data/Gemfile +1 -7
- data/{Gemfile.norbnacl → Gemfile.noed25519} +1 -1
- data/Manifest +4 -5
- data/README.md +287 -0
- data/Rakefile +40 -29
- data/appveyor.yml +12 -6
- data/lib/net/ssh.rb +68 -32
- data/lib/net/ssh/authentication/agent.rb +234 -222
- data/lib/net/ssh/authentication/certificate.rb +175 -164
- data/lib/net/ssh/authentication/constants.rb +17 -14
- data/lib/net/ssh/authentication/ed25519.rb +162 -141
- data/lib/net/ssh/authentication/ed25519_loader.rb +32 -29
- data/lib/net/ssh/authentication/key_manager.rb +40 -9
- data/lib/net/ssh/authentication/methods/abstract.rb +53 -47
- data/lib/net/ssh/authentication/methods/hostbased.rb +32 -33
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +1 -1
- data/lib/net/ssh/authentication/methods/none.rb +10 -10
- data/lib/net/ssh/authentication/methods/password.rb +13 -13
- data/lib/net/ssh/authentication/methods/publickey.rb +56 -55
- data/lib/net/ssh/authentication/pageant.rb +468 -465
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
- data/lib/net/ssh/authentication/session.rb +130 -122
- data/lib/net/ssh/buffer.rb +345 -312
- data/lib/net/ssh/buffered_io.rb +163 -163
- data/lib/net/ssh/config.rb +316 -238
- data/lib/net/ssh/connection/channel.rb +670 -650
- data/lib/net/ssh/connection/constants.rb +30 -26
- data/lib/net/ssh/connection/event_loop.rb +108 -105
- data/lib/net/ssh/connection/keepalive.rb +54 -50
- data/lib/net/ssh/connection/session.rb +682 -671
- data/lib/net/ssh/connection/term.rb +180 -176
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +195 -108
- data/lib/net/ssh/known_hosts.rb +161 -152
- data/lib/net/ssh/loggable.rb +57 -55
- data/lib/net/ssh/packet.rb +82 -78
- data/lib/net/ssh/prompt.rb +55 -53
- data/lib/net/ssh/proxy/command.rb +104 -89
- data/lib/net/ssh/proxy/errors.rb +12 -8
- data/lib/net/ssh/proxy/http.rb +93 -91
- data/lib/net/ssh/proxy/https.rb +42 -39
- data/lib/net/ssh/proxy/jump.rb +50 -47
- data/lib/net/ssh/proxy/socks4.rb +0 -2
- data/lib/net/ssh/proxy/socks5.rb +11 -12
- data/lib/net/ssh/service/forward.rb +370 -317
- data/lib/net/ssh/test.rb +83 -77
- data/lib/net/ssh/test/channel.rb +146 -142
- data/lib/net/ssh/test/extensions.rb +150 -146
- data/lib/net/ssh/test/kex.rb +35 -31
- data/lib/net/ssh/test/local_packet.rb +48 -44
- data/lib/net/ssh/test/packet.rb +87 -84
- data/lib/net/ssh/test/remote_packet.rb +35 -31
- data/lib/net/ssh/test/script.rb +173 -171
- data/lib/net/ssh/test/socket.rb +59 -55
- data/lib/net/ssh/transport/algorithms.rb +430 -364
- data/lib/net/ssh/transport/cipher_factory.rb +95 -91
- data/lib/net/ssh/transport/constants.rb +33 -25
- data/lib/net/ssh/transport/ctr.rb +33 -11
- data/lib/net/ssh/transport/hmac.rb +15 -13
- data/lib/net/ssh/transport/hmac/abstract.rb +82 -63
- data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
- data/lib/net/ssh/transport/identity_cipher.rb +55 -51
- data/lib/net/ssh/transport/kex.rb +14 -13
- data/lib/net/ssh/transport/kex/abstract.rb +123 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +38 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +33 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +112 -217
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -62
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +36 -90
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
- data/lib/net/ssh/transport/key_expander.rb +29 -25
- data/lib/net/ssh/transport/openssl.rb +116 -116
- data/lib/net/ssh/transport/packet_stream.rb +223 -190
- data/lib/net/ssh/transport/server_version.rb +64 -66
- data/lib/net/ssh/transport/session.rb +306 -257
- data/lib/net/ssh/transport/state.rb +198 -196
- data/lib/net/ssh/verifiers/accept_new.rb +35 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +34 -0
- data/lib/net/ssh/verifiers/always.rb +56 -0
- data/lib/net/ssh/verifiers/never.rb +21 -0
- data/lib/net/ssh/version.rb +55 -53
- data/net-ssh-public_cert.pem +18 -19
- data/net-ssh.gemspec +12 -11
- data/support/ssh_tunnel_bug.rb +2 -2
- metadata +86 -75
- metadata.gz.sig +0 -0
- data/Gemfile.norbnacl.lock +0 -41
- data/README.rdoc +0 -169
- data/lib/net/ssh/ruby_compat.rb +0 -24
- data/lib/net/ssh/verifiers/lenient.rb +0 -30
- data/lib/net/ssh/verifiers/null.rb +0 -12
- data/lib/net/ssh/verifiers/secure.rb +0 -52
- data/lib/net/ssh/verifiers/strict.rb +0 -24
- data/support/arcfour_check.rb +0 -20
@@ -6,162 +6,166 @@ require 'net/ssh/connection/constants'
|
|
6
6
|
require 'net/ssh/transport/constants'
|
7
7
|
require 'net/ssh/transport/packet_stream'
|
8
8
|
|
9
|
-
module Net
|
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
|
-
# An extension to Net::SSH::Transport::PacketStream (assumes that the
|
37
|
-
# underlying IO is actually a StringIO). Facilitates unit testing.
|
38
|
-
module PacketStream
|
39
|
-
include BufferedIo # make sure we get the extensions here, too
|
40
|
-
|
41
|
-
def self.included(base) #:nodoc:
|
42
|
-
base.send :alias_method, :real_available_for_read?, :available_for_read?
|
43
|
-
base.send :alias_method, :available_for_read?, :test_available_for_read?
|
44
|
-
|
45
|
-
base.send :alias_method, :real_enqueue_packet, :enqueue_packet
|
46
|
-
base.send :alias_method, :enqueue_packet, :test_enqueue_packet
|
47
|
-
|
48
|
-
base.send :alias_method, :real_poll_next_packet, :poll_next_packet
|
49
|
-
base.send :alias_method, :poll_next_packet, :test_poll_next_packet
|
50
|
-
end
|
51
|
-
|
52
|
-
# Called when another packet should be inspected from the current
|
53
|
-
# script. If the next packet is a remote packet, it pops it off the
|
54
|
-
# script and shoves it onto this IO object, making it available to
|
55
|
-
# be read.
|
56
|
-
def idle!
|
57
|
-
return false unless script.next(:first)
|
58
|
-
|
59
|
-
if script.next(:first).remote?
|
60
|
-
self.string << script.next.to_s
|
61
|
-
self.pos = pos
|
9
|
+
module Net
|
10
|
+
module SSH
|
11
|
+
module Test
|
12
|
+
|
13
|
+
# A collection of modules used to extend/override the default behavior of
|
14
|
+
# Net::SSH internals for ease of testing. As a consumer of Net::SSH, you'll
|
15
|
+
# never need to use this directly--they're all used under the covers by
|
16
|
+
# the Net::SSH::Test system.
|
17
|
+
module Extensions
|
18
|
+
|
19
|
+
# An extension to Net::SSH::BufferedIo (assumes that the underlying IO
|
20
|
+
# is actually a StringIO). Facilitates unit testing.
|
21
|
+
module BufferedIo
|
22
|
+
# Returns +true+ if the position in the stream is less than the total
|
23
|
+
# length of the stream.
|
24
|
+
def select_for_read?
|
25
|
+
pos < size
|
26
|
+
end
|
27
|
+
|
28
|
+
# Set this to +true+ if you want the IO to pretend to be available for writing
|
29
|
+
attr_accessor :select_for_write
|
30
|
+
|
31
|
+
# Set this to +true+ if you want the IO to pretend to be in an error state
|
32
|
+
attr_accessor :select_for_error
|
33
|
+
|
34
|
+
alias select_for_write? select_for_write
|
35
|
+
alias select_for_error? select_for_error
|
62
36
|
end
|
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
|
-
def self.with_test_extension(&block)
|
119
|
-
orig_value = @extension_enabled
|
120
|
-
@extension_enabled = true
|
121
|
-
begin
|
122
|
-
yield
|
123
|
-
ensure
|
124
|
-
@extension_enabled = orig_value
|
37
|
+
|
38
|
+
# An extension to Net::SSH::Transport::PacketStream (assumes that the
|
39
|
+
# underlying IO is actually a StringIO). Facilitates unit testing.
|
40
|
+
module PacketStream
|
41
|
+
include BufferedIo # make sure we get the extensions here, too
|
42
|
+
|
43
|
+
def self.included(base) #:nodoc:
|
44
|
+
base.send :alias_method, :real_available_for_read?, :available_for_read?
|
45
|
+
base.send :alias_method, :available_for_read?, :test_available_for_read?
|
46
|
+
|
47
|
+
base.send :alias_method, :real_enqueue_packet, :enqueue_packet
|
48
|
+
base.send :alias_method, :enqueue_packet, :test_enqueue_packet
|
49
|
+
|
50
|
+
base.send :alias_method, :real_poll_next_packet, :poll_next_packet
|
51
|
+
base.send :alias_method, :poll_next_packet, :test_poll_next_packet
|
52
|
+
end
|
53
|
+
|
54
|
+
# Called when another packet should be inspected from the current
|
55
|
+
# script. If the next packet is a remote packet, it pops it off the
|
56
|
+
# script and shoves it onto this IO object, making it available to
|
57
|
+
# be read.
|
58
|
+
def idle!
|
59
|
+
return false unless script.next(:first)
|
60
|
+
|
61
|
+
if script.next(:first).remote?
|
62
|
+
self.string << script.next.to_s
|
63
|
+
self.pos = pos
|
64
|
+
end
|
65
|
+
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
|
69
|
+
# The testing version of Net::SSH::Transport::PacketStream#available_for_read?.
|
70
|
+
# Returns true if there is data pending to be read. Otherwise calls #idle!.
|
71
|
+
def test_available_for_read?
|
72
|
+
return true if select_for_read?
|
73
|
+
idle!
|
74
|
+
false
|
75
|
+
end
|
76
|
+
|
77
|
+
# The testing version of Net::SSH::Transport::PacketStream#enqueued_packet.
|
78
|
+
# Simply calls Net::SSH::Test::Script#process on the packet.
|
79
|
+
def test_enqueue_packet(payload)
|
80
|
+
packet = Net::SSH::Buffer.new(payload.to_s)
|
81
|
+
script.process(packet)
|
82
|
+
end
|
83
|
+
|
84
|
+
# The testing version of Net::SSH::Transport::PacketStream#poll_next_packet.
|
85
|
+
# Reads the next available packet from the IO object and returns it.
|
86
|
+
def test_poll_next_packet
|
87
|
+
return nil if available <= 0
|
88
|
+
packet = Net::SSH::Buffer.new(read_available(4))
|
89
|
+
length = packet.read_long
|
90
|
+
Net::SSH::Packet.new(read_available(length))
|
91
|
+
end
|
125
92
|
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
93
|
+
|
94
|
+
# An extension to Net::SSH::Connection::Channel. Facilitates unit testing.
|
95
|
+
module Channel
|
96
|
+
def self.included(base) #:nodoc:
|
97
|
+
base.send :alias_method, :send_data_for_real, :send_data
|
98
|
+
base.send :alias_method, :send_data, :send_data_for_test
|
99
|
+
end
|
100
|
+
|
101
|
+
# The testing version of Net::SSH::Connection::Channel#send_data. Calls
|
102
|
+
# the original implementation, and then immediately enqueues the data for
|
103
|
+
# output so that scripted sends are properly interpreted as discrete
|
104
|
+
# (rather than concatenated) data packets.
|
105
|
+
def send_data_for_test(data)
|
106
|
+
send_data_for_real(data)
|
107
|
+
enqueue_pending_output
|
137
108
|
end
|
138
109
|
end
|
139
|
-
|
140
|
-
#
|
141
|
-
#
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
ready_readers = Array(readers).select { |r| r.select_for_read? }
|
146
|
-
ready_writers = Array(writers).select { |r| r.select_for_write? }
|
147
|
-
ready_errors = Array(errors).select { |r| r.select_for_error? }
|
148
|
-
|
149
|
-
if ready_readers.any? || ready_writers.any? || ready_errors.any?
|
150
|
-
return [ready_readers, ready_writers, ready_errors]
|
110
|
+
|
111
|
+
# An extension to the built-in ::IO class. Simply redefines IO.select
|
112
|
+
# so that it can be scripted in Net::SSH unit tests.
|
113
|
+
module IO
|
114
|
+
def self.included(base) #:nodoc:
|
115
|
+
base.extend(ClassMethods)
|
151
116
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
117
|
+
|
118
|
+
@extension_enabled = false
|
119
|
+
|
120
|
+
def self.with_test_extension(&block)
|
121
|
+
orig_value = @extension_enabled
|
122
|
+
@extension_enabled = true
|
123
|
+
begin
|
124
|
+
yield
|
125
|
+
ensure
|
126
|
+
@extension_enabled = orig_value
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.extension_enabled?
|
131
|
+
@extension_enabled
|
132
|
+
end
|
133
|
+
|
134
|
+
module ClassMethods
|
135
|
+
def self.extended(obj) #:nodoc:
|
136
|
+
class <<obj
|
137
|
+
alias_method :select_for_real, :select
|
138
|
+
alias_method :select, :select_for_test
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# The testing version of ::IO.select. Assumes that all readers,
|
143
|
+
# writers, and errors arrays are either nil, or contain only objects
|
144
|
+
# that mix in Net::SSH::Test::Extensions::BufferedIo.
|
145
|
+
def select_for_test(readers=nil, writers=nil, errors=nil, wait=nil)
|
146
|
+
return select_for_real(readers, writers, errors, wait) unless Net::SSH::Test::Extensions::IO.extension_enabled?
|
147
|
+
ready_readers = Array(readers).select { |r| r.select_for_read? }
|
148
|
+
ready_writers = Array(writers).select { |r| r.select_for_write? }
|
149
|
+
ready_errors = Array(errors).select { |r| r.select_for_error? }
|
150
|
+
|
151
|
+
return [ready_readers, ready_writers, ready_errors] if ready_readers.any? || ready_writers.any? || ready_errors.any?
|
152
|
+
|
153
|
+
processed = 0
|
154
|
+
Array(readers).each do |reader|
|
155
|
+
processed += 1 if reader.idle!
|
156
|
+
end
|
157
|
+
|
158
|
+
raise "no readers were ready for reading, and none had any incoming packets" if processed == 0 && wait != 0
|
159
|
+
|
160
|
+
[[], [], []]
|
161
|
+
end
|
156
162
|
end
|
157
|
-
|
158
|
-
raise "no readers were ready for reading, and none had any incoming packets" if processed == 0
|
159
163
|
end
|
160
164
|
end
|
165
|
+
|
161
166
|
end
|
162
167
|
end
|
163
|
-
|
164
|
-
end; end; end
|
168
|
+
end
|
165
169
|
|
166
170
|
Net::SSH::BufferedIo.send(:include, Net::SSH::Test::Extensions::BufferedIo)
|
167
171
|
Net::SSH::Transport::PacketStream.send(:include, Net::SSH::Test::Extensions::PacketStream)
|
data/lib/net/ssh/test/kex.rb
CHANGED
@@ -5,40 +5,44 @@ require 'net/ssh/transport/algorithms'
|
|
5
5
|
require 'net/ssh/transport/constants'
|
6
6
|
require 'net/ssh/transport/kex'
|
7
7
|
|
8
|
-
module Net
|
8
|
+
module Net
|
9
|
+
module SSH
|
10
|
+
module Test
|
11
|
+
|
12
|
+
# An implementation of a key-exchange strategy specifically for unit tests.
|
13
|
+
# (This strategy would never really work against a real SSH server--it makes
|
14
|
+
# too many assumptions about the server's response.)
|
15
|
+
#
|
16
|
+
# This registers itself with the transport key-exchange system as the
|
17
|
+
# "test" algorithm.
|
18
|
+
class Kex
|
19
|
+
include Net::SSH::Transport::Constants
|
20
|
+
|
21
|
+
# Creates a new instance of the testing key-exchange algorithm with the
|
22
|
+
# given arguments.
|
23
|
+
def initialize(algorithms, connection, data)
|
24
|
+
@connection = connection
|
25
|
+
end
|
26
|
+
|
27
|
+
# Exchange keys with the server. This returns a hash of constant values,
|
28
|
+
# and does not actually exchange keys.
|
29
|
+
def exchange_keys
|
30
|
+
result = Net::SSH::Buffer.from(:byte, NEWKEYS)
|
31
|
+
@connection.send_message(result)
|
32
|
+
|
33
|
+
buffer = @connection.next_message
|
34
|
+
raise Net::SSH::Exception, "expected NEWKEYS" unless buffer.type == NEWKEYS
|
35
|
+
|
36
|
+
{ session_id: "abc-xyz",
|
37
|
+
server_key: OpenSSL::PKey::RSA.new(512),
|
38
|
+
shared_secret: OpenSSL::BN.new("1234567890", 10),
|
39
|
+
hashing_algorithm: OpenSSL::Digest::SHA1 }
|
40
|
+
end
|
41
|
+
end
|
9
42
|
|
10
|
-
# An implementation of a key-exchange strategy specifically for unit tests.
|
11
|
-
# (This strategy would never really work against a real SSH server--it makes
|
12
|
-
# too many assumptions about the server's response.)
|
13
|
-
#
|
14
|
-
# This registers itself with the transport key-exchange system as the
|
15
|
-
# "test" algorithm.
|
16
|
-
class Kex
|
17
|
-
include Net::SSH::Transport::Constants
|
18
|
-
|
19
|
-
# Creates a new instance of the testing key-exchange algorithm with the
|
20
|
-
# given arguments.
|
21
|
-
def initialize(algorithms, connection, data)
|
22
|
-
@connection = connection
|
23
|
-
end
|
24
|
-
|
25
|
-
# Exchange keys with the server. This returns a hash of constant values,
|
26
|
-
# and does not actually exchange keys.
|
27
|
-
def exchange_keys
|
28
|
-
result = Net::SSH::Buffer.from(:byte, NEWKEYS)
|
29
|
-
@connection.send_message(result)
|
30
|
-
|
31
|
-
buffer = @connection.next_message
|
32
|
-
raise Net::SSH::Exception, "expected NEWKEYS" unless buffer.type == NEWKEYS
|
33
|
-
|
34
|
-
{ session_id: "abc-xyz",
|
35
|
-
server_key: OpenSSL::PKey::RSA.new(512),
|
36
|
-
shared_secret: OpenSSL::BN.new("1234567890", 10),
|
37
|
-
hashing_algorithm: OpenSSL::Digest::SHA1 }
|
38
43
|
end
|
39
44
|
end
|
40
|
-
|
41
|
-
end; end; end
|
45
|
+
end
|
42
46
|
|
43
47
|
Net::SSH::Transport::Algorithms::ALGORITHMS[:kex] << "test"
|
44
48
|
Net::SSH::Transport::Kex::MAP["test"] = Net::SSH::Test::Kex
|
@@ -1,51 +1,55 @@
|
|
1
1
|
require 'net/ssh/packet'
|
2
2
|
require 'net/ssh/test/packet'
|
3
3
|
|
4
|
-
module Net
|
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
|
-
|
4
|
+
module Net
|
5
|
+
module SSH
|
6
|
+
module Test
|
7
|
+
|
8
|
+
# This is a specialization of Net::SSH::Test::Packet for representing mock
|
9
|
+
# packets that are sent from the local (client) host. These are created
|
10
|
+
# automatically by Net::SSH::Test::Script and Net::SSH::Test::Channel by any
|
11
|
+
# of the sends_* methods.
|
12
|
+
class LocalPacket < Packet
|
13
|
+
attr_reader :init
|
14
|
+
|
15
|
+
# Extend the default Net::SSH::Test::Packet constructor to also accept an
|
16
|
+
# optional block, which is used to finalize the initialization of the
|
17
|
+
# packet when #process is first called.
|
18
|
+
def initialize(type, *args, &block)
|
19
|
+
super(type, *args)
|
20
|
+
@init = block
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns +true+; this is a local packet.
|
24
|
+
def local?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
# Called by Net::SSH::Test::Extensions::PacketStream#test_enqueue_packet
|
29
|
+
# to mimic remote processing of a locally-sent packet. It compares the
|
30
|
+
# packet it was given with the contents of this LocalPacket's data, to see
|
31
|
+
# if what was sent matches what was scripted. If it differs in any way,
|
32
|
+
# an exception is raised.
|
33
|
+
def process(packet)
|
34
|
+
@init.call(Net::SSH::Packet.new(packet.to_s)) if @init
|
35
|
+
type = packet.read_byte
|
36
|
+
raise "expected #{@type}, but got #{type}" if @type != type
|
37
|
+
|
38
|
+
@data.zip(types).each do |expected, _type|
|
39
|
+
_type ||= case expected
|
40
|
+
when nil then break
|
41
|
+
when Numeric then :long
|
42
|
+
when String then :string
|
43
|
+
when TrueClass, FalseClass then :bool
|
44
|
+
end
|
45
|
+
|
46
|
+
actual = packet.send("read_#{_type}")
|
47
|
+
next if expected.nil?
|
48
|
+
raise "expected #{_type} #{expected.inspect} but got #{actual.inspect}" unless expected == actual
|
42
49
|
end
|
43
|
-
|
44
|
-
actual = packet.send("read_#{_type}")
|
45
|
-
next if expected.nil?
|
46
|
-
raise "expected #{_type} #{expected.inspect} but got #{actual.inspect}" unless expected == actual
|
50
|
+
end
|
47
51
|
end
|
52
|
+
|
48
53
|
end
|
49
54
|
end
|
50
|
-
|
51
|
-
end; end; end
|
55
|
+
end
|