"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "t/maxminddb_test_helper.c" between
libmaxminddb-1.5.0.tar.gz and libmaxminddb-1.5.2.tar.gz

About: libmaxminddb is a library that provides functions for working with MaxMind DB files (free GeoLite 2 or commercial GeoIP2).

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

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)