From fb3a535f57987b978abc48e5f4a624ce68ddac8c Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 16:49:48 +0200 Subject: [PATCH 1/7] :punch: whitespace --- test.rb | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/test.rb b/test.rb index 31938c8..b977c32 100644 --- a/test.rb +++ b/test.rb @@ -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,11 +65,11 @@ 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 @@ -77,11 +77,11 @@ def setup def test_construction_default db = GeoIP::City.new(@dbfile) - - assert_raises TypeError do - db.look_up(nil) + + assert_raises TypeError do + db.look_up(nil) end - + h = db.look_up('24.24.24.24') #debugger assert_kind_of Hash, h @@ -123,7 +123,7 @@ def test_character_encoding_converted_to_utf8_first end class GeoIPOrgTest < Test::Unit::TestCase - + def setup ## Change me! @dbfile = ORG_DB @@ -131,11 +131,11 @@ def setup def test_construction_default db = GeoIP::Organization.new(@dbfile) - - assert_raises TypeError do - db.look_up(nil) + + assert_raises TypeError do + db.look_up(nil) end - + h = db.look_up('24.24.24.24') assert_kind_of Hash, h assert_equal 'Road Runner', h[:name] From 1d2ddb2cc681a83cabd9c1469e7d19d5f50e3786 Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 16:59:41 +0200 Subject: [PATCH 2/7] Remove debuggers --- test.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/test.rb b/test.rb index b977c32..353e4df 100644 --- a/test.rb +++ b/test.rb @@ -1,8 +1,6 @@ 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') @@ -83,7 +81,6 @@ def test_construction_default 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] From a13be09da514ecfdcd2a49e8165e24578c8a0974 Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 17:06:57 +0200 Subject: [PATCH 3/7] Skip tests unless DB exists, warn tester --- test.rb | 154 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 82 insertions(+), 72 deletions(-) diff --git a/test.rb b/test.rb index 353e4df..bd0045d 100644 --- a/test.rb +++ b/test.rb @@ -66,102 +66,112 @@ def test_num_to_addr_expects_a_numeric_ip end -class GeoIPCityTest < Test::Unit::TestCase +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 setup + ## Change me! + @dbfile = CITY_DB + end - def setup - ## Change me! - @dbfile = CITY_DB - end + def test_construction_default + db = GeoIP::City.new(@dbfile) - def test_construction_default - db = GeoIP::City.new(@dbfile) + assert_raises TypeError do + db.look_up(nil) + end - assert_raises TypeError do - db.look_up(nil) + 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 - 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_index + db = GeoIP::City.new(@dbfile, :index) + 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_construction_filesystem + db = GeoIP::City.new(@dbfile, :filesystem) + assert_look_up(db, '24.24.24.24', :city, 'New York') + end - def test_construction_filesystem - db = GeoIP::City.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :city, 'New York') - end + def test_construction_memory + db = GeoIP::City.new(@dbfile, :memory) + assert_look_up(db, '24.24.24.24', :city, 'New York') + end - def test_construction_memory - db = GeoIP::City.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :city, 'New York') - 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_filesystem_check - db = GeoIP::City.new(@dbfile, :filesystem, true) - 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') + end + end - def test_bad_db_file - assert_raises Errno::ENOENT do - GeoIP::City.new('/supposed-to-fail') + 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 - 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 - end -class GeoIPOrgTest < Test::Unit::TestCase +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 setup - ## Change me! - @dbfile = ORG_DB - end + def setup + ## Change me! + @dbfile = ORG_DB + end - def test_construction_default - db = GeoIP::Organization.new(@dbfile) + def test_construction_default + db = GeoIP::Organization.new(@dbfile) - assert_raises TypeError do - db.look_up(nil) - end + assert_raises TypeError do + db.look_up(nil) + end - h = db.look_up('24.24.24.24') - assert_kind_of Hash, h - assert_equal 'Road Runner', h[:name] - 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_construction_index + db = GeoIP::Organization.new(@dbfile, :index) + assert_look_up(db, '24.24.24.24', :name, 'Road Runner') + end - def test_construction_filesystem - db = GeoIP::Organization.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end + def test_construction_filesystem + db = GeoIP::Organization.new(@dbfile, :filesystem) + assert_look_up(db, '24.24.24.24', :name, 'Road Runner') + end - def test_construction_memory - db = GeoIP::Organization.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end + def test_construction_memory + db = GeoIP::Organization.new(@dbfile, :memory) + assert_look_up(db, '24.24.24.24', :name, 'Road Runner') + end - 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 test_construction_filesystem_check + db = GeoIP::Organization.new(@dbfile, :filesystem, true) + assert_look_up(db, '24.24.24.24', :name, 'Road Runner') + end - def test_bad_db_file - assert_raises Errno::ENOENT do - GeoIP::Organization.new('/supposed-to-fail') + def test_bad_db_file + assert_raises Errno::ENOENT do + GeoIP::Organization.new('/supposed-to-fail') + end end - end + end end From f6262c3bc407129f8975c7a4676e64643fc83e96 Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 17:23:33 +0200 Subject: [PATCH 4/7] Use MaxMind-supplied test DBs --- test.rb | 14 +++++++------- test/fixtures/asnum.dat | Bin 0 -> 503 bytes test/fixtures/asnum.txt | 6 ++++++ test/fixtures/city.dat | Bin 0 -> 521 bytes test/fixtures/city.txt | 6 ++++++ test/fixtures/country.dat | Bin 0 -> 489 bytes test/fixtures/country.txt | 6 ++++++ test/fixtures/isp.dat | Bin 0 -> 651 bytes test/fixtures/isp.txt | 6 ++++++ test/fixtures/netspeed.dat | Bin 0 -> 494 bytes test/fixtures/netspeed.txt | 6 ++++++ test/fixtures/org.dat | Bin 0 -> 658 bytes test/fixtures/org.txt | 6 ++++++ test/fixtures/region.dat | Bin 0 -> 495 bytes test/fixtures/region.txt | 6 ++++++ 15 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/asnum.dat create mode 100644 test/fixtures/asnum.txt create mode 100644 test/fixtures/city.dat create mode 100644 test/fixtures/city.txt create mode 100644 test/fixtures/country.dat create mode 100644 test/fixtures/country.txt create mode 100644 test/fixtures/isp.dat create mode 100644 test/fixtures/isp.txt create mode 100644 test/fixtures/netspeed.dat create mode 100644 test/fixtures/netspeed.txt create mode 100644 test/fixtures/org.dat create mode 100644 test/fixtures/org.txt create mode 100644 test/fixtures/region.dat create mode 100644 test/fixtures/region.txt diff --git a/test.rb b/test.rb index bd0045d..46b2de1 100644 --- a/test.rb +++ b/test.rb @@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/geoip' require 'rubygems' -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 @@ -144,27 +144,27 @@ def test_construction_default h = db.look_up('24.24.24.24') assert_kind_of Hash, h - assert_equal 'Road Runner', h[:name] + 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, 'Road Runner') + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') end def test_construction_filesystem db = GeoIP::Organization.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') + 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, 'Road Runner') + 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, 'Road Runner') + assert_look_up(db, '24.24.24.24', :name, 'org test 123 org') end def test_bad_db_file diff --git a/test/fixtures/asnum.dat b/test/fixtures/asnum.dat new file mode 100644 index 0000000000000000000000000000000000000000..d401e1b4e886121010507807b46ebcece0c073b9 GIT binary patch literal 503 zcmXxh=T5^g7)IfjS;}5z4?|fkEyPKn5N2pomI^-t7XT7>(EAV8sVrHKj-1G!qYO#I z(ug!F%}MjpnDow?l*WCRqzT_;>CN{PF^w}gi-Q*yqEwbs3U6PiiinPMcj^AG; z)^Hs+a1*z18{PC>!r&ebh91yv%DHq%AK@{c_@_0g?)y|~_-;yPv>V|LaVtQJAx0SE z1zutcukacZyun+%!+U(dM@;bv+t|SjyZDSR_=@h6>uGMoD0cUdI$7`2r1{5puVM0^ dzUlt`8XK9wCUKP5mT6~SCJby)6h+znoPP40boab2_P|H~nwVMXf7RLq@iMWtWFEwH1!e~r9jEQ&h2%e;^yrh!K zlQ-3!`ctVcDJ6AFJ<_-|A@xcP){xZax?dV_Jt*C~9wtU`6vwdh!=$t>ZAepW+GD4s z8EICUV`s(fr-^x7z(rib3@)RczDgKe!_Lr6+D?p^teCuz*E8#uA?3DTa84Wjx0Vyu>TK#v6>Vf>o^HE#6@r?Ul2C%~;L$mxY#&sW>cgXn>a93uo}NEVt^H#Bz<7RG2tvPPqH4?JJ>N@`BxRrL EANn^mwEzGB literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..4a37f002e4e418cbcfa76b1ba281ae63a0a7b10d GIT binary patch literal 489 zcmXxf*-ipM5QX96E^fHszBMYSfI7{f=(sx!>V=XhUU;dAZ{Q32Bv|=LcPd{_sw-XT z0clVgl7^+cG%Jlrf4!d7gftrRm^2=8P7(t)Gf8s_r*Q`R19Q@jv@6ZiwD|?8AT3Hu zbhbi&nXrPZxQ6SvftzUCw+Rl5*k8Iwwk;=8iCo5gJP5akQYGZ7R15h?Iwso+d&DXt zc&uZL4Lrp&Y~ndyV1k!;h1Yn4w|Iy5_<$+4FvB)J;uAijec4Vcd+E5e^YPk!`>eV5 ZuV2^x`F1{bkxv?N;+rnbx~?Ai=pV&g-xL4< literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..0704dd1481f222547c9319c91c42ae143b1c73ca GIT binary patch literal 651 zcmYk)*-pYh0EOYIxbOR}1MZ00p(t7x#8MQE(F9)r6E9sZyz%MlndB##i<9r1(x#J! zo*)Q%l|Ch>Oe#}Kzw$&zKDzr3l%lr})l1$!Qa@(9 zGq`75LbO=M2rGDsXIRB^yucVQ@d~f;25WeWb-cp_8<=7f@9_a!=)PgGZj$fMpU&5x pM$kx~Urek0S~Bg#+4|xRgBJl0f+8g^01|J&`F}(`?6n zg|LcixQ-jRiCbvfD+GsC>@VFV+m>@_kGziuco^RvNyjmtNcEUcr8BavuuJSD1dk0& zv56OWi7mXsYs~NlZ}ATA@c|$437@fzIR@wbE53fcX1E*(eFD@in)HMxs>Zinvd!J z4*ngV5FJJsV;N8J3@dn!7ntBBUg0&~;4R+aJw9L+YnWmk8~BJ%=)Ym{=l9#P)?a2W sicLjpK`ni4TJx<@viHx->a8zxv4t)v$BC<0HT|$CbYZ*QF6h_z2f9QtH~;_u literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..41e7b2417538570220e1fb1a46cc047aba281ac3 GIT binary patch literal 495 zcmXxfSx>@16o%nXao=&rjZwfAwL=lKE{ISN6D3jpfFxdd>reN-j&C!~DQeZX~OrUH066*Qt|(SJF^Vua2^*hy^xg-q=K}}(A{5=R;4v* zoyne`ZxEZ9!!6v#9o$8?Jx>_i$8>3tc3Uo`5`BnAc4J7E($@uq z#R`U4#Vf2~9k1~QBfP~syvGM@;3GEi30v647(3X-XY8SSvisWcYu~)b-CqB#Y(74J cOy_H8Y-j=-Rl~^EO)DOlN??N|Nir|eA6{vKn*aa+ literal 0 HcmV?d00001 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 From d3a5dafadb6bbf27befe80ac34e148da1cd5ac38 Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 17:24:24 +0200 Subject: [PATCH 5/7] Mark test.rb as UTF-8 --- test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test.rb b/test.rb index 46b2de1..1bb02b5 100644 --- a/test.rb +++ b/test.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'test/unit' require File.dirname(__FILE__) + '/geoip' require 'rubygems' From 43f11d20a157b5ad98cdc3071b4e9ee96a0a7aad Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 17:52:26 +0200 Subject: [PATCH 6/7] Set region_name only when region is present --- geoip.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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)); From 4d3c1ec03387a963befb35b5c876b9767874a10a Mon Sep 17 00:00:00 2001 From: Matt Todd Date: Wed, 11 Jul 2012 17:52:58 +0200 Subject: [PATCH 7/7] Test skipping region_name when region is blank --- test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test.rb b/test.rb index 1bb02b5..510cc37 100644 --- a/test.rb +++ b/test.rb @@ -123,6 +123,15 @@ def test_character_encoding_converted_to_utf8_first assert_look_up(db, '201.85.50.148', :city, 'São Paulo') 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 + end end