LCOV - code coverage report
Current view: top level - tests - tst_tld.c (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 79 135 58.5 %
Date: 2020-07-22 17:53:13 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* tst_tld.c --- Self tests for tld_*().
       2             :  * Copyright (C) 2004-2020 Simon Josefsson
       3             :  *
       4             :  * This file is part of GNU Libidn.
       5             :  *
       6             :  * This program is free software: you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  *
      19             :  */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : # include "config.h"
      23             : #endif
      24             : 
      25             : #include <stdio.h>
      26             : #include <stdlib.h>
      27             : #include <stdarg.h>
      28             : #include <string.h>
      29             : 
      30             : #include <stringprep.h>
      31             : #include <idn-free.h>
      32             : #include <tld.h>
      33             : 
      34             : #include "utils.h"
      35             : 
      36             : struct tld
      37             : {
      38             :   const char *name;
      39             :   const char *tld;
      40             :   const char *example;
      41             :   size_t inlen;
      42             :   uint32_t in[100];
      43             :   int rc;
      44             :   size_t errpos;
      45             : };
      46             : 
      47             : static const struct tld tld[] = {
      48             :   {
      49             :    "Simple valid French domain",
      50             :    "fr",
      51             :    "example.fr",
      52             :    3,
      53             :    {0x00E0, 0x00E2, 0x00E6},
      54             :    TLD_SUCCESS},
      55             :   {
      56             :    "Simple invalid French domain",
      57             :    "fr",
      58             :    "ÂȘexample.fr",
      59             :    5,
      60             :    {0x00E0, 0x00E2, 0x00E6, 0x4711, 0x0042},
      61             :    TLD_INVALID,
      62             :    3}
      63             : };
      64             : 
      65             : static const Tld_table _tld_fr_override = {
      66             :   "fr",
      67             :   "2.0",
      68             :   0,
      69             :   NULL
      70             : };
      71             : 
      72             : /* Main array */
      73             : const Tld_table *my_tld_tables[] = {
      74             :   &_tld_fr_override,
      75             :   NULL
      76             : };
      77             : 
      78             : void
      79           1 : doit (void)
      80             : {
      81             :   unsigned i;
      82             :   const Tld_table *tldtable;
      83             :   char *out;
      84             :   size_t errpos;
      85             :   int rc;
      86             : 
      87           1 :   tldtable = tld_get_table (NULL, NULL);
      88           1 :   if (tldtable != NULL)
      89           0 :     fail ("FAIL: tld_get_table (NULL, NULL) != NULL\n");
      90             : 
      91           1 :   tldtable = tld_get_table ("nonexisting", NULL);
      92           1 :   if (tldtable != NULL)
      93           0 :     fail ("FAIL: tld_get_table (\"nonexisting\", NULL) != NULL\n");
      94             : 
      95           1 :   tldtable = tld_default_table (NULL, NULL);
      96           1 :   if (tldtable != NULL)
      97           0 :     fail ("FAIL: tld_default_table (NULL, NULL) != NULL\n");
      98             : 
      99           1 :   tldtable = tld_default_table (NULL, NULL);
     100           1 :   if (tldtable != NULL)
     101           0 :     fail ("FAIL: tld_default_table (NULL, NULL) != NULL\n");
     102             : 
     103           1 :   tldtable = tld_default_table ("fr", NULL);
     104           1 :   if (tldtable == NULL)
     105           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL) == NULL\n");
     106           1 :   else if (tldtable->version == NULL)
     107           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version == NULL\n");
     108           1 :   else if (tldtable->name && strcmp (tldtable->version, "1.0") != 0)
     109           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version = \"%s\""
     110             :           " != \"1.0\"\n", tldtable->version);
     111             : 
     112           1 :   tldtable = tld_default_table ("fr", my_tld_tables);
     113           1 :   if (tldtable == NULL)
     114           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL) == NULL\n");
     115           1 :   else if (tldtable->version == NULL)
     116           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version == NULL\n");
     117           1 :   else if (tldtable->name && strcmp (tldtable->version, "2.0") != 0)
     118           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version = \"%s\""
     119             :           " != \"2.0\"\n", tldtable->version);
     120             : 
     121           1 :   rc = tld_get_4 (NULL, 42, &out);
     122           1 :   if (rc != TLD_NODATA)
     123           0 :     fail ("FAIL: tld_get_4 (NULL, 42, &out) != TLD_NODATA: %d\n", rc);
     124             : 
     125           1 :   rc = tld_get_4 (tld[0].in, 0, &out);
     126           1 :   if (rc != TLD_NODATA)
     127           0 :     fail ("FAIL: tld_get_4 (NULL, 42, &out) != TLD_NODATA: %d\n", rc);
     128             : 
     129           1 :   rc = tld_check_4t (tld[0].in, tld[0].inlen, NULL, NULL);
     130           1 :   if (rc != TLD_SUCCESS)
     131           0 :     fail ("FAIL: tld_check_4t (tld=NULL) != TLD_SUCCESS: %d\n", rc);
     132             : 
     133           1 :   rc = tld_check_4z (NULL, NULL, NULL);
     134           1 :   if (rc != TLD_NODATA)
     135           0 :     fail ("FAIL: tld_check_4z (NULL) != TLD_NODATA: %d\n", rc);
     136             : 
     137           1 :   rc = tld_check_4z (tld[0].in, NULL, NULL);
     138           1 :   if (rc != TLD_SUCCESS)
     139           0 :     fail ("FAIL: tld_check_4z (in) != TLD_SUCCESS: %d\n", rc);
     140             : 
     141           1 :   rc = tld_check_8z (NULL, NULL, NULL);
     142           1 :   if (rc != TLD_NODATA)
     143           0 :     fail ("FAIL: tld_check_8z (NULL) != TLD_NODATA: %d\n", rc);
     144             : 
     145           1 :   rc = tld_check_lz (NULL, NULL, NULL);
     146           1 :   if (rc != TLD_NODATA)
     147           0 :     fail ("FAIL: tld_check_lz (NULL) != TLD_NODATA: %d\n", rc);
     148             : 
     149           1 :   rc = tld_check_lz ("foo", NULL, NULL);
     150           1 :   if (rc != TLD_SUCCESS)
     151           0 :     fail ("FAIL: tld_check_lz (\"foo\") != TLD_SUCCESS: %d\n", rc);
     152             : 
     153             :   {
     154           1 :     uint32_t in[] = { 0x73, 0x6a, 0x64, 0x2e, 0x73, 0x65, 0x00 };
     155             :     const char *p;
     156             : 
     157           1 :     rc = tld_get_4 (in, 6, &out);
     158           1 :     if (rc != TLD_SUCCESS)
     159           0 :       fail ("FAIL: tld_get_4 (in, 6, &out) != TLD_OK: %d\n", rc);
     160           1 :     if (strcmp ("se", out) != 0)
     161           0 :       fail ("FAIL: tld_get_4 (in, 6, &out): %s\n", out);
     162           1 :     idn_free (out);
     163             : 
     164           1 :     rc = tld_get_4z (in, &out);
     165           1 :     if (rc != TLD_SUCCESS)
     166           0 :       fail ("FAIL: tld_get_4z (in, &out) != TLD_OK: %d\n", rc);
     167           1 :     if (strcmp ("se", out) != 0)
     168           0 :       fail ("FAIL: tld_get_4z (in, &out): %s\n", out);
     169           1 :     idn_free (out);
     170             : 
     171           1 :     p = "sjd.se";
     172           1 :     rc = tld_get_z (p, &out);
     173           1 :     if (rc != TLD_SUCCESS)
     174           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     175           1 :     if (strcmp ("se", out) != 0)
     176           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     177           1 :     idn_free (out);
     178             : 
     179           1 :     p = "foo.bar.baz.sjd.se";
     180           1 :     rc = tld_get_z (p, &out);
     181           1 :     if (rc != TLD_SUCCESS)
     182           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     183           1 :     if (strcmp ("se", out) != 0)
     184           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     185           1 :     idn_free (out);
     186             : 
     187           1 :     p = ".sjd.se";
     188           1 :     rc = tld_get_z (p, &out);
     189           1 :     if (rc != TLD_SUCCESS)
     190           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     191           1 :     if (strcmp ("se", out) != 0)
     192           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     193           1 :     idn_free (out);
     194             : 
     195           1 :     p = ".se";
     196           1 :     rc = tld_get_z (p, &out);
     197           1 :     if (rc != TLD_SUCCESS)
     198           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     199           1 :     if (strcmp ("se", out) != 0)
     200           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     201           1 :     idn_free (out);
     202             :   }
     203             : 
     204           3 :   for (i = 0; i < sizeof (tld) / sizeof (tld[0]); i++)
     205             :     {
     206           2 :       if (debug)
     207           0 :         printf ("TLD entry %u: %s\n", i, tld[i].name);
     208             : 
     209           2 :       if (debug)
     210             :         {
     211           0 :           printf ("in:\n");
     212           0 :           ucs4print (tld[i].in, tld[i].inlen);
     213             :         }
     214             : 
     215           2 :       tldtable = tld_default_table (tld[i].tld, NULL);
     216           2 :       if (tldtable == NULL)
     217             :         {
     218           0 :           fail ("TLD entry %u tld_get_table (%s)\n", i, tld[i].tld);
     219           0 :           if (debug)
     220           0 :             printf ("FATAL\n");
     221           0 :           continue;
     222             :         }
     223             : 
     224           2 :       rc = tld_check_4t (tld[i].in, tld[i].inlen, &errpos, tldtable);
     225           2 :       if (rc != tld[i].rc)
     226             :         {
     227           0 :           fail ("TLD entry %u failed: %d\n", i, rc);
     228           0 :           if (debug)
     229           0 :             printf ("FATAL\n");
     230           0 :           continue;
     231             :         }
     232             : 
     233           2 :       if (debug)
     234           0 :         printf ("returned %d expected %d\n", rc, tld[i].rc);
     235             : 
     236           2 :       if (rc != tld[i].rc)
     237             :         {
     238           0 :           fail ("TLD entry %u failed\n", i);
     239           0 :           if (debug)
     240           0 :             printf ("ERROR\n");
     241             :         }
     242           2 :       else if (rc == TLD_INVALID)
     243             :         {
     244           1 :           if (debug)
     245           0 :             printf ("returned errpos %ld expected errpos %ld\n",
     246           0 :                     (long) errpos, (long) tld[i].errpos);
     247             : 
     248           1 :           if (tld[i].errpos != errpos)
     249             :             {
     250           0 :               fail ("TLD entry %u failed because errpos %ld != %ld\n", i,
     251           0 :                     (long) tld[i].errpos, (long) errpos);
     252           0 :               if (debug)
     253           0 :                 printf ("ERROR\n");
     254             :             }
     255             :         }
     256           1 :       else if (debug)
     257           0 :         printf ("OK\n");
     258             : 
     259             :       {
     260           2 :         rc = tld_check_8z (tld[i].example, &errpos, NULL);
     261           2 :         if (rc != tld[i].rc)
     262             :           {
     263           0 :             fail ("TLD entry %u failed\n", i);
     264           0 :             if (debug)
     265           0 :               printf ("ERROR\n");
     266             :           }
     267           2 :         if (debug)
     268           0 :           printf ("TLD entry %u tld_check_8z (%s)\n", i, tld[i].example);
     269             :       }
     270             :     }
     271           1 : }

Generated by: LCOV version 1.13