require 'test/unit' require 'lib/whois' require 'ipaddr' class TestWhois < Test::Unit::TestCase def test_ipv4_string ip_with_server '72.14.207.99', Server::Arin end def test_rescue ip_with_server '42.14.221.147', Server::Arin ip_with_server '41.14.221.147', Server::Afrinic #ip_with_server '218.14.221.147', Server::Apnic #=> It's random, because Apnic has 2 nodes ip_with_server '61.80.221.147', Server::Nicor ip_with_server '194.14.221.147', Server::Ripe ip_with_server '216.14.221.147', Server::Arin #ip_with_server '200.14.221.147', Server::Lacnic #=> it's random because a hour of request is renseign end def test_second_example ip = IPAddr.new '72.17.207.99' w = Whois::Whois.new ip w.search_whois # All return of request assert_equal((request_whois_unix "72.17.207.99"), w.all) # The ip return with object IPAddr assert_equal(IPAddr.new('72.17.207.99'), w.ip) # The server where the request has send assert_instance_of(Server::Arin, w.server) # The host of this IPv4 assert_nil w.host end def test_third_example w = Whois::Whois.new '72.14.221.147', true w.search_whois # All return of request assert_equal((request_whois_unix "72.14.221.147"), w.all) # The ip return with object IPAddr assert_equal((IPAddr.new '72.14.221.147'), w.ip) # The server where the request has send assert_instance_of Server::Arin, w.server # The host of this IPv4 assert_equal(request_host_with_ip('72.14.221.147'), w.host) end def test_four_example host = 'fg-in-f147.google.com' w = Whois::Whois.new host w.search_whois # The ip return with object IPAddr assert_equal(request_ip_with_host(host), w.ip) assert_equal((request_whois_unix w.ip), w.all) # The server where the request has send assert_instance_of Server::Arin, w.server # The host of this host assert_equal 'fg-in-f147.google.com', w.host end def test_fifth_example ip_with_server '2001:0660::', Server::Ripe ip_with_server '2001:4200:a42e:2131', Server::Ripe, false, true end def test_Ipaddr_for_ipv6 ip = IPAddr.new '2001:0660::' w = Whois::Whois.new ip w.search_whois # All return of request assert_equal((request_whois_unix "2001:0660::"), w.all) # The ip return with object IPAddr assert_equal(IPAddr.new('2001:0660::'), w.ip) # The server where the request has send assert_instance_of(Server::Ripe, w.server) # The host of this IPv4 assert_nil w.host end def test_Ipv6_not_host ip = IPAddr.new 'a13e:1678:129b:7341' assert_raise Whois::WhoisException do w = Whois::Whois.new ip end end private def request_host_with_ip(ip) line = IO.popen("host #{ip}") line = line.gets if line =~ /(.+)domain name pointer (.+)\./ Regexp.last_match 2 end end def request_ip_with_host(host) line = IO.popen("host #{host}") line = line.gets if line =~ /(.+)has address (.+)/ IPAddr.new(Regexp.last_match(2)) end end def request_whois_unix(ip) result = '' IO.popen("whois #{ip}").each do |l| # not good but use because ServerReferral use if l =~ /^Renvoi/ result = result.chop().chop() break end result += l end result end def ip_with_server(ip, server, read=false, except=false) w = nil unless except assert_nothing_raised do w = Whois::Whois.new ip end else assert_raise Whois::WhoisException do Whois::Whois.new(ip) end return end w.search_whois if read puts w.server puts(request_whois_unix(ip)) puts w.all puts w.ip end assert_equal((request_whois_unix ip).strip().gsub(/[\s]/, ''), w.all.strip().gsub(/[\s]/, '')) assert_equal(IPAddr.new(ip), w.ip) assert_instance_of(server, w.server) assert_nil w.host end end