$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_VLAN < Test::Unit::TestCase def test_unpack vlan = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x08, 0x00, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") v = Libnet::VLAN.decode(vlan) assert_equal([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff].pack("C*"), v.dst) assert_equal([0x11, 0x22, 0x33, 0x44, 0x55, 0x66].pack("C*"), v.src) assert_equal(0xbeef, v.tpi) assert_equal(6, v.priority) assert_equal(0, v.cfi) assert_equal(2730, v.id) assert_equal(0x0800, v.len) assert_equal(0x0800, v.type) assert_equal("this is the message", v.payload) assert_raise ArgumentError do Libnet::VLAN.decode("foobar") end end def test_pack vlan = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x08, 0x00, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") v = Libnet::VLAN.new v.dst = 'aa:bb:cc:dd:ee:ff' v.src = '11:22:33:44:55:66' v.tpi = 0xbeef v.priority = 6 v.cfi = 0 v.id = 2730 v.type = 0x0800 v.payload = "this is the message" l = Libnet.new assert_nil(v.ptag) l.build_vlan(v) assert(v.ptag > 0) assert(vlan, l.pack) l = Libnet.new v = nil v = l.build_vlan do |v| v.dst = 'aa:bb:cc:dd:ee:ff' v.src = '11:22:33:44:55:66' v.tpi = 0xbeef v.priority = 6 v.cfi = 0 v.id = 2730 v.len = 0x0800 v.payload = "this is the message" end assert(v.ptag > 0) assert_equal(vlan, l.pack) end def test_modify_ptag vlan1 = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x08, 0x00, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") vlan2 = [ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x08, 0x88, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 ].pack("C*") l = Libnet.new v = l.build_vlan do |v| v.dst = 'aa:bb:cc:dd:ee:ff' v.src = '11:22:33:44:55:66' v.tpi = 0xbeef v.priority = 6 v.cfi = 0 v.id = 2730 v.len = 0x0800 v.payload = "this is the message" end assert_equal(vlan1, l.pack) assert_not_nil(v.ptag) ptag = v.ptag v.type = 0x0888 l.build_vlan(v) assert_equal(ptag, v.ptag) assert_equal(vlan2, l.pack) end end