"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 
    3 /* This test exercises a bug found in MMDB_get_value for certain types of
    4  * nested data structures which contain pointers. See
    5  * https://github.com/maxmind/libmaxminddb/issues/2 and
    6  * https://github.com/maxmind/libmaxminddb/issues/3.
    7  *
    8  * There is also the potential for a similar bug when looking up a value by
    9  * path in an array. This is not tested (yet) as we don't have the right test
   10  * data for it.
   11  *
   12  * These tests are somewhat fragile since they depend on a specific data
   13  * layout in the database. Ideally the test would check that this layout
   14  * exists before checking to see if the lookups are correct.
   15  */
   16 
   17 void test_one_ip(MMDB_s *mmdb,
   18                  const char *filename,
   19                  const char *mode_desc,
   20                  char *ip,
   21                  char *country_code) {
   22     MMDB_lookup_result_s result =
   23         lookup_string_ok(mmdb, ip, filename, mode_desc);
   24 
   25     MMDB_entry_data_s entry_data = data_ok(&result,
   26                                            MMDB_DATA_TYPE_UTF8_STRING,
   27                                            "country{iso_code}",
   28                                            "country",
   29                                            "iso_code",
   30                                            NULL);
   31 
   32     if (ok(entry_data.has_data, "found data for country{iso_code}")) {
   33         char *string =
   34             mmdb_strndup(entry_data.utf8_string, entry_data.data_size);
   35         if (!string) {
   36             ok(0, "mmdb_strndup() call failed");
   37             exit(1);
   38         }
   39         if (!ok(strcmp(string, country_code) == 0,
   40                 "iso_code is %s",
   41                 country_code)) {
   42             diag("  value is %s", string);
   43         }
   44         free(string);
   45     }
   46 }
   47 
   48 void run_tests(int mode, const char *mode_desc) {
   49     const char *filename = "GeoIP2-City-Test.mmdb";
   50     const char *path = test_database_path(filename);
   51     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
   52     free((void *)path);
   53 
   54     /* This exercises a bug where the entire top-level value is a pointer to
   55      * another part of the data section. */
   56     test_one_ip(mmdb, filename, mode_desc, "2001:218::", "JP");
   57     /* This exercises a bug where one subnet's data shares part of the data
   58      * with another subnet - in this case it is the "country" key (and others)
   59      * in the top level map. We are testing that the "country" key's value is
   60      * handled correctly. The value _should_ be a pointer to another map. */
   61     test_one_ip(mmdb, filename, mode_desc, "81.2.69.160", "GB");
   62 
   63     MMDB_close(mmdb);
   64     free(mmdb);
   65 }
   66 
   67 int main(void) {
   68     plan(NO_PLAN);
   69     for_all_modes(&run_tests);
   70     done_testing();
   71 }