File: packet_spec.rb

package info (click to toggle)
ruby-packetfu 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,520 kB
  • sloc: ruby: 8,344; makefile: 2
file content (99 lines) | stat: -rw-r--r-- 2,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'spec_helper'
require 'packetfu/packet'
require 'packetfu/pcap'
require 'packetfu/protos/eth'
require 'packetfu/protos/ip'
require 'packetfu/protos/ipv6'
require 'packetfu/protos/tcp'
require 'packetfu/protos/icmp'
require 'fake_packets'

describe PacketFu::Packet, "abstract packet class behavior" do

  before(:all) do
    add_fake_packets
  end

  after(:all) do
    remove_fake_packets
  end

  it "should not be instantiated" do
    expect { PacketFu::Packet.new }.to raise_error(NoMethodError)
  end

  it "should allow subclasses to instantiate" do
    expect(PacketFu::FooPacket.new).to be
    PacketFu.packet_classes.include?(PacketFu::FooPacket).should be true
  end

  it "should register packet classes with PacketFu" do
    PacketFu.packet_classes {should include(FooPacket) }
    PacketFu.packet_classes {should include(BarPacket) }
  end

  it "should disallow badly named subclasses" do
    expect {
      class PacketFu::PacketNot < PacketFu::Packet
      end
    }.to raise_error(RuntimeError, "Packet classes should be named 'ProtoPacket'")
    PacketFu.packet_classes.include?(PacketFu::PacketNot).should be false
    PacketFu.packet_classes {should_not include(PacketNot) }
  end

  before(:each) do
    @tcp_packet = PacketFu::TCPPacket.new
    @tcp_packet.ip_saddr = "10.10.10.10"
  end

  it "should shallow copy with dup()" do
    p2 = @tcp_packet.dup
    p2.ip_saddr = "20.20.20.20"
    p2.ip_saddr.should == @tcp_packet.ip_saddr
    p2.headers[1].object_id.should == @tcp_packet.headers[1].object_id
  end

  it "should deep copy with clone()" do
    p3 = @tcp_packet.clone
    p3.ip_saddr = "30.30.30.30"
    p3.ip_saddr.should_not == @tcp_packet.ip_saddr
    p3.headers[1].object_id.should_not == @tcp_packet.headers[1].object_id
  end

  it "should have senisble equality" do
    p4 = @tcp_packet.dup
    p4.should == @tcp_packet
    p5 = @tcp_packet.clone
    p5.should == @tcp_packet
  end

  # It's actually kinda hard to manually create identical TCP packets
  it "should be possible to manually create identical packets" do
    p6 = @tcp_packet.clone
    p6.should == @tcp_packet
    p7 = PacketFu::TCPPacket.new
    p7.ip_saddr = p6.ip_saddr
    p7.ip_id = p6.ip_id
    p7.tcp_seq = p6.tcp_seq
    p7.tcp_src = p6.tcp_src
    p7.tcp_sum = p6.tcp_sum
    p7.should == p6
  end

  it "should parse IPv4 packets" do
    packets = PacketFu::PcapFile.read(File.join(File.dirname(__FILE__), 'ipv4_icmp.pcap'))
    packets.size.should == 1
    packet = PacketFu::Packet.parse(packets.first.data.to_s)
    packet.should be_a(PacketFu::ICMPPacket)
    packet.headers[1].should be_a(PacketFu::IPHeader)
  end

  it "should parse IPv6 packets" do
    packets = PacketFu::PcapFile.read(File.join(File.dirname(__FILE__), 'ipv6_udp.pcap'))
    packets.size.should == 1
    packet = PacketFu::Packet.parse(packets.first.data.to_s)
    packet.should be_a(PacketFu::UDPPacket)
    packet.headers[1].should be_a(PacketFu::IPv6Header)
  end

end