$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_ARP < Test::Unit::TestCase def test_unpack bytes = [ 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xc0, 0xa8, 0x01, 0x64, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xc0, 0xa8, 0x01, 0x7b ].pack("C*") a = Libnet::ARP.decode(bytes) assert_equal(0x0001, a.htype) assert_equal(0x0800, a.protocol) assert_equal(6, a.hlen) assert_equal(4, a.plen) assert_equal(1, a.operation) assert_equal([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff].pack("C*"), a.sender_haddr) assert_equal([0xc0, 0xa8, 0x01, 0x64].pack("C*"), a.sender_paddr) assert_equal([0x11, 0x22, 0x33, 0x44, 0x55, 0x66].pack("C*"), a.target_haddr) assert_equal([0xc0, 0xa8, 0x01, 0x7b].pack("C*"), a.target_paddr) assert_nil(a.payload) assert_raise ArgumentError do Libnet::ARP.decode("foobar") end end def test_pack exp_bytes = [ 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xc0, 0xa8, 0x01, 0x64, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xc0, 0xa8, 0x01, 0x7b ].pack("C*") # build the ARP header object separately a = Libnet::ARP.new a.htype = 1 a.protocol = 0x0800 a.hlen = 6 a.plen = 4 a.operation = 1 a.sender_haddr = 'aa:bb:cc:dd:ee:ff' a.sender_paddr = '192.168.1.100' a.target_haddr = '11:22:33:44:55:66' a.target_paddr = '192.168.1.123' l = Libnet.new assert_nil(a.ptag) l.build_arp(a) assert(a.ptag > 0) assert_equal(exp_bytes, l.pack) # build the ARP header as a yielded object l = Libnet.new l.build_arp do |a| a.htype = 1 a.protocol = 0x0800 a.hlen = 6 a.plen = 4 a.operation = 1 a.sender_haddr = 'aa:bb:cc:dd:ee:ff' a.sender_paddr = '192.168.1.100' a.target_haddr = '11:22:33:44:55:66' a.target_paddr = '192.168.1.123' end assert(a.ptag > 0) assert_equal(exp_bytes, l.pack) end def test_modify_ptag arp1 = [ 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xc0, 0xa8, 0x01, 0x64, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xc0, 0xa8, 0x01, 0x7b ].pack("C*") arp2 = [ 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xc0, 0xa8, 0x01, 0x64, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xc0, 0xa8, 0x01, 0x7b ].pack("C*") l = Libnet.new a = l.build_arp do |a| a.htype = 1 a.protocol = 0x0800 a.hlen = 6 a.plen = 4 a.operation = 1 a.sender_haddr = 'aa:bb:cc:dd:ee:ff' a.sender_paddr = '192.168.1.100' a.target_haddr = '11:22:33:44:55:66' a.target_paddr = '192.168.1.123' end assert(a.ptag > 0) ptag = a.ptag assert_equal(arp1, l.pack) a.operation = 2 l.build_arp(a) assert_equal(ptag, a.ptag) assert_equal(arp2, l.pack) end def test_missing_fields l = Libnet.new assert_raise ArgumentError do l.build_arp { |e| } end assert_raise ArgumentError do l.build_arp do |a| a.sender_haddr = 'aa:bb:cc:dd:ee:ff' a.target_haddr = '11:22:33:44:55:66' end end end def test_frame exp_bytes = [ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0x22, 0x33, 0x22, 0x33, 0x22, 0x33, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xc0, 0xa8, 0x01, 0x64, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xc0, 0xa8, 0x01, 0x7b ].pack("C*") l = Libnet.new(:link) a = l.build_arp do |a| a.htype = 1 a.protocol = 0x0800 a.hlen = 6 a.plen = 4 a.operation = 1 a.sender_haddr = 'aa:bb:cc:dd:ee:ff' a.sender_paddr = '192.168.1.100' a.target_haddr = '11:22:33:44:55:66' a.target_paddr = '192.168.1.123' end e = l.build_ethernet do |e| e.dst = '12:34:56:78:90:ab' e.src = '22:33:22:33:22:33' e.type = 0x0806 end assert_equal(exp_bytes, l.pack) end end