"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 
    3 MMDB_entry_data_list_s *
    4 test_array_value(MMDB_entry_data_list_s *entry_data_list) {
    5     MMDB_entry_data_list_s *array = entry_data_list = entry_data_list->next;
    6     cmp_ok(array->entry_data.type,
    7            "==",
    8            MMDB_DATA_TYPE_ARRAY,
    9            "'array' key's value is an array");
   10     cmp_ok(array->entry_data.data_size,
   11            "==",
   12            3,
   13            "'array' key's value has 3 elements");
   14 
   15     MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
   16     cmp_ok(idx0->entry_data.type,
   17            "==",
   18            MMDB_DATA_TYPE_UINT32,
   19            "first array entry is a UINT32");
   20     cmp_ok(idx0->entry_data.uint32, "==", 1, "first array entry value is 1");
   21 
   22     MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
   23     cmp_ok(idx1->entry_data.type,
   24            "==",
   25            MMDB_DATA_TYPE_UINT32,
   26            "second array entry is a UINT32");
   27     cmp_ok(idx1->entry_data.uint32, "==", 2, "second array entry value is 2");
   28 
   29     MMDB_entry_data_list_s *idx2 = entry_data_list = entry_data_list->next;
   30     cmp_ok(idx2->entry_data.type,
   31            "==",
   32            MMDB_DATA_TYPE_UINT32,
   33            "third array entry is a UINT32");
   34     cmp_ok(idx2->entry_data.uint32, "==", 3, "third array entry value is 3");
   35 
   36     return entry_data_list;
   37 }
   38 
   39 MMDB_entry_data_list_s *
   40 test_boolean_value(MMDB_entry_data_list_s *entry_data_list) {
   41     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
   42 
   43     cmp_ok(value->entry_data.type,
   44            "==",
   45            MMDB_DATA_TYPE_BOOLEAN,
   46            "'boolean' key's value is a boolean");
   47     ok(value->entry_data.boolean, "'boolean' key's value is true");
   48 
   49     return entry_data_list;
   50 }
   51 
   52 MMDB_entry_data_list_s *
   53 test_bytes_value(MMDB_entry_data_list_s *entry_data_list) {
   54     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
   55 
   56     cmp_ok(value->entry_data.type,
   57            "==",
   58            MMDB_DATA_TYPE_BYTES,
   59            "'bytes' key's value is bytes");
   60     uint8_t *bytes = malloc(value->entry_data.data_size);
   61     if (NULL == bytes) {
   62         BAIL_OUT("malloc failed");
   63     }
   64     memcpy(bytes, value->entry_data.bytes, value->entry_data.data_size);
   65     uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a};
   66 
   67     ok(memcmp(bytes, expect, 4) == 0, "got expected value for bytes key");
   68 
   69     free((void *)bytes);
   70 
   71     return entry_data_list;
   72 }
   73 
   74 MMDB_entry_data_list_s *
   75 test_double_value(MMDB_entry_data_list_s *entry_data_list) {
   76     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
   77 
   78     cmp_ok(value->entry_data.type,
   79            "==",
   80            MMDB_DATA_TYPE_DOUBLE,
   81            "'double' key's value is a double");
   82 
   83     compare_double(value->entry_data.double_value, 42.123456);
   84 
   85     return entry_data_list;
   86 }
   87 
   88 MMDB_entry_data_list_s *
   89 test_float_value(MMDB_entry_data_list_s *entry_data_list) {
   90     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
   91 
   92     cmp_ok(value->entry_data.type,
   93            "==",
   94            MMDB_DATA_TYPE_FLOAT,
   95            "'float' key's value is a float");
   96 
   97     compare_float(value->entry_data.float_value, 1.1F);
   98 
   99     return entry_data_list;
  100 }
  101 
  102 MMDB_entry_data_list_s *
  103 test_int32_value(MMDB_entry_data_list_s *entry_data_list) {
  104     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  105 
  106     cmp_ok(value->entry_data.type,
  107            "==",
  108            MMDB_DATA_TYPE_INT32,
  109            "'int32' key's value is an int32");
  110 
  111     int32_t expect = 1 << 28;
  112     expect *= -1;
  113     cmp_ok(value->entry_data.int32,
  114            "==",
  115            expect,
  116            "got expected value for int32 key");
  117 
  118     return entry_data_list;
  119 }
  120 
  121 MMDB_entry_data_list_s *
  122 test_arrayX_value(MMDB_entry_data_list_s *entry_data_list) {
  123     MMDB_entry_data_list_s *arrayX = entry_data_list = entry_data_list->next;
  124     cmp_ok(arrayX->entry_data.type,
  125            "==",
  126            MMDB_DATA_TYPE_ARRAY,
  127            "'map{mapX}{arrayX}' key's value is an array");
  128     cmp_ok(arrayX->entry_data.data_size,
  129            "==",
  130            3,
  131            "'map{mapX}{arrayX}' key's value has 3 elements");
  132 
  133     MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
  134     cmp_ok(idx0->entry_data.type,
  135            "==",
  136            MMDB_DATA_TYPE_UINT32,
  137            "first array entry is a UINT32");
  138     cmp_ok(idx0->entry_data.uint32, "==", 7, "first array entry value is 7");
  139 
  140     MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
  141     cmp_ok(idx1->entry_data.type,
  142            "==",
  143            MMDB_DATA_TYPE_UINT32,
  144            "second array entry is a UINT32");
  145     cmp_ok(idx1->entry_data.uint32, "==", 8, "second array entry value is 8");
  146 
  147     MMDB_entry_data_list_s *idx2 = entry_data_list = entry_data_list->next;
  148     cmp_ok(idx2->entry_data.type,
  149            "==",
  150            MMDB_DATA_TYPE_UINT32,
  151            "third array entry is a UINT32");
  152     cmp_ok(idx2->entry_data.uint32, "==", 9, "third array entry value is 9");
  153 
  154     return entry_data_list;
  155 }
  156 
  157 MMDB_entry_data_list_s *
  158 test_mapX_key_value_pair(MMDB_entry_data_list_s *entry_data_list) {
  159     MMDB_entry_data_list_s *mapX_key = entry_data_list = entry_data_list->next;
  160     cmp_ok(mapX_key->entry_data.type,
  161            "==",
  162            MMDB_DATA_TYPE_UTF8_STRING,
  163            "found a map key in 'map{mapX}'");
  164     const char *mapX_key_name = dup_entry_string_or_bail(mapX_key->entry_data);
  165 
  166     if (strcmp(mapX_key_name, "utf8_stringX") == 0) {
  167         MMDB_entry_data_list_s *mapX_value = entry_data_list =
  168             entry_data_list->next;
  169         cmp_ok(mapX_value->entry_data.type,
  170                "==",
  171                MMDB_DATA_TYPE_UTF8_STRING,
  172                "'map{mapX}{utf8_stringX}' type is utf8_string");
  173         const char *utf8_stringX_value =
  174             dup_entry_string_or_bail(mapX_value->entry_data);
  175         ok(strcmp(utf8_stringX_value, "hello") == 0,
  176            "map{mapX}{utf8_stringX} value is 'hello'");
  177         free((void *)utf8_stringX_value);
  178     } else if (strcmp(mapX_key_name, "arrayX") == 0) {
  179         entry_data_list = test_arrayX_value(entry_data_list);
  180     } else {
  181         ok(0, "unknown key found in map{mapX} - %s", mapX_key_name);
  182     }
  183 
  184     free((void *)mapX_key_name);
  185 
  186     return entry_data_list;
  187 }
  188 
  189 MMDB_entry_data_list_s *
  190 test_map_value(MMDB_entry_data_list_s *entry_data_list) {
  191     MMDB_entry_data_list_s *map = entry_data_list = entry_data_list->next;
  192     cmp_ok(map->entry_data.type,
  193            "==",
  194            MMDB_DATA_TYPE_MAP,
  195            "'map' key's value is a map");
  196     cmp_ok(map->entry_data.data_size,
  197            "==",
  198            1,
  199            "'map' key's value has 1 key/value pair");
  200 
  201     MMDB_entry_data_list_s *map_key_1 = entry_data_list = entry_data_list->next;
  202     cmp_ok(map_key_1->entry_data.type,
  203            "==",
  204            MMDB_DATA_TYPE_UTF8_STRING,
  205            "found a map key in 'map'");
  206     const char *map_key_1_name =
  207         dup_entry_string_or_bail(map_key_1->entry_data);
  208     ok(strcmp(map_key_1_name, "mapX") == 0, "key name is mapX");
  209     free((void *)map_key_1_name);
  210 
  211     MMDB_entry_data_list_s *mapX = entry_data_list = entry_data_list->next;
  212     cmp_ok(mapX->entry_data.type,
  213            "==",
  214            MMDB_DATA_TYPE_MAP,
  215            "'map{mapX}' key's value is a map");
  216     cmp_ok(mapX->entry_data.data_size,
  217            "==",
  218            2,
  219            "'map' key's value has 2 key/value pairs");
  220 
  221     entry_data_list = test_mapX_key_value_pair(entry_data_list);
  222     entry_data_list = test_mapX_key_value_pair(entry_data_list);
  223 
  224     return entry_data_list;
  225 }
  226 
  227 MMDB_entry_data_list_s *
  228 test_uint128_value(MMDB_entry_data_list_s *entry_data_list) {
  229     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  230 
  231     cmp_ok(value->entry_data.type,
  232            "==",
  233            MMDB_DATA_TYPE_UINT128,
  234            "'uint128' key's value is an uint128");
  235 
  236 #if MMDB_UINT128_IS_BYTE_ARRAY
  237     uint8_t expect[16] = {0x01,
  238                           0x00,
  239                           0x00,
  240                           0x00,
  241                           0x00,
  242                           0x00,
  243                           0x00,
  244                           0x00,
  245                           0x00,
  246                           0x00,
  247                           0x00,
  248                           0x00,
  249                           0x00,
  250                           0x00,
  251                           0x00,
  252                           0x00};
  253     ok(memcmp(value->entry_data.uint128, expect, 16) == 0,
  254        "uint128 field is 2**120");
  255 #else
  256     mmdb_uint128_t expect = 1;
  257     expect <<= 120;
  258     cmp_ok(value->entry_data.uint128, "==", expect, "uint128 field is 2**120");
  259 #endif
  260 
  261     return entry_data_list;
  262 }
  263 
  264 MMDB_entry_data_list_s *
  265 test_uint16_value(MMDB_entry_data_list_s *entry_data_list) {
  266     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  267 
  268     cmp_ok(value->entry_data.type,
  269            "==",
  270            MMDB_DATA_TYPE_UINT16,
  271            "'uint16' key's value is an uint16");
  272     uint16_t expect = 100;
  273     ok(value->entry_data.uint16 == expect, "uint16 field is 100");
  274 
  275     return entry_data_list;
  276 }
  277 
  278 MMDB_entry_data_list_s *
  279 test_uint32_value(MMDB_entry_data_list_s *entry_data_list) {
  280     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  281 
  282     cmp_ok(value->entry_data.type,
  283            "==",
  284            MMDB_DATA_TYPE_UINT32,
  285            "'uint32' key's value is an uint32");
  286     uint32_t expect = 1 << 28;
  287     cmp_ok(value->entry_data.uint32, "==", expect, "uint32 field is 100");
  288 
  289     return entry_data_list;
  290 }
  291 
  292 MMDB_entry_data_list_s *
  293 test_uint64_value(MMDB_entry_data_list_s *entry_data_list) {
  294     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  295 
  296     cmp_ok(value->entry_data.type,
  297            "==",
  298            MMDB_DATA_TYPE_UINT64,
  299            "'uint64' key's value is an uint64");
  300     uint64_t expect = 1;
  301     expect <<= 60;
  302     cmp_ok(value->entry_data.uint64, "==", expect, "uint64 field is 2**60");
  303 
  304     return entry_data_list;
  305 }
  306 
  307 MMDB_entry_data_list_s *
  308 test_utf8_string_value(MMDB_entry_data_list_s *entry_data_list) {
  309     MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next;
  310 
  311     cmp_ok(value->entry_data.type,
  312            "==",
  313            MMDB_DATA_TYPE_UTF8_STRING,
  314            "'utf8_string' key's value is a string");
  315     const char *utf8_string = dup_entry_string_or_bail(value->entry_data);
  316     // This is hex for "unicode! ☯ - ♫" as bytes
  317     char expect[19] = {0x75,
  318                        0x6e,
  319                        0x69,
  320                        0x63,
  321                        0x6f,
  322                        0x64,
  323                        0x65,
  324                        0x21,
  325                        0x20,
  326                        0xe2,
  327                        0x98,
  328                        0xaf,
  329                        0x20,
  330                        0x2d,
  331                        0x20,
  332                        0xe2,
  333                        0x99,
  334                        0xab,
  335                        0x00};
  336 
  337     is(utf8_string, expect, "got expected value for utf8_string key");
  338 
  339     free((void *)utf8_string);
  340 
  341     return entry_data_list;
  342 }
  343 
  344 void run_tests(int mode, const char *description) {
  345     const char *filename = "MaxMind-DB-test-decoder.mmdb";
  346     const char *path = test_database_path(filename);
  347     MMDB_s *mmdb = open_ok(path, mode, description);
  348     free((void *)path);
  349 
  350     char *ip = "1.1.1.1";
  351     MMDB_lookup_result_s result =
  352         lookup_string_ok(mmdb, ip, filename, description);
  353 
  354     MMDB_entry_data_list_s *entry_data_list, *first;
  355     int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
  356 
  357     if (MMDB_SUCCESS != status) {
  358         BAIL_OUT("MMDB_get_entry_data_list failed with %s",
  359                  MMDB_strerror(status));
  360     } else {
  361         cmp_ok(
  362             status, "==", MMDB_SUCCESS, "MMDB_get_entry_data_list succeeded");
  363     }
  364 
  365     first = entry_data_list;
  366 
  367     cmp_ok(entry_data_list->entry_data.type,
  368            "==",
  369            MMDB_DATA_TYPE_MAP,
  370            "first entry in entry data list is a map");
  371     cmp_ok(entry_data_list->entry_data.data_size,
  372            "==",
  373            12,
  374            "first map in entry data list has 12 k/v pairs");
  375 
  376     while (1) {
  377         MMDB_entry_data_list_s *key = entry_data_list = entry_data_list->next;
  378 
  379         if (!key) {
  380             break;
  381         }
  382 
  383         cmp_ok(key->entry_data.type,
  384                "==",
  385                MMDB_DATA_TYPE_UTF8_STRING,
  386                "found a map key");
  387 
  388         const char *key_name = dup_entry_string_or_bail(key->entry_data);
  389         if (strcmp(key_name, "array") == 0) {
  390             entry_data_list = test_array_value(entry_data_list);
  391         } else if (strcmp(key_name, "boolean") == 0) {
  392             entry_data_list = test_boolean_value(entry_data_list);
  393         } else if (strcmp(key_name, "bytes") == 0) {
  394             entry_data_list = test_bytes_value(entry_data_list);
  395         } else if (strcmp(key_name, "double") == 0) {
  396             entry_data_list = test_double_value(entry_data_list);
  397         } else if (strcmp(key_name, "float") == 0) {
  398             entry_data_list = test_float_value(entry_data_list);
  399         } else if (strcmp(key_name, "int32") == 0) {
  400             entry_data_list = test_int32_value(entry_data_list);
  401         } else if (strcmp(key_name, "map") == 0) {
  402             entry_data_list = test_map_value(entry_data_list);
  403         } else if (strcmp(key_name, "uint128") == 0) {
  404             entry_data_list = test_uint128_value(entry_data_list);
  405         } else if (strcmp(key_name, "uint16") == 0) {
  406             entry_data_list = test_uint16_value(entry_data_list);
  407         } else if (strcmp(key_name, "uint32") == 0) {
  408             entry_data_list = test_uint32_value(entry_data_list);
  409         } else if (strcmp(key_name, "uint64") == 0) {
  410             entry_data_list = test_uint64_value(entry_data_list);
  411         } else if (strcmp(key_name, "utf8_string") == 0) {
  412             entry_data_list = test_utf8_string_value(entry_data_list);
  413         } else {
  414             ok(0, "unknown key found in map - %s", key_name);
  415         }
  416 
  417         free((void *)key_name);
  418     }
  419 
  420     MMDB_free_entry_data_list(first);
  421 
  422     MMDB_close(mmdb);
  423     free(mmdb);
  424 }
  425 
  426 int main(void) {
  427     plan(NO_PLAN);
  428     for_all_modes(&run_tests);
  429     done_testing();
  430 }