"Fossies" - the Fresh Open Source Software Archive

Member "libmaxminddb-1.5.2/t/dump_t.c" (18 Feb 2021, 3340 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 "dump_t.c": 1.5.0_vs_1.5.2.

    1 #define _XOPEN_SOURCE 700
    2 #include "maxminddb_test_helper.h"
    3 
    4 #ifdef HAVE_OPEN_MEMSTREAM
    5 void run_tests(int mode, const char *mode_desc) {
    6     const char *filename = "MaxMind-DB-test-decoder.mmdb";
    7     const char *path = test_database_path(filename);
    8     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
    9     free((void *)path);
   10 
   11     const char *ip = "1.1.1.1";
   12     MMDB_lookup_result_s result =
   13         lookup_string_ok(mmdb, ip, filename, mode_desc);
   14 
   15     MMDB_entry_data_list_s *entry_data_list;
   16     int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
   17 
   18     ok(MMDB_SUCCESS == status, "MMDB_get_entry_data_list is successful");
   19 
   20     char *dump_output;
   21     size_t dump_size;
   22     FILE *stream = open_memstream(&dump_output, &dump_size);
   23     status = MMDB_dump_entry_data_list(stream, entry_data_list, 0);
   24     fclose(stream);
   25     MMDB_free_entry_data_list(entry_data_list);
   26 
   27     ok(MMDB_SUCCESS == status,
   28        "MMDB_dump_entry_data_list is successful - %s",
   29        mode_desc);
   30 
   31     cmp_ok(dump_size, ">", 0, "MMDB_dump produced output - %s", mode_desc);
   32 
   33     char *expect[] = {"{",
   34                       "  \"array\": ",
   35                       "    [",
   36                       "      1 <uint32>",
   37                       "      2 <uint32>",
   38                       "      3 <uint32>",
   39                       "    ]",
   40                       "  \"boolean\": ",
   41                       "    true <boolean>",
   42                       "  \"bytes\": ",
   43                       "    0000002A <bytes>",
   44                       "  \"double\": ",
   45                       "    42.123456 <double>",
   46                       "  \"float\": ",
   47                       "    1.100000 <float>",
   48                       "  \"int32\": ",
   49                       "    -268435456 <int32>",
   50                       "  \"map\": ",
   51                       "    {",
   52                       "      \"mapX\": ",
   53                       "        {",
   54                       "          \"arrayX\": ",
   55                       "            [",
   56                       "              7 <uint32>",
   57                       "              8 <uint32>",
   58                       "              9 <uint32>",
   59                       "            ]",
   60                       "          \"utf8_stringX\": ",
   61                       "            \"hello\" <utf8_string>",
   62                       "        }",
   63                       "    }",
   64                       "  \"uint128\": ",
   65                       "    0x01000000000000000000000000000000 <uint128>",
   66                       "  \"uint16\": ",
   67                       "    100 <uint16>",
   68                       "  \"uint32\": ",
   69                       "    268435456 <uint32>",
   70                       "  \"uint64\": ",
   71                       "    1152921504606846976 <uint64>",
   72                       "  \"utf8_string\": ",
   73                       "    \"unicode! ☯ - ♫\" <utf8_string>",
   74                       "}"};
   75 
   76     for (int i = 0; i < 42; i++) {
   77         ok((strstr(dump_output, expect[i]) != NULL),
   78            "dump output contains expected line (%s) - %s",
   79            expect[i],
   80            mode_desc);
   81     }
   82 
   83     free(dump_output);
   84 
   85     MMDB_close(mmdb);
   86     free(mmdb);
   87 }
   88 
   89 int main(void) {
   90     plan(NO_PLAN);
   91     for_all_modes(&run_tests);
   92     done_testing();
   93 }
   94 #else
   95 int main(void) {
   96     plan(SKIP_ALL, "This test requires the open_memstream() function");
   97 }
   98 #endif