LCOV - code coverage report
Current view: top level - tests - tst_tld.c (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 79 133 59.4 %
Date: 2015-03-02 11:46:03 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-2015 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             :   {
      67             :     "fr",
      68             :     "2.0",
      69             :     0,
      70             :     NULL
      71             :   };
      72             : 
      73             : /* Main array */
      74             : const Tld_table * my_tld_tables[] =
      75             :   {
      76             :     &_tld_fr_override,
      77             :     NULL
      78             :   };
      79             : 
      80             : void
      81           1 : doit (void)
      82             : {
      83             :   size_t i;
      84             :   const Tld_table *tldtable;
      85             :   char *out;
      86             :   size_t errpos;
      87             :   int rc;
      88             : 
      89           1 :   tldtable = tld_get_table (NULL, NULL);
      90           1 :   if (tldtable != NULL)
      91           0 :     fail ("FAIL: tld_get_table (NULL, NULL) != NULL\n");
      92             : 
      93           1 :   tldtable = tld_get_table ("nonexisting", NULL);
      94           1 :   if (tldtable != NULL)
      95           0 :     fail ("FAIL: tld_get_table (\"nonexisting\", NULL) != NULL\n");
      96             : 
      97           1 :   tldtable = tld_default_table (NULL, NULL);
      98           1 :   if (tldtable != NULL)
      99           0 :     fail ("FAIL: tld_default_table (NULL, NULL) != NULL\n");
     100             : 
     101           1 :   tldtable = tld_default_table (NULL, NULL);
     102           1 :   if (tldtable != NULL)
     103           0 :     fail ("FAIL: tld_default_table (NULL, NULL) != NULL\n");
     104             : 
     105           1 :   tldtable = tld_default_table ("fr", NULL);
     106           1 :   if (tldtable == NULL)
     107           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL) == NULL\n");
     108           1 :   else if (tldtable->version == NULL)
     109           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version == NULL\n");
     110           1 :   else if (tldtable->name && strcmp (tldtable->version, "1.0") != 0)
     111           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version = \"%s\""
     112             :           " != \"1.0\"\n", tldtable->version);
     113             : 
     114           1 :   tldtable = tld_default_table ("fr", my_tld_tables);
     115           1 :   if (tldtable == NULL)
     116           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL) == NULL\n");
     117           1 :   else if (tldtable->version == NULL)
     118           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version == NULL\n");
     119           1 :   else if (tldtable->name && strcmp (tldtable->version, "2.0") != 0)
     120           0 :     fail ("FAIL: tld_default_table (\"fr\", NULL)->version = \"%s\""
     121             :           " != \"2.0\"\n", tldtable->version);
     122             : 
     123           1 :   rc = tld_get_4 (NULL, 42, &out);
     124           1 :   if (rc != TLD_NODATA)
     125           0 :     fail ("FAIL: tld_get_4 (NULL, 42, &out) != TLD_NODATA: %d\n", rc);
     126             : 
     127           1 :   rc = tld_get_4 (tld[0].in, 0, &out);
     128           1 :   if (rc != TLD_NODATA)
     129           0 :     fail ("FAIL: tld_get_4 (NULL, 42, &out) != TLD_NODATA: %d\n", rc);
     130             : 
     131           1 :   rc = tld_check_4t (tld[0].in, tld[0].inlen, NULL, NULL);
     132           1 :   if (rc != TLD_SUCCESS)
     133           0 :     fail ("FAIL: tld_check_4t (tld=NULL) != TLD_SUCCESS: %d\n", rc);
     134             : 
     135           1 :   rc = tld_check_4z (NULL, NULL, NULL);
     136           1 :   if (rc != TLD_NODATA)
     137           0 :     fail ("FAIL: tld_check_4z (NULL) != TLD_NODATA: %d\n", rc);
     138             : 
     139           1 :   rc = tld_check_4z (tld[0].in, NULL, NULL);
     140           1 :   if (rc != TLD_SUCCESS)
     141           0 :     fail ("FAIL: tld_check_4z (in) != TLD_SUCCESS: %d\n", rc);
     142             : 
     143           1 :   rc = tld_check_8z (NULL, NULL, NULL);
     144           1 :   if (rc != TLD_NODATA)
     145           0 :     fail ("FAIL: tld_check_8z (NULL) != TLD_NODATA: %d\n", rc);
     146             : 
     147           1 :   rc = tld_check_lz (NULL, NULL, NULL);
     148           1 :   if (rc != TLD_NODATA)
     149           0 :     fail ("FAIL: tld_check_lz (NULL) != TLD_NODATA: %d\n", rc);
     150             : 
     151           1 :   rc = tld_check_lz ("foo", NULL, NULL);
     152           1 :   if (rc != TLD_SUCCESS)
     153           0 :     fail ("FAIL: tld_check_lz (\"foo\") != TLD_SUCCESS: %d\n", rc);
     154             : 
     155             :   {
     156           1 :     uint32_t in[] = { 0x73, 0x6a, 0x64, 0x2e, 0x73, 0x65, 0x00 };
     157             :     const char *p;
     158             : 
     159           1 :     rc = tld_get_4 (in, 6, &out);
     160           1 :     if (rc != TLD_SUCCESS)
     161           0 :       fail ("FAIL: tld_get_4 (in, 6, &out) != TLD_OK: %d\n", rc);
     162           1 :     if (strcmp ("se", out) != 0)
     163           0 :       fail ("FAIL: tld_get_4 (in, 6, &out): %s\n", out);
     164           1 :     idn_free (out);
     165             : 
     166           1 :     rc = tld_get_4z (in, &out);
     167           1 :     if (rc != TLD_SUCCESS)
     168           0 :       fail ("FAIL: tld_get_4z (in, &out) != TLD_OK: %d\n", rc);
     169           1 :     if (strcmp ("se", out) != 0)
     170           0 :       fail ("FAIL: tld_get_4z (in, &out): %s\n", out);
     171           1 :     idn_free (out);
     172             : 
     173           1 :     p = "sjd.se";
     174           1 :     rc = tld_get_z (p, &out);
     175           1 :     if (rc != TLD_SUCCESS)
     176           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     177           1 :     if (strcmp ("se", out) != 0)
     178           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     179           1 :     idn_free (out);
     180             : 
     181           1 :     p = "foo.bar.baz.sjd.se";
     182           1 :     rc = tld_get_z (p, &out);
     183           1 :     if (rc != TLD_SUCCESS)
     184           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     185           1 :     if (strcmp ("se", out) != 0)
     186           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     187           1 :     idn_free (out);
     188             : 
     189           1 :     p = ".sjd.se";
     190           1 :     rc = tld_get_z (p, &out);
     191           1 :     if (rc != TLD_SUCCESS)
     192           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     193           1 :     if (strcmp ("se", out) != 0)
     194           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     195           1 :     idn_free (out);
     196             : 
     197           1 :     p = ".se";
     198           1 :     rc = tld_get_z (p, &out);
     199           1 :     if (rc != TLD_SUCCESS)
     200           0 :       fail ("FAIL: tld_get_z (\"%s\", &out) != TLD_OK: %d\n", p, rc);
     201           1 :     if (strcmp ("se", out) != 0)
     202           0 :       fail ("FAIL: tld_get_z (\"%s\", &out): %s\n", p, out);
     203           1 :     idn_free (out);
     204             :   }
     205             : 
     206           3 :   for (i = 0; i < sizeof (tld) / sizeof (tld[0]); i++)
     207             :     {
     208           2 :       if (debug)
     209           0 :         printf ("TLD entry %ld: %s\n", i, tld[i].name);
     210             : 
     211           2 :       if (debug)
     212             :         {
     213           0 :           printf ("in:\n");
     214           0 :           ucs4print (tld[i].in, tld[i].inlen);
     215             :         }
     216             : 
     217           2 :       tldtable = tld_default_table (tld[i].tld, NULL);
     218           2 :       if (tldtable == NULL)
     219             :         {
     220           0 :           fail ("TLD entry %ld tld_get_table (%s)\n", i, tld[i].tld);
     221           0 :           if (debug)
     222           0 :             printf ("FATAL\n");
     223           0 :           continue;
     224             :         }
     225             : 
     226           2 :       rc = tld_check_4t (tld[i].in, tld[i].inlen, &errpos, tldtable);
     227           2 :       if (rc != tld[i].rc)
     228             :         {
     229           0 :           fail ("TLD entry %ld failed: %d\n", i, rc);
     230           0 :           if (debug)
     231           0 :             printf ("FATAL\n");
     232           0 :           continue;
     233             :         }
     234             : 
     235           2 :       if (debug)
     236           0 :         printf ("returned %d expected %d\n", rc, tld[i].rc);
     237             : 
     238           2 :       if (rc != tld[i].rc)
     239             :         {
     240           0 :           fail ("TLD entry %ld failed\n", i);
     241           0 :           if (debug)
     242           0 :             printf ("ERROR\n");
     243             :         }
     244           2 :       else if (rc == TLD_INVALID)
     245             :         {
     246           1 :           if (debug)
     247           0 :             printf ("returned errpos %ld expected errpos %ld\n",
     248             :                     errpos, tld[i].errpos);
     249             : 
     250           1 :           if (tld[i].errpos != errpos)
     251             :             {
     252           0 :               fail ("TLD entry %ld failed because errpos %ld != %ld\n", i,
     253             :                     tld[i].errpos, errpos);
     254           0 :               if (debug)
     255           0 :                 printf ("ERROR\n");
     256             :             }
     257             :         }
     258           1 :       else if (debug)
     259           0 :         printf ("OK\n");
     260             : 
     261             :       {
     262           2 :         rc = tld_check_8z (tld[i].example, &errpos, NULL);
     263           2 :         if (rc != tld[i].rc)
     264             :           {
     265           0 :             fail ("TLD entry %ld failed\n", i);
     266           0 :             if (debug)
     267           0 :               printf ("ERROR\n");
     268             :           }
     269           2 :         if (debug)
     270           0 :           printf ("TLD entry %ld tld_check_8z (%s)\n", i, tld[i].example);
     271             :       }
     272             :     }
     273           1 : }

Generated by: LCOV version 1.11