Skip to content

Commit

Permalink
Add IPv4Header.read tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 7, 2016
1 parent 3b7969a commit 334d5b5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
46 changes: 46 additions & 0 deletions features/ipv4_header.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,49 @@ Feature: IPv4Header
| source_ip_address | 1.2.3.4 |
| destination_ip_address | 4.3.2.1 |
| ip_option | |

Scenario: read an IPv4 header
Given I use the fixture "ipv4_header"
When I create a packet with:
"""ruby
Pio::IPv4Header.read(eval(IO.read('ipv4_header.rb')))
"""
Then the packet has the following fields and values:
| field | value |
| class | Pio::IPv4Header |
| ip_version | 4 |
| ip_header_length | 5 |
| ip_type_of_service | 0 |
| ip_total_length | 20 |
| ip_identifier | 0 |
| ip_flag | 0 |
| ip_fragment | 0 |
| ip_ttl | 128 |
| ip_protocol | 0 |
| ip_header_checksum | 12513 |
| source_ip_address | 1.2.3.4 |
| destination_ip_address | 4.3.2.1 |
| ip_option | |

@wip
Scenario: convert IPv4 header to Ruby code
When I eval the following Ruby code:
"""ruby
IPv4Header.new(source_ip_address: '1.2.3.4',
destination_ip_address: '4.3.2.1').to_ruby
"""
Then the result of eval should be:
"""ruby
[
0b0100_0101, # ip_version, ip_header_length
0x00, # ip_type_of_service
0x00, 0x14, # ip_total_length
0x00, 0x00, # ip_identifier
0b000_0000000000000, # ip_flag, ip_fragment
0x80, # ip_ttl
0x00, # ip_protocol
0x30, 0xe1, # ip_header_checksum
0x01, 0x02, 0x03, 0x04, # source_ip_address
0x04, 0x03, 0x02, 0x01, # destination_ip_address
].pack('C6nC12')
"""
12 changes: 12 additions & 0 deletions fixtures/ipv4_header/ipv4_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
0b0100_0101, # ip_version, ip_header_length
0x00, # ip_type_of_service
0x00, 0x14, # ip_total_length
0x00, 0x00, # ip_identifier
0b000_0000000000000, # ip_flag, ip_fragment
0x80, # ip_ttl
0x00, # ip_protocol
0x30, 0xe1, # ip_header_checksum
0x01, 0x02, 0x03, 0x04, # source_ip_address
0x04, 0x03, 0x02, 0x01, # destination_ip_address
].pack('C6nC12')
3 changes: 3 additions & 0 deletions lib/pio/ipv4_header.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'pio/monkey_patch/bindata_string'
require 'pio/payload'
require 'pio/ruby_dumper'
require 'pio/type/ip_address'

module Pio
Expand Down Expand Up @@ -86,6 +88,7 @@ def ip_option_length
# IPv4 header generator/parser
class IPv4Header < BinData::Record
include IPv4
include RubyDumper

ipv4_header
end
Expand Down
7 changes: 7 additions & 0 deletions lib/pio/monkey_patch/bindata_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module BinData
class String
def to_hex
to_s.unpack('H*').pop.scan(/[0-9a-f]{2}/).map { |each| "0x#{each}" }.join(', ')
end
end
end

0 comments on commit 334d5b5

Please sign in to comment.