"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 
    3 void test_array_0_result(int status,
    4                          MMDB_entry_data_s entry_data,
    5                          char *function) {
    6     cmp_ok(status,
    7            "==",
    8            MMDB_SUCCESS,
    9            "status for %s() is MMDB_SUCCESS - array[0]",
   10            function);
   11     ok(entry_data.has_data, "found a value for array[0]");
   12     cmp_ok(entry_data.type,
   13            "==",
   14            MMDB_DATA_TYPE_UINT32,
   15            "returned entry type is uint32 - array[0]");
   16     cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[0]");
   17 }
   18 
   19 void test_array_2_result(int status,
   20                          MMDB_entry_data_s entry_data,
   21                          char *function) {
   22     cmp_ok(status,
   23            "==",
   24            MMDB_SUCCESS,
   25            "status for %s() is MMDB_SUCCESS - array[2]",
   26            function);
   27     ok(entry_data.has_data, "found a value for array[2]");
   28     cmp_ok(entry_data.type,
   29            "==",
   30            MMDB_DATA_TYPE_UINT32,
   31            "returned entry type is uint32 - array[2]");
   32     cmp_ok(entry_data.uint32, "==", 3, "entry value is 3 - array[2]");
   33 }
   34 
   35 void test_array_minus_3_result(int status,
   36                                MMDB_entry_data_s entry_data,
   37                                char *function) {
   38     cmp_ok(status,
   39            "==",
   40            MMDB_SUCCESS,
   41            "status for %s() is MMDB_SUCCESS - array[-3]",
   42            function);
   43     ok(entry_data.has_data, "found a value for array[-3]");
   44     cmp_ok(entry_data.type,
   45            "==",
   46            MMDB_DATA_TYPE_UINT32,
   47            "returned entry type is uint32 - array[-3]");
   48     cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[-3]");
   49 }
   50 
   51 void test_array_minus_1_result(int status,
   52                                MMDB_entry_data_s entry_data,
   53                                char *function) {
   54     cmp_ok(status,
   55            "==",
   56            MMDB_SUCCESS,
   57            "status for %s() is MMDB_SUCCESS - array[-1]",
   58            function);
   59     ok(entry_data.has_data, "found a value for array[-1]");
   60     cmp_ok(entry_data.type,
   61            "==",
   62            MMDB_DATA_TYPE_UINT32,
   63            "returned entry type is uint32 - array[-1]");
   64     cmp_ok(entry_data.uint32, "==", 3, "entry value is 3 - array[-1]");
   65 }
   66 
   67 int call_vget_value(MMDB_entry_s *entry, MMDB_entry_data_s *entry_data, ...) {
   68     va_list keys;
   69     va_start(keys, entry_data);
   70 
   71     int status = MMDB_vget_value(entry, entry_data, keys);
   72 
   73     va_end(keys);
   74 
   75     return status;
   76 }
   77 
   78 void test_simple_structure(int mode, const char *mode_desc) {
   79     const char *filename = "MaxMind-DB-test-decoder.mmdb";
   80     const char *path = test_database_path(filename);
   81     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
   82     free((void *)path);
   83 
   84     const char *ip = "1.1.1.1";
   85     MMDB_lookup_result_s result =
   86         lookup_string_ok(mmdb, ip, filename, mode_desc);
   87 
   88     {
   89         MMDB_entry_data_s entry_data;
   90         const char *lookup_path[] = {"array", "0", NULL};
   91         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
   92         test_array_0_result(status, entry_data, "MMDB_aget_value");
   93 
   94         status = MMDB_get_value(&result.entry, &entry_data, "array", "0", NULL);
   95         test_array_0_result(status, entry_data, "MMDB_get_value");
   96 
   97         status =
   98             call_vget_value(&result.entry, &entry_data, "array", "0", NULL);
   99         test_array_0_result(status, entry_data, "MMDB_vget_value");
  100     }
  101 
  102     {
  103         MMDB_entry_data_s entry_data;
  104         const char *lookup_path[] = {"array", "2", NULL};
  105         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  106         test_array_2_result(status, entry_data, "MMDB_aget_value");
  107 
  108         status = MMDB_get_value(&result.entry, &entry_data, "array", "2", NULL);
  109         test_array_2_result(status, entry_data, "MMDB_get_value");
  110 
  111         status =
  112             call_vget_value(&result.entry, &entry_data, "array", "2", NULL);
  113         test_array_2_result(status, entry_data, "MMDB_vget_value");
  114     }
  115 
  116     {
  117         MMDB_entry_data_s entry_data;
  118         int status =
  119             MMDB_get_value(&result.entry, &entry_data, "array", "zero", NULL);
  120         cmp_ok(status,
  121                "==",
  122                MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
  123                "MMDB_get_value() returns error on non-integer array index");
  124     }
  125 
  126     {
  127         MMDB_entry_data_s entry_data;
  128         const char *lookup_path[] = {"array", "-1", NULL};
  129         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  130         test_array_minus_1_result(status, entry_data, "MMDB_aget_value");
  131 
  132         status =
  133             MMDB_get_value(&result.entry, &entry_data, "array", "-1", NULL);
  134         test_array_minus_1_result(status, entry_data, "MMDB_get_value");
  135 
  136         status =
  137             call_vget_value(&result.entry, &entry_data, "array", "-1", NULL);
  138         test_array_minus_1_result(status, entry_data, "MMDB_vget_value");
  139     }
  140 
  141     {
  142         MMDB_entry_data_s entry_data;
  143         const char *lookup_path[] = {"array", "-3", NULL};
  144         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  145         test_array_minus_3_result(status, entry_data, "MMDB_aget_value");
  146 
  147         status =
  148             MMDB_get_value(&result.entry, &entry_data, "array", "-3", NULL);
  149         test_array_minus_3_result(status, entry_data, "MMDB_get_value");
  150 
  151         status =
  152             call_vget_value(&result.entry, &entry_data, "array", "-3", NULL);
  153         test_array_minus_3_result(status, entry_data, "MMDB_vget_value");
  154     }
  155 
  156     {
  157         MMDB_entry_data_s entry_data;
  158         int status =
  159             MMDB_get_value(&result.entry, &entry_data, "array", "-4", NULL);
  160         cmp_ok(status,
  161                "==",
  162                MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
  163                "MMDB_get_value() returns error on too large negative integer");
  164     }
  165 
  166     {
  167         MMDB_entry_data_s entry_data;
  168         int status = MMDB_get_value(
  169             &result.entry, &entry_data, "array", "-18446744073709551616", NULL);
  170         cmp_ok(
  171             status,
  172             "==",
  173             MMDB_INVALID_LOOKUP_PATH_ERROR,
  174             "MMDB_get_value() returns error on integer smaller than LONG_MIN");
  175     }
  176 
  177     {
  178         MMDB_entry_data_s entry_data;
  179         int status = MMDB_get_value(
  180             &result.entry, &entry_data, "array", "18446744073709551616", NULL);
  181         cmp_ok(
  182             status,
  183             "==",
  184             MMDB_INVALID_LOOKUP_PATH_ERROR,
  185             "MMDB_get_value() returns error on integer larger than LONG_MAX");
  186     }
  187 
  188     MMDB_close(mmdb);
  189     free(mmdb);
  190 }
  191 
  192 void test_complex_map_a_result(int status,
  193                                MMDB_entry_data_s entry_data,
  194                                char *function) {
  195     cmp_ok(status,
  196            "==",
  197            MMDB_SUCCESS,
  198            "status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{a}",
  199            function);
  200     ok(entry_data.has_data, "found a value for map1{map2}{array}[0]{map3}{a}");
  201     cmp_ok(entry_data.type,
  202            "==",
  203            MMDB_DATA_TYPE_UINT32,
  204            "returned entry type is uint32 - map1{map2}{array}[0]{map3}{a}");
  205     cmp_ok(entry_data.uint32,
  206            "==",
  207            1,
  208            "entry value is 1 - map1{map2}{array}[0]{map3}{a}");
  209 }
  210 
  211 void test_complex_map_c_result(int status,
  212                                MMDB_entry_data_s entry_data,
  213                                char *function) {
  214     cmp_ok(status,
  215            "==",
  216            MMDB_SUCCESS,
  217            "status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{c}",
  218            function);
  219     ok(entry_data.has_data, "found a value for map1{map2}{array}[0]{map3}{c}");
  220     cmp_ok(entry_data.type,
  221            "==",
  222            MMDB_DATA_TYPE_UINT32,
  223            "returned entry type is uint32 - map1{map2}{array}[0]{map3}{c}");
  224     cmp_ok(entry_data.uint32,
  225            "==",
  226            3,
  227            "entry value is 3 - map1{map2}{array}[0]{map3}{c}");
  228 }
  229 
  230 void test_no_result(int status,
  231                     MMDB_entry_data_s entry_data,
  232                     char *function,
  233                     char *path_description) {
  234     cmp_ok(status,
  235            "==",
  236            MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR,
  237            "status for %s() is MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR - %s",
  238            function,
  239            path_description);
  240     ok(!entry_data.has_data, "did not find a value for %s", path_description);
  241 }
  242 
  243 void test_nested_structure(int mode, const char *mode_desc) {
  244     const char *filename = "MaxMind-DB-test-nested.mmdb";
  245     const char *path = test_database_path(filename);
  246     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
  247     free((void *)path);
  248 
  249     const char *ip = "1.1.1.1";
  250     MMDB_lookup_result_s result =
  251         lookup_string_ok(mmdb, ip, filename, mode_desc);
  252 
  253     {
  254         MMDB_entry_data_s entry_data;
  255         const char *lookup_path[] = {
  256             "map1", "map2", "array", "0", "map3", "a", NULL};
  257         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  258         test_complex_map_a_result(status, entry_data, "MMDB_aget_value");
  259 
  260         status = MMDB_get_value(&result.entry,
  261                                 &entry_data,
  262                                 "map1",
  263                                 "map2",
  264                                 "array",
  265                                 "0",
  266                                 "map3",
  267                                 "a",
  268                                 NULL);
  269         test_complex_map_a_result(status, entry_data, "MMDB_get_value");
  270 
  271         status = call_vget_value(&result.entry,
  272                                  &entry_data,
  273                                  "map1",
  274                                  "map2",
  275                                  "array",
  276                                  "0",
  277                                  "map3",
  278                                  "a",
  279                                  NULL);
  280         test_complex_map_a_result(status, entry_data, "MMDB_vget_value");
  281     }
  282 
  283     {
  284         MMDB_entry_data_s entry_data;
  285         const char *lookup_path[] = {
  286             "map1", "map2", "array", "0", "map3", "c", NULL};
  287         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  288         test_complex_map_c_result(status, entry_data, "MMDB_aget_value");
  289 
  290         status = MMDB_get_value(&result.entry,
  291                                 &entry_data,
  292                                 "map1",
  293                                 "map2",
  294                                 "array",
  295                                 "0",
  296                                 "map3",
  297                                 "c",
  298                                 NULL);
  299         test_complex_map_c_result(status, entry_data, "MMDB_get_value");
  300 
  301         status = call_vget_value(&result.entry,
  302                                  &entry_data,
  303                                  "map1",
  304                                  "map2",
  305                                  "array",
  306                                  "0",
  307                                  "map3",
  308                                  "c",
  309                                  NULL);
  310         test_complex_map_c_result(status, entry_data, "MMDB_vget_value");
  311     }
  312 
  313     {
  314         MMDB_entry_data_s entry_data;
  315         const char *lookup_path[] = {
  316             "map1", "map42", "array", "0", "map3", "c", NULL};
  317         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  318         test_no_result(status,
  319                        entry_data,
  320                        "MMDB_aget_value",
  321                        "map1{map42}{array}[0]{map3}{c}");
  322 
  323         status = MMDB_get_value(&result.entry,
  324                                 &entry_data,
  325                                 "map1",
  326                                 "map42",
  327                                 "array",
  328                                 "0",
  329                                 "map3",
  330                                 "c",
  331                                 NULL);
  332         test_no_result(status,
  333                        entry_data,
  334                        "MMDB_get_value",
  335                        "map1{map42}{array}[0]{map3}{c}");
  336 
  337         status = call_vget_value(&result.entry,
  338                                  &entry_data,
  339                                  "map1",
  340                                  "map42",
  341                                  "array",
  342                                  "0",
  343                                  "map3",
  344                                  "c",
  345                                  NULL);
  346         test_no_result(status,
  347                        entry_data,
  348                        "MMDB_vget_value",
  349                        "map1{map42}{array}[0]{map3}{c}");
  350     }
  351 
  352     {
  353         MMDB_entry_data_s entry_data;
  354         const char *lookup_path[] = {
  355             "map1", "map2", "array", "9", "map3", "c", NULL};
  356         int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);
  357         test_no_result(status,
  358                        entry_data,
  359                        "MMDB_aget_value",
  360                        "map1{map42}{array}[9]{map3}{c}");
  361 
  362         status = MMDB_get_value(&result.entry,
  363                                 &entry_data,
  364                                 "map1",
  365                                 "map2",
  366                                 "array",
  367                                 "9",
  368                                 "map3",
  369                                 "c",
  370                                 NULL);
  371         test_no_result(status,
  372                        entry_data,
  373                        "MMDB_get_value",
  374                        "map1{map42}{array}[9]{map3}{c}");
  375 
  376         status = call_vget_value(&result.entry,
  377                                  &entry_data,
  378                                  "map1",
  379                                  "map2",
  380                                  "array",
  381                                  "9",
  382                                  "map3",
  383                                  "c",
  384                                  NULL);
  385         test_no_result(status,
  386                        entry_data,
  387                        "MMDB_vget_value",
  388                        "map1{map42}{array}[9]{map3}{c}");
  389     }
  390 
  391     MMDB_close(mmdb);
  392     free(mmdb);
  393 }
  394 
  395 void run_tests(int mode, const char *mode_desc) {
  396     test_simple_structure(mode, mode_desc);
  397     test_nested_structure(mode, mode_desc);
  398 }
  399 
  400 int main(void) {
  401     plan(NO_PLAN);
  402     for_all_modes(&run_tests);
  403     done_testing();
  404 }