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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://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 <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.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>
|
2018-08-07 16:46:53 +02:00
|
|
|
#include <maxminddb.h>
|
2018-10-24 21:28:34 -07:00
|
|
|
|
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>
|
|
|
|
|
2020-01-13 14:32:19 +01:00
|
|
|
#include "../geoip2.c"
|
2018-08-07 16:46:53 +02:00
|
|
|
#include "dnstest.h"
|
2020-01-13 14:32:19 +01:00
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
/* Use GeoIP2 databases from the 'geoip2' system test */
|
|
|
|
#define TEST_GEOIP_DATA "../../../bin/tests/system/geoip2/data"
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2019-07-04 11:17:16 +02:00
|
|
|
static dns_geoip_databases_t geoip;
|
2019-06-27 21:08:20 -07:00
|
|
|
|
|
|
|
static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
|
|
|
load_geoip(const char *dir);
|
|
|
|
static void
|
|
|
|
close_geoip(void);
|
2019-07-16 15:28:20 +02:00
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-24 21:28:34 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = dns_test_begin(NULL, false);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
2019-07-16 15:28:20 +02:00
|
|
|
/* Use databases from the geoip system test */
|
|
|
|
load_geoip(TEST_GEOIP_DATA);
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-24 21:28:34 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
2019-07-16 15:28:20 +02:00
|
|
|
close_geoip();
|
|
|
|
|
2018-10-24 21:28:34 -07:00
|
|
|
dns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2019-06-11 20:32:21 -07:00
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
static MMDB_s *
|
2020-02-13 14:44:37 -08:00
|
|
|
open_geoip2(const char *dir, const char *dbfile, MMDB_s *mmdb) {
|
2019-06-22 23:45:59 -07:00
|
|
|
char pathbuf[PATH_MAX];
|
2020-02-13 14:44:37 -08:00
|
|
|
int ret;
|
2019-06-22 23:45:59 -07:00
|
|
|
|
|
|
|
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dir, dbfile);
|
|
|
|
ret = MMDB_open(pathbuf, MMDB_MODE_MMAP, mmdb);
|
|
|
|
if (ret == MMDB_SUCCESS) {
|
|
|
|
return (mmdb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
load_geoip(const char *dir) {
|
2020-02-12 13:59:18 +01:00
|
|
|
geoip.country = open_geoip2(dir, "GeoIP2-Country.mmdb", &geoip_country);
|
2019-06-22 23:45:59 -07:00
|
|
|
geoip.city = open_geoip2(dir, "GeoIP2-City.mmdb", &geoip_city);
|
|
|
|
geoip.as = open_geoip2(dir, "GeoLite2-ASN.mmdb", &geoip_as);
|
|
|
|
geoip.isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp);
|
|
|
|
geoip.domain = open_geoip2(dir, "GeoIP2-Domain.mmdb", &geoip_domain);
|
|
|
|
}
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2019-07-16 15:28:20 +02:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
close_geoip(void) {
|
2019-07-16 15:28:20 +02:00
|
|
|
MMDB_close(&geoip_country);
|
|
|
|
MMDB_close(&geoip_city);
|
|
|
|
MMDB_close(&geoip_as);
|
|
|
|
MMDB_close(&geoip_isp);
|
|
|
|
MMDB_close(&geoip_domain);
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:32:19 +01:00
|
|
|
static bool
|
|
|
|
/* Check if an MMDB entry of a given subtype exists for the given IP */
|
2020-02-13 14:44:37 -08:00
|
|
|
entry_exists(dns_geoip_subtype_t subtype, const char *addr) {
|
2020-01-13 14:32:19 +01:00
|
|
|
struct in6_addr in6;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct in_addr in4;
|
|
|
|
isc_netaddr_t na;
|
|
|
|
MMDB_s *db;
|
2020-01-13 14:32:19 +01:00
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, addr, &in6) == 1) {
|
|
|
|
isc_netaddr_fromin6(&na, &in6);
|
|
|
|
} else if (inet_pton(AF_INET, addr, &in4) == 1) {
|
|
|
|
isc_netaddr_fromin(&na, &in4);
|
|
|
|
} else {
|
|
|
|
INSIST(0);
|
|
|
|
ISC_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
db = geoip2_database(&geoip, fix_subtype(&geoip, subtype));
|
|
|
|
|
|
|
|
return (db != NULL && get_entry_for(db, &na) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Baseline test - check if get_entry_for() works as expected, i.e. that its
|
|
|
|
* return values are consistent with the contents of the test MMDBs found in
|
|
|
|
* bin/tests/system/geoip2/data/ (10.53.0.1 and fd92:7065:b8e:ffff::1 should be
|
|
|
|
* present in all databases, 192.0.2.128 should only be present in the country
|
|
|
|
* database, ::1 should be absent from all databases).
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
baseline(void **state) {
|
2020-01-13 14:32:19 +01:00
|
|
|
dns_geoip_subtype_t subtype;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
subtype = dns_geoip_city_name;
|
|
|
|
|
|
|
|
assert_true(entry_exists(subtype, "10.53.0.1"));
|
|
|
|
assert_false(entry_exists(subtype, "192.0.2.128"));
|
|
|
|
assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
|
|
|
|
assert_false(entry_exists(subtype, "::1"));
|
|
|
|
|
|
|
|
subtype = dns_geoip_country_name;
|
|
|
|
|
|
|
|
assert_true(entry_exists(subtype, "10.53.0.1"));
|
|
|
|
assert_true(entry_exists(subtype, "192.0.2.128"));
|
|
|
|
assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
|
|
|
|
assert_false(entry_exists(subtype, "::1"));
|
|
|
|
|
|
|
|
subtype = dns_geoip_domain_name;
|
|
|
|
|
|
|
|
assert_true(entry_exists(subtype, "10.53.0.1"));
|
|
|
|
assert_false(entry_exists(subtype, "192.0.2.128"));
|
|
|
|
assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
|
|
|
|
assert_false(entry_exists(subtype, "::1"));
|
|
|
|
|
|
|
|
subtype = dns_geoip_isp_name;
|
|
|
|
|
|
|
|
assert_true(entry_exists(subtype, "10.53.0.1"));
|
|
|
|
assert_false(entry_exists(subtype, "192.0.2.128"));
|
|
|
|
assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
|
|
|
|
assert_false(entry_exists(subtype, "::1"));
|
|
|
|
|
|
|
|
subtype = dns_geoip_as_asnum;
|
|
|
|
|
|
|
|
assert_true(entry_exists(subtype, "10.53.0.1"));
|
|
|
|
assert_false(entry_exists(subtype, "192.0.2.128"));
|
|
|
|
assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
|
|
|
|
assert_false(entry_exists(subtype, "::1"));
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2019-06-22 23:45:59 -07:00
|
|
|
do_lookup_string(const char *addr, dns_geoip_subtype_t subtype,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *string) {
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_elem_t elt;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct in_addr in4;
|
|
|
|
isc_netaddr_t na;
|
2020-07-07 19:52:23 +10:00
|
|
|
int n;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-07-07 19:52:23 +10:00
|
|
|
n = inet_pton(AF_INET, addr, &in4);
|
|
|
|
assert_int_equal(n, 1);
|
2019-06-22 23:45:59 -07:00
|
|
|
isc_netaddr_fromin(&na, &in4);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
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
|
2019-06-22 23:45:59 -07:00
|
|
|
do_lookup_string_v6(const char *addr, dns_geoip_subtype_t subtype,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *string) {
|
2013-02-27 17:19:39 -08:00
|
|
|
dns_geoip_elem_t elt;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct in6_addr in6;
|
|
|
|
isc_netaddr_t na;
|
2020-07-07 19:52:23 +10:00
|
|
|
int n;
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-07-07 19:52:23 +10:00
|
|
|
n = inet_pton(AF_INET6, addr, &in6);
|
|
|
|
assert_int_equal(n, 1);
|
2019-06-22 23:45:59 -07:00
|
|
|
isc_netaddr_fromin6(&na, &in6);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
|
|
|
elt.subtype = subtype;
|
2019-06-22 23:45:59 -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
|
|
|
}
|
|
|
|
|
|
|
|
/* GeoIP country matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.country == NULL) {
|
|
|
|
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
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", 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
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("192.0.2.128", 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
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.country == NULL) {
|
|
|
|
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_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
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.city == NULL) {
|
|
|
|
skip();
|
|
|
|
}
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_city_continentcode,
|
|
|
|
"NA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_city_countrycode, "US");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", 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
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_city_region, "CA");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_city_regionname,
|
|
|
|
"California");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", 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
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.1", 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 city (ipv6) matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.city == NULL) {
|
|
|
|
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_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
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
/* GeoIP asnum matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.as == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
match = do_lookup_string("10.53.0.3", dns_geoip_as_asnum, "AS100003");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
/* GeoIP isp matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.isp == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
match = do_lookup_string("10.53.0.1", dns_geoip_isp_name,
|
|
|
|
"One Systems, Inc.");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
/* GeoIP org matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
|
|
|
if (geoip.as == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
match = do_lookup_string("10.53.0.2", dns_geoip_org_name,
|
|
|
|
"Two Technology Ltd.");
|
2018-10-24 21:28:34 -07:00
|
|
|
assert_true(match);
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
/* GeoIP domain matching */
|
2018-10-24 21:28:34 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
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
|
|
|
|
2019-06-22 23:45:59 -07:00
|
|
|
if (geoip.domain == NULL) {
|
2018-10-24 21:28:34 -07:00
|
|
|
skip();
|
2013-02-27 17:19:39 -08:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
match = do_lookup_string("10.53.0.5", dns_geoip_domain_name, "five.es");
|
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
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 21:28:34 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test(baseline), cmocka_unit_test(country),
|
|
|
|
cmocka_unit_test(country_v6), cmocka_unit_test(city),
|
|
|
|
cmocka_unit_test(city_v6), cmocka_unit_test(asnum),
|
|
|
|
cmocka_unit_test(isp), cmocka_unit_test(org),
|
2019-12-02 11:42:50 +01:00
|
|
|
cmocka_unit_test(domain),
|
2018-10-24 21:28:34 -07:00
|
|
|
};
|
|
|
|
|
2019-12-02 11:42:50 +01:00
|
|
|
return (cmocka_run_group_tests(tests, _setup, _teardown));
|
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
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 21:28:34 -07:00
|
|
|
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 */
|