Sha256: 00edd2575ef196e03c76c2dbdac018225a81f624bb7fae51b7f52b392de8ea7a

Contents?: true

Size: 1.91 KB

Versions: 28

Compression:

Stored size: 1.91 KB

Contents

require 'net/ssh/packet'
require 'net/ssh/test/packet'

module Net
  module SSH
    module Test
      # This is a specialization of Net::SSH::Test::Packet for representing mock
      # packets that are sent from the local (client) host. These are created
      # automatically by Net::SSH::Test::Script and Net::SSH::Test::Channel by any
      # of the sends_* methods.
      class LocalPacket < Packet
        attr_reader :init

        # Extend the default Net::SSH::Test::Packet constructor to also accept an
        # optional block, which is used to finalize the initialization of the
        # packet when #process is first called.
        def initialize(type, *args, &block)
          super(type, *args)
          @init = block
        end

        # Returns +true+; this is a local packet.
        def local?
          true
        end

        # Called by Net::SSH::Test::Extensions::PacketStream#test_enqueue_packet
        # to mimic remote processing of a locally-sent packet. It compares the
        # packet it was given with the contents of this LocalPacket's data, to see
        # if what was sent matches what was scripted. If it differs in any way,
        # an exception is raised.
        def process(packet)
          @init.call(Net::SSH::Packet.new(packet.to_s)) if @init
          type = packet.read_byte
          raise "expected #{@type}, but got #{type}" if @type != type

          @data.zip(types).each do |expected, _type|
            _type ||= case expected
                      when nil then break
                      when Numeric then :long
                      when String then :string
                      when TrueClass, FalseClass then :bool
                      end

            actual = packet.send("read_#{_type}")
            next if expected.nil?
            raise "expected #{_type} #{expected.inspect} but got #{actual.inspect}" unless expected == actual
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 6 rubygems

Version Path
net-ssh-7.3.0 lib/net/ssh/test/local_packet.rb
net-ssh-7.3.0.rc1 lib/net/ssh/test/local_packet.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.1/lib/net/ssh/test/local_packet.rb
net-ssh-7.2.3 lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.6.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.5.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.4.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.3.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.2.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.1.backports lib/net/ssh/test/local_packet.rb
net-ssh-backports-6.3.0.backports lib/net/ssh/test/local_packet.rb
net-ssh-7.2.2.rc1 lib/net/ssh/test/local_packet.rb
net-ssh-7.2.1 lib/net/ssh/test/local_packet.rb
net-ssh-7.2.1.rc1 lib/net/ssh/test/local_packet.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/test/local_packet.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/test/local_packet.rb
net-ssh-7.2.0 lib/net/ssh/test/local_packet.rb
net-ssh-7.2.0.rc1 lib/net/ssh/test/local_packet.rb
net-ssh-7.2.0.beta1 lib/net/ssh/test/local_packet.rb
net-ssh-7.1.0 lib/net/ssh/test/local_packet.rb