diff options
author | Miklos Fazekas <mfazekas@szemafor.com> | 2016-04-08 21:16:56 +0200 |
---|---|---|
committer | Miklos Fazekas <mfazekas@szemafor.com> | 2016-04-08 22:40:06 +0200 |
commit | 5834936ffd1a4c1bc24673c94b73a9fdaa3f710b (patch) | |
tree | 492ae2f2ce6e896753d63da29016fdc26afd7032 | |
parent | 035f27c705aed4fbfd9bc66b3f8e99bbc4141cb3 (diff) | |
download | net-ssh-5834936ffd1a4c1bc24673c94b73a9fdaa3f710b.tar.gz |
Added tests for Net::SSH::Test
-rw-r--r-- | .rubocop_todo.yml | 5 | ||||
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | Rakefile | 10 | ||||
-rw-r--r-- | lib/net/ssh/test/packet.rb | 21 | ||||
-rw-r--r-- | lib/net/ssh/test/script.rb | 11 | ||||
-rw-r--r-- | test/test/test_test.rb | 76 | ||||
-rw-r--r-- | test/test_all.rb | 3 |
7 files changed, 118 insertions, 9 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2913e69..45b48d2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1046,8 +1046,11 @@ Style/StringLiteralsInInterpolation: # Offense count: 17 # Cop supports --auto-correct. # Configuration parameters: IgnoredMethods. -# IgnoredMethods: respond_to +# IgnoredMethods: respond_to, on_close Style/SymbolProc: + IgnoredMethods: + - respond_to + - on_close Exclude: - 'lib/net/ssh/authentication/session.rb' - 'lib/net/ssh/buffer.rb' diff --git a/.travis.yml b/.travis.yml index e035f14..34171f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,7 @@ install: script: - bundle exec rake test + - bundle exec rake test_test - bundle exec rubocop @@ -75,5 +75,15 @@ Rake::TestTask.new do |t| t.libs << "test/integration" if ENV['NET_SSH_RUN_INTEGRATION_TESTS'] test_files = FileList['test/**/test_*.rb'] test_files -= FileList['test/integration/**/test_*.rb'] unless ENV['NET_SSH_RUN_INTEGRATION_TESTS'] + test_files -= FileList['test/test/**/test_*.rb'] + t.test_files = test_files +end + +desc "Run tests of Net::SSH:Test" +Rake::TestTask.new do |t| + t.name = "test_test" + # we need to run test/test separatedly as it hacks io + other modules + t.libs = ["lib", "test"] + test_files = FileList['test/test/**/test_*.rb'] t.test_files = test_files end diff --git a/lib/net/ssh/test/packet.rb b/lib/net/ssh/test/packet.rb index 8767253..ca3ff2b 100644 --- a/lib/net/ssh/test/packet.rb +++ b/lib/net/ssh/test/packet.rb @@ -17,6 +17,17 @@ module Net; module SSH; module Test include Net::SSH::Transport::Constants include Net::SSH::Connection::Constants + # Register a custom channel request. extra_parts is an array of types + # of extra parameters + def self.register_channel_request(request, extra_parts) + @registered_requests ||= {} + @registered_requests[request] = {extra_parts: extra_parts} + end + + def self.registered_channel_requests(request) + @registered_requests && @registered_requests[request] + end + # Ceate a new packet of the given +type+, and with +args+ being a list of # data elements in the order expected for packets of the given +type+ # (see #types). @@ -70,10 +81,14 @@ module Net; module SSH; module Test when CHANNEL_REQUEST parts = [:long, :string, :bool] case @data[1] - when "exec", "subsystem" then parts << :string + when "exec", "subsystem","shell" then parts << :string when "exit-status" then parts << :long - when "pty-req" then parts += [:string, :long, :long, :long, :long, :string] - else raise "don't know what to do about #{@data[1]} channel request" + when "pty-req" then parts.concat([:string, :long, :long, :long, :long, :string]) + when "env" then parts.contact([:string,:string]) + else + request = Packet.registered_channel_requests(@data[1]) + raise "don't know what to do about #{@data[1]} channel request" unless request + parts.concat(request[:extra_parts]) end else raise "don't know how to parse packet type #{@type}" end diff --git a/lib/net/ssh/test/script.rb b/lib/net/ssh/test/script.rb index d3813c3..9014c7d 100644 --- a/lib/net/ssh/test/script.rb +++ b/lib/net/ssh/test/script.rb @@ -63,7 +63,8 @@ module Net; module SSH; module Test # indicating whether a response to this packet is required , and +data+ # is any additional request-specific data that this packet should send. # +success+ indicates whether the response (if one is required) should be - # success or failure. + # success or failure. If +data+ is an array it will be treated as multiple + # data. # # If a reply is desired, a remote packet will also be queued, :channel_success # if +success+ is true, or :channel_failure if +success+ is false. @@ -71,7 +72,11 @@ module Net; module SSH; module Test # This will typically be called via Net::SSH::Test::Channel#sends_exec or # Net::SSH::Test::Channel#sends_subsystem. def sends_channel_request(channel, request, reply, data, success=true) - events << LocalPacket.new(:channel_request, channel.remote_id, request, reply, data) + if data.is_a? Array + events << LocalPacket.new(:channel_request, channel.remote_id, request, reply, *data) + else + events << LocalPacket.new(:channel_request, channel.remote_id, request, reply, data) + end if reply if success events << RemotePacket.new(:channel_success, channel.local_id) @@ -109,7 +114,7 @@ module Net; module SSH; module Test # Net::SSH::Test::Channel#sends_request_pty. def sends_channel_request_pty(channel) data = ['pty-req', false] - data += Net::SSH::Connection::Channel::VALID_PTY_OPTIONS.merge(:modes => "\0").values + data += Net::SSH::Connection::Channel::VALID_PTY_OPTIONS.merge(modes: "\0").values events << LocalPacket.new(:channel_request, channel.remote_id, *data) end diff --git a/test/test/test_test.rb b/test/test/test_test.rb new file mode 100644 index 0000000..20a0e4f --- /dev/null +++ b/test/test/test_test.rb @@ -0,0 +1,76 @@ +require_relative '../common' +require 'net/ssh/test' + +class TestNetSSHTest < NetSSHTest + include Net::SSH::Test + + def test_example + story do |session| + channel = session.opens_channel + channel.sends_exec "ls" + channel.gets_data "result of ls" + channel.gets_close + channel.sends_close + end + + assert_scripted do + result = nil + + connection.open_channel do |ch| + ch.exec("ls") do |_success| + ch.on_data { |_c, data| result = data } + ch.on_close { |c| c.close } + end + end + + connection.loop + assert_equal "result of ls", result + end + end + + def test_pty + story do |session| + channel = session.opens_channel + channel.sends_request_pty + session.sends_channel_request(channel, "shell", false, nil, true) + channel.sends_exec "ls" + channel.gets_data "result of ls" + channel.gets_close + channel.sends_close + end + + assert_scripted do + result = nil + connection.open_channel do |ch| + ch.request_pty + ch.send_channel_request("shell") + ch.exec("ls") do |_success| + ch.on_data { |_c, data| result = data } + ch.on_close { |c| c.close } + end + end + + connection.loop + assert_equal "result of ls", result + end + end + + def test_custom + Packet.register_channel_request("custom", [:string, :string, :long]) + story do |session| + channel = session.opens_channel + session.sends_channel_request(channel, "custom", false, ["hello", "hello", 42], true) + channel.gets_close + channel.sends_close + end + + assert_scripted do + connection.open_channel do |ch| + ch.send_channel_request("custom", :string, "hello", :string, "hello", :long, 42) + ch.on_close { |c| c.close } + end + + connection.loop + end + end +end
\ No newline at end of file diff --git a/test/test_all.rb b/test/test_all.rb index 3958b3e..5f65eec 100644 --- a/test/test_all.rb +++ b/test/test_all.rb @@ -1,10 +1,9 @@ $: << '.' -# $ ruby -Ilib -Itest -rrubygems test/test_all.rb -# $ ruby -Ilib -Itest -rrubygems test/transport/test_server_version.rb Dir.chdir(File.dirname(__FILE__)) do test_files = Dir['**/test_*.rb']-['test_all.rb'] # prevent circular require test_files -= Dir['integration/test_*.rb'] unless ENV['NET_SSH_RUN_INTEGRATION_TESTS'] + test_files -= Dir['test/test_*.rb'] test_files = test_files.reject { |f| f =~ /^manual/ } test_files = test_files.select { |f| f =~ Regexp.new(ENV['ONLY']) } if ENV['ONLY'] test_files = test_files.reject { |f| f =~ Regexp.new(ENV['EXCEPT']) } if ENV['EXCEPT'] |