$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_IPv6 < Test::Unit::TestCase def test_unpack bytes = [ 0x62, 0x2d, 0xbe, 0xef, 0x00, 0x2c, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") dst_ip = [ 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" i = Libnet::IPv6.decode(bytes) assert_equal(6, i.version) assert_equal(0x22, i.traffic_class) assert_equal(0xdbeef, i.flow_label) assert_equal(44, i.length) assert_equal(Libnet::IPPROTO_UDP, i.next_header) assert_equal(24, i.hop_limit) assert_equal(src_ip, i.src_ip) assert_equal(dst_ip, i.dst_ip) assert_equal(payload, i.payload) assert_raise ArgumentError do Libnet::IPv6.decode("foobar") end end def test_pack bytes = [ 0x62, 0x2d, 0xbe, 0xef, 0x00, 0x54, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" i = Libnet::IPv6.new i.traffic_class = 0x22 i.flow_label = 0xdbeef i.length = Libnet::HL_IPV6 + payload.length i.next_header = Libnet::IPPROTO_UDP i.hop_limit = 24 i.src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") i.dst_ip = "aabb:aabb:ccdd:ccdd:eeff:eeff:2233:2233" i.payload = payload assert_nil(i.ptag) l = Libnet.new l.build_ipv6(i) assert(i.ptag > 0) assert_equal(bytes, l.pack) l = Libnet.new i = nil l.build_ipv6 do |i| i.traffic_class = 0x22 i.flow_label = 0xdbeef i.length = Libnet::HL_IPV6 + payload.length i.next_header = Libnet::IPPROTO_UDP i.hop_limit = 24 i.src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") i.dst_ip = "aabb:aabb:ccdd:ccdd:eeff:eeff:2233:2233" i.payload = payload end assert(i.ptag > 0) assert_equal(bytes, l.pack) end def test_modify_ptag bytes1 = [ 0x62, 0x2d, 0xbe, 0xef, 0x00, 0x54, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") bytes2 = [ 0x62, 0x2a, 0xde, 0xad, 0x00, 0x54, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new i = l.build_ipv6 do |i| i.traffic_class = 0x22 i.flow_label = 0xdbeef i.length = Libnet::HL_IPV6 + payload.length i.next_header = Libnet::IPPROTO_UDP i.hop_limit = 24 i.src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") i.dst_ip = "aabb:aabb:ccdd:ccdd:eeff:eeff:2233:2233" i.payload = payload end assert_equal(bytes1, l.pack) assert(i.ptag > 0) ptag = i.ptag i.flow_label = 0xadead l.build_ipv6(i) assert_equal(bytes2, l.pack) assert(ptag, i.ptag) end def test_ethernet bytes = [ # ethernet 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x86, 0xdd, # ipv6 0x62, 0x2d, 0xbe, 0xef, 0x00, 0x54, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new l.build_ipv6 do |i| i.traffic_class = 0x22 i.flow_label = 0xdbeef i.length = Libnet::HL_IPV6 + payload.length i.next_header = Libnet::IPPROTO_UDP i.hop_limit = 24 i.src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") i.dst_ip = "aabb:aabb:ccdd:ccdd:eeff:eeff:2233:2233" i.payload = payload end l.build_ethernet do |e| e.dst = 'aa:bb:cc:dd:ee:ff' e.src = '11:22:33:44:55:66' e.type = 0x86dd end assert_equal(bytes, l.pack) end def test_vlan bytes = [ # vlan 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x86, 0xdd, # ipv6 0x62, 0x2d, 0xbe, 0xef, 0x00, 0x54, 0x11, 0x18, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc, 0xdd, 0xcc, 0xdd, 0xee, 0xff, 0xee, 0xff, 0x22, 0x33, 0x22, 0x33, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new l.build_ipv6 do |i| i.traffic_class = 0x22 i.flow_label = 0xdbeef i.length = Libnet::HL_IPV6 + payload.length i.next_header = Libnet::IPPROTO_UDP i.hop_limit = 24 i.src_ip = [ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff ].pack("C*") i.dst_ip = "aabb:aabb:ccdd:ccdd:eeff:eeff:2233:2233" i.payload = payload end 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.type = 0x86dd end assert_equal(bytes, l.pack) end end