PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_packet.rb

http://packetfu.googlecode.com/
Ruby | 174 lines | 154 code | 19 blank | 1 comment | 0 complexity | 3bb5a3a42153b5c24672a701abbc9d06 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'test/unit'
  3. $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
  4. require 'packetfu'
  5. class NewPacketTest < Test::Unit::TestCase
  6. include PacketFu
  7. def test_method_missing_and_respond_to
  8. p = TCPPacket.new
  9. assert p.respond_to?(:ip_len)
  10. assert p.ip_len = 20
  11. assert !(p.respond_to? :ip_bogus_header)
  12. assert_raise NoMethodError do
  13. p.bogus_header = 20
  14. end
  15. end
  16. def test_more_method_missing_magic
  17. p = UDPPacket.new
  18. assert_kind_of(UDPPacket,p)
  19. assert p.is_udp?
  20. assert p.is_ip?
  21. assert p.is_eth?
  22. assert_equal(p.ip_hl,5)
  23. assert p.layer
  24. assert_raise NoMethodError do
  25. p.is_blue?
  26. end
  27. assert_raise NoMethodError do
  28. p.tcp_blue
  29. end
  30. assert_raise NoMethodError do
  31. p.udp_blue
  32. end
  33. assert_raise NoMethodError do
  34. p.blue
  35. end
  36. end
  37. end
  38. class PacketStrippingTest < Test::Unit::TestCase
  39. include PacketFu
  40. def test_arp_strip
  41. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  42. p = Packet.parse(pcaps[5], :fix => true) # Really ARP request.
  43. assert_kind_of(Packet,p)
  44. assert_kind_of(ARPPacket,p)
  45. end
  46. end
  47. class PacketParsersTest < Test::Unit::TestCase
  48. include PacketFu
  49. def test_parse_eth_packet
  50. assert_equal(EthPacket.layer, 1)
  51. assert_equal(EthPacket.layer_symbol, :link)
  52. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  53. p = Packet.parse(pcaps[5]) # Really ARP.
  54. assert_kind_of(Packet,p)
  55. assert_kind_of(EthHeader, p.headers[0])
  56. assert p.is_eth?
  57. assert_equal(pcaps[5],p.to_s)
  58. end
  59. def test_parse_arp_request
  60. assert_equal(ARPPacket.layer, 2)
  61. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  62. p = Packet.parse(pcaps[5]) # Really ARP request.
  63. assert p.is_eth?
  64. assert_kind_of(EthPacket,p)
  65. assert_kind_of(ARPPacket,p)
  66. assert p.is_arp?
  67. assert_equal(p.to_s, pcaps[5])
  68. assert_equal(1, p.arp_opcode.to_i)
  69. assert_equal("\x00\x01", p.headers.last[:arp_opcode].to_s)
  70. end
  71. def test_parse_arp_reply
  72. assert_equal(ARPPacket.layer, 2)
  73. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  74. p = Packet.parse(pcaps[6]) # Really ARP reply.
  75. assert_equal(p.to_s, pcaps[6])
  76. assert_equal(2, p.arp_opcode.to_i)
  77. assert_equal("\x00\x02", p.headers.last[:arp_opcode].to_s)
  78. end
  79. def test_parse_ip_packet
  80. assert_equal(IPPacket.layer, 2)
  81. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  82. p = Packet.parse(pcaps[0]) # Really DNS request
  83. assert_equal(p.to_s[0,20], pcaps[0][0,20])
  84. assert_equal(p.to_s, pcaps[0])
  85. assert_kind_of(EthPacket,p)
  86. assert_kind_of(IPPacket,p)
  87. end
  88. def test_parse_tcp_packet
  89. assert_equal(TCPPacket.layer, 3)
  90. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  91. p = Packet.parse(pcaps[7]) # Really FIN/ACK
  92. assert_equal(p.to_s, pcaps[7])
  93. assert_kind_of(EthPacket,p)
  94. assert_kind_of(IPPacket,p)
  95. assert_kind_of(TCPPacket,p)
  96. end
  97. def test_parse_udp_packet
  98. assert_equal(UDPPacket.layer, 3)
  99. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  100. p = Packet.parse(pcaps[0]) # Really DNS request
  101. assert_equal(p.to_s, pcaps[0])
  102. assert_kind_of(EthPacket,p)
  103. assert_kind_of(IPPacket,p)
  104. assert_kind_of(UDPPacket,p)
  105. end
  106. def test_parse_icmp_packet
  107. assert_equal(ICMPPacket.layer, 3)
  108. assert_equal(ICMPPacket.layer_symbol, :transport)
  109. pcaps = PcapFile.new.file_to_array(:f => 'sample.pcap')
  110. p = Packet.parse(pcaps[3]) # Really ICMP reply
  111. assert_equal(p.to_s, pcaps[3])
  112. assert_kind_of(EthPacket,p)
  113. assert_kind_of(IPPacket,p)
  114. assert_kind_of(ICMPPacket,p)
  115. end
  116. def test_parse_invalid_packet
  117. assert_equal(InvalidPacket.layer, 0)
  118. assert_equal(InvalidPacket.layer_symbol, :invalid)
  119. p = Packet.parse("\xff\xfe\x00\x01")
  120. assert_equal(p.to_s, "\xff\xfe\x00\x01")
  121. assert_kind_of(InvalidPacket,p)
  122. end
  123. def test_parse_ipv6_packet
  124. assert_equal(IPv6Packet.layer, 2)
  125. assert_equal(IPv6Packet.layer_symbol, :internet)
  126. pcaps = PcapFile.new.file_to_array(:f => 'sample-ipv6.pcap')
  127. p = Packet.parse(pcaps[0]) # Really an IPv6 packet
  128. assert_equal(p.to_s, pcaps[0])
  129. assert_kind_of(EthPacket,p)
  130. assert(!p.kind_of?(IPPacket), "Misidentified as an IP Packet!")
  131. assert_kind_of(IPv6Packet,p)
  132. end
  133. def test_parse_hsrp_packet
  134. assert_equal(HSRPPacket.layer, 4)
  135. assert_equal(HSRPPacket.layer_symbol, :application)
  136. pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
  137. p = Packet.parse(pcaps[0]) # Really an HSRP Hello packet
  138. assert_equal(p.to_s, pcaps[0])
  139. assert_kind_of(EthPacket,p)
  140. assert_kind_of(IPPacket,p)
  141. assert_kind_of(UDPPacket,p)
  142. assert_kind_of(HSRPPacket,p)
  143. end
  144. def test_parse_hsrp_as_udp
  145. assert_equal(:application, HSRPPacket.layer_symbol)
  146. pcaps = PcapFile.new.file_to_array(:f => 'sample_hsrp_pcapr.cap')
  147. p = Packet.parse(pcaps[0], :parse_app => false) # Really an HSRP Hello packet
  148. assert_kind_of(UDPPacket,p)
  149. assert(!p.kind_of?(HSRPPacket), "Misidentified HSRP packet when we didn't want it!" )
  150. end
  151. end
  152. # vim: nowrap sw=2 sts=0 ts=2 ff=unix ft=ruby