Module: Net::SSH::Test::Extensions::PacketStream

Includes:
BufferedIo
Defined in:
lib/net/ssh/test/extensions.rb

Overview

An extension to Net::SSH::Transport::PacketStream (assumes that the underlying IO is actually a StringIO). Facilitates unit testing.

Instance Attribute Summary

Attributes included from BufferedIo

#select_for_error, #select_for_write

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BufferedIo

#select_for_read?

Class Method Details

.included(base) ⇒ Object

:nodoc:



41
42
43
44
45
46
47
48
49
50
# File 'lib/net/ssh/test/extensions.rb', line 41

def self.included(base) # :nodoc:
  base.send :alias_method, :real_available_for_read?, :available_for_read?
  base.send :alias_method, :available_for_read?, :test_available_for_read?

  base.send :alias_method, :real_enqueue_packet, :enqueue_packet
  base.send :alias_method, :enqueue_packet, :test_enqueue_packet

  base.send :alias_method, :real_poll_next_packet, :poll_next_packet
  base.send :alias_method, :poll_next_packet, :test_poll_next_packet
end

Instance Method Details

#idle!Object

Called when another packet should be inspected from the current script. If the next packet is a remote packet, it pops it off the script and shoves it onto this IO object, making it available to be read.



56
57
58
59
60
61
62
63
64
65
# File 'lib/net/ssh/test/extensions.rb', line 56

def idle!
  return false unless script.next(:first)

  if script.next(:first).remote?
    self.string << script.next.to_s
    self.pos = pos
  end

  return true
end

#test_available_for_read?Boolean

The testing version of Net::SSH::Transport::PacketStream#available_for_read?. Returns true if there is data pending to be read. Otherwise calls #idle!.

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/net/ssh/test/extensions.rb', line 69

def test_available_for_read?
  return true if select_for_read?

  idle!
  false
end

#test_enqueue_packet(payload) ⇒ Object

The testing version of Net::SSH::Transport::PacketStream#enqueued_packet. Simply calls Net::SSH::Test::Script#process on the packet.



78
79
80
81
# File 'lib/net/ssh/test/extensions.rb', line 78

def test_enqueue_packet(payload)
  packet = Net::SSH::Buffer.new(payload.to_s)
  script.process(packet)
end

#test_poll_next_packetObject

The testing version of Net::SSH::Transport::PacketStream#poll_next_packet. Reads the next available packet from the IO object and returns it.



85
86
87
88
89
90
91
# File 'lib/net/ssh/test/extensions.rb', line 85

def test_poll_next_packet
  return nil if available <= 0

  packet = Net::SSH::Buffer.new(read_available(4))
  length = packet.read_long
  Net::SSH::Packet.new(read_available(length))
end