$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_Class < Test::Unit::TestCase def test_addr2name4 assert_respond_to(Libnet, :addr2name4) ip = 0xcdea6d12 assert_equal("rubyforge.org", Libnet.addr2name4(ip)) assert_equal("rubyforge.org", Libnet.addr2name4(ip, true)) assert_equal("205.234.109.18", Libnet.addr2name4(ip, false)) end # FIXME: I don't know of a valid IPv6 address to use here def test_addr2name6 assert_respond_to(Libnet, :addr2name6) ip = Libnet.ipv6_aton("fe80::20d:93ff:fe2f:3704") assert_equal("fe80::20d:93ff:fe2f:3704", Libnet.addr2name6(ip, false)) end def test_hex_aton assert_respond_to(Libnet, :hex_aton) bytes1 = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff].pack("C*") bytes2 = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab].pack("C*") assert_equal(bytes1, Libnet.hex_aton("aa:bb:cc:dd:ee:ff")) assert_equal(bytes2, Libnet.hex_aton("01:23:45:67:89:ab")) assert_raise ArgumentError do Libnet.hex_aton("aa:bb:cc:dd:ee:fg") end end def test_ipv4_aton assert_respond_to(Libnet, :ipv4_aton) assert_equal(0xcdea6d12, Libnet.ipv4_aton("205.234.109.18")) # check for invalid ip address assert_raise ArgumentError do Libnet.ipv4_aton("256.234.109.18") end assert_raise ArgumentError do Libnet.ipv4_aton("205.234.109") end assert_raise ArgumentError do Libnet.ipv4_aton("205.234.109.ab") end end def test_ipv4_ntoa assert_respond_to(Libnet, :ipv4_ntoa) assert_equal("205.234.109.18", Libnet.ipv4_ntoa(0xcdea6d12)) end def test_ipv6_aton ip6_1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack("C*") ip6_2 = [ 0x80, 0x5b, 0x2d, 0x9d, 0xdc, 0x28, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x57, 0xd4, 0xc8, 0x1f, 0xff ].pack("C*") assert_respond_to(Libnet, :ipv6_aton) assert_equal(ip6_1, Libnet.ipv6_aton("::")) assert_equal(ip6_2, Libnet.ipv6_aton("805b:2d9d:dc28::fc57:d4c8:1fff")) # check for invalid ip address assert_raise ArgumentError do Libnet.ipv6_aton("1234") end assert_raise ArgumentError do Libnet.ipv6_aton("1234::4321:abcd::dcba") end end def test_ipv6_ntoa ip6_1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack("C*") ip6_2 = [ 0x80, 0x5b, 0x2d, 0x9d, 0xdc, 0x28, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x57, 0xd4, 0xc8, 0x1f, 0xff ].pack("C*") assert_respond_to(Libnet, :ipv6_ntoa) assert_equal("::", Libnet.ipv6_ntoa(ip6_1)) assert_equal("805b:2d9d:dc28::fc57:d4c8:1fff", Libnet.ipv6_ntoa(ip6_2)) assert_raise ArgumentError do Libnet.hexdump(Libnet.ipv6_ntoa("foobar")) end end def test_hexdump assert_respond_to(Libnet, :hexdump) bytes = [ 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xcb ].pack("C*") dump = <<_END_ 0x00000000: deadbeef feedface 00112233 44556677 0x00000010: 8899aabb ccddeeff cb _END_ assert_equal(dump, Libnet.hexdump(bytes)) end def test_write l = Libnet.new(:link) 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 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 if Process.uid > 0 && Process.euid > 0 # user is not root, so we should get an exception here assert_raise RuntimeError do l.write end else # user is root, so this should work assert_equal(42, l.write) end end def test_write_bytes 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*") ip4 = [ 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 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*") ip6 = [ 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*") l = Libnet.new(:link) if Process.uid > 0 && Process.euid > 0 # user is not root, so we should get an exception here assert_raise RuntimeError do l.write_bytes(eth) end else # user is root, so this should work n = l.write_bytes(eth) assert_equal(eth.length, n) end l = Libnet.new(:raw4) if Process.uid > 0 && Process.euid > 0 # user is not root, so we should get an exception here assert_raise RuntimeError do l.write_bytes(ip4) end else # user is root, so this should work n = l.write_bytes(ip4) assert_equal(ip4.length, n) end l = Libnet.new(:raw6) if Process.uid > 0 && Process.euid > 0 # user is not root, so we should get an exception here assert_raise RuntimeError do l.write_bytes(ip6) end else # FIXME: I keep getting "no route to host" errors here, not sure how to # fix this yet # # user is root, so this should work # n =l.write_bytes(ip6) # assert_equal(ip6.length, n) end end end