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

248 lines
6.6 KiB
C
Raw Permalink Normal View History

2021-01-31 17:48:31 +01:00
#if HAVE_CONFIG_H
#include <config.h>
#endif
#define _POSIX_C_SOURCE 200112L
#include <assert.h>
#include <stdarg.h>
#include <sys/types.h>
#include "maxminddb.h"
#include "maxminddb_test_helper.h"
#ifdef _WIN32
#include <io.h>
#else
#include <libgen.h>
#include <unistd.h>
#endif
void for_all_record_sizes(const char *filename_fmt,
2021-08-22 19:15:19 +02:00
void (*tests)(int record_size,
const char *filename,
const char *description)) {
int sizes[] = {24, 28, 32};
2021-01-31 17:48:31 +01:00
for (int i = 0; i < 3; i++) {
int size = sizes[i];
char filename[500];
snprintf(filename, 500, filename_fmt, size);
char description[14];
snprintf(description, 14, "%i bit record", size);
tests(size, filename, description);
}
}
2021-08-22 19:15:19 +02:00
void for_all_modes(void (*tests)(int mode, const char *description)) {
2021-01-31 17:48:31 +01:00
tests(MMDB_MODE_MMAP, "mmap mode");
}
2021-08-22 19:15:19 +02:00
const char *test_database_path(const char *filename) {
2021-01-31 17:48:31 +01:00
char *test_db_dir;
#ifdef _WIN32
test_db_dir = "../t/maxmind-db/test-data";
#else
char cwd[500];
char *UNUSED(tmp) = getcwd(cwd, 500);
if (strcmp(basename(cwd), "t") == 0) {
test_db_dir = "./maxmind-db/test-data";
} else {
test_db_dir = "./t/maxmind-db/test-data";
}
#endif
char *path = malloc(500);
assert(NULL != path);
snprintf(path, 500, "%s/%s", test_db_dir, filename);
return (const char *)path;
}
2021-08-22 19:15:19 +02:00
const char *dup_entry_string_or_bail(MMDB_entry_data_s entry_data) {
const char *string =
mmdb_strndup(entry_data.utf8_string, entry_data.data_size);
2021-01-31 17:48:31 +01:00
if (NULL == string) {
BAIL_OUT("mmdb_strndup failed");
}
return string;
}
2021-08-22 19:15:19 +02:00
MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc) {
2021-01-31 17:48:31 +01:00
if (0 != access(db_file, R_OK)) {
2021-08-22 19:15:19 +02:00
BAIL_OUT("could not read the specified file - %s\nIf you are in a git "
"checkout try running 'git submodule update --init'",
db_file);
2021-01-31 17:48:31 +01:00
}
MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s));
if (NULL == mmdb) {
BAIL_OUT("could not allocate memory for our MMDB_s struct");
}
int status = MMDB_open(db_file, mode, mmdb);
2021-08-22 19:15:19 +02:00
int is_ok = ok(MMDB_SUCCESS == status,
"open %s status is success - %s",
db_file,
mode_desc);
2021-01-31 17:48:31 +01:00
if (!is_ok) {
diag("open status code = %d (%s)", status, MMDB_strerror(status));
free(mmdb);
return NULL;
}
is_ok = ok(mmdb->file_size > 0,
"mmdb struct has been set for %s - %s",
2021-08-22 19:15:19 +02:00
db_file,
mode_desc);
2021-01-31 17:48:31 +01:00
if (!is_ok) {
free(mmdb);
return NULL;
}
return mmdb;
}
2021-08-22 19:15:19 +02:00
MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc) {
2021-01-31 17:48:31 +01:00
int gai_error, mmdb_error;
MMDB_lookup_result_s result =
MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
2021-08-22 19:15:19 +02:00
test_lookup_errors(
gai_error, mmdb_error, "MMDB_lookup_string", ip, file, mode_desc);
2021-01-31 17:48:31 +01:00
return result;
}
2021-08-22 19:15:19 +02:00
MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc) {
2021-01-31 17:48:31 +01:00
int ai_flags = AI_NUMERICHOST;
2021-08-22 19:15:19 +02:00
struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
2021-01-31 17:48:31 +01:00
struct addrinfo *addresses = NULL;
if (ip[0] == ':') {
hints.ai_flags = ai_flags;
#if defined AI_V4MAPPED && !defined __FreeBSD__
hints.ai_flags |= AI_V4MAPPED;
#endif
hints.ai_family = AF_INET6;
} else {
hints.ai_flags = ai_flags;
hints.ai_family = AF_INET;
}
int gai_error = getaddrinfo(ip, NULL, &hints, &addresses);
int mmdb_error = 0;
2021-08-22 19:15:19 +02:00
MMDB_lookup_result_s result = {.found_entry = false};
2021-01-31 17:48:31 +01:00
if (gai_error == 0) {
result = MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, &mmdb_error);
}
if (NULL != addresses) {
freeaddrinfo(addresses);
}
2021-08-22 19:15:19 +02:00
test_lookup_errors(
gai_error, mmdb_error, "MMDB_lookup_sockaddr", ip, file, mode_desc);
2021-01-31 17:48:31 +01:00
return result;
}
2021-08-22 19:15:19 +02:00
void test_lookup_errors(int gai_error,
int mmdb_error,
const char *function,
const char *ip,
const char *file,
const char *mode_desc) {
2021-01-31 17:48:31 +01:00
int is_ok = ok(0 == gai_error,
"no getaddrinfo error in call to %s for %s - %s - %s",
2021-08-22 19:15:19 +02:00
function,
ip,
file,
mode_desc);
2021-01-31 17:48:31 +01:00
if (!is_ok) {
diag("error from call to getaddrinfo for %s - %s",
2021-08-22 19:15:19 +02:00
ip,
gai_strerror(gai_error));
2021-01-31 17:48:31 +01:00
}
is_ok = ok(0 == mmdb_error,
"no MMDB error in call to %s for %s - %s - %s",
2021-08-22 19:15:19 +02:00
function,
ip,
file,
mode_desc);
2021-01-31 17:48:31 +01:00
if (!is_ok) {
diag("MMDB error - %s", MMDB_strerror(mmdb_error));
}
}
2021-08-22 19:15:19 +02:00
MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result,
uint32_t expect_type,
const char *description,
...) {
2021-01-31 17:48:31 +01:00
va_list keys;
va_start(keys, description);
MMDB_entry_data_s data;
int status = MMDB_vget_value(&result->entry, &data, keys);
va_end(keys);
2021-08-22 19:15:19 +02:00
if (cmp_ok(status,
"==",
MMDB_SUCCESS,
"no error from call to MMDB_vget_value - %s",
description)) {
2021-01-31 17:48:31 +01:00
2021-08-22 19:15:19 +02:00
if (!cmp_ok(data.type,
"==",
expect_type,
"got the expected data type - %s",
description)) {
2021-01-31 17:48:31 +01:00
2021-08-22 19:15:19 +02:00
diag(" data type value is %i but expected %i",
data.type,
2021-01-31 17:48:31 +01:00
expect_type);
}
} else {
diag(" error from MMDB_vget_value - %s", MMDB_strerror(status));
}
return data;
}
2021-08-22 19:15:19 +02:00
void compare_double(double got, double expect) {
2021-01-31 17:48:31 +01:00
double diff = fabs(got - expect);
int is_ok = ok(diff < 0.01, "double value was approximately %2.6f", expect);
if (!is_ok) {
2021-08-22 19:15:19 +02:00
diag(
" got %2.6f but expected %2.6f (diff = %2.6f)", got, expect, diff);
2021-01-31 17:48:31 +01:00
}
}
2021-08-22 19:15:19 +02:00
void compare_float(float got, float expect) {
2021-01-31 17:48:31 +01:00
float diff = fabsf(got - expect);
int is_ok = ok(diff < 0.01, "float value was approximately %2.1f", expect);
if (!is_ok) {
2021-08-22 19:15:19 +02:00
diag(
" got %2.4f but expected %2.1f (diff = %2.1f)", got, expect, diff);
2021-01-31 17:48:31 +01:00
}
}