$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_Ethernet < Test::Unit::TestCase def test_unpack eth = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x12, 0x34, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") e = Libnet::Ethernet.decode(eth) assert_equal([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff].pack("C*"), e.dst) assert_equal([0x11, 0x22, 0x33, 0x44, 0x55, 0x66].pack("C*"), e.src) assert_equal(0x1234, e.type) assert_equal("this is the message", e.payload) assert_raise ArgumentError do Libnet::Ethernet.decode("foobar") end end def test_pack eth = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x12, 0x34, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") e = Libnet::Ethernet.new e.dst = 'aa:bb:cc:dd:ee:ff' e.src = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66].pack("C*") e.type = 0x1234 e.payload = "this is the message" l = Libnet.new assert_nil(e.ptag) l.build_ethernet(e) assert(e.ptag > 0) assert_equal(eth, l.pack) l = Libnet.new e = nil l.build_ethernet do |e| e.dst = 'aa:bb:cc:dd:ee:ff' e.src = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66].pack("C*") e.type = 0x1234 e.payload = "this is the message" end assert(e.ptag > 0) assert_equal(eth, l.pack) end def test_modify_ptag eth1 = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x12, 0x34, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") eth2 = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xab, 0xcd, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") l = Libnet.new e = l.build_ethernet do |e| e.dst = 'aa:bb:cc:dd:ee:ff' e.src = '11:22:33:44:55:66' e.type = 0x1234 e.payload = "this is the message" end assert_not_nil(e.ptag) ptag = e.ptag assert_equal(eth1, l.pack) e.type = 0xabcd l.build_ethernet(e) assert_equal(ptag, e.ptag) assert_equal(eth2, l.pack) end end