diff --git a/.github/workflows/clang-addresssanitizer.yml b/.github/workflows/clang-addresssanitizer.yml index 0557ac6d..afaa5e0e 100644 --- a/.github/workflows/clang-addresssanitizer.yml +++ b/.github/workflows/clang-addresssanitizer.yml @@ -21,7 +21,7 @@ jobs: with: submodules: true persist-credentials: false - - run: sudo apt install clang libipc-run3-perl + - run: sudo apt install clang libcmocka-dev libipc-run3-perl - run: ./bootstrap - run: ./configure - run: make diff --git a/.github/workflows/clang-analyzer.yml b/.github/workflows/clang-analyzer.yml index f7d679ff..a04b6502 100644 --- a/.github/workflows/clang-analyzer.yml +++ b/.github/workflows/clang-analyzer.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - run: sudo apt install clang-tools libipc-run3-perl + - run: sudo apt install clang-tools libcmocka-dev libipc-run3-perl - run: ./bootstrap - run: scan-build ./configure env: diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 5ee459e3..7ef5badd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -35,7 +35,7 @@ jobs: - name: Initialize CodeQL uses: github/codeql-action/init@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9 - - run: sudo apt install libipc-run3-perl pandoc + - run: sudo apt install libcmocka-dev libipc-run3-perl pandoc - run: | ./bootstrap ./configure diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 917be21b..9a53391d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,11 +8,9 @@ permissions: {} jobs: # The -m32 variants build and test as 32-bit. ssize_t and long are 32 bits # wide on i686, which exposes signedness and range problems that the 64-bit - # builds cannot see. The flag goes in CC rather than CFLAGS so that the - # libtap build, which has its own Makefile, and the test scripts that - # compile code themselves pick it up too. CFLAGS must stay out of the job - # environment: when it is set, even empty, make exports the configured - # CFLAGS to libtap, and libtap does not build on macOS with _POSIX_C_SOURCE. + # builds cannot see. The flag goes in CC rather than CFLAGS so that the test + # scripts that compile code themselves pick it up too. The -m32 jobs build + # cmocka from source because Ubuntu does not ship a 32-bit libcmocka-dev. test-autoconf: strategy: matrix: @@ -36,9 +34,22 @@ jobs: persist-credentials: false - run: sudo apt install libipc-run3-perl if: ${{ matrix.os == 'ubuntu-latest' }} + - run: sudo apt install libcmocka-dev + if: ${{ matrix.os == 'ubuntu-latest' && matrix.arch == '' }} - run: sudo apt-get update && sudo apt-get install -y gcc-multilib g++-multilib if: ${{ matrix.os == 'ubuntu-latest' && matrix.arch == '-m32' }} - - run: brew install autoconf automake libtool + - name: Build 32-bit cmocka + if: ${{ matrix.arch == '-m32' }} + run: | + curl -sSLO https://cmocka.org/files/2.0/cmocka-2.0.1.tar.xz + echo '3f3533382ba29ab3abf5c4f4b27b79d165f0df51ea587de749b11b68b4019180 cmocka-2.0.1.tar.xz' | sha256sum -c + tar xf cmocka-2.0.1.tar.xz + cmake -S cmocka-2.0.1 -B cmocka-build -DCMAKE_C_FLAGS=-m32 -DWITH_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX="$HOME/cmocka" + cmake --build cmocka-build + cmake --install cmocka-build + echo "PKG_CONFIG_PATH=$HOME/cmocka/lib/pkgconfig" >> "$GITHUB_ENV" + echo "LD_LIBRARY_PATH=$HOME/cmocka/lib" >> "$GITHUB_ENV" + - run: brew install autoconf automake libtool cmocka pkgconf if: ${{ matrix.os == 'macos-latest' }} - run: ./bootstrap - run: ./configure diff --git a/.gitmodules b/.gitmodules index 58fc7941..c7cecd3f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "t/libtap"] - path = t/libtap - url = https://github.com/zorgnax/libtap.git [submodule "t/maxmind-db"] path = t/maxmind-db url = https://github.com/maxmind/MaxMind-DB.git diff --git a/.precious.toml b/.precious.toml index 33d74e02..0aa2acf8 100644 --- a/.precious.toml +++ b/.precious.toml @@ -1,7 +1,6 @@ exclude = [ ".git", "maxmind-db/**", - "t/libtap/**", "t/maxmind-db/**", ] diff --git a/CMakeLists.txt b/CMakeLists.txt index 33d8a944..41877ad4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.9...3.30) +cmake_minimum_required (VERSION 3.14...3.30) project(maxminddb LANGUAGES C diff --git a/Changes.md b/Changes.md index 4bb1ef96..c7e99579 100644 --- a/Changes.md +++ b/Changes.md @@ -1,5 +1,10 @@ ## next release +- The C tests now use [cmocka](https://cmocka.org/) instead of the bundled + `libtap` submodule. Building the tests requires cmocka to be installed. The + autotools build finds it with `pkg-config`. The CMake build finds it with + `find_package` and downloads it when it is not installed. The CMake build now + requires CMake 3.14 or later. - Fixed a `-Wsign-compare` warning in `MMDB_open()` that broke builds with `-Werror` on platforms where `ssize_t` is 32 bits, such as i686. Pull request by Robert Scheck. GitHub #487. diff --git a/README.dev.md b/README.dev.md index c004acc3..52a982f9 100644 --- a/README.dev.md +++ b/README.dev.md @@ -51,7 +51,7 @@ version updated promptly for some reason. - Required packages (Ubuntu 25.10): vim git-core dput build-essential autoconf automake libtool git-buildpackage lowdown dirmngr debhelper dh-autoreconf - libipc-run3-perl libtest-output-perl devscripts + libcmocka-dev libipc-run3-perl libtest-output-perl devscripts - Install [gh](https://github.com/cli/cli/releases). - GitHub ssh key (e.g. in `~/.ssh/id_rsa`) - Git config (e.g. `~/.gitconfig`) diff --git a/README.md b/README.md index 8248fb25..44631dc0 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,9 @@ sudo ldconfig ``` You can skip the `make check` step but it's always good to know that tests are -passing on your platform. +passing on your platform. The tests need [cmocka](https://cmocka.org/), for +example the `libcmocka-dev` package on Debian and Ubuntu or the `cmocka` +formula on Homebrew. The `configure` script takes the standard options to set where files are installed such as `--prefix`, etc. See `./configure --help` for details. diff --git a/configure.ac b/configure.ac index 93678976..f0b6d8db 100644 --- a/configure.ac +++ b/configure.ac @@ -130,6 +130,7 @@ AC_ARG_ENABLE([tests], [enable_tests=${enableval}], [enable_tests=yes]) AM_CONDITIONAL([TESTS], [test "${enable_tests}" = "yes"]) +AS_IF([test "${enable_tests}" = "yes"], [PKG_CHECK_MODULES([CMOCKA], [cmocka])]) AC_CONFIG_FILES([Makefile src/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 6d86368f..baac0db1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,12 +18,9 @@ TESTS = test-data-pool check_PROGRAMS = test-data-pool test_data_pool_SOURCES = data-pool.c data-pool.h -test_data_pool_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/t -DTEST_DATA_POOL -test_data_pool_LDADD = $(top_srcdir)/t/libmmdbtest.la \ - $(top_srcdir)/t/libtap/libtap.a +test_data_pool_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/t -DTEST_DATA_POOL \ + $(CMOCKA_CFLAGS) +test_data_pool_LDADD = $(top_srcdir)/t/libmmdbtest.la $(CMOCKA_LIBS) $(top_srcdir)/t/libmmdbtest.la: $(MAKE) -C $(top_srcdir)/t libmmdbtest.la - -$(top_srcdir)/t/libtap/libtap.a: - $(MAKE) -C $(top_srcdir)/t/libtap libtap.a diff --git a/src/data-pool.c b/src/data-pool.c index e4b00f3b..5ffe0f78 100644 --- a/src/data-pool.c +++ b/src/data-pool.c @@ -162,30 +162,22 @@ MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool) { #ifdef TEST_DATA_POOL - #include #include -static void test_can_multiply(void); - -int main(void) { - plan(NO_PLAN); - test_can_multiply(); - done_testing(); +static void test_can_multiply(void **UNUSED(state)) { + assert_true_desc(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); + assert_true_desc(!can_multiply(SIZE_MAX, 2, SIZE_MAX), + "2*SIZE_MAX is not ok"); + assert_true_desc( + can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)), + "1024 entry_data_list_s's are okay"); } -static void test_can_multiply(void) { - { - ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); - } - - { - ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); - } - - { - ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)), - "1024 entry_data_list_s's are okay"); - } +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_can_multiply), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } #endif diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index bb9c23c7..d308550a 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -1,6 +1,33 @@ -add_library(tap - libtap/tap.c -) +find_package(cmocka CONFIG QUIET) +if(NOT cmocka_FOUND) + message(STATUS "cmocka not found, building it from source") + include(FetchContent) + # These only apply to the cmocka subdirectory. + set(BUILD_SHARED_LIBS OFF) + set(WITH_EXAMPLES OFF) + set(UNIT_TESTING OFF) + set(CMOCKA_URL https://cmocka.org/files/2.0/cmocka-2.0.1.tar.xz) + set(CMOCKA_SHA256 3f3533382ba29ab3abf5c4f4b27b79d165f0df51ea587de749b11b68b4019180) + # EXCLUDE_FROM_ALL keeps cmocka out of the install tree. + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + FetchContent_Declare(cmocka + URL ${CMOCKA_URL} + URL_HASH SHA256=${CMOCKA_SHA256} + EXCLUDE_FROM_ALL + ) + FetchContent_MakeAvailable(cmocka) + else() + FetchContent_Declare(cmocka + URL ${CMOCKA_URL} + URL_HASH SHA256=${CMOCKA_SHA256} + ) + FetchContent_GetProperties(cmocka) + if(NOT cmocka_POPULATED) + FetchContent_Populate(cmocka) + add_subdirectory(${cmocka_SOURCE_DIR} ${cmocka_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() + endif() +endif() # test programs set(TEST_TARGET_NAMES @@ -41,17 +68,10 @@ if(UNIX) # or if (NOT WIN32) find_package(Threads) endif() -if(WIN32) - # 4244, 4267 - libtap causes a significant number of conversion warning in - # our tests on Windows. - # 4996 - vsprintf used by libtap is unsafe. - add_definitions("/wd4244 /wd4267 /wd4996") -endif(WIN32) - foreach(TEST_TARGET_NAME ${TEST_TARGET_NAMES}) add_executable(${TEST_TARGET_NAME} ${TEST_TARGET_NAME}.c maxminddb_test_helper.c) target_include_directories(${TEST_TARGET_NAME} PRIVATE ../src) - target_link_libraries(${TEST_TARGET_NAME} maxminddb tap) + target_link_libraries(${TEST_TARGET_NAME} maxminddb cmocka::cmocka) target_compile_definitions(${TEST_TARGET_NAME} PRIVATE PACKAGE_VERSION="${PROJECT_VERSION}") if(UNIX) diff --git a/t/Makefile.am b/t/Makefile.am index f2c01aca..b35e5f11 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -1,20 +1,13 @@ include $(top_srcdir)/common.mk -all-local: - cd libtap && $(MAKE) $(AM_MAKEFLAGS) all -clean-local: - cd libtap && $(MAKE) $(AM_MAKEFLAGS) clean - AM_LDFLAGS = $(top_builddir)/src/libmaxminddb.la -CFLAGS += -I$(top_srcdir)/src +CFLAGS += -I$(top_srcdir)/src $(CMOCKA_CFLAGS) noinst_LTLIBRARIES = libmmdbtest.la libmmdbtest_la_SOURCES = maxminddb_test_helper.c maxminddb_test_helper.h EXTRA_DIST = compile_c++_t.pl decoder_limits_t.pl external_symbols_t.pl \ - mmdblookup_t.pl \ - libtap/COPYING libtap/INSTALL libtap/Makefile libtap/README.md \ - libtap/tap.c libtap/tap.h maxmind-db + mmdblookup_t.pl maxmind-db check_PROGRAMS = \ bad_pointers_t bad_databases_t bad_data_size_t bad_epoch_t bad_indent_t \ @@ -36,4 +29,4 @@ threads_t_CFLAGS = $(CFLAGS) -pthread TESTS = $(check_PROGRAMS) compile_c++_t.pl decoder_limits_t.pl \ external_symbols_t.pl mmdblookup_t.pl -LDADD = libmmdbtest.la libtap/libtap.a +LDADD = libmmdbtest.la $(CMOCKA_LIBS) diff --git a/t/bad_data_size_t.c b/t/bad_data_size_t.c index b5ac9d26..57ca2adc 100644 --- a/t/bad_data_size_t.c +++ b/t/bad_data_size_t.c @@ -1,33 +1,27 @@ #include "maxminddb_test_helper.h" -void test_bad_data_size_rejected(void) { +void test_bad_data_size_rejected(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-oversized-array.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened bad-data-size MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "opened bad-data-size MMDB"); int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "1.2.3.4", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); - ok(result.found_entry, "entry found"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); + assert_true_desc(result.found_entry, "entry found"); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, - "==", - MMDB_INVALID_DATA_ERROR, - "MMDB_get_entry_data_list returns INVALID_DATA_ERROR " - "for array with size exceeding remaining data"); + assert_int_equal_desc( + status, + MMDB_INVALID_DATA_ERROR, + "MMDB_get_entry_data_list returns INVALID_DATA_ERROR " + "for array with size exceeding remaining data"); MMDB_free_entry_data_list(entry_data_list); } @@ -35,34 +29,28 @@ void test_bad_data_size_rejected(void) { free(db_file); } -void test_bad_map_size_rejected(void) { +void test_bad_map_size_rejected(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-oversized-map.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened bad-map-size MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "opened bad-map-size MMDB"); int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "1.2.3.4", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); - ok(result.found_entry, "entry found"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); + assert_true_desc(result.found_entry, "entry found"); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, - "==", - MMDB_INVALID_DATA_ERROR, - "MMDB_get_entry_data_list returns INVALID_DATA_ERROR " - "for map with size exceeding remaining data"); + assert_int_equal_desc( + status, + MMDB_INVALID_DATA_ERROR, + "MMDB_get_entry_data_list returns INVALID_DATA_ERROR " + "for map with size exceeding remaining data"); MMDB_free_entry_data_list(entry_data_list); } @@ -71,8 +59,9 @@ void test_bad_map_size_rejected(void) { } int main(void) { - plan(NO_PLAN); - test_bad_data_size_rejected(); - test_bad_map_size_rejected(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_bad_data_size_rejected), + cmocka_unit_test(test_bad_map_size_rejected), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/bad_databases_t.c b/t/bad_databases_t.c index 9ae7d632..4950211d 100644 --- a/t/bad_databases_t.c +++ b/t/bad_databases_t.c @@ -20,13 +20,12 @@ int test_read(const char *path, MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s)); if (NULL == mmdb) { - BAIL_OUT("could not allocate memory for our MMDB_s struct"); + fail_msg("could not allocate memory for our MMDB_s struct"); } int status = MMDB_open(path, MMDB_MODE_MMAP, mmdb); if (status != MMDB_SUCCESS) { - ok(1, "received error when opening %s", path); free(mmdb); return 0; } @@ -35,11 +34,10 @@ int test_read(const char *path, MMDB_lookup_result_s result = MMDB_lookup_string(mmdb, "1.1.1.1", &gai_error, &mmdb_error); if (gai_error != 0) { - BAIL_OUT("could not parse IP address"); + fail_msg("could not parse IP address"); } if (mmdb_error != MMDB_SUCCESS) { - ok(1, "received error on lookup for %s", path); MMDB_close(mmdb); free(mmdb); return 0; @@ -51,7 +49,6 @@ int test_read(const char *path, MMDB_free_entry_data_list(entry_data_list); if (status != MMDB_SUCCESS) { - ok(1, "received error from MMDB_get_entry_data_list for %s", path); MMDB_close(mmdb); free(mmdb); return 0; @@ -61,16 +58,13 @@ int test_read(const char *path, // Some bad-data files (e.g. uint64-max-epoch) are valid databases with // extreme metadata values. They don't produce errors in libmaxminddb // but are useful for testing other reader implementations. - ok(1, - "no error reading %s (database may have extreme but valid data)", - path); MMDB_close(mmdb); free(mmdb); return 0; } -int main(void) { +static void test_bad_databases(void **UNUSED(state)) { char *test_db_dir; #ifdef _WIN32 test_db_dir = "../t/maxmind-db/bad-data"; @@ -84,9 +78,13 @@ int main(void) { test_db_dir = "./t/maxmind-db/bad-data"; } #endif - plan(NO_PLAN); - if (nftw(test_db_dir, test_read, 10, FTW_PHYS) != 0) { - BAIL_OUT("nftw failed"); - } - done_testing(); + assert_int_equal_desc( + nftw(test_db_dir, test_read, 10, FTW_PHYS), 0, "nftw succeeded"); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_bad_databases), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/bad_epoch_t.c b/t/bad_epoch_t.c index 918adca0..fb85d482 100644 --- a/t/bad_epoch_t.c +++ b/t/bad_epoch_t.c @@ -1,18 +1,12 @@ #include "maxminddb_test_helper.h" -void test_bad_epoch(void) { +void test_bad_epoch(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-uint64-max-epoch.mmdb"); /* Verify we can at least open the DB without crashing */ MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened bad-epoch MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "opened bad-epoch MMDB"); /* Run mmdblookup --verbose via system() and check it doesn't crash. * We redirect output to /dev/null; the return code tells us @@ -29,7 +23,12 @@ void test_bad_epoch(void) { fclose(test_bin); } - skip(!test_bin, 1, "mmdblookup binary not found"); + if (!test_bin) { + print_message("mmdblookup binary not found\n"); + MMDB_close(&mmdb); + free(db_file); + skip(); + } snprintf(cmd, sizeof(cmd), "%s -f %s -i 1.2.3.4 -v > /dev/null 2>&1", @@ -38,16 +37,17 @@ void test_bad_epoch(void) { int ret = system(cmd); /* system() returns the exit status; a signal-killed process gives * a non-zero status. WIFEXITED checks for normal exit. */ - ok(WIFEXITED(ret) && WEXITSTATUS(ret) == 0, - "mmdblookup --verbose with UINT64_MAX build_epoch does not crash"); - end_skip; + assert_true_desc( + WIFEXITED(ret) && WEXITSTATUS(ret) == 0, + "mmdblookup --verbose with UINT64_MAX build_epoch does not crash"); MMDB_close(&mmdb); free(db_file); } int main(void) { - plan(NO_PLAN); - test_bad_epoch(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_bad_epoch), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/bad_indent_t.c b/t/bad_indent_t.c index 21075ce9..d7ce06d6 100644 --- a/t/bad_indent_t.c +++ b/t/bad_indent_t.c @@ -1,52 +1,48 @@ #include "maxminddb_test_helper.h" #include -void test_negative_indent(void) { +void test_negative_indent(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-ipv4-24.mmdb"); MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, "mmap mode"); free(db_file); - if (!mmdb) { - return; - } - int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(mmdb, "1.1.1.1", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, "==", MMDB_SUCCESS, "get_entry_data_list succeeded"); + assert_int_equal_desc( + status, MMDB_SUCCESS, "get_entry_data_list succeeded"); if (MMDB_SUCCESS == status && entry_data_list) { FILE *devnull = fopen("/dev/null", "w"); if (!devnull) { - BAIL_OUT("could not open /dev/null"); + fail_msg("could not open /dev/null"); } /* Negative indent should not crash — it should be clamped to 0 */ status = MMDB_dump_entry_data_list(devnull, entry_data_list, -1); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "MMDB_dump_entry_data_list with indent=-1 returns success"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "MMDB_dump_entry_data_list with indent=-1 returns success"); status = MMDB_dump_entry_data_list(devnull, entry_data_list, -100); - cmp_ok( + assert_int_equal_desc( status, - "==", MMDB_SUCCESS, "MMDB_dump_entry_data_list with indent=-100 returns success"); status = MMDB_dump_entry_data_list(devnull, entry_data_list, INT_MIN); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "MMDB_dump_entry_data_list with indent=INT_MIN returns " - "success"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "MMDB_dump_entry_data_list with indent=INT_MIN returns " + "success"); fclose(devnull); } @@ -58,7 +54,8 @@ void test_negative_indent(void) { } int main(void) { - plan(NO_PLAN); - test_negative_indent(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_negative_indent), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/bad_pointers_t.c b/t/bad_pointers_t.c index 6572ae94..d8a8cf6c 100644 --- a/t/bad_pointers_t.c +++ b/t/bad_pointers_t.c @@ -14,20 +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"); + assert_int_equal_desc( + 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"); + assert_int_equal_desc( + status, + MMDB_INVALID_DATA_ERROR, + "MMDB_get_entry_data_list returns MMDB_INVALID_DATA_ERROR for " + "bad pointer in data section"); // This is not necessary as on error we should not need to free // anything. However test that it is safe to do so. See change in @@ -41,20 +41,25 @@ void run_tests(int mode, const char *mode_desc) { int gai_error, mmdb_error; 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"); + assert_int_equal_desc( + 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) { - plan(NO_PLAN); +static void test_bad_pointers(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_bad_pointers), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/bad_search_tree_t.c b/t/bad_search_tree_t.c index 4eb0cf39..7b28eff4 100644 --- a/t/bad_search_tree_t.c +++ b/t/bad_search_tree_t.c @@ -5,37 +5,29 @@ * must return MMDB_INVALID_NODE_NUMBER_ERROR. Previously the check used * >, allowing node_number == node_count to read past the tree. */ -void test_read_node_bounds(void) { +void test_read_node_bounds(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-ipv4-24.mmdb"); MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, "mmap mode"); free(db_file); - if (!mmdb) { - return; - } - MMDB_search_node_s node; /* node_count - 1 is the last valid node */ int status = MMDB_read_node(mmdb, mmdb->metadata.node_count - 1, &node); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "MMDB_read_node succeeds for last valid node"); + assert_int_equal_desc( + status, MMDB_SUCCESS, "MMDB_read_node succeeds for last valid node"); /* node_count itself is out of bounds (the off-by-one fix) */ status = MMDB_read_node(mmdb, mmdb->metadata.node_count, &node); - cmp_ok(status, - "==", - MMDB_INVALID_NODE_NUMBER_ERROR, - "MMDB_read_node rejects node_number == node_count"); + assert_int_equal_desc(status, + MMDB_INVALID_NODE_NUMBER_ERROR, + "MMDB_read_node rejects node_number == node_count"); /* node_count + 1 is also out of bounds */ status = MMDB_read_node(mmdb, mmdb->metadata.node_count + 1, &node); - cmp_ok(status, - "==", - MMDB_INVALID_NODE_NUMBER_ERROR, - "MMDB_read_node rejects node_number > node_count"); + assert_int_equal_desc(status, + MMDB_INVALID_NODE_NUMBER_ERROR, + "MMDB_read_node rejects node_number > node_count"); MMDB_close(mmdb); free(mmdb); @@ -50,37 +42,33 @@ static void check_corrupt_record(const char *label, char *db_file = bad_database_path(fixture); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "%s: opened crafted bad MMDB", label); - if (status != MMDB_SUCCESS) { - free(db_file); - return; - } + assert_int_equal_desc( + status, MMDB_SUCCESS, "%s: opened crafted bad MMDB", label); MMDB_search_node_s node; status = MMDB_read_node(&mmdb, 0, &node); - cmp_ok(status, - "==", - MMDB_CORRUPT_SEARCH_TREE_ERROR, - "%s: MMDB_read_node rejects record as corrupt", - label); + assert_int_equal_desc(status, + MMDB_CORRUPT_SEARCH_TREE_ERROR, + "%s: MMDB_read_node rejects record as corrupt", + label); int gai_error = 0; int mmdb_error = 0; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, lookup_ip, &gai_error, &mmdb_error); - cmp_ok(gai_error, "==", 0, "%s: lookup string parse succeeds", label); - cmp_ok(mmdb_error, - "==", - MMDB_CORRUPT_SEARCH_TREE_ERROR, - "%s: MMDB_lookup_string rejects record", - label); - ok(!result.found_entry, "%s: lookup reports no entry", label); + assert_int_equal_desc( + gai_error, 0, "%s: lookup string parse succeeds", label); + assert_int_equal_desc(mmdb_error, + MMDB_CORRUPT_SEARCH_TREE_ERROR, + "%s: MMDB_lookup_string rejects record", + label); + assert_true_desc(!result.found_entry, "%s: lookup reports no entry", label); MMDB_close(&mmdb); free(db_file); } -void test_separator_record_rejected(void) { +void test_separator_record_rejected(void **UNUSED(state)) { // Records in the half-open range [node_count + 1, node_count + 16) point // into the 16-byte separator between the search tree and data section // and must be rejected as corrupt. @@ -96,8 +84,9 @@ void test_separator_record_rejected(void) { } int main(void) { - plan(NO_PLAN); - test_read_node_bounds(); - test_separator_record_rejected(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_read_node_bounds), + cmocka_unit_test(test_separator_record_rejected), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/basic_lookup_t.c b/t/basic_lookup_t.c index adb4349e..8a5b7e43 100644 --- a/t/basic_lookup_t.c +++ b/t/basic_lookup_t.c @@ -1,6 +1,6 @@ #include "maxminddb_test_helper.h" -static void test_big_lookup(void); +static void test_big_lookup(void **UNUSED(state)); /* These globals are gross but it's the easiest way to mix calling * for_all_modes() and for_all_record_sizes() */ @@ -14,16 +14,13 @@ void test_one_result(MMDB_s *mmdb, 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); - - if (!is_ok) { - return; - } + assert_true_desc( + result.found_entry, + "got a result for an IP in the database - %s - %s - %s - %s", + function, + ip, + filename, + mode_desc); MMDB_entry_data_s data = data_ok(&result, MMDB_DATA_TYPE_UTF8_STRING, "result{ip}", "ip", NULL); @@ -39,18 +36,19 @@ void test_one_result(MMDB_s *mmdb, size_t maxlen = strlen(expect) + 3; real_expect = malloc(maxlen); if (!real_expect) { - BAIL_OUT("could not allocate memory"); + fail_msg("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); + assert_string_equal_desc( + string, + real_expect, + "found expected result for ip key - %s - %s - %s - %s", + function, + ip, + filename, + mode_desc); free(real_expect); free(string); @@ -93,21 +91,23 @@ void run_ipX_tests(const char *filename, 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); + assert_true_desc(!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); + assert_true_desc(!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) { @@ -178,28 +178,29 @@ void all_record_sizes(int mode, const char *description) { } } -static void test_big_lookup(void) { +static void test_big_lookup(void **UNUSED(state)) { const char *const db_filename = "GeoIP2-Precision-Enterprise-Test.mmdb"; char *db_path = test_database_path(db_filename); - ok(db_path != NULL, "got database path"); + assert_true_desc(db_path != NULL, "got database path"); MMDB_s *const mmdb = open_ok(db_path, MMDB_MODE_MMAP, "mmap mode"); - ok(mmdb != NULL, "opened MMDB"); + assert_true_desc(mmdb != NULL, "opened MMDB"); free(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); - ok(gai_err == 0, "no getaddrinfo error"); - ok(mmdb_err == MMDB_SUCCESS, "no error from maxminddb library"); - ok(result.found_entry, "found IP"); + assert_true_desc(gai_err == 0, "no getaddrinfo error"); + assert_true_desc(mmdb_err == MMDB_SUCCESS, + "no error from maxminddb library"); + assert_true_desc(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(entry_data_list != NULL, "got an entry_data_list"); + int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); + assert_int_equal_desc( + status, MMDB_SUCCESS, "successfully looked up entry data list"); + assert_true_desc(entry_data_list != NULL, "got an entry_data_list"); MMDB_free_entry_data_list(entry_data_list); @@ -207,9 +208,14 @@ static void test_big_lookup(void) { free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_basic_lookup(void **UNUSED(state)) { for_all_modes(&all_record_sizes); - test_big_lookup(); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_basic_lookup), + cmocka_unit_test(test_big_lookup), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/data-pool-t.c b/t/data-pool-t.c index cd9eb693..b4a47636 100644 --- a/t/data-pool-t.c +++ b/t/data-pool-t.c @@ -1,98 +1,103 @@ -#include "libtap/tap.h" #include "maxminddb_test_helper.h" #include #include #include #include -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 void test_data_pool_new(void **UNUSED(state)); +static void test_data_pool_destroy(void **UNUSED(state)); +static void test_data_pool_alloc(void **UNUSED(state)); +static void test_data_pool_to_list(void **UNUSED(state)); 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) { - plan(NO_PLAN); - test_data_pool_new(); - test_data_pool_destroy(); - test_data_pool_alloc(); - test_data_pool_to_list(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_data_pool_new), + cmocka_unit_test(test_data_pool_destroy), + cmocka_unit_test(test_data_pool_alloc), + cmocka_unit_test(test_data_pool_to_list), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } -static void test_data_pool_new(void) { +static void test_data_pool_new(void **UNUSED(state)) { { MMDB_data_pool_s *const pool = data_pool_new(0, 512); - ok(!pool, "size 0 is not valid"); + assert_true_desc(!pool, "size 0 is not valid"); } { MMDB_data_pool_s *const pool = data_pool_new(SIZE_MAX - 10, SIZE_MAX); - ok(!pool, "very large size is not valid"); + assert_true_desc(!pool, "very large size is not valid"); } { MMDB_data_pool_s *const pool = data_pool_new(512, 1024); - ok(pool != NULL, "size 512 is valid"); - cmp_ok(pool->size, "==", 512, "size is 512"); - cmp_ok(pool->used, "==", 0, "used size is 0"); - cmp_ok(pool->capacity, "==", 512, "capacity is 512"); - cmp_ok(pool->max_size, "==", 1024, "maximum size is 1024"); + assert_true_desc(pool != NULL, "size 512 is valid"); + assert_int_equal_desc(pool->size, 512, "size is 512"); + assert_int_equal_desc(pool->used, 0, "used size is 0"); + assert_int_equal_desc(pool->capacity, 512, "capacity is 512"); + assert_int_equal_desc(pool->max_size, 1024, "maximum size is 1024"); data_pool_destroy(pool); } { MMDB_data_pool_s *const pool = data_pool_new(512, 10); - ok(pool != NULL, "maximum smaller than initial size is valid"); - cmp_ok(pool->size, "==", 10, "initial size is clamped to maximum"); - cmp_ok(pool->capacity, "==", 10, "capacity is clamped to maximum"); + assert_true_desc(pool != NULL, + "maximum smaller than initial size is valid"); + assert_int_equal_desc( + pool->size, 10, "initial size is clamped to maximum"); + assert_int_equal_desc( + pool->capacity, 10, "capacity is clamped to maximum"); data_pool_destroy(pool); } } -static void test_data_pool_destroy(void) { +static void test_data_pool_destroy(void **UNUSED(state)) { { data_pool_destroy(NULL); } { MMDB_data_pool_s *const pool = data_pool_new(512, 512); - ok(pool != NULL, "created pool"); + assert_true_desc(pool != NULL, "created pool"); data_pool_destroy(pool); } } -static void test_data_pool_alloc(void) { +static void test_data_pool_alloc(void **UNUSED(state)) { { MMDB_data_pool_s *const pool = data_pool_new(1, 3); - ok(pool != NULL, "created pool"); - cmp_ok(pool->used, "==", 0, "used size starts at 0"); + assert_true_desc(pool != NULL, "created pool"); + assert_int_equal_desc(pool->used, 0, "used size starts at 0"); MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool); - ok(entry1 != NULL, "allocated first entry"); + assert_true_desc(entry1 != NULL, "allocated first entry"); // Arbitrary so that we can recognize it. entry1->entry_data.offset = (uint32_t)123; - cmp_ok(pool->size, "==", 1, "size is still 1"); - cmp_ok(pool->used, "==", 1, "used size is 1 after taking one"); + assert_int_equal_desc(pool->size, 1, "size is still 1"); + assert_int_equal_desc(pool->used, 1, "used size is 1 after taking one"); MMDB_entry_data_list_s *const entry2 = data_pool_alloc(pool); - ok(entry2 != NULL, "got another entry"); - ok(entry1 != entry2, "second entry is different from first entry"); + assert_true_desc(entry2 != NULL, "got another entry"); + assert_true_desc(entry1 != entry2, + "second entry is different from first entry"); - cmp_ok(pool->size, "==", 2, "size is 2 (new block)"); - cmp_ok(pool->used, "==", 1, "used size is 1 in current block"); + assert_int_equal_desc(pool->size, 2, "size is 2 (new block)"); + assert_int_equal_desc(pool->used, 1, "used size is 1 in current block"); MMDB_entry_data_list_s *const entry3 = data_pool_alloc(pool); - ok(entry3 != NULL, "got the final allowed entry"); - ok(data_pool_alloc(pool) == NULL, - "allocation past maximum capacity is rejected"); - cmp_ok(pool->capacity, "==", 3, "capacity does not exceed maximum"); + assert_true_desc(entry3 != NULL, "got the final allowed entry"); + assert_true_desc(data_pool_alloc(pool) == NULL, + "allocation past maximum capacity is rejected"); + assert_int_equal_desc( + pool->capacity, 3, "capacity does not exceed maximum"); - ok(entry1->entry_data.offset == 123, - "accessing the original entry's memory is ok"); + assert_true_desc(entry1->entry_data.offset == 123, + "accessing the original entry's memory is ok"); data_pool_destroy(pool); } @@ -101,12 +106,12 @@ static void test_data_pool_alloc(void) { size_t const initial_size = 10; MMDB_data_pool_s *const pool = data_pool_new(initial_size, initial_size * 3); - ok(pool != NULL, "created pool"); + assert_true_desc(pool != NULL, "created pool"); MMDB_entry_data_list_s *entry1 = NULL; for (size_t i = 0; i < initial_size; i++) { MMDB_entry_data_list_s *const entry = data_pool_alloc(pool); - ok(entry != NULL, "got an entry"); + assert_true_desc(entry != NULL, "got an entry"); // Give each a unique number so we can check it. entry->entry_data.offset = (uint32_t)i; if (i == 0) { @@ -114,30 +119,33 @@ static void test_data_pool_alloc(void) { } } - cmp_ok(pool->size, "==", initial_size, "size is the initial size"); - cmp_ok(pool->used, "==", initial_size, "used size is as expected"); + assert_int_equal_desc( + pool->size, initial_size, "size is the initial size"); + assert_int_equal_desc( + pool->used, initial_size, "used size is as expected"); MMDB_entry_data_list_s *const entry = data_pool_alloc(pool); - ok(entry != NULL, "got an entry"); + assert_true_desc(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->used, "==", 1, "used size is as expected"); + assert_int_equal_desc( + pool->size, initial_size * 2, "size is the initial size*2"); + assert_int_equal_desc(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); + assert_true_desc(element->entry_data.offset == (uint32_t)i, + "found offset %" PRIu32 ", should have %zu", + element->entry_data.offset, + i); element = element->next; } - ok(entry1->entry_data.offset == (uint32_t)0, - "accessing entry1's original memory is ok after growing the pool"); + assert_true_desc( + entry1->entry_data.offset == (uint32_t)0, + "accessing entry1's original memory is ok after growing the pool"); data_pool_destroy(pool); } @@ -145,57 +153,60 @@ static void test_data_pool_alloc(void) { { size_t const maximum_size = 65536; MMDB_data_pool_s *const pool = data_pool_new(64, maximum_size); - ok(pool != NULL, "created a decoder-sized pool"); + assert_true_desc(pool != NULL, "created a decoder-sized pool"); for (size_t i = 0; i < maximum_size; i++) { MMDB_entry_data_list_s *const entry = data_pool_alloc(pool); assert(entry != NULL); (void)entry; } - cmp_ok(pool->capacity, - "==", - maximum_size, - "final block is clamped to the remaining capacity"); - cmp_ok(pool->sizes[pool->index], - "==", - 64, - "the clamped final block reserves only 64 entries"); - ok(data_pool_alloc(pool) == NULL, - "decoder-sized pool refuses a 65,537th entry"); + assert_int_equal_desc( + pool->capacity, + maximum_size, + "final block is clamped to the remaining capacity"); + assert_int_equal_desc( + pool->sizes[pool->index], + 64, + "the clamped final block reserves only 64 entries"); + assert_true_desc(data_pool_alloc(pool) == NULL, + "decoder-sized pool refuses a 65,537th entry"); data_pool_destroy(pool); } } -static void test_data_pool_to_list(void) { +static void test_data_pool_to_list(void **UNUSED(state)) { { size_t const initial_size = 16; MMDB_data_pool_s *const pool = data_pool_new(initial_size, initial_size); - ok(pool != NULL, "created pool"); + assert_true_desc(pool != NULL, "created pool"); MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool); - ok(entry1 != NULL, "got an entry"); + assert_true_desc(entry1 != NULL, "got an entry"); 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"); - ok(list_one_element->next == NULL, "list is one element in size"); + assert_true_desc(list_one_element != NULL, "got a list"); + assert_true_desc(list_one_element == entry1, + "list's first element is the first we retrieved"); + assert_true_desc(list_one_element->next == NULL, + "list is one element in size"); MMDB_entry_data_list_s *const entry2 = data_pool_alloc(pool); - ok(entry2 != NULL, "got another entry"); + assert_true_desc(entry2 != NULL, "got another entry"); 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"); - ok(list_two_elements->next != NULL, "list has a second element"); + assert_true_desc(list_two_elements != NULL, "got a list"); + assert_true_desc(list_two_elements == entry1, + "list's first element is the first we retrieved"); + assert_true_desc(list_two_elements->next != NULL, + "list has a second element"); MMDB_entry_data_list_s *const second_element = list_two_elements->next; - ok(second_element == entry2, - "second item in list is second we retrieved"); - ok(second_element->next == NULL, "list ends with the second element"); + assert_true_desc(second_element == entry2, + "second item in list is second we retrieved"); + assert_true_desc(second_element->next == NULL, + "list ends with the second element"); data_pool_destroy(pool); } @@ -204,17 +215,18 @@ static void test_data_pool_to_list(void) { size_t const initial_size = 1; MMDB_data_pool_s *const pool = data_pool_new(initial_size, initial_size); - ok(pool != NULL, "created pool"); + assert_true_desc(pool != NULL, "created pool"); MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool); - ok(entry1 != NULL, "got an entry"); + assert_true_desc(entry1 != NULL, "got an entry"); 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"); - ok(list_one_element->next == NULL, "list ends with this element"); + assert_true_desc(list_one_element != NULL, "got a list"); + assert_true_desc(list_one_element == entry1, + "list's first element is the first we retrieved"); + assert_true_desc(list_one_element->next == NULL, + "list ends with this element"); data_pool_destroy(pool); } @@ -223,72 +235,77 @@ static void test_data_pool_to_list(void) { size_t const initial_size = 2; MMDB_data_pool_s *const pool = data_pool_new(initial_size, initial_size); - ok(pool != NULL, "created pool"); + assert_true_desc(pool != NULL, "created pool"); MMDB_entry_data_list_s *const entry1 = data_pool_alloc(pool); - ok(entry1 != NULL, "got an entry"); + assert_true_desc(entry1 != NULL, "got an entry"); MMDB_entry_data_list_s *const entry2 = data_pool_alloc(pool); - ok(entry2 != NULL, "got an entry"); - ok(entry1 != entry2, "second entry is different from the first"); + assert_true_desc(entry2 != NULL, "got an entry"); + assert_true_desc(entry1 != entry2, + "second entry is different from the first"); MMDB_entry_data_list_s *const list_element1 = data_pool_to_list(pool); - ok(list_element1 != NULL, "got a list"); - ok(list_element1 == entry1, - "list's first element is the first we retrieved"); + assert_true_desc(list_element1 != NULL, "got a list"); + assert_true_desc(list_element1 == entry1, + "list's first element is the first we retrieved"); MMDB_entry_data_list_s *const list_element2 = list_element1->next; - ok(list_element2 == entry2, - "second element is the second we retrieved"); - ok(list_element2->next == NULL, "list ends with this element"); + assert_true_desc(list_element2 == entry2, + "second element is the second we retrieved"); + assert_true_desc(list_element2->next == NULL, + "list ends with this element"); data_pool_destroy(pool); } { - diag("starting test: fill one block save for one spot"); - ok(create_and_check_list(3, 2), "fill one block save for one spot"); + print_message("starting test: fill one block save for one spot\n"); + assert_true_desc(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"); + print_message("starting test: fill one block\n"); + assert_true_desc(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"); + print_message("starting test: fill one block and use one spot in the " + "next block\n"); + assert_true_desc(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"); + print_message("starting test: fill two blocks save for one spot\n"); + assert_true_desc(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"); + print_message("starting test: fill two blocks\n"); + assert_true_desc(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"); + print_message( + "starting test: fill two blocks and use one spot in the next\n"); + assert_true_desc(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"); + print_message("starting test: fill three blocks save for one spot\n"); + assert_true_desc(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"); + print_message("starting test: fill three blocks\n"); + assert_true_desc(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 @@ -309,7 +326,7 @@ static void test_data_pool_to_list(void) { } } -// Use assert() rather than libtap as libtap is significantly slower and we run +// Use assert() rather than cmocka as cmocka is significantly slower and we run // this frequently. static bool create_and_check_list(size_t const initial_size, size_t const element_count) { @@ -365,7 +382,7 @@ static bool create_and_check_list(size_t const initial_size, return true; } -// Use assert() rather than libtap as libtap is significantly slower and we run +// Use assert() rather than cmocka as cmocka 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) { diff --git a/t/data_entry_list_t.c b/t/data_entry_list_t.c index 76eb2266..326beba5 100644 --- a/t/data_entry_list_t.c +++ b/t/data_entry_list_t.c @@ -3,35 +3,32 @@ 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, - "'array' key's value is an array"); - cmp_ok(array->entry_data.data_size, - "==", - 3, - "'array' key's value has 3 elements"); + assert_int_equal_desc(array->entry_data.type, + MMDB_DATA_TYPE_ARRAY, + "'array' key's value is an array"); + assert_int_equal_desc( + 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, - "first array entry is a UINT32"); - cmp_ok(idx0->entry_data.uint32, "==", 1, "first array entry value is 1"); + assert_int_equal_desc(idx0->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "first array entry is a UINT32"); + assert_int_equal_desc( + 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, - "second array entry is a UINT32"); - cmp_ok(idx1->entry_data.uint32, "==", 2, "second array entry value is 2"); + assert_int_equal_desc(idx1->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "second array entry is a UINT32"); + assert_int_equal_desc( + 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, - "third array entry is a UINT32"); - cmp_ok(idx2->entry_data.uint32, "==", 3, "third array entry value is 3"); + assert_int_equal_desc(idx2->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "third array entry is a UINT32"); + assert_int_equal_desc( + idx2->entry_data.uint32, 3, "third array entry value is 3"); return entry_data_list; } @@ -40,11 +37,11 @@ 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, - "'boolean' key's value is a boolean"); - ok(value->entry_data.boolean, "'boolean' key's value is true"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_BOOLEAN, + "'boolean' key's value is a boolean"); + assert_true_desc(value->entry_data.boolean, + "'boolean' key's value is true"); return entry_data_list; } @@ -53,18 +50,18 @@ 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, - "'bytes' key's value is bytes"); + assert_int_equal_desc(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"); + fail_msg("malloc failed"); } memcpy(bytes, value->entry_data.bytes, value->entry_data.data_size); uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a}; - ok(memcmp(bytes, expect, 4) == 0, "got expected value for bytes key"); + assert_true_desc(memcmp(bytes, expect, 4) == 0, + "got expected value for bytes key"); free((void *)bytes); @@ -75,10 +72,9 @@ 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, - "'double' key's value is a double"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_DOUBLE, + "'double' key's value is a double"); compare_double(value->entry_data.double_value, 42.123456); @@ -89,10 +85,9 @@ 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, - "'float' key's value is a float"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_FLOAT, + "'float' key's value is a float"); compare_float(value->entry_data.float_value, 1.1F); @@ -103,17 +98,14 @@ 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, - "'int32' key's value is an int32"); + assert_int_equal_desc(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, - "got expected value for int32 key"); + assert_int_equal_desc( + value->entry_data.int32, expect, "got expected value for int32 key"); return entry_data_list; } @@ -121,35 +113,33 @@ test_int32_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, - "'map{mapX}{arrayX}' key's value is an array"); - cmp_ok(arrayX->entry_data.data_size, - "==", - 3, - "'map{mapX}{arrayX}' key's value has 3 elements"); + assert_int_equal_desc(arrayX->entry_data.type, + MMDB_DATA_TYPE_ARRAY, + "'map{mapX}{arrayX}' key's value is an array"); + assert_int_equal_desc(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, - "first array entry is a UINT32"); - cmp_ok(idx0->entry_data.uint32, "==", 7, "first array entry value is 7"); + assert_int_equal_desc(idx0->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "first array entry is a UINT32"); + assert_int_equal_desc( + 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, - "second array entry is a UINT32"); - cmp_ok(idx1->entry_data.uint32, "==", 8, "second array entry value is 8"); + assert_int_equal_desc(idx1->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "second array entry is a UINT32"); + assert_int_equal_desc( + 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, - "third array entry is a UINT32"); - cmp_ok(idx2->entry_data.uint32, "==", 9, "third array entry value is 9"); + assert_int_equal_desc(idx2->entry_data.type, + MMDB_DATA_TYPE_UINT32, + "third array entry is a UINT32"); + assert_int_equal_desc( + idx2->entry_data.uint32, 9, "third array entry value is 9"); return entry_data_list; } @@ -157,28 +147,26 @@ test_arrayX_value(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, - "found a map key in 'map{mapX}'"); + assert_int_equal_desc(mapX_key->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "found a map key in 'map{mapX}'"); 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, - "'map{mapX}{utf8_stringX}' type is utf8_string"); + assert_int_equal_desc(mapX_value->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "'map{mapX}{utf8_stringX}' type is utf8_string"); 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'"); + assert_true_desc(strcmp(utf8_stringX_value, "hello") == 0, + "map{mapX}{utf8_stringX} value is 'hello'"); free(utf8_stringX_value); } else if (strcmp(mapX_key_name, "arrayX") == 0) { entry_data_list = test_arrayX_value(entry_data_list); } else { - ok(0, "unknown key found in map{mapX} - %s", mapX_key_name); + fail_msg("unknown key found in map{mapX} - %s", mapX_key_name); } free(mapX_key_name); @@ -189,33 +177,26 @@ test_mapX_key_value_pair(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, - "'map' key's value is a map"); - cmp_ok(map->entry_data.data_size, - "==", - 1, - "'map' key's value has 1 key/value pair"); + assert_int_equal_desc( + map->entry_data.type, MMDB_DATA_TYPE_MAP, "'map' key's value is a map"); + assert_int_equal_desc( + 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, - "found a map key in 'map'"); + assert_int_equal_desc(map_key_1->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "found a map key in 'map'"); char *map_key_1_name = dup_entry_string_or_bail(map_key_1->entry_data); - ok(strcmp(map_key_1_name, "mapX") == 0, "key name is mapX"); + assert_true_desc(strcmp(map_key_1_name, "mapX") == 0, "key name is mapX"); free(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, - "'map{mapX}' key's value is a map"); - cmp_ok(mapX->entry_data.data_size, - "==", - 2, - "'map' key's value has 2 key/value pairs"); + assert_int_equal_desc(mapX->entry_data.type, + MMDB_DATA_TYPE_MAP, + "'map{mapX}' key's value is a map"); + assert_int_equal_desc(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); entry_data_list = test_mapX_key_value_pair(entry_data_list); @@ -227,10 +208,9 @@ 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, - "'uint128' key's value is an uint128"); + assert_int_equal_desc(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, @@ -249,12 +229,13 @@ test_uint128_value(MMDB_entry_data_list_s *entry_data_list) { 0x00, 0x00, 0x00}; - ok(memcmp(value->entry_data.uint128, expect, 16) == 0, - "uint128 field is 2**120"); + assert_true_desc(memcmp(value->entry_data.uint128, expect, 16) == 0, + "uint128 field is 2**120"); #else mmdb_uint128_t expect = 1; expect <<= 120; - cmp_ok(value->entry_data.uint128, "==", expect, "uint128 field is 2**120"); + assert_int_equal_desc( + value->entry_data.uint128, expect, "uint128 field is 2**120"); #endif return entry_data_list; @@ -264,12 +245,11 @@ 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, - "'uint16' key's value is an uint16"); + assert_int_equal_desc(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"); + assert_true_desc(value->entry_data.uint16 == expect, "uint16 field is 100"); return entry_data_list; } @@ -278,12 +258,12 @@ 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, - "'uint32' key's value is an uint32"); + assert_int_equal_desc(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"); + assert_int_equal_desc( + value->entry_data.uint32, expect, "uint32 field is 100"); return entry_data_list; } @@ -292,13 +272,13 @@ 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, - "'uint64' key's value is an uint64"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_UINT64, + "'uint64' key's value is an uint64"); uint64_t expect = 1; expect <<= 60; - cmp_ok(value->entry_data.uint64, "==", expect, "uint64 field is 2**60"); + assert_int_equal_desc( + value->entry_data.uint64, expect, "uint64 field is 2**60"); return entry_data_list; } @@ -307,10 +287,9 @@ 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, - "'utf8_string' key's value is a string"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "'utf8_string' key's value is a string"); char *utf8_string = dup_entry_string_or_bail(value->entry_data); // This is hex for "unicode! ☯ - ♫" as bytes char expect[19] = {0x75, @@ -333,7 +312,8 @@ test_utf8_string_value(MMDB_entry_data_list_s *entry_data_list) { (char)0xab, 0x00}; - is(utf8_string, expect, "got expected value for utf8_string key"); + assert_string_equal_desc( + utf8_string, expect, "got expected value for utf8_string key"); free(utf8_string); @@ -354,23 +334,21 @@ void run_tests(int mode, const char *description) { int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); if (MMDB_SUCCESS != status) { - BAIL_OUT("MMDB_get_entry_data_list failed with %s", + fail_msg("MMDB_get_entry_data_list failed with %s", MMDB_strerror(status)); } else { - cmp_ok( - status, "==", MMDB_SUCCESS, "MMDB_get_entry_data_list succeeded"); + assert_int_equal_desc( + 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, - "first entry in entry data list is a map"); - cmp_ok(entry_data_list->entry_data.data_size, - "==", - 12, - "first map in entry data list has 12 k/v pairs"); + assert_int_equal_desc(entry_data_list->entry_data.type, + MMDB_DATA_TYPE_MAP, + "first entry in entry data list is a map"); + assert_int_equal_desc(entry_data_list->entry_data.data_size, + 12, + "first map in entry data list has 12 k/v pairs"); while (1) { MMDB_entry_data_list_s *key = entry_data_list = entry_data_list->next; @@ -379,10 +357,9 @@ void run_tests(int mode, const char *description) { break; } - cmp_ok(key->entry_data.type, - "==", - MMDB_DATA_TYPE_UTF8_STRING, - "found a map key"); + assert_int_equal_desc(key->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "found a map key"); char *key_name = dup_entry_string_or_bail(key->entry_data); if (strcmp(key_name, "array") == 0) { @@ -410,7 +387,7 @@ void run_tests(int mode, const char *description) { } else if (strcmp(key_name, "utf8_string") == 0) { entry_data_list = test_utf8_string_value(entry_data_list); } else { - ok(0, "unknown key found in map - %s", key_name); + fail_msg("unknown key found in map - %s", key_name); } free(key_name); @@ -422,8 +399,13 @@ void run_tests(int mode, const char *description) { free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_data_entry_list(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_data_entry_list), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/data_types_t.c b/t/data_types_t.c index 9a97c5c5..3a3d5307 100644 --- a/t/data_types_t.c +++ b/t/data_types_t.c @@ -35,7 +35,8 @@ void test_all_data_types(MMDB_lookup_result_s *result, (char)0x99, (char)0xab, 0x00}; - is(string, expect, "got expected utf8_string value"); + assert_string_equal_desc( + string, expect, "got expected utf8_string value"); free(string); } @@ -67,8 +68,8 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_BYTES, description, "bytes", NULL); uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a}; - ok(memcmp(data.bytes, expect, 4) == 0, - "bytes field has expected value"); + assert_true_desc(memcmp(data.bytes, expect, 4) == 0, + "bytes field has expected value"); } { @@ -78,7 +79,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_UINT16, description, "uint16", NULL); uint16_t expect = 100; - ok(data.uint16 == expect, "uint16 field is 100"); + assert_true_desc(data.uint16 == expect, "uint16 field is 100"); } { @@ -88,7 +89,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_UINT32, description, "uint32", NULL); uint32_t expect = 1 << 28; - cmp_ok(data.uint32, "==", expect, "uint32 field is 2**28"); + assert_int_equal_desc(data.uint32, expect, "uint32 field is 2**28"); } { @@ -99,7 +100,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, data_ok(result, MMDB_DATA_TYPE_INT32, description, "int32", NULL); int32_t expect = 1 << 28; expect *= -1; - cmp_ok(data.int32, "==", expect, "int32 field is -(2**28)"); + assert_int_equal_desc(data.int32, expect, "int32 field is -(2**28)"); } { @@ -110,7 +111,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, data_ok(result, MMDB_DATA_TYPE_UINT64, description, "uint64", NULL); uint64_t expect = 1; expect <<= 60; - cmp_ok(data.uint64, "==", expect, "uint64 field is 2**60"); + assert_int_equal_desc(data.uint64, expect, "uint64 field is 2**60"); } { @@ -136,11 +137,12 @@ void test_all_data_types(MMDB_lookup_result_s *result, 0x00, 0x00, 0x00}; - ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 2**120"); + assert_true_desc(memcmp(data.uint128, expect, 16) == 0, + "uint128 field is 2**120"); #else mmdb_uint128_t expect = 1; expect <<= 120; - cmp_ok(data.uint128, "==", expect, "uint128 field is 2**120"); + assert_int_equal_desc(data.uint128, expect, "uint128 field is 2**120"); #endif } @@ -150,7 +152,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok( result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL); - cmp_ok(data.boolean, "==", true, "boolean field is true"); + assert_int_equal_desc(data.boolean, true, "boolean field is true"); } { @@ -159,22 +161,22 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_ARRAY, description, "array", NULL); - ok(data.data_size == 3, "array field has 3 elements"); + assert_true_desc(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); - ok(data.uint32 == 1, "array[0] is 1"); + assert_true_desc(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); - ok(data.uint32 == 2, "array[1] is 1"); + assert_true_desc(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); - ok(data.uint32 == 3, "array[2] is 1"); + assert_true_desc(data.uint32 == 3, "array[2] is 1"); } { @@ -183,13 +185,13 @@ void test_all_data_types(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_MAP, description, "map", NULL); - ok(data.data_size == 1, "map field has 1 element"); + assert_true_desc(data.data_size == 1, "map field has 1 element"); snprintf(description, 500, "map{mapX} for %s - %s", ip, mode_desc); data = data_ok( result, MMDB_DATA_TYPE_MAP, description, "map", "mapX", NULL); - ok(data.data_size == 2, "map{mapX} field has 2 elements"); + assert_true_desc(data.data_size == 2, "map{mapX} field has 2 elements"); snprintf(description, 500, @@ -205,7 +207,8 @@ void test_all_data_types(MMDB_lookup_result_s *result, "utf8_stringX", NULL); char *string = mmdb_strndup(data.utf8_string, data.data_size); - is(string, "hello", "map{mapX}{utf8_stringX} is 'hello'"); + assert_string_equal_desc( + string, "hello", "map{mapX}{utf8_stringX} is 'hello'"); free(string); snprintf( @@ -217,7 +220,8 @@ void test_all_data_types(MMDB_lookup_result_s *result, "mapX", "arrayX", NULL); - ok(data.data_size == 3, "map{mapX}{arrayX} field has 3 elements"); + assert_true_desc(data.data_size == 3, + "map{mapX}{arrayX} field has 3 elements"); snprintf(description, 500, @@ -232,7 +236,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, "arrayX", "0", NULL); - ok(data.uint32 == 7, "map{mapX}{arrayX}[0] is 7"); + assert_true_desc(data.uint32 == 7, "map{mapX}{arrayX}[0] is 7"); snprintf(description, 500, @@ -247,7 +251,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, "arrayX", "1", NULL); - ok(data.uint32 == 8, "map{mapX}{arrayX}[1] is 8"); + assert_true_desc(data.uint32 == 8, "map{mapX}{arrayX}[1] is 8"); snprintf(description, 500, @@ -262,7 +266,7 @@ void test_all_data_types(MMDB_lookup_result_s *result, "arrayX", "2", NULL); - ok(data.uint32 == 9, "map{mapX}{arrayX}[2] is 9"); + assert_true_desc(data.uint32 == 9, "map{mapX}{arrayX}[2] is 9"); } } @@ -280,7 +284,8 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, description, "utf8_string", NULL); - is(data.utf8_string, "", "got expected utf8_string value (NULL)"); + assert_string_equal_desc( + data.utf8_string, "", "got expected utf8_string value (NULL)"); } { @@ -309,11 +314,11 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_BYTES, description, "bytes", NULL); - ok(data.data_size == 0, "bytes field data_size is 0"); + assert_true_desc(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)"); */ + assert_true_desc(memcmp(data.bytes, expect, 0) == 0, "got expected + bytes value (NULL)"); */ } { @@ -323,7 +328,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_UINT16, description, "uint16", NULL); uint16_t expect = 0; - ok(data.uint16 == expect, "uint16 field is 0"); + assert_true_desc(data.uint16 == expect, "uint16 field is 0"); } { @@ -333,7 +338,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_UINT32, description, "uint32", NULL); uint32_t expect = 0; - cmp_ok(data.uint32, "==", expect, "uint32 field is 0"); + assert_int_equal_desc(data.uint32, expect, "uint32 field is 0"); } { @@ -344,7 +349,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, data_ok(result, MMDB_DATA_TYPE_INT32, description, "int32", NULL); int32_t expect = 0; expect *= -1; - cmp_ok(data.int32, "==", expect, "int32 field is 0"); + assert_int_equal_desc(data.int32, expect, "int32 field is 0"); } { @@ -354,7 +359,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_UINT64, description, "uint64", NULL); uint64_t expect = 0; - cmp_ok(data.uint64, "==", expect, "uint64 field is 0"); + assert_int_equal_desc(data.uint64, expect, "uint64 field is 0"); } { @@ -380,10 +385,11 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, 0x00, 0x00, 0x00}; - ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 0"); + assert_true_desc(memcmp(data.uint128, expect, 16) == 0, + "uint128 field is 0"); #else mmdb_uint128_t expect = 0; - cmp_ok(data.uint128, "==", expect, "uint128 field is 0"); + assert_int_equal_desc(data.uint128, expect, "uint128 field is 0"); #endif } @@ -393,7 +399,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok( result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL); - cmp_ok(data.boolean, "==", false, "boolean field is false"); + assert_int_equal_desc(data.boolean, false, "boolean field is false"); } { @@ -402,7 +408,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_ARRAY, description, "array", NULL); - ok(data.data_size == 0, "array field has 0 elements"); + assert_true_desc(data.data_size == 0, "array field has 0 elements"); } { @@ -411,7 +417,7 @@ void test_all_data_types_as_zero(MMDB_lookup_result_s *result, MMDB_entry_data_s data = data_ok(result, MMDB_DATA_TYPE_MAP, description, "map", NULL); - ok(data.data_size == 0, "map field has 0 elements"); + assert_true_desc(data.data_size == 0, "map field has 0 elements"); } } @@ -420,12 +426,6 @@ void run_tests(int mode, const char *mode_desc) { char *path = test_database_path(filename); MMDB_s *mmdb = open_ok(path, mode, mode_desc); - // All of the remaining tests require an open mmdb - if (NULL == mmdb) { - diag("could not open %s - skipping remaining tests", path); - return; - } - free(path); { @@ -435,15 +435,16 @@ 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); + assert_int_equal_desc( + 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); + assert_true_desc( + !result.found_entry, + "no result entry struct returned for invalid IP address '%s'", + ip); } { @@ -451,12 +452,13 @@ 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); + assert_true_desc(!result.found_entry, + "no result entry struct returned for IP address not " + "in the database " + "- %s - %s - %s", + ip, + filename, + mode_desc); } { @@ -464,21 +466,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); - - cmp_ok(result.entry.offset, - ">", - 0, - "result.entry.offset > 0 for address in the database - %s - %s " - "- %s", - ip, - filename, - mode_desc); + assert_true_desc(result.found_entry, + "got a result entry struct for IP address in the " + "database - %s - %s " + "- %s", + ip, + filename, + mode_desc); + + assert_true_desc( + 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); } @@ -488,21 +490,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); - - cmp_ok(result.entry.offset, - ">", - 0, - "result.entry.offset > 0 for address in the database - %s - %s " - "- %s", - ip, - filename, - mode_desc); + assert_true_desc(result.found_entry, + "got a result entry struct for IP address in the " + "database - %s - %s " + "- %s", + ip, + filename, + mode_desc); + + assert_true_desc( + 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); } @@ -512,12 +514,13 @@ 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); + assert_true_desc(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); } @@ -526,8 +529,11 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } +static void test_data_types(void **UNUSED(state)) { for_all_modes(&run_tests); } + int main(void) { - plan(NO_PLAN); - for_all_modes(&run_tests); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_data_types), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/double_close_t.c b/t/double_close_t.c index 606fe84f..48a7c6f3 100644 --- a/t/double_close_t.c +++ b/t/double_close_t.c @@ -1,47 +1,41 @@ #include "maxminddb_test_helper.h" -void test_double_close(void) { +void test_double_close(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-ipv4-24.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); free(db_file); - cmp_ok(status, "==", MMDB_SUCCESS, "MMDB_open succeeded"); - - if (status != MMDB_SUCCESS) { - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "MMDB_open succeeded"); /* First close should work normally */ MMDB_close(&mmdb); - ok(mmdb.file_content == NULL, "file_content is NULL after first close"); - ok(mmdb.data_section == NULL, "data_section is NULL after close"); - ok(mmdb.metadata_section == NULL, "metadata_section is NULL after close"); - cmp_ok(mmdb.metadata.languages.count, - "==", - 0, - "languages.count is 0 after close"); - cmp_ok(mmdb.metadata.description.count, - "==", - 0, - "description.count is 0 after close"); - cmp_ok(mmdb.file_size, "==", 0, "file_size is 0 after close"); - cmp_ok( - mmdb.data_section_size, "==", 0, "data_section_size is 0 after close"); - cmp_ok(mmdb.metadata_section_size, - "==", - 0, - "metadata_section_size is 0 after close"); + assert_true_desc(mmdb.file_content == NULL, + "file_content is NULL after first close"); + assert_true_desc(mmdb.data_section == NULL, + "data_section is NULL after close"); + assert_true_desc(mmdb.metadata_section == NULL, + "metadata_section is NULL after close"); + assert_int_equal_desc( + mmdb.metadata.languages.count, 0, "languages.count is 0 after close"); + assert_int_equal_desc(mmdb.metadata.description.count, + 0, + "description.count is 0 after close"); + assert_int_equal_desc(mmdb.file_size, 0, "file_size is 0 after close"); + assert_int_equal_desc( + mmdb.data_section_size, 0, "data_section_size is 0 after close"); + assert_int_equal_desc(mmdb.metadata_section_size, + 0, + "metadata_section_size is 0 after close"); /* Second close should be a safe no-op (file_content was NULLed) */ MMDB_close(&mmdb); - - ok(1, "calling MMDB_close twice does not crash"); } int main(void) { - plan(NO_PLAN); - test_double_close(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_double_close), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/dump_t.c b/t/dump_t.c index 2c817d03..7aab446d 100644 --- a/t/dump_t.c +++ b/t/dump_t.c @@ -16,7 +16,8 @@ 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"); + assert_true_desc(MMDB_SUCCESS == status, + "MMDB_get_entry_data_list is successful"); char *dump_output; size_t dump_size; @@ -25,11 +26,12 @@ void run_tests(int mode, const char *mode_desc) { fclose(stream); MMDB_free_entry_data_list(entry_data_list); - ok(MMDB_SUCCESS == status, - "MMDB_dump_entry_data_list is successful - %s", - mode_desc); + assert_true_desc(MMDB_SUCCESS == status, + "MMDB_dump_entry_data_list is successful - %s", + mode_desc); - cmp_ok(dump_size, ">", 0, "MMDB_dump produced output - %s", mode_desc); + assert_true_desc( + dump_size > 0, "MMDB_dump produced output - %s", mode_desc); char *expect[] = {"{", " \"array\": ", @@ -75,10 +77,10 @@ void run_tests(int mode, const char *mode_desc) { "}"}; for (int i = 0; i < 42; i++) { - ok((strstr(dump_output, expect[i]) != NULL), - "dump output contains expected line (%s) - %s", - expect[i], - mode_desc); + assert_true_desc(strstr(dump_output, expect[i]) != NULL, + "dump output contains expected line (%s) - %s", + expect[i], + mode_desc); } free(dump_output); @@ -87,13 +89,17 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } -int main(void) { - plan(NO_PLAN); - for_all_modes(&run_tests); - done_testing(); -} +static void test_dump(void **UNUSED(state)) { for_all_modes(&run_tests); } #else -int main(void) { - plan(SKIP_ALL, "This test requires the open_memstream() function"); +static void test_dump(void **UNUSED(state)) { + print_message("This test requires the open_memstream() function\n"); + skip(); } #endif + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_dump), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/t/empty_container_metadata_t.c b/t/empty_container_metadata_t.c index 7799ae78..3db64709 100644 --- a/t/empty_container_metadata_t.c +++ b/t/empty_container_metadata_t.c @@ -6,38 +6,33 @@ static void test_db_opens_and_lookup_succeeds(const char *filename, MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, open_msg); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "%s", open_msg); int gai_error, mmdb_error; MMDB_lookup_string(&mmdb, "1.2.3.4", &gai_error, &mmdb_error); - cmp_ok(gai_error, "==", 0, "getaddrinfo succeeded"); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); + assert_int_equal_desc(gai_error, 0, "getaddrinfo succeeded"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); MMDB_close(&mmdb); free(db_file); } -void test_empty_map_last_in_metadata(void) { +void test_empty_map_last_in_metadata(void **UNUSED(state)) { test_db_opens_and_lookup_succeeds( "libmaxminddb-empty-map-last-in-metadata.mmdb", "opened MMDB with empty map at end of metadata"); } -void test_empty_array_last_in_metadata(void) { +void test_empty_array_last_in_metadata(void **UNUSED(state)) { test_db_opens_and_lookup_succeeds( "libmaxminddb-empty-array-last-in-metadata.mmdb", "opened MMDB with empty array at end of metadata"); } int main(void) { - plan(NO_PLAN); - test_empty_map_last_in_metadata(); - test_empty_array_last_in_metadata(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_empty_map_last_in_metadata), + cmocka_unit_test(test_empty_array_last_in_metadata), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/gai_error_t.c b/t/gai_error_t.c index 9e9f0304..04b5d398 100644 --- a/t/gai_error_t.c +++ b/t/gai_error_t.c @@ -1,14 +1,10 @@ #include "maxminddb_test_helper.h" -void test_mmdb_error_set_on_gai_failure(void) { +void test_mmdb_error_set_on_gai_failure(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-ipv4-24.mmdb"); MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, "mmap mode"); free(db_file); - if (!mmdb) { - return; - } - /* Set mmdb_error to a known non-zero value to detect if it gets written */ int gai_error = 0; int mmdb_error = 0xDEAD; @@ -16,18 +12,20 @@ void test_mmdb_error_set_on_gai_failure(void) { /* ".." is not a valid IP address - getaddrinfo will fail */ MMDB_lookup_string(mmdb, "..", &gai_error, &mmdb_error); - ok(gai_error != 0, "gai_error is non-zero for invalid IP '..'"); - cmp_ok(mmdb_error, - "==", - MMDB_SUCCESS, - "mmdb_error is set to MMDB_SUCCESS when gai_error is non-zero"); + assert_true_desc(gai_error != 0, + "gai_error is non-zero for invalid IP '..'"); + assert_int_equal_desc( + mmdb_error, + MMDB_SUCCESS, + "mmdb_error is set to MMDB_SUCCESS when gai_error is non-zero"); MMDB_close(mmdb); free(mmdb); } int main(void) { - plan(NO_PLAN); - test_mmdb_error_set_on_gai_failure(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_mmdb_error_set_on_gai_failure), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/get_value_pointer_bug_t.c b/t/get_value_pointer_bug_t.c index 2d1d5290..577ebf08 100644 --- a/t/get_value_pointer_bug_t.c +++ b/t/get_value_pointer_bug_t.c @@ -29,20 +29,12 @@ void test_one_ip(MMDB_s *mmdb, "iso_code", NULL); - if (ok(entry_data.has_data, "found data for country{iso_code}")) { - char *string = - mmdb_strndup(entry_data.utf8_string, entry_data.data_size); - if (!string) { - ok(0, "mmdb_strndup() call failed"); - exit(1); - } - if (!ok(strcmp(string, country_code) == 0, - "iso_code is %s", - country_code)) { - diag(" value is %s", string); - } - free(string); - } + assert_true_desc(entry_data.has_data, "found data for country{iso_code}"); + char *string = mmdb_strndup(entry_data.utf8_string, entry_data.data_size); + assert_non_null(string); + assert_string_equal_desc( + string, country_code, "iso_code is %s", country_code); + free(string); } void run_tests(int mode, const char *mode_desc) { @@ -64,8 +56,13 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_get_value_pointer_bug(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_get_value_pointer_bug), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/get_value_t.c b/t/get_value_t.c index 06dcf3b5..6d80aaf5 100644 --- a/t/get_value_t.c +++ b/t/get_value_t.c @@ -3,65 +3,57 @@ 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, - "returned entry type is uint32 - array[0]"); - cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[0]"); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - array[0]", + function); + assert_true_desc(entry_data.has_data, "found a value for array[0]"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - array[0]"); + assert_int_equal_desc(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); - ok(entry_data.has_data, "found a value for array[2]"); - 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]"); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - array[2]", + function); + assert_true_desc(entry_data.has_data, "found a value for array[2]"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - array[2]"); + assert_int_equal_desc(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); - ok(entry_data.has_data, "found a value for array[-3]"); - 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]"); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - array[-3]", + function); + assert_true_desc(entry_data.has_data, "found a value for array[-3]"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - array[-3]"); + assert_int_equal_desc(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); - ok(entry_data.has_data, "found a value for array[-1]"); - 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]"); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - array[-1]", + function); + assert_true_desc(entry_data.has_data, "found a value for array[-1]"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - array[-1]"); + assert_int_equal_desc(entry_data.uint32, 3, "entry value is 3 - array[-1]"); } int call_vget_value(MMDB_entry_s *entry, MMDB_entry_data_s *entry_data, ...) { @@ -117,10 +109,10 @@ 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", "zero", NULL); - cmp_ok(status, - "==", - MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR, - "MMDB_get_value() returns error on non-integer array index"); + assert_int_equal_desc( + status, + MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR, + "MMDB_get_value() returns error on non-integer array index"); } { @@ -157,19 +149,18 @@ 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, - "MMDB_get_value() returns error on too large negative integer"); + assert_int_equal_desc( + 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( + assert_int_equal_desc( status, - "==", MMDB_INVALID_LOOKUP_PATH_ERROR, "MMDB_get_value() returns error on integer smaller than LONG_MIN"); } @@ -178,9 +169,8 @@ 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", "18446744073709551616", NULL); - cmp_ok( + assert_int_equal_desc( status, - "==", MMDB_INVALID_LOOKUP_PATH_ERROR, "MMDB_get_value() returns error on integer larger than LONG_MAX"); } @@ -192,52 +182,53 @@ void test_simple_structure(int mode, const char *mode_desc) { 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, - "returned entry type is uint32 - map1{map2}{array}[0]{map3}{a}"); - cmp_ok(entry_data.uint32, - "==", - 1, - "entry value is 1 - map1{map2}{array}[0]{map3}{a}"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{a}", + function); + assert_true_desc(entry_data.has_data, + "found a value for map1{map2}{array}[0]{map3}{a}"); + assert_int_equal_desc( + entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - map1{map2}{array}[0]{map3}{a}"); + assert_int_equal_desc(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, - "returned entry type is uint32 - map1{map2}{array}[0]{map3}{c}"); - cmp_ok(entry_data.uint32, - "==", - 3, - "entry value is 3 - map1{map2}{array}[0]{map3}{c}"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{c}", + function); + assert_true_desc(entry_data.has_data, + "found a value for map1{map2}{array}[0]{map3}{c}"); + assert_int_equal_desc( + entry_data.type, + MMDB_DATA_TYPE_UINT32, + "returned entry type is uint32 - map1{map2}{array}[0]{map3}{c}"); + assert_int_equal_desc(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, - "status for %s() is MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR - %s", - function, - path_description); - ok(!entry_data.has_data, "did not find a value for %s", path_description); + assert_int_equal_desc( + 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); + assert_true_desc( + !entry_data.has_data, "did not find a value for %s", path_description); } void test_nested_structure(int mode, const char *mode_desc) { @@ -397,8 +388,11 @@ void run_tests(int mode, const char *mode_desc) { test_nested_structure(mode, mode_desc); } +static void test_get_value(void **UNUSED(state)) { for_all_modes(&run_tests); } + int main(void) { - plan(NO_PLAN); - for_all_modes(&run_tests); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_get_value), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/invalid_sockaddr_t.c b/t/invalid_sockaddr_t.c index 32535437..8885608e 100644 --- a/t/invalid_sockaddr_t.c +++ b/t/invalid_sockaddr_t.c @@ -8,29 +8,24 @@ static void test_invalid_sockaddr_family(const char *filename, MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, open_msg); free(db_file); - if (!mmdb) { - return; - } - struct sockaddr addr = {.sa_family = family}; int mmdb_error = MMDB_SUCCESS; MMDB_lookup_result_s result = MMDB_lookup_sockaddr(mmdb, &addr, &mmdb_error); - ok(!result.found_entry, "%s: no entry returned", family_msg); - cmp_ok(result.netmask, "==", 0, "%s: netmask left at zero", family_msg); - cmp_ok(mmdb_error, - "==", - MMDB_INVALID_NETWORK_ADDRESS_ERROR, - "%s: MMDB_lookup_sockaddr rejects unsupported family", - family_msg); + assert_true_desc(!result.found_entry, "%s: no entry returned", family_msg); + assert_int_equal_desc( + result.netmask, 0, "%s: netmask left at zero", family_msg); + assert_int_equal_desc(mmdb_error, + MMDB_INVALID_NETWORK_ADDRESS_ERROR, + "%s: MMDB_lookup_sockaddr rejects unsupported family", + family_msg); MMDB_close(mmdb); free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_invalid_sockaddr(void **UNUSED(state)) { test_invalid_sockaddr_family("MaxMind-DB-test-ipv4-24.mmdb", AF_UNIX, "opened IPv4 test database (AF_UNIX)", @@ -47,5 +42,11 @@ int main(void) { AF_UNSPEC, "opened IPv6 test database (AF_UNSPEC)", "AF_UNSPEC against IPv6 db"); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_invalid_sockaddr), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/ipv4_start_cache_t.c b/t/ipv4_start_cache_t.c index cfd9f57f..d8ec1434 100644 --- a/t/ipv4_start_cache_t.c +++ b/t/ipv4_start_cache_t.c @@ -7,11 +7,12 @@ void test_one_ip(MMDB_s *mmdb, 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); + assert_true_desc(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); } @@ -29,8 +30,13 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_ipv4_start_cache(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ipv4_start_cache), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/ipv6_lookup_in_ipv4_t.c b/t/ipv6_lookup_in_ipv4_t.c index 66dc479b..2a9b40b1 100644 --- a/t/ipv6_lookup_in_ipv4_t.c +++ b/t/ipv6_lookup_in_ipv4_t.c @@ -10,12 +10,12 @@ void run_tests(int mode, const char *mode_desc) { int gai_error, mmdb_error; 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"); + assert_int_equal_desc( + 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}; @@ -23,26 +23,31 @@ void run_tests(int mode, const char *mode_desc) { 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)); + fail_msg("getaddrinfo failed: %s", gai_strerror(gai_error)); } 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"); + assert_int_equal_desc( + 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) { - plan(NO_PLAN); +static void test_ipv6_lookup_in_ipv4(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ipv6_lookup_in_ipv4), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/libtap b/t/libtap deleted file mode 160000 index b53e4ef5..00000000 --- a/t/libtap +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b53e4ef5257f80e881762b6143834d8aae29da1a diff --git a/t/max_depth_t.c b/t/max_depth_t.c index 14ea0fc7..a44ddc0e 100644 --- a/t/max_depth_t.c +++ b/t/max_depth_t.c @@ -1,24 +1,18 @@ #include "maxminddb_test_helper.h" -void test_deep_nesting_rejected(void) { +void test_deep_nesting_rejected(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-deep-nesting.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened deeply nested MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc(status, MMDB_SUCCESS, "opened deeply nested MMDB"); int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "1.2.3.4", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); - ok(result.found_entry, "entry found"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); + assert_true_desc(result.found_entry, "entry found"); if (result.found_entry) { /* Looking up non-existent key "z" forces skip_map_or_array to @@ -27,45 +21,39 @@ void test_deep_nesting_rejected(void) { MMDB_entry_data_s entry_data; const char *lookup_path[] = {"z", NULL}; status = MMDB_aget_value(&result.entry, &entry_data, lookup_path); - cmp_ok(status, - "==", - MMDB_DECODER_LIMIT_ERROR, - "MMDB_aget_value returns MMDB_DECODER_LIMIT_ERROR for " - "deeply nested data exceeding max depth"); + assert_int_equal_desc( + status, + MMDB_DECODER_LIMIT_ERROR, + "MMDB_aget_value returns MMDB_DECODER_LIMIT_ERROR for " + "deeply nested data exceeding max depth"); } MMDB_close(&mmdb); free(db_file); } -void test_valid_nesting_allowed(void) { +void test_valid_nesting_allowed(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-nested.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened moderately nested MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc( + status, MMDB_SUCCESS, "opened moderately nested MMDB"); int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "1.1.1.1", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); - ok(result.found_entry, "entry found"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); + assert_true_desc(result.found_entry, "entry found"); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "MMDB_get_entry_data_list succeeds for " - "valid nesting depth"); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "MMDB_get_entry_data_list succeeds for " + "valid nesting depth"); MMDB_free_entry_data_list(entry_data_list); } @@ -73,34 +61,29 @@ void test_valid_nesting_allowed(void) { free(db_file); } -void test_deep_array_nesting_rejected(void) { +void test_deep_array_nesting_rejected(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-deep-array-nesting.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, "==", MMDB_SUCCESS, "opened deeply nested array MMDB"); - - if (status != MMDB_SUCCESS) { - diag("MMDB_open failed: %s", MMDB_strerror(status)); - free(db_file); - return; - } + assert_int_equal_desc( + status, MMDB_SUCCESS, "opened deeply nested array MMDB"); int gai_error, mmdb_error; MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, "1.2.3.4", &gai_error, &mmdb_error); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); - ok(result.found_entry, "entry found"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "lookup succeeded"); + assert_true_desc(result.found_entry, "entry found"); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, - "==", - MMDB_DECODER_LIMIT_ERROR, - "MMDB_get_entry_data_list returns MMDB_DECODER_LIMIT_ERROR " - "for deeply nested arrays exceeding max depth"); + assert_int_equal_desc( + status, + MMDB_DECODER_LIMIT_ERROR, + "MMDB_get_entry_data_list returns MMDB_DECODER_LIMIT_ERROR " + "for deeply nested arrays exceeding max depth"); MMDB_free_entry_data_list(entry_data_list); } @@ -109,9 +92,10 @@ void test_deep_array_nesting_rejected(void) { } int main(void) { - plan(NO_PLAN); - test_deep_nesting_rejected(); - test_deep_array_nesting_rejected(); - test_valid_nesting_allowed(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_deep_nesting_rejected), + cmocka_unit_test(test_deep_array_nesting_rejected), + cmocka_unit_test(test_valid_nesting_allowed), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/maxminddb_test_helper.c b/t/maxminddb_test_helper.c index a049800f..91e6221a 100644 --- a/t/maxminddb_test_helper.c +++ b/t/maxminddb_test_helper.c @@ -96,7 +96,7 @@ char *bad_database_path(const char *filename) { char *dup_entry_string_or_bail(MMDB_entry_data_s entry_data) { char *string = mmdb_strndup(entry_data.utf8_string, entry_data.data_size); if (NULL == string) { - BAIL_OUT("mmdb_strndup failed"); + fail_msg("mmdb_strndup failed"); } return string; @@ -109,7 +109,7 @@ MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc) { int access_rv = access(db_file, R_OK); #endif if (access_rv != 0) { - BAIL_OUT("could not read the specified file - %s\nIf you are in a git " + fail_msg("could not read the specified file - %s\nIf you are in a git " "checkout try running 'git submodule update --init'", db_file); } @@ -117,31 +117,21 @@ MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc) { MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s)); if (NULL == mmdb) { - BAIL_OUT("could not allocate memory for our MMDB_s struct"); + fail_msg("could not allocate memory for our MMDB_s struct"); } int status = MMDB_open(db_file, (uint32_t)mode, mmdb); - 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)); - free(mmdb); - return NULL; - } - - is_ok = ok(mmdb->file_size > 0, - "mmdb struct has been set for %s - %s", - db_file, - mode_desc); - - if (!is_ok) { - free(mmdb); - return NULL; - } + assert_int_equal_desc(status, + MMDB_SUCCESS, + "open %s status is success - %s (%s)", + db_file, + mode_desc, + MMDB_strerror(status)); + assert_true_desc(mmdb->file_size > 0, + "mmdb struct has been set for %s - %s", + db_file, + mode_desc); return mmdb; } @@ -202,30 +192,23 @@ void test_lookup_errors(int gai_error, 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); - - if (!is_ok) { - diag("error from call to getaddrinfo for %s - %s", - 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); - - if (!is_ok) { - diag("MMDB error - %s", MMDB_strerror(mmdb_error)); - } + assert_int_equal_desc(gai_error, + 0, + "no getaddrinfo error in call to %s for %s - %s - %s " + "(%s)", + function, + ip, + file, + mode_desc, + gai_strerror(gai_error)); + assert_int_equal_desc(mmdb_error, + MMDB_SUCCESS, + "no MMDB error in call to %s for %s - %s - %s (%s)", + function, + ip, + file, + mode_desc, + MMDB_strerror(mmdb_error)); } MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, @@ -240,43 +223,27 @@ MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, va_end(keys); - if (cmp_ok(status, - "==", - MMDB_SUCCESS, - "no error from call to MMDB_vget_value - %s", - description)) { - - if (!cmp_ok((int)data.type, - "==", - (int)expect_type, - "got the expected data type - %s", - description)) { - - diag(" data type value is %i but expected %i", - data.type, - expect_type); - } - } else { - diag(" error from MMDB_vget_value - %s", MMDB_strerror(status)); - } + assert_int_equal_desc(status, + MMDB_SUCCESS, + "no error from call to MMDB_vget_value - %s (%s)", + description, + MMDB_strerror(status)); + assert_int_equal_desc( + data.type, expect_type, "got the expected data type - %s", description); return data; } 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); - } + assert_true_desc(fabs(got - expect) < 0.01, + "double value %2.6f is approximately %2.6f", + got, + 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); - } + assert_true_desc(fabsf(got - expect) < 0.01, + "float value %2.4f is approximately %2.1f", + got, + expect); } diff --git a/t/maxminddb_test_helper.h b/t/maxminddb_test_helper.h index 0b66d0b9..8e373616 100644 --- a/t/maxminddb_test_helper.h +++ b/t/maxminddb_test_helper.h @@ -8,13 +8,20 @@ #if HAVE_CONFIG_H #include #endif -#include "libtap/tap.h" #include "maxminddb-compat-util.h" #include "maxminddb.h" #include #include #include +// cmocka.h needs these four headers included first. +#include +#include +#include +#include + +#include + #ifdef _WIN32 #include #include @@ -42,6 +49,37 @@ #define MAX_DESCRIPTION_LENGTH 500 +// cmocka assertions do not take a description. These print a printf-style +// description when the check fails and then hand off to the cmocka assertion, +// which reports the values and the source location. The operands are +// evaluated twice, so pass expressions without side effects. + #define assert_true_desc(c, ...) \ + do { \ + if (!(c)) { \ + print_error(__VA_ARGS__); \ + print_error("\n"); \ + } \ + assert_true(c); \ + } while (0) + + #define assert_int_equal_desc(a, b, ...) \ + do { \ + if ((intmax_t)(a) != (intmax_t)(b)) { \ + print_error(__VA_ARGS__); \ + print_error("\n"); \ + } \ + assert_int_equal(a, b); \ + } while (0) + + #define assert_string_equal_desc(a, b, ...) \ + do { \ + if (strcmp((a), (b)) != 0) { \ + print_error(__VA_ARGS__); \ + print_error("\n"); \ + } \ + assert_string_equal(a, b); \ + } while (0) + extern void for_all_record_sizes(const char *filename_fmt, void (*tests)(int record_size, const char *filename, diff --git a/t/metadata_marker_t.c b/t/metadata_marker_t.c index ee004bb3..830292d3 100644 --- a/t/metadata_marker_t.c +++ b/t/metadata_marker_t.c @@ -1,13 +1,13 @@ #include "maxminddb_test_helper.h" -static void test_trailing_metadata_marker(void) { +static void test_trailing_metadata_marker(void **UNUSED(state)) { char *db_file = bad_database_path("libmaxminddb-metadata-marker-only.mmdb"); MMDB_s mmdb; int status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, - "==", - MMDB_INVALID_METADATA_ERROR, - "MMDB_open rejects a file containing only the metadata marker"); + assert_int_equal_desc( + status, + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a file containing only the metadata marker"); if (status == MMDB_SUCCESS) { MMDB_close(&mmdb); @@ -17,7 +17,8 @@ static void test_trailing_metadata_marker(void) { } int main(void) { - plan(NO_PLAN); - test_trailing_metadata_marker(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_trailing_metadata_marker), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/metadata_pointers_t.c b/t/metadata_pointers_t.c index f3f9ff5c..2fe081c3 100644 --- a/t/metadata_pointers_t.c +++ b/t/metadata_pointers_t.c @@ -8,24 +8,30 @@ void run_tests(int mode, const char *mode_desc) { char *repeated_string = "Lots of pointers in metadata"; - is(mmdb->metadata.database_type, - repeated_string, - "decoded pointer database_type"); + assert_string_equal_desc(mmdb->metadata.database_type, + repeated_string, + "decoded pointer database_type"); for (uint16_t i = 0; i < mmdb->metadata.description.count; i++) { const char *language = mmdb->metadata.description.descriptions[i]->language; const char *description = mmdb->metadata.description.descriptions[i]->description; - is(description, repeated_string, "%s description", language); + assert_string_equal_desc( + description, repeated_string, "%s description", language); } MMDB_close(mmdb); free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_metadata_pointers(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_metadata_pointers), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/metadata_t.c b/t/metadata_t.c index a7e58560..d903bc33 100644 --- a/t/metadata_t.c +++ b/t/metadata_t.c @@ -1,117 +1,94 @@ #include "maxminddb_test_helper.h" void test_metadata(MMDB_s *mmdb, const char *mode_desc) { - cmp_ok(mmdb->metadata.node_count, - "==", - 163, - "node_count is 163 - %s", - mode_desc); - 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", - mode_desc); + assert_int_equal_desc( + mmdb->metadata.node_count, 163, "node_count is 163 - %s", mode_desc); + assert_int_equal_desc( + mmdb->metadata.record_size, 24, "record_size is 24 - %s", mode_desc); + assert_int_equal_desc( + mmdb->metadata.ip_version, 4, "ip_version is 4 - %s", mode_desc); + assert_string_equal_desc(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); - 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", - 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", - mode_desc); - - cmp_ok(mmdb->metadata.description.count, - "==", - 2, - "found 2 descriptions - %s", - mode_desc); + assert_true_desc(mmdb->metadata.build_epoch >= expect_epoch, + "build_epoch is at least %llu", + (unsigned long long)expect_epoch); + + assert_int_equal_desc(mmdb->metadata.binary_format_major_version, + 2, + "binary_format_major_version is 2 - %s", + mode_desc); + assert_int_equal_desc(mmdb->metadata.binary_format_minor_version, + 0, + "binary_format_minor_version is 0 - %s", + mode_desc); + + assert_int_equal_desc( + mmdb->metadata.languages.count, 2, "found 2 languages - %s", mode_desc); + assert_string_equal_desc(mmdb->metadata.languages.names[0], + "en", + "first language is en - %s", + mode_desc); + assert_string_equal_desc(mmdb->metadata.languages.names[1], + "zh", + "second language is zh - %s", + mode_desc); + + assert_int_equal_desc(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; const char *description = mmdb->metadata.description.descriptions[i]->description; if (strncmp(language, "en", 2) == 0) { - ok(1, "found en description"); - is(description, "Test Database", "en description"); + assert_string_equal_desc( + description, "Test Database", "en description"); } else if (strncmp(language, "zh", 2) == 0) { - ok(1, "found zh description"); - is(description, "Test Database Chinese", "zh description"); + assert_string_equal_desc( + description, "Test Database Chinese", "zh description"); } else { - ok(0, - "found unknown description in unexpected language - %s", - language); + fail_msg("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); + assert_int_equal_desc(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 *languages = entry_data_list = entry_data_list->next; - 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, - "'languages' key's value has 2 elements"); + assert_int_equal_desc(languages->entry_data.type, + MMDB_DATA_TYPE_ARRAY, + "'languages' key's value is an array"); + assert_int_equal_desc(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, - "first array entry is a UTF8_STRING"); + assert_int_equal_desc(idx0->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "first array entry is a UTF8_STRING"); char *lang0 = dup_entry_string_or_bail(idx0->entry_data); - is(lang0, "en", "first language is en"); + assert_string_equal_desc(lang0, "en", "first language is en"); free(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, - "second array entry is a UTF8_STRING"); + assert_int_equal_desc(idx1->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "second array entry is a UTF8_STRING"); char *lang1 = dup_entry_string_or_bail(idx1->entry_data); - is(lang1, "zh", "second language is zh"); + assert_string_equal_desc(lang1, "zh", "second language is zh"); free(lang1); return entry_data_list; @@ -121,40 +98,37 @@ 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, - "'description' key's value is a map"); - cmp_ok(description->entry_data.data_size, - "==", - 2, - "'description' key's value has 2 key/value pairs"); + assert_int_equal_desc(description->entry_data.type, + MMDB_DATA_TYPE_MAP, + "'description' key's value is a map"); + assert_int_equal_desc(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_DATA_TYPE_UTF8_STRING, - "found a map key in 'map'"); + assert_int_equal_desc(key->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "found a map key in 'map'"); 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, - "map value is a UTF8_STRING"); + assert_int_equal_desc(value->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "map value is a UTF8_STRING"); char *description = dup_entry_string_or_bail(value->entry_data); if (strcmp(key_name, "en") == 0) { - is(description, - "Test Database", - "en description == 'Test Database'"); + assert_string_equal_desc(description, + "Test Database", + "en description == 'Test Database'"); } else if (strcmp(key_name, "zh") == 0) { - is(description, - "Test Database Chinese", - "zh description == 'Test Database Chinese'"); + assert_string_equal_desc( + description, + "Test Database Chinese", + "zh description == 'Test Database Chinese'"); } else { - ok(0, "unknown key found in description map - %s", key_name); + fail_msg("unknown key found in description map - %s", key_name); } free(key_name); @@ -170,16 +144,13 @@ void test_metadata_as_data_entry_list(MMDB_s *mmdb, const char *mode_desc) { first = entry_data_list; - cmp_ok(status, - "==", - MMDB_SUCCESS, - "get metadata as data_entry_list - %s", - mode_desc); + assert_int_equal_desc(status, + MMDB_SUCCESS, + "get metadata as data_entry_list - %s", + mode_desc); - cmp_ok(first->entry_data.data_size, - "==", - 9, - "metadata map has 9 key/value pairs"); + assert_int_equal_desc( + 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; @@ -188,55 +159,55 @@ void test_metadata_as_data_entry_list(MMDB_s *mmdb, const char *mode_desc) { break; } - cmp_ok(key->entry_data.type, - "==", - MMDB_DATA_TYPE_UTF8_STRING, - "found a map key"); + assert_int_equal_desc(key->entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "found a map key"); 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; - cmp_ok(value->entry_data.uint32, "==", 163, "node_count == 163"); + assert_int_equal_desc( + value->entry_data.uint32, 163, "node_count == 163"); } else if (strcmp(key_name, "record_size") == 0) { MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next; - cmp_ok(value->entry_data.uint16, "==", 24, "record_size == 24"); + assert_int_equal_desc( + 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; - cmp_ok(value->entry_data.uint16, "==", 4, "ip_version == 4"); + assert_int_equal_desc( + 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, - "binary_format_major_version == 2"); + assert_int_equal_desc(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, - "binary_format_minor_version == 0"); + assert_int_equal_desc(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; - ok(value->entry_data.uint64 > 1373571901, - "build_epoch > 1373571901"); + assert_true_desc(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; char *type = dup_entry_string_or_bail(value->entry_data); - is(type, "Test", "type == Test"); + assert_string_equal_desc(type, "Test", "type == Test"); free(type); } else if (strcmp(key_name, "languages") == 0) { entry_data_list = test_languages_value(entry_data_list); } 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); + fail_msg("unknown key found in metadata map - %s", key_name); } free(key_name); @@ -250,11 +221,6 @@ void run_tests(int mode, const char *mode_desc) { char *path = test_database_path(file); MMDB_s *mmdb = open_ok(path, mode, mode_desc); - // All of the remaining tests require an open mmdb - if (NULL == mmdb) { - diag("could not open %s - skipping remaining tests", path); - return; - } free(path); test_metadata(mmdb, mode_desc); @@ -264,8 +230,13 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_metadata_from_database(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_metadata_from_database), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/no_map_get_value_t.c b/t/no_map_get_value_t.c index 8f5b79ea..eb1304b3 100644 --- a/t/no_map_get_value_t.c +++ b/t/no_map_get_value_t.c @@ -13,22 +13,25 @@ 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, - "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, - "returned entry type is utf8_string"); + assert_int_equal_desc( + status, MMDB_SUCCESS, "status for MMDB_get_value() is MMDB_SUCCESS"); + assert_true_desc(entry_data.has_data, + "found a value when varargs list is just NULL"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_UTF8_STRING, + "returned entry type is utf8_string"); MMDB_close(mmdb); free(mmdb); } -int main(void) { - plan(NO_PLAN); +static void test_no_map_get_value(void **UNUSED(state)) { for_all_modes(&run_tests); - done_testing(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_no_map_get_value), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/overflow_bounds_t.c b/t/overflow_bounds_t.c index 136cd370..fde790b8 100644 --- a/t/overflow_bounds_t.c +++ b/t/overflow_bounds_t.c @@ -9,26 +9,22 @@ * The real protection is the cast to uint64_t before addition, * matching the pattern used elsewhere in the codebase. */ -void test_normal_lookup_still_works(void) { +void test_normal_lookup_still_works(void **UNUSED(state)) { char *db_file = test_database_path("MaxMind-DB-test-ipv4-24.mmdb"); MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, "mmap mode"); free(db_file); - if (!mmdb) { - return; - } - int gai_error, mmdb_error; (void)MMDB_lookup_string(mmdb, "1.1.1.1", &gai_error, &mmdb_error); - cmp_ok(gai_error, "==", 0, "no gai error"); - cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "no mmdb error"); + assert_int_equal_desc(gai_error, 0, "no gai error"); + assert_int_equal_desc(mmdb_error, MMDB_SUCCESS, "no mmdb error"); MMDB_close(mmdb); free(mmdb); } -void test_record_type_bounds(void) { +void test_record_type_bounds(void **UNUSED(state)) { /* Test that record_type correctly handles values near the boundary. * With the uint64_t cast fix, valid records should still be classified * correctly. */ @@ -36,32 +32,29 @@ void test_record_type_bounds(void) { MMDB_s *mmdb = open_ok(db_file, MMDB_MODE_MMAP, "mmap mode"); free(db_file); - if (!mmdb) { - return; - } - /* Read node 0 and verify records are valid types */ MMDB_search_node_s node; int status = MMDB_read_node(mmdb, 0, &node); - cmp_ok(status, "==", MMDB_SUCCESS, "MMDB_read_node succeeded"); + assert_int_equal_desc(status, MMDB_SUCCESS, "MMDB_read_node succeeded"); - ok(node.left_record_type == MMDB_RECORD_TYPE_SEARCH_NODE || - node.left_record_type == MMDB_RECORD_TYPE_EMPTY || - node.left_record_type == MMDB_RECORD_TYPE_DATA, - "left record type is valid"); + assert_true_desc(node.left_record_type == MMDB_RECORD_TYPE_SEARCH_NODE || + node.left_record_type == MMDB_RECORD_TYPE_EMPTY || + node.left_record_type == MMDB_RECORD_TYPE_DATA, + "left record type is valid"); - ok(node.right_record_type == MMDB_RECORD_TYPE_SEARCH_NODE || - node.right_record_type == MMDB_RECORD_TYPE_EMPTY || - node.right_record_type == MMDB_RECORD_TYPE_DATA, - "right record type is valid"); + assert_true_desc(node.right_record_type == MMDB_RECORD_TYPE_SEARCH_NODE || + node.right_record_type == MMDB_RECORD_TYPE_EMPTY || + node.right_record_type == MMDB_RECORD_TYPE_DATA, + "right record type is valid"); MMDB_close(mmdb); free(mmdb); } int main(void) { - plan(NO_PLAN); - test_normal_lookup_still_works(); - test_record_type_bounds(); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_normal_lookup_still_works), + cmocka_unit_test(test_record_type_bounds), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/pointer_dos_t.c b/t/pointer_dos_t.c index 2d77e7c1..bf909e31 100644 --- a/t/pointer_dos_t.c +++ b/t/pointer_dos_t.c @@ -6,25 +6,22 @@ static void test_record_rejected(const char *fixture, char *path = test_database_path(fixture); MMDB_s *mmdb = open_ok(path, MMDB_MODE_MMAP, desc); free(path); - if (!mmdb) { - return; - } MMDB_lookup_result_s result = lookup_string_ok(mmdb, address, fixture, desc); - ok(result.found_entry, "%s: entry found", desc); + assert_true_desc(result.found_entry, "%s: entry found", desc); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; int const status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, - "==", - MMDB_DECODER_LIMIT_ERROR, - "%s: full decode returns MMDB_DECODER_LIMIT_ERROR", - desc); - ok(entry_data_list == NULL, - "%s: error leaves the output list set to NULL", - desc); + assert_int_equal_desc( + status, + MMDB_DECODER_LIMIT_ERROR, + "%s: full decode returns MMDB_DECODER_LIMIT_ERROR", + desc); + assert_true_desc(entry_data_list == NULL, + "%s: error leaves the output list set to NULL", + desc); MMDB_free_entry_data_list(entry_data_list); } @@ -40,19 +37,18 @@ static void test_record_allowed(const char *fixture, char *path = test_database_path(fixture); MMDB_s *mmdb = open_ok(path, MMDB_MODE_MMAP, desc); free(path); - if (!mmdb) { - return; - } MMDB_lookup_result_s result = lookup_string_ok(mmdb, address, fixture, desc); - ok(result.found_entry, "%s: entry found", desc); + assert_true_desc(result.found_entry, "%s: entry found", desc); if (result.found_entry) { MMDB_entry_data_list_s *entry_data_list = NULL; int const status = MMDB_get_entry_data_list(&result.entry, &entry_data_list); - cmp_ok(status, "==", MMDB_SUCCESS, "%s: full decode succeeds", desc); - ok(entry_data_list != NULL, "%s: full decode returns a list", desc); + assert_int_equal_desc( + status, MMDB_SUCCESS, "%s: full decode succeeds", desc); + assert_true_desc( + entry_data_list != NULL, "%s: full decode returns a list", desc); size_t values = 0; uint64_t payload = 0; @@ -64,16 +60,14 @@ static void test_record_allowed(const char *fixture, payload += node->entry_data.data_size; } } - cmp_ok(values, - "==", - expected_values, - "%s: decoded the expected number of values", - desc); - cmp_ok(payload, - "==", - expected_payload, - "%s: decoded the expected payload bytes", - desc); + assert_int_equal_desc(values, + expected_values, + "%s: decoded the expected number of values", + desc); + assert_int_equal_desc(payload, + expected_payload, + "%s: decoded the expected payload bytes", + desc); MMDB_free_entry_data_list(entry_data_list); } @@ -86,37 +80,34 @@ static void test_per_call_state(void) { char *path = test_database_path(fixture); MMDB_s *mmdb = open_ok(path, MMDB_MODE_MMAP, "per-call state"); free(path); - if (!mmdb) { - return; - } MMDB_lookup_result_s result = lookup_string_ok(mmdb, "1.1.1.1", fixture, "per-call state"); - ok(result.found_entry, "per-call state: entry found"); + assert_true_desc(result.found_entry, "per-call state: entry found"); if (result.found_entry) { for (int i = 1; i <= 2; i++) { MMDB_entry_data_list_s *list = NULL; int const status = MMDB_get_entry_data_list(&result.entry, &list); - cmp_ok(status, - "==", - MMDB_DECODER_LIMIT_ERROR, - "per-call: attack decode %d is rejected", - i); - ok(list == NULL, - "per-call: attack decode %d leaves a NULL list", - i); + assert_int_equal_desc(status, + MMDB_DECODER_LIMIT_ERROR, + "per-call: attack decode %d is rejected", + i); + assert_true_desc(list == NULL, + "per-call: attack decode %d leaves a NULL list", + i); MMDB_free_entry_data_list(list); } MMDB_entry_data_list_s *metadata = NULL; int const status = MMDB_get_metadata_as_entry_data_list(mmdb, &metadata); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "per-call: metadata decode on the same reader succeeds"); - ok(metadata != NULL, - "per-call: metadata decode on the same reader returns a list"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "per-call: metadata decode on the same reader succeeds"); + assert_true_desc( + metadata != NULL, + "per-call: metadata decode on the same reader returns a list"); MMDB_free_entry_data_list(metadata); } @@ -130,31 +121,28 @@ static void test_targeted_lookup_bypasses_full_decode_limit(void) { char *path = test_database_path(fixture); MMDB_s *mmdb = open_ok(path, MMDB_MODE_MMAP, desc); free(path); - if (!mmdb) { - return; - } MMDB_lookup_result_s result = lookup_string_ok(mmdb, "1.1.1.1", fixture, desc); - ok(result.found_entry, "%s: entry found", desc); + assert_true_desc(result.found_entry, "%s: entry found", desc); if (result.found_entry) { MMDB_entry_data_s entry_data; int const status = MMDB_get_value(&result.entry, &entry_data, "0", NULL); - cmp_ok(status, - "==", - MMDB_SUCCESS, - "targeted lookup succeeds without expanding the structure"); + assert_int_equal_desc( + status, + MMDB_SUCCESS, + "targeted lookup succeeds without expanding the structure"); if (status == MMDB_SUCCESS) { - ok(entry_data.has_data, "targeted lookup returns data"); - cmp_ok(entry_data.type, - "==", - MMDB_DATA_TYPE_BYTES, - "targeted lookup returns the bytes value"); - cmp_ok(entry_data.data_size, - "==", - 65535, - "targeted lookup returns the complete bytes value"); + assert_true_desc(entry_data.has_data, + "targeted lookup returns data"); + assert_int_equal_desc(entry_data.type, + MMDB_DATA_TYPE_BYTES, + "targeted lookup returns the bytes value"); + assert_int_equal_desc( + entry_data.data_size, + 65535, + "targeted lookup returns the complete bytes value"); } } @@ -167,23 +155,24 @@ static void test_metadata_limit_error(void) { test_database_path("MaxMind-DB-test-metadata-payload-limit.mmdb"); MMDB_s mmdb; int const status = MMDB_open(db_file, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, - "==", - MMDB_INVALID_METADATA_ERROR, - "metadata decoder limit is reported as invalid metadata by open"); + assert_int_equal_desc( + status, + MMDB_INVALID_METADATA_ERROR, + "metadata decoder limit is reported as invalid metadata by open"); if (status == MMDB_SUCCESS) { MMDB_close(&mmdb); } free(db_file); } -int main(void) { - plan(NO_PLAN); - - is(MMDB_strerror(MMDB_DECODER_LIMIT_ERROR), - "The decoded data structure exceeds the configured resource limits", - "decoder limit status has a distinct error message"); +static void test_decoder_limit_error_message(void **UNUSED(state)) { + assert_string_equal_desc( + MMDB_strerror(MMDB_DECODER_LIMIT_ERROR), + "The decoded data structure exceeds the configured resource limits", + "decoder limit status has a distinct error message"); +} +static void test_decoder_limits(void **UNUSED(state)) { test_record_rejected("MaxMind-DB-test-pointer-decoder-dos.mmdb", "1.1.1.1", "IPv4 value-count fan-out"); @@ -233,6 +222,12 @@ int main(void) { test_per_call_state(); test_targeted_lookup_bypasses_full_decode_limit(); test_metadata_limit_error(); +} - done_testing(); +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_decoder_limit_error_message), + cmocka_unit_test(test_decoder_limits), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/read_node_t.c b/t/read_node_t.c index b6ab6d83..bbf9f6b5 100644 --- a/t/read_node_t.c +++ b/t/read_node_t.c @@ -6,13 +6,14 @@ void test_entry_data(MMDB_s *mmdb, 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); + assert_int_equal_desc( + status, MMDB_SUCCESS, "successful data lookup for node"); + assert_int_equal_desc( + 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, @@ -24,46 +25,43 @@ void run_read_node_tests(MMDB_s *mmdb, 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], - "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, - tests[i][2]); + assert_int_equal_desc(node.left_record, + tests[i][1], + "left record for node %i is %i - %i bit DB", + node_number, + tests[i][1], + record_size); + assert_int_equal_desc(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"); } - 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, - tests[i][4]); + assert_int_equal_desc(node.right_record, + tests[i][3], + "right record for node %i is %i - %i bit DB", + node_number, + tests[i][3], + record_size); + assert_int_equal_desc(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"); } } else { - diag("call to MMDB_read_node for node %i failed - %i bit DB", - node_number, - record_size); + print_message( + "call to MMDB_read_node for node %i failed - %i bit DB\n", + node_number, + record_size); } } } @@ -266,19 +264,19 @@ void run_read_node_invalid_node_number_tests(int mode, const char *mode_desc) { /* node_count is one past the last valid node (nodes are 0-indexed). * This must be rejected. */ status = MMDB_read_node(mmdb, node_count, &node); - cmp_ok(status, - "==", - MMDB_INVALID_NODE_NUMBER_ERROR, - "MMDB_read_node with node_number == node_count returns " - "MMDB_INVALID_NODE_NUMBER_ERROR"); + assert_int_equal_desc( + status, + MMDB_INVALID_NODE_NUMBER_ERROR, + "MMDB_read_node with node_number == node_count returns " + "MMDB_INVALID_NODE_NUMBER_ERROR"); /* node_count + 1 should also be rejected. */ status = MMDB_read_node(mmdb, node_count + 1, &node); - cmp_ok(status, - "==", - MMDB_INVALID_NODE_NUMBER_ERROR, - "MMDB_read_node with node_number > node_count returns " - "MMDB_INVALID_NODE_NUMBER_ERROR"); + assert_int_equal_desc( + status, + MMDB_INVALID_NODE_NUMBER_ERROR, + "MMDB_read_node with node_number > node_count returns " + "MMDB_INVALID_NODE_NUMBER_ERROR"); MMDB_close(mmdb); free(mmdb); @@ -291,8 +289,11 @@ void run_tests(int mode, const char *mode_desc) { run_read_node_invalid_node_number_tests(mode, mode_desc); } +static void test_read_node(void **UNUSED(state)) { for_all_modes(&run_tests); } + int main(void) { - plan(NO_PLAN); - for_all_modes(&run_tests); - done_testing(); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_read_node), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/threads_t.c b/t/threads_t.c index 7adf7a30..e9f5c357 100644 --- a/t/threads_t.c +++ b/t/threads_t.c @@ -67,7 +67,7 @@ void *run_one_thread(void *arg) { test_result_s *result = malloc(sizeof(test_result_s)); if (!result) { - BAIL_OUT("could not allocate memory"); + fail_msg("could not allocate memory"); } test_one_ip(mmdb, ip, result); @@ -77,52 +77,36 @@ void *run_one_thread(void *arg) { 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); - if (!is_ok) { - return; - } - - 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); - 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); - 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); - if (!is_ok) { - return; - } - - is(result->data_value, - expect, - "found expected result for 'ip' key for %s - %s", - result->ip_looked_up, - mode_desc); + assert_int_equal_desc(result->lookup_string_gai_error, + 0, + "no getaddrinfo error for %s - %s", + result->ip_looked_up, + mode_desc); + assert_int_equal_desc(result->lookup_string_mmdb_error, + 0, + "no mmdb error for %s - %s", + result->ip_looked_up, + mode_desc); + assert_true_desc(result->found_entry, + "got a result for %s in the database - %s", + result->ip_looked_up, + mode_desc); + assert_int_equal_desc(result->get_value_status, + 0, + "no error from MMDB_get_value for %s - %s", + result->ip_looked_up, + mode_desc); + assert_true_desc( + result->data_type_ok, + "MMDB_get_value found a utf8_string at 'ip' key for %s - %s", + result->ip_looked_up, + mode_desc); + + assert_string_equal_desc(result->data_value, + expect, + "found expected result for 'ip' key for %s - %s", + result->ip_looked_up, + mode_desc); } void run_ipX_tests(MMDB_s *mmdb, @@ -141,7 +125,7 @@ void run_ipX_tests(MMDB_s *mmdb, int error = pthread_create(&threads[i], NULL, run_one_thread, &thread_args[i]); if (error) { - BAIL_OUT("pthread_create failed"); + fail_msg("pthread_create failed"); } } @@ -149,7 +133,7 @@ void run_ipX_tests(MMDB_s *mmdb, void *thread_return; int error = pthread_join(threads[i], &thread_return); if (error) { - BAIL_OUT("pthread_join failed"); + fail_msg("pthread_join failed"); } test_result_s *test_result = (test_result_s *)thread_return; @@ -199,9 +183,11 @@ void run_tests(int mode, const char *mode_desc) { free(mmdb); } +static void test_threads(void **UNUSED(state)) { for_all_modes(&run_tests); } + int main(void) { - plan(NO_PLAN); - for_all_modes(&run_tests); - done_testing(); - pthread_exit(NULL); + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_threads), + }; + return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/t/version_t.c b/t/version_t.c index 401d2a5e..4d3409fa 100644 --- a/t/version_t.c +++ b/t/version_t.c @@ -1,9 +1,14 @@ #include "maxminddb_test_helper.h" -int main(void) { +static void test_version(void **UNUSED(state)) { const char *version = MMDB_lib_version(); - if (ok((version != NULL), "MMDB_lib_version exists")) { - is(version, PACKAGE_VERSION, "version is " PACKAGE_VERSION); - } - done_testing(); + assert_non_null(version); + assert_string_equal(version, PACKAGE_VERSION); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_version), + }; + return cmocka_run_group_tests(tests, NULL, NULL); }