packetfu 1.1.10 → 1.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -0
  4. data/.gitignore +3 -0
  5. data/.travis.yml +8 -0
  6. data/CONTRIBUTING.md +47 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +1 -1
  9. data/README.rdoc +35 -30
  10. data/Rakefile +4 -4
  11. data/bench/octets.rb +9 -9
  12. data/examples/100kpackets.rb +12 -12
  13. data/examples/ackscan.rb +16 -16
  14. data/examples/arp.rb +35 -35
  15. data/examples/arphood.rb +36 -36
  16. data/examples/dissect_thinger.rb +6 -6
  17. data/examples/new-simple-stats.rb +23 -23
  18. data/examples/packetfu-shell.rb +25 -25
  19. data/examples/simple-sniffer.rb +9 -9
  20. data/examples/simple-stats.rb +23 -23
  21. data/examples/slammer.rb +3 -3
  22. data/gem-public_cert.pem +21 -0
  23. data/lib/packetfu.rb +149 -127
  24. data/lib/packetfu/capture.rb +169 -169
  25. data/lib/packetfu/config.rb +52 -52
  26. data/lib/packetfu/inject.rb +56 -56
  27. data/lib/packetfu/packet.rb +531 -528
  28. data/lib/packetfu/pcap.rb +579 -579
  29. data/lib/packetfu/protos/arp.rb +90 -90
  30. data/lib/packetfu/protos/arp/header.rb +158 -158
  31. data/lib/packetfu/protos/arp/mixin.rb +36 -36
  32. data/lib/packetfu/protos/eth.rb +44 -44
  33. data/lib/packetfu/protos/eth/header.rb +243 -243
  34. data/lib/packetfu/protos/eth/mixin.rb +3 -3
  35. data/lib/packetfu/protos/hsrp.rb +69 -69
  36. data/lib/packetfu/protos/hsrp/header.rb +107 -107
  37. data/lib/packetfu/protos/hsrp/mixin.rb +29 -29
  38. data/lib/packetfu/protos/icmp.rb +71 -71
  39. data/lib/packetfu/protos/icmp/header.rb +82 -82
  40. data/lib/packetfu/protos/icmp/mixin.rb +14 -14
  41. data/lib/packetfu/protos/invalid.rb +49 -49
  42. data/lib/packetfu/protos/ip.rb +69 -69
  43. data/lib/packetfu/protos/ip/header.rb +291 -291
  44. data/lib/packetfu/protos/ip/mixin.rb +40 -40
  45. data/lib/packetfu/protos/ipv6.rb +50 -50
  46. data/lib/packetfu/protos/ipv6/header.rb +188 -188
  47. data/lib/packetfu/protos/ipv6/mixin.rb +29 -29
  48. data/lib/packetfu/protos/tcp.rb +176 -176
  49. data/lib/packetfu/protos/tcp/ecn.rb +35 -35
  50. data/lib/packetfu/protos/tcp/flags.rb +74 -74
  51. data/lib/packetfu/protos/tcp/header.rb +268 -268
  52. data/lib/packetfu/protos/tcp/hlen.rb +32 -32
  53. data/lib/packetfu/protos/tcp/mixin.rb +46 -46
  54. data/lib/packetfu/protos/tcp/option.rb +321 -321
  55. data/lib/packetfu/protos/tcp/options.rb +95 -95
  56. data/lib/packetfu/protos/tcp/reserved.rb +35 -35
  57. data/lib/packetfu/protos/udp.rb +159 -123
  58. data/lib/packetfu/protos/udp/header.rb +91 -91
  59. data/lib/packetfu/protos/udp/mixin.rb +3 -3
  60. data/lib/packetfu/structfu.rb +280 -280
  61. data/lib/packetfu/utils.rb +292 -225
  62. data/lib/packetfu/version.rb +41 -41
  63. data/packetfu.gemspec +14 -3
  64. data/spec/arp_spec.rb +191 -0
  65. data/spec/eth_spec.rb +148 -0
  66. data/spec/icmp_spec.rb +97 -0
  67. data/spec/ip_spec.rb +78 -0
  68. data/spec/ipv6_spec.rb +81 -0
  69. data/spec/packet_spec.rb +61 -59
  70. data/spec/packet_subclasses_spec.rb +9 -10
  71. data/spec/packetfu_spec.rb +55 -62
  72. data/spec/sample3.pcap +0 -0
  73. data/spec/spec_helper.rb +44 -0
  74. data/spec/structfu_spec.rb +270 -271
  75. data/spec/tcp_spec.rb +76 -77
  76. data/spec/udp_spec.rb +32 -0
  77. data/spec/utils_spec.rb +95 -0
  78. data/test/all_tests.rb +14 -17
  79. data/test/func_lldp.rb +3 -3
  80. data/test/ptest.rb +2 -2
  81. data/test/test_capture.rb +45 -45
  82. data/test/test_eth.rb +70 -68
  83. data/test/test_hsrp.rb +9 -9
  84. data/test/test_inject.rb +18 -18
  85. data/test/test_invalid.rb +16 -16
  86. data/test/test_octets.rb +23 -21
  87. data/test/test_packet.rb +156 -154
  88. data/test/test_pcap.rb +172 -170
  89. data/test/test_structfu.rb +99 -97
  90. data/test/test_tcp.rb +322 -320
  91. data/test/test_udp.rb +78 -76
  92. metadata +108 -44
  93. metadata.gz.sig +2 -0
  94. data/spec/ethpacket_spec.rb +0 -74
  95. data/test/test_arp.rb +0 -135
  96. data/test/test_icmp.rb +0 -62
  97. data/test/test_ip.rb +0 -50
  98. data/test/test_ip6.rb +0 -68
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: binary -*-
3
+
2
4
  require 'test/unit'
3
5
  $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
6
  require 'packetfu'
@@ -6,85 +8,85 @@ puts "Testing #{PacketFu.version}: #{$0}"
6
8
 
7
9
  class EthTest < Test::Unit::TestCase
8
10
 
9
- def test_ethmac
10
- dst = "\x00\x03\x2f\x1a\x74\xde"
11
- e = PacketFu::EthMac.new
12
- e.read dst
13
- assert_equal(dst, e.to_s)
14
- assert_equal(0x32f, e.oui.oui)
15
- assert_equal("\x1a\x74\xde", e.nic.to_s)
16
- assert_equal(222, e.nic.n2)
17
- end
11
+ def test_ethmac
12
+ dst = "\x00\x03\x2f\x1a\x74\xde"
13
+ e = PacketFu::EthMac.new
14
+ e.read dst
15
+ assert_equal(dst, e.to_s)
16
+ assert_equal(0x32f, e.oui.oui)
17
+ assert_equal("\x1a\x74\xde", e.nic.to_s)
18
+ assert_equal(222, e.nic.n2)
19
+ end
18
20
 
19
- def test_ethmac_ipad
20
- dst = "\x7c\x6d\x62\x01\x02\x03"
21
- e = PacketFu::EthMac.new
22
- e.read dst
23
- assert_equal(dst, e.to_s)
24
- assert_equal(0x6d62, e.oui.oui)
25
- end
21
+ def test_ethmac_ipad
22
+ dst = "\x7c\x6d\x62\x01\x02\x03"
23
+ e = PacketFu::EthMac.new
24
+ e.read dst
25
+ assert_equal(dst, e.to_s)
26
+ assert_equal(0x6d62, e.oui.oui)
27
+ end
26
28
 
27
- def test_ethmac_class
28
- src = "\x00\x1b\x11\x51\xb7\xce"
29
- e = PacketFu::EthMac.new
30
- e.read src
31
- assert_instance_of(PacketFu::EthMac, e)
32
- end
29
+ def test_ethmac_class
30
+ src = "\x00\x1b\x11\x51\xb7\xce"
31
+ e = PacketFu::EthMac.new
32
+ e.read src
33
+ assert_instance_of(PacketFu::EthMac, e)
34
+ end
33
35
 
34
- def test_eth
35
- header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
36
- src = "\x00\x1b\x11\x51\xb7\xce"
37
- dst = "\x00\x03\x2f\x1a\x74\xde"
38
- e = PacketFu::EthHeader.new
39
- e.eth_dst = dst
40
- e.eth_src = src
41
- e.eth_proto = "\x08\x00"
42
- assert_equal(header, e.to_s)
43
- assert_equal(header, PacketFu::EthHeader.new.read(header).to_s)
44
- end
36
+ def test_eth
37
+ header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
38
+ src = "\x00\x1b\x11\x51\xb7\xce"
39
+ dst = "\x00\x03\x2f\x1a\x74\xde"
40
+ e = PacketFu::EthHeader.new
41
+ e.eth_dst = dst
42
+ e.eth_src = src
43
+ e.eth_proto = "\x08\x00"
44
+ assert_equal(header, e.to_s)
45
+ assert_equal(header, PacketFu::EthHeader.new.read(header).to_s)
46
+ end
45
47
 
46
- def test_macaddr
47
- dst = "\x00\x03\x2f\x1a\x74\xde"
48
- dstmac = "00:03:2f:1a:74:de"
49
- assert_equal(dstmac,PacketFu::EthHeader.str2mac(dst))
50
- assert_equal(dst, PacketFu::EthHeader.mac2str(dstmac))
51
- end
48
+ def test_macaddr
49
+ dst = "\x00\x03\x2f\x1a\x74\xde"
50
+ dstmac = "00:03:2f:1a:74:de"
51
+ assert_equal(dstmac,PacketFu::EthHeader.str2mac(dst))
52
+ assert_equal(dst, PacketFu::EthHeader.mac2str(dstmac))
53
+ end
52
54
 
53
55
  end
54
56
 
55
57
  class EthPacketTest < Test::Unit::TestCase
56
- include PacketFu
58
+ include PacketFu
57
59
 
58
- def test_eth_create
59
- sample_packet = PcapFile.new.file_to_array(:f => 'sample.pcap')[0]
60
- e = EthPacket.new
61
- header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
62
- assert_kind_of EthPacket, e
63
- assert_kind_of EthHeader, e.headers[0]
64
- assert e.is_eth?
65
- assert !e.is_tcp?
66
- e.eth_dst = "\x00\x03\x2f\x1a\x74\xde"
67
- e.eth_src = "\x00\x1b\x11\x51\xb7\xce"
68
- e.eth_proto = 0x0800
69
- assert_equal header, e.to_s[0,14]
70
- end
60
+ def test_eth_create
61
+ sample_packet = PcapFile.new.file_to_array(:f => 'sample.pcap')[0]
62
+ e = EthPacket.new
63
+ header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
64
+ assert_kind_of EthPacket, e
65
+ assert_kind_of EthHeader, e.headers[0]
66
+ assert e.is_eth?
67
+ assert !e.is_tcp?
68
+ e.eth_dst = "\x00\x03\x2f\x1a\x74\xde"
69
+ e.eth_src = "\x00\x1b\x11\x51\xb7\xce"
70
+ e.eth_proto = 0x0800
71
+ assert_equal header, e.to_s[0,14]
72
+ end
71
73
 
72
- def test_eth_new
73
- p = EthPacket.new(
74
- :eth_dst => "\x00\x03\x2f\x1a\x74\xde",
75
- :eth_src => "\x00\x1b\x11\x51\xb7\xce",
76
- :eth_proto => 0x0800)
77
- header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
78
- assert_equal header, p.to_s[0,14]
79
- end
74
+ def test_eth_new
75
+ p = EthPacket.new(
76
+ :eth_dst => "\x00\x03\x2f\x1a\x74\xde",
77
+ :eth_src => "\x00\x1b\x11\x51\xb7\xce",
78
+ :eth_proto => 0x0800)
79
+ header = "00032f1a74de001b1151b7ce0800".scan(/../).map { |x| x.to_i(16) }.pack("C*")
80
+ assert_equal header, p.to_s[0,14]
81
+ end
80
82
 
81
- def test_eth_write
82
- p = EthPacket.new(
83
- :eth_dst => "\x00\x03\x2f\x1a\x74\xde",
84
- :eth_src => "\x00\x1b\x11\x51\xb7\xce",
85
- :eth_proto => 0x0800)
86
- p.to_f('eth_test.pcap')
87
- end
83
+ def test_eth_write
84
+ p = EthPacket.new(
85
+ :eth_dst => "\x00\x03\x2f\x1a\x74\xde",
86
+ :eth_src => "\x00\x1b\x11\x51\xb7\xce",
87
+ :eth_proto => 0x0800)
88
+ p.to_f('eth_test.pcap')
89
+ end
88
90
 
89
91
  end
90
92
 
@@ -4,16 +4,16 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class HSRPTest < Test::Unit::TestCase
7
- include PacketFu
7
+ include PacketFu
8
8
 
9
- def test_hsrp_read
10
- sample_packet = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')[0]
11
- pkt = Packet.parse(sample_packet)
12
- assert pkt.is_hsrp?
13
- assert pkt.is_udp?
14
- assert_equal(0x2d8d, pkt.udp_sum.to_i)
15
- # pkt.to_f('udp_test.pcap','a')
16
- end
9
+ def test_hsrp_read
10
+ sample_packet = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')[0]
11
+ pkt = Packet.parse(sample_packet)
12
+ assert pkt.is_hsrp?
13
+ assert pkt.is_udp?
14
+ assert_equal(0x2d8d, pkt.udp_sum.to_i)
15
+ # pkt.to_f('udp_test.pcap','a')
16
+ end
17
17
 
18
18
  end
19
19
 
@@ -6,24 +6,24 @@ require 'packetfu'
6
6
 
7
7
  class InjectTest < Test::Unit::TestCase
8
8
 
9
- def test_cap
10
- assert_nothing_raised { PacketFu::Capture }
11
- end
12
-
13
- def test_whoami
14
- assert_nothing_raised { PacketFu::Utils.whoami?(:iface => (ENV['IFACE'] || 'lo')) }
15
- end
16
-
17
- def test_to_w
18
- assert_equal(Process.euid, 0, "TEST FAIL: This test must be run as root")
19
- conf = PacketFu::Utils.whoami?(:iface => (ENV['IFACE'] || 'lo'))
20
- p = PacketFu::UDPPacket.new(:config => conf)
21
- p.udp_dport = 12345
22
- p.udp_sport = 12345
23
- p.payload = "PacketFu test packet"
24
- p.recalc
25
- assert p.to_w
26
- end
9
+ def test_cap
10
+ assert_nothing_raised { PacketFu::Capture }
11
+ end
12
+
13
+ def test_whoami
14
+ assert_nothing_raised { PacketFu::Utils.whoami?(:iface => PacketFu::Utils.default_int) }
15
+ end
16
+
17
+ def test_to_w
18
+ assert_equal(Process.euid, 0, "TEST FAIL: This test must be run as root")
19
+ conf = PacketFu::Utils.whoami?(:iface => PacketFu::Utils.default_int)
20
+ p = PacketFu::UDPPacket.new(:config => conf)
21
+ p.udp_dport = 12345
22
+ p.udp_sport = 12345
23
+ p.payload = "PacketFu test packet"
24
+ p.recalc
25
+ assert p.to_w
26
+ end
27
27
 
28
28
  end
29
29
 
@@ -4,24 +4,24 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class InvalidTest < Test::Unit::TestCase
7
- include PacketFu
7
+ include PacketFu
8
8
 
9
- def test_create_invalid
10
- p = InvalidPacket.new
11
- assert_kind_of InvalidPacket, p
12
- assert_kind_of Packet, p
13
- assert p.is_invalid?
14
- assert_equal false, p.is_eth?
15
- assert_not_equal EthPacket, p.class
16
- end
9
+ def test_create_invalid
10
+ p = InvalidPacket.new
11
+ assert_kind_of InvalidPacket, p
12
+ assert_kind_of Packet, p
13
+ assert p.is_invalid?
14
+ assert_equal false, p.is_eth?
15
+ assert_not_equal EthPacket, p.class
16
+ end
17
17
 
18
- # Sadly, the only way to generate an "InvalidPacket" is
19
- # to read a packet that's less than 14 bytes. Otherwise,
20
- # it's presumed to be an EthPacket. TODO: Fix this assumption!
21
- def test_parse_invalid
22
- p = Packet.parse("A" * 13)
23
- assert_kind_of InvalidPacket, p
24
- end
18
+ # Sadly, the only way to generate an "InvalidPacket" is
19
+ # to read a packet that's less than 14 bytes. Otherwise,
20
+ # it's presumed to be an EthPacket. TODO: Fix this assumption!
21
+ def test_parse_invalid
22
+ p = Packet.parse("A" * 13)
23
+ assert_kind_of InvalidPacket, p
24
+ end
25
25
 
26
26
  end
27
27
 
@@ -1,33 +1,35 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: binary -*-
3
+
2
4
  require 'test/unit'
3
5
  $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
6
  require 'packetfu'
5
7
 
6
8
  class OctetsTest < Test::Unit::TestCase
7
- include PacketFu
9
+ include PacketFu
8
10
 
9
- def test_octets_read
10
- o = Octets.new
11
- o.read("\x04\x03\x02\x01")
12
- assert_equal("4.3.2.1", o.to_x)
13
- end
11
+ def test_octets_read
12
+ o = Octets.new
13
+ o.read("\x04\x03\x02\x01")
14
+ assert_equal("4.3.2.1", o.to_x)
15
+ end
14
16
 
15
- def test_octets_read_quad
16
- o = Octets.new
17
- o.read_quad("1.2.3.4")
18
- assert_equal("1.2.3.4", o.to_x)
19
- assert_equal("\x01\x02\x03\x04", o.to_s)
20
- assert_equal(0x01020304, o.to_i)
21
- end
17
+ def test_octets_read_quad
18
+ o = Octets.new
19
+ o.read_quad("1.2.3.4")
20
+ assert_equal("1.2.3.4", o.to_x)
21
+ assert_equal("\x01\x02\x03\x04", o.to_s)
22
+ assert_equal(0x01020304, o.to_i)
23
+ end
22
24
 
23
- def test_octets_single_octet
24
- o = Octets.new
25
- o.read("ABCD")
26
- assert_equal(o.o1, 0x41)
27
- assert_equal(o.o2, 0x42)
28
- assert_equal(o.o3, 0x43)
29
- assert_equal(o.o4, 0x44)
30
- end
25
+ def test_octets_single_octet
26
+ o = Octets.new
27
+ o.read("ABCD")
28
+ assert_equal(o.o1, 0x41)
29
+ assert_equal(o.o2, 0x42)
30
+ assert_equal(o.o3, 0x43)
31
+ assert_equal(o.o4, 0x44)
32
+ end
31
33
 
32
34
  end
33
35
 
@@ -1,172 +1,174 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: binary -*-
3
+
2
4
  require 'test/unit'
3
5
  $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
4
6
  require 'packetfu'
5
7
 
6
8
  class NewPacketTest < Test::Unit::TestCase
7
- include PacketFu
8
-
9
- def test_method_missing_and_respond_to
10
- p = TCPPacket.new
11
- assert p.respond_to?(:ip_len)
12
- assert p.ip_len = 20
13
- assert !(p.respond_to? :ip_bogus_header)
14
- assert_raise NoMethodError do
15
- p.bogus_header = 20
16
- end
17
- end
18
-
19
- def test_more_method_missing_magic
20
- p = UDPPacket.new
21
- assert_kind_of(UDPPacket,p)
22
- assert p.is_udp?
23
- assert p.is_ip?
24
- assert p.is_eth?
25
- assert_equal(p.ip_hl,5)
26
- assert p.layer
27
- assert_raise NoMethodError do
28
- p.is_blue?
29
- end
30
- assert_raise NoMethodError do
31
- p.tcp_blue
32
- end
33
- assert_raise NoMethodError do
34
- p.udp_blue
35
- end
36
- assert_raise NoMethodError do
37
- p.blue
38
- end
39
- end
9
+ include PacketFu
10
+
11
+ def test_method_missing_and_respond_to
12
+ p = TCPPacket.new
13
+ assert p.respond_to?(:ip_len)
14
+ assert p.ip_len = 20
15
+ assert !(p.respond_to? :ip_bogus_header)
16
+ assert_raise NoMethodError do
17
+ p.bogus_header = 20
18
+ end
19
+ end
20
+
21
+ def test_more_method_missing_magic
22
+ p = UDPPacket.new
23
+ assert_kind_of(UDPPacket,p)
24
+ assert p.is_udp?
25
+ assert p.is_ip?
26
+ assert p.is_eth?
27
+ assert_equal(p.ip_hl,5)
28
+ assert p.layer
29
+ assert_raise NoMethodError do
30
+ p.is_blue?
31
+ end
32
+ assert_raise NoMethodError do
33
+ p.tcp_blue
34
+ end
35
+ assert_raise NoMethodError do
36
+ p.udp_blue
37
+ end
38
+ assert_raise NoMethodError do
39
+ p.blue
40
+ end
41
+ end
40
42
  end
41
43
 
42
44
  class PacketStrippingTest < Test::Unit::TestCase
43
45
 
44
- include PacketFu
46
+ include PacketFu
45
47
 
46
- def test_arp_strip
47
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
48
- p = Packet.parse(pcaps[5], :fix => true) # Really ARP request.
49
- assert_kind_of(Packet,p)
50
- assert_kind_of(ARPPacket,p)
51
- end
48
+ def test_arp_strip
49
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
50
+ p = Packet.parse(pcaps[5], :fix => true) # Really ARP request.
51
+ assert_kind_of(Packet,p)
52
+ assert_kind_of(ARPPacket,p)
53
+ end
52
54
 
53
55
  end
54
56
 
55
57
  class PacketParsersTest < Test::Unit::TestCase
56
- include PacketFu
57
-
58
- def test_parse_eth_packet
59
- assert_equal(EthPacket.layer, 1)
60
- assert_equal(EthPacket.layer_symbol, :link)
61
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
62
- p = Packet.parse(pcaps[5]) # Really ARP.
63
- assert_kind_of(Packet,p)
64
- assert_kind_of(EthHeader, p.headers[0])
65
- assert p.is_eth?
66
- assert_equal(pcaps[5],p.to_s)
67
- end
68
-
69
- def test_parse_arp_request
70
- assert_equal(ARPPacket.layer, 2)
71
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
72
- p = Packet.parse(pcaps[5]) # Really ARP request.
73
- assert p.is_eth?
74
- assert_kind_of(EthPacket,p)
75
- assert_kind_of(ARPPacket,p)
76
- assert p.is_arp?
77
- assert_equal(p.to_s, pcaps[5])
78
- assert_equal(1, p.arp_opcode.to_i)
79
- assert_equal("\x00\x01", p.headers.last[:arp_opcode].to_s)
80
- end
81
-
82
- def test_parse_arp_reply
83
- assert_equal(ARPPacket.layer, 2)
84
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
85
- p = Packet.parse(pcaps[6]) # Really ARP reply.
86
- assert_equal(p.to_s, pcaps[6])
87
- assert_equal(2, p.arp_opcode.to_i)
88
- assert_equal("\x00\x02", p.headers.last[:arp_opcode].to_s)
89
- end
90
-
91
- def test_parse_ip_packet
92
- assert_equal(IPPacket.layer, 2)
93
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
94
- p = Packet.parse(pcaps[0]) # Really DNS request
95
- assert_equal(p.to_s[0,20], pcaps[0][0,20])
96
- assert_equal(p.to_s, pcaps[0])
97
- assert_kind_of(EthPacket,p)
98
- assert_kind_of(IPPacket,p)
99
- end
100
-
101
- def test_parse_tcp_packet
102
- assert_equal(TCPPacket.layer, 3)
103
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
104
- p = Packet.parse(pcaps[7]) # Really FIN/ACK
105
- assert_equal(p.to_s, pcaps[7])
106
- assert_kind_of(EthPacket,p)
107
- assert_kind_of(IPPacket,p)
108
- assert_kind_of(TCPPacket,p)
109
- end
110
-
111
- def test_parse_udp_packet
112
- assert_equal(UDPPacket.layer, 3)
113
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
114
- p = Packet.parse(pcaps[0]) # Really DNS request
115
- assert_equal(p.to_s, pcaps[0])
116
- assert_kind_of(EthPacket,p)
117
- assert_kind_of(IPPacket,p)
118
- assert_kind_of(UDPPacket,p)
119
- end
120
-
121
- def test_parse_icmp_packet
122
- assert_equal(ICMPPacket.layer, 3)
123
- assert_equal(ICMPPacket.layer_symbol, :transport)
124
- pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
125
- p = Packet.parse(pcaps[3]) # Really ICMP reply
126
- assert_equal(p.to_s, pcaps[3])
127
- assert_kind_of(EthPacket,p)
128
- assert_kind_of(IPPacket,p)
129
- assert_kind_of(ICMPPacket,p)
130
- end
131
-
132
- def test_parse_invalid_packet
133
- assert_equal(InvalidPacket.layer, 0)
134
- assert_equal(InvalidPacket.layer_symbol, :invalid)
135
- p = Packet.parse("\xff\xfe\x00\x01")
136
- assert_equal(p.to_s, "\xff\xfe\x00\x01")
137
- assert_kind_of(InvalidPacket,p)
138
- end
139
-
140
- def test_parse_ipv6_packet
141
- assert_equal(IPv6Packet.layer, 2)
142
- assert_equal(IPv6Packet.layer_symbol, :internet)
143
- pcaps = PcapFile.new.file_to_array(:f => 'sample-ipv6.pcap')
144
- p = Packet.parse(pcaps[0]) # Really an IPv6 packet
145
- assert_equal(p.to_s, pcaps[0])
146
- assert_kind_of(EthPacket,p)
147
- assert(!p.kind_of?(IPPacket), "Misidentified as an IP Packet!")
148
- assert_kind_of(IPv6Packet,p)
149
- end
150
-
151
- def test_parse_hsrp_packet
152
- assert_equal(HSRPPacket.layer, 4)
153
- assert_equal(HSRPPacket.layer_symbol, :application)
154
- pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
155
- p = Packet.parse(pcaps[0]) # Really an HSRP Hello packet
156
- assert_equal(p.to_s, pcaps[0])
157
- assert_kind_of(EthPacket,p)
158
- assert_kind_of(IPPacket,p)
159
- assert_kind_of(UDPPacket,p)
160
- assert_kind_of(HSRPPacket,p)
161
- end
162
-
163
- def test_parse_hsrp_as_udp
164
- assert_equal(:application, HSRPPacket.layer_symbol)
165
- pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
166
- p = Packet.parse(pcaps[0], :parse_app => false) # Really an HSRP Hello packet
167
- assert_kind_of(UDPPacket,p)
168
- assert(!p.kind_of?(HSRPPacket), "Misidentified HSRP packet when we didn't want it!" )
169
- end
58
+ include PacketFu
59
+
60
+ def test_parse_eth_packet
61
+ assert_equal(EthPacket.layer, 1)
62
+ assert_equal(EthPacket.layer_symbol, :link)
63
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
64
+ p = Packet.parse(pcaps[5]) # Really ARP.
65
+ assert_kind_of(Packet,p)
66
+ assert_kind_of(EthHeader, p.headers[0])
67
+ assert p.is_eth?
68
+ assert_equal(pcaps[5],p.to_s)
69
+ end
70
+
71
+ def test_parse_arp_request
72
+ assert_equal(ARPPacket.layer, 2)
73
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
74
+ p = Packet.parse(pcaps[5]) # Really ARP request.
75
+ assert p.is_eth?
76
+ assert_kind_of(EthPacket,p)
77
+ assert_kind_of(ARPPacket,p)
78
+ assert p.is_arp?
79
+ assert_equal(p.to_s, pcaps[5])
80
+ assert_equal(1, p.arp_opcode.to_i)
81
+ assert_equal("\x00\x01", p.headers.last[:arp_opcode].to_s)
82
+ end
83
+
84
+ def test_parse_arp_reply
85
+ assert_equal(ARPPacket.layer, 2)
86
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
87
+ p = Packet.parse(pcaps[6]) # Really ARP reply.
88
+ assert_equal(p.to_s, pcaps[6])
89
+ assert_equal(2, p.arp_opcode.to_i)
90
+ assert_equal("\x00\x02", p.headers.last[:arp_opcode].to_s)
91
+ end
92
+
93
+ def test_parse_ip_packet
94
+ assert_equal(IPPacket.layer, 2)
95
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
96
+ p = Packet.parse(pcaps[0]) # Really DNS request
97
+ assert_equal(p.to_s[0,20], pcaps[0][0,20])
98
+ assert_equal(p.to_s, pcaps[0])
99
+ assert_kind_of(EthPacket,p)
100
+ assert_kind_of(IPPacket,p)
101
+ end
102
+
103
+ def test_parse_tcp_packet
104
+ assert_equal(TCPPacket.layer, 3)
105
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
106
+ p = Packet.parse(pcaps[7]) # Really FIN/ACK
107
+ assert_equal(p.to_s, pcaps[7])
108
+ assert_kind_of(EthPacket,p)
109
+ assert_kind_of(IPPacket,p)
110
+ assert_kind_of(TCPPacket,p)
111
+ end
112
+
113
+ def test_parse_udp_packet
114
+ assert_equal(UDPPacket.layer, 3)
115
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
116
+ p = Packet.parse(pcaps[0]) # Really DNS request
117
+ assert_equal(p.to_s, pcaps[0])
118
+ assert_kind_of(EthPacket,p)
119
+ assert_kind_of(IPPacket,p)
120
+ assert_kind_of(UDPPacket,p)
121
+ end
122
+
123
+ def test_parse_icmp_packet
124
+ assert_equal(ICMPPacket.layer, 3)
125
+ assert_equal(ICMPPacket.layer_symbol, :transport)
126
+ pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
127
+ p = Packet.parse(pcaps[3]) # Really ICMP reply
128
+ assert_equal(p.to_s, pcaps[3])
129
+ assert_kind_of(EthPacket,p)
130
+ assert_kind_of(IPPacket,p)
131
+ assert_kind_of(ICMPPacket,p)
132
+ end
133
+
134
+ def test_parse_invalid_packet
135
+ assert_equal(InvalidPacket.layer, 0)
136
+ assert_equal(InvalidPacket.layer_symbol, :invalid)
137
+ p = Packet.parse("\xff\xfe\x00\x01")
138
+ assert_equal(p.to_s, "\xff\xfe\x00\x01")
139
+ assert_kind_of(InvalidPacket,p)
140
+ end
141
+
142
+ def test_parse_ipv6_packet
143
+ assert_equal(IPv6Packet.layer, 2)
144
+ assert_equal(IPv6Packet.layer_symbol, :internet)
145
+ pcaps = PcapFile.new.file_to_array(:f => 'sample-ipv6.pcap')
146
+ p = Packet.parse(pcaps[0]) # Really an IPv6 packet
147
+ assert_equal(p.to_s, pcaps[0])
148
+ assert_kind_of(EthPacket,p)
149
+ assert(!p.kind_of?(IPPacket), "Misidentified as an IP Packet!")
150
+ assert_kind_of(IPv6Packet,p)
151
+ end
152
+
153
+ def test_parse_hsrp_packet
154
+ assert_equal(HSRPPacket.layer, 4)
155
+ assert_equal(HSRPPacket.layer_symbol, :application)
156
+ pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
157
+ p = Packet.parse(pcaps[0]) # Really an HSRP Hello packet
158
+ assert_equal(p.to_s, pcaps[0])
159
+ assert_kind_of(EthPacket,p)
160
+ assert_kind_of(IPPacket,p)
161
+ assert_kind_of(UDPPacket,p)
162
+ assert_kind_of(HSRPPacket,p)
163
+ end
164
+
165
+ def test_parse_hsrp_as_udp
166
+ assert_equal(:application, HSRPPacket.layer_symbol)
167
+ pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
168
+ p = Packet.parse(pcaps[0], :parse_app => false) # Really an HSRP Hello packet
169
+ assert_kind_of(UDPPacket,p)
170
+ assert(!p.kind_of?(HSRPPacket), "Misidentified HSRP packet when we didn't want it!" )
171
+ end
170
172
 
171
173
  end
172
174