1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00
SqMod/vendor/MaxmindDB/t/basic_lookup_t.c

216 lines
6.6 KiB
C
Raw Normal View History

2021-01-31 17:48:31 +01:00
#include "maxminddb_test_helper.h"
static void test_big_lookup(void);
/* These globals are gross but it's the easiest way to mix calling
* for_all_modes() and for_all_record_sizes() */
static int Current_Mode;
static const char *Current_Mode_Description;
2021-08-22 19:15:19 +02:00
void test_one_result(MMDB_s *mmdb,
MMDB_lookup_result_s result,
const char *ip,
const char *expect,
const char *function,
const char *filename,
const char *mode_desc) {
2021-01-31 17:48:31 +01:00
int is_ok = ok(result.found_entry,
"got a result for an IP in the database - %s - %s - %s - %s",
2021-08-22 19:15:19 +02:00
function,
ip,
filename,
mode_desc);
2021-01-31 17:48:31 +01:00
if (!is_ok) {
return;
}
MMDB_entry_data_s data =
data_ok(&result, MMDB_DATA_TYPE_UTF8_STRING, "result{ip}", "ip", NULL);
char *string = mmdb_strndup(data.utf8_string, data.data_size);
char *real_expect;
if (mmdb->metadata.ip_version == 4 || strncmp(expect, "::", 2) == 0) {
real_expect = mmdb_strndup(expect, strlen(expect));
} else {
// When looking up IPv4 addresses in a mixed DB the result will be
// something like "::1.2.3.4", not just "1.2.3.4".
int maxlen = strlen(expect) + 3;
real_expect = malloc(maxlen);
2021-08-22 19:15:19 +02:00
if (!real_expect) {
BAIL_OUT("could not allocate memory");
}
2021-01-31 17:48:31 +01:00
snprintf(real_expect, maxlen, "::%s", expect);
}
2021-08-22 19:15:19 +02:00
is(string,
real_expect,
"found expected result for ip key - %s - %s - %s - %s",
function,
ip,
filename,
mode_desc);
2021-01-31 17:48:31 +01:00
free(real_expect);
free(string);
}
2021-08-22 19:15:19 +02:00
void test_one_ip(MMDB_s *mmdb,
const char *ip,
const char *expect,
const char *filename,
const char *mode_desc) {
2021-01-31 17:48:31 +01:00
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
2021-08-22 19:15:19 +02:00
test_one_result(
mmdb, result, ip, expect, "MMDB_lookup_string", filename, mode_desc);
2021-01-31 17:48:31 +01:00
result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
2021-08-22 19:15:19 +02:00
test_one_result(
mmdb, result, ip, expect, "MMDB_lookup_addrinfo", filename, mode_desc);
2021-01-31 17:48:31 +01:00
}
2021-08-22 19:15:19 +02:00
void run_ipX_tests(const char *filename,
const char **missing_ips,
int missing_ips_length,
const char *pairs[][2],
int pairs_rows) {
2021-01-31 17:48:31 +01:00
const char *path = test_database_path(filename);
int mode = Current_Mode;
const char *mode_desc = Current_Mode_Description;
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
free((void *)path);
char desc_suffix[500];
snprintf(desc_suffix, 500, "%s - %s", filename, mode_desc);
for (int i = 0; i < missing_ips_length; i++) {
const char *ip = missing_ips[i];
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
2021-08-22 19:15:19 +02:00
ok(!result.found_entry,
"no result entry struct returned for IP address not in the database "
"(string lookup) - %s - %s - %s",
ip,
filename,
mode_desc);
2021-01-31 17:48:31 +01:00
result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
2021-08-22 19:15:19 +02:00
ok(!result.found_entry,
"no result entry struct returned for IP address not in the database "
"(ipv4 lookup) - %s - %s - %s",
ip,
filename,
mode_desc);
2021-01-31 17:48:31 +01:00
}
for (int i = 0; i < pairs_rows; i += 1) {
const char *ip_to_lookup = pairs[i][0];
const char *expect = pairs[i][1];
test_one_ip(mmdb, ip_to_lookup, expect, filename, mode_desc);
}
MMDB_close(mmdb);
free(mmdb);
}
2021-08-22 19:15:19 +02:00
void run_ipv4_tests(int UNUSED(record_size),
const char *filename,
const char *UNUSED(ignored)) {
2021-01-31 17:48:31 +01:00
const char *pairs[9][2] = {
2021-08-22 19:15:19 +02:00
{"1.1.1.1", "1.1.1.1"},
{"1.1.1.2", "1.1.1.2"},
{"1.1.1.3", "1.1.1.2"},
{"1.1.1.7", "1.1.1.4"},
{"1.1.1.9", "1.1.1.8"},
{"1.1.1.15", "1.1.1.8"},
{"1.1.1.17", "1.1.1.16"},
{"1.1.1.31", "1.1.1.16"},
{"1.1.1.32", "1.1.1.32"},
2021-01-31 17:48:31 +01:00
};
2021-08-22 19:15:19 +02:00
const char *missing[1] = {"2.3.4.5"};
2021-01-31 17:48:31 +01:00
run_ipX_tests(filename, missing, 1, pairs, 9);
}
2021-08-22 19:15:19 +02:00
void run_ipv6_tests(int UNUSED(record_size),
const char *filename,
const char *UNUSED(ignored)) {
2021-01-31 17:48:31 +01:00
const char *pairs[9][2] = {
2021-08-22 19:15:19 +02:00
{"::1:ffff:ffff", "::1:ffff:ffff"},
{"::2:0:0", "::2:0:0"},
{"::2:0:1a", "::2:0:0"},
{"::2:0:40", "::2:0:40"},
{"::2:0:4f", "::2:0:40"},
{"::2:0:50", "::2:0:50"},
{"::2:0:52", "::2:0:50"},
{"::2:0:58", "::2:0:58"},
{"::2:0:59", "::2:0:58"},
2021-01-31 17:48:31 +01:00
};
2021-08-22 19:15:19 +02:00
const char *missing[2] = {"2.3.4.5", "::abcd"};
2021-01-31 17:48:31 +01:00
run_ipX_tests(filename, missing, 2, pairs, 9);
}
2021-08-22 19:15:19 +02:00
void all_record_sizes(int mode, const char *description) {
const char *ipv4_filename_fmts[] = {"MaxMind-DB-test-ipv4-%i.mmdb",
"MaxMind-DB-test-mixed-%i.mmdb"};
2021-01-31 17:48:31 +01:00
Current_Mode = mode;
Current_Mode_Description = description;
for (int i = 0; i < 2; i++) {
for_all_record_sizes(ipv4_filename_fmts[i], &run_ipv4_tests);
}
2021-08-22 19:15:19 +02:00
const char *ipv6_filename_fmts[] = {"MaxMind-DB-test-ipv6-%i.mmdb",
"MaxMind-DB-test-mixed-%i.mmdb"};
2021-01-31 17:48:31 +01:00
for (int i = 0; i < 2; i++) {
for_all_record_sizes(ipv6_filename_fmts[i], &run_ipv6_tests);
}
}
2021-08-22 19:15:19 +02:00
static void test_big_lookup(void) {
2021-01-31 17:48:31 +01:00
const char *const db_filename = "GeoIP2-Precision-Enterprise-Test.mmdb";
const char *const db_path = test_database_path(db_filename);
ok(db_path != NULL, "got database path");
2021-08-22 19:15:19 +02:00
MMDB_s *const mmdb = open_ok(db_path, MMDB_MODE_MMAP, "mmap mode");
2021-01-31 17:48:31 +01:00
ok(mmdb != NULL, "opened MMDB");
free((char *)db_path);
int gai_err = 0, mmdb_err = 0;
const char *const ip_address = "81.2.69.160";
2021-08-22 19:15:19 +02:00
MMDB_lookup_result_s result =
MMDB_lookup_string(mmdb, ip_address, &gai_err, &mmdb_err);
2021-01-31 17:48:31 +01:00
ok(gai_err == 0, "no getaddrinfo error");
ok(mmdb_err == MMDB_SUCCESS, "no error from maxminddb library");
ok(result.found_entry, "found IP");
MMDB_entry_data_list_s *entry_data_list = NULL;
2021-08-22 19:15:19 +02:00
ok(MMDB_get_entry_data_list(&result.entry, &entry_data_list) ==
MMDB_SUCCESS,
"successfully looked up entry data list");
2021-01-31 17:48:31 +01:00
ok(entry_data_list != NULL, "got an entry_data_list");
MMDB_free_entry_data_list(entry_data_list);
MMDB_close(mmdb);
free(mmdb);
}
2021-08-22 19:15:19 +02:00
int main(void) {
2021-01-31 17:48:31 +01:00
plan(NO_PLAN);
for_all_modes(&all_record_sizes);
test_big_lookup();
done_testing();
}