Skip to content

Commit

Permalink
Introduce InstanceInspector
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 12, 2016
1 parent f7bd93c commit d0dd84c
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 65 deletions.
14 changes: 7 additions & 7 deletions features/ethernet_header.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: EthernetHeader
| class | Pio::EthernetHeader |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type.to_hex | 0x08, 0x00 |
| ether_type.to_hex | 0x800 |

Scenario: create a VLAN-tagged Ethernet header
When I create a packet with:
Expand All @@ -32,7 +32,7 @@ Feature: EthernetHeader
| class | Pio::EthernetHeader |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type.to_hex | 0x81, 0x00 |
| ether_type.to_hex | 0x8100 |
| vlan_pcp | 5 |
| vlan_cfi | 0 |
| vlan_vid | 100 |
Expand All @@ -48,7 +48,7 @@ Feature: EthernetHeader
| class | Pio::EthernetHeader |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type.to_hex | 0x08, 0x00 |
| ether_type.to_hex | 0x800 |

Scenario: read a VLAN-tagged Ethernet header
Given I use the fixture "ethernet_header"
Expand All @@ -61,7 +61,7 @@ Feature: EthernetHeader
| class | Pio::EthernetHeader |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:26:82:eb:ea:d1 |
| ether_type.to_hex | 0x81, 0x00 |
| ether_type.to_hex | 0x8100 |
| vlan_pcp | 5 |
| vlan_cfi | 0 |
| vlan_vid | 100 |
Expand Down Expand Up @@ -118,7 +118,7 @@ Feature: EthernetHeader
"""
Then the result of eval should be:
"""
#<EthernetHeader destination_mac: "ff:ff:ff:ff:ff:ff", source_mac: "00:26:82:eb:ea:d1", ether_type: 0x0800>
#<Pio::EthernetHeader destination_mac: "ff:ff:ff:ff:ff:ff", source_mac: "00:26:82:eb:ea:d1", ether_type: 0x0800>
"""

Scenario: VLAN-tagged EthernetHeader instance inspection
Expand All @@ -135,7 +135,7 @@ Feature: EthernetHeader
"""
Then the result of eval should be:
"""
#<EthernetHeader destination_mac: "ff:ff:ff:ff:ff:ff", source_mac: "00:26:82:eb:ea:d1", ether_type: 0x8100, vlan_pcp: 5, vlan_cfi: 0, vlan_vid: 100>
#<Pio::EthernetHeader destination_mac: "ff:ff:ff:ff:ff:ff", source_mac: "00:26:82:eb:ea:d1", ether_type: 0x8100, vlan_pcp: 5, vlan_cfi: 0, vlan_vid: 100, ether_type_vlan: 0x8100>
"""

Scenario: EthernetHeader class inspection
Expand All @@ -145,5 +145,5 @@ Feature: EthernetHeader
"""
Then the result of eval should be:
"""
Pio::EthernetHeader(destination_mac: mac_address, source_mac: mac_address, ether_type: uint16, vlan_pcp: bit3, vlan_cfi: bit1, vlan_vid: bit12, ether_type_vlan: uint16)
Pio::EthernetHeader(destination_mac: mac_address, source_mac: mac_address, ether_type: ether_type, vlan_pcp: bit3, vlan_cfi: bit1, vlan_vid: bit12, ether_type_vlan: uint16)
"""
4 changes: 2 additions & 2 deletions features/open_flow/header.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Feature: OpenFlow::Header
transaction_id: 0xff)
"""
Then the message has the following fields and values:
| field | value |
| to_hex | 0x01, 0x0a, 0x00, 0x12, 0x00, 0x00, 0x00, 0xff |
| field | value |
| to_bytes | 0x01, 0x0a, 0x00, 0x12, 0x00, 0x00, 0x00, 0xff |
8 changes: 4 additions & 4 deletions features/open_flow10/vendor_action.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: VendorAction
Pio::OpenFlow10::VendorAction.new(1)
"""
Then the action has the following fields and values:
| field | value |
| action_type.to_hex | 0xff, 0xff |
| length | 8 |
| vendor | 1 |
| field | value |
| action_type.to_hex | 0xffff |
| length | 8 |
| vendor | 1 |
10 changes: 5 additions & 5 deletions features/open_flow13/nicira_send_out_port.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Feature: NiciraSendOutPort
Pio::NiciraSendOutPort.new(:reg0)
"""
Then the action has the following fields and values:
| field | value |
| offset | 0 |
| n_bits | 32 |
| source | :reg0 |
| max_length.to_hex | 0xff, 0xff |
| field | value |
| offset | 0 |
| n_bits | 32 |
| source | :reg0 |
| max_length.to_hex | 0xffff |

Scenario: new(:reg0, offset: 16, n_bits: 16, max_length: 256)
When I create an OpenFlow action with:
Expand Down
2 changes: 1 addition & 1 deletion features/udp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Udp
| class | Pio::Udp |
| destination_mac | ff:ff:ff:ff:ff:ff |
| source_mac | 00:0b:82:01:fc:42 |
| ether_type | 2048 |
| ether_type.to_hex | 0x800 |
| ip_version | 4 |
| ip_header_length | 5 |
| ip_type_of_service | 0 |
Expand Down
2 changes: 1 addition & 1 deletion lib/pio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
require 'pio/icmp'
require 'pio/lldp'
require 'pio/mac'
require 'pio/open_flow'
require 'pio/udp'
require 'pio/open_flow'
2 changes: 1 addition & 1 deletion lib/pio/arp/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Request < Message

# rubocop:disable LineLength
def inspect
%(#<Arp::Request destination_mac: "#{destination_mac}", source_mac: "#{source_mac}", ether_type: #{format '0x%04x', ether_type}, hardware_type: #{hardware_type}, protocol_length: #{protocol_length}, operation: #{operation}, sender_hardware_address: "#{sender_hardware_address}", sender_protocol_address: "#{sender_protocol_address}", target_hardware_address: "#{target_hardware_address}", target_protocol_address: "#{target_protocol_address}">)
%(#<Arp::Request destination_mac: "#{destination_mac}", source_mac: "#{source_mac}", ether_type: #{ether_type.inspect}, hardware_type: #{hardware_type}, protocol_length: #{protocol_length}, operation: #{operation}, sender_hardware_address: "#{sender_hardware_address}", sender_protocol_address: "#{sender_protocol_address}", target_hardware_address: "#{target_hardware_address}", target_protocol_address: "#{target_protocol_address}">)
end
# rubocop:enable LineLength

Expand Down
17 changes: 5 additions & 12 deletions lib/pio/ethernet_header.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'pio/class_inspector'
require 'pio/instance_inspector'
require 'pio/ruby_dumper'
require 'pio/type/ether_type'
require 'pio/type/mac_address'

module Pio
Expand All @@ -22,9 +24,9 @@ def klass.ethernet_header(options = nil)
mac_address :destination_mac
mac_address :source_mac
if options
uint16 :ether_type, value: options.fetch(:ether_type)
ether_type :ether_type, value: options.fetch(:ether_type)
else
uint16 :ether_type
ether_type :ether_type
end
bit3 :vlan_pcp, onlyif: :vlan?
bit1 :vlan_cfi, onlyif: :vlan?
Expand All @@ -49,20 +51,11 @@ def vlan?
class EthernetHeader < BinData::Record
extend ClassInspector
include Ethernet
include InstanceInspector
include RubyDumper

endian :big

ethernet_header

# rubocop:disable LineLength
def inspect
if vlan?
%(#<EthernetHeader destination_mac: "#{destination_mac}", source_mac: "#{source_mac}", ether_type: #{format('0x%04x', ether_type)}, vlan_pcp: #{vlan_pcp}, vlan_cfi: #{vlan_cfi}, vlan_vid: #{vlan_vid}>)
else
%(#<EthernetHeader destination_mac: "#{destination_mac}", source_mac: "#{source_mac}", ether_type: #{format('0x%04x', ether_type)}>)
end
end
# rubocop:enable LineLength
end
end
13 changes: 13 additions & 0 deletions lib/pio/instance_inspector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Pio
# Introduces #inspect method
module InstanceInspector
def inspect
"#<#{self.class.name} " +
field_names.map do |each|
next unless __send__("#{each}?")
"#{each}: #{__send__(each).inspect}"
end.compact.join(', ') +
'>'
end
end
end
4 changes: 2 additions & 2 deletions lib/pio/monkey_patch/bindata_string.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module BinData
# Add BinData::String#to_hex
# Add BinData::String#to_bytes
class String
def to_hex
def to_bytes
to_s.unpack('H*').pop.scan(/[0-9a-f]{2}/).map do |each|
"0x#{each}"
end.join(', ')
Expand Down
4 changes: 2 additions & 2 deletions lib/pio/monkey_patch/uint/base_conversions.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module MonkeyPatch
module Uint
# Uint8#to_hex, Uintxxbe#to_hex
# Uint8#to_bytes, Uintxxbe#to_bytes
module BaseConversions
def to_hex
def to_bytes
/Uint(8|\d+be)$/=~ self.class.name
nbyte = Regexp.last_match(1).to_i / 4
format("%0#{nbyte}x", to_i).scan(/.{1,2}/).map do |each|
Expand Down
4 changes: 2 additions & 2 deletions lib/pio/open_flow/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Header < BinData::Record
transaction_id :transaction_id
rest :body

def to_hex
def to_bytes
[version,
type,
message_length,
transaction_id].map(&:to_hex).join(', ')
transaction_id].map(&:to_bytes).join(', ')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pio/open_flow/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def set(value)
self.version = VERSIONS.invert.fetch(value)
end

def to_hex
def to_bytes
version.to_hex
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pio/open_flow10/packet_in/reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def set(value)
self.reason = REASONS.fetch(value)
end

def to_hex
def to_bytes
reason.to_hex
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pio/ruby_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def to_ruby
bit_names = []
bit = false
end
list = __send__(each).to_hex
list = __send__(each).to_bytes
next if list.empty?
bytes << " #{list}, # #{each}\n"
pack_template << 'C' * (list.count(',') + 1)
Expand Down
30 changes: 30 additions & 0 deletions lib/pio/type/ether_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'bindata'

module Pio
module Type
# Ether type
class EtherType < BinData::Primitive
endian :big

uint16 :ether_type

def set(value)
self.ether_type = value
end

def get
ether_type
end

def to_bytes
byte1 = format('%02x', (self & 0xff00) >> 8)
byte2 = format('%02x', self & 0xff)
"0x#{byte1}, 0x#{byte2}"
end

def inspect
Kernel.format '0x%04x', self
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pio/type/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def ==(other)
get == other
end

def to_hex
def to_bytes
octets.map(&:to_hex).join(', ')
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/pio/type/mac_address.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'bindata'
require 'pio/mac'
require 'pio/monkey_patch/integer'
require 'pio/monkey_patch/uint'

module Pio
module Type
Expand All @@ -16,6 +18,14 @@ def get
str + format('%02x', each)
end.hex)
end

def to_bytes
octets.map(&:to_hex).join(', ')
end

def inspect
%("#{get}")
end
end
end
end
22 changes: 0 additions & 22 deletions spec/pio/monkey_patch/uint_spec.rb

This file was deleted.

0 comments on commit d0dd84c

Please sign in to comment.