From 3b149631f0eb732049e04fa0c89d09c7aa2376f9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 20:22:53 +0200 Subject: Adding upstream version 1.6.12. Signed-off-by: Daniel Baumann --- test/Makefile.am | 38 +++++++++ test/Makefile.vc | 29 +++++++ test/benchmark.c | 183 +++++++++++++++++++++++++++++++++++++++++ test/city_test.txt | 2 + test/country_test.txt | 61 ++++++++++++++ test/country_test2.txt | 61 ++++++++++++++ test/country_test_name.txt | 1 + test/fail_test.txt | 1 + test/proxy_test.txt | 17 ++++ test/region_test.txt | 24 ++++++ test/test-geoip-asnum.c | 56 +++++++++++++ test/test-geoip-city.c | 77 +++++++++++++++++ test/test-geoip-domain.c | 67 +++++++++++++++ test/test-geoip-invalid-file.c | 29 +++++++ test/test-geoip-isp.c | 58 +++++++++++++ test/test-geoip-netspeed.c | 59 +++++++++++++ test/test-geoip-org.c | 67 +++++++++++++++ test/test-geoip-region.c | 118 ++++++++++++++++++++++++++ test/test-geoip.c | 146 ++++++++++++++++++++++++++++++++ 19 files changed, 1094 insertions(+) create mode 100644 test/Makefile.am create mode 100644 test/Makefile.vc create mode 100644 test/benchmark.c create mode 100644 test/city_test.txt create mode 100644 test/country_test.txt create mode 100644 test/country_test2.txt create mode 100644 test/country_test_name.txt create mode 100644 test/fail_test.txt create mode 100644 test/proxy_test.txt create mode 100644 test/region_test.txt create mode 100644 test/test-geoip-asnum.c create mode 100644 test/test-geoip-city.c create mode 100644 test/test-geoip-domain.c create mode 100644 test/test-geoip-invalid-file.c create mode 100644 test/test-geoip-isp.c create mode 100644 test/test-geoip-netspeed.c create mode 100644 test/test-geoip-org.c create mode 100644 test/test-geoip-region.c create mode 100644 test/test-geoip.c (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..8c38178 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,38 @@ +check_PROGRAMS = test-geoip test-geoip-invalid-file + +noinst_PROGRAMS = benchmark test-geoip-region test-geoip-city test-geoip-org test-geoip-domain test-geoip-asnum test-geoip-isp test-geoip-netspeed + +EXTRA_PROGRAMS = benchmark \ + test-geoip-region \ + test-geoip-city \ + test-geoip-org \ + test-geoip-domain \ + test-geoip-asnum \ + test-geoip-isp \ + test-geoip-netspeed + +LDADD = $(top_builddir)/libGeoIP/libGeoIP.la +AM_CPPFLAGS = -I$(top_srcdir)/libGeoIP -DSRCDIR=\"$(top_srcdir)\" -Wall + +test_geoip_SOURCES = test-geoip.c + +test_geoip_invalid_file_SOURCES = test-geoip-invalid-file.c + +test_geoip_region_SOURCES = test-geoip-region.c + +test_geoip_org_SOURCES = test-geoip-org.c + +test_geoip_domain_SOURCES = test-geoip-domain.c + +test_geoip_isp_SOURCES = test-geoip-isp.c + +test_geoip_asnum_SOURCES = test-geoip-asnum.c + +test_geoip_netspeed_SOURCES = test-geoip-netspeed.c + +test_geoip_city_SOURCES = test-geoip-city.c + +benchmark_SOURCES = benchmark.c + +EXTRA_DIST = Makefile.vc city_test.txt country_test.txt country_test2.txt country_test_name.txt region_test.txt +TESTS = test-geoip test-geoip-invalid-file diff --git a/test/Makefile.vc b/test/Makefile.vc new file mode 100644 index 0000000..db80c10 --- /dev/null +++ b/test/Makefile.vc @@ -0,0 +1,29 @@ +#NMAKE makefile for Windows developers. +#Produces a static library (GeoIP.lib). + +COMPILER=cl + +LINK = link -nologo + +CFLAGS=-DWIN32 -MD -nologo + +GEOIPINC = -I..\libGeoIP + +CC1 = $(COMPILER) $(CFLAGS) $(GEOIPINC) -DSRCDIR=\"../\" + +GEOIPLIB = ..\libGeoIP\GeoIP.lib + +EXTRA_LIBS= advapi32.lib wsock32.lib + +AR=lib + +TEST: benchmark.exe test-geoip.exe + +benchmark.exe: benchmark.c + $(CC1) -c benchmark.c + $(LINK) benchmark.obj $(GEOIPLIB) + +test-geoip.exe: test-geoip.c + $(CC1) -c test-geoip.c + $(LINK) test-geoip.obj $(GEOIPLIB) + diff --git a/test/benchmark.c b/test/benchmark.c new file mode 100644 index 0000000..2d7925a --- /dev/null +++ b/test/benchmark.c @@ -0,0 +1,183 @@ +#include "GeoIP.h" +#include "GeoIPCity.h" +#include +#if !defined(_MSC_VER) +#include +#endif + +char *ipstring[4] = { + "24.24.24.24", "80.24.24.80", "200.24.24.40", "68.24.24.46"}; + +int numipstrings = 4; + +#if defined(_WIN32) +#define DATADIR SRCDIR "/data/" +#else +#define DATADIR "/usr/local/share/GeoIP/" +#endif + +#define GEOIP_OPEN(basename, flg) \ + do { \ + i = GeoIP_open(DATADIR basename, (flg) | GEOIP_SILENCE); \ + if (i == NULL) { \ + printf("error: %s%s does not exist\n", DATADIR, basename); \ + return; \ + /* or a 'longjmp(geo_jmp,1)'? */ \ + } \ + } while (0) + +#if !defined(_WIN32) +struct timeval timer_t1; +struct timeval timer_t2; +#else /* !defined(_WIN32) */ +FILETIME timer_t1; /* 100 ns */ +FILETIME timer_t2; +#endif /* !defined(_WIN32) */ + +#if !defined(_WIN32) +void timerstart(void) { gettimeofday(&timer_t1, NULL); } + +double timerstop(void) { + int a1 = 0; + int a2 = 0; + double r = 0; + gettimeofday(&timer_t2, NULL); + a1 = timer_t2.tv_sec - timer_t1.tv_sec; + a2 = timer_t2.tv_usec - timer_t1.tv_usec; + if (a1 < 0) { + a1 = a1 - 1; + a2 = a2 + 1000000; + } + r = (((double)a1) + (((double)a2) / 1000000)); + return r; +} +#else /* !defined(_WIN32) */ +void timerstart(void) { GetSystemTimeAsFileTime(&timer_t1); } + +double timerstop(void) { + __int64 delta; /* VC6 can't convert an unsigned int64 to to double */ + GetSystemTimeAsFileTime(&timer_t2); + delta = FILETIME_TO_USEC(timer_t2) - FILETIME_TO_USEC(timer_t1); + return ((double)delta) / 1E6; +} +#endif /* !defined(_WIN32) */ + +void testgeoipcountry(int flags, const char *msg, int numlookups) { + double t = 0; + int i4 = 0; + int i2 = 0; + GeoIP *i = NULL; + + GEOIP_OPEN("GeoIP.dat", flags); + timerstart(); + for (i2 = 0; i2 < numlookups; i2++) { + GeoIP_country_name_by_addr(i, ipstring[i4]); + i4 = (i4 + 1) % numipstrings; + } + t = timerstop(); + printf("%s\n", msg); + printf("%d lookups made in %f seconds \n", numlookups, t); + GeoIP_delete(i); +} + +void testgeoiporg(int flags, const char *msg, int numlookups) { + GeoIP *i = NULL; + int i4 = 0; + int i2 = 0; + double t = 0; + + GEOIP_OPEN("GeoIPOrg.dat", flags); + timerstart(); + for (i2 = 0; i2 < numlookups; i2++) { + free(GeoIP_name_by_addr(i, ipstring[i4])); + i4 = (i4 + 1) % numipstrings; + } + t = timerstop(); + printf("%s\n", msg); + printf("%d lookups made in %f seconds \n", numlookups, t); + GeoIP_delete(i); +} + +void testgeoipregion(int flags, const char *msg, int numlookups) { + GeoIP *i = NULL; + GeoIPRegion *i3 = NULL; + int i4 = 0; + int i2 = 0; + double t = 0; + + GEOIP_OPEN("GeoIPRegion.dat", flags); + timerstart(); + for (i2 = 0; i2 < numlookups; i2++) { + i3 = GeoIP_region_by_addr(i, ipstring[i4]); + GeoIPRegion_delete(i3); + i4 = (i4 + 1) % numipstrings; + } + t = timerstop(); + printf("%s\n", msg); + printf("%d lookups made in %f seconds \n", numlookups, t); + GeoIP_delete(i); +} + +void testgeoipcity(int flags, const char *msg, int numlookups) { + GeoIP *i = NULL; + GeoIPRecord *i3 = NULL; + int i4 = 0; + int i2 = 0; + double t = 0; + + GEOIP_OPEN("GeoLiteCity.dat", flags); + timerstart(); + for (i2 = 0; i2 < numlookups; i2++) { + i3 = GeoIP_record_by_addr(i, ipstring[i4]); + GeoIPRecord_delete(i3); + i4 = (i4 + 1) % numipstrings; + } + t = timerstop(); + printf("%s\n", msg); + printf("%d lookups made in %f seconds \n", numlookups, t); + GeoIP_delete(i); +} + +int main(void) { + int time = 300 * numipstrings; + testgeoipcountry(0, "GeoIP Country", 100 * time); + testgeoipcountry( + GEOIP_CHECK_CACHE, "GeoIP Country with GEOIP_CHECK_CACHE", 100 * time); + testgeoipcountry(GEOIP_MEMORY_CACHE, + "GeoIP Country with GEOIP_MEMORY_CACHE", + 1000 * time); + testgeoipcountry( + GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE, + "GeoIP Country with GEOIP_MEMORY_CACHE and GEOIP_CHECK_CACHE", + 1000 * time); + + testgeoipregion(0, "GeoIP Region", 100 * time); + testgeoipregion( + GEOIP_CHECK_CACHE, "GeoIP Region with GEOIP_CHECK_CACHE", 100 * time); + testgeoipregion(GEOIP_MEMORY_CACHE, + "GeoIP Region with GEOIP_MEMORY_CACHE", + 1000 * time); + testgeoipregion( + GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE, + "GeoIP Region with GEOIP_MEMORY_CACHE and GEOIP_CHECK_CACHE", + 1000 * time); + + testgeoiporg(0, "GeoIP Org", 50 * time); + testgeoiporg( + GEOIP_INDEX_CACHE, "GeoIP Org with GEOIP_INDEX_CACHE", 200 * time); + testgeoiporg(GEOIP_INDEX_CACHE | GEOIP_CHECK_CACHE, + "GeoIP Org with GEOIP_INDEX_CACHE and GEOIP_CHECK_CACHE", + 200 * time); + testgeoiporg( + GEOIP_MEMORY_CACHE, "GeoIP Org with GEOIP_MEMORY_CACHE", 500 * time); + + testgeoipcity(0, "GeoIP City", 50 * time); + testgeoipcity( + GEOIP_INDEX_CACHE, "GeoIP City with GEOIP_INDEX_CACHE", 200 * time); + testgeoipcity(GEOIP_INDEX_CACHE | GEOIP_CHECK_CACHE, + "GeoIP City with GEOIP_INDEX_CACHE and GEOIP_CHECK_CACHE", + 200 * time); + testgeoipcity( + GEOIP_MEMORY_CACHE, "GeoIP City with GEOIP_MEMORY_CACHE", 500 * time); + return 0; +} diff --git a/test/city_test.txt b/test/city_test.txt new file mode 100644 index 0000000..2ba04ac --- /dev/null +++ b/test/city_test.txt @@ -0,0 +1,2 @@ +24.24.24.24 # Should return Ithaca, NY, US +80.24.24.24 # Should return Madrid, 29, ES diff --git a/test/country_test.txt b/test/country_test.txt new file mode 100644 index 0000000..a395b75 --- /dev/null +++ b/test/country_test.txt @@ -0,0 +1,61 @@ +12.87.118.0 US USA +63.147.126.103 FR FRA +64.17.254.216 US USA +65.23.121.221 US USA +65.116.3.80 US USA +66.92.181.240 US USA +67.43.156.0 A1 A1 +67.43.156.64 A1 A1 +67.43.156.128 A1 A1 +67.43.156.192 A1 A1 +69.218.251.16 US USA +70.46.123.145 US USA +71.138.104.240 US USA +72.214.6.72 GB GBR +74.0.137.194 US USA +75.77.211.208 US USA +78.26.70.208 IT ITA +81.2.69.160 GB GBR +82.99.17.96 NO NOR +83.206.36.224 FR FRA +85.88.2.224 DE DEU +89.92.212.0 FR FRA +89.92.212.32 FR FRA +89.92.212.80 FR FRA +89.92.212.94 FR FRA +89.92.212.95 FR FRA +89.92.212.96 FR FRA +89.92.212.144 FR FRA +89.92.212.224 FR FRA +89.92.212.248 FR FRA +89.92.212.252 FR FRA +89.92.213.0 FR FRA +89.92.213.16 FR FRA +89.92.213.32 FR FRA +89.92.213.44 FR FRA +89.92.213.48 FR FRA +89.92.213.64 FR FRA +89.92.213.80 FR FRA +89.92.213.88 FR FRA +89.92.213.96 FR FRA +89.92.213.104 FR FRA +89.92.213.108 FR FRA +89.92.213.110 FR FRA +89.92.213.112 FR FRA +89.92.213.166 FR FRA +89.92.213.167 FR FRA +89.92.213.168 FR FRA +89.92.213.176 FR FRA +89.92.213.192 FR FRA +89.92.213.200 FR FRA +89.92.213.224 FR FRA +89.160.20.112 SE SWE +142.217.214.0 CA CAN +143.217.214.0 VN VNM +204.128.241.208 CA CAN +208.110.217.107 US USA +216.83.171.192 US USA +216.160.83.56 US USA +222.230.136.0 JP JPN +222.230.137.0 JP JPN +223.0.0.0 FR FRA diff --git a/test/country_test2.txt b/test/country_test2.txt new file mode 100644 index 0000000..30c5982 --- /dev/null +++ b/test/country_test2.txt @@ -0,0 +1,61 @@ +12.87.118.0 US +63.147.126.103 FR +64.17.254.216 US +65.23.121.221 US +65.116.3.80 US +66.92.181.240 US +67.43.156.0 A1 +67.43.156.64 A1 +67.43.156.128 A1 +67.43.156.192 A1 +69.218.251.16 US +70.46.123.145 US +71.138.104.240 US +72.214.6.72 GB +74.0.137.194 US +75.77.211.208 US +78.26.70.208 IT +81.2.69.160 GB +82.99.17.96 NO +83.206.36.224 FR +85.88.2.224 DE +89.92.212.0 FR +89.92.212.32 FR +89.92.212.80 FR +89.92.212.94 FR +89.92.212.95 FR +89.92.212.96 FR +89.92.212.144 FR +89.92.212.224 FR +89.92.212.248 FR +89.92.212.252 FR +89.92.213.0 FR +89.92.213.16 FR +89.92.213.32 FR +89.92.213.44 FR +89.92.213.48 FR +89.92.213.64 FR +89.92.213.80 FR +89.92.213.88 FR +89.92.213.96 FR +89.92.213.104 FR +89.92.213.108 FR +89.92.213.110 FR +89.92.213.112 FR +89.92.213.166 FR +89.92.213.167 FR +89.92.213.168 FR +89.92.213.176 FR +89.92.213.192 FR +89.92.213.200 FR +89.92.213.224 FR +89.160.20.112 SE +142.217.214.0 CA +143.217.214.0 VN +204.128.241.208 CA +208.110.217.107 US +216.83.171.192 US +216.160.83.56 US +222.230.136.0 JP +222.230.137.0 JP +223.0.0.0 FR diff --git a/test/country_test_name.txt b/test/country_test_name.txt new file mode 100644 index 0000000..ce71409 --- /dev/null +++ b/test/country_test_name.txt @@ -0,0 +1 @@ +yahoo.com US diff --git a/test/fail_test.txt b/test/fail_test.txt new file mode 100644 index 0000000..902ee09 --- /dev/null +++ b/test/fail_test.txt @@ -0,0 +1 @@ +136.253.10.148 AU diff --git a/test/proxy_test.txt b/test/proxy_test.txt new file mode 100644 index 0000000..b11a2e1 --- /dev/null +++ b/test/proxy_test.txt @@ -0,0 +1,17 @@ +66.119.33.170 3 +139.134.64.157 2 +12.148.160.0 1 +24.24.24.21 0 +24.24.24.22 0 +24.24.24.23 0 +24.24.24.24 0 +24.24.24.25 0 +24.24.24.26 0 +145.253.2.25 1 +203.26.24.212 1 +195.229.241.233 1 +200.158.23.208 2 +80.247.152.5 2 +200.161.189.226 2 +66.119.33.135 3 +80.58.39.42 3 diff --git a/test/region_test.txt b/test/region_test.txt new file mode 100644 index 0000000..2828a57 --- /dev/null +++ b/test/region_test.txt @@ -0,0 +1,24 @@ +216.236.135.152 US USA +24.24.24.24 US USA +147.251.48.1 CZ CZE +203.174.65.12 JP JPN +212.208.74.140 FR FRA +200.219.192.106 BR BRA +134.102.101.18 DE DEU +193.75.148.28 BE BEL +194.244.83.2 IT ITA +203.15.106.23 AU AUS +196.31.1.1 ZA ZAF +151.28.39.114 IT ITA +151.38.70.94 IT ITA +193.56.4.124 FR FRA +195.142.146.198 TR TUR +211.232.0.0 KR KOR +211.240.0.0 KR KOR +193.194.4.0 MA MAR +139.20.112.104 DE DEU +139.20.112.3 DE DEU +145.236.125.211 HU HUN +yahoo.com US USA +amazon.com US USA +www.uspto.gov US USA diff --git a/test/test-geoip-asnum.c b/test/test-geoip-asnum.c new file mode 100644 index 0000000..56d1cc8 --- /dev/null +++ b/test/test-geoip-asnum.c @@ -0,0 +1,56 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-asnum.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + char *org; + char host[50]; + + gi = GeoIP_open("../data/GeoIPASNum.dat", GEOIP_STANDARD); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("asnum_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening asnum_test.txt\n"); + exit(1); + } + + while (fscanf(f, "%s", host) != EOF) { + org = GeoIP_org_by_name(gi, (const char *)host); + + if (org != NULL) { + printf("%s\t%s\n", host, _mk_NA(org)); + } + } + + GeoIP_delete(gi); + fclose(f); + return 0; +} diff --git a/test/test-geoip-city.c b/test/test-geoip-city.c new file mode 100644 index 0000000..93568e0 --- /dev/null +++ b/test/test-geoip-city.c @@ -0,0 +1,77 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-city.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" +#include "GeoIPCity.h" + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + GeoIPRecord *gir; + char host[50]; + const char *time_zone = NULL; + char **ret; + + gi = GeoIP_open("../data/GeoIPCity.dat", GEOIP_INDEX_CACHE); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("city_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening city_test.txt\n"); + exit(1); + } + + while (fscanf(f, "%s", host) != EOF) { + gir = GeoIP_record_by_name(gi, (const char *)host); + + if (gir != NULL) { + ret = GeoIP_range_by_ip(gi, (const char *)host); + time_zone = GeoIP_time_zone_by_country_and_region(gir->country_code, + gir->region); + printf("%s\t%s\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%s\t%s\n", + host, + _mk_NA(gir->country_code), + _mk_NA(gir->region), + _mk_NA(GeoIP_region_name_by_code(gir->country_code, + gir->region)), + _mk_NA(gir->city), + _mk_NA(gir->postal_code), + gir->latitude, + gir->longitude, + gir->metro_code, + gir->area_code, + _mk_NA(time_zone), + ret[0], + ret[1]); + GeoIP_range_by_ip_delete(ret); + GeoIPRecord_delete(gir); + } + } + GeoIP_delete(gi); + fclose(f); + return 0; +} diff --git a/test/test-geoip-domain.c b/test/test-geoip-domain.c new file mode 100644 index 0000000..d8ac609 --- /dev/null +++ b/test/test-geoip-domain.c @@ -0,0 +1,67 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-org.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + char *domain; + char host[50]; + char **ret; + + gi = GeoIP_open("../data/GeoIPDomain.dat", GEOIP_INDEX_CACHE); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("domain_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening domain_test.txt\n"); + exit(1); + } + + printf("IP\tdomain\tnetmask\tbeginIp\tendIp\n"); + while (fscanf(f, "%s", host) != EOF) { + domain = GeoIP_name_by_name(gi, (const char *)host); + + if (domain != NULL) { + ret = GeoIP_range_by_ip(gi, (const char *)host); + + printf("%s\t%s\t%d\t%s\t%s\n", + host, + _mk_NA(domain), + GeoIP_last_netmask(gi), + ret[0], + ret[1]); + GeoIP_range_by_ip_delete(ret); + free(domain); + } + } + + fclose(f); + GeoIP_delete(gi); + return 0; +} diff --git a/test/test-geoip-invalid-file.c b/test/test-geoip-invalid-file.c new file mode 100644 index 0000000..10fe246 --- /dev/null +++ b/test/test-geoip-invalid-file.c @@ -0,0 +1,29 @@ +#include "GeoIP.h" + +int main(void) { + GeoIP *gi = GeoIP_open(SRCDIR "/README.md", GEOIP_MEMORY_CACHE); + + /* We don't detect invalid files at load, unfortunately. */ + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + return 1; + } + + const char *country = GeoIP_country_code_by_addr(gi, "24.24.24.24"); + if (country != NULL) { + fprintf(stderr, + "Received a non-NULL value on an invalid database from " + "GeoIP_country_code_by_addr\n"); + return 1; + } + + country = GeoIP_country_code_by_addr_v6(gi, "24.24.24.24"); + if (country != NULL) { + fprintf(stderr, + "Received a non-NULL value on an invalid database from " + "GeoIP_country_code_by_addr_v6\n"); + return 1; + } + + return 0; +} diff --git a/test/test-geoip-isp.c b/test/test-geoip-isp.c new file mode 100644 index 0000000..e4c3bb4 --- /dev/null +++ b/test/test-geoip-isp.c @@ -0,0 +1,58 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-isp.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + char *org; + char host[50]; + + gi = GeoIP_open("../data/GeoIPISP.dat", GEOIP_STANDARD); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("isp_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening isp_test.txt\n"); + exit(1); + } + + while (fscanf(f, "%s", host) != EOF) { + org = GeoIP_org_by_name(gi, (const char *)host); + + if (org != NULL) { + printf("%s\t%s\n", host, _mk_NA(org)); + free(org); + } + } + + fclose(f); + GeoIP_delete(gi); + + return 0; +} diff --git a/test/test-geoip-netspeed.c b/test/test-geoip-netspeed.c new file mode 100644 index 0000000..44aac64 --- /dev/null +++ b/test/test-geoip-netspeed.c @@ -0,0 +1,59 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-netspeed.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + int netspeed; + char host[50]; + + gi = GeoIP_open("../data/GeoIPNetSpeed.dat", GEOIP_STANDARD); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("netspeed_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening netspeed_test.txt\n"); + exit(1); + } + + while (fscanf(f, "%s", host) != EOF) { + netspeed = GeoIP_id_by_name(gi, (const char *)host); + if (netspeed == GEOIP_UNKNOWN_SPEED) { + printf("%s\tUnknown\n", host); + } else if (netspeed == GEOIP_DIALUP_SPEED) { + printf("%s\tDialup\n", host); + } else if (netspeed == GEOIP_CABLEDSL_SPEED) { + printf("%s\tCable/DSL\n", host); + } else if (netspeed == GEOIP_CORPORATE_SPEED) { + printf("%s\tCorporate\n", host); + } + } + fclose(f); + GeoIP_delete(gi); + + return 0; +} diff --git a/test/test-geoip-org.c b/test/test-geoip-org.c new file mode 100644 index 0000000..e42ed53 --- /dev/null +++ b/test/test-geoip-org.c @@ -0,0 +1,67 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-org.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(int argc, char *argv[]) { + FILE *f; + GeoIP *gi; + char *org; + char host[50]; + char **ret; + + gi = GeoIP_open("../data/GeoIPOrg.dat", GEOIP_INDEX_CACHE); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("org_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening org_test.txt\n"); + exit(1); + } + + printf("IP\torganization\tnetmask\tbeginIp\tendIp\n"); + while (fscanf(f, "%s", host) != EOF) { + org = GeoIP_name_by_name(gi, (const char *)host); + + if (org != NULL) { + ret = GeoIP_range_by_ip(gi, (const char *)host); + + printf("%s\t%s\t%d\t%s\t%s\n", + host, + _mk_NA(org), + GeoIP_last_netmask(gi), + ret[0], + ret[1]); + GeoIP_range_by_ip_delete(ret); + free(org); + } + } + + fclose(f); + GeoIP_delete(gi); + return 0; +} diff --git a/test/test-geoip-region.c b/test/test-geoip-region.c new file mode 100644 index 0000000..839bf45 --- /dev/null +++ b/test/test-geoip-region.c @@ -0,0 +1,118 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip-region.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" +#include /* For uint32_t */ +#ifdef HAVE_STDINT_H +#include /* For uint32_t */ +#endif +#if !defined(_WIN32) +#include /* For gethostbyname */ +#include /* For ntohl */ +#else +#include +#include +#endif +#include + +unsigned long inetaddr(const char *name) { + struct hostent *host; + struct in_addr inaddr; + + host = gethostbyname(name); + assert(host); + inaddr.s_addr = *((uint32_t *)host->h_addr_list[0]); + return inaddr.s_addr; +} + +static const char *_mk_NA(const char *p) { return p ? p : "N/A"; } + +int main(void) { + GeoIP *gi; + GeoIPRegion *gir, giRegion; + + FILE *f; + char ipAddress[30]; + char expectedCountry[3]; + char expectedCountry3[4]; + const char *time_zone; + + gi = GeoIP_open("../data/GeoIPRegion.dat", GEOIP_MEMORY_CACHE); + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + f = fopen("region_test.txt", "r"); + + if (f == NULL) { + fprintf(stderr, "Error opening region_test.txt\n"); + exit(1); + } + + gir = GeoIP_region_by_addr(gi, "10.0.0.0"); + if (gir != NULL) { + printf("lookup of private IP address: country = %s, region = %s\n", + gir->country_code, + gir->region); + } + + while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) != + EOF) { + printf("ip = %s\n", ipAddress); + + gir = GeoIP_region_by_name(gi, ipAddress); + if (gir != NULL) { + time_zone = GeoIP_time_zone_by_country_and_region(gir->country_code, + gir->region); + printf("%s, %s, %s, %s\n", + gir->country_code, + (!gir->region[0]) ? "N/A" : gir->region, + _mk_NA(GeoIP_region_name_by_code(gir->country_code, + gir->region)), + _mk_NA(time_zone)); + } else { + printf("NULL!\n"); + } + + GeoIP_assign_region_by_inetaddr(gi, inetaddr(ipAddress), &giRegion); + if (gir != NULL) { + assert(giRegion.country_code[0]); + assert(!strcmp(gir->country_code, giRegion.country_code)); + if (gir->region[0]) { + assert(giRegion.region[0]); + assert(!strcmp(gir->region, giRegion.region)); + } else { + assert(!giRegion.region[0]); + } + } else { + assert(!giRegion.country_code[0]); + } + + if (gir != NULL) { + GeoIPRegion_delete(gir); + } + } + + GeoIP_delete(gi); + fclose(f); + return 0; +} diff --git a/test/test-geoip.c b/test/test-geoip.c new file mode 100644 index 0000000..909e63c --- /dev/null +++ b/test/test-geoip.c @@ -0,0 +1,146 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* test-geoip.c + * + * Copyright (C) 2016 MaxMind, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "GeoIP.h" + +int main(void) { + FILE *f; + char *db_info; + char ipAddress[30]; + char expectedCountry[3]; + char expectedCountry3[4]; + const char *returnedCountry; + GeoIP *gi; + int failed = 0; + int test_num = 1; + + int i; + for (i = 0; i < 2; ++i) { + if (0 == i) { + /* Read from filesystem, check for updated file */ + gi = GeoIP_open(SRCDIR "/data/GeoIP.dat", + GEOIP_STANDARD | GEOIP_CHECK_CACHE); + } else { + /* Read from memory, faster but takes up more memory */ + gi = GeoIP_open(SRCDIR "/data/GeoIP.dat", GEOIP_MEMORY_CACHE); + } + + if (gi == NULL) { + fprintf(stderr, "Error opening database\n"); + exit(1); + } + + db_info = GeoIP_database_info(gi); + if (!db_info || strcmp(db_info, + "GEO-106 20160621 Build 1 Copyright (c) 2016 " + "MaxMind Inc All Rights Reserved")) { + fprintf(stderr, "Error reading database info (got %s).\n", db_info); + free(db_info); + failed = 1; + } + + /* make sure GeoIP deals with invalid query gracefully */ + returnedCountry = GeoIP_country_code_by_addr(gi, NULL); + if (returnedCountry != NULL) { + fprintf(stderr, + "Invalid Query test failed, got non NULL, expected NULL\n"); + failed = 1; + } + + returnedCountry = GeoIP_country_code_by_name(gi, NULL); + if (returnedCountry != NULL) { + fprintf(stderr, + "Invalid Query test failed, got non NULL, expected NULL\n"); + failed = 1; + } + + f = fopen(SRCDIR "/test/country_test.txt", "r"); + + while ( + fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) != + EOF) { + returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress); + if (returnedCountry == NULL || + strcmp(returnedCountry, expectedCountry) != 0) { + fprintf(stderr, + "Test addr %d for %s failed, got %s, expected %s\n", + test_num, + ipAddress, + returnedCountry, + expectedCountry); + failed = 1; + } + returnedCountry = GeoIP_country_code_by_name(gi, ipAddress); + if (returnedCountry == NULL || + strcmp(returnedCountry, expectedCountry) != 0) { + fprintf(stderr, + "Test name %d for %s failed, got %s, expected %s\n", + test_num, + ipAddress, + returnedCountry, + expectedCountry); + failed = 1; + } + returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress); + if (returnedCountry == NULL || + strcmp(returnedCountry, expectedCountry3) != 0) { + fprintf(stderr, + "Test addr %d for %s failed, got %s, expected %s\n", + test_num, + ipAddress, + returnedCountry, + expectedCountry); + failed = 1; + } + returnedCountry = GeoIP_country_code3_by_name(gi, ipAddress); + if (returnedCountry == NULL || + strcmp(returnedCountry, expectedCountry3) != 0) { + fprintf(stderr, + "Test name %d for %s failed, got %s, expected %s\n", + test_num, + ipAddress, + returnedCountry, + expectedCountry); + failed = 1; + } + test_num++; + } + fclose(f); + + f = fopen(SRCDIR "/test/country_test2.txt", "r"); + while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) { + returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress); + if (returnedCountry == NULL || + strcmp(returnedCountry, expectedCountry) != 0) { + fprintf(stderr, + "Test addr %d %s failed, got %s, expected %s\n", + test_num, + ipAddress, + returnedCountry, + expectedCountry); + failed = 1; + } + test_num++; + } + fclose(f); + GeoIP_delete(gi); + } + return failed; +} -- cgit v1.2.3