"Fossies" - the Fresh Open Source Software Archive

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

    1 #include "maxminddb_test_helper.h"
    2 
    3 void test_entry_data(MMDB_s *mmdb,
    4                      MMDB_entry_s *entry,
    5                      uint32_t node_number,
    6                      char *node_record) {
    7     MMDB_entry_data_s entry_data;
    8     int status = MMDB_get_value(entry, &entry_data, "ip", NULL);
    9     cmp_ok(status, "==", MMDB_SUCCESS, "successful data lookup for node");
   10     cmp_ok(entry_data.type,
   11            "==",
   12            MMDB_DATA_TYPE_UTF8_STRING,
   13            "returned entry type is UTF8_STRING for %s record of node %i",
   14            node_record,
   15            node_number);
   16 }
   17 
   18 void run_read_node_tests(MMDB_s *mmdb,
   19                          const uint32_t tests[][5],
   20                          int test_count,
   21                          uint8_t record_size) {
   22     for (int i = 0; i < test_count; i++) {
   23         uint32_t node_number = tests[i][0];
   24         MMDB_search_node_s node;
   25         int status = MMDB_read_node(mmdb, node_number, &node);
   26         if (MMDB_SUCCESS == status) {
   27             cmp_ok(node.left_record,
   28                    "==",
   29                    tests[i][1],
   30                    "left record for node %i is %i - %i bit DB",
   31                    node_number,
   32                    tests[i][1],
   33                    record_size);
   34             cmp_ok(node.left_record_type,
   35                    "==",
   36                    tests[i][2],
   37                    "left record type for node %i is %i",
   38                    node_number,
   39                    tests[i][2]);
   40             if (node.left_record_type == MMDB_RECORD_TYPE_DATA) {
   41                 test_entry_data(
   42                     mmdb, &node.left_record_entry, node_number, "left");
   43             }
   44 
   45             cmp_ok(node.right_record,
   46                    "==",
   47                    tests[i][3],
   48                    "right record for node %i is %i - %i bit DB",
   49                    node_number,
   50                    tests[i][3],
   51                    record_size);
   52             cmp_ok(node.right_record_type,
   53                    "==",
   54                    tests[i][4],
   55                    "right record type for node %i is %i",
   56                    node_number,
   57                    tests[i][4]);
   58 
   59             if (node.right_record_type == MMDB_RECORD_TYPE_DATA) {
   60                 test_entry_data(
   61                     mmdb, &node.right_record_entry, node_number, "right");
   62             }
   63         } else {
   64             diag("call to MMDB_read_node for node %i failed - %i bit DB",
   65                  node_number,
   66                  record_size);
   67         }
   68     }
   69 }
   70 
   71 void run_24_bit_record_tests(int mode, const char *mode_desc) {
   72     const char *filename = "MaxMind-DB-test-mixed-24.mmdb";
   73     const char *path = test_database_path(filename);
   74     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
   75     free((void *)path);
   76 
   77     const uint32_t tests[7][5] = {
   78         {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
   79         {
   80             80,
   81             81,
   82             MMDB_RECORD_TYPE_SEARCH_NODE,
   83             197,
   84             MMDB_RECORD_TYPE_SEARCH_NODE,
   85         },
   86         {
   87             96,
   88             97,
   89             MMDB_RECORD_TYPE_SEARCH_NODE,
   90             242,
   91             MMDB_RECORD_TYPE_EMPTY,
   92         },
   93         {
   94             103,
   95             242,
   96             MMDB_RECORD_TYPE_EMPTY,
   97             104,
   98             MMDB_RECORD_TYPE_SEARCH_NODE,
   99         },
  100         {
  101             127,
  102             242,
  103             MMDB_RECORD_TYPE_EMPTY,
  104             315,
  105             MMDB_RECORD_TYPE_DATA,
  106         },
  107         {
  108             132,
  109             329,
  110             MMDB_RECORD_TYPE_DATA,
  111             242,
  112             MMDB_RECORD_TYPE_EMPTY,
  113         },
  114         {
  115             241,
  116             96,
  117             MMDB_RECORD_TYPE_SEARCH_NODE,
  118             242,
  119             MMDB_RECORD_TYPE_EMPTY,
  120         }};
  121     run_read_node_tests(mmdb, tests, 7, 24);
  122 
  123     MMDB_close(mmdb);
  124     free(mmdb);
  125 }
  126 
  127 void run_28_bit_record_tests(int mode, const char *mode_desc) {
  128     const char *filename = "MaxMind-DB-test-mixed-28.mmdb";
  129     const char *path = test_database_path(filename);
  130     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
  131     free((void *)path);
  132 
  133     const uint32_t tests[7][5] = {
  134         {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
  135         {
  136             80,
  137             81,
  138             MMDB_RECORD_TYPE_SEARCH_NODE,
  139             197,
  140             MMDB_RECORD_TYPE_SEARCH_NODE,
  141         },
  142         {
  143             96,
  144             97,
  145             MMDB_RECORD_TYPE_SEARCH_NODE,
  146             242,
  147             MMDB_RECORD_TYPE_EMPTY,
  148         },
  149         {
  150             103,
  151             242,
  152             MMDB_RECORD_TYPE_EMPTY,
  153             104,
  154             MMDB_RECORD_TYPE_SEARCH_NODE,
  155         },
  156         {
  157             127,
  158             242,
  159             MMDB_RECORD_TYPE_EMPTY,
  160             315,
  161             MMDB_RECORD_TYPE_DATA,
  162         },
  163         {
  164             132,
  165             329,
  166             MMDB_RECORD_TYPE_DATA,
  167             242,
  168             MMDB_RECORD_TYPE_EMPTY,
  169         },
  170         {
  171             241,
  172             96,
  173             MMDB_RECORD_TYPE_SEARCH_NODE,
  174             242,
  175             MMDB_RECORD_TYPE_EMPTY,
  176         }};
  177     run_read_node_tests(mmdb, tests, 7, 28);
  178 
  179     MMDB_close(mmdb);
  180     free(mmdb);
  181 }
  182 
  183 void run_32_bit_record_tests(int mode, const char *mode_desc) {
  184     const char *filename = "MaxMind-DB-test-mixed-32.mmdb";
  185     const char *path = test_database_path(filename);
  186     MMDB_s *mmdb = open_ok(path, mode, mode_desc);
  187     free((void *)path);
  188 
  189     const uint32_t tests[7][5] = {
  190         {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY},
  191         {
  192             80,
  193             81,
  194             MMDB_RECORD_TYPE_SEARCH_NODE,
  195             197,
  196             MMDB_RECORD_TYPE_SEARCH_NODE,
  197         },
  198         {
  199             96,
  200             97,
  201             MMDB_RECORD_TYPE_SEARCH_NODE,
  202             242,
  203             MMDB_RECORD_TYPE_EMPTY,
  204         },
  205         {
  206             103,
  207             242,
  208             MMDB_RECORD_TYPE_EMPTY,
  209             104,
  210             MMDB_RECORD_TYPE_SEARCH_NODE,
  211         },
  212         {
  213             127,
  214             242,
  215             MMDB_RECORD_TYPE_EMPTY,
  216             315,
  217             MMDB_RECORD_TYPE_DATA,
  218         },
  219         {
  220             132,
  221             329,
  222             MMDB_RECORD_TYPE_DATA,
  223             242,
  224             MMDB_RECORD_TYPE_EMPTY,
  225         },
  226         {
  227             241,
  228             96,
  229             MMDB_RECORD_TYPE_SEARCH_NODE,
  230             242,
  231             MMDB_RECORD_TYPE_EMPTY,
  232         }};
  233 
  234     run_read_node_tests(mmdb, tests, 7, 32);
  235 
  236     MMDB_close(mmdb);
  237     free(mmdb);
  238 }
  239 
  240 void run_tests(int mode, const char *mode_desc) {
  241     run_24_bit_record_tests(mode, mode_desc);
  242     run_28_bit_record_tests(mode, mode_desc);
  243     run_32_bit_record_tests(mode, mode_desc);
  244 }
  245 
  246 int main(void) {
  247     plan(NO_PLAN);
  248     for_all_modes(&run_tests);
  249     done_testing();
  250 }