1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-16 15:17:13 +02:00

Update MaxmindDB to current git.

This commit is contained in:
Sandu Liviu Catalin
2021-08-22 20:15:19 +03:00
parent 0008869ddd
commit 69a4d305a5
40 changed files with 2291 additions and 1912 deletions

View File

@ -8,9 +8,10 @@
#include "maxminddb_test_helper.h"
int test_read(const char *path, const struct stat *UNUSED(
sbuf), int flags, struct FTW *UNUSED(ftw))
{
int test_read(const char *path,
const struct stat *UNUSED(sbuf),
int flags,
struct FTW *UNUSED(ftw)) {
// Check if path is a regular file)
if (flags != FTW_F) {
return 0;
@ -36,16 +37,15 @@ int test_read(const char *path, const struct stat *UNUSED(
BAIL_OUT("could not parse IP address");
}
cmp_ok(mmdb_error, "!=", MMDB_SUCCESS, "opening %s returned an error",
path);
cmp_ok(
mmdb_error, "!=", MMDB_SUCCESS, "opening %s returned an error", path);
MMDB_close(mmdb);
free(mmdb);
return 0;
}
int main(void)
{
int main(void) {
char *test_db_dir;
#ifdef _WIN32
test_db_dir = "../t/maxmind-db/bad-data";

View File

@ -1,7 +1,6 @@
#include "maxminddb_test_helper.h"
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-broken-pointers-24.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -15,16 +14,20 @@ void run_tests(int mode, const char *mode_desc)
MMDB_entry_data_s entry_data;
int status = MMDB_get_value(&result.entry, &entry_data, NULL);
cmp_ok(
status, "==", MMDB_INVALID_DATA_ERROR,
"MMDB_get_value returns MMDB_INVALID_DATA_ERROR for bad pointer in data section");
cmp_ok(status,
"==",
MMDB_INVALID_DATA_ERROR,
"MMDB_get_value returns MMDB_INVALID_DATA_ERROR for bad pointer "
"in data section");
MMDB_entry_data_list_s *entry_data_list;
status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
cmp_ok(
status, "==", MMDB_INVALID_DATA_ERROR,
"MMDB_get_entry_data_list returns MMDB_INVALID_DATA_ERROR for bad pointer in data section");
cmp_ok(status,
"==",
MMDB_INVALID_DATA_ERROR,
"MMDB_get_entry_data_list returns MMDB_INVALID_DATA_ERROR for "
"bad pointer in data section");
MMDB_free_entry_data_list(entry_data_list);
}
@ -36,17 +39,19 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s UNUSED(result) =
MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
cmp_ok(
mmdb_error, "==", MMDB_CORRUPT_SEARCH_TREE_ERROR,
"MMDB_lookup_string sets mmdb_error to MMDB_CORRUPT_SEARCH_TREE_ERROR when a search tree record points outside the data section");
cmp_ok(mmdb_error,
"==",
MMDB_CORRUPT_SEARCH_TREE_ERROR,
"MMDB_lookup_string sets mmdb_error to "
"MMDB_CORRUPT_SEARCH_TREE_ERROR when a search tree record "
"points outside the data section");
}
MMDB_close(mmdb);
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -7,14 +7,19 @@ static void test_big_lookup(void);
static int Current_Mode;
static const char *Current_Mode_Description;
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)
{
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) {
int is_ok = ok(result.found_entry,
"got a result for an IP in the database - %s - %s - %s - %s",
function, ip, filename, mode_desc);
function,
ip,
filename,
mode_desc);
if (!is_ok) {
return;
@ -33,35 +38,45 @@ void test_one_result(MMDB_s *mmdb, MMDB_lookup_result_s result,
// something like "::1.2.3.4", not just "1.2.3.4".
int maxlen = strlen(expect) + 3;
real_expect = malloc(maxlen);
if (!real_expect) {
BAIL_OUT("could not allocate memory");
}
snprintf(real_expect, maxlen, "::%s", expect);
}
is(string, real_expect,
"found expected result for ip key - %s - %s - %s - %s", function, ip,
filename, mode_desc);
is(string,
real_expect,
"found expected result for ip key - %s - %s - %s - %s",
function,
ip,
filename,
mode_desc);
free(real_expect);
free(string);
}
void test_one_ip(MMDB_s *mmdb, const char *ip, const char *expect,
const char *filename, const char *mode_desc)
{
void test_one_ip(MMDB_s *mmdb,
const char *ip,
const char *expect,
const char *filename,
const char *mode_desc) {
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
test_one_result(mmdb, result, ip, expect, "MMDB_lookup_string", filename,
mode_desc);
test_one_result(
mmdb, result, ip, expect, "MMDB_lookup_string", filename, mode_desc);
result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
test_one_result(mmdb, result, ip, expect, "MMDB_lookup_addrinfo", filename,
mode_desc);
test_one_result(
mmdb, result, ip, expect, "MMDB_lookup_addrinfo", filename, mode_desc);
}
void run_ipX_tests(const char *filename, const char **missing_ips,
int missing_ips_length, const char *pairs[][2],
int pairs_rows)
{
void run_ipX_tests(const char *filename,
const char **missing_ips,
int missing_ips_length,
const char *pairs[][2],
int pairs_rows) {
const char *path = test_database_path(filename);
int mode = Current_Mode;
const char *mode_desc = Current_Mode_Description;
@ -78,17 +93,21 @@ void run_ipX_tests(const char *filename, const char **missing_ips,
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
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);
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);
result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
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);
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);
}
for (int i = 0; i < pairs_rows; i += 1) {
@ -102,52 +121,47 @@ void run_ipX_tests(const char *filename, const char **missing_ips,
free(mmdb);
}
void run_ipv4_tests(int UNUSED(
record_size), const char *filename, const char *UNUSED(
ignored))
{
void run_ipv4_tests(int UNUSED(record_size),
const char *filename,
const char *UNUSED(ignored)) {
const char *pairs[9][2] = {
{ "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" },
{"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"},
};
const char *missing[1] = { "2.3.4.5" };
const char *missing[1] = {"2.3.4.5"};
run_ipX_tests(filename, missing, 1, pairs, 9);
}
void run_ipv6_tests(int UNUSED(
record_size), const char *filename, const char *UNUSED(
ignored))
{
void run_ipv6_tests(int UNUSED(record_size),
const char *filename,
const char *UNUSED(ignored)) {
const char *pairs[9][2] = {
{ "::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" },
{"::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"},
};
const char *missing[2] = { "2.3.4.5", "::abcd" };
const char *missing[2] = {"2.3.4.5", "::abcd"};
run_ipX_tests(filename, missing, 2, pairs, 9);
}
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"
};
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"};
Current_Mode = mode;
Current_Mode_Description = description;
@ -156,40 +170,35 @@ void all_record_sizes(int mode, const char *description)
for_all_record_sizes(ipv4_filename_fmts[i], &run_ipv4_tests);
}
const char *ipv6_filename_fmts[] = {
"MaxMind-DB-test-ipv6-%i.mmdb",
"MaxMind-DB-test-mixed-%i.mmdb"
};
const char *ipv6_filename_fmts[] = {"MaxMind-DB-test-ipv6-%i.mmdb",
"MaxMind-DB-test-mixed-%i.mmdb"};
for (int i = 0; i < 2; i++) {
for_all_record_sizes(ipv6_filename_fmts[i], &run_ipv6_tests);
}
}
static void test_big_lookup(void)
{
static void test_big_lookup(void) {
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");
MMDB_s * const mmdb = open_ok(db_path, MMDB_MODE_MMAP, "mmap mode");
MMDB_s *const mmdb = open_ok(db_path, MMDB_MODE_MMAP, "mmap mode");
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";
MMDB_lookup_result_s result = MMDB_lookup_string(mmdb, ip_address, &gai_err,
&mmdb_err);
MMDB_lookup_result_s result =
MMDB_lookup_string(mmdb, ip_address, &gai_err, &mmdb_err);
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;
ok(
MMDB_get_entry_data_list(&result.entry,
&entry_data_list) == MMDB_SUCCESS,
"successfully looked up entry data list"
);
ok(MMDB_get_entry_data_list(&result.entry, &entry_data_list) ==
MMDB_SUCCESS,
"successfully looked up entry data list");
ok(entry_data_list != NULL, "got an entry_data_list");
MMDB_free_entry_data_list(entry_data_list);
@ -198,8 +207,7 @@ static void test_big_lookup(void)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&all_record_sizes);
test_big_lookup();

View File

@ -1,21 +1,19 @@
#include "libtap/tap.h"
#include "maxminddb_test_helper.h"
#include <assert.h>
#include <data-pool.h>
#include <inttypes.h>
#include "libtap/tap.h"
#include <math.h>
#include "maxminddb_test_helper.h"
static void test_data_pool_new(void);
static void test_data_pool_destroy(void);
static void test_data_pool_alloc(void);
static void test_data_pool_to_list(void);
static bool create_and_check_list(size_t const,
size_t const);
static bool create_and_check_list(size_t const, size_t const);
static void check_block_count(MMDB_entry_data_list_s const *const,
size_t const);
int main(void)
{
int main(void) {
plan(NO_PLAN);
test_data_pool_new();
test_data_pool_destroy();
@ -24,8 +22,7 @@ int main(void)
done_testing();
}
static void test_data_pool_new(void)
{
static void test_data_pool_new(void) {
{
MMDB_data_pool_s *const pool = data_pool_new(0);
ok(!pool, "size 0 is not valid");
@ -45,11 +42,8 @@ static void test_data_pool_new(void)
}
}
static void test_data_pool_destroy(void)
{
{
data_pool_destroy(NULL);
}
static void test_data_pool_destroy(void) {
{ data_pool_destroy(NULL); }
{
MMDB_data_pool_s *const pool = data_pool_new(512);
@ -58,8 +52,7 @@ static void test_data_pool_destroy(void)
}
}
static void test_data_pool_alloc(void)
{
static void test_data_pool_alloc(void) {
{
MMDB_data_pool_s *const pool = data_pool_new(1);
ok(pool != NULL, "created pool");
@ -109,20 +102,18 @@ static void test_data_pool_alloc(void)
ok(entry != NULL, "got an entry");
entry->entry_data.offset = (uint32_t)initial_size;
cmp_ok(pool->size, "==", initial_size * 2,
"size is the initial size*2");
cmp_ok(
pool->size, "==", initial_size * 2, "size is the initial size*2");
cmp_ok(pool->used, "==", 1, "used size is as expected");
MMDB_entry_data_list_s *const list = data_pool_to_list(pool);
MMDB_entry_data_list_s *element = list;
for (size_t i = 0; i < initial_size + 1; i++) {
ok(
element->entry_data.offset == (uint32_t)i,
"found offset %" PRIu32 ", should have %zu",
element->entry_data.offset,
i
);
ok(element->entry_data.offset == (uint32_t)i,
"found offset %" PRIu32 ", should have %zu",
element->entry_data.offset,
i);
element = element->next;
}
@ -133,8 +124,7 @@ static void test_data_pool_alloc(void)
}
}
static void test_data_pool_to_list(void)
{
static void test_data_pool_to_list(void) {
{
size_t const initial_size = 16;
MMDB_data_pool_s *const pool = data_pool_new(initial_size);
@ -143,8 +133,8 @@ static void test_data_pool_to_list(void)
MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool);
ok(entry1 != NULL, "got an entry");
MMDB_entry_data_list_s *const list_one_element
= data_pool_to_list(pool);
MMDB_entry_data_list_s *const list_one_element =
data_pool_to_list(pool);
ok(list_one_element != NULL, "got a list");
ok(list_one_element == entry1,
"list's first element is the first we retrieved");
@ -153,8 +143,8 @@ static void test_data_pool_to_list(void)
MMDB_entry_data_list_s *const entry2 = data_pool_alloc(pool);
ok(entry2 != NULL, "got another entry");
MMDB_entry_data_list_s *const list_two_elements
= data_pool_to_list(pool);
MMDB_entry_data_list_s *const list_two_elements =
data_pool_to_list(pool);
ok(list_two_elements != NULL, "got a list");
ok(list_two_elements == entry1,
"list's first element is the first we retrieved");
@ -176,8 +166,8 @@ static void test_data_pool_to_list(void)
MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool);
ok(entry1 != NULL, "got an entry");
MMDB_entry_data_list_s *const list_one_element
= data_pool_to_list(pool);
MMDB_entry_data_list_s *const list_one_element =
data_pool_to_list(pool);
ok(list_one_element != NULL, "got a list");
ok(list_one_element == entry1,
"list's first element is the first we retrieved");
@ -213,72 +203,54 @@ static void test_data_pool_to_list(void)
{
diag("starting test: fill one block save for one spot");
ok(
create_and_check_list(3, 2),
"fill one block save for one spot"
);
ok(create_and_check_list(3, 2), "fill one block save for one spot");
}
{
diag("starting test: fill one block");
ok(
create_and_check_list(3, 3),
"fill one block"
);
ok(create_and_check_list(3, 3), "fill one block");
}
{
diag("starting test: fill one block and use one spot in the next block");
ok(
create_and_check_list(3, 3 + 1),
"fill one block and use one spot in the next block"
);
diag(
"starting test: fill one block and use one spot in the next block");
ok(create_and_check_list(3, 3 + 1),
"fill one block and use one spot in the next block");
}
{
diag("starting test: fill two blocks save for one spot");
ok(
create_and_check_list(3, 3 + 3 * 2 - 1),
"fill two blocks save for one spot"
);
ok(create_and_check_list(3, 3 + 3 * 2 - 1),
"fill two blocks save for one spot");
}
{
diag("starting test: fill two blocks");
ok(
create_and_check_list(3, 3 + 3 * 2),
"fill two blocks"
);
ok(create_and_check_list(3, 3 + 3 * 2), "fill two blocks");
}
{
diag("starting test: fill two blocks and use one spot in the next");
ok(
create_and_check_list(3, 3 + 3 * 2 + 1),
"fill two blocks and use one spot in the next"
);
ok(create_and_check_list(3, 3 + 3 * 2 + 1),
"fill two blocks and use one spot in the next");
}
{
diag("starting test: fill three blocks save for one spot");
ok(
create_and_check_list(3, 3 + 3 * 2 + 3 * 2 * 2 - 1),
"fill three blocks save for one spot"
);
ok(create_and_check_list(3, 3 + 3 * 2 + 3 * 2 * 2 - 1),
"fill three blocks save for one spot");
}
{
diag("starting test: fill three blocks");
ok(
create_and_check_list(3, 3 + 3 * 2 + 3 * 2 * 2),
"fill three blocks"
);
ok(create_and_check_list(3, 3 + 3 * 2 + 3 * 2 * 2),
"fill three blocks");
}
// It would be nice to have a larger number of these, but it's expensive to
// run many. We currently hardcode what this will be anyway, so varying
// this is not very interesting.
size_t const initial_sizes[] = { 1, 2, 32, 64, 128, 256 };
size_t const initial_sizes[] = {1, 2, 32, 64, 128, 256};
size_t const max_element_count = 4096;
@ -296,8 +268,7 @@ static void test_data_pool_to_list(void)
// Use assert() rather than libtap as libtap is significantly slower and we run
// this frequently.
static bool create_and_check_list(size_t const initial_size,
size_t const element_count)
{
size_t const element_count) {
MMDB_data_pool_s *const pool = data_pool_new(initial_size);
assert(pool != NULL);
@ -305,8 +276,8 @@ static bool create_and_check_list(size_t const initial_size,
// Hold on to the pointers as we initially see them so that we can check
// they are still valid after building the list.
MMDB_entry_data_list_s **const entry_array
= calloc(element_count, sizeof(MMDB_entry_data_list_s *));
MMDB_entry_data_list_s **const entry_array =
calloc(element_count, sizeof(MMDB_entry_data_list_s *));
assert(entry_array != NULL);
for (size_t i = 0; i < element_count; i++) {
@ -349,8 +320,7 @@ static bool create_and_check_list(size_t const initial_size,
// Use assert() rather than libtap as libtap is significantly slower and we run
// this frequently.
static void check_block_count(MMDB_entry_data_list_s const *const list,
size_t const initial_size)
{
size_t const initial_size) {
size_t got_block_count = 0;
size_t got_element_count = 0;

View File

@ -1,57 +1,68 @@
#include "maxminddb_test_helper.h"
MMDB_entry_data_list_s *test_array_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_array_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *array = entry_data_list = entry_data_list->next;
cmp_ok(array->entry_data.type, "==", MMDB_DATA_TYPE_ARRAY,
cmp_ok(array->entry_data.type,
"==",
MMDB_DATA_TYPE_ARRAY,
"'array' key's value is an array");
cmp_ok(array->entry_data.data_size, "==", 3,
cmp_ok(array->entry_data.data_size,
"==",
3,
"'array' key's value has 3 elements");
MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
cmp_ok(idx0->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx0->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"first array entry is a UINT32");
cmp_ok(idx0->entry_data.uint32, "==", 1, "first array entry value is 1");
MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
cmp_ok(idx1->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx1->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"second array entry is a UINT32");
cmp_ok(idx1->entry_data.uint32, "==", 2, "second array entry value is 2");
MMDB_entry_data_list_s *idx2 = entry_data_list = entry_data_list->next;
cmp_ok(idx2->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx2->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"third array entry is a UINT32");
cmp_ok(idx2->entry_data.uint32, "==", 3, "third array entry value is 3");
return entry_data_list;
}
MMDB_entry_data_list_s *test_boolean_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_boolean_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_BOOLEAN,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_BOOLEAN,
"'boolean' key's value is a boolean");
ok(value->entry_data.boolean, "'boolean' key's value is true");
return entry_data_list;
}
MMDB_entry_data_list_s *test_bytes_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_bytes_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_BYTES,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_BYTES,
"'bytes' key's value is bytes");
uint8_t *bytes = malloc(value->entry_data.data_size);
if (NULL == bytes) {
BAIL_OUT("malloc failed");
}
memcpy(bytes, value->entry_data.bytes, value->entry_data.data_size);
uint8_t expect[] = { 0x00, 0x00, 0x00, 0x2a };
uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a};
ok(memcmp(bytes, expect, 4) == 0, "got expected value for bytes key");
@ -60,12 +71,13 @@ MMDB_entry_data_list_s *test_bytes_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_double_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_double_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_DOUBLE,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_DOUBLE,
"'double' key's value is a double");
compare_double(value->entry_data.double_value, 42.123456);
@ -73,12 +85,13 @@ MMDB_entry_data_list_s *test_double_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_float_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_float_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_FLOAT,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_FLOAT,
"'float' key's value is a float");
compare_float(value->entry_data.float_value, 1.1F);
@ -86,64 +99,79 @@ MMDB_entry_data_list_s *test_float_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_int32_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_int32_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_INT32,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_INT32,
"'int32' key's value is an int32");
int32_t expect = 1 << 28;
expect *= -1;
cmp_ok(value->entry_data.int32, "==", expect,
cmp_ok(value->entry_data.int32,
"==",
expect,
"got expected value for int32 key");
return entry_data_list;
}
MMDB_entry_data_list_s *test_arrayX_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_arrayX_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *arrayX = entry_data_list = entry_data_list->next;
cmp_ok(arrayX->entry_data.type, "==", MMDB_DATA_TYPE_ARRAY,
cmp_ok(arrayX->entry_data.type,
"==",
MMDB_DATA_TYPE_ARRAY,
"'map{mapX}{arrayX}' key's value is an array");
cmp_ok(arrayX->entry_data.data_size, "==", 3,
cmp_ok(arrayX->entry_data.data_size,
"==",
3,
"'map{mapX}{arrayX}' key's value has 3 elements");
MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
cmp_ok(idx0->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx0->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"first array entry is a UINT32");
cmp_ok(idx0->entry_data.uint32, "==", 7, "first array entry value is 7");
MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
cmp_ok(idx1->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx1->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"second array entry is a UINT32");
cmp_ok(idx1->entry_data.uint32, "==", 8, "second array entry value is 8");
MMDB_entry_data_list_s *idx2 = entry_data_list = entry_data_list->next;
cmp_ok(idx2->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(idx2->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"third array entry is a UINT32");
cmp_ok(idx2->entry_data.uint32, "==", 9, "third array entry value is 9");
return entry_data_list;
}
MMDB_entry_data_list_s *test_mapX_key_value_pair(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_mapX_key_value_pair(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *mapX_key = entry_data_list = entry_data_list->next;
cmp_ok(mapX_key->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(mapX_key->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"found a map key in 'map{mapX}'");
const char *mapX_key_name = dup_entry_string_or_bail(mapX_key->entry_data);
if (strcmp(mapX_key_name, "utf8_stringX") == 0) {
MMDB_entry_data_list_s *mapX_value =
entry_data_list = entry_data_list->next;
cmp_ok(mapX_value->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
MMDB_entry_data_list_s *mapX_value = entry_data_list =
entry_data_list->next;
cmp_ok(mapX_value->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"'map{mapX}{utf8_stringX}' type is utf8_string");
const char *utf8_stringX_value = dup_entry_string_or_bail(
mapX_value->entry_data);
const char *utf8_stringX_value =
dup_entry_string_or_bail(mapX_value->entry_data);
ok(strcmp(utf8_stringX_value, "hello") == 0,
"map{mapX}{utf8_stringX} value is 'hello'");
free((void *)utf8_stringX_value);
@ -158,16 +186,22 @@ MMDB_entry_data_list_s *test_mapX_key_value_pair(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_map_value(MMDB_entry_data_list_s *entry_data_list)
{
MMDB_entry_data_list_s *
test_map_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *map = entry_data_list = entry_data_list->next;
cmp_ok(map->entry_data.type, "==", MMDB_DATA_TYPE_MAP,
cmp_ok(map->entry_data.type,
"==",
MMDB_DATA_TYPE_MAP,
"'map' key's value is a map");
cmp_ok(map->entry_data.data_size, "==", 1,
cmp_ok(map->entry_data.data_size,
"==",
1,
"'map' key's value has 1 key/value pair");
MMDB_entry_data_list_s *map_key_1 = entry_data_list = entry_data_list->next;
cmp_ok(map_key_1->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(map_key_1->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"found a map key in 'map'");
const char *map_key_1_name =
dup_entry_string_or_bail(map_key_1->entry_data);
@ -175,9 +209,13 @@ MMDB_entry_data_list_s *test_map_value(MMDB_entry_data_list_s *entry_data_list)
free((void *)map_key_1_name);
MMDB_entry_data_list_s *mapX = entry_data_list = entry_data_list->next;
cmp_ok(mapX->entry_data.type, "==", MMDB_DATA_TYPE_MAP,
cmp_ok(mapX->entry_data.type,
"==",
MMDB_DATA_TYPE_MAP,
"'map{mapX}' key's value is a map");
cmp_ok(mapX->entry_data.data_size, "==", 2,
cmp_ok(mapX->entry_data.data_size,
"==",
2,
"'map' key's value has 2 key/value pairs");
entry_data_list = test_mapX_key_value_pair(entry_data_list);
@ -186,17 +224,32 @@ MMDB_entry_data_list_s *test_map_value(MMDB_entry_data_list_s *entry_data_list)
return entry_data_list;
}
MMDB_entry_data_list_s *test_uint128_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_uint128_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UINT128,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT128,
"'uint128' key's value is an uint128");
#if MMDB_UINT128_IS_BYTE_ARRAY
uint8_t expect[16] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t expect[16] = {0x01,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00};
ok(memcmp(value->entry_data.uint128, expect, 16) == 0,
"uint128 field is 2**120");
#else
@ -208,12 +261,13 @@ MMDB_entry_data_list_s *test_uint128_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_uint16_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_uint16_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UINT16,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT16,
"'uint16' key's value is an uint16");
uint16_t expect = 100;
ok(value->entry_data.uint16 == expect, "uint16 field is 100");
@ -221,12 +275,13 @@ MMDB_entry_data_list_s *test_uint16_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_uint32_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_uint32_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"'uint32' key's value is an uint32");
uint32_t expect = 1 << 28;
cmp_ok(value->entry_data.uint32, "==", expect, "uint32 field is 100");
@ -234,12 +289,13 @@ MMDB_entry_data_list_s *test_uint32_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_uint64_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_uint64_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UINT64,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UINT64,
"'uint64' key's value is an uint64");
uint64_t expect = 1;
expect <<= 60;
@ -248,18 +304,35 @@ MMDB_entry_data_list_s *test_uint64_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_utf8_string_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_utf8_string_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"'utf8_string' key's value is a string");
const char *utf8_string = dup_entry_string_or_bail(value->entry_data);
// This is hex for "unicode! ☯ - ♫" as bytes
char expect[19] =
{ 0x75, 0x6e, 0x69, 0x63, 0x6f, 0x64, 0x65, 0x21, 0x20, 0xe2, 0x98,
0xaf, 0x20, 0x2d, 0x20, 0xe2, 0x99, 0xab, 0x00 };
char expect[19] = {0x75,
0x6e,
0x69,
0x63,
0x6f,
0x64,
0x65,
0x21,
0x20,
0xe2,
0x98,
0xaf,
0x20,
0x2d,
0x20,
0xe2,
0x99,
0xab,
0x00};
is(utf8_string, expect, "got expected value for utf8_string key");
@ -268,8 +341,7 @@ MMDB_entry_data_list_s *test_utf8_string_value(MMDB_entry_data_list_s
return entry_data_list;
}
void run_tests(int mode, const char *description)
{
void run_tests(int mode, const char *description) {
const char *filename = "MaxMind-DB-test-decoder.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, description);
@ -286,15 +358,19 @@ void run_tests(int mode, const char *description)
BAIL_OUT("MMDB_get_entry_data_list failed with %s",
MMDB_strerror(status));
} else {
cmp_ok(status, "==", MMDB_SUCCESS,
"MMDB_get_entry_data_list succeeded");
cmp_ok(
status, "==", MMDB_SUCCESS, "MMDB_get_entry_data_list succeeded");
}
first = entry_data_list;
cmp_ok(entry_data_list->entry_data.type, "==", MMDB_DATA_TYPE_MAP,
cmp_ok(entry_data_list->entry_data.type,
"==",
MMDB_DATA_TYPE_MAP,
"first entry in entry data list is a map");
cmp_ok(entry_data_list->entry_data.data_size, "==", 12,
cmp_ok(entry_data_list->entry_data.data_size,
"==",
12,
"first map in entry data list has 12 k/v pairs");
while (1) {
@ -304,7 +380,9 @@ void run_tests(int mode, const char *description)
break;
}
cmp_ok(key->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(key->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"found a map key");
const char *key_name = dup_entry_string_or_bail(key->entry_data);
@ -345,8 +423,7 @@ void run_tests(int mode, const char *description)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,21 +1,40 @@
#include "maxminddb_test_helper.h"
void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
const char *UNUSED(filename), const char *mode_desc)
{
void test_all_data_types(MMDB_lookup_result_s *result,
const char *ip,
const char *UNUSED(filename),
const char *mode_desc) {
{
char description[500];
snprintf(description, 500, "utf8_string field for %s - %s", ip,
mode_desc);
snprintf(
description, 500, "utf8_string field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_UTF8_STRING, description,
"utf8_string", NULL);
MMDB_entry_data_s data = data_ok(result,
MMDB_DATA_TYPE_UTF8_STRING,
description,
"utf8_string",
NULL);
const char *string = mmdb_strndup(data.utf8_string, data.data_size);
// This is hex for "unicode! ☯ - ♫" as bytes
char expect[19] =
{ 0x75, 0x6e, 0x69, 0x63, 0x6f, 0x64, 0x65, 0x21, 0x20, 0xe2, 0x98,
0xaf, 0x20, 0x2d, 0x20, 0xe2, 0x99, 0xab, 0x00 };
char expect[19] = {0x75,
0x6e,
0x69,
0x63,
0x6f,
0x64,
0x65,
0x21,
0x20,
0xe2,
0x98,
0xaf,
0x20,
0x2d,
0x20,
0xe2,
0x99,
0xab,
0x00};
is(string, expect, "got expected utf8_string value");
free((char *)string);
@ -47,7 +66,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_BYTES, description, "bytes", NULL);
uint8_t expect[] = { 0x00, 0x00, 0x00, 0x2a };
uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a};
ok(memcmp((uint8_t *)data.bytes, expect, 4) == 0,
"bytes field has expected value");
}
@ -98,12 +117,25 @@ void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
char description[500];
snprintf(description, 500, "uint128 field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_UINT128, description, "uint128",
NULL);
MMDB_entry_data_s data = data_ok(
result, MMDB_DATA_TYPE_UINT128, description, "uint128", NULL);
#if MMDB_UINT128_IS_BYTE_ARRAY
uint8_t expect[16] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t expect[16] = {0x01,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00};
ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 2**120");
#else
mmdb_uint128_t expect = 1;
@ -116,9 +148,8 @@ void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
char description[500];
snprintf(description, 500, "boolean field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean",
NULL);
MMDB_entry_data_s data = data_ok(
result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL);
cmp_ok(data.boolean, "==", true, "boolean field is true");
}
@ -131,21 +162,18 @@ void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
ok(data.data_size == 3, "array field has 3 elements");
snprintf(description, 500, "array[0] for %s - %s", ip, mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "array", "0",
NULL);
data = data_ok(
result, MMDB_DATA_TYPE_UINT32, description, "array", "0", NULL);
ok(data.uint32 == 1, "array[0] is 1");
snprintf(description, 500, "array[1] for %s - %s", ip, mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "array", "1",
NULL);
data = data_ok(
result, MMDB_DATA_TYPE_UINT32, description, "array", "1", NULL);
ok(data.uint32 == 2, "array[1] is 1");
snprintf(description, 500, "array[2] for %s - %s", ip, mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "array", "2",
NULL);
data = data_ok(
result, MMDB_DATA_TYPE_UINT32, description, "array", "2", NULL);
ok(data.uint32 == 3, "array[2] is 1");
}
@ -159,64 +187,99 @@ void test_all_data_types(MMDB_lookup_result_s *result, const char *ip,
snprintf(description, 500, "map{mapX} for %s - %s", ip, mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_MAP, description, "map", "mapX",
NULL);
data = data_ok(
result, MMDB_DATA_TYPE_MAP, description, "map", "mapX", NULL);
ok(data.data_size == 2, "map{mapX} field has 2 elements");
snprintf(description, 500, "map{mapX}{utf8_stringX} for %s - %s", ip,
snprintf(description,
500,
"map{mapX}{utf8_stringX} for %s - %s",
ip,
mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UTF8_STRING, description, "map",
"mapX", "utf8_stringX", NULL);
data = data_ok(result,
MMDB_DATA_TYPE_UTF8_STRING,
description,
"map",
"mapX",
"utf8_stringX",
NULL);
const char *string = mmdb_strndup(data.utf8_string, data.data_size);
is(string, "hello", "map{mapX}{utf8_stringX} is 'hello'");
free((char *)string);
snprintf(description, 500, "map{mapX}{arrayX} for %s - %s", ip,
mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_ARRAY, description, "map", "mapX",
"arrayX", NULL);
snprintf(
description, 500, "map{mapX}{arrayX} for %s - %s", ip, mode_desc);
data = data_ok(result,
MMDB_DATA_TYPE_ARRAY,
description,
"map",
"mapX",
"arrayX",
NULL);
ok(data.data_size == 3, "map{mapX}{arrayX} field has 3 elements");
snprintf(description, 500, "map{mapX}{arrayX}[0] for %s - %s", ip,
snprintf(description,
500,
"map{mapX}{arrayX}[0] for %s - %s",
ip,
mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "map", "mapX",
"arrayX", "0", NULL);
data = data_ok(result,
MMDB_DATA_TYPE_UINT32,
description,
"map",
"mapX",
"arrayX",
"0",
NULL);
ok(data.uint32 == 7, "map{mapX}{arrayX}[0] is 7");
snprintf(description, 500, "map{mapX}{arrayX}[1] for %s - %s", ip,
snprintf(description,
500,
"map{mapX}{arrayX}[1] for %s - %s",
ip,
mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "map", "mapX",
"arrayX", "1", NULL);
data = data_ok(result,
MMDB_DATA_TYPE_UINT32,
description,
"map",
"mapX",
"arrayX",
"1",
NULL);
ok(data.uint32 == 8, "map{mapX}{arrayX}[1] is 8");
snprintf(description, 500, "map{mapX}{arrayX}[2] for %s - %s", ip,
snprintf(description,
500,
"map{mapX}{arrayX}[2] for %s - %s",
ip,
mode_desc);
data =
data_ok(result, MMDB_DATA_TYPE_UINT32, description, "map", "mapX",
"arrayX", "2", NULL);
data = data_ok(result,
MMDB_DATA_TYPE_UINT32,
description,
"map",
"mapX",
"arrayX",
"2",
NULL);
ok(data.uint32 == 9, "map{mapX}{arrayX}[2] is 9");
}
}
void test_all_data_types_as_zero(MMDB_lookup_result_s *result, const char *ip,
const char *UNUSED(
filename), const char *mode_desc)
{
void test_all_data_types_as_zero(MMDB_lookup_result_s *result,
const char *ip,
const char *UNUSED(filename),
const char *mode_desc) {
{
char description[500];
snprintf(description, 500, "utf8_string field for %s - %s", ip,
mode_desc);
snprintf(
description, 500, "utf8_string field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_UTF8_STRING, description,
"utf8_string", NULL);
MMDB_entry_data_s data = data_ok(result,
MMDB_DATA_TYPE_UTF8_STRING,
description,
"utf8_string",
NULL);
is(data.utf8_string, "", "got expected utf8_string value (NULL)");
}
@ -249,7 +312,8 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, const char *ip,
ok(data.data_size == 0, "bytes field data_size is 0");
/* In C does it makes sense to write something like this?
uint8_t expect[0] = {};
ok(memcmp(data.bytes, expect, 0) == 0, "got expected bytes value (NULL)"); */
ok(memcmp(data.bytes, expect, 0) == 0, "got expected bytes value
(NULL)"); */
}
{
@ -297,12 +361,25 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, const char *ip,
char description[500];
snprintf(description, 500, "uint128 field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_UINT128, description, "uint128",
NULL);
MMDB_entry_data_s data = data_ok(
result, MMDB_DATA_TYPE_UINT128, description, "uint128", NULL);
#if MMDB_UINT128_IS_BYTE_ARRAY
uint8_t expect[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t expect[16] = {0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00};
ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 0");
#else
mmdb_uint128_t expect = 0;
@ -314,9 +391,8 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, const char *ip,
char description[500];
snprintf(description, 500, "boolean field for %s - %s", ip, mode_desc);
MMDB_entry_data_s data =
data_ok(result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean",
NULL);
MMDB_entry_data_s data = data_ok(
result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL);
cmp_ok(data.boolean, "==", false, "boolean field is false");
}
@ -339,8 +415,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, const char *ip,
}
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-decoder.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -360,11 +435,15 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s result =
MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
cmp_ok(gai_error, "==", EAI_NONAME,
"MMDB_lookup populates getaddrinfo error properly - %s", ip);
cmp_ok(gai_error,
"==",
EAI_NONAME,
"MMDB_lookup populates getaddrinfo error properly - %s",
ip);
ok(!result.found_entry,
"no result entry struct returned for invalid IP address '%s'", ip);
"no result entry struct returned for invalid IP address '%s'",
ip);
}
{
@ -372,10 +451,12 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
ok(
!result.found_entry,
"no result entry struct returned for IP address not in the database - %s - %s - %s",
ip, filename, mode_desc);
ok(!result.found_entry,
"no result entry struct returned for IP address not in the database "
"- %s - %s - %s",
ip,
filename,
mode_desc);
}
{
@ -383,15 +464,21 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
ok(
result.found_entry,
"got a result entry struct for IP address in the database - %s - %s - %s",
ip, filename, mode_desc);
ok(result.found_entry,
"got a result entry struct for IP address in the database - %s - %s "
"- %s",
ip,
filename,
mode_desc);
cmp_ok(
result.entry.offset, ">", 0,
"result.entry.offset > 0 for address in the database - %s - %s - %s",
ip, filename, mode_desc);
cmp_ok(result.entry.offset,
">",
0,
"result.entry.offset > 0 for address in the database - %s - %s "
"- %s",
ip,
filename,
mode_desc);
test_all_data_types(&result, ip, filename, mode_desc);
}
@ -401,15 +488,21 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
ok(
result.found_entry,
"got a result entry struct for IP address in the database - %s - %s - %s",
ip, filename, mode_desc);
ok(result.found_entry,
"got a result entry struct for IP address in the database - %s - %s "
"- %s",
ip,
filename,
mode_desc);
cmp_ok(
result.entry.offset, ">", 0,
"result.entry.offset > 0 for address in the database - %s - %s - %s",
ip, filename, mode_desc);
cmp_ok(result.entry.offset,
">",
0,
"result.entry.offset > 0 for address in the database - %s - %s "
"- %s",
ip,
filename,
mode_desc);
test_all_data_types(&result, ip, filename, mode_desc);
}
@ -419,10 +512,12 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
ok(
result.found_entry,
"got a result entry struct for IP address in the database - %s - %s - %s",
ip, filename, mode_desc);
ok(result.found_entry,
"got a result entry struct for IP address in the database - %s - %s "
"- %s",
ip,
filename,
mode_desc);
test_all_data_types_as_zero(&result, ip, filename, mode_desc);
}
@ -431,8 +526,7 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -2,8 +2,7 @@
#include "maxminddb_test_helper.h"
#ifdef HAVE_OPEN_MEMSTREAM
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-decoder.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -16,8 +15,7 @@ void run_tests(int mode, const char *mode_desc)
MMDB_entry_data_list_s *entry_data_list;
int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
ok(MMDB_SUCCESS == status,
"MMDB_get_entry_data_list is successful");
ok(MMDB_SUCCESS == status, "MMDB_get_entry_data_list is successful");
char *dump_output;
size_t dump_size;
@ -32,54 +30,53 @@ void run_tests(int mode, const char *mode_desc)
cmp_ok(dump_size, ">", 0, "MMDB_dump produced output - %s", mode_desc);
char *expect[] = {
"{",
" \"array\": ",
" [",
" 1 <uint32>",
" 2 <uint32>",
" 3 <uint32>",
" ]",
" \"boolean\": ",
" true <boolean>",
" \"bytes\": ",
" 0000002A <bytes>",
" \"double\": ",
" 42.123456 <double>",
" \"float\": ",
" 1.100000 <float>",
" \"int32\": ",
" -268435456 <int32>",
" \"map\": ",
" {",
" \"mapX\": ",
" {",
" \"arrayX\": ",
" [",
" 7 <uint32>",
" 8 <uint32>",
" 9 <uint32>",
" ]",
" \"utf8_stringX\": ",
" \"hello\" <utf8_string>",
" }",
" }",
" \"uint128\": ",
" 0x01000000000000000000000000000000 <uint128>",
" \"uint16\": ",
" 100 <uint16>",
" \"uint32\": ",
" 268435456 <uint32>",
" \"uint64\": ",
" 1152921504606846976 <uint64>",
" \"utf8_string\": ",
" \"unicode! ☯ - ♫\" <utf8_string>",
"}"
};
char *expect[] = {"{",
" \"array\": ",
" [",
" 1 <uint32>",
" 2 <uint32>",
" 3 <uint32>",
" ]",
" \"boolean\": ",
" true <boolean>",
" \"bytes\": ",
" 0000002A <bytes>",
" \"double\": ",
" 42.123456 <double>",
" \"float\": ",
" 1.100000 <float>",
" \"int32\": ",
" -268435456 <int32>",
" \"map\": ",
" {",
" \"mapX\": ",
" {",
" \"arrayX\": ",
" [",
" 7 <uint32>",
" 8 <uint32>",
" 9 <uint32>",
" ]",
" \"utf8_stringX\": ",
" \"hello\" <utf8_string>",
" }",
" }",
" \"uint128\": ",
" 0x01000000000000000000000000000000 <uint128>",
" \"uint16\": ",
" 100 <uint16>",
" \"uint32\": ",
" 268435456 <uint32>",
" \"uint64\": ",
" 1152921504606846976 <uint64>",
" \"utf8_string\": ",
" \"unicode! ☯ - ♫\" <utf8_string>",
"}"};
for (int i = 0; i < 42; i++) {
ok((strstr(dump_output, expect[i]) != NULL),
"dump output contains expected line (%s) - %s", expect[i],
"dump output contains expected line (%s) - %s",
expect[i],
mode_desc);
}
@ -89,15 +86,13 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();
}
#else
int main(void)
{
int main(void) {
plan(SKIP_ALL, "This test requires the open_memstream() function");
}
#endif

View File

@ -14,15 +14,20 @@
* exists before checking to see if the lookups are correct.
*/
void test_one_ip(MMDB_s *mmdb, const char *filename, const char *mode_desc,
char *ip, char *country_code)
{
void test_one_ip(MMDB_s *mmdb,
const char *filename,
const char *mode_desc,
char *ip,
char *country_code) {
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
MMDB_entry_data_s entry_data =
data_ok(&result, MMDB_DATA_TYPE_UTF8_STRING, "country{iso_code}",
"country", "iso_code", NULL);
MMDB_entry_data_s entry_data = data_ok(&result,
MMDB_DATA_TYPE_UTF8_STRING,
"country{iso_code}",
"country",
"iso_code",
NULL);
if (ok(entry_data.has_data, "found data for country{iso_code}")) {
char *string =
@ -31,16 +36,16 @@ void test_one_ip(MMDB_s *mmdb, const char *filename, const char *mode_desc,
ok(0, "mmdb_strndup() call failed");
exit(1);
}
if (!ok(strcmp(string,
country_code) == 0, "iso_code is %s", country_code)) {
if (!ok(strcmp(string, country_code) == 0,
"iso_code is %s",
country_code)) {
diag(" value is %s", string);
}
free(string);
}
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "GeoIP2-City-Test.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -50,17 +55,16 @@ void run_tests(int mode, const char *mode_desc)
* another part of the data section. */
test_one_ip(mmdb, filename, mode_desc, "2001:218::", "JP");
/* This exercises a bug where one subnet's data shares part of the data
* with another subnet - in this case it is the "country" key (and others)
* in the top level map. We are testing that the "country" key's value is
* handled correctly. The value _should_ be a pointer to another map. */
* with another subnet - in this case it is the "country" key (and others)
* in the top level map. We are testing that the "country" key's value is
* handled correctly. The value _should_ be a pointer to another map. */
test_one_ip(mmdb, filename, mode_desc, "81.2.69.160", "GB");
MMDB_close(mmdb);
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,52 +1,70 @@
#include "maxminddb_test_helper.h"
void test_array_0_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(status, "==", MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[0]", function);
void test_array_0_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[0]",
function);
ok(entry_data.has_data, "found a value for array[0]");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - array[0]");
cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[0]");
}
void test_array_2_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(status, "==", MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[2]", function);
void test_array_2_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[2]",
function);
ok(entry_data.has_data, "found a value for array[2]");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - array[2]");
cmp_ok(entry_data.uint32, "==", 3, "entry value is 3 - array[2]");
}
void test_array_minus_3_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(status, "==", MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[-3]", function);
void test_array_minus_3_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[-3]",
function);
ok(entry_data.has_data, "found a value for array[-3]");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - array[-3]");
cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[-3]");
}
void test_array_minus_1_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(status, "==", MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[-1]", function);
void test_array_minus_1_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - array[-1]",
function);
ok(entry_data.has_data, "found a value for array[-1]");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - array[-1]");
cmp_ok(entry_data.uint32, "==", 3, "entry value is 3 - array[-1]");
}
int call_vget_value(MMDB_entry_s *entry, MMDB_entry_data_s *entry_data, ...)
{
int call_vget_value(MMDB_entry_s *entry, MMDB_entry_data_s *entry_data, ...) {
va_list keys;
va_start(keys, entry_data);
@ -57,8 +75,7 @@ int call_vget_value(MMDB_entry_s *entry, MMDB_entry_data_s *entry_data, ...)
return status;
}
void test_simple_structure(int mode, const char *mode_desc)
{
void test_simple_structure(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-decoder.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -70,7 +87,7 @@ void test_simple_structure(int mode, const char *mode_desc)
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] = { "array", "0", NULL };
const char *lookup_path[] = {"array", "0", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_array_0_result(status, entry_data, "MMDB_aget_value");
@ -84,7 +101,7 @@ void test_simple_structure(int mode, const char *mode_desc)
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] = { "array", "2", NULL };
const char *lookup_path[] = {"array", "2", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_array_2_result(status, entry_data, "MMDB_aget_value");
@ -96,18 +113,19 @@ void test_simple_structure(int mode, const char *mode_desc)
test_array_2_result(status, entry_data, "MMDB_vget_value");
}
{
MMDB_entry_data_s entry_data;
int status = MMDB_get_value(&result.entry, &entry_data, "array", "zero",
NULL);
cmp_ok(status, "==", MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
int status =
MMDB_get_value(&result.entry, &entry_data, "array", "zero", NULL);
cmp_ok(status,
"==",
MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
"MMDB_get_value() returns error on non-integer array index");
}
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] = { "array", "-1", NULL };
const char *lookup_path[] = {"array", "-1", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_array_minus_1_result(status, entry_data, "MMDB_aget_value");
@ -122,7 +140,7 @@ void test_simple_structure(int mode, const char *mode_desc)
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] = { "array", "-3", NULL };
const char *lookup_path[] = {"array", "-3", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_array_minus_3_result(status, entry_data, "MMDB_aget_value");
@ -137,77 +155,92 @@ void test_simple_structure(int mode, const char *mode_desc)
{
MMDB_entry_data_s entry_data;
int status = MMDB_get_value(&result.entry, &entry_data, "array", "-4",
NULL);
cmp_ok(status, "==", MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
int status =
MMDB_get_value(&result.entry, &entry_data, "array", "-4", NULL);
cmp_ok(status,
"==",
MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
"MMDB_get_value() returns error on too large negative integer");
}
{
MMDB_entry_data_s entry_data;
int status =
MMDB_get_value(&result.entry, &entry_data, "array",
"-18446744073709551616",
NULL);
cmp_ok(status, "==", MMDB_INVALID_LOOKUP_PATH_ERROR,
"MMDB_get_value() returns error on integer smaller than LONG_MIN");
int status = MMDB_get_value(
&result.entry, &entry_data, "array", "-18446744073709551616", NULL);
cmp_ok(
status,
"==",
MMDB_INVALID_LOOKUP_PATH_ERROR,
"MMDB_get_value() returns error on integer smaller than LONG_MIN");
}
{
MMDB_entry_data_s entry_data;
int status =
MMDB_get_value(&result.entry, &entry_data, "array",
"18446744073709551616",
NULL);
cmp_ok(status, "==", MMDB_INVALID_LOOKUP_PATH_ERROR,
"MMDB_get_value() returns error on integer larger than LONG_MAX");
int status = MMDB_get_value(
&result.entry, &entry_data, "array", "18446744073709551616", NULL);
cmp_ok(
status,
"==",
MMDB_INVALID_LOOKUP_PATH_ERROR,
"MMDB_get_value() returns error on integer larger than LONG_MAX");
}
MMDB_close(mmdb);
free(mmdb);
}
void test_complex_map_a_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(status, "==", MMDB_SUCCESS,
void test_complex_map_a_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{a}",
function);
ok(entry_data.has_data,
"found a value for map1{map2}{array}[0]{map3}{a}");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
ok(entry_data.has_data, "found a value for map1{map2}{array}[0]{map3}{a}");
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - map1{map2}{array}[0]{map3}{a}");
cmp_ok(entry_data.uint32, "==", 1,
cmp_ok(entry_data.uint32,
"==",
1,
"entry value is 1 - map1{map2}{array}[0]{map3}{a}");
}
void test_complex_map_c_result(int status, MMDB_entry_data_s entry_data,
char *function)
{
cmp_ok(
status, "==", MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{c}",
function);
ok(entry_data.has_data,
"found a value for map1{map2}{array}[0]{map3}{c}");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
void test_complex_map_c_result(int status,
MMDB_entry_data_s entry_data,
char *function) {
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{c}",
function);
ok(entry_data.has_data, "found a value for map1{map2}{array}[0]{map3}{c}");
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UINT32,
"returned entry type is uint32 - map1{map2}{array}[0]{map3}{c}");
cmp_ok(entry_data.uint32, "==", 3,
cmp_ok(entry_data.uint32,
"==",
3,
"entry value is 3 - map1{map2}{array}[0]{map3}{c}");
}
void test_no_result(int status, MMDB_entry_data_s entry_data, char *function,
char *path_description)
{
cmp_ok(status, "==", MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
void test_no_result(int status,
MMDB_entry_data_s entry_data,
char *function,
char *path_description) {
cmp_ok(status,
"==",
MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
"status for %s() is MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR - %s",
function, path_description);
function,
path_description);
ok(!entry_data.has_data, "did not find a value for %s", path_description);
}
void test_nested_structure(int mode, const char *mode_desc)
{
void test_nested_structure(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-nested.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -219,79 +252,139 @@ void test_nested_structure(int mode, const char *mode_desc)
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] =
{ "map1", "map2", "array", "0", "map3", "a", NULL };
const char *lookup_path[] = {
"map1", "map2", "array", "0", "map3", "a", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_complex_map_a_result(status, entry_data, "MMDB_aget_value");
status = MMDB_get_value(&result.entry, &entry_data,
"map1", "map2", "array", "0", "map3", "a",
status = MMDB_get_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"0",
"map3",
"a",
NULL);
test_complex_map_a_result(status, entry_data, "MMDB_get_value");
status = call_vget_value(&result.entry, &entry_data,
"map1", "map2", "array", "0", "map3", "a",
status = call_vget_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"0",
"map3",
"a",
NULL);
test_complex_map_a_result(status, entry_data, "MMDB_vget_value");
}
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] =
{ "map1", "map2", "array", "0", "map3", "c", NULL };
const char *lookup_path[] = {
"map1", "map2", "array", "0", "map3", "c", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_complex_map_c_result(status, entry_data, "MMDB_aget_value");
status = MMDB_get_value(&result.entry, &entry_data,
"map1", "map2", "array", "0", "map3", "c",
status = MMDB_get_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"0",
"map3",
"c",
NULL);
test_complex_map_c_result(status, entry_data, "MMDB_get_value");
status = call_vget_value(&result.entry, &entry_data,
"map1", "map2", "array", "0", "map3", "c",
status = call_vget_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"0",
"map3",
"c",
NULL);
test_complex_map_c_result(status, entry_data, "MMDB_vget_value");
}
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] =
{ "map1", "map42", "array", "0", "map3", "c", NULL };
const char *lookup_path[] = {
"map1", "map42", "array", "0", "map3", "c", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_no_result(status, entry_data, "MMDB_aget_value",
test_no_result(status,
entry_data,
"MMDB_aget_value",
"map1{map42}{array}[0]{map3}{c}");
status = MMDB_get_value(&result.entry, &entry_data,
"map1", "map42", "array", "0", "map3", "c",
status = MMDB_get_value(&result.entry,
&entry_data,
"map1",
"map42",
"array",
"0",
"map3",
"c",
NULL);
test_no_result(status, entry_data, "MMDB_get_value",
test_no_result(status,
entry_data,
"MMDB_get_value",
"map1{map42}{array}[0]{map3}{c}");
status = call_vget_value(&result.entry, &entry_data,
"map1", "map42", "array", "0", "map3", "c",
status = call_vget_value(&result.entry,
&entry_data,
"map1",
"map42",
"array",
"0",
"map3",
"c",
NULL);
test_no_result(status, entry_data, "MMDB_vget_value",
test_no_result(status,
entry_data,
"MMDB_vget_value",
"map1{map42}{array}[0]{map3}{c}");
}
{
MMDB_entry_data_s entry_data;
const char *lookup_path[] =
{ "map1", "map2", "array", "9", "map3", "c", NULL };
const char *lookup_path[] = {
"map1", "map2", "array", "9", "map3", "c", NULL};
int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
test_no_result(status, entry_data, "MMDB_aget_value",
test_no_result(status,
entry_data,
"MMDB_aget_value",
"map1{map42}{array}[9]{map3}{c}");
status = MMDB_get_value(&result.entry, &entry_data,
"map1", "map2", "array", "9", "map3", "c",
status = MMDB_get_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"9",
"map3",
"c",
NULL);
test_no_result(status, entry_data, "MMDB_get_value",
test_no_result(status,
entry_data,
"MMDB_get_value",
"map1{map42}{array}[9]{map3}{c}");
status = call_vget_value(&result.entry, &entry_data,
"map1", "map2", "array", "9", "map3", "c",
status = call_vget_value(&result.entry,
&entry_data,
"map1",
"map2",
"array",
"9",
"map3",
"c",
NULL);
test_no_result(status, entry_data, "MMDB_vget_value",
test_no_result(status,
entry_data,
"MMDB_vget_value",
"map1{map42}{array}[9]{map3}{c}");
}
@ -299,14 +392,12 @@ void test_nested_structure(int mode, const char *mode_desc)
free(mmdb);
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
test_simple_structure(mode, mode_desc);
test_nested_structure(mode, mode_desc);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,21 +1,22 @@
#include "maxminddb_test_helper.h"
void test_one_ip(MMDB_s *mmdb, const char *ip, const char *filename,
const char *mode_desc)
{
void test_one_ip(MMDB_s *mmdb,
const char *ip,
const char *filename,
const char *mode_desc) {
MMDB_lookup_result_s result =
lookup_string_ok(mmdb, ip, filename, mode_desc);
ok(
result.found_entry,
"got a result for an IPv4 address included in a larger-than-IPv4 subnet - %s - %s",
ip, mode_desc);
ok(result.found_entry,
"got a result for an IPv4 address included in a larger-than-IPv4 subnet "
"- %s - %s",
ip,
mode_desc);
data_ok(&result, MMDB_DATA_TYPE_UTF8_STRING, "string value for IP", NULL);
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-no-ipv4-search-tree.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -28,8 +29,7 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,7 +1,6 @@
#include "maxminddb_test_helper.h"
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-ipv4-28.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -12,18 +11,18 @@ void run_tests(int mode, const char *mode_desc)
MMDB_lookup_result_s UNUSED(result) =
MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
cmp_ok(
mmdb_error, "==", MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR,
"MMDB_lookup_string sets mmdb_error to MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR when we try to look up an IPv6 address in an IPv4-only database");
cmp_ok(mmdb_error,
"==",
MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR,
"MMDB_lookup_string sets mmdb_error to "
"MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR when we try to look up an "
"IPv6 address in an IPv4-only database");
struct addrinfo hints = {
.ai_family = AF_INET6,
.ai_flags = AI_NUMERICHOST
};
struct addrinfo hints = {.ai_family = AF_INET6, .ai_flags = AI_NUMERICHOST};
struct addrinfo *addresses;
gai_error = getaddrinfo("2001:db8:85a3:0:0:8a2e:370:7334", NULL,
&hints, &addresses);
gai_error = getaddrinfo(
"2001:db8:85a3:0:0:8a2e:370:7334", NULL, &hints, &addresses);
if (gai_error) {
BAIL_OUT("getaddrinfo failed: %s", gai_strerror(gai_error));
}
@ -31,17 +30,19 @@ void run_tests(int mode, const char *mode_desc)
mmdb_error = 0;
MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, &mmdb_error);
cmp_ok(
mmdb_error, "==", MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR,
"MMDB_lookup_sockaddr sets mmdb_error to MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR when we try to look up an IPv6 address in an IPv4-only database");
cmp_ok(mmdb_error,
"==",
MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR,
"MMDB_lookup_sockaddr sets mmdb_error to "
"MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR when we try to look up an "
"IPv6 address in an IPv4-only database");
freeaddrinfo(addresses);
MMDB_close(mmdb);
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -18,10 +18,10 @@
#endif
void for_all_record_sizes(const char *filename_fmt,
void (*tests)(int record_size, const char *filename,
const char *description))
{
int sizes[] = { 24, 28, 32 };
void (*tests)(int record_size,
const char *filename,
const char *description)) {
int sizes[] = {24, 28, 32};
for (int i = 0; i < 3; i++) {
int size = sizes[i];
@ -35,13 +35,11 @@ void for_all_record_sizes(const char *filename_fmt,
}
}
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");
}
const char *test_database_path(const char *filename)
{
const char *test_database_path(const char *filename) {
char *test_db_dir;
#ifdef _WIN32
test_db_dir = "../t/maxmind-db/test-data";
@ -64,10 +62,9 @@ const char *test_database_path(const char *filename)
return (const char *)path;
}
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);
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);
if (NULL == string) {
BAIL_OUT("mmdb_strndup failed");
}
@ -75,12 +72,11 @@ const char *dup_entry_string_or_bail(MMDB_entry_data_s entry_data)
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)) {
BAIL_OUT(
"could not read the specified file - %s\nIf you are in a git checkout try running 'git submodule update --init'",
db_file);
BAIL_OUT("could not read the specified file - %s\nIf you are in a git "
"checkout try running 'git submodule update --init'",
db_file);
}
MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s));
@ -91,8 +87,10 @@ MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc)
int status = MMDB_open(db_file, mode, mmdb);
int is_ok = ok(MMDB_SUCCESS == status, "open %s status is success - %s",
db_file, mode_desc);
int is_ok = ok(MMDB_SUCCESS == status,
"open %s status is success - %s",
db_file,
mode_desc);
if (!is_ok) {
diag("open status code = %d (%s)", status, MMDB_strerror(status));
@ -102,7 +100,8 @@ MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc)
is_ok = ok(mmdb->file_size > 0,
"mmdb struct has been set for %s - %s",
db_file, mode_desc);
db_file,
mode_desc);
if (!is_ok) {
free(mmdb);
@ -112,26 +111,26 @@ MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc)
return mmdb;
}
MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb, const char *ip,
const char *file, const char *mode_desc)
{
MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc) {
int gai_error, mmdb_error;
MMDB_lookup_result_s result =
MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
test_lookup_errors(gai_error, mmdb_error, "MMDB_lookup_string", ip, file,
mode_desc);
test_lookup_errors(
gai_error, mmdb_error, "MMDB_lookup_string", ip, file, mode_desc);
return result;
}
MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb, const char *ip,
const char *file, const char *mode_desc)
{
MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc) {
int ai_flags = AI_NUMERICHOST;
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM
};
struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
struct addrinfo *addresses = NULL;
if (ip[0] == ':') {
@ -148,7 +147,7 @@ MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb, const char *ip,
int gai_error = getaddrinfo(ip, NULL, &hints, &addresses);
int mmdb_error = 0;
MMDB_lookup_result_s result = { .found_entry = false };
MMDB_lookup_result_s result = {.found_entry = false};
if (gai_error == 0) {
result = MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, &mmdb_error);
}
@ -156,38 +155,48 @@ MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb, const char *ip,
freeaddrinfo(addresses);
}
test_lookup_errors(gai_error, mmdb_error, "MMDB_lookup_sockaddr", ip, file,
mode_desc);
test_lookup_errors(
gai_error, mmdb_error, "MMDB_lookup_sockaddr", ip, file, mode_desc);
return result;
}
void test_lookup_errors(int gai_error, int mmdb_error,
const char *function, const char *ip,
const char *file, const char *mode_desc)
{
void test_lookup_errors(int gai_error,
int mmdb_error,
const char *function,
const char *ip,
const char *file,
const char *mode_desc) {
int is_ok = ok(0 == gai_error,
"no getaddrinfo error in call to %s for %s - %s - %s",
function, ip, file, mode_desc);
function,
ip,
file,
mode_desc);
if (!is_ok) {
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,
"no MMDB error in call to %s for %s - %s - %s",
function, ip, file, mode_desc);
function,
ip,
file,
mode_desc);
if (!is_ok) {
diag("MMDB error - %s", MMDB_strerror(mmdb_error));
}
}
MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, uint32_t expect_type,
const char *description, ...)
{
MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result,
uint32_t expect_type,
const char *description,
...) {
va_list keys;
va_start(keys, description);
@ -196,13 +205,20 @@ MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, uint32_t expect_type,
va_end(keys);
if (cmp_ok(status, "==", MMDB_SUCCESS,
"no error from call to MMDB_vget_value - %s", description)) {
if (cmp_ok(status,
"==",
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,
"got the expected data type - %s",
description)) {
diag(" data type value is %i but expected %i", data.type,
diag(" data type value is %i but expected %i",
data.type,
expect_type);
}
} else {
@ -212,22 +228,20 @@ MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, uint32_t expect_type,
return data;
}
void compare_double(double got, double expect)
{
void compare_double(double got, double expect) {
double diff = fabs(got - expect);
int is_ok = ok(diff < 0.01, "double value was approximately %2.6f", expect);
if (!is_ok) {
diag(" got %2.6f but expected %2.6f (diff = %2.6f)",
got, expect, diff);
diag(
" 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);
int is_ok = ok(diff < 0.01, "float value was approximately %2.1f", expect);
if (!is_ok) {
diag(" got %2.4f but expected %2.1f (diff = %2.1f)",
got, expect, diff);
diag(
" got %2.4f but expected %2.1f (diff = %2.1f)", got, expect, diff);
}
}

View File

@ -6,12 +6,12 @@
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include "libtap/tap.h"
#include "maxminddb-compat-util.h"
#include "maxminddb.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "maxminddb.h"
#include "maxminddb-compat-util.h"
#include "libtap/tap.h"
#ifdef _WIN32
#include <winsock2.h>
@ -33,9 +33,9 @@
#define MMDB_TEST_HELPER_C (1)
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#define UNUSED(x) UNUSED_##x __attribute__((__unused__))
#else
# define UNUSED
#define UNUSED
#endif
#define MAX_DESCRIPTION_LENGTH 500
@ -48,18 +48,24 @@ extern void for_all_modes(void (*tests)(int mode, const char *description));
extern const char *test_database_path(const char *filename);
extern const char *dup_entry_string_or_bail(MMDB_entry_data_s entry_data);
extern MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc);
extern MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb, const char *ip,
extern MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc);
extern MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb, const char *ip,
extern MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb,
const char *ip,
const char *file,
const char *mode_desc);
extern void test_lookup_errors(int gai_error, int mmdb_error,
const char *function, const char *ip,
const char *file, const char *mode_desc);
extern void test_lookup_errors(int gai_error,
int mmdb_error,
const char *function,
const char *ip,
const char *file,
const char *mode_desc);
extern MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result,
uint32_t expect_type,
const char *description, ...);
const char *description,
...);
extern void compare_double(double got, double expect);
extern void compare_float(float got, float expect);

View File

@ -1,7 +1,6 @@
#include "maxminddb_test_helper.h"
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-metadata-pointers.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -9,7 +8,8 @@ void run_tests(int mode, const char *mode_desc)
char *repeated_string = "Lots of pointers in metadata";
is(mmdb->metadata.database_type, repeated_string,
is(mmdb->metadata.database_type,
repeated_string,
"decoded pointer database_type");
for (uint16_t i = 0; i < mmdb->metadata.description.count; i++) {
@ -24,8 +24,7 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,38 +1,63 @@
#include "maxminddb_test_helper.h"
void test_metadata(MMDB_s *mmdb, const char *mode_desc)
{
cmp_ok(mmdb->metadata.node_count, "==", 37, "node_count is 37 - %s",
void test_metadata(MMDB_s *mmdb, const char *mode_desc) {
cmp_ok(mmdb->metadata.node_count,
"==",
37,
"node_count is 37 - %s",
mode_desc);
cmp_ok(mmdb->metadata.record_size, "==", 24, "record_size is 24 - %s",
cmp_ok(mmdb->metadata.record_size,
"==",
24,
"record_size is 24 - %s",
mode_desc);
cmp_ok(mmdb->metadata.ip_version, "==", 4, "ip_version is 4 - %s",
mode_desc);
is(mmdb->metadata.database_type, "Test", "database_type is Test - %s",
cmp_ok(
mmdb->metadata.ip_version, "==", 4, "ip_version is 4 - %s", mode_desc);
is(mmdb->metadata.database_type,
"Test",
"database_type is Test - %s",
mode_desc);
// 2013-07-01T00:00:00Z
uint64_t expect_epoch = 1372636800;
int is_ok =
cmp_ok(mmdb->metadata.build_epoch, ">=", expect_epoch,
"build_epoch > %lli", expect_epoch);
int is_ok = cmp_ok(mmdb->metadata.build_epoch,
">=",
expect_epoch,
"build_epoch > %lli",
expect_epoch);
if (!is_ok) {
diag(" epoch is %lli", mmdb->metadata.build_epoch);
}
cmp_ok(mmdb->metadata.binary_format_major_version, "==", 2,
"binary_format_major_version is 2 - %s", mode_desc);
cmp_ok(mmdb->metadata.binary_format_minor_version, "==", 0,
"binary_format_minor_version is 0 - %s", mode_desc);
cmp_ok(mmdb->metadata.languages.count, "==", 2, "found 2 languages - %s",
cmp_ok(mmdb->metadata.binary_format_major_version,
"==",
2,
"binary_format_major_version is 2 - %s",
mode_desc);
is(mmdb->metadata.languages.names[0], "en", "first language is en - %s",
cmp_ok(mmdb->metadata.binary_format_minor_version,
"==",
0,
"binary_format_minor_version is 0 - %s",
mode_desc);
cmp_ok(mmdb->metadata.languages.count,
"==",
2,
"found 2 languages - %s",
mode_desc);
is(mmdb->metadata.languages.names[0],
"en",
"first language is en - %s",
mode_desc);
is(mmdb->metadata.languages.names[1], "zh", "second language is zh - %s",
is(mmdb->metadata.languages.names[1],
"zh",
"second language is zh - %s",
mode_desc);
cmp_ok(mmdb->metadata.description.count, "==", 2,
"found 2 descriptions - %s", mode_desc);
cmp_ok(mmdb->metadata.description.count,
"==",
2,
"found 2 descriptions - %s",
mode_desc);
for (uint16_t i = 0; i < mmdb->metadata.description.count; i++) {
const char *language =
mmdb->metadata.description.descriptions[i]->language;
@ -45,34 +70,45 @@ void test_metadata(MMDB_s *mmdb, const char *mode_desc)
ok(1, "found zh description");
is(description, "Test Database Chinese", "zh description");
} else {
ok(0, "found unknown description in unexpected language - %s",
ok(0,
"found unknown description in unexpected language - %s",
language);
}
}
cmp_ok(mmdb->full_record_byte_size, "==", 6,
"full_record_byte_size is 6 - %s", mode_desc);
cmp_ok(mmdb->full_record_byte_size,
"==",
6,
"full_record_byte_size is 6 - %s",
mode_desc);
}
MMDB_entry_data_list_s *test_languages_value(MMDB_entry_data_list_s
*entry_data_list)
{
MMDB_entry_data_list_s *
test_languages_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *languages = entry_data_list = entry_data_list->next;
cmp_ok(languages->entry_data.type, "==", MMDB_DATA_TYPE_ARRAY,
cmp_ok(languages->entry_data.type,
"==",
MMDB_DATA_TYPE_ARRAY,
"'languages' key's value is an array");
cmp_ok(languages->entry_data.data_size, "==", 2,
cmp_ok(languages->entry_data.data_size,
"==",
2,
"'languages' key's value has 2 elements");
MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
cmp_ok(idx0->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(idx0->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"first array entry is a UTF8_STRING");
const char *lang0 = dup_entry_string_or_bail(idx0->entry_data);
is(lang0, "en", "first language is en");
free((void *)lang0);
MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
cmp_ok(idx1->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(idx1->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"second array entry is a UTF8_STRING");
const char *lang1 = dup_entry_string_or_bail(idx1->entry_data);
is(lang1, "zh", "second language is zh");
@ -81,36 +117,41 @@ MMDB_entry_data_list_s *test_languages_value(MMDB_entry_data_list_s
return entry_data_list;
}
MMDB_entry_data_list_s *test_description_value(
MMDB_entry_data_list_s *entry_data_list)
{
MMDB_entry_data_list_s *
test_description_value(MMDB_entry_data_list_s *entry_data_list) {
MMDB_entry_data_list_s *description = entry_data_list =
entry_data_list->next;
cmp_ok(description->entry_data.type, "==", MMDB_DATA_TYPE_MAP,
cmp_ok(description->entry_data.type,
"==",
MMDB_DATA_TYPE_MAP,
"'description' key's value is a map");
cmp_ok(description->entry_data.data_size, "==", 2,
cmp_ok(description->entry_data.data_size,
"==",
2,
"'description' key's value has 2 key/value pairs");
for (int i = 0; i < 2; i++) {
MMDB_entry_data_list_s *key = entry_data_list =
entry_data_list->next;
cmp_ok(key->entry_data.type, "==",
MMDB_entry_data_list_s *key = entry_data_list = entry_data_list->next;
cmp_ok(key->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"found a map key in 'map'");
const char *key_name = dup_entry_string_or_bail(key->entry_data);
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"map value is a UTF8_STRING");
const char *description =
dup_entry_string_or_bail(value->entry_data);
const char *description = dup_entry_string_or_bail(value->entry_data);
if (strcmp(key_name, "en") == 0) {
is(description, "Test Database",
is(description,
"Test Database",
"en description == 'Test Database'");
} else if (strcmp(key_name, "zh") == 0) {
is(description, "Test Database Chinese",
is(description,
"Test Database Chinese",
"zh description == 'Test Database Chinese'");
} else {
ok(0, "unknown key found in description map - %s", key_name);
@ -123,63 +164,70 @@ MMDB_entry_data_list_s *test_description_value(
return entry_data_list;
}
void test_metadata_as_data_entry_list(MMDB_s * mmdb,
const char *mode_desc)
{
void test_metadata_as_data_entry_list(MMDB_s *mmdb, const char *mode_desc) {
MMDB_entry_data_list_s *entry_data_list, *first;
int status =
MMDB_get_metadata_as_entry_data_list(mmdb, &entry_data_list);
int status = MMDB_get_metadata_as_entry_data_list(mmdb, &entry_data_list);
first = entry_data_list;
cmp_ok(status, "==", MMDB_SUCCESS, "get metadata as data_entry_list - %s",
cmp_ok(status,
"==",
MMDB_SUCCESS,
"get metadata as data_entry_list - %s",
mode_desc);
cmp_ok(first->entry_data.data_size, "==", 9,
cmp_ok(first->entry_data.data_size,
"==",
9,
"metadata map has 9 key/value pairs");
while (1) {
MMDB_entry_data_list_s *key = entry_data_list =
entry_data_list->next;
MMDB_entry_data_list_s *key = entry_data_list = entry_data_list->next;
if (!key) {
break;
}
cmp_ok(key->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(key->entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"found a map key");
const char *key_name = dup_entry_string_or_bail(key->entry_data);
if (strcmp(key_name, "node_count") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.uint32, "==", 37, "node_count == 37");
} else if (strcmp(key_name, "record_size") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.uint16, "==", 24, "record_size == 24");
} else if (strcmp(key_name, "ip_version") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.uint16, "==", 4, "ip_version == 4");
} else if (strcmp(key_name, "binary_format_major_version") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.uint16, "==", 2,
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.uint16,
"==",
2,
"binary_format_major_version == 2");
} else if (strcmp(key_name, "binary_format_minor_version") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
cmp_ok(value->entry_data.uint16, "==", 0,
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
cmp_ok(value->entry_data.uint16,
"==",
0,
"binary_format_minor_version == 0");
} else if (strcmp(key_name, "build_epoch") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
ok(value->entry_data.uint64 > 1373571901,
"build_epoch > 1373571901");
} else if (strcmp(key_name, "database_type") == 0) {
MMDB_entry_data_list_s *value
= entry_data_list = entry_data_list->next;
MMDB_entry_data_list_s *value = entry_data_list =
entry_data_list->next;
const char *type = dup_entry_string_or_bail(value->entry_data);
is(type, "Test", "type == Test");
free((void *)type);
@ -188,8 +236,7 @@ void test_metadata_as_data_entry_list(MMDB_s * mmdb,
} else if (strcmp(key_name, "description") == 0) {
entry_data_list = test_description_value(entry_data_list);
} else {
ok(0, "unknown key found in metadata map - %s",
key_name);
ok(0, "unknown key found in metadata map - %s", key_name);
}
free((void *)key_name);
@ -198,8 +245,7 @@ void test_metadata_as_data_entry_list(MMDB_s * mmdb,
MMDB_free_entry_data_list(first);
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *file = "MaxMind-DB-test-ipv4-24.mmdb";
const char *path = test_database_path(file);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -218,8 +264,7 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,7 +1,6 @@
#include "maxminddb_test_helper.h"
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-string-value-entries.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
@ -14,18 +13,21 @@ void run_tests(int mode, const char *mode_desc)
MMDB_entry_data_s entry_data;
int status = MMDB_get_value(&result.entry, &entry_data, NULL);
cmp_ok(status, "==", MMDB_SUCCESS,
cmp_ok(status,
"==",
MMDB_SUCCESS,
"status for MMDB_get_value() is MMDB_SUCCESS");
ok(entry_data.has_data, "found a value when varargs list is just NULL");
cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"returned entry type is utf8_string");
MMDB_close(mmdb);
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,50 +1,64 @@
#include "maxminddb_test_helper.h"
void test_entry_data(MMDB_s *mmdb, MMDB_entry_s *entry, uint32_t node_number,
char * node_record)
{
void test_entry_data(MMDB_s *mmdb,
MMDB_entry_s *entry,
uint32_t node_number,
char *node_record) {
MMDB_entry_data_s entry_data;
int status =
MMDB_get_value(entry, &entry_data, "ip",
NULL);
cmp_ok(status, "==", MMDB_SUCCESS,
"successful data lookup for node");
cmp_ok(
entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
"returned entry type is UTF8_STRING for %s record of node %i",
node_record, node_number);
int status = MMDB_get_value(entry, &entry_data, "ip", NULL);
cmp_ok(status, "==", MMDB_SUCCESS, "successful data lookup for node");
cmp_ok(entry_data.type,
"==",
MMDB_DATA_TYPE_UTF8_STRING,
"returned entry type is UTF8_STRING for %s record of node %i",
node_record,
node_number);
}
void run_read_node_tests(MMDB_s *mmdb, const uint32_t tests[][5],
void run_read_node_tests(MMDB_s *mmdb,
const uint32_t tests[][5],
int test_count,
uint8_t record_size)
{
uint8_t record_size) {
for (int i = 0; i < test_count; i++) {
uint32_t node_number = tests[i][0];
MMDB_search_node_s node;
int status = MMDB_read_node(mmdb, node_number, &node);
if (MMDB_SUCCESS == status) {
cmp_ok(node.left_record, "==", tests[i][1],
cmp_ok(node.left_record,
"==",
tests[i][1],
"left record for node %i is %i - %i bit DB",
node_number, tests[i][1], record_size);
cmp_ok(node.left_record_type, "==", tests[i][2],
"left record type for node %i is %i", node_number,
node_number,
tests[i][1],
record_size);
cmp_ok(node.left_record_type,
"==",
tests[i][2],
"left record type for node %i is %i",
node_number,
tests[i][2]);
if (node.left_record_type == MMDB_RECORD_TYPE_DATA) {
test_entry_data(mmdb, &node.left_record_entry, node_number,
"left");
test_entry_data(
mmdb, &node.left_record_entry, node_number, "left");
}
cmp_ok(node.right_record, "==", tests[i][3],
cmp_ok(node.right_record,
"==",
tests[i][3],
"right record for node %i is %i - %i bit DB",
node_number, tests[i][3], record_size);
cmp_ok(node.right_record_type, "==", tests[i][4],
"right record type for node %i is %i", node_number,
node_number,
tests[i][3],
record_size);
cmp_ok(node.right_record_type,
"==",
tests[i][4],
"right record type for node %i is %i",
node_number,
tests[i][4]);
if (node.right_record_type == MMDB_RECORD_TYPE_DATA) {
test_entry_data(mmdb, &node.right_record_entry, node_number,
"right");
test_entry_data(
mmdb, &node.right_record_entry, node_number, "right");
}
} else {
diag("call to MMDB_read_node for node %i failed - %i bit DB",
@ -54,87 +68,168 @@ void run_read_node_tests(MMDB_s *mmdb, const uint32_t tests[][5],
}
}
void run_24_bit_record_tests(int mode, const char *mode_desc)
{
void run_24_bit_record_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-mixed-24.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
free((void *)path);
const uint32_t tests[7][5] = {
{ 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY },
{ 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
MMDB_RECORD_TYPE_DATA, },
{ 132, 329, MMDB_RECORD_TYPE_DATA, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, }
};
{0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
{
80,
81,
MMDB_RECORD_TYPE_SEARCH_NODE,
197,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
96,
97,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
103,
242,
MMDB_RECORD_TYPE_EMPTY,
104,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
127,
242,
MMDB_RECORD_TYPE_EMPTY,
315,
MMDB_RECORD_TYPE_DATA,
},
{
132,
329,
MMDB_RECORD_TYPE_DATA,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
241,
96,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
}};
run_read_node_tests(mmdb, tests, 7, 24);
MMDB_close(mmdb);
free(mmdb);
}
void run_28_bit_record_tests(int mode, const char *mode_desc)
{
void run_28_bit_record_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-mixed-28.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
free((void *)path);
const uint32_t tests[7][5] = {
{ 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY },
{ 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
MMDB_RECORD_TYPE_DATA, },
{ 132, 329, MMDB_RECORD_TYPE_DATA, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, }
};
{0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
{
80,
81,
MMDB_RECORD_TYPE_SEARCH_NODE,
197,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
96,
97,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
103,
242,
MMDB_RECORD_TYPE_EMPTY,
104,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
127,
242,
MMDB_RECORD_TYPE_EMPTY,
315,
MMDB_RECORD_TYPE_DATA,
},
{
132,
329,
MMDB_RECORD_TYPE_DATA,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
241,
96,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
}};
run_read_node_tests(mmdb, tests, 7, 28);
MMDB_close(mmdb);
free(mmdb);
}
void run_32_bit_record_tests(int mode, const char *mode_desc)
{
void run_32_bit_record_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-mixed-32.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
free((void *)path);
const uint32_t tests[7][5] = {
{ 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY },
{ 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
MMDB_RECORD_TYPE_SEARCH_NODE, },
{ 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
MMDB_RECORD_TYPE_DATA, },
{ 132, 329, MMDB_RECORD_TYPE_DATA, 242,
MMDB_RECORD_TYPE_EMPTY, },
{ 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
MMDB_RECORD_TYPE_EMPTY, }
};
{0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
{
80,
81,
MMDB_RECORD_TYPE_SEARCH_NODE,
197,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
96,
97,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
103,
242,
MMDB_RECORD_TYPE_EMPTY,
104,
MMDB_RECORD_TYPE_SEARCH_NODE,
},
{
127,
242,
MMDB_RECORD_TYPE_EMPTY,
315,
MMDB_RECORD_TYPE_DATA,
},
{
132,
329,
MMDB_RECORD_TYPE_DATA,
242,
MMDB_RECORD_TYPE_EMPTY,
},
{
241,
96,
MMDB_RECORD_TYPE_SEARCH_NODE,
242,
MMDB_RECORD_TYPE_EMPTY,
}};
run_read_node_tests(mmdb, tests, 7, 32);
@ -142,15 +237,13 @@ void run_32_bit_record_tests(int mode, const char *mode_desc)
free(mmdb);
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
run_24_bit_record_tests(mode, mode_desc);
run_28_bit_record_tests(mode, mode_desc);
run_32_bit_record_tests(mode, mode_desc);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -17,8 +17,7 @@ typedef struct test_result {
char *data_value;
} test_result_s;
void test_one_ip(MMDB_s *mmdb, const char *ip, test_result_s *test_result)
{
void test_one_ip(MMDB_s *mmdb, const char *ip, test_result_s *test_result) {
test_result->ip_looked_up = ip;
@ -60,66 +59,77 @@ void test_one_ip(MMDB_s *mmdb, const char *ip, test_result_s *test_result)
return;
}
void *run_one_thread(void *arg)
{
void *run_one_thread(void *arg) {
thread_arg_s *thread_arg = (thread_arg_s *)arg;
MMDB_s *mmdb = thread_arg->mmdb;
const char *ip = thread_arg->ip_to_lookup;
test_result_s *result = malloc(sizeof(test_result_s));
if (!result) {
BAIL_OUT("could not allocate memory");
}
test_one_ip(mmdb, ip, result);
pthread_exit((void *)result);
}
void process_result(test_result_s *result, const char *expect,
const char *mode_desc)
{
void process_result(test_result_s *result,
const char *expect,
const char *mode_desc) {
int is_ok;
is_ok =
ok(!result->lookup_string_gai_error, "no getaddrinfo error for %s - %s",
result->ip_looked_up, mode_desc);
is_ok = ok(!result->lookup_string_gai_error,
"no getaddrinfo error for %s - %s",
result->ip_looked_up,
mode_desc);
if (!is_ok) {
return;
}
is_ok = ok(!result->lookup_string_mmdb_error, "no mmdb error for %s - %s",
result->ip_looked_up, mode_desc);
is_ok = ok(!result->lookup_string_mmdb_error,
"no mmdb error for %s - %s",
result->ip_looked_up,
mode_desc);
if (!is_ok) {
return;
}
is_ok = ok(result->found_entry, "got a result for %s in the database - %s",
result->ip_looked_up, mode_desc);
is_ok = ok(result->found_entry,
"got a result for %s in the database - %s",
result->ip_looked_up,
mode_desc);
if (!is_ok) {
return;
}
is_ok =
ok(!result->get_value_status,
"no error from MMDB_get_value for %s - %s",
result->ip_looked_up,
mode_desc);
is_ok = ok(!result->get_value_status,
"no error from MMDB_get_value for %s - %s",
result->ip_looked_up,
mode_desc);
if (!is_ok) {
return;
}
is_ok = ok(result->data_type_ok,
"MMDB_get_value found a utf8_string at 'ip' key for %s - %s",
result->ip_looked_up, mode_desc);
result->ip_looked_up,
mode_desc);
if (!is_ok) {
return;
}
is(result->data_value, expect,
is(result->data_value,
expect,
"found expected result for 'ip' key for %s - %s",
result->ip_looked_up, mode_desc);
result->ip_looked_up,
mode_desc);
}
void run_ipX_tests(MMDB_s *mmdb, const char *pairs[][2], int pairs_rows,
int mode, const char *mode_desc)
{
void run_ipX_tests(MMDB_s *mmdb,
const char *pairs[][2],
int pairs_rows,
int mode,
const char *mode_desc) {
pthread_t threads[pairs_rows];
struct thread_arg thread_args[pairs_rows];
@ -128,8 +138,8 @@ void run_ipX_tests(MMDB_s *mmdb, const char *pairs[][2], int pairs_rows,
thread_args[i].mmdb = mmdb;
thread_args[i].ip_to_lookup = pairs[i][0];
int error = pthread_create(&threads[i], NULL, run_one_thread,
&thread_args[i]);
int error =
pthread_create(&threads[i], NULL, run_one_thread, &thread_args[i]);
if (error) {
BAIL_OUT("pthread_create failed");
}
@ -153,32 +163,31 @@ void run_ipX_tests(MMDB_s *mmdb, const char *pairs[][2], int pairs_rows,
}
}
void run_tests(int mode, const char *mode_desc)
{
void run_tests(int mode, const char *mode_desc) {
const char *filename = "MaxMind-DB-test-mixed-32.mmdb";
const char *path = test_database_path(filename);
MMDB_s *mmdb = open_ok(path, mode, mode_desc);
free((void *)path);
const char *pairs[18][2] = {
{ "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" },
{ "::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" },
{"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"},
{"::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"},
};
run_ipX_tests(mmdb, pairs, 18, mode, mode_desc);
@ -187,8 +196,7 @@ void run_tests(int mode, const char *mode_desc)
free(mmdb);
}
int main(void)
{
int main(void) {
plan(NO_PLAN);
for_all_modes(&run_tests);
done_testing();

View File

@ -1,7 +1,6 @@
#include "maxminddb_test_helper.h"
int main(void)
{
int main(void) {
const char *version = MMDB_lib_version();
if (ok((version != NULL), "MMDB_lib_version exists")) {
is(version, PACKAGE_VERSION, "version is " PACKAGE_VERSION);