"Fossies" - the Fresh Open Source Software Archive

Member "libmaxminddb-1.5.2/t/bad_databases_t.c" (18 Feb 2021, 1579 Bytes) of package /linux/misc/libmaxminddb-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "bad_databases_t.c": 1.5.0_vs_1.5.2.

    1 // This test currently does not work on Windows as nftw is
    2 // not available.
    3 #define _XOPEN_SOURCE 500
    4 #include <ftw.h>
    5 
    6 #include <libgen.h>
    7 #include <unistd.h>
    8 
    9 #include "maxminddb_test_helper.h"
   10 
   11 int test_read(const char *path,
   12               const struct stat *UNUSED(sbuf),
   13               int flags,
   14               struct FTW *UNUSED(ftw)) {
   15     // Check if path is a regular file)
   16     if (flags != FTW_F) {
   17         return 0;
   18     }
   19 
   20     MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s));
   21 
   22     if (NULL == mmdb) {
   23         BAIL_OUT("could not allocate memory for our MMDB_s struct");
   24     }
   25 
   26     int status = MMDB_open(path, MMDB_MODE_MMAP, mmdb);
   27 
   28     if (status != MMDB_SUCCESS) {
   29         ok(1, "received error when opening %s", path);
   30         free(mmdb);
   31         return 0;
   32     }
   33 
   34     int gai_error, mmdb_error;
   35     MMDB_lookup_string(mmdb, "1.1.1.1", &gai_error, &mmdb_error);
   36     if (gai_error != 0) {
   37         BAIL_OUT("could not parse IP address");
   38     }
   39 
   40     cmp_ok(
   41         mmdb_error, "!=", MMDB_SUCCESS, "opening %s returned an error", path);
   42 
   43     MMDB_close(mmdb);
   44     free(mmdb);
   45     return 0;
   46 }
   47 
   48 int main(void) {
   49     char *test_db_dir;
   50 #ifdef _WIN32
   51     test_db_dir = "../t/maxmind-db/bad-data";
   52 #else
   53     char cwd[500];
   54     char *UNUSED(tmp) = getcwd(cwd, 500);
   55 
   56     if (strcmp(basename(cwd), "t") == 0) {
   57         test_db_dir = "./maxmind-db/bad-data";
   58     } else {
   59         test_db_dir = "./t/maxmind-db/bad-data";
   60     }
   61 #endif
   62     plan(NO_PLAN);
   63     if (nftw(test_db_dir, test_read, 10, FTW_PHYS) != 0) {
   64         BAIL_OUT("nftw failed");
   65     }
   66     done_testing();
   67 }