LCOV - code coverage report
Current view: top level - tests - tld_internal_test.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 56 76.8 %
Date: 2021-05-08 12:27:55 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* TLD library -- Test the TLD library by including the tld.c file.
       2             :  * Copyright (c) 2011-2021  Made to Order Software Corp.  All Rights Reserved
       3             :  *
       4             :  * Permission is hereby granted, free of charge, to any person obtaining a
       5             :  * copy of this software and associated documentation files (the
       6             :  * "Software"), to deal in the Software without restriction, including
       7             :  * without limitation the rights to use, copy, modify, merge, publish,
       8             :  * distribute, sublicense, and/or sell copies of the Software, and to
       9             :  * permit persons to whom the Software is furnished to do so, subject to
      10             :  * the following conditions:
      11             :  *
      12             :  * The above copyright notice and this permission notice shall be included
      13             :  * in all copies or substantial portions of the Software.
      14             :  *
      15             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      16             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      17             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      18             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      19             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      20             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      21             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      22             :  */
      23             : 
      24             : /** \file
      25             :  * \brief Test the tld.c, tld_data.c, and tld_domain_to_lowercase.c functions.
      26             :  *
      27             :  * This file implements various tests that can directly access the internal
      28             :  * functions of the tld.c, tld_data.c, and tld_domain_to_lowercase.c
      29             :  * files.
      30             :  *
      31             :  * For that purpose we directly include those files in this test. This
      32             :  * is why the test is not actually linked against the library, it
      33             :  * includes it within itself.
      34             :  */
      35             : 
      36             : #include "tld.c"
      37             : #include "tld_data.c"
      38             : #include "tld_domain_to_lowercase.c"
      39             : 
      40             : #include <stdlib.h>
      41             : #include <string.h>
      42             : 
      43             : int err_count = 0;
      44             : int verbose = 0;
      45             : 
      46           1 : void test_compare()
      47             : {
      48             :         struct data
      49             :         {
      50             :                 const char *a;
      51             :                 const char *b;
      52             :                 int n;
      53             :                 int r;
      54             :         };
      55           1 :         struct data d[] = {
      56             :                 { "uj", "uk", 2, -1 },
      57             :                 { "uk", "uk", 2,  0 },
      58             :                 { "ul", "uk", 2,  1 },
      59             : 
      60             :                 { "uj", "ukmore",  2, -1 },
      61             :                 { "uk", "ukstuff", 2,  0 },
      62             :                 { "ul", "ukhere",  2,  1 },
      63             : 
      64             :                 { "uk1", "ukmore",  2, 1 },
      65             :                 { "uk2", "ukstuff", 2, 1 },
      66             :                 { "uk3", "ukhere",  2, 1 },
      67             : 
      68             :                 { "uk1", "uk.", 3, 1 },
      69             :                 { "uk2", "uk.", 3, 1 },
      70             :                 { "uk3", "uk.", 3, 1 },
      71             : 
      72             :                 { "uk1", ".uk", 3, 1 },
      73             :                 { "uk2", ".uk", 3, 1 },
      74             :                 { "uk3", ".uk", 3, 1 },
      75             : 
      76             :                 { "uk", "uk1",   3, -1 },
      77             :                 { "uk", "uk22",  4, -1 },
      78             :                 { "uk", "uk333", 5, -1 },
      79             : 
      80             :                 { "uk1",   "uk", 2, 1 },
      81             :                 { "uk22",  "uk", 2, 1 },
      82             :                 { "uk333", "uk", 2, 1 },
      83             :         };
      84             :         int i, r, max;
      85             :         char *s, *vd, *u;
      86             : 
      87           1 :         max = sizeof(d) / sizeof(d[0]);
      88          22 :         for(i = 0; i < max; ++i)
      89             :         {
      90          21 :                 r = cmp(d[i].a, d[i].b, d[i].n);
      91          21 :                 if(r != d[i].r) {
      92           0 :                         fprintf(stderr, "error: cmp() failed with \"%s\" / \"%s\", expected %d and got %d [1]\n",
      93             :                                         d[i].a, d[i].b, d[i].r, r);
      94           0 :                         ++err_count;
      95             :                 }
      96             : 
      97             :                 // create a version with uppercase and try again
      98          21 :                 s = strdup(d[i].b);
      99         101 :                 for(u = s; *u != '\0'; ++u)
     100             :                 {
     101          80 :                         if(*u >= 'a' && *u <= 'z')
     102             :                         {
     103          68 :                                 *u &= 0x5F;
     104             :                         }
     105             :                 }
     106          21 :                 vd = tld_domain_to_lowercase(s);
     107          21 :                 r = cmp(d[i].a, d[i].b, d[i].n);
     108          21 :                 if(r != d[i].r) {
     109           0 :                         fprintf(stderr, "error: cmp() failed with \"%s\" / \"%s\", expected %d and got %d (with domain to lowercase) [2]\n",
     110             :                                         d[i].a, d[i].b, d[i].r, r);
     111           0 :                         ++err_count;
     112             :                 }
     113          21 :                 free(vd);
     114          21 :                 free(s);
     115             :         }
     116           1 : }
     117             : 
     118           1 : void test_search()
     119             : {
     120             :         struct search_info
     121             :         {
     122             :                 int                             f_start;
     123             :                 int                             f_end;
     124             :                 const char *    f_tld;
     125             :                 int                             f_length;
     126             :                 int                             f_result;
     127             :         };
     128           1 :         struct search_info d[] = {
     129             :                 /*
     130             :                  * This table is very annoying since each time the data changes
     131             :                  * it gets out of sync. On the other hand that's the best way
     132             :                  * to make sure our tests work like in the real world.
     133             :                  */
     134             : 
     135             :                 /* get the .uk offset */
     136             :                 { 8842, 10466, "uk", 2, 10348 },
     137             : 
     138             :                 /* get each offset of the .uk 2nd level domain */
     139             :                 { 8595, 8625, "ac", 2,                                                        8595 },
     140             :                 { 8595, 8625, "barsy", 5,                                             8596 },
     141             :                 { 8595, 8625, "bl", 2,                                                        8597 },
     142             :                 { 8595, 8625, "british-library", 15,                  8598 },
     143             :                 { 8595, 8625, "co", 2,                                                        8599 },
     144             :                 { 8595, 8625, "conn", 4,                                              8600 },
     145             :                 { 8595, 8625, "copro", 5,                                             8601 },
     146             :                 { 8595, 8625, "gov", 3,                                                       8602 },
     147             :                 { 8595, 8625, "govt", 4,                                              8603 },
     148             :                 { 8595, 8625, "hosp", 4,                                              8604 },
     149             :                 { 8595, 8625, "icnet", 5,                                             8605 },
     150             :                 { 8595, 8625, "jet", 3,                                                       8606 },
     151             :                 { 8595, 8625, "lea", 3,                                                       8607 },
     152             :                 { 8595, 8625, "ltd", 3,                                                       8608 },
     153             :                 { 8595, 8625, "me", 2,                                                        8609 },
     154             :                 { 8595, 8625, "mil", 3,                                                       8610 },
     155             :                 { 8595, 8625, "mod", 3,                                                       8611 },
     156             :                 { 8595, 8625, "national-library-scotland", 25,        8612 },
     157             :                 { 8595, 8625, "nel", 3,                                                       8613 },
     158             :                 { 8595, 8625, "net", 3,                                                       8614 },
     159             :                 { 8595, 8625, "nhs", 3,                                                       8615 },
     160             :                 { 8595, 8625, "nic", 3,                                                       8616 },
     161             :                 { 8595, 8625, "nls", 3,                                                       8617 },
     162             :                 { 8595, 8625, "org", 3,                                                       8618 },
     163             :                 { 8595, 8625, "orgn", 4,                                              8619 },
     164             :                 { 8595, 8625, "parliament", 10,                                       8620 },
     165             :                 { 8595, 8625, "plc", 3,                                                       8621 },
     166             :                 { 8595, 8625, "police", 6,                                            8622 },
     167             :                 { 8595, 8625, "pymnt", 5,                                             8623 },
     168             :                 { 8595, 8625, "sch", 3,                                                       8624 },
     169             : 
     170             :                 /* test with a few invalid TLDs for .uk */
     171             :                 { 8595, 8625, "com", 3, -1 },
     172             :                 { 8595, 8625, "aca", 3, -1 },
     173             :                 { 8595, 8625, "aac", 3, -1 },
     174             :                 { 8595, 8625, "ca", 2, -1 },
     175             :                 { 8595, 8625, "cn", 2, -1 },
     176             :                 { 8595, 8625, "cp", 2, -1 },
     177             :                 { 8595, 8625, "cz", 2, -1 },
     178             :                 { 8595, 8625, "school", 2, -1 },
     179             : 
     180             :                 /* get the .vu offset */
     181             :                 { 8842, 10466, "vu", 2, 10395 },
     182             : 
     183             :                 /* get the 2nd level .vu offsets */
     184             :                 { 8755, 8764, "blog", 4, 8755 },
     185             :                 { 8755, 8764, "cn", 2,   8756 },
     186             :                 { 8755, 8764, "com", 3,  8757 },
     187             :                 { 8755, 8764, "dev", 3,  8758 },
     188             :                 { 8755, 8764, "edu", 3,  8759 },
     189             :                 { 8755, 8764, "gov", 3,  8760 },
     190             :                 { 8755, 8764, "me", 2,   8761 },
     191             :                 { 8755, 8764, "net", 3,  8762 },
     192             :                 { 8755, 8764, "org", 3,  8763 },
     193             : 
     194             :                 /* test with a few .vu 2nd level domains that do not exist */
     195             :                 { 8755, 8764, "nom", 3, -1 },
     196             :                 { 8755, 8764, "sch", 3, -1 },
     197             :                 { 8755, 8764, "zero", 4, -1 },
     198             : 
     199             :                 /* verify ordering of mari, mari-el, and marine (from .ru) */
     200             :                 { 7928, 8077, "mari",    4, 7994 },
     201             :                 { 7928, 8077, "mari-el", 7, 7995 },
     202             :                 { 7928, 8077, "marine",  6, 7996 },
     203             :         };
     204             : 
     205             :         size_t i;
     206             : 
     207           1 :         size_t const max = sizeof(d) / sizeof(d[0]);
     208          56 :         for(i = 0; i < max; ++i)
     209             :         {
     210          55 :                 int const r = search(d[i].f_start, d[i].f_end, d[i].f_tld, d[i].f_length);
     211          55 :                 if(r != d[i].f_result)
     212             :                 {
     213           0 :                         fprintf(stderr, "error: test_search() failed with \"%s\", expected %d and got %d [3]\n",
     214             :                                         d[i].f_tld, d[i].f_result, r);
     215           0 :                         ++err_count;
     216             :                 }
     217             :         }
     218           1 : }
     219             : 
     220             : 
     221         796 : void test_search_array(int start, int end)
     222             : {
     223             :         int             i, r;
     224             : 
     225             :         /* now test all from the arrays */
     226       11262 :         for(i = start; i < end; ++i)
     227             :         {
     228       10466 :                 if(verbose)
     229             :                 {
     230           0 :                         printf("{%d..%d} i = %d, [%s]\n", start, end, i, tld_descriptions[i].f_tld);
     231             :                 }
     232       10466 :                 r = search(start, end, tld_descriptions[i].f_tld, strlen(tld_descriptions[i].f_tld));
     233       10466 :                 if(r != i)
     234             :                 {
     235           0 :                         fprintf(stderr, "error: test_search_array() failed with \"%s\", expected %d and got %d [4]\n",
     236             :                                         tld_descriptions[i].f_tld, i, r);
     237           0 :                         ++err_count;
     238             :                 }
     239       10466 :                 if(tld_descriptions[i].f_start_offset != USHRT_MAX)
     240             :                 {
     241         795 :                         test_search_array(tld_descriptions[i].f_start_offset,
     242         795 :                                                           tld_descriptions[i].f_end_offset);
     243             :                 }
     244             :         }
     245         796 : }
     246             : 
     247           1 : void test_search_all()
     248             : {
     249           1 :         test_search_array(tld_start_offset, tld_end_offset);
     250           1 : }
     251             : 
     252             : 
     253           1 : int main(int argc, char *argv[])
     254             : {
     255           1 :         fprintf(stderr, "testing internal tld version %s\n", tld_version());
     256             : 
     257           1 :         if(argc > 1)
     258             :         {
     259           0 :                 if(strcmp(argv[1], "-v") == 0)
     260             :                 {
     261           0 :                         verbose = 1;
     262             :                 }
     263             :         }
     264             : 
     265             :         /* call all the tests, one by one
     266             :          * failures are "recorded" in the err_count global variable
     267             :          * and the process stops with an error message and exit(1)
     268             :          * if err_count is not zero.
     269             :          */
     270           1 :         test_compare();
     271           1 :         test_search();
     272           1 :         test_search_all();
     273             : 
     274           1 :         if(err_count)
     275             :         {
     276           0 :                 fprintf(stderr, "%d error%s occured.\n",
     277           0 :                                         err_count, err_count != 1 ? "s" : "");
     278             :         }
     279           1 :         exit(err_count ? 1 : 0);
     280             : }
     281             : 
     282             : /* vim: ts=4 sw=4
     283             :  */

Generated by: LCOV version 1.13