2013-02-27 17:19:39 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2013-02-27 17:19:39 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2013-02-27 17:19:39 -08:00
|
|
|
*/
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-10-24 21:28:34 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-02-27 17:19:39 -08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2017-09-13 23:43:43 +10:00
|
|
|
#include <isc/string.h>
|
2013-02-27 17:19:39 -08:00
|
|
|
#include <isc/types.h>
|
2018-10-24 21:28:34 -07:00
|
|
|
#include <isc/util.h>
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
#include <dns/geoip.h>
|
|
|
|
|
|
|
|
#include "dnstest.h"
|
|
|
|
|
2019-06-11 18:36:52 -07:00
|
|
|
#if defined(HAVE_GEOIP2)
|
|
|
|
#include <maxminddb.h>
|
|
|
|
|
|
|
|
/* TODO GEOIP2 */
|
|
|
|
#define TEST_GEOIP_DATA ""
|
|
|
|
#elif defined(HAVE_GEOI2)
|
2013-02-27 17:19:39 -08:00
|
|
|
#include <GeoIP.h>
|
|
|
|
|
|
|
|
/* We use GeoIP databases from the 'geoip' system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
#define TEST_GEOIP_DATA "../../../bin/tests/system/geoip/data"
|
2019-06-11 18:36:52 -07:00
|
|
|
#endif
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2019-06-11 18:36:52 -07:00
|
|
|
#if defined(HAVE_GEOIP) || defined(HAVE_GEOIP2)
|
2018-10-24 21:28:34 -07:00
|
|
|
static int
|
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = dns_test_begin(NULL, false);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_teardown(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
dns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2019-06-11 18:36:52 -07:00
|
|
|
#endif /* HAVE_GEOIP || HAVE_GEOIP2 */
|
2018-10-24 21:28:34 -07:00
|
|
|
|
2019-06-11 18:36:52 -07:00
|
|
|
#ifdef HAVE_GEOIP
|
2013-02-27 17:19:39 -08:00
|
|
|
/*
|
|
|
|
* Helper functions
|
|
|
|
* (Mostly copied from bin/named/geoip.c)
|
|
|
|
*/
|
|
|
|
static dns_geoip_databases_t geoip = {
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-06-11 18:36:52 -07:00
|
|
|
init_geoip_db(void **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
|
2013-02-27 17:19:39 -08:00
|
|
|
GeoIPOptions method, const char *name)
|
|
|
|
{
|
|
|
|
GeoIP *db;
|
|
|
|
|
|
|
|
REQUIRE(dbp != NULL);
|
|
|
|
|
2019-06-11 18:36:52 -07:00
|
|
|
db = (GeoIP *)*dbp;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (db != NULL) {
|
|
|
|
GeoIP_delete(db);
|
|
|
|
db = *dbp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! GeoIP_db_avail(edition)) {
|
2014-01-23 12:46:02 -08:00
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
db = GeoIP_open_type(edition, method);
|
|
|
|
if (db == NULL) {
|
2014-01-23 12:46:02 -08:00
|
|
|
goto fail;
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*dbp = db;
|
2014-01-23 12:46:02 -08:00
|
|
|
return;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
fail:
|
2018-10-24 21:28:34 -07:00
|
|
|
if (fallback != 0) {
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(dbp, fallback, 0, method, name);
|
2018-10-24 21:28:34 -07:00
|
|
|
}
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_geoip(const char *dir) {
|
|
|
|
GeoIPOptions method;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
method = GEOIP_STANDARD;
|
|
|
|
#else
|
|
|
|
method = GEOIP_MMAP_CACHE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (dir != NULL) {
|
|
|
|
char *p;
|
|
|
|
DE_CONST(dir, p);
|
|
|
|
GeoIP_setup_custom_directory(p);
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.country_v4, GEOIP_COUNTRY_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv4)");
|
|
|
|
#ifdef HAVE_GEOIP_V6
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Country (IPv6)");
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.city_v4, GEOIP_CITY_EDITION_REV1,
|
|
|
|
GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.city_v6, GEOIP_CITY_EDITION_REV1_V6,
|
|
|
|
GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
|
2013-02-27 17:19:39 -08:00
|
|
|
#endif
|
|
|
|
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.region, GEOIP_REGION_EDITION_REV1,
|
|
|
|
GEOIP_REGION_EDITION_REV0, method, "Region");
|
|
|
|
init_geoip_db(&geoip.isp, GEOIP_ISP_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "ISP");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.org, GEOIP_ORG_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Org");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.as, GEOIP_ASNUM_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "AS");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.domain, GEOIP_DOMAIN_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "Domain");
|
2014-01-23 12:46:02 -08:00
|
|
|
init_geoip_db(&geoip.netspeed, GEOIP_NETSPEED_EDITION, 0,
|
2013-02-27 17:19:39 -08:00
|
|
|
method, "NetSpeed");
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2018-04-26 20:57:41 -07:00
|
|
|
do_lookup_string(const char *addr, dns_geoip_subtype_t subtype,
|
|
|
|
const char *string)
|
2013-02-27 17:19:39 -08:00
|
|
|
{
|
|
|
|
dns_geoip_elem_t elt;
|
|
|
|
struct in_addr in4;
|
|
|
|
isc_netaddr_t na;
|
|
|
|
|
|
|
|
inet_pton(AF_INET, addr, &in4);
|
|
|
|
isc_netaddr_fromin(&na, &in4);
|
|
|
|
|
|
|
|
elt.subtype = subtype;
|
2017-09-13 00:14:37 -07:00
|
|
|
strlcpy(elt.as_string, string, sizeof(elt.as_string));
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
return (dns_geoip_match(&na, &geoip, &elt));
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2018-04-26 20:57:41 -07:00
|
|
|
do_lookup_string_v6(const char *addr, dns_geoip_subtype_t subtype,
|
|
|
|
const char *string)
|
2013-02-27 17:19:39 -08:00
|
|
|
{
|
|
|
|
dns_geoip_elem_t elt;
|
|
|
|
struct in6_addr in6;
|
|
|
|
isc_netaddr_t na;
|
|
|
|
|
|
|
|
inet_pton(AF_INET6, addr, &in6);
|
|
|
|
isc_netaddr_fromin6(&na, &in6);
|
|
|
|
|
|
|
|
elt.subtype = subtype;
|
2017-09-13 00:14:37 -07:00
|
|
|
strlcpy(elt.as_string, string, sizeof(elt.as_string));
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
return (dns_geoip_match(&na, &geoip, &elt));
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2018-04-26 20:57:41 -07:00
|
|
|
do_lookup_int(const char *addr, dns_geoip_subtype_t subtype, int id) {
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_elem_t elt;
|
|
|
|
struct in_addr in4;
|
|
|
|
isc_netaddr_t na;
|
|
|
|
|
|
|
|
inet_pton(AF_INET, addr, &in4);
|
|
|
|
isc_netaddr_fromin(&na, &in4);
|
|
|
|
|
|
|
|
elt.subtype = subtype;
|
|
|
|
elt.as_int = id;
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
return (dns_geoip_match(&na, &geoip, &elt));
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP country matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
country(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.country_v4 == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_country_code, "AU");
|
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_country_code3, "AUS");
|
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_country_name, "Australia");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2014-08-28 22:05:57 -07:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
match = do_lookup_string("192.0.2.128", dns_geoip_country_code, "O1");
|
|
|
|
assert_true(match);
|
2014-08-28 22:05:57 -07:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("192.0.2.128",
|
2014-08-28 22:05:57 -07:00
|
|
|
dns_geoip_country_name, "Other");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP country (ipv6) matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
country_v6(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.country_v6 == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_country_code, "AU");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_country_code3, "AUS");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_country_name, "Australia");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP city (ipv4) matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
city(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.city_v4 == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_continentcode, "NA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countrycode, "US");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countrycode3, "USA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countryname, "United States");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_region, "CA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_regionname, "California");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_name, "Redwood City");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_postalcode, "94063");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.1", dns_geoip_city_areacode, 650);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.1", dns_geoip_city_metrocode, 807);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP city (ipv6) matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
city_v6(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.city_v6 == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_continentcode, "NA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countrycode, "US");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countrycode3, "USA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_countryname,
|
|
|
|
"United States");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_region, "CA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_regionname, "California");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_name, "Redwood City");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_city_postalcode, "94063");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* GeoIP region matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
region(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.region == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_region_code, "CA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_region_name, "California");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_region_countrycode, "US");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GeoIP best-database matching
|
|
|
|
* (With no specified databse and a city database available, answers
|
|
|
|
* should come from city database. With city database unavailable, region
|
|
|
|
* database. Region database unavailable, country database.)
|
|
|
|
*/
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
best(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.region == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode, "US");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode3, "USA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countryname, "United States");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_regionname, "Virginia");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_region, "VA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
GeoIP_delete(geoip.city_v4);
|
|
|
|
geoip.city_v4 = NULL;
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode, "AU");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, region doesn't support code3 or countryname, so
|
|
|
|
* the next two would be answered from the country database instead
|
|
|
|
*/
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode3, "CAN");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countryname, "Canada");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
GeoIP_delete(geoip.region);
|
|
|
|
geoip.region = NULL;
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode, "CA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countrycode3, "CAN");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_countryname, "Canada");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* GeoIP asnum matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
asnum(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.as == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.3", dns_geoip_as_asnum,
|
2013-02-27 17:19:39 -08:00
|
|
|
"AS100003 Three Network Labs");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP isp matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
isp(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.isp == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_isp_name,
|
2013-02-27 17:19:39 -08:00
|
|
|
"One Systems, Inc.");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP org matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
org(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.org == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.2", dns_geoip_org_name,
|
2013-02-27 17:19:39 -08:00
|
|
|
"Two Technology Ltd.");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP domain matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
domain(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.domain == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_string("10.53.0.4",
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_domain_name, "four.com");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP netspeed matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
|
|
|
netspeed(void **state) {
|
2018-04-17 08:29:14 -07:00
|
|
|
bool match;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
/* Use databases from the geoip system test */
|
2014-09-29 09:32:59 +10:00
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
if (geoip.netspeed == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.1", dns_geoip_netspeed_id, 0);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.2", dns_geoip_netspeed_id, 1);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.3", dns_geoip_netspeed_id, 2);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-04-26 20:57:41 -07:00
|
|
|
match = do_lookup_int("10.53.0.4", dns_geoip_netspeed_id, 3);
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
2018-10-24 21:28:34 -07:00
|
|
|
#endif /* HAVE_GEOIP */
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
int
|
|
|
|
main(void) {
|
2019-06-11 18:36:52 -07:00
|
|
|
#if defined(HAVE_GEOIP2)
|
|
|
|
/* TODO GEOIP2 */
|
|
|
|
print_message("1..0 # Skip geoip2 tests not complete\n");
|
|
|
|
#elif defined(HAVE_GEOIP)
|
2018-10-24 21:28:34 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(country, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(country_v6, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(city, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(city_v6, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(region, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(best, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(asnum, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(isp, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(org, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(domain, _setup, _teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(netspeed, _setup, _teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2013-02-27 17:19:39 -08:00
|
|
|
#else
|
2018-10-24 21:28:34 -07:00
|
|
|
print_message("1..0 # Skip geoip not enabled\n");
|
2019-06-11 18:36:52 -07:00
|
|
|
#endif
|
2018-10-24 21:28:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
#endif /* HAVE_CMOCKA */
|