Class: Net::SSH::Test::Packet

Inherits:
Object
  • Object
show all
Includes:
Connection::Constants, Net::SSH::Transport::Constants
Defined in:
lib/net/ssh/test/packet.rb

Overview

This is an abstract class, not to be instantiated directly, subclassed by Net::SSH::Test::LocalPacket and Net::SSH::Test::RemotePacket. It implements functionality common to those subclasses.

These packets are not true packets, in that they don’t represent what was actually sent between the hosst; rather, they represent what was expected to be sent, as dictated by the script (Net::SSH::Test::Script). Thus, though they are defined with data elements, these data elements are used to either validate data that was sent by the local host (Net::SSH::Test::LocalPacket) or to mimic the sending of data by the remote host (Net::SSH::Test::RemotePacket).

Direct Known Subclasses

LocalPacket, RemotePacket

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_INIT, Net::SSH::Transport::Constants::KEXDH_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 Method Summary collapse

Constructor Details

#initialize(type, *args) ⇒ Packet

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).



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

def initialize(type, *args)
  @type = self.class.const_get(type.to_s.upcase)
  @data = args
end

Instance Method Details

#instantiate!Object

Instantiates the packets data elements. When the packet was first defined, some elements may not have been fully realized, and were described as Proc objects rather than atomic types. This invokes those Proc objects and replaces them with their returned values. This allows for values like Net::SSH::Test::Channel#remote_id to be used in scripts before the remote_id is known (since it is only known after a channel has been confirmed open).



45
46
47
# File 'lib/net/ssh/test/packet.rb', line 45

def instantiate!
  @data.map! { |i| i.respond_to?(:call) ? i.call : i }
end

#local?Boolean

The default for local? is false. Subclasses should override as necessary.

Returns:

  • (Boolean)


34
35
36
# File 'lib/net/ssh/test/packet.rb', line 34

def local?
  false
end

#remote?Boolean

The default for remote? is false. Subclasses should override as necessary.

Returns:

  • (Boolean)


29
30
31
# File 'lib/net/ssh/test/packet.rb', line 29

def remote?
  false
end

#typesObject

Returns an array of symbols describing the data elements for packets of the same type as this packet. These types are used to either validate sent packets (Net::SSH::Test::LocalPacket) or build received packets (Net::SSH::Test::RemotePacket).

Not all packet types are defined here. As new packet types are required (e.g., a unit test needs to test that the remote host sent a packet that is not implemented here), the description of that packet should be added. Unsupported packet types will otherwise raise an exception.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/net/ssh/test/packet.rb', line 58

def types
  @types ||= case @type
    when KEXINIT then 
      [:long, :long, :long, :long,
       :string, :string, :string, :string, :string, :string, :string, :string, :string, :string,
       :bool]
    when NEWKEYS then []
    when CHANNEL_OPEN then [:string, :long, :long, :long]
    when CHANNEL_OPEN_CONFIRMATION then [:long, :long, :long, :long]
    when CHANNEL_DATA then [:long, :string]
    when CHANNEL_EOF, CHANNEL_CLOSE, CHANNEL_SUCCESS, CHANNEL_FAILURE then [:long]
    when CHANNEL_REQUEST
      parts = [:long, :string, :bool]
      case @data[1]
      when "exec", "subsystem" then parts << :string
      when "exit-status" then parts << :long
      else raise "don't know what to do about #{@data[1]} channel request"
      end
    else raise "don't know how to parse packet type #{@type}"
    end
end