"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 
    3 static void test_big_lookup(void);
    4 
    5 /* These globals are gross but it's the easiest way to mix calling
    6  * for_all_modes() and for_all_record_sizes() */
    7 static int Current_Mode;
    8 static const char *Current_Mode_Description;
    9 
   10 void test_one_result(MMDB_s *mmdb,
   11                      MMDB_lookup_result_s result,
   12                      const char *ip,
   13                      const char *expect,
   14                      const char *function,
   15                      const char *filename,
   16                      const char *mode_desc) {
   17     int is_ok = ok(result.found_entry,
   18                    "got a result for an IP in the database - %s - %s - %s - %s",
   19                    function,
   20                    ip,
   21                    filename,
   22                    mode_desc);
   23 
   24     if (!is_ok) {
   25         return;
   26     }
   27 
   28     MMDB_entry_data_s data =
   29         data_ok(&result, MMDB_DATA_TYPE_UTF8_STRING, "result{ip}", "ip", NULL);
   30 
   31     char *string = mmdb_strndup(data.utf8_string, data.data_size);
   32 
   33     char *real_expect;
   34     if (mmdb->metadata.ip_version == 4 || strncmp(expect, "::", 2) == 0) {
   35         real_expect = mmdb_strndup(expect, strlen(expect));
   36     } else {
   37         // When looking up IPv4 addresses in a mixed DB the result will be
   38         // something like "::1.2.3.4", not just "1.2.3.4".
   39         int maxlen = strlen(expect) + 3;
   40         real_expect = malloc(maxlen);
   41         if (!real_expect) {
   42             BAIL_OUT("could not allocate memory");
   43         }
   44         snprintf(real_expect, maxlen, "::%s", expect);
   45     }
   46 
   47     is(string,
   48        real_expect,
   49        "found expected result for ip key - %s - %s - %s - %s",
   50        function,
   51        ip,
   52        filename,
   53        mode_desc);
   54 
   55     free(real_expect);
   56     free(string);
   57 }
   58 
   59 void test_one_ip(MMDB_s *mmdb,
   60                  const char *ip,
   61                  const char *expect,
   62                  const char *filename,
   63                  const char *mode_desc) {
   64     MMDB_lookup_result_s result =
   65         lookup_string_ok(mmdb, ip, filename, mode_desc);
   66 
   67     test_one_result(
   68         mmdb, result, ip, expect, "MMDB_lookup_string", filename, mode_desc);
   69 
   70     result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
   71     test_one_result(
   72         mmdb, result, ip, expect, "MMDB_lookup_addrinfo", filename, mode_desc);
   73 }
   74 
   75 void run_ipX_tests(const char *filename,
   76                    const char **missing_ips,
   77                    int missing_ips_length,
   78                    const char *pairs[][2],
   79                    int pairs_rows) {
   80     const char *path = test_database_path(filename);
   81     int mode = Current_Mode;
   82     const char *mode_desc = Current_Mode_Description;
   83 
   84     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
   85     free((void *)path);
   86 
   87     char desc_suffix[500];
   88     snprintf(desc_suffix, 500, "%s - %s", filename, mode_desc);
   89 
   90     for (int i = 0; i < missing_ips_length; i++) {
   91         const char *ip = missing_ips[i];
   92 
   93         MMDB_lookup_result_s result =
   94             lookup_string_ok(mmdb, ip, filename, mode_desc);
   95 
   96         ok(!result.found_entry,
   97            "no result entry struct returned for IP address not in the database "
   98            "(string lookup) - %s - %s - %s",
   99            ip,
  100            filename,
  101            mode_desc);
  102 
  103         result = lookup_sockaddr_ok(mmdb, ip, filename, mode_desc);
  104 
  105         ok(!result.found_entry,
  106            "no result entry struct returned for IP address not in the database "
  107            "(ipv4 lookup) - %s - %s - %s",
  108            ip,
  109            filename,
  110            mode_desc);
  111     }
  112 
  113     for (int i = 0; i < pairs_rows; i += 1) {
  114         const char *ip_to_lookup = pairs[i][0];
  115         const char *expect = pairs[i][1];
  116 
  117         test_one_ip(mmdb, ip_to_lookup, expect, filename, mode_desc);
  118     }
  119 
  120     MMDB_close(mmdb);
  121     free(mmdb);
  122 }
  123 
  124 void run_ipv4_tests(int UNUSED(record_size),
  125                     const char *filename,
  126                     const char *UNUSED(ignored)) {
  127     const char *pairs[9][2] = {
  128         {"1.1.1.1", "1.1.1.1"},
  129         {"1.1.1.2", "1.1.1.2"},
  130         {"1.1.1.3", "1.1.1.2"},
  131         {"1.1.1.7", "1.1.1.4"},
  132         {"1.1.1.9", "1.1.1.8"},
  133         {"1.1.1.15", "1.1.1.8"},
  134         {"1.1.1.17", "1.1.1.16"},
  135         {"1.1.1.31", "1.1.1.16"},
  136         {"1.1.1.32", "1.1.1.32"},
  137     };
  138 
  139     const char *missing[1] = {"2.3.4.5"};
  140     run_ipX_tests(filename, missing, 1, pairs, 9);
  141 }
  142 
  143 void run_ipv6_tests(int UNUSED(record_size),
  144                     const char *filename,
  145                     const char *UNUSED(ignored)) {
  146     const char *pairs[9][2] = {
  147         {"::1:ffff:ffff", "::1:ffff:ffff"},
  148         {"::2:0:0", "::2:0:0"},
  149         {"::2:0:1a", "::2:0:0"},
  150         {"::2:0:40", "::2:0:40"},
  151         {"::2:0:4f", "::2:0:40"},
  152         {"::2:0:50", "::2:0:50"},
  153         {"::2:0:52", "::2:0:50"},
  154         {"::2:0:58", "::2:0:58"},
  155         {"::2:0:59", "::2:0:58"},
  156     };
  157 
  158     const char *missing[2] = {"2.3.4.5", "::abcd"};
  159     run_ipX_tests(filename, missing, 2, pairs, 9);
  160 }
  161 
  162 void all_record_sizes(int mode, const char *description) {
  163     const char *ipv4_filename_fmts[] = {"MaxMind-DB-test-ipv4-%i.mmdb",
  164                                         "MaxMind-DB-test-mixed-%i.mmdb"};
  165 
  166     Current_Mode = mode;
  167     Current_Mode_Description = description;
  168 
  169     for (int i = 0; i < 2; i++) {
  170         for_all_record_sizes(ipv4_filename_fmts[i], &run_ipv4_tests);
  171     }
  172 
  173     const char *ipv6_filename_fmts[] = {"MaxMind-DB-test-ipv6-%i.mmdb",
  174                                         "MaxMind-DB-test-mixed-%i.mmdb"};
  175 
  176     for (int i = 0; i < 2; i++) {
  177         for_all_record_sizes(ipv6_filename_fmts[i], &run_ipv6_tests);
  178     }
  179 }
  180 
  181 static void test_big_lookup(void) {
  182     const char *const db_filename = "GeoIP2-Precision-Enterprise-Test.mmdb";
  183     const char *const db_path = test_database_path(db_filename);
  184     ok(db_path != NULL, "got database path");
  185 
  186     MMDB_s *const mmdb = open_ok(db_path, MMDB_MODE_MMAP, "mmap mode");
  187     ok(mmdb != NULL, "opened MMDB");
  188     free((char *)db_path);
  189 
  190     int gai_err = 0, mmdb_err = 0;
  191     const char *const ip_address = "81.2.69.160";
  192     MMDB_lookup_result_s result =
  193         MMDB_lookup_string(mmdb, ip_address, &gai_err, &mmdb_err);
  194     ok(gai_err == 0, "no getaddrinfo error");
  195     ok(mmdb_err == MMDB_SUCCESS, "no error from maxminddb library");
  196     ok(result.found_entry, "found IP");
  197 
  198     MMDB_entry_data_list_s *entry_data_list = NULL;
  199     ok(MMDB_get_entry_data_list(&result.entry, &entry_data_list) ==
  200            MMDB_SUCCESS,
  201        "successfully looked up entry data list");
  202     ok(entry_data_list != NULL, "got an entry_data_list");
  203 
  204     MMDB_free_entry_data_list(entry_data_list);
  205 
  206     MMDB_close(mmdb);
  207     free(mmdb);
  208 }
  209 
  210 int main(void) {
  211     plan(NO_PLAN);
  212     for_all_modes(&all_record_sizes);
  213     test_big_lookup();
  214     done_testing();
  215 }