"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 #include <pthread.h>
    3 
    4 typedef struct thread_arg {
    5     int thread_id;
    6     MMDB_s *mmdb;
    7     const char *ip_to_lookup;
    8 } thread_arg_s;
    9 
   10 typedef struct test_result {
   11     const char *ip_looked_up;
   12     int lookup_string_gai_error;
   13     int lookup_string_mmdb_error;
   14     int found_entry;
   15     int get_value_status;
   16     int data_type_ok;
   17     char *data_value;
   18 } test_result_s;
   19 
   20 void test_one_ip(MMDB_s *mmdb, const char *ip, test_result_s *test_result) {
   21 
   22     test_result->ip_looked_up = ip;
   23 
   24     int gai_error = 0;
   25     int mmdb_error = 0;
   26     MMDB_lookup_result_s result =
   27         MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
   28 
   29     test_result->lookup_string_gai_error = gai_error;
   30     if (gai_error) {
   31         return;
   32     }
   33 
   34     test_result->lookup_string_mmdb_error = mmdb_error;
   35     if (mmdb_error) {
   36         return;
   37     }
   38 
   39     test_result->found_entry = result.found_entry;
   40     if (!result.found_entry) {
   41         return;
   42     }
   43 
   44     MMDB_entry_data_s data;
   45     int status = MMDB_get_value(&result.entry, &data, "ip", NULL);
   46 
   47     test_result->get_value_status = status;
   48     if (status) {
   49         return;
   50     }
   51 
   52     test_result->data_type_ok = data.type == MMDB_DATA_TYPE_UTF8_STRING;
   53     if (!test_result->data_type_ok) {
   54         return;
   55     }
   56 
   57     test_result->data_value = mmdb_strndup(data.utf8_string, data.data_size);
   58 
   59     return;
   60 }
   61 
   62 void *run_one_thread(void *arg) {
   63     thread_arg_s *thread_arg = (thread_arg_s *)arg;
   64 
   65     MMDB_s *mmdb = thread_arg->mmdb;
   66     const char *ip = thread_arg->ip_to_lookup;
   67 
   68     test_result_s *result = malloc(sizeof(test_result_s));
   69     if (!result) {
   70         BAIL_OUT("could not allocate memory");
   71     }
   72     test_one_ip(mmdb, ip, result);
   73 
   74     pthread_exit((void *)result);
   75 }
   76 
   77 void process_result(test_result_s *result,
   78                     const char *expect,
   79                     const char *mode_desc) {
   80     int is_ok;
   81     is_ok = ok(!result->lookup_string_gai_error,
   82                "no getaddrinfo error for %s - %s",
   83                result->ip_looked_up,
   84                mode_desc);
   85     if (!is_ok) {
   86         return;
   87     }
   88 
   89     is_ok = ok(!result->lookup_string_mmdb_error,
   90                "no mmdb error for %s - %s",
   91                result->ip_looked_up,
   92                mode_desc);
   93     if (!is_ok) {
   94         return;
   95     }
   96 
   97     is_ok = ok(result->found_entry,
   98                "got a result for %s in the database - %s",
   99                result->ip_looked_up,
  100                mode_desc);
  101     if (!is_ok) {
  102         return;
  103     }
  104 
  105     is_ok = ok(!result->get_value_status,
  106                "no error from MMDB_get_value for %s - %s",
  107                result->ip_looked_up,
  108                mode_desc);
  109     if (!is_ok) {
  110         return;
  111     }
  112 
  113     is_ok = ok(result->data_type_ok,
  114                "MMDB_get_value found a utf8_string at 'ip' key for %s - %s",
  115                result->ip_looked_up,
  116                mode_desc);
  117     if (!is_ok) {
  118         return;
  119     }
  120 
  121     is(result->data_value,
  122        expect,
  123        "found expected result for 'ip' key for %s - %s",
  124        result->ip_looked_up,
  125        mode_desc);
  126 }
  127 
  128 void run_ipX_tests(MMDB_s *mmdb,
  129                    const char *pairs[][2],
  130                    int pairs_rows,
  131                    int mode,
  132                    const char *mode_desc) {
  133     pthread_t threads[pairs_rows];
  134     struct thread_arg thread_args[pairs_rows];
  135 
  136     for (int i = 0; i < pairs_rows; i += 1) {
  137         thread_args[i].thread_id = i;
  138         thread_args[i].mmdb = mmdb;
  139         thread_args[i].ip_to_lookup = pairs[i][0];
  140 
  141         int error =
  142             pthread_create(&threads[i], NULL, run_one_thread, &thread_args[i]);
  143         if (error) {
  144             BAIL_OUT("pthread_create failed");
  145         }
  146     }
  147 
  148     for (int i = 0; i < pairs_rows; i += 1) {
  149         void *thread_return;
  150         int error = pthread_join(threads[i], &thread_return);
  151         if (error) {
  152             BAIL_OUT("pthread_join failed");
  153         }
  154 
  155         test_result_s *test_result = (test_result_s *)thread_return;
  156         if (NULL != test_result) {
  157             process_result(test_result, pairs[i][1], mode_desc);
  158             if (test_result->data_type_ok) {
  159                 free(test_result->data_value);
  160             }
  161             free(test_result);
  162         }
  163     }
  164 }
  165 
  166 void run_tests(int mode, const char *mode_desc) {
  167     const char *filename = "MaxMind-DB-test-mixed-32.mmdb";
  168     const char *path = test_database_path(filename);
  169     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
  170     free((void *)path);
  171 
  172     const char *pairs[18][2] = {
  173         {"1.1.1.1", "::1.1.1.1"},
  174         {"1.1.1.2", "::1.1.1.2"},
  175         {"1.1.1.3", "::1.1.1.2"},
  176         {"1.1.1.7", "::1.1.1.4"},
  177         {"1.1.1.9", "::1.1.1.8"},
  178         {"1.1.1.15", "::1.1.1.8"},
  179         {"1.1.1.17", "::1.1.1.16"},
  180         {"1.1.1.31", "::1.1.1.16"},
  181         {"1.1.1.32", "::1.1.1.32"},
  182         {"::1:ffff:ffff", "::1:ffff:ffff"},
  183         {"::2:0:0", "::2:0:0"},
  184         {"::2:0:1a", "::2:0:0"},
  185         {"::2:0:40", "::2:0:40"},
  186         {"::2:0:4f", "::2:0:40"},
  187         {"::2:0:50", "::2:0:50"},
  188         {"::2:0:52", "::2:0:50"},
  189         {"::2:0:58", "::2:0:58"},
  190         {"::2:0:59", "::2:0:58"},
  191     };
  192 
  193     run_ipX_tests(mmdb, pairs, 18, mode, mode_desc);
  194 
  195     MMDB_close(mmdb);
  196     free(mmdb);
  197 }
  198 
  199 int main(void) {
  200     plan(NO_PLAN);
  201     for_all_modes(&run_tests);
  202     done_testing();
  203     pthread_exit(NULL);
  204 }