LCOV - code coverage report
Current view: top level - tests - tst_stringprep.c (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 26 64 40.6 %
Date: 2015-03-02 11:46:03 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* tst_stringprep.c --- Self tests for stringprep().
       2             :  * Copyright (C) 2002-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             : 
      32             : #include "utils.h"
      33             : 
      34             : struct stringprep
      35             : {
      36             :   const char *comment;
      37             :   const char *in;
      38             :   const char *out;
      39             :   const char *profile;
      40             :   int flags;
      41             :   int rc;
      42             : };
      43             : 
      44             : const struct stringprep strprep[] = {
      45             :   {"Map to nothing",
      46             :    "foo\xC2\xAD\xCD\x8F\xE1\xA0\x86\xE1\xA0\x8B"
      47             :    "bar" "\xE2\x80\x8B\xE2\x81\xA0" "baz\xEF\xB8\x80\xEF\xB8\x88"
      48             :    "\xEF\xB8\x8F\xEF\xBB\xBF", "foobarbaz"},
      49             :   {"Case folding ASCII U+0043 U+0041 U+0046 U+0045", "CAFE", "cafe"},
      50             :   {"Case folding 8bit U+00DF (german sharp s)", "\xC3\x9F", "ss"},
      51             :   {"Case folding U+0130 (turkish capital I with dot)",
      52             :    "\xC4\xB0", "i\xcc\x87"},
      53             :   {"Case folding multibyte U+0143 U+037A",
      54             :    "\xC5\x83\xCD\xBA", "\xC5\x84 \xCE\xB9"},
      55             :   {"Case folding U+2121 U+33C6 U+1D7BB",
      56             :    "\xE2\x84\xA1\xE3\x8F\x86\xF0\x9D\x9E\xBB",
      57             :    "telc\xE2\x88\x95" "kg\xCF\x83"},
      58             :   {"Normalization of U+006a U+030c U+00A0 U+00AA",
      59             :    "\x6A\xCC\x8C\xC2\xA0\xC2\xAA", "\xC7\xB0 a"},
      60             :   {"Case folding U+1FB7 and normalization",
      61             :    "\xE1\xBE\xB7", "\xE1\xBE\xB6\xCE\xB9"},
      62             :   {"Self-reverting case folding U+01F0 and normalization",
      63             :    "\xC7\xB0", "\xC7\xB0"},
      64             :   {"Self-reverting case folding U+0390 and normalization",
      65             :    "\xCE\x90", "\xCE\x90"},
      66             :   {"Self-reverting case folding U+03B0 and normalization",
      67             :    "\xCE\xB0", "\xCE\xB0"},
      68             :   {"Self-reverting case folding U+1E96 and normalization",
      69             :    "\xE1\xBA\x96", "\xE1\xBA\x96"},
      70             :   {"Self-reverting case folding U+1F56 and normalization",
      71             :    "\xE1\xBD\x96", "\xE1\xBD\x96"},
      72             :   {"ASCII space character U+0020", "\x20", "\x20"},
      73             :   {"Non-ASCII 8bit space character U+00A0", "\xC2\xA0", "\x20"},
      74             :   {"Non-ASCII multibyte space character U+1680",
      75             :    "\xE1\x9A\x80", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
      76             :   {"Non-ASCII multibyte space character U+2000", "\xE2\x80\x80", "\x20"},
      77             :   {"Zero Width Space U+200b", "\xE2\x80\x8b", ""},
      78             :   {"Non-ASCII multibyte space character U+3000", "\xE3\x80\x80", "\x20"},
      79             :   {"ASCII control characters U+0010 U+007F", "\x10\x7F", "\x10\x7F"},
      80             :   {"Non-ASCII 8bit control character U+0085",
      81             :    "\xC2\x85", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
      82             :   {"Non-ASCII multibyte control character U+180E",
      83             :    "\xE1\xA0\x8E", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
      84             :   {"Zero Width No-Break Space U+FEFF", "\xEF\xBB\xBF", ""},
      85             :   {"Non-ASCII control character U+1D175",
      86             :    "\xF0\x9D\x85\xB5", NULL, "Nameprep", 0,
      87             :    STRINGPREP_CONTAINS_PROHIBITED},
      88             :   {"Plane 0 private use character U+F123",
      89             :    "\xEF\x84\xA3", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
      90             :   {"Plane 15 private use character U+F1234",
      91             :    "\xF3\xB1\x88\xB4", NULL, "Nameprep", 0,
      92             :    STRINGPREP_CONTAINS_PROHIBITED},
      93             :   {"Plane 16 private use character U+10F234",
      94             :    "\xF4\x8F\x88\xB4", NULL, "Nameprep", 0,
      95             :    STRINGPREP_CONTAINS_PROHIBITED},
      96             :   {"Non-character code point U+8FFFE",
      97             :    "\xF2\x8F\xBF\xBE", NULL, "Nameprep", 0,
      98             :    STRINGPREP_CONTAINS_PROHIBITED},
      99             :   {"Non-character code point U+10FFFF",
     100             :    "\xF4\x8F\xBF\xBF", NULL, "Nameprep", 0,
     101             :    STRINGPREP_CONTAINS_PROHIBITED},
     102             :   {"Surrogate code U+DF42",
     103             :    "\xED\xBD\x82", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     104             :   {"Non-plain text character U+FFFD",
     105             :    "\xEF\xBF\xBD", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     106             :   {"Ideographic description character U+2FF5",
     107             :    "\xE2\xBF\xB5", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     108             :   {"Display property character U+0341", "\xCD\x81", "\xCC\x81"},
     109             :   {"Left-to-right mark U+200E",
     110             :    "\xE2\x80\x8E", "\xCC\x81", "Nameprep", 0,
     111             :    STRINGPREP_CONTAINS_PROHIBITED},
     112             :   {"Deprecated U+202A", "\xE2\x80\xAA", "\xCC\x81", "Nameprep", 0,
     113             :    STRINGPREP_CONTAINS_PROHIBITED},
     114             :   {"Language tagging character U+E0001",
     115             :    "\xF3\xA0\x80\x81", "\xCC\x81", "Nameprep", 0,
     116             :    STRINGPREP_CONTAINS_PROHIBITED},
     117             :   {"Language tagging character U+E0042",
     118             :    "\xF3\xA0\x81\x82", NULL, "Nameprep", 0,
     119             :    STRINGPREP_CONTAINS_PROHIBITED},
     120             :   {"Bidi: RandALCat character U+05BE and LCat characters",
     121             :    "foo\xD6\xBE" "bar", NULL, "Nameprep", 0,
     122             :    STRINGPREP_BIDI_BOTH_L_AND_RAL},
     123             :   {"Bidi: RandALCat character U+FD50 and LCat characters",
     124             :    "foo\xEF\xB5\x90" "bar", NULL, "Nameprep", 0,
     125             :    STRINGPREP_BIDI_BOTH_L_AND_RAL},
     126             :   {"Bidi: RandALCat character U+FB38 and LCat characters",
     127             :    "foo\xEF\xB9\xB6" "bar", "foo \xd9\x8e" "bar"},
     128             :   {"Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
     129             :    "\xD8\xA7\x31", NULL, "Nameprep", 0, STRINGPREP_BIDI_LEADTRAIL_NOT_RAL},
     130             :   {"Bidi: RandALCat character U+0627 U+0031 U+0628",
     131             :    "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8"},
     132             :   {"Unassigned code point U+E0002",
     133             :    "\xF3\xA0\x80\x82", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
     134             :    STRINGPREP_CONTAINS_UNASSIGNED},
     135             :   {"Larger test (shrinking)",
     136             :    "X\xC2\xAD\xC3\x9F\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
     137             :    "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87" "tel\xc7\xb0 a\xce\xb0 ",
     138             :    "Nameprep"},
     139             :   {"Larger test (expanding)",
     140             :    "X\xC3\x9F\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
     141             :    "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
     142             :    "\xe3\x83\xab" "i\xcc\x87" "tel\x28" "d\x29\xe3\x82\xa2\xe3\x83\x91"
     143             :    "\xe3\x83\xbc\xe3\x83\x88"},
     144             :   {"Test of prohibited ASCII character U+0020",
     145             :    "\x20", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     146             :   {"Test of NFKC U+00A0 and prohibited character U+0020",
     147             :    "\xC2\xA0", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     148             :   {"Case map + normalization", "\xC2\xB5", "\xCE\xBC", "Nameprep"},
     149             :   /* The rest are rather non-interesting, but no point in removing
     150             :      working test cases... */
     151             :   {"case_nonfkc", "\xC2\xB5", "\xCE\xBC", "Nameprep", STRINGPREP_NO_NFKC,
     152             :    STRINGPREP_FLAG_ERROR},
     153             :   {"NFKC test", "\xC2\xAA", "\x61", "Nameprep"},
     154             :   {"nameprep, exposed a bug in libstringprep 0.0.5",
     155             :    "\xC2\xAA\x0A", "\x61\x0A"},
     156             :   {"unassigned code point U+0221", "\xC8\xA1", "\xC8\xA1", "Nameprep"},
     157             :   {"Unassigned code point U+0221",
     158             :    "\xC8\xA1", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
     159             :    STRINGPREP_CONTAINS_UNASSIGNED},
     160             :   {"Unassigned code point U+0236", "\xC8\xB6", "\xC8\xB6", "Nameprep"},
     161             :   {"unassigned code point U+0236",
     162             :    "\xC8\xB6", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
     163             :    STRINGPREP_CONTAINS_UNASSIGNED},
     164             :   {"bidi both RandALCat and LCat  U+0627 U+00AA U+0628",
     165             :    "\xD8\xA7\xC2\xAA\xD8\xA8", NULL, "Nameprep", 0,
     166             :    STRINGPREP_BIDI_BOTH_L_AND_RAL},
     167             :   /* XMPP */
     168             :   {"XMPP node profile prohibited output",
     169             :    "foo@bar", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
     170             :   {"XMPP resource profile on same string should work though",
     171             :    "foo@bar", "foo@bar", "Resourceprep"},
     172             :   /* iSCSI */
     173             :   {"iSCSI 1", "Example-Name", "example-name", "iSCSI"},
     174             :   {"iSCSI 2", "O+o", NULL, "iSCSI", 0, STRINGPREP_CONTAINS_PROHIBITED},
     175             :   {"iSCSI 3", "\x01", NULL, "iSCSI", 0, STRINGPREP_CONTAINS_PROHIBITED},
     176             :   {"iSCSI 4", "\xE3\x80\x82", NULL, "iSCSI", 0,
     177             :    STRINGPREP_CONTAINS_PROHIBITED},
     178             :   {"iSCSI 5", "\xE2\xBF\xB5", NULL, "iSCSI", 0,
     179             :    STRINGPREP_CONTAINS_PROHIBITED},
     180             :   {"SASL profile", "Example\xC2\xA0" "Name", "Example Name", "SASLprep"},
     181             :   /* SASL trace */
     182             :   {"SASL ANONYMOUS plain mechanism", "simon@josefsson.org",
     183             :    "simon@josefsson.org", "plain"},
     184             :   {"SASLprep 1 old", "x\xC2\xADy", "xy", "SASLprep"},
     185             :   {"SASLprep 4 old", "\xE2\x85\xA3", "IV", "SASLprep"},
     186             :   /* SASLprep test vectors. */
     187             :   {"SASLprep 1 SOFT HYPHEN mapped to nothing", "I\xC2\xADX", "IX",
     188             :    "SASLprep"},
     189             :   {"SASLprep 2 no transformation", "user", "user", "SASLprep"},
     190             :   {"SASLprep 3 case preserved, will not match #2", "USER", "USER",
     191             :    "SASLprep"},
     192             :   {"SASLprep 4 output is NFKC, input in ISO 8859-1", "\xC2\xAA", "a",
     193             :    "SASLprep"},
     194             :   {"SASLprep 5 output is NFKC, will match #1", "\xE2\x85\xA8", "IX",
     195             :    "SASLprep"},
     196             :   {"SASLprep 6 Error - prohibited character", "\x07", NULL, "SASLprep",
     197             :    0, STRINGPREP_CONTAINS_PROHIBITED},
     198             :   {"SASLprep 7 Error - bidirectional check", "\xD8\xA7" "1", NULL, "SASLprep",
     199             :    0, STRINGPREP_BIDI_LEADTRAIL_NOT_RAL}
     200             : };
     201             : 
     202             : void
     203           1 : doit (void)
     204             : {
     205             :   char *p;
     206             :   int rc;
     207             :   size_t i;
     208             : 
     209           1 :   if (!stringprep_check_version (STRINGPREP_VERSION))
     210           0 :     fail ("stringprep_check_version failed (header %s runtime %s)\n",
     211             :           STRINGPREP_VERSION, stringprep_check_version (NULL));
     212             : 
     213           1 :   if (!stringprep_check_version (NULL))
     214           0 :     fail ("stringprep_check_version(NULL) failed\n");
     215             : 
     216           1 :   if (strcmp (stringprep_check_version (NULL), STRINGPREP_VERSION) != 0)
     217           0 :     fail ("stringprep_check_version failure (header %s runtime %s)\n",
     218             :           STRINGPREP_VERSION, stringprep_check_version (NULL));
     219             : 
     220           1 :   if (stringprep_check_version ("100.100"))
     221           0 :     fail ("stringprep_check_version(\"100.100\") failed\n");
     222             : 
     223          75 :   for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
     224             :     {
     225          74 :       if (debug)
     226           0 :         printf ("STRINGPREP entry %ld\n", i);
     227             : 
     228          74 :       if (debug)
     229             :         {
     230           0 :           printf ("flags: %d\n", strprep[i].flags);
     231             : 
     232           0 :           printf ("in: ");
     233           0 :           escapeprint (strprep[i].in, strlen (strprep[i].in));
     234           0 :           hexprint (strprep[i].in, strlen (strprep[i].in));
     235           0 :           binprint (strprep[i].in, strlen (strprep[i].in));
     236             :         }
     237             : 
     238             :       {
     239             :         uint32_t *l;
     240             :         char *x;
     241          74 :         l = stringprep_utf8_to_ucs4 (strprep[i].in, -1, NULL);
     242          74 :         x = stringprep_ucs4_to_utf8 (l, -1, NULL, NULL);
     243          74 :         free (l);
     244             : 
     245          74 :         if (strcmp (strprep[i].in, x) != 0)
     246             :           {
     247           0 :             fail ("bad UTF-8 in entry %ld\n", i);
     248           0 :             if (debug)
     249             :               {
     250           0 :                 puts ("expected:");
     251           0 :                 escapeprint (strprep[i].in, strlen (strprep[i].in));
     252           0 :                 hexprint (strprep[i].in, strlen (strprep[i].in));
     253           0 :                 puts ("computed:");
     254           0 :                 escapeprint (x, strlen (x));
     255           0 :                 hexprint (x, strlen (x));
     256             :               }
     257             :           }
     258             : 
     259          74 :         free (x);
     260             :       }
     261         148 :       rc = stringprep_profile (strprep[i].in, &p,
     262          74 :                                strprep[i].profile ?
     263             :                                strprep[i].profile :
     264          74 :                                "Nameprep", strprep[i].flags);
     265          74 :       if (rc != strprep[i].rc)
     266             :         {
     267           0 :           fail ("stringprep() entry %ld failed: %d\n", i, rc);
     268           0 :           if (debug)
     269           0 :             printf ("FATAL\n");
     270           0 :           if (rc == STRINGPREP_OK)
     271           0 :             free (p);
     272           0 :           continue;
     273             :         }
     274             : 
     275          74 :       if (debug && rc == STRINGPREP_OK)
     276             :         {
     277           0 :           printf ("out: ");
     278           0 :           escapeprint (p, strlen (p));
     279           0 :           hexprint (p, strlen (p));
     280           0 :           binprint (p, strlen (p));
     281             : 
     282           0 :           printf ("expected out: ");
     283           0 :           escapeprint (strprep[i].out, strlen (strprep[i].out));
     284           0 :           hexprint (strprep[i].out, strlen (strprep[i].out));
     285           0 :           binprint (strprep[i].out, strlen (strprep[i].out));
     286             :         }
     287          74 :       else if (debug)
     288           0 :         printf ("returned %d expected %d\n", rc, strprep[i].rc);
     289             : 
     290          74 :       if (rc == STRINGPREP_OK)
     291             :         {
     292          82 :           if (strlen (strprep[i].out) != strlen (p) ||
     293          41 :               memcmp (strprep[i].out, p, strlen (p)) != 0)
     294             :             {
     295           0 :               fail ("stringprep() entry %ld failed\n", i);
     296           0 :               if (debug)
     297           0 :                 printf ("ERROR\n");
     298             :             }
     299          41 :           else if (debug)
     300           0 :             printf ("OK\n\n");
     301             : 
     302          41 :           free (p);
     303             :         }
     304          33 :       else if (debug)
     305           0 :         printf ("OK\n\n");
     306             :     }
     307             : 
     308             : #if 0
     309             :   {
     310             :     char p[20];
     311             :     memset (p, 0, 10);
     312             :     stringprep_unichar_to_utf8 (0x00DF, p);
     313             :     hexprint (p, strlen (p));
     314             :     puts ("");
     315             :   }
     316             : #endif
     317           1 : }

Generated by: LCOV version 1.11