Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion geoip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
212 changes: 115 additions & 97 deletions test.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand All @@ -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
Binary file added test/fixtures/asnum.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/asnum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0, 167772159, AS12345
184549376, 2130706431, AS12345
2147483648, 2851995647, AS12345
2852061184, 2886729727, AS12345
2887778304, 3232235519, AS12345
3232301056, 4294967294, AS12345
Binary file added test/fixtures/city.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/city.txt
Original file line number Diff line number Diff line change
@@ -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"
Binary file added test/fixtures/country.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/country.txt
Original file line number Diff line number Diff line change
@@ -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
Binary file added test/fixtures/isp.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/isp.txt
Original file line number Diff line number Diff line change
@@ -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"
Binary file added test/fixtures/netspeed.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/netspeed.txt
Original file line number Diff line number Diff line change
@@ -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
Binary file added test/fixtures/org.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/org.txt
Original file line number Diff line number Diff line change
@@ -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"
Binary file added test/fixtures/region.dat
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/region.txt
Original file line number Diff line number Diff line change
@@ -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