Class: Net::SSH::Test::LocalPacket

Inherits:
Packet
  • Object
show all
Defined in:
lib/net/ssh/test/local_packet.rb

Overview

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.

Constant Summary

Constants included from Connection::Constants

Connection::Constants::CHANNEL_CLOSE, Connection::Constants::CHANNEL_DATA, Connection::Constants::CHANNEL_EOF, Connection::Constants::CHANNEL_EXTENDED_DATA, Connection::Constants::CHANNEL_FAILURE, Connection::Constants::CHANNEL_OPEN, Connection::Constants::CHANNEL_OPEN_CONFIRMATION, Connection::Constants::CHANNEL_OPEN_FAILURE, Connection::Constants::CHANNEL_REQUEST, Connection::Constants::CHANNEL_SUCCESS, Connection::Constants::CHANNEL_WINDOW_ADJUST, Connection::Constants::GLOBAL_REQUEST, Connection::Constants::REQUEST_FAILURE, Connection::Constants::REQUEST_SUCCESS

Constants included from Net::SSH::Transport::Constants

Net::SSH::Transport::Constants::DEBUG, Net::SSH::Transport::Constants::DISCONNECT, Net::SSH::Transport::Constants::IGNORE, Net::SSH::Transport::Constants::KEXDH_GEX_GROUP, Net::SSH::Transport::Constants::KEXDH_GEX_INIT, Net::SSH::Transport::Constants::KEXDH_GEX_REPLY, Net::SSH::Transport::Constants::KEXDH_GEX_REQUEST, Net::SSH::Transport::Constants::KEXDH_INIT, Net::SSH::Transport::Constants::KEXDH_REPLY, Net::SSH::Transport::Constants::KEXECDH_INIT, Net::SSH::Transport::Constants::KEXECDH_REPLY, Net::SSH::Transport::Constants::KEXINIT, Net::SSH::Transport::Constants::NEWKEYS, Net::SSH::Transport::Constants::SERVICE_ACCEPT, Net::SSH::Transport::Constants::SERVICE_REQUEST, Net::SSH::Transport::Constants::UNIMPLEMENTED

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packet

#instantiate!, register_channel_request, registered_channel_requests, #remote?, #types

Constructor Details

#initialize(type, *args, &block) ⇒ LocalPacket

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.



17
18
19
20
# File 'lib/net/ssh/test/local_packet.rb', line 17

def initialize(type, *args, &block)
  super(type, *args)
  @init = block
end

Instance Attribute Details

#initObject (readonly)

Returns the value of attribute init.



12
13
14
# File 'lib/net/ssh/test/local_packet.rb', line 12

def init
  @init
end

Instance Method Details

#local?Boolean

Returns true; this is a local packet.

Returns:

  • (Boolean)


23
24
25
# File 'lib/net/ssh/test/local_packet.rb', line 23

def local?
  true
end

#process(packet) ⇒ Object

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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/net/ssh/test/local_packet.rb', line 32

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