diff --git a/geoip.c b/geoip.c index 085f6e1..d2d7bc0 100644 --- a/geoip.c +++ b/geoip.c @@ -117,6 +117,7 @@ static VALUE generic_single_value_lookup_response(char *key, char *value) VALUE rb_city_record_to_hash(GeoIPRecord *record) { VALUE hash = rb_hash_new(); + const char *region_name; if(record->country_code) rb_hash_sset(hash, "country_code", encode_to_utf8_and_return_rb_str(record->country_code)); @@ -126,7 +127,9 @@ VALUE rb_city_record_to_hash(GeoIPRecord *record) rb_hash_sset(hash, "country_name", encode_to_utf8_and_return_rb_str(record->country_name)); if(record->region) { rb_hash_sset(hash, "region", encode_to_utf8_and_return_rb_str(record->region)); - rb_hash_sset(hash, "region_name", encode_to_utf8_and_return_rb_str(GeoIP_region_name_by_code(record->country_code, record->region))); + + if (region_name = GeoIP_region_name_by_code(record->country_code, record->region)) + rb_hash_sset(hash, "region_name", encode_to_utf8_and_return_rb_str((char*)region_name)); } if(record->city) rb_hash_sset(hash, "city", encode_to_utf8_and_return_rb_str(record->city)); diff --git a/test.rb b/test.rb index 31938c8..510cc37 100644 --- a/test.rb +++ b/test.rb @@ -1,11 +1,11 @@ +# encoding: utf-8 + require 'test/unit' require File.dirname(__FILE__) + '/geoip' require 'rubygems' -# require 'ruby-debug' -# Debugger.start -CITY_DB = ENV.fetch("CITY", '/usr/local/GeoIP/share/GeoIP/GeoLiteCity.dat') -ORG_DB = ENV.fetch("ORG", '/usr/local/GeoIP/share/GeoIP/GeoIPOrg.dat') +CITY_DB = ENV.fetch("CITY", File.expand_path('test/fixtures/city.dat')) +ORG_DB = ENV.fetch("ORG", File.expand_path('test/fixtures/org.dat')) class Test::Unit::TestCase @@ -18,45 +18,45 @@ def assert_look_up(db, addr, field, value) end class GeoIPTest < Test::Unit::TestCase - + def setup @ip = "24.24.24.24" @ipnum = 16777216*24 + 65536*24 + 256*24 + 24 - + @large_ip = "245.245.245.245" @large_ipnum = 16777216*245 + 65536*245 + 256*245 + 245 end - + # addr_to_num - + def test_addr_to_num_converts_an_ip_to_an_ipnum assert_equal @ipnum, GeoIP.addr_to_num(@ip) end - + def test_addr_to_num_converts_large_ips_to_an_ipnum_correctly assert_equal @large_ipnum, GeoIP.addr_to_num(@large_ip) end - + def test_addr_to_num_expects_an_ip_string - assert_raises TypeError do - GeoIP.addr_to_num(nil) + assert_raises TypeError do + GeoIP.addr_to_num(nil) end end - + def test_addr_to_num_returns_zero_for_an_illformed_ip_string assert_equal 0, GeoIP.addr_to_num("foo.bar") end - + # num_to_addr - + def test_num_to_addr_converts_an_ipnum_to_an_ip assert_equal @ip, GeoIP.num_to_addr(@ipnum) end - + def test_num_to_addr_converts_large_ipnums_to_an_ip_correctly assert_equal @large_ip, GeoIP.num_to_addr(@large_ipnum) end - + def test_num_to_addr_expects_a_numeric_ip assert_raises TypeError do GeoIP.num_to_addr(nil) @@ -65,106 +65,124 @@ def test_num_to_addr_expects_a_numeric_ip GeoIP.num_to_addr("foo.bar") end end - + end -class GeoIPCityTest < Test::Unit::TestCase - - def setup - ## Change me! - @dbfile = CITY_DB - end +unless File.exist?(CITY_DB) + puts "No City DB found, skipping GeoIP::City tests" + puts "* Set CITY=/path/to/GeoIPCity.dat to run tests" +else + class GeoIPCityTest < Test::Unit::TestCase - def test_construction_default - db = GeoIP::City.new(@dbfile) - - assert_raises TypeError do - db.look_up(nil) - end - - h = db.look_up('24.24.24.24') - #debugger - assert_kind_of Hash, h - assert_equal 'New York', h[:city] - assert_equal 'United States', h[:country_name] - end + def setup + ## Change me! + @dbfile = CITY_DB + end - def test_construction_index - db = GeoIP::City.new(@dbfile, :index) - assert_look_up(db, '24.24.24.24', :city, 'New York') - end + def test_construction_default + db = GeoIP::City.new(@dbfile) - def test_construction_filesystem - db = GeoIP::City.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :city, 'New York') - end + assert_raises TypeError do + db.look_up(nil) + end - def test_construction_memory - db = GeoIP::City.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :city, 'New York') - end + h = db.look_up('24.24.24.24') + assert_kind_of Hash, h + assert_equal 'New York', h[:city] + assert_equal 'United States', h[:country_name] + end - def test_construction_filesystem_check - db = GeoIP::City.new(@dbfile, :filesystem, true) - assert_look_up(db, '24.24.24.24', :city, 'New York') - end + def test_construction_index + db = GeoIP::City.new(@dbfile, :index) + assert_look_up(db, '24.24.24.24', :city, 'New York') + end - def test_bad_db_file - assert_raises Errno::ENOENT do - GeoIP::City.new('/supposed-to-fail') + def test_construction_filesystem + db = GeoIP::City.new(@dbfile, :filesystem) + assert_look_up(db, '24.24.24.24', :city, 'New York') end - end - def test_character_encoding_converted_to_utf8_first - db = GeoIP::City.new(@dbfile, :filesystem, true) - assert_look_up(db, '201.85.50.148', :city, 'São Paulo') - end + def test_construction_memory + db = GeoIP::City.new(@dbfile, :memory) + assert_look_up(db, '24.24.24.24', :city, 'New York') + end -end + def test_construction_filesystem_check + db = GeoIP::City.new(@dbfile, :filesystem, true) + assert_look_up(db, '24.24.24.24', :city, 'New York') + end -class GeoIPOrgTest < Test::Unit::TestCase - - def setup - ## Change me! - @dbfile = ORG_DB - end + def test_bad_db_file + assert_raises Errno::ENOENT do + GeoIP::City.new('/supposed-to-fail') + end + end - def test_construction_default - db = GeoIP::Organization.new(@dbfile) - - assert_raises TypeError do - db.look_up(nil) + def test_character_encoding_converted_to_utf8_first + db = GeoIP::City.new(@dbfile, :filesystem, true) + assert_look_up(db, '201.85.50.148', :city, 'São Paulo') end - - h = db.look_up('24.24.24.24') - assert_kind_of Hash, h - assert_equal 'Road Runner', h[:name] - end - def test_construction_index - db = GeoIP::Organization.new(@dbfile, :index) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end + def test_blank_region_skips_region_name_set + db = GeoIP::City.new(@dbfile, :filesystem, true) + ip = '116.48.136.76' + assert_look_up(db, ip, :region, nil) + assert_look_up(db, ip, :region_name, nil) + assert_look_up(db, ip, :city, 'Central District') + assert_look_up(db, ip, :country_name, 'Hong Kong') + end - def test_construction_filesystem - db = GeoIP::Organization.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') end +end - def test_construction_memory - db = GeoIP::Organization.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end +unless File.exist?(ORG_DB) + puts "No Org DB found, skipping GeoIP::Organization tests" + puts "* Set ORG=/path/to/GeoIPORG.dat to run tests" +else + class GeoIPOrgTest < Test::Unit::TestCase - def test_construction_filesystem_check - db = GeoIP::Organization.new(@dbfile, :filesystem, true) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end + def setup + ## Change me! + @dbfile = ORG_DB + end + + def test_construction_default + db = GeoIP::Organization.new(@dbfile) - def test_bad_db_file - assert_raises Errno::ENOENT do - GeoIP::Organization.new('/supposed-to-fail') + assert_raises TypeError do + db.look_up(nil) + end + + h = db.look_up('24.24.24.24') + assert_kind_of Hash, h + assert_equal 'org test 123 org', h[:name] + end + + def test_construction_index + db = GeoIP::Organization.new(@dbfile, :index) + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') end - end + def test_construction_filesystem + db = GeoIP::Organization.new(@dbfile, :filesystem) + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') + end + + def test_construction_memory + db = GeoIP::Organization.new(@dbfile, :memory) + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') + end + + def test_construction_filesystem_check + db = GeoIP::Organization.new(@dbfile, :filesystem, true) + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') + end + + def test_bad_db_file + assert_raises Errno::ENOENT do + GeoIP::Organization.new('/supposed-to-fail') + end + end + + end end diff --git a/test/fixtures/asnum.dat b/test/fixtures/asnum.dat new file mode 100644 index 0000000..d401e1b Binary files /dev/null and b/test/fixtures/asnum.dat differ diff --git a/test/fixtures/asnum.txt b/test/fixtures/asnum.txt new file mode 100644 index 0000000..0bd9353 --- /dev/null +++ b/test/fixtures/asnum.txt @@ -0,0 +1,6 @@ +0, 167772159, AS12345 +184549376, 2130706431, AS12345 +2147483648, 2851995647, AS12345 +2852061184, 2886729727, AS12345 +2887778304, 3232235519, AS12345 +3232301056, 4294967294, AS12345 diff --git a/test/fixtures/city.dat b/test/fixtures/city.dat new file mode 100644 index 0000000..1a280ee Binary files /dev/null and b/test/fixtures/city.dat differ diff --git a/test/fixtures/city.txt b/test/fixtures/city.txt new file mode 100644 index 0000000..ec5d5d4 --- /dev/null +++ b/test/fixtures/city.txt @@ -0,0 +1,6 @@ +"0","167772159","US","NY","New York","5.4321","-25.2015","1005","212","123" +"184549376","2130706431","US","NY","New York","5.4321","-25.2015","1005","212","123" +"2147483648","2851995647","US","NY","New York","5.4321","-25.2015","1005","212","123" +"2852061184","2886729727","US","NY","New York","5.4321","-25.2015","1005","212","123" +"2887778304","3232235519","US","NY","New York","5.4321","-25.2015","1005","212","123" +"3232301056","4294967294","US","NY","New York","5.4321","-25.2015","1005","212","123" diff --git a/test/fixtures/country.dat b/test/fixtures/country.dat new file mode 100644 index 0000000..4a37f00 Binary files /dev/null and b/test/fixtures/country.dat differ diff --git a/test/fixtures/country.txt b/test/fixtures/country.txt new file mode 100644 index 0000000..87dc980 --- /dev/null +++ b/test/fixtures/country.txt @@ -0,0 +1,6 @@ +0.0.0.0, 9.255.255.255, 0, 167772159, US +11.0.0.0, 126.255.255.255, 184549376, 2130706431, US +128.0.0.0, 169.253.255.255, 2147483648, 2851995647, US +169.255.0.0, 172.15.255.255, 2852061184, 2886729727, US +172.32.0.0, 192.167.255.255, 2887778304, 3232235519, US +192.169.0.0, 255.255.255.254, 3232301056, 4294967294, US diff --git a/test/fixtures/isp.dat b/test/fixtures/isp.dat new file mode 100644 index 0000000..0704dd1 Binary files /dev/null and b/test/fixtures/isp.dat differ diff --git a/test/fixtures/isp.txt b/test/fixtures/isp.txt new file mode 100644 index 0000000..acccbe1 --- /dev/null +++ b/test/fixtures/isp.txt @@ -0,0 +1,6 @@ +0,167772159,0.0.0.0,9.255.255.255,"A Company" +184549376,2130706431,11.0.0.0,126.255.255.255,"A Company" +2147483648,2851995647,128.0.0.0,169.253.255.255,"A Company" +2852061184,2886729727,169.255.0.0,172.15.255.255,"A Company" +2887778304,3232235519,172.32.0.0,192.167.255.255,"A Company" +3232301056,4294967294,192.169.0.0,255.255.255.254,"A Company" diff --git a/test/fixtures/netspeed.dat b/test/fixtures/netspeed.dat new file mode 100644 index 0000000..1666fa4 Binary files /dev/null and b/test/fixtures/netspeed.dat differ diff --git a/test/fixtures/netspeed.txt b/test/fixtures/netspeed.txt new file mode 100644 index 0000000..e26c265 --- /dev/null +++ b/test/fixtures/netspeed.txt @@ -0,0 +1,6 @@ +0,167772159,0.0.0.0,9.255.255.255,2 +184549376,2130706431,11.0.0.0,126.255.255.255,2 +2147483648,2851995647,128.0.0.0,169.253.255.255,2 +2852061184,2886729727,169.255.0.0,172.15.255.255,2 +2887778304,3232235519,172.32.0.0,192.167.255.255,2 +3232301056,4294967294,192.169.0.0,255.255.255.254,2 diff --git a/test/fixtures/org.dat b/test/fixtures/org.dat new file mode 100644 index 0000000..bae95fc Binary files /dev/null and b/test/fixtures/org.dat differ diff --git a/test/fixtures/org.txt b/test/fixtures/org.txt new file mode 100644 index 0000000..32163bf --- /dev/null +++ b/test/fixtures/org.txt @@ -0,0 +1,6 @@ +0,167772159,"org test 123 org" +184549376,2130706431,"org test 123 org" +2147483648,2851995647,"org test 123 org" +2852061184,2886729727,"org test 123 org" +2887778304,3232235519,"org test 123 org" +3232301056,4294967294,"org test 123 org" diff --git a/test/fixtures/region.dat b/test/fixtures/region.dat new file mode 100644 index 0000000..41e7b24 Binary files /dev/null and b/test/fixtures/region.dat differ diff --git a/test/fixtures/region.txt b/test/fixtures/region.txt new file mode 100644 index 0000000..5509e68 --- /dev/null +++ b/test/fixtures/region.txt @@ -0,0 +1,6 @@ +0,167772159,0.0.0.0,9.255.255.255,US,NY +184549376,2130706431,11.0.0.0,126.255.255.255,US,NY +2147483648,2851995647,128.0.0.0,169.253.255.255,US,NY +2852061184,2886729727,169.255.0.0,172.15.255.255,US,NY +2887778304,3232235519,172.32.0.0,192.167.255.255,US,NY +3232301056,4294967294,192.169.0.0,255.255.255.254,US,NY