packetfu 1.1.9 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/bench/octets.rb +9 -9
  2. data/examples/100kpackets.rb +12 -12
  3. data/examples/ackscan.rb +16 -16
  4. data/examples/arp.rb +35 -35
  5. data/examples/arphood.rb +36 -36
  6. data/examples/dissect_thinger.rb +6 -6
  7. data/examples/new-simple-stats.rb +23 -23
  8. data/examples/packetfu-shell.rb +25 -25
  9. data/examples/simple-sniffer.rb +9 -9
  10. data/examples/simple-stats.rb +23 -23
  11. data/examples/slammer.rb +3 -3
  12. data/lib/packetfu.rb +127 -127
  13. data/lib/packetfu/capture.rb +169 -169
  14. data/lib/packetfu/config.rb +52 -52
  15. data/lib/packetfu/inject.rb +56 -56
  16. data/lib/packetfu/packet.rb +528 -528
  17. data/lib/packetfu/pcap.rb +579 -579
  18. data/lib/packetfu/protos/arp.rb +90 -90
  19. data/lib/packetfu/protos/arp/header.rb +158 -158
  20. data/lib/packetfu/protos/arp/mixin.rb +36 -36
  21. data/lib/packetfu/protos/eth.rb +44 -44
  22. data/lib/packetfu/protos/eth/header.rb +243 -243
  23. data/lib/packetfu/protos/eth/mixin.rb +3 -3
  24. data/lib/packetfu/protos/hsrp.rb +69 -69
  25. data/lib/packetfu/protos/hsrp/header.rb +107 -107
  26. data/lib/packetfu/protos/hsrp/mixin.rb +29 -29
  27. data/lib/packetfu/protos/icmp.rb +71 -71
  28. data/lib/packetfu/protos/icmp/header.rb +82 -82
  29. data/lib/packetfu/protos/icmp/mixin.rb +14 -14
  30. data/lib/packetfu/protos/invalid.rb +49 -49
  31. data/lib/packetfu/protos/ip.rb +69 -69
  32. data/lib/packetfu/protos/ip/header.rb +291 -291
  33. data/lib/packetfu/protos/ip/mixin.rb +40 -40
  34. data/lib/packetfu/protos/ipv6.rb +50 -50
  35. data/lib/packetfu/protos/ipv6/header.rb +188 -188
  36. data/lib/packetfu/protos/ipv6/mixin.rb +29 -29
  37. data/lib/packetfu/protos/tcp.rb +176 -176
  38. data/lib/packetfu/protos/tcp/ecn.rb +35 -35
  39. data/lib/packetfu/protos/tcp/flags.rb +74 -74
  40. data/lib/packetfu/protos/tcp/header.rb +268 -268
  41. data/lib/packetfu/protos/tcp/hlen.rb +32 -32
  42. data/lib/packetfu/protos/tcp/mixin.rb +46 -46
  43. data/lib/packetfu/protos/tcp/option.rb +321 -321
  44. data/lib/packetfu/protos/tcp/options.rb +95 -95
  45. data/lib/packetfu/protos/tcp/reserved.rb +35 -35
  46. data/lib/packetfu/protos/udp.rb +116 -116
  47. data/lib/packetfu/protos/udp/header.rb +91 -91
  48. data/lib/packetfu/protos/udp/mixin.rb +3 -3
  49. data/lib/packetfu/structfu.rb +280 -280
  50. data/lib/packetfu/utils.rb +226 -217
  51. data/lib/packetfu/version.rb +41 -41
  52. data/packetfu.gemspec +2 -1
  53. data/spec/ethpacket_spec.rb +48 -48
  54. data/spec/packet_spec.rb +57 -57
  55. data/spec/packet_subclasses_spec.rb +8 -8
  56. data/spec/packetfu_spec.rb +59 -59
  57. data/spec/structfu_spec.rb +268 -268
  58. data/spec/tcp_spec.rb +75 -75
  59. data/test/all_tests.rb +13 -13
  60. data/test/func_lldp.rb +3 -3
  61. data/test/ptest.rb +2 -2
  62. data/test/test_arp.rb +116 -116
  63. data/test/test_capture.rb +45 -45
  64. data/test/test_eth.rb +68 -68
  65. data/test/test_hsrp.rb +9 -9
  66. data/test/test_icmp.rb +52 -52
  67. data/test/test_inject.rb +18 -18
  68. data/test/test_invalid.rb +16 -16
  69. data/test/test_ip.rb +36 -36
  70. data/test/test_ip6.rb +48 -48
  71. data/test/test_octets.rb +21 -21
  72. data/test/test_packet.rb +154 -154
  73. data/test/test_pcap.rb +170 -170
  74. data/test/test_structfu.rb +97 -97
  75. data/test/test_tcp.rb +320 -320
  76. data/test/test_udp.rb +76 -76
  77. metadata +4 -3
@@ -4,46 +4,46 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class IPTest < Test::Unit::TestCase
7
- include PacketFu
7
+ include PacketFu
8
8
 
9
- def test_ip_header_new
10
- i = IPHeader.new
11
- assert_kind_of IPHeader, i
12
- i.ip_id = 0x1234
13
- i.ip_recalc :ip_sum
14
- assert_equal("E\000\000\024\0224\000\000 \000\210\267\000\000\000\000\000\000\000\000", i.to_s)
15
- end
9
+ def test_ip_header_new
10
+ i = IPHeader.new
11
+ assert_kind_of IPHeader, i
12
+ i.ip_id = 0x1234
13
+ i.ip_recalc :ip_sum
14
+ assert_equal("E\000\000\024\0224\000\000 \000\210\267\000\000\000\000\000\000\000\000", i.to_s)
15
+ end
16
16
 
17
- def test_ip_packet_new
18
- i = IPPacket.new
19
- assert i.is_ip?
20
- end
17
+ def test_ip_packet_new
18
+ i = IPPacket.new
19
+ assert i.is_ip?
20
+ end
21
21
 
22
- def test_ip_peek
23
- i = IPPacket.new
24
- i.ip_saddr = "1.2.3.4"
25
- i.ip_daddr = "5.6.7.8"
26
- i.ip_proto = 94
27
- i.payload = '\x00' * 30
28
- i.recalc
29
- puts "\n"
30
- puts "IP Peek format: "
31
- puts i.peek
32
- assert (i.peek.size <= 80)
33
- end
22
+ def test_ip_peek
23
+ i = IPPacket.new
24
+ i.ip_saddr = "1.2.3.4"
25
+ i.ip_daddr = "5.6.7.8"
26
+ i.ip_proto = 94
27
+ i.payload = '\x00' * 30
28
+ i.recalc
29
+ puts "\n"
30
+ puts "IP Peek format: "
31
+ puts i.peek
32
+ assert (i.peek.size <= 80)
33
+ end
34
34
 
35
- def test_ip_pcap
36
- i = IPPacket.new
37
- assert_kind_of IPPacket, i
38
- i.recalc
39
- i.to_f('ip_test.pcap')
40
- i.ip_saddr = "1.2.3.4"
41
- i.ip_daddr = "5.6.7.8"
42
- i.ip_proto = 94
43
- i.payload = "\x23" * 10
44
- i.recalc
45
- i.to_f('ip_test.pcap','a')
46
- end
35
+ def test_ip_pcap
36
+ i = IPPacket.new
37
+ assert_kind_of IPPacket, i
38
+ i.recalc
39
+ i.to_f('ip_test.pcap')
40
+ i.ip_saddr = "1.2.3.4"
41
+ i.ip_daddr = "5.6.7.8"
42
+ i.ip_proto = 94
43
+ i.payload = "\x23" * 10
44
+ i.recalc
45
+ i.to_f('ip_test.pcap','a')
46
+ end
47
47
 
48
48
  end
49
49
 
@@ -4,65 +4,65 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class IPv6AddrTest < Test::Unit::TestCase
7
- include PacketFu
7
+ include PacketFu
8
8
 
9
- def test_addr_read
10
- a = AddrIpv6.new
11
- addr = "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x1a\xc5\xff\xfe\x00\x01\x52"
12
- a.read(addr)
13
- assert_equal(338288524927261089654170548082086773074, a.to_i)
14
- assert_equal("fe80::21a:c5ff:fe00:152",a.to_x)
15
- end
9
+ def test_addr_read
10
+ a = AddrIpv6.new
11
+ addr = "\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x1a\xc5\xff\xfe\x00\x01\x52"
12
+ a.read(addr)
13
+ assert_equal(338288524927261089654170548082086773074, a.to_i)
14
+ assert_equal("fe80::21a:c5ff:fe00:152",a.to_x)
15
+ end
16
16
 
17
- def test_octets_read_quad
18
- a = AddrIpv6.new
19
- addr = "fe80::21a:c5ff:fe00:152"
20
- a.read_x(addr)
21
- assert_equal(addr,a.to_x)
22
- end
17
+ def test_octets_read_quad
18
+ a = AddrIpv6.new
19
+ addr = "fe80::21a:c5ff:fe00:152"
20
+ a.read_x(addr)
21
+ assert_equal(addr,a.to_x)
22
+ end
23
23
 
24
24
  end
25
25
 
26
26
  class IPv6Test < Test::Unit::TestCase
27
- include PacketFu
27
+ include PacketFu
28
28
 
29
- def test_ipv6_header_new
30
- i = IPv6Header.new
31
- assert_kind_of IPv6Header, i
32
- assert_equal("`\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", i.to_s)
33
- end
29
+ def test_ipv6_header_new
30
+ i = IPv6Header.new
31
+ assert_kind_of IPv6Header, i
32
+ assert_equal("`\000\000\000\000\000\000\377\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000", i.to_s)
33
+ end
34
34
 
35
- def test_ipv6_packet_new
36
- i = IPv6Packet.new
37
- assert i.is_ipv6?
38
- end
35
+ def test_ipv6_packet_new
36
+ i = IPv6Packet.new
37
+ assert i.is_ipv6?
38
+ end
39
39
 
40
- def test_ipv6_peek
41
- i = IPv6Packet.new
42
- i.ipv6_saddr = "fe80::1"
43
- i.ipv6_daddr = "fe80::2"
44
- i.ipv6_next = 0x11
45
- i.payload = '\x00' * 30
46
- i.recalc
47
- puts "\n"
48
- puts "IPv6 Peek format: "
49
- puts i.peek
50
- assert (i.peek.size <= 80)
51
- end
40
+ def test_ipv6_peek
41
+ i = IPv6Packet.new
42
+ i.ipv6_saddr = "fe80::1"
43
+ i.ipv6_daddr = "fe80::2"
44
+ i.ipv6_next = 0x11
45
+ i.payload = '\x00' * 30
46
+ i.recalc
47
+ puts "\n"
48
+ puts "IPv6 Peek format: "
49
+ puts i.peek
50
+ assert (i.peek.size <= 80)
51
+ end
52
52
 
53
53
  =begin
54
- def test_ipv6_pcap
55
- i = IPPacket.new
56
- assert_kind_of IPPacket, i
57
- i.recalc
58
- i.to_f('ip_test.pcap')
59
- i.ip_saddr = "1.2.3.4"
60
- i.ip_daddr = "5.6.7.8"
61
- i.ip_proto = 94
62
- i.payload = "\x23" * 10
63
- i.recalc
64
- i.to_f('ip_test.pcap','a')
65
- end
54
+ def test_ipv6_pcap
55
+ i = IPPacket.new
56
+ assert_kind_of IPPacket, i
57
+ i.recalc
58
+ i.to_f('ip_test.pcap')
59
+ i.ip_saddr = "1.2.3.4"
60
+ i.ip_daddr = "5.6.7.8"
61
+ i.ip_proto = 94
62
+ i.payload = "\x23" * 10
63
+ i.recalc
64
+ i.to_f('ip_test.pcap','a')
65
+ end
66
66
  =end
67
67
  end
68
68
  # vim: nowrap sw=2 sts=0 ts=2 ff=unix ft=ruby
@@ -4,30 +4,30 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class OctetsTest < Test::Unit::TestCase
7
- include PacketFu
7
+ include PacketFu
8
8
 
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
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
14
14
 
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
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
22
22
 
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
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
31
31
 
32
32
  end
33
33
 
@@ -4,169 +4,169 @@ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
4
4
  require 'packetfu'
5
5
 
6
6
  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
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
40
40
  end
41
41
 
42
42
  class PacketStrippingTest < Test::Unit::TestCase
43
43
 
44
- include PacketFu
44
+ include PacketFu
45
45
 
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
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
52
52
 
53
53
  end
54
54
 
55
55
  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
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
170
170
 
171
171
  end
172
172