\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/net-ssh-6.1.0/lib/net/ssh/test/

Viewing File: script.rb

require 'net/ssh/test/channel'
require 'net/ssh/test/local_packet'
require 'net/ssh/test/remote_packet'

module Net 
  module SSH 
    module Test

      # Represents a sequence of scripted events that identify the behavior that
      # a test expects. Methods named "sends_*" create events for packets being
      # sent from the local to the remote host, and methods named "gets_*" create
      # events for packets being received by the local from the remote host.
      #
      # A reference to a script. is generally obtained in a unit test via the
      # Net::SSH::Test#story helper method:
      #
      #   story do |script|
      #     channel = script.opens_channel
      #     ...
      #   end
      class Script
        # The list of scripted events. These will be Net::SSH::Test::LocalPacket
        # and Net::SSH::Test::RemotePacket instances.
        attr_reader :events
    
        # Create a new, empty script.
        def initialize
          @events = []
        end
    
        # Scripts the opening of a channel by adding a local packet sending the
        # channel open request, and if +confirm+ is true (the default), also
        # adding a remote packet confirming the new channel.
        #
        # A new Net::SSH::Test::Channel instance is returned, which can be used
        # to script additional channel operations.
        def opens_channel(confirm=true)
          channel = Channel.new(self)
          channel.remote_id = 5555
    
          events << LocalPacket.new(:channel_open) { |p| channel.local_id = p[:remote_id] }
    
          events << RemotePacket.new(:channel_open_confirmation, channel.local_id, channel.remote_id, 0x20000, 0x10000) if confirm
    
          channel
        end
    
        # A convenience method for adding an arbitrary local packet to the events
        # list.
        def sends(type, *args, &block)
          events << LocalPacket.new(type, *args, &block)
        end
    
        # A convenience method for adding an arbitrary remote packet to the events
        # list.
        def gets(type, *args)
          events << RemotePacket.new(type, *args)
        end
    
        # Scripts the sending of a new channel request packet to the remote host.
        # +channel+ should be an instance of Net::SSH::Test::Channel. +request+
        # is a string naming the request type to send, +reply+ is a boolean
        # 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. 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.
        #
        # 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)
          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)
            else
              events << RemotePacket.new(:channel_failure, channel.local_id)
            end
          end
        end
    
        # Scripts the sending of a channel data packet. +channel+ must be a
        # Net::SSH::Test::Channel object, and +data+ is the (string) data to
        # expect will be sent.
        #
        # This will typically be called via Net::SSH::Test::Channel#sends_data.
        def sends_channel_data(channel, data)
          events << LocalPacket.new(:channel_data, channel.remote_id, data)
        end
    
        # Scripts the sending of a channel EOF packet from the given
        # Net::SSH::Test::Channel +channel+. This will typically be called via
        # Net::SSH::Test::Channel#sends_eof.
        def sends_channel_eof(channel)
          events << LocalPacket.new(:channel_eof, channel.remote_id)
        end
    
        # Scripts the sending of a channel close packet from the given
        # Net::SSH::Test::Channel +channel+. This will typically be called via
        # Net::SSH::Test::Channel#sends_close.
        def sends_channel_close(channel)
          events << LocalPacket.new(:channel_close, channel.remote_id)
        end
    
        # Scripts the sending of a channel request pty packets from the given
        # Net::SSH::Test::Channel +channel+. This will typically be called via
        # 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
          events << LocalPacket.new(:channel_request, channel.remote_id, *data)
        end
    
        # Scripts the reception of a channel data packet from the remote host by
        # the given Net::SSH::Test::Channel +channel+. This will typically be
        # called via Net::SSH::Test::Channel#gets_data.
        def gets_channel_data(channel, data)
          events << RemotePacket.new(:channel_data, channel.local_id, data)
        end
    
        # Scripts the reception of a channel extended data packet from the remote
        # host by the given Net::SSH::Test::Channel +channel+. This will typically
        # be called via Net::SSH::Test::Channel#gets_extended_data.
        #
        # Currently the only extended data type is stderr == 1.
        def gets_channel_extended_data(channel, data)
          events << RemotePacket.new(:channel_extended_data, channel.local_id, 1, data)
        end
    
        # Scripts the reception of a channel request packet from the remote host by
        # the given Net::SSH::Test::Channel +channel+. This will typically be
        # called via Net::SSH::Test::Channel#gets_exit_status.
        def gets_channel_request(channel, request, reply, data)
          events << RemotePacket.new(:channel_request, channel.local_id, request, reply, data)
        end
    
        # Scripts the reception of a channel EOF packet from the remote host by
        # the given Net::SSH::Test::Channel +channel+. This will typically be
        # called via Net::SSH::Test::Channel#gets_eof.
        def gets_channel_eof(channel)
          events << RemotePacket.new(:channel_eof, channel.local_id)
        end
    
        # Scripts the reception of a channel close packet from the remote host by
        # the given Net::SSH::Test::Channel +channel+. This will typically be
        # called via Net::SSH::Test::Channel#gets_close.
        def gets_channel_close(channel)
          events << RemotePacket.new(:channel_close, channel.local_id)
        end
    
        # By default, removes the next event in the list and returns it. However,
        # this can also be used to non-destructively peek at the next event in the
        # list, by passing :first as the argument.
        #
        #   # remove the next event and return it
        #   event = script.next
        #
        #   # peek at the next event
        #   event = script.next(:first)
        def next(mode=:shift)
          events.send(mode)
        end
    
        # Compare the given packet against the next event in the list. If there is
        # no next event, an exception will be raised. This is called by
        # Net::SSH::Test::Extensions::PacketStream#test_enqueue_packet.
        def process(packet)
          event = events.shift or raise "end of script reached, but got a packet type #{packet.read_byte}"
          event.process(packet)
        end
      end

    end
  end
end