From 70866e3004e8bfda076e45e52801afd2e1fbec13 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:17:04 -0300 Subject: [PATCH 01/17] Ensure file is loaded with correct encoding --- geoip-c.gemspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/geoip-c.gemspec b/geoip-c.gemspec index da88c50..cec052a 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + Gem::Specification.new do |s| s.name = 'geoip-c' s.version = "0.8.1" From 6fe375568d3c5ecddf872afa0914a07e0434a18a Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:18:17 -0300 Subject: [PATCH 02/17] Stripe spaces --- geoip-c.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geoip-c.gemspec b/geoip-c.gemspec index cec052a..d53b057 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -3,14 +3,14 @@ Gem::Specification.new do |s| s.name = 'geoip-c' s.version = "0.8.1" - + s.authors = ['Ryan Dahl', 'Matt Todd', 'Charles Brian Quinn', 'Michael Sheakoski', 'Silvio Quadri', 'Andy Lindeman'] s.email = ['alindeman@gmail.com', 'mtodd@highgroove.com'] - + s.summary = "A Binding to the GeoIP C library" s.description = 'Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryan Dahl' s.homepage = "http://github.com/mtodd/geoip" - + s.files = ["Rakefile", "extconf.rb", "test.rb", "geoip.c", "README.md"] s.test_files = ['test.rb'] s.extensions = ['extconf.rb'] From 6ddc717b24e01a2c7ec988e8f5f77f4ab42d6b77 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:18:43 -0300 Subject: [PATCH 03/17] Introduce a Gemfile to manage development dependencies Lot has been deprecated in Rake so we need to install things like RDoc. Also prepare the ground for usage of rake-compiler. --- Gemfile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Gemfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b4e2a20 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gemspec From f73a6142e8cc76b1819e81e7220ab882ba2b8190 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:19:43 -0300 Subject: [PATCH 04/17] Ignore bundler generated files --- .gitignore | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index f62c75f..0310cfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ -pkg/ -mkmf.log -Makefile +.bundle conftest.dSYM/ +Gemfile.lock geoip.bundle -geoip.so geoip.o +geoip.so +Makefile +mkmf.log +pkg/ From 48dc69fcae95da5f559f712549c6fb6306c16cd4 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:22:56 -0300 Subject: [PATCH 05/17] Relocate extension files into ext/geoip folder Move things out of root of the gem to avoid things like: require "extconf" Messing things up Once extension is compiled it will be moved by RubyGems to lib directory, so there is no need to indicate "." be added to the $LOAD_PATH --- extconf.rb => ext/geoip/extconf.rb | 0 geoip.c => ext/geoip/geoip.c | 0 geoip-c.gemspec | 5 ++--- 3 files changed, 2 insertions(+), 3 deletions(-) rename extconf.rb => ext/geoip/extconf.rb (100%) rename geoip.c => ext/geoip/geoip.c (100%) diff --git a/extconf.rb b/ext/geoip/extconf.rb similarity index 100% rename from extconf.rb rename to ext/geoip/extconf.rb diff --git a/geoip.c b/ext/geoip/geoip.c similarity index 100% rename from geoip.c rename to ext/geoip/geoip.c diff --git a/geoip-c.gemspec b/geoip-c.gemspec index d53b057..943e687 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -11,8 +11,7 @@ Gem::Specification.new do |s| s.description = 'Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryan Dahl' s.homepage = "http://github.com/mtodd/geoip" - s.files = ["Rakefile", "extconf.rb", "test.rb", "geoip.c", "README.md"] + s.files = ["Rakefile", "ext/geoip/extconf.rb", "ext/geoip/geoip.c", "test.rb", "README.md"] s.test_files = ['test.rb'] - s.extensions = ['extconf.rb'] - s.require_path = '.' + s.extensions = ["ext/geoip/extconf.rb"] end From ea19eb09252322b6cadd4542cc138b30fd434420 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:33:36 -0300 Subject: [PATCH 06/17] Use RDoc task for documentation This resolves the deprecation warning caused by usage of old rake. --- Rakefile | 8 ++++---- geoip-c.gemspec | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Rakefile b/Rakefile index c8dadc9..0bec0f1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ require 'rake' require 'rake/clean' require 'rake/testtask' -require 'rake/rdoctask' +require 'rdoc/task' require 'rake/gempackagetask' task :default => [:compile, :test] @@ -9,9 +9,9 @@ task :default => [:compile, :test] CLEAN.add "geoip.{o,bundle,so,obj,pdb,lib,def,exp}" CLOBBER.add ['Makefile', 'mkmf.log','doc'] -Rake::RDocTask.new do |rdoc| - rdoc.rdoc_files.add ['README', 'geoip.c'] - rdoc.main = "README" # page to start on +RDoc::Task.new do |rdoc| + rdoc.main = "README.md" # page to start on + rdoc.rdoc_files.include("README.md", "ext/geoip/geoip.c") rdoc.rdoc_dir = 'doc/' # rdoc output folder end diff --git a/geoip-c.gemspec b/geoip-c.gemspec index 943e687..f23b39e 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -14,4 +14,7 @@ Gem::Specification.new do |s| s.files = ["Rakefile", "ext/geoip/extconf.rb", "ext/geoip/geoip.c", "test.rb", "README.md"] s.test_files = ['test.rb'] s.extensions = ["ext/geoip/extconf.rb"] + + s.add_development_dependency "rake", "~> 0.9.6" + s.add_development_dependency "rdoc", "~> 3.12" end From 1606a560b9a4896c2fdb20c6204eaccbc0852125 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:34:02 -0300 Subject: [PATCH 07/17] Adjust ignored directories to exclude RDoc artifacts --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0310cfd..9cb94e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .bundle +/doc +/pkg conftest.dSYM/ Gemfile.lock geoip.bundle @@ -6,4 +8,3 @@ geoip.o geoip.so Makefile mkmf.log -pkg/ From 989960c4f72eee3eff62ba8118ad54091d1213b3 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:37:21 -0300 Subject: [PATCH 08/17] DRY gemspec usage Instead of duplicated gemspec to maintain, load the spec from the file. --- Rakefile | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Rakefile b/Rakefile index 0bec0f1..657018a 100644 --- a/Rakefile +++ b/Rakefile @@ -20,22 +20,8 @@ Rake::TestTask.new do |t| t.verbose = true end -spec = Gem::Specification.new do |s| - s.name = 'geoip-c' - s.version = "0.8.1" +spec = Gem::Specification.load "geoip-c.gemspec" - s.authors = ['Ryah Dahl', 'Matt Todd', 'Andy Lindeman'] - s.email = ['alindeman@gmail.com', 'mtodd@highgroove.com'] - - s.summary = "A Binding to the GeoIP C library" - s.description = 'Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryah Dahl' - s.homepage = "http://github.com/mtodd/geoip" - - s.files = ["Rakefile", "extconf.rb", "test.rb", "geoip.c", "README.md"] - s.test_files = ['test.rb'] - s.extensions = ['extconf.rb'] - s.require_path = '.' -end Rake::GemPackageTask.new(spec) do |p| p.need_tar = true From df65cf9eb6590fed49b73b5dd66e4ac654e131f5 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:38:17 -0300 Subject: [PATCH 09/17] Use RubyGems package task instead of Rake one This solve deprecation warnings and also ensure gems get properly packaged. --- Rakefile | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Rakefile b/Rakefile index 657018a..e9f1788 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require 'rake' require 'rake/clean' require 'rake/testtask' require 'rdoc/task' -require 'rake/gempackagetask' +require 'rubygems/package_task' task :default => [:compile, :test] @@ -22,10 +22,8 @@ end spec = Gem::Specification.load "geoip-c.gemspec" - -Rake::GemPackageTask.new(spec) do |p| - p.need_tar = true - p.gem_spec = spec +Gem::PackageTask.new(spec) do |pkg| + pkg.need_tar = true end desc 'compile the extension' From 08557dba6865225ef3109fc19d9de6d8b1b4b3c0 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 16 Jan 2013 16:42:12 -0300 Subject: [PATCH 10/17] Compile extension using rake-compiler This opens the door for cleaner compilation and later add cross compilation support. --- Rakefile | 11 +++-------- geoip-c.gemspec | 1 + 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Rakefile b/Rakefile index e9f1788..0f1169c 100644 --- a/Rakefile +++ b/Rakefile @@ -3,11 +3,11 @@ require 'rake/clean' require 'rake/testtask' require 'rdoc/task' require 'rubygems/package_task' +require "rake/extensiontask" task :default => [:compile, :test] -CLEAN.add "geoip.{o,bundle,so,obj,pdb,lib,def,exp}" -CLOBBER.add ['Makefile', 'mkmf.log','doc'] +CLOBBER.add 'doc' RDoc::Task.new do |rdoc| rdoc.main = "README.md" # page to start on @@ -26,12 +26,7 @@ Gem::PackageTask.new(spec) do |pkg| pkg.need_tar = true end -desc 'compile the extension' -task(:compile => 'Makefile') { sh 'make' } -file('Makefile' => "geoip.c") { ruby 'extconf.rb' } - -task :install => [:gem] do - `env ARCHFLAGS="-arch i386" gem install pkg/geoip-c-0.5.0.gem -- --with-geoip-dir=/usr/local/GeoIP` +Rake::ExtensionTask.new("geoip", spec) do |ext| end task(:webpage) do diff --git a/geoip-c.gemspec b/geoip-c.gemspec index f23b39e..f735bd5 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -17,4 +17,5 @@ Gem::Specification.new do |s| s.add_development_dependency "rake", "~> 0.9.6" s.add_development_dependency "rdoc", "~> 3.12" + s.add_development_dependency "rake-compiler", "~> 0.8.2" end From 3339260d7942e7fc49690ac9fe8b157716510853 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Thu, 17 Jan 2013 12:03:40 -0300 Subject: [PATCH 11/17] Set test encoding header and remove full path require Without it, Ruby 1.9.x can't read the test file. Rake::TestTask automatically adds 'lib' directory to $LOAD_PATH. --- test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test.rb b/test.rb index 31938c8..abb567c 100644 --- a/test.rb +++ b/test.rb @@ -1,5 +1,7 @@ +# -*- encoding: utf-8 -*- + require 'test/unit' -require File.dirname(__FILE__) + '/geoip' +require 'geoip' require 'rubygems' # require 'ruby-debug' # Debugger.start From ca6de810881abf011a9ff85895a0e5f1ece5ad2f Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Thu, 17 Jan 2013 12:05:23 -0300 Subject: [PATCH 12/17] Download GeoLiteCity database and use it on tests Clobber the data files from 'data' when requested. Also ignore data directory from Git. --- .gitignore | 1 + Rakefile | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9cb94e8..b1e36e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .bundle +/data /doc /pkg conftest.dSYM/ diff --git a/Rakefile b/Rakefile index 0f1169c..e1b6773 100644 --- a/Rakefile +++ b/Rakefile @@ -7,7 +7,7 @@ require "rake/extensiontask" task :default => [:compile, :test] -CLOBBER.add 'doc' +CLOBBER.add 'doc', 'data' RDoc::Task.new do |rdoc| rdoc.main = "README.md" # page to start on @@ -32,3 +32,19 @@ end task(:webpage) do sh 'scp -r doc/* rydahl@rubyforge.org:/var/www/gforge-projects/geoip-city/' end + +directory "data" + +file "data/GeoLiteCity.dat" => ["data"] do |f| + url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz" + + sh "curl #{url} -o data/GeoLiteCity.dat.gz" + sh "gunzip data/GeoLiteCity.dat.gz" + touch f.name +end + +task :database => ["data/GeoLiteCity.dat"] do + ENV['CITY'] = File.expand_path("data/GeoLiteCity.dat") +end + +task :test => [:compile, :database] From e81d71b700d0493327e5ec9fcbdd46269e22ccbf Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Thu, 17 Jan 2013 12:06:16 -0300 Subject: [PATCH 13/17] Default task is to run tests Since test already depends on compile --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index e1b6773..2cef006 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,7 @@ require 'rdoc/task' require 'rubygems/package_task' require "rake/extensiontask" -task :default => [:compile, :test] +task :default => [:test] CLOBBER.add 'doc', 'data' From 3e11a48f988bf16be2cc6af75478ac7f16f799fe Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Thu, 17 Jan 2013 12:28:49 -0300 Subject: [PATCH 14/17] Relocate test file to default 'test' directory And rename it to match the module it is testing. --- Rakefile | 1 - geoip-c.gemspec | 4 ++-- test.rb => test/test_geoip.rb | 0 3 files changed, 2 insertions(+), 3 deletions(-) rename test.rb => test/test_geoip.rb (100%) diff --git a/Rakefile b/Rakefile index 2cef006..3f01b01 100644 --- a/Rakefile +++ b/Rakefile @@ -16,7 +16,6 @@ RDoc::Task.new do |rdoc| end Rake::TestTask.new do |t| - t.test_files = ['test.rb'] t.verbose = true end diff --git a/geoip-c.gemspec b/geoip-c.gemspec index f735bd5..5a81639 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -11,8 +11,8 @@ Gem::Specification.new do |s| s.description = 'Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryan Dahl' s.homepage = "http://github.com/mtodd/geoip" - s.files = ["Rakefile", "ext/geoip/extconf.rb", "ext/geoip/geoip.c", "test.rb", "README.md"] - s.test_files = ['test.rb'] + s.files = ["Rakefile", "ext/geoip/extconf.rb", "ext/geoip/geoip.c", "test/test_geoip.rb", "README.md"] + s.test_files = ['test/test_geoip.rb'] s.extensions = ["ext/geoip/extconf.rb"] s.add_development_dependency "rake", "~> 0.9.6" diff --git a/test.rb b/test/test_geoip.rb similarity index 100% rename from test.rb rename to test/test_geoip.rb From 4a493814f8137686c9d477634c33dad11dd6812b Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 27 Feb 2013 14:47:14 -0300 Subject: [PATCH 15/17] Avoid segfault caused by missing region_name For some country+region combos, region_name might not be found, which results in a NULL string and causes segfault when strlen() is performed on it. --- ext/geoip/geoip.c | 5 ++++- test/test_geoip.rb | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/geoip/geoip.c b/ext/geoip/geoip.c index 085f6e1..e1eebfa 100644 --- a/ext/geoip/geoip.c +++ b/ext/geoip/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(); + 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))); + region_name = GeoIP_region_name_by_code(record->country_code, record->region); + if (region_name) + rb_hash_sset(hash, "region_name", encode_to_utf8_and_return_rb_str(region_name)); } if(record->city) rb_hash_sset(hash, "city", encode_to_utf8_and_return_rb_str(record->city)); diff --git a/test/test_geoip.rb b/test/test_geoip.rb index abb567c..0db2187 100644 --- a/test/test_geoip.rb +++ b/test/test_geoip.rb @@ -122,6 +122,10 @@ def test_character_encoding_converted_to_utf8_first assert_look_up(db, '201.85.50.148', :city, 'São Paulo') end + def test_hong_kong_segfault + db = GeoIP::City.new(@dbfile, :filesystem, true) + assert_look_up(db, "61.93.14.4", :country_name, "Hong Kong") + end end class GeoIPOrgTest < Test::Unit::TestCase @@ -168,5 +172,4 @@ def test_bad_db_file GeoIP::Organization.new('/supposed-to-fail') end end - end From 0b384aeac8caa0d88973b50c0e5f8b14bff4e8c7 Mon Sep 17 00:00:00 2001 From: Borna Novak Date: Mon, 6 May 2013 07:29:02 +0200 Subject: [PATCH 16/17] Merged gem structure pull from area17 with testdbs from mtodd --- 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 + test/test_geoip.rb | 218 ++++++++++++++++++++----------------- 15 files changed, 160 insertions(+), 100 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/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 diff --git a/test/test_geoip.rb b/test/test_geoip.rb index 0db2187..73719e3 100644 --- a/test/test_geoip.rb +++ b/test/test_geoip.rb @@ -1,13 +1,11 @@ -# -*- encoding: utf-8 -*- +# encoding: utf-8 require 'test/unit' require '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 @@ -20,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) @@ -67,109 +65,129 @@ 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 + + def test_hong_kong_segfault + db = GeoIP::City.new(@dbfile, :filesystem, true) + assert_look_up(db, "61.93.14.4", :country_name, "Hong Kong") + 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_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_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_hong_kong_segfault - db = GeoIP::City.new(@dbfile, :filesystem, true) - assert_look_up(db, "61.93.14.4", :country_name, "Hong Kong") end end -class GeoIPOrgTest < Test::Unit::TestCase - - def setup - ## Change me! - @dbfile = ORG_DB - 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_default - db = GeoIP::Organization.new(@dbfile) - - assert_raises TypeError do - db.look_up(nil) + def setup + ## Change me! + @dbfile = ORG_DB 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_default + db = GeoIP::Organization.new(@dbfile) - def test_construction_filesystem - db = GeoIP::Organization.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - end + assert_raises TypeError do + db.look_up(nil) + end - def test_construction_memory - db = GeoIP::Organization.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :name, 'Road Runner') - 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_filesystem_check - db = GeoIP::Organization.new(@dbfile, :filesystem, true) - 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, 'org test 123 org') + end - def test_bad_db_file - assert_raises Errno::ENOENT do - GeoIP::Organization.new('/supposed-to-fail') + 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 From fcd8c23b5b14a846630bcbc1a6411a84740d8748 Mon Sep 17 00:00:00 2001 From: Borna Novak Date: Mon, 6 May 2013 07:57:35 +0200 Subject: [PATCH 17/17] Tests pass for New York now (24.24.24.24), its East Syracuse really --- test/test_geoip.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/test_geoip.rb b/test/test_geoip.rb index 73719e3..7c63edb 100644 --- a/test/test_geoip.rb +++ b/test/test_geoip.rb @@ -19,12 +19,20 @@ def assert_look_up(db, addr, field, value) class GeoIPTest < Test::Unit::TestCase + def ip_address_to_int address + ( o1, o2, o3, o4 ) = address.split('.').map(&:to_i) + + 16777216 * o1 + 65536 * o2 + 256 * o3 + o4 + end + def setup + # 29690,"US","NY","East Syracuse","13057",43.0892,-76.0250,555,315 @ip = "24.24.24.24" - @ipnum = 16777216*24 + 65536*24 + 256*24 + 24 + @ipnum = ip_address_to_int @ip + # @large_ip = "245.245.245.245" - @large_ipnum = 16777216*245 + 65536*245 + 256*245 + 245 + @large_ipnum = ip_address_to_int @large_ip end # addr_to_num @@ -88,23 +96,23 @@ def test_construction_default h = db.look_up('24.24.24.24') assert_kind_of Hash, h - assert_equal 'New York', h[:city] + assert_equal 'East Syracuse', 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') + assert_look_up(db, '24.24.24.24', :city, 'East Syracuse') end def test_construction_filesystem db = GeoIP::City.new(@dbfile, :filesystem) - assert_look_up(db, '24.24.24.24', :city, 'New York') + assert_look_up(db, '24.24.24.24', :city, 'East Syracuse') end def test_construction_memory db = GeoIP::City.new(@dbfile, :memory) - assert_look_up(db, '24.24.24.24', :city, 'New York') + assert_look_up(db, '24.24.24.24', :city, 'East Syracuse') end def test_hong_kong_segfault @@ -114,7 +122,7 @@ def test_hong_kong_segfault def test_construction_filesystem_check db = GeoIP::City.new(@dbfile, :filesystem, true) - assert_look_up(db, '24.24.24.24', :city, 'New York') + assert_look_up(db, '24.24.24.24', :city, 'East Syracuse') end def test_bad_db_file