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: 2015-11-01 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-2015  Made to Order Software Corp.
       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\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)\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             :                 { 7159, 8536, "uk", 2, 8430 },
     137             : 
     138             :                 /* get each offset of the .uk 2nd level domain */
     139             :                 { 6960, 6985, "ac", 2,                                                        6960 },
     140             :                 { 6960, 6985, "bl", 2,                                                        6961 },
     141             :                 { 6960, 6985, "british-library", 15,                  6962 },
     142             :                 { 6960, 6985, "co", 2,                                                        6963 },
     143             :                 { 6960, 6985, "gov", 3,                                                       6964 },
     144             :                 { 6960, 6985, "govt", 4,                                              6965 },
     145             :                 { 6960, 6985, "icnet", 5,                                             6966 },
     146             :                 { 6960, 6985, "jet", 3,                                                       6967 },
     147             :                 { 6960, 6985, "lea", 3,                                                       6968 },
     148             :                 { 6960, 6985, "ltd", 3,                                                       6969 },
     149             :                 { 6960, 6985, "me", 2,                                                        6970 },
     150             :                 { 6960, 6985, "mil", 3,                                                       6971 },
     151             :                 { 6960, 6985, "mod", 3,                                                       6972 },
     152             :                 { 6960, 6985, "national-library-scotland", 25,        6973 },
     153             :                 { 6960, 6985, "nel", 3,                                                       6974 },
     154             :                 { 6960, 6985, "net", 3,                                                       6975 },
     155             :                 { 6960, 6985, "nhs", 3,                                                       6976 },
     156             :                 { 6960, 6985, "nic", 3,                                                       6977 },
     157             :                 { 6960, 6985, "nls", 3,                                                       6978 },
     158             :                 { 6960, 6985, "org", 3,                                                       6979 },
     159             :                 { 6960, 6985, "orgn", 4,                                              6980 },
     160             :                 { 6960, 6985, "parliament", 10,                                       6981 },
     161             :                 { 6960, 6985, "plc", 3,                                                       6982 },
     162             :                 { 6960, 6985, "police", 6,                                            6983 },
     163             :                 { 6960, 6985, "sch", 3,                                                       6984 },
     164             : 
     165             :                 /* test with a few invalid TLDs for .uk */
     166             :                 { 6960, 6985, "com", 3, -1 },
     167             :                 { 6960, 6985, "aca", 3, -1 },
     168             :                 { 6960, 6985, "aac", 3, -1 },
     169             :                 { 6960, 6985, "ca", 2, -1 },
     170             :                 { 6960, 6985, "cn", 2, -1 },
     171             :                 { 6960, 6985, "cp", 2, -1 },
     172             :                 { 6960, 6985, "cz", 2, -1 },
     173             : 
     174             :                 /* get the .vu offset */
     175             :                 { 7159, 8536, "vu", 2, 8471 },
     176             : 
     177             :                 /* get the 2nd level .vu offsets */
     178             :                 { 7099, 7104, "edu", 3, 7100 },
     179             :                 { 7099, 7104, "gov", 3, 7101 },
     180             :                 { 7099, 7104, "net", 3, 7102 },
     181             : 
     182             :                 /* test with a few .vu 2nd level domains that do not exist */
     183             :                 { 7099, 7104, "nom", 3, -1 },
     184             :                 { 7099, 7104, "sch", 3, -1 },
     185             : 
     186             :                 /* verify ordering of mari, mari-el, and marine (from .ru) */
     187             :                 { 6419, 6556, "mari",    4, 6482 },
     188             :                 { 6419, 6556, "mari-el", 7, 6483 },
     189             :                 { 6419, 6556, "marine",  6, 6484 },
     190             :         };
     191             : 
     192             :         size_t i;
     193             : 
     194           1 :         size_t const max = sizeof(d) / sizeof(d[0]);
     195          43 :         for(i = 0; i < max; ++i)
     196             :         {
     197          42 :                 int const r = search(d[i].f_start, d[i].f_end, d[i].f_tld, d[i].f_length);
     198          42 :                 if(r != d[i].f_result)
     199             :                 {
     200           0 :                         fprintf(stderr, "error: test_search() failed with \"%s\", expected %d and got %d\n",
     201             :                                         d[i].f_tld, d[i].f_result, r);
     202           0 :                         ++err_count;
     203             :                 }
     204             :         }
     205           1 : }
     206             : 
     207             : 
     208         475 : void test_search_array(int start, int end)
     209             : {
     210             :         int             i, r;
     211             : 
     212             :         /* now test all from the arrays */
     213        9011 :         for(i = start; i < end; ++i)
     214             :         {
     215        8536 :                 if(verbose)
     216             :                 {
     217           0 :                         printf("{%d..%d} i = %d, [%s]\n", start, end, i, tld_descriptions[i].f_tld);
     218             :                 }
     219        8536 :                 r = search(start, end, tld_descriptions[i].f_tld, strlen(tld_descriptions[i].f_tld));
     220        8536 :                 if(r != i)
     221             :                 {
     222           0 :                         fprintf(stderr, "error: test_search_array() failed with \"%s\", expected %d and got %d\n",
     223             :                                         tld_descriptions[i].f_tld, i, r);
     224           0 :                         ++err_count;
     225             :                 }
     226        8536 :                 if(tld_descriptions[i].f_start_offset != USHRT_MAX)
     227             :                 {
     228         474 :                         test_search_array(tld_descriptions[i].f_start_offset,
     229         474 :                                                           tld_descriptions[i].f_end_offset);
     230             :                 }
     231             :         }
     232         475 : }
     233             : 
     234           1 : void test_search_all()
     235             : {
     236           1 :         test_search_array(tld_start_offset, tld_end_offset);
     237           1 : }
     238             : 
     239             : 
     240           1 : int main(int argc, char *argv[])
     241             : {
     242           1 :         fprintf(stderr, "testing internal tld version %s\n", tld_version());
     243             : 
     244           1 :         if(argc > 1)
     245             :         {
     246           0 :                 if(strcmp(argv[1], "-v") == 0)
     247             :                 {
     248           0 :                         verbose = 1;
     249             :                 }
     250             :         }
     251             : 
     252             :         /* call all the tests, one by one
     253             :          * failures are "recorded" in the err_count global variable
     254             :          * and the process stops with an error message and exit(1)
     255             :          * if err_count is not zero.
     256             :          */
     257           1 :         test_compare();
     258           1 :         test_search();
     259           1 :         test_search_all();
     260             : 
     261           1 :         if(err_count)
     262             :         {
     263           0 :                 fprintf(stderr, "%d error%s occured.\n",
     264           0 :                                         err_count, err_count != 1 ? "s" : "");
     265             :         }
     266           1 :         exit(err_count ? 1 : 0);
     267             : }
     268             : 
     269             : /* vim: ts=4 sw=4
     270             :  */

Generated by: LCOV version 1.10