Home | History | Annotate | Download | only in intltest
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4  *******************************************************************************
      5  *
      6  *   Copyright (C) 2003-2016, International Business Machines
      7  *   Corporation and others.  All Rights Reserved.
      8  *
      9  *******************************************************************************
     10  *   file name:  testidna.cpp
     11  *   encoding:   UTF-8
     12  *   tab size:   8 (not used)
     13  *   indentation:4
     14  *
     15  *   created on: 2003feb1
     16  *   created by: Ram Viswanadha
     17  */
     18 
     19 #include "unicode/utypes.h"
     20 
     21 #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
     22 
     23 #include <time.h>
     24 #include <limits.h>
     25 #include <stdlib.h>
     26 #include <string.h>
     27 #include "unicode/localpointer.h"
     28 #include "unicode/ustring.h"
     29 #include "unicode/usprep.h"
     30 #include "unicode/uniset.h"
     31 #include "unicode/utf16.h"
     32 #include "cmemory.h"
     33 #include "testidna.h"
     34 #include "idnaref.h"
     35 #include "nptrans.h"
     36 #include "unicode/putil.h"
     37 #include "idnaconf.h"
     38 
     39 static const UChar unicodeIn[][41] ={
     40     {
     41         0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644,
     42         0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F, 0x0000
     43     },
     44     {
     45         0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587,
     46         0x0000
     47     },
     48     {
     49         0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074,
     50         0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D,
     51         0x0065, 0x0073, 0x006B, 0x0079, 0x0000
     52     },
     53     {
     54         0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8,
     55         0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2,
     56         0x05D1, 0x05E8, 0x05D9, 0x05EA, 0x0000
     57     },
     58     {
     59         0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D,
     60         0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939,
     61         0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947,
     62         0x0939, 0x0948, 0x0902, 0x0000
     63     },
     64     {
     65         0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092,
     66         0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B,
     67         0x0000
     68     },
     69 /*
     70     {
     71         0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
     72         0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
     73         0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C, 0x0000
     74     },
     75 */
     76     {
     77         0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E,
     78         0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
     79         0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A,
     80         0x0438, 0x0000
     81     },
     82     {
     83         0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070,
     84         0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070,
     85         0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061,
     86         0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070,
     87         0x0061, 0x00F1, 0x006F, 0x006C, 0x0000
     88     },
     89     {
     90         0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587,
     91         0x0000
     92     },
     93     {
     94         0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B,
     95         0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068,
     96         0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067,
     97         0x0056, 0x0069, 0x1EC7, 0x0074, 0x0000
     98     },
     99     {
    100         0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F, 0x0000
    101     },
    102     {
    103         0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074,
    104         0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D,
    105         0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053, 0x0000
    106     },
    107     {
    108         0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F,
    109         0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D,
    110         0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240, 0x0000
    111     },
    112     {
    113         0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032, 0x0000
    114     },
    115     {
    116         0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059,
    117         0x308B, 0x0035, 0x79D2, 0x524D, 0x0000
    118     },
    119     {
    120         0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0,
    121         0x0000
    122     },
    123     {
    124         0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067, 0x0000
    125     },
    126     // test non-BMP code points
    127     {
    128         0xD800, 0xDF00, 0xD800, 0xDF01, 0xD800, 0xDF02, 0xD800, 0xDF03, 0xD800, 0xDF05,
    129         0xD800, 0xDF06, 0xD800, 0xDF07, 0xD800, 0xDF09, 0xD800, 0xDF0A, 0xD800, 0xDF0B,
    130         0x0000
    131     },
    132     {
    133         0xD800, 0xDF0D, 0xD800, 0xDF0C, 0xD800, 0xDF1E, 0xD800, 0xDF0F, 0xD800, 0xDF16,
    134         0xD800, 0xDF15, 0xD800, 0xDF14, 0xD800, 0xDF12, 0xD800, 0xDF10, 0xD800, 0xDF20,
    135         0xD800, 0xDF21,
    136         0x0000
    137     },
    138     // Greek
    139     {
    140         0x03b5, 0x03bb, 0x03bb, 0x03b7, 0x03bd, 0x03b9, 0x03ba, 0x03ac
    141     },
    142     // Maltese
    143     {
    144         0x0062, 0x006f, 0x006e, 0x0121, 0x0075, 0x0073, 0x0061, 0x0127,
    145         0x0127, 0x0061
    146     },
    147     // Russian
    148     {
    149         0x043f, 0x043e, 0x0447, 0x0435, 0x043c, 0x0443, 0x0436, 0x0435,
    150         0x043e, 0x043d, 0x0438, 0x043d, 0x0435, 0x0433, 0x043e, 0x0432,
    151         0x043e, 0x0440, 0x044f, 0x0442, 0x043f, 0x043e, 0x0440, 0x0443,
    152         0x0441, 0x0441, 0x043a, 0x0438
    153     },
    154     {
    155         0xFB00, 0xFB01
    156     }
    157 
    158 };
    159 
    160 static const char *asciiIn[] = {
    161     "xn--egbpdaj6bu4bxfgehfvwxn",
    162     "xn--ihqwcrb4cv8a8dqg056pqjye",
    163     "xn--Proprostnemluvesky-uyb24dma41a",
    164     "xn--4dbcagdahymbxekheh6e0a7fei0b",
    165     "xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd",
    166     "xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa",
    167 /*  "xn--989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c",*/
    168     "xn--b1abfaaepdrnnbgefbaDotcwatmq2g4l",
    169     "xn--PorqunopuedensimplementehablarenEspaol-fmd56a",
    170     "xn--ihqwctvzc91f659drss3x8bo0yb",
    171     "xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g",
    172     "xn--3B-ww4c5e180e575a65lsy2b",
    173     "xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n",
    174     "xn--Hello-Another-Way--fc4qua05auwb3674vfr0b",
    175     "xn--2-u9tlzr9756bt3uc0v",
    176     "xn--MajiKoi5-783gue6qz075azm5e",
    177     "xn--de-jg4avhby1noc0d",
    178     "xn--d9juau41awczczp",
    179     "XN--097CCDEKGHQJK",
    180     "XN--db8CBHEJLGH4E0AL",
    181     "xn--hxargifdar",                       // Greek
    182     "xn--bonusaa-5bb1da",                   // Maltese
    183     "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l", // Russian (Cyrillic)
    184     "fffi"
    185 };
    186 
    187 static const char *domainNames[] = {
    188     "slip129-37-118-146.nc.us.ibm.net",
    189     "saratoga.pe.utexas.edu",
    190     "dial-120-45.ots.utexas.edu",
    191     "woo-085.dorms.waller.net",
    192     "hd30-049.hil.compuserve.com",
    193     "pem203-31.pe.ttu.edu",
    194     "56K-227.MaxTNT3.pdq.net",
    195     "dial-36-2.ots.utexas.edu",
    196     "slip129-37-23-152.ga.us.ibm.net",
    197     "ts45ip119.cadvision.com",
    198     "sdn-ts-004txaustP05.dialsprint.net",
    199     "bar-tnt1s66.erols.com",
    200     "101.st-louis-15.mo.dial-access.att.net",
    201     "h92-245.Arco.COM",
    202     "dial-13-2.ots.utexas.edu",
    203     "net-redynet29.datamarkets.com.ar",
    204     "ccs-shiva28.reacciun.net.ve",
    205     "7.houston-11.tx.dial-access.att.net",
    206     "ingw129-37-120-26.mo.us.ibm.net",
    207     "dialup6.austintx.com",
    208     "dns2.tpao.gov.tr",
    209     "slip129-37-119-194.nc.us.ibm.net",
    210     "cs7.dillons.co.uk.203.119.193.in-addr.arpa",
    211     "swprd1.innovplace.saskatoon.sk.ca",
    212     "bikini.bologna.maraut.it",
    213     "node91.subnet159-198-79.baxter.com",
    214     "cust19.max5.new-york.ny.ms.uu.net",
    215     "balexander.slip.andrew.cmu.edu",
    216     "pool029.max2.denver.co.dynip.alter.net",
    217     "cust49.max9.new-york.ny.ms.uu.net",
    218     "s61.abq-dialin2.hollyberry.com",
    219     "\\u0917\\u0928\\u0947\\u0936.sanjose.ibm.com", //':'(0x003a) produces U_IDNA_STD3_ASCII_RULES_ERROR
    220     "www.xn--vea.com",
    221    // "www.\\u00E0\\u00B3\\u00AF.com",//' ' (0x0020) produces U_IDNA_STD3_ASCII_RULES_ERROR
    222     "www.\\u00C2\\u00A4.com",
    223     "www.\\u00C2\\u00A3.com",
    224     // "\\u0025", //'%' (0x0025) produces U_IDNA_STD3_ASCII_RULES_ERROR
    225     // "\\u005C\\u005C", //'\' (0x005C) produces U_IDNA_STD3_ASCII_RULES_ERROR
    226     //"@",
    227     //"\\u002F",
    228     //"www.\\u0021.com",
    229     //"www.\\u0024.com",
    230     //"\\u003f",
    231     // These yeild U_IDNA_PROHIBITED_ERROR
    232     //"\\u00CF\\u0082.com",
    233     //"\\u00CE\\u00B2\\u00C3\\u009Fss.com",
    234     //"\\u00E2\\u0098\\u00BA.com",
    235     "\\u00C3\\u00BC.com",
    236 
    237 };
    238 
    239 typedef struct ErrorCases ErrorCases;
    240 
    241 static const struct ErrorCases{
    242 
    243     UChar unicode[100];
    244     const char *ascii;
    245     UErrorCode expected;
    246     UBool useSTD3ASCIIRules;
    247     UBool testToUnicode;
    248     UBool testLabel;
    249 } errorCases[] = {
    250       {
    251 
    252         {
    253             0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    254             0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    255             0x070F,/*prohibited*/
    256             0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
    257             0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    258             0x0000
    259         },
    260         "www.XN--8mb5595fsoa28orucya378bqre2tcwop06c5qbw82a1rffmae0361dea96b.com",
    261         U_IDNA_PROHIBITED_ERROR,
    262         FALSE, FALSE, TRUE
    263     },
    264 
    265     {
    266         {
    267             0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    268             0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    269             0x0221, 0x0234/*Unassigned code points*/,
    270             0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    271             0x0000
    272         },
    273         "www.XN--6lA2Bz548Fj1GuA391Bf1Gb1N59Ab29A7iA.com",
    274 
    275         U_IDNA_UNASSIGNED_ERROR,
    276         FALSE, FALSE, TRUE
    277     },
    278     {
    279         {
    280             0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    281             0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    282             0x0644, 0x064A, 0x0647,/*Arabic code points. Cannot mix RTL with LTR*/
    283             0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
    284             0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    285             0x0000
    286         },
    287         "www.xn--ghBGI4851OiyA33VqrD6Az86C4qF83CtRv93D5xBk15AzfG0nAgA0578DeA71C.com",
    288         U_IDNA_CHECK_BIDI_ERROR,
    289         FALSE, FALSE, TRUE
    290     },
    291     {
    292         {
    293             0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    294             /* labels cannot begin with an HYPHEN */
    295             0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    296             0x002E,
    297             0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
    298             0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    299             0x0000
    300 
    301         },
    302         "www.xn----b95Ew8SqA315Ao5FbuMlnNmhA.com",
    303         U_IDNA_STD3_ASCII_RULES_ERROR,
    304         TRUE, FALSE, FALSE
    305     },
    306     {
    307         {
    308             /* correct ACE-prefix followed by unicode */
    309             0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    310             0x0078, 0x006e, 0x002d,0x002d,  /* ACE Prefix */
    311             0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    312             0x002D,
    313             0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
    314             0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    315             0x0000
    316 
    317         },
    318         /* wrong ACE-prefix followed by valid ACE-encoded ASCII */
    319         "www.XY-----b91I0V65S96C2A355Cw1E5yCeQr19CsnP1mFfmAE0361DeA96B.com",
    320         U_IDNA_ACE_PREFIX_ERROR,
    321         FALSE, FALSE, FALSE
    322     },
    323     /* cannot verify U_IDNA_VERIFICATION_ERROR */
    324 
    325     {
    326       {
    327         0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    328         0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
    329         0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
    330         0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C,
    331         0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    332         0x0000
    333       },
    334       "www.xn--989AoMsVi5E83Db1D2A355Cv1E0vAk1DwRv93D5xBh15A0Dt30A5JpSD879Ccm6FeA98C.com",
    335       U_IDNA_LABEL_TOO_LONG_ERROR,
    336       FALSE, FALSE, TRUE
    337     },
    338 
    339     {
    340       {
    341         0x0077, 0x0077, 0x0077, 0x002e, /* www. */
    342         0x0030, 0x0644, 0x064A, 0x0647, 0x0031, /* Arabic code points squashed between EN codepoints */
    343         0x002e, 0x0063, 0x006f, 0x006d, /* com. */
    344         0x0000
    345       },
    346       "www.xn--01-tvdmo.com",
    347       U_IDNA_CHECK_BIDI_ERROR,
    348       FALSE, FALSE, TRUE
    349     },
    350 
    351     {
    352       {
    353         0x0077, 0x0077, 0x0077, 0x002e, // www.
    354         0x206C, 0x0644, 0x064A, 0x0647, 0x206D, // Arabic code points squashed between BN codepoints
    355         0x002e, 0x0063, 0x006f, 0x006d, // com.
    356         0x0000
    357       },
    358       "www.XN--ghbgi278xia.com",
    359       U_IDNA_PROHIBITED_ERROR,
    360       FALSE, FALSE, TRUE
    361     },
    362     {
    363       {
    364         0x0077, 0x0077, 0x0077, 0x002e, // www.
    365         0x002D, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, // HYPHEN at the start of label
    366         0x002e, 0x0063, 0x006f, 0x006d, // com.
    367         0x0000
    368       },
    369       "www.-abcde.com",
    370       U_IDNA_STD3_ASCII_RULES_ERROR,
    371       TRUE, FALSE, FALSE
    372     },
    373     {
    374       {
    375         0x0077, 0x0077, 0x0077, 0x002e, // www.
    376         0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x002D, // HYPHEN at the end of the label
    377         0x002e, 0x0063, 0x006f, 0x006d, // com.
    378         0x0000
    379       },
    380       "www.abcde-.com",
    381       U_IDNA_STD3_ASCII_RULES_ERROR,
    382       TRUE, FALSE, FALSE
    383     },
    384     {
    385       {
    386         0x0077, 0x0077, 0x0077, 0x002e, // www.
    387         0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x0040, // Containing non LDH code point
    388         0x002e, 0x0063, 0x006f, 0x006d, // com.
    389         0x0000
    390       },
    391       "www.abcde@.com",
    392       U_IDNA_STD3_ASCII_RULES_ERROR,
    393       TRUE, FALSE, FALSE
    394     },
    395     {
    396       {
    397         0x0077, 0x0077, 0x0077, 0x002e, // www.
    398         // zero length label
    399         0x002e, 0x0063, 0x006f, 0x006d, // com.
    400         0x0000
    401       },
    402       "www..com",
    403       U_IDNA_ZERO_LENGTH_LABEL_ERROR,
    404       TRUE, FALSE, FALSE
    405     },
    406     {
    407       {0},
    408       NULL,
    409       U_ILLEGAL_ARGUMENT_ERROR,
    410       TRUE, TRUE, FALSE
    411     }
    412 };
    413 
    414 
    415 
    416 
    417 #define MAX_DEST_SIZE 300
    418 
    419 void TestIDNA::debug(const UChar* src, int32_t srcLength, int32_t options){
    420     UParseError parseError;
    421     UErrorCode transStatus = U_ZERO_ERROR;
    422     UErrorCode prepStatus  = U_ZERO_ERROR;
    423     NamePrepTransform* trans = NamePrepTransform::createInstance(parseError,transStatus);
    424     int32_t prepOptions = (((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0);
    425     LocalUStringPrepProfilePointer prep(usprep_openByType(USPREP_RFC3491_NAMEPREP,&prepStatus));
    426     UChar *transOut=NULL, *prepOut=NULL;
    427     int32_t transOutLength=0, prepOutLength=0;
    428 
    429 
    430     transOutLength  = trans->process(src,srcLength,transOut, 0, prepOptions>0, &parseError, transStatus);
    431     if( transStatus == U_BUFFER_OVERFLOW_ERROR){
    432         transStatus = U_ZERO_ERROR;
    433         transOut    = (UChar*) malloc(U_SIZEOF_UCHAR * transOutLength);
    434         transOutLength = trans->process(src,srcLength,transOut, transOutLength, prepOptions>0, &parseError, transStatus);
    435     }
    436 
    437     prepOutLength  = usprep_prepare(prep.getAlias(), src, srcLength, prepOut, 0, prepOptions, &parseError, &prepStatus);
    438 
    439     if( prepStatus == U_BUFFER_OVERFLOW_ERROR){
    440         prepStatus = U_ZERO_ERROR;
    441         prepOut    = (UChar*) malloc(U_SIZEOF_UCHAR * prepOutLength);
    442         prepOutLength  = usprep_prepare(prep.getAlias(), src, srcLength, prepOut, prepOutLength, prepOptions, &parseError, &prepStatus);
    443     }
    444 
    445     if(UnicodeString(transOut,transOutLength)!= UnicodeString(prepOut, prepOutLength)){
    446         errln("Failed. Expected: " + prettify(UnicodeString(transOut, transOutLength))
    447               + " Got: " + prettify(UnicodeString(prepOut,prepOutLength)));
    448     }
    449     free(transOut);
    450     free(prepOut);
    451     delete trans;
    452 }
    453 
    454 void TestIDNA::testAPI(const UChar* src, const UChar* expected, const char* testName,
    455             UBool useSTD3ASCIIRules,UErrorCode expectedStatus,
    456             UBool doCompare, UBool testUnassigned, TestFunc func, UBool testSTD3ASCIIRules){
    457 
    458     UErrorCode status = U_ZERO_ERROR;
    459     UChar destStack[MAX_DEST_SIZE];
    460     int32_t destLen = 0;
    461     UChar* dest = NULL;
    462     int32_t expectedLen = (expected != NULL) ? u_strlen(expected) : 0;
    463     int32_t options = (useSTD3ASCIIRules == TRUE) ? UIDNA_USE_STD3_RULES : UIDNA_DEFAULT;
    464     UParseError parseError;
    465     int32_t tSrcLen = 0;
    466     UChar* tSrc = NULL;
    467 
    468     if(src != NULL){
    469         tSrcLen = u_strlen(src);
    470         tSrc  =(UChar*) malloc( U_SIZEOF_UCHAR * tSrcLen );
    471         memcpy(tSrc,src,tSrcLen * U_SIZEOF_UCHAR);
    472     }
    473 
    474     // test null-terminated source and return value of number of UChars required
    475     destLen = func(src,-1,NULL,0,options, &parseError , &status);
    476     if(status == U_BUFFER_OVERFLOW_ERROR){
    477         status = U_ZERO_ERROR; // reset error code
    478         if(destLen+1 < MAX_DEST_SIZE){
    479             dest = destStack;
    480             destLen = func(src,-1,dest,destLen+1,options, &parseError, &status);
    481             // TODO : compare output with expected
    482             if(U_SUCCESS(status) && expectedStatus != U_IDNA_STD3_ASCII_RULES_ERROR&& (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    483                 errln("Did not get the expected result for "+UnicodeString(testName) +" null terminated source. Expected : "
    484                        + prettify(UnicodeString(expected,expectedLen))
    485                        + " Got: " + prettify(UnicodeString(dest,destLen))
    486                     );
    487             }
    488         }else{
    489             errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
    490         }
    491     }
    492 
    493     if(status != expectedStatus){
    494         errcheckln(status, "Did not get the expected error for "+
    495                 UnicodeString(testName)+
    496                 " null terminated source. Expected: " +UnicodeString(u_errorName(expectedStatus))
    497                 + " Got: "+ UnicodeString(u_errorName(status))
    498                 + " Source: " + prettify(UnicodeString(src))
    499                );
    500         free(tSrc);
    501         return;
    502     }
    503     if(testUnassigned ){
    504         status = U_ZERO_ERROR;
    505         destLen = func(src,-1,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
    506         if(status == U_BUFFER_OVERFLOW_ERROR){
    507             status = U_ZERO_ERROR; // reset error code
    508             if(destLen+1 < MAX_DEST_SIZE){
    509                 dest = destStack;
    510                 destLen = func(src,-1,dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
    511                 // TODO : compare output with expected
    512                 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    513                     //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
    514                     errln("Did not get the expected result for "+UnicodeString(testName) +
    515                           " null terminated source "+ prettify(src) +
    516                           " with both options set. Expected: "+  prettify(UnicodeString(expected,expectedLen))+
    517                           "Got: " + prettify(UnicodeString(dest,destLen)));
    518 
    519                     debug(src,-1,options | UIDNA_ALLOW_UNASSIGNED);
    520 
    521                 }
    522             }else{
    523                 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
    524             }
    525         }
    526         //testing query string
    527         if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
    528                 errln( "Did not get the expected error for "+
    529                     UnicodeString(testName)+
    530                     " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
    531                     + " Got: "+ UnicodeString(u_errorName(status))
    532                     + " Source: " + prettify(UnicodeString(src))
    533                    );
    534         }
    535     }
    536 
    537     status = U_ZERO_ERROR;
    538 
    539     // test source with lengthand return value of number of UChars required
    540     destLen = func(tSrc, tSrcLen, NULL,0,options, &parseError, &status);
    541     if(status == U_BUFFER_OVERFLOW_ERROR){
    542         status = U_ZERO_ERROR; // reset error code
    543         if(destLen+1 < MAX_DEST_SIZE){
    544             dest = destStack;
    545             destLen = func(src,u_strlen(src),dest,destLen+1,options, &parseError, &status);
    546             // TODO : compare output with expected
    547             if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    548                 errln("Did not get the expected result for %s with source length.\n",testName);
    549             }
    550         }else{
    551             errln( "%s with source length  failed. Requires destCapacity > 300\n",testName);
    552         }
    553     }
    554 
    555     if(status != expectedStatus){
    556         errln( "Did not get the expected error for "+
    557                     UnicodeString(testName)+
    558                     " with source length. Expected: " +UnicodeString(u_errorName(expectedStatus))
    559                     + " Got: "+ UnicodeString(u_errorName(status))
    560                     + " Source: " + prettify(UnicodeString(src))
    561                    );
    562     }
    563     if(testUnassigned){
    564         status = U_ZERO_ERROR;
    565 
    566         destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
    567 
    568         if(status == U_BUFFER_OVERFLOW_ERROR){
    569             status = U_ZERO_ERROR; // reset error code
    570             if(destLen+1 < MAX_DEST_SIZE){
    571                 dest = destStack;
    572                 destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
    573                 // TODO : compare output with expected
    574                 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    575                     errln("Did not get the expected result for %s with source length and both options set.\n",testName);
    576                 }
    577             }else{
    578                 errln( "%s with source length  failed. Requires destCapacity > 300\n",testName);
    579             }
    580         }
    581         //testing query string
    582         if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
    583             errln( "Did not get the expected error for "+
    584                     UnicodeString(testName)+
    585                     " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
    586                     + " Got: "+ UnicodeString(u_errorName(status))
    587                     + " Source: " + prettify(UnicodeString(src))
    588                    );
    589         }
    590     }
    591 
    592     status = U_ZERO_ERROR;
    593     if(testSTD3ASCIIRules==TRUE){
    594         destLen = func(src,-1,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
    595         if(status == U_BUFFER_OVERFLOW_ERROR){
    596             status = U_ZERO_ERROR; // reset error code
    597             if(destLen+1 < MAX_DEST_SIZE){
    598                 dest = destStack;
    599                 destLen = func(src,-1,dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
    600                 // TODO : compare output with expected
    601                 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    602                     //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
    603                     errln("Did not get the expected result for "+UnicodeString(testName) +" null terminated source with both options set. Expected: "+ prettify(UnicodeString(expected,expectedLen)));
    604 
    605                 }
    606             }else{
    607                 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
    608             }
    609         }
    610         //testing query string
    611         if(status != expectedStatus){
    612             errln( "Did not get the expected error for "+
    613                         UnicodeString(testName)+
    614                         " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
    615                         + " Got: "+ UnicodeString(u_errorName(status))
    616                         + " Source: " + prettify(UnicodeString(src))
    617                        );
    618         }
    619 
    620         status = U_ZERO_ERROR;
    621 
    622         destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
    623 
    624         if(status == U_BUFFER_OVERFLOW_ERROR){
    625             status = U_ZERO_ERROR; // reset error code
    626             if(destLen+1 < MAX_DEST_SIZE){
    627                 dest = destStack;
    628                 destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
    629                 // TODO : compare output with expected
    630                 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
    631                     errln("Did not get the expected result for %s with source length and both options set.\n",testName);
    632                 }
    633             }else{
    634                 errln( "%s with source length  failed. Requires destCapacity > 300\n",testName);
    635             }
    636         }
    637         //testing query string
    638         if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
    639             errln( "Did not get the expected error for "+
    640                         UnicodeString(testName)+
    641                         " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
    642                         + " Got: "+ UnicodeString(u_errorName(status))
    643                         + " Source: " + prettify(UnicodeString(src))
    644                        );
    645         }
    646     }
    647     free(tSrc);
    648 }
    649 
    650 void TestIDNA::testCompare(const UChar* s1, int32_t s1Len,
    651                         const UChar* s2, int32_t s2Len,
    652                         const char* testName, CompareFunc func,
    653                         UBool isEqual){
    654 
    655     UErrorCode status = U_ZERO_ERROR;
    656     int32_t retVal = func(s1,-1,s2,-1,UIDNA_DEFAULT,&status);
    657 
    658     if(isEqual==TRUE &&  retVal !=0){
    659         errln("Did not get the expected result for %s with null termniated strings.\n",testName);
    660     }
    661     if(U_FAILURE(status)){
    662         errcheckln(status, "%s null terminated source failed. Error: %s", testName,u_errorName(status));
    663     }
    664 
    665     status = U_ZERO_ERROR;
    666     retVal = func(s1,-1,s2,-1,UIDNA_ALLOW_UNASSIGNED,&status);
    667 
    668     if(isEqual==TRUE &&  retVal !=0){
    669         errln("Did not get the expected result for %s with null termniated strings with options set.\n", testName);
    670     }
    671     if(U_FAILURE(status)){
    672         errcheckln(status, "%s null terminated source and options set failed. Error: %s",testName, u_errorName(status));
    673     }
    674 
    675     status = U_ZERO_ERROR;
    676     retVal = func(s1,s1Len,s2,s2Len,UIDNA_DEFAULT,&status);
    677 
    678     if(isEqual==TRUE &&  retVal !=0){
    679         errln("Did not get the expected result for %s with string length.\n",testName);
    680     }
    681     if(U_FAILURE(status)){
    682         errcheckln(status, "%s with string length. Error: %s",testName, u_errorName(status));
    683     }
    684 
    685     status = U_ZERO_ERROR;
    686     retVal = func(s1,s1Len,s2,s2Len,UIDNA_ALLOW_UNASSIGNED,&status);
    687 
    688     if(isEqual==TRUE &&  retVal !=0){
    689         errln("Did not get the expected result for %s with string length and options set.\n",testName);
    690     }
    691     if(U_FAILURE(status)){
    692         errcheckln(status, "%s with string length and options set. Error: %s", u_errorName(status), testName);
    693     }
    694 }
    695 
    696 void TestIDNA::testToASCII(const char* testName, TestFunc func){
    697 
    698     int32_t i;
    699     UChar buf[MAX_DEST_SIZE];
    700 
    701     for(i=0;i< UPRV_LENGTHOF(unicodeIn); i++){
    702         u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
    703         testAPI(unicodeIn[i], buf,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
    704 
    705     }
    706 }
    707 
    708 void TestIDNA::testToUnicode(const char* testName, TestFunc func){
    709 
    710     int32_t i;
    711     UChar buf[MAX_DEST_SIZE];
    712 
    713     for(i=0;i< UPRV_LENGTHOF(asciiIn); i++){
    714         u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
    715         testAPI(buf,unicodeIn[i],testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
    716     }
    717 }
    718 
    719 
    720 void TestIDNA::testIDNToUnicode(const char* testName, TestFunc func){
    721     int32_t i;
    722     UChar buf[MAX_DEST_SIZE];
    723     UChar expected[MAX_DEST_SIZE];
    724     UErrorCode status = U_ZERO_ERROR;
    725     int32_t bufLen = 0;
    726     UParseError parseError;
    727     for(i=0;i< UPRV_LENGTHOF(domainNames); i++){
    728         bufLen = (int32_t)strlen(domainNames[i]);
    729         bufLen = u_unescape(domainNames[i],buf, bufLen+1);
    730         func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
    731         if(U_FAILURE(status)){
    732             errcheckln(status, "%s failed to convert domainNames[%i].Error: %s",testName, i, u_errorName(status));
    733             break;
    734         }
    735         testAPI(buf,expected,testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
    736          //test toUnicode with all labels in the string
    737         testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
    738         if(U_FAILURE(status)){
    739             errln( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
    740             break;
    741         }
    742     }
    743 
    744 }
    745 
    746 void TestIDNA::testIDNToASCII(const char* testName, TestFunc func){
    747     int32_t i;
    748     UChar buf[MAX_DEST_SIZE];
    749     UChar expected[MAX_DEST_SIZE];
    750     UErrorCode status = U_ZERO_ERROR;
    751     int32_t bufLen = 0;
    752     UParseError parseError;
    753     for(i=0;i< UPRV_LENGTHOF(domainNames); i++){
    754         bufLen = (int32_t)strlen(domainNames[i]);
    755         bufLen = u_unescape(domainNames[i],buf, bufLen+1);
    756         func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
    757         if(U_FAILURE(status)){
    758             errcheckln(status, "%s failed to convert domainNames[%i].Error: %s",testName,i, u_errorName(status));
    759             break;
    760         }
    761         testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
    762         //test toASCII with all labels in the string
    763         testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, FALSE, TRUE, func);
    764         if(U_FAILURE(status)){
    765             errln( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
    766             break;
    767         }
    768     }
    769 
    770 }
    771 
    772 void TestIDNA::testCompare(const char* testName, CompareFunc func){
    773     int32_t i;
    774 
    775 
    776     UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
    777     UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x0000};
    778     UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
    779 
    780     UnicodeString source(www), uni0(www),uni1(www), ascii0(www), ascii1(www);
    781 
    782     uni0.append(unicodeIn[0]);
    783     uni0.append(com);
    784     uni0.append((UChar)0x0000);
    785 
    786     uni1.append(unicodeIn[1]);
    787     uni1.append(com);
    788     uni1.append((UChar)0x0000);
    789 
    790     ascii0.append(asciiIn[0]);
    791     ascii0.append(com);
    792     ascii0.append((UChar)0x0000);
    793 
    794     ascii1.append(asciiIn[1]);
    795     ascii1.append(com);
    796     ascii1.append((UChar)0x0000);
    797 
    798     for(i=0;i< UPRV_LENGTHOF(unicodeIn); i++){
    799 
    800         u_charsToUChars(asciiIn[i],buf+4, (int32_t)(strlen(asciiIn[i])+1));
    801         u_strcat(buf,com);
    802 
    803         // for every entry in unicodeIn array
    804         // prepend www. and append .com
    805         source.truncate(4);
    806         source.append(unicodeIn[i]);
    807         source.append(com);
    808         source.append((UChar)0x0000);
    809         // a) compare it with itself
    810         const UChar* src = source.getBuffer();
    811         int32_t srcLen = u_strlen(src); //subtract null
    812 
    813         testCompare(src,srcLen,src,srcLen,testName, func, TRUE);
    814 
    815         // b) compare it with asciiIn equivalent
    816         testCompare(src,srcLen,buf,u_strlen(buf),testName, func,TRUE);
    817 
    818         // c) compare it with unicodeIn not equivalent
    819         if(i==0){
    820             testCompare(src,srcLen,uni1.getBuffer(),uni1.length()-1,testName, func,FALSE);
    821         }else{
    822             testCompare(src,srcLen,uni0.getBuffer(),uni0.length()-1,testName, func,FALSE);
    823         }
    824         // d) compare it with asciiIn not equivalent
    825         if(i==0){
    826             testCompare(src,srcLen,ascii1.getBuffer(),ascii1.length()-1,testName, func,FALSE);
    827         }else{
    828             testCompare(src,srcLen,ascii0.getBuffer(),ascii0.length()-1,testName, func,FALSE);
    829         }
    830 
    831     }
    832 }
    833 
    834 #if 0
    835 
    836 static int32_t
    837 getNextSeperator(UChar *src,int32_t srcLength,
    838                  UChar **limit){
    839     if(srcLength == -1){
    840         int32_t i;
    841         for(i=0 ; ;i++){
    842             if(src[i] == 0){
    843                 *limit = src + i; // point to null
    844                 return i;
    845             }
    846             if(src[i]==0x002e){
    847                 *limit = src + (i+1); // go past the delimiter
    848                 return i;
    849             }
    850         }
    851         // we have not found the delimiter
    852         if(i==srcLength){
    853             *limit = src+srcLength;
    854         }
    855         return i;
    856     }else{
    857         int32_t i;
    858         for(i=0;i<srcLength;i++){
    859             if(src[i]==0x002e){
    860                 *limit = src + (i+1); // go past the delimiter
    861                 return i;
    862             }
    863         }
    864         // we have not found the delimiter
    865         if(i==srcLength){
    866             *limit = src+srcLength;
    867         }
    868         return i;
    869     }
    870 }
    871 
    872 void printPunycodeOutput(){
    873 
    874     UChar dest[MAX_DEST_SIZE];
    875     int32_t destCapacity=MAX_DEST_SIZE;
    876     UChar* start;
    877     UChar* limit;
    878     int32_t labelLen=0;
    879     UBool caseFlags[MAX_DEST_SIZE];
    880 
    881     for(int32_t i=0;i< UPRV_LENGTHOF(errorCases);i++){
    882         ErrorCases errorCase = errorCases[i];
    883         UErrorCode status = U_ZERO_ERROR;
    884         start = errorCase.unicode;
    885         int32_t srcLen = u_strlen(start);
    886         labelLen = getNextSeperator(start,srcLen,&limit);
    887         start = limit;
    888         labelLen=getNextSeperator(start,srcLen-labelLen,&limit);
    889         int32_t destLen = u_strToPunycode(dest,destCapacity,start,labelLen,caseFlags, &status);
    890         if(U_FAILURE(status)){
    891             printf("u_strToPunycode failed for index %i\n",i);
    892             continue;
    893         }
    894         for(int32_t j=0; j<destLen; j++){
    895             printf("%c",(char)dest[j]);
    896         }
    897         printf("\n");
    898     }
    899 }
    900 #endif
    901 
    902 void TestIDNA::testErrorCases(const char* IDNToASCIIName, TestFunc IDNToASCII,
    903                               const char* IDNToUnicodeName, TestFunc IDNToUnicode){
    904     UChar buf[MAX_DEST_SIZE];
    905     int32_t bufLen=0;
    906 
    907     for(int32_t i=0;i< UPRV_LENGTHOF(errorCases);i++){
    908         ErrorCases errorCase = errorCases[i];
    909         UChar* src =NULL;
    910         if(errorCase.ascii != NULL){
    911             bufLen =  (int32_t)strlen(errorCase.ascii);
    912             u_charsToUChars(errorCase.ascii,buf, bufLen+1);
    913         }else{
    914             bufLen = 1 ;
    915             memset(buf,0,U_SIZEOF_UCHAR*MAX_DEST_SIZE);
    916         }
    917 
    918         if(errorCase.unicode[0]!=0){
    919             src = errorCase.unicode;
    920         }
    921         // test toASCII
    922         testAPI(src,buf,
    923                 IDNToASCIIName, errorCase.useSTD3ASCIIRules,
    924                 errorCase.expected, TRUE, TRUE, IDNToASCII);
    925         if(errorCase.testLabel ==TRUE){
    926             testAPI(src,buf,
    927                 IDNToASCIIName, errorCase.useSTD3ASCIIRules,
    928                 errorCase.expected, FALSE,TRUE, IDNToASCII);
    929         }
    930         if(errorCase.testToUnicode ==TRUE){
    931             testAPI((src==NULL)? NULL : buf,src,
    932                     IDNToUnicodeName, errorCase.useSTD3ASCIIRules,
    933                     errorCase.expected, TRUE, TRUE, IDNToUnicode);
    934         }
    935 
    936     }
    937 
    938 }
    939 /*
    940 void TestIDNA::testConformance(const char* toASCIIName, TestFunc toASCII,
    941                                const char* IDNToASCIIName, TestFunc IDNToASCII,
    942                                const char* IDNToUnicodeName, TestFunc IDNToUnicode,
    943                                const char* toUnicodeName, TestFunc toUnicode){
    944     UChar src[MAX_DEST_SIZE];
    945     int32_t srcLen=0;
    946     UChar expected[MAX_DEST_SIZE];
    947     int32_t expectedLen = 0;
    948     for(int32_t i=0;i< UPRV_LENGTHOF(conformanceTestCases);i++){
    949         const char* utf8Chars1 = conformanceTestCases[i].in;
    950         int32_t utf8Chars1Len = (int32_t)strlen(utf8Chars1);
    951         const char* utf8Chars2 = conformanceTestCases[i].out;
    952         int32_t utf8Chars2Len = (utf8Chars2 == NULL) ? 0 : (int32_t)strlen(utf8Chars2);
    953 
    954         UErrorCode status = U_ZERO_ERROR;
    955         u_strFromUTF8(src,MAX_DEST_SIZE,&srcLen,utf8Chars1,utf8Chars1Len,&status);
    956         if(U_FAILURE(status)){
    957             errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
    958             continue;
    959         }
    960         if(utf8Chars2 != NULL){
    961             u_strFromUTF8(expected,MAX_DEST_SIZE,&expectedLen,utf8Chars2,utf8Chars2Len, &status);
    962             if(U_FAILURE(status)){
    963                 errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
    964                 continue;
    965             }
    966         }
    967 
    968         if(conformanceTestCases[i].expectedStatus != U_ZERO_ERROR){
    969             // test toASCII
    970             testAPI(src,expected,
    971                     IDNToASCIIName, FALSE,
    972                     conformanceTestCases[i].expectedStatus,
    973                     TRUE,
    974                     (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
    975                     IDNToASCII);
    976 
    977             testAPI(src,expected,
    978                     toASCIIName, FALSE,
    979                     conformanceTestCases[i].expectedStatus, TRUE,
    980                     (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
    981                     toASCII);
    982         }
    983 
    984         testAPI(src,src,
    985                 IDNToUnicodeName, FALSE,
    986                 conformanceTestCases[i].expectedStatus, TRUE, TRUE, IDNToUnicode);
    987         testAPI(src,src,
    988                 toUnicodeName, FALSE,
    989                 conformanceTestCases[i].expectedStatus, TRUE, TRUE, toUnicode);
    990 
    991     }
    992 
    993 }
    994 */
    995 // test and ascertain
    996 // func(func(func(src))) == func(src)
    997 void TestIDNA::testChaining(const UChar* src,int32_t numIterations,const char* testName,
    998                   UBool useSTD3ASCIIRules, UBool caseInsensitive, TestFunc func){
    999     UChar even[MAX_DEST_SIZE];
   1000     UChar odd[MAX_DEST_SIZE];
   1001     UChar expected[MAX_DEST_SIZE];
   1002     int32_t i=0,evenLen=0,oddLen=0,expectedLen=0;
   1003     UErrorCode status = U_ZERO_ERROR;
   1004     int32_t srcLen = u_strlen(src);
   1005     int32_t options = (useSTD3ASCIIRules == TRUE) ? UIDNA_USE_STD3_RULES : UIDNA_DEFAULT;
   1006     UParseError parseError;
   1007 
   1008     // test null-terminated source
   1009     expectedLen = func(src,-1,expected,MAX_DEST_SIZE, options, &parseError, &status);
   1010     if(U_FAILURE(status)){
   1011         errcheckln(status, "%s null terminated source failed. Error: %s",testName, u_errorName(status));
   1012     }
   1013     memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1014     memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1015     for(;i<=numIterations; i++){
   1016         if((i%2) ==0){
   1017             evenLen = func(odd,-1,even,MAX_DEST_SIZE,options, &parseError, &status);
   1018             if(U_FAILURE(status)){
   1019                 errcheckln(status, "%s null terminated source failed - %s",testName, u_errorName(status));
   1020                 break;
   1021             }
   1022         }else{
   1023             oddLen = func(even,-1,odd,MAX_DEST_SIZE,options, &parseError, &status);
   1024             if(U_FAILURE(status)){
   1025                 errln("%s null terminated source failed\n",testName);
   1026                 break;
   1027             }
   1028         }
   1029     }
   1030     if(caseInsensitive ==TRUE){
   1031         if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
   1032             u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
   1033 
   1034             errln("Chaining for %s null terminated source failed\n",testName);
   1035         }
   1036     }else{
   1037         if( u_strncmp(even,expected,expectedLen) != 0 ||
   1038             u_strncmp(odd,expected,expectedLen) !=0 ){
   1039 
   1040             errln("Chaining for %s null terminated source failed\n",testName);
   1041         }
   1042     }
   1043 
   1044     // test null-terminated source
   1045     status = U_ZERO_ERROR;
   1046     expectedLen = func(src,-1,expected,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1047     if(U_FAILURE(status)){
   1048         errcheckln(status, "%s null terminated source with options set failed. Error: %s",testName, u_errorName(status));
   1049     }
   1050     memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1051     memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1052     for(;i<=numIterations; i++){
   1053         if((i%2) ==0){
   1054             evenLen = func(odd,-1,even,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1055             if(U_FAILURE(status)){
   1056                 errcheckln(status, "%s null terminated source with options set failed - %s",testName, u_errorName(status));
   1057                 break;
   1058             }
   1059         }else{
   1060             oddLen = func(even,-1,odd,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1061             if(U_FAILURE(status)){
   1062                 errln("%s null terminated source with options set failed\n",testName);
   1063                 break;
   1064             }
   1065         }
   1066     }
   1067     if(caseInsensitive ==TRUE){
   1068         if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
   1069             u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
   1070 
   1071             errln("Chaining for %s null terminated source with options set failed\n",testName);
   1072         }
   1073     }else{
   1074         if( u_strncmp(even,expected,expectedLen) != 0 ||
   1075             u_strncmp(odd,expected,expectedLen) !=0 ){
   1076 
   1077             errln("Chaining for %s null terminated source with options set failed\n",testName);
   1078         }
   1079     }
   1080 
   1081 
   1082     // test source with length
   1083     status = U_ZERO_ERROR;
   1084     expectedLen = func(src,srcLen,expected,MAX_DEST_SIZE,options, &parseError, &status);
   1085     if(U_FAILURE(status)){
   1086         errcheckln(status, "%s null terminated source failed. Error: %s",testName, u_errorName(status));
   1087     }
   1088     memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1089     memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1090     for(;i<=numIterations; i++){
   1091         if((i%2) ==0){
   1092             evenLen = func(odd,oddLen,even,MAX_DEST_SIZE,options, &parseError, &status);
   1093             if(U_FAILURE(status)){
   1094                 errcheckln(status, "%s source with source length failed - %s",testName, u_errorName(status));
   1095                 break;
   1096             }
   1097         }else{
   1098             oddLen = func(even,evenLen,odd,MAX_DEST_SIZE,options, &parseError, &status);
   1099             if(U_FAILURE(status)){
   1100                 errcheckln(status, "%s source with source length failed - %s",testName, u_errorName(status));
   1101                 break;
   1102             }
   1103         }
   1104     }
   1105     if(caseInsensitive ==TRUE){
   1106         if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
   1107             u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
   1108 
   1109             errln("Chaining for %s source with source length failed\n",testName);
   1110         }
   1111     }else{
   1112         if( u_strncmp(even,expected,expectedLen) != 0 ||
   1113             u_strncmp(odd,expected,expectedLen) !=0 ){
   1114 
   1115             errln("Chaining for %s source with source length failed\n",testName);
   1116         }
   1117     }
   1118     status = U_ZERO_ERROR;
   1119     expectedLen = func(src,srcLen,expected,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1120     if(U_FAILURE(status)){
   1121         errcheckln(status, "%s null terminated source with options set failed. Error: %s",testName, u_errorName(status));
   1122     }
   1123     memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1124     memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
   1125     for(;i<=numIterations; i++){
   1126         if((i%2) ==0){
   1127             evenLen = func(odd,oddLen,even,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1128             if(U_FAILURE(status)){
   1129                 errcheckln(status, "%s source with source length and options set failed - %s",testName, u_errorName(status));
   1130                 break;
   1131             }
   1132         }else{
   1133             oddLen = func(even,evenLen,odd,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
   1134             if(U_FAILURE(status)){
   1135                 errcheckln(status, "%s  source with source length and options set failed - %s",testName, u_errorName(status));
   1136                 break;
   1137             }
   1138         }
   1139     }
   1140     if(caseInsensitive ==TRUE){
   1141         if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
   1142             u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
   1143 
   1144             errln("Chaining for %s  source with source length and options set failed\n",testName);
   1145         }
   1146     }else{
   1147         if( u_strncmp(even,expected,expectedLen) != 0 ||
   1148             u_strncmp(odd,expected,expectedLen) !=0 ){
   1149 
   1150             errln("Chaining for %s  source with source length and options set failed\n",testName);
   1151         }
   1152     }
   1153 }
   1154 void TestIDNA::testChaining(const char* toASCIIName, TestFunc toASCII,
   1155                   const char* toUnicodeName, TestFunc toUnicode){
   1156     int32_t i;
   1157     UChar buf[MAX_DEST_SIZE];
   1158 
   1159     for(i=0;i< UPRV_LENGTHOF(asciiIn); i++){
   1160         u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
   1161         testChaining(buf,5,toUnicodeName, FALSE, FALSE, toUnicode);
   1162     }
   1163     for(i=0;i< UPRV_LENGTHOF(unicodeIn); i++){
   1164         testChaining(unicodeIn[i], 5,toASCIIName, FALSE, TRUE, toASCII);
   1165     }
   1166 }
   1167 
   1168 
   1169 void TestIDNA::testRootLabelSeparator(const char* testName, CompareFunc func,
   1170                             const char* IDNToASCIIName, TestFunc IDNToASCII,
   1171                             const char* IDNToUnicodeName, TestFunc IDNToUnicode){
   1172     int32_t i;
   1173 
   1174 
   1175     UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
   1176     UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x002E, /* root label separator */0x0000};
   1177     UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
   1178 
   1179     UnicodeString source(www), uni0(www),uni1(www), ascii0(www), ascii1(www);
   1180 
   1181     uni0.append(unicodeIn[0]);
   1182     uni0.append(com);
   1183     uni0.append((UChar)0x0000);
   1184 
   1185     uni1.append(unicodeIn[1]);
   1186     uni1.append(com);
   1187     uni1.append((UChar)0x0000);
   1188 
   1189     ascii0.append(asciiIn[0]);
   1190     ascii0.append(com);
   1191     ascii0.append((UChar)0x0000);
   1192 
   1193     ascii1.append(asciiIn[1]);
   1194     ascii1.append(com);
   1195     ascii1.append((UChar)0x0000);
   1196 
   1197     for(i=0;i< UPRV_LENGTHOF(unicodeIn); i++){
   1198 
   1199         u_charsToUChars(asciiIn[i],buf+4, (int32_t)(strlen(asciiIn[i])+1));
   1200         u_strcat(buf,com);
   1201 
   1202         // for every entry in unicodeIn array
   1203         // prepend www. and append .com
   1204         source.truncate(4);
   1205         source.append(unicodeIn[i]);
   1206         source.append(com);
   1207         source.append((UChar)0x0000);
   1208 
   1209         const UChar* src = source.getBuffer();
   1210         int32_t srcLen = u_strlen(src); //subtract null
   1211 
   1212         // b) compare it with asciiIn equivalent
   1213         testCompare(src,srcLen,buf,u_strlen(buf),testName, func,TRUE);
   1214 
   1215         // a) compare it with itself
   1216         testCompare(src,srcLen,src,srcLen,testName, func,TRUE);
   1217 
   1218 
   1219         // IDNToASCII comparison
   1220         testAPI(src,buf,IDNToASCIIName,FALSE,U_ZERO_ERROR,TRUE, TRUE, IDNToASCII);
   1221         // IDNToUnicode comparison
   1222         testAPI(buf,src,IDNToUnicodeName, FALSE,U_ZERO_ERROR, TRUE, TRUE, IDNToUnicode);
   1223 
   1224         // c) compare it with unicodeIn not equivalent
   1225         if(i==0){
   1226             testCompare(src,srcLen,uni1.getBuffer(),uni1.length()-1,testName, func,FALSE);
   1227         }else{
   1228             testCompare(src,srcLen,uni0.getBuffer(),uni0.length()-1,testName, func,FALSE);
   1229         }
   1230         // d) compare it with asciiIn not equivalent
   1231         if(i==0){
   1232             testCompare(src,srcLen,ascii1.getBuffer(),ascii1.length()-1,testName, func,FALSE);
   1233         }else{
   1234             testCompare(src,srcLen,ascii0.getBuffer(),ascii0.length()-1,testName, func,FALSE);
   1235         }
   1236     }
   1237 }
   1238 
   1239 //---------------------------------------------
   1240 // runIndexedTest
   1241 //---------------------------------------------
   1242 
   1243 extern IntlTest *createUTS46Test();
   1244 
   1245 void TestIDNA::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par)
   1246 {
   1247     if (exec) logln((UnicodeString)"TestSuite IDNA API ");
   1248     switch (index) {
   1249 
   1250         case 0: name = "TestToASCII"; if (exec) TestToASCII(); break;
   1251         case 1: name = "TestToUnicode"; if (exec) TestToUnicode(); break;
   1252         case 2: name = "TestIDNToASCII"; if (exec) TestIDNToASCII(); break;
   1253         case 3: name = "TestIDNToUnicode"; if (exec) TestIDNToUnicode(); break;
   1254         case 4: name = "TestCompare"; if (exec) TestCompare(); break;
   1255         case 5: name = "TestErrorCases"; if (exec) TestErrorCases(); break;
   1256         case 6: name = "TestChaining"; if (exec) TestChaining(); break;
   1257         case 7: name = "TestRootLabelSeparator"; if(exec) TestRootLabelSeparator(); break;
   1258         case 8: name = "TestCompareReferenceImpl"; if(exec) TestCompareReferenceImpl(); break;
   1259         case 9: name = "TestDataFile"; if(exec) TestDataFile(); break;
   1260 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
   1261         case 10: name = "TestRefIDNA"; if(exec) TestRefIDNA(); break;
   1262         case 11: name = "TestIDNAMonkeyTest"; if(exec) TestIDNAMonkeyTest(); break;
   1263 #else
   1264         case 10: case 11: name = "skip"; break;
   1265 #endif
   1266         case 12:
   1267             {
   1268                 name = "TestConformanceTestVectors";
   1269                 if(exec){
   1270                     logln("TestSuite IDNA conf----"); logln();
   1271                     IdnaConfTest test;
   1272                     callTest(test, par);
   1273                 }
   1274                 break;
   1275             }
   1276         case 13:
   1277             name = "UTS46Test";
   1278             if (exec) {
   1279                 logln("TestSuite UTS46Test---"); logln();
   1280                 LocalPointer<IntlTest> test(createUTS46Test());
   1281                 callTest(*test, par);
   1282             }
   1283             break;
   1284         default: name = ""; break; /*needed to end loop*/
   1285     }
   1286 }
   1287 void TestIDNA::TestToASCII(){
   1288     testToASCII("uidna_toASCII", uidna_toASCII);
   1289 }
   1290 void TestIDNA::TestToUnicode(){
   1291     testToUnicode("uidna_toUnicode", uidna_toUnicode);
   1292 }
   1293 void TestIDNA::TestIDNToASCII(){
   1294     testIDNToASCII("uidna_IDNToASCII", uidna_IDNToASCII);
   1295 }
   1296 void TestIDNA::TestIDNToUnicode(){
   1297     testIDNToUnicode("uidna_IDNToUnicode", uidna_IDNToUnicode);
   1298 }
   1299 void TestIDNA::TestCompare(){
   1300     testCompare("uidna_compare",uidna_compare);
   1301 }
   1302 void TestIDNA::TestErrorCases(){
   1303     testErrorCases( "uidna_IDNToASCII",uidna_IDNToASCII,
   1304                     "uidna_IDNToUnicode",uidna_IDNToUnicode);
   1305 }
   1306 void TestIDNA::TestRootLabelSeparator(){
   1307     testRootLabelSeparator( "uidna_compare",uidna_compare,
   1308                             "uidna_IDNToASCII", uidna_IDNToASCII,
   1309                             "uidna_IDNToUnicode",uidna_IDNToUnicode
   1310                             );
   1311 }
   1312 void TestIDNA::TestChaining(){
   1313     testChaining("uidna_toASCII",uidna_toASCII, "uidna_toUnicode", uidna_toUnicode);
   1314 }
   1315 
   1316 
   1317 static const int loopCount = 100;
   1318 static const int maxCharCount = 20;
   1319 
   1320 static uint32_t
   1321 randul()
   1322 {
   1323     static UBool initialized = FALSE;
   1324     if (!initialized)
   1325     {
   1326         srand((unsigned)time(NULL));
   1327         initialized = TRUE;
   1328     }
   1329     // Assume rand has at least 12 bits of precision
   1330     uint32_t l = 0;
   1331     for (uint32_t i=0; i<sizeof(l); ++i)
   1332         ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
   1333     return l;
   1334 }
   1335 
   1336 /**
   1337  * Return a random integer i where 0 <= i < n.
   1338  * A special function that gets random codepoints from planes 0,1,2 and 14
   1339  */
   1340 static int32_t rand_uni()
   1341 {
   1342    int32_t retVal = (int32_t)(randul()& 0x3FFFF);
   1343    if(retVal >= 0x30000){
   1344        retVal+=0xB0000;
   1345    }
   1346    return retVal;
   1347 }
   1348 
   1349 static int32_t randi(int32_t n){
   1350     return (int32_t) (randul() % (n+1));
   1351 }
   1352 
   1353 void getTestSource(UnicodeString& fillIn) {
   1354     int32_t i = 0;
   1355     int32_t charCount = (randi(maxCharCount) + 1);
   1356     while (i <charCount ) {
   1357         int32_t codepoint = rand_uni();
   1358         if(codepoint == 0x0000){
   1359             continue;
   1360         }
   1361         fillIn.append((UChar32)codepoint);
   1362         i++;
   1363     }
   1364 
   1365 }
   1366 
   1367 UnicodeString TestIDNA::testCompareReferenceImpl(UnicodeString& src,
   1368                                         TestFunc refIDNA, const char* refIDNAName,
   1369                                         TestFunc uIDNA, const char* uIDNAName,
   1370                                         int32_t options){
   1371 
   1372     const UChar* srcUChars = src.getBuffer();
   1373     UChar exp[MAX_DEST_SIZE]={0};
   1374     int32_t expCap = MAX_DEST_SIZE, expLen=0;
   1375     UErrorCode expStatus = U_ZERO_ERROR;
   1376     UParseError parseError;
   1377 
   1378     logln("Comparing "+ UnicodeString(refIDNAName)
   1379            + " with "+ UnicodeString(uIDNAName)
   1380            +" for input: " + prettify(srcUChars));
   1381 
   1382     expLen = refIDNA(srcUChars, src.length()-1, exp, expCap,
   1383                       options, &parseError, &expStatus);
   1384 
   1385     UChar got[MAX_DEST_SIZE]={0};
   1386     int32_t gotCap = MAX_DEST_SIZE, gotLen=0;
   1387     UErrorCode gotStatus = U_ZERO_ERROR;
   1388 
   1389     gotLen = uIDNA(srcUChars, src.length()-1, got, gotCap,
   1390                    options, &parseError, &gotStatus);
   1391 
   1392     if(expStatus != gotStatus){
   1393         errln("Did not get the expected status while comparing " + UnicodeString(refIDNAName)
   1394                + " with " + UnicodeString(uIDNAName)
   1395                + " Expected: " + UnicodeString(u_errorName(expStatus))
   1396                + " Got: " + UnicodeString(u_errorName(gotStatus))
   1397                + " for Source: "+ prettify(srcUChars)
   1398                + " Options: " + options);
   1399         return UnicodeString("");
   1400     }
   1401 
   1402     // now we know that both implementations yielded same error
   1403     if(U_SUCCESS(expStatus)){
   1404         // compare the outputs if status == U_ZERO_ERROR
   1405         if(u_strCompare(exp, expLen, got, gotLen, TRUE) != 0){
   1406             errln("Did not get the expected output while comparing " + UnicodeString(refIDNAName)
   1407                + " with " + UnicodeString(uIDNAName)
   1408                + " Expected: " + prettify(UnicodeString(exp, expLen))
   1409                + " Got: " + prettify(UnicodeString(got, gotLen))
   1410                + " for Source: "+ prettify(srcUChars)
   1411                + " Options: " + options);
   1412         }
   1413         return UnicodeString(exp, expLen);
   1414 
   1415     }else{
   1416         logln("Got the same error while comparing "
   1417             + UnicodeString(refIDNAName)
   1418             + " with "+ UnicodeString(uIDNAName)
   1419             +" for input: " + prettify(srcUChars));
   1420     }
   1421     return UnicodeString("");
   1422 }
   1423 
   1424 void TestIDNA::testCompareReferenceImpl(const UChar* src, int32_t srcLen){
   1425     UnicodeString label(src,srcLen);
   1426     label.append((UChar)0x0000);
   1427 
   1428     //test idnaref_toASCII and idnare
   1429     UnicodeString asciiLabel = testCompareReferenceImpl(label,
   1430                                                       idnaref_toASCII, "idnaref_toASCII",
   1431                                                       uidna_toASCII, "uidna_toASCII",
   1432                                                       UIDNA_ALLOW_UNASSIGNED);
   1433     testCompareReferenceImpl(label,
   1434                               idnaref_toASCII, "idnaref_toASCII",
   1435                               uidna_toASCII, "uidna_toASCII",
   1436                               UIDNA_DEFAULT);
   1437     testCompareReferenceImpl(label,
   1438                               idnaref_toASCII, "idnaref_toASCII",
   1439                               uidna_toASCII, "uidna_toASCII",
   1440                               UIDNA_USE_STD3_RULES);
   1441     testCompareReferenceImpl(label,
   1442                               idnaref_toASCII, "idnaref_toASCII",
   1443                               uidna_toASCII, "uidna_toASCII",
   1444                               UIDNA_USE_STD3_RULES | UIDNA_ALLOW_UNASSIGNED);
   1445 
   1446     if(asciiLabel.length()!=0){
   1447         asciiLabel.append((UChar)0x0000);
   1448 
   1449         // test toUnciode
   1450         testCompareReferenceImpl(asciiLabel,
   1451                                   idnaref_toUnicode, "idnaref_toUnicode",
   1452                                   uidna_toUnicode, "uidna_toUnicode",
   1453                                   UIDNA_ALLOW_UNASSIGNED);
   1454         testCompareReferenceImpl(asciiLabel,
   1455                                   idnaref_toUnicode, "idnaref_toUnicode",
   1456                                   uidna_toUnicode, "uidna_toUnicode",
   1457                                   UIDNA_DEFAULT);
   1458         testCompareReferenceImpl(asciiLabel,
   1459                                   idnaref_toUnicode, "idnaref_toUnicode",
   1460                                   uidna_toUnicode, "uidna_toUnicode",
   1461                                   UIDNA_USE_STD3_RULES);
   1462         testCompareReferenceImpl(asciiLabel,
   1463                                   idnaref_toUnicode, "idnaref_toUnicode",
   1464                                   uidna_toUnicode, "uidna_toUnicode",
   1465                                   UIDNA_USE_STD3_RULES | UIDNA_ALLOW_UNASSIGNED);
   1466     }
   1467 
   1468 }
   1469 const char* failures[] ={
   1470     "\\uAA42\\U0001F8DD\\U00019D01\\U000149A3\\uD385\\U000EE0F5\\U00018B92\\U000179D1\\U00018624\\U0002227F\\U000E83C0\\U000E8DCD\\u5460\\U00017F34\\U0001570B\\u43D1\\U0002C9C9\\U000281EC\\u2105\\U000180AE\\uC5D4",
   1471     "\\U0002F5A6\\uD638\\u0D0A\\u9E9C\\uFE5B\\U0001FCCB\\u66C4",
   1472 };
   1473 
   1474 void TestIDNA::TestIDNAMonkeyTest(){
   1475     UnicodeString source;
   1476     UErrorCode status = U_ZERO_ERROR;
   1477     int i;
   1478 
   1479     getInstance(status);    // Init prep
   1480     if (U_FAILURE(status)) {
   1481         dataerrln("Test could not initialize. Got %s", u_errorName(status));
   1482         return;
   1483     }
   1484 
   1485     for(i=0; i<loopCount; i++){
   1486         source.truncate(0);
   1487         getTestSource(source);
   1488         source.append((UChar)0x0000);
   1489         const UChar* src = source.getBuffer();
   1490         testCompareReferenceImpl(src,source.length()-1);
   1491         testCompareReferenceImpl(src,source.length()-1);
   1492     }
   1493 
   1494     /* for debugging */
   1495     for (i=0; i<UPRV_LENGTHOF(failures); i++){
   1496         source.truncate(0);
   1497         source.append( UnicodeString(failures[i], -1, US_INV) );
   1498         source = source.unescape();
   1499         source.append((UChar)0x0000);
   1500         const UChar *src = source.getBuffer();
   1501         testCompareReferenceImpl(src,source.length()-1);
   1502         //debug(source.getBuffer(),source.length(),UIDNA_ALLOW_UNASSIGNED);
   1503     }
   1504 
   1505 
   1506     source.truncate(0);
   1507     source.append(UNICODE_STRING_SIMPLE("\\uCF18\\U00021161\\U000EEF11\\U0002BB82\\U0001D63C"));
   1508     debug(source.getBuffer(),source.length(),UIDNA_ALLOW_UNASSIGNED);
   1509 
   1510     { // test deletion of code points
   1511         UnicodeString source("\\u043f\\u00AD\\u034f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0000", -1, US_INV);
   1512         source = source.unescape();
   1513         UnicodeString expected("\\u043f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0000", -1, US_INV);
   1514         expected = expected.unescape();
   1515         UnicodeString ascii("xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
   1516         ascii.append((UChar)0x0000);
   1517         testAPI(source.getBuffer(),ascii.getBuffer(), "uidna_toASCII", FALSE, U_ZERO_ERROR, TRUE, TRUE, uidna_toASCII);
   1518 
   1519         testAPI(source.getBuffer(),ascii.getBuffer(), "idnaref_toASCII", FALSE, U_ZERO_ERROR, TRUE, TRUE, idnaref_toASCII);
   1520 
   1521         testCompareReferenceImpl(source.getBuffer(), source.length()-1);
   1522     }
   1523 
   1524 }
   1525 
   1526 void TestIDNA::TestCompareReferenceImpl(){
   1527 
   1528     UChar src [2] = {0,0};
   1529     int32_t srcLen = 0;
   1530 
   1531     // data even OK?
   1532     {
   1533       UErrorCode dataStatus = U_ZERO_ERROR;
   1534       loadTestData(dataStatus);
   1535       if(U_FAILURE(dataStatus)) {
   1536         dataerrln("Couldn't load test data: %s\n", u_errorName(dataStatus)); // save us from thousands and thousands of errors
   1537         return;
   1538       }
   1539     }
   1540 
   1541     for (int32_t i = 0; i <= 0x10FFFF; i++){
   1542         if (quick == TRUE && i > 0x0FFF){
   1543             return;
   1544         }
   1545         if(i == 0x30000){
   1546             // jump to E0000, no characters assigned in plain 3 to plain 13 as of Unicode 6.0
   1547             i = 0xE0000;
   1548         }
   1549         if (i > 0xFFFF){
   1550             src[0] = U16_LEAD(i);
   1551             src[1] = U16_TRAIL(i);
   1552             srcLen =2;
   1553         } else {
   1554             src[0] = (UChar)i;
   1555             src[1] = 0;
   1556             srcLen = 1;
   1557         }
   1558         testCompareReferenceImpl(src, srcLen);
   1559     }
   1560 }
   1561 
   1562 void TestIDNA::TestRefIDNA(){
   1563     UErrorCode status = U_ZERO_ERROR;
   1564     getInstance(status);    // Init prep
   1565     if (U_FAILURE(status)) {
   1566         if (status == U_FILE_ACCESS_ERROR) {
   1567             dataerrln("Test could not initialize. Got %s", u_errorName(status));
   1568         }
   1569         return;
   1570     }
   1571 
   1572     testToASCII("idnaref_toASCII", idnaref_toASCII);
   1573     testToUnicode("idnaref_toUnicode", idnaref_toUnicode);
   1574     testIDNToASCII("idnaref_IDNToASCII", idnaref_IDNToASCII);
   1575     testIDNToUnicode("idnaref_IDNToUnicode", idnaref_IDNToUnicode);
   1576     testCompare("idnaref_compare",idnaref_compare);
   1577     testErrorCases( "idnaref_IDNToASCII",idnaref_IDNToASCII,
   1578                     "idnaref_IDNToUnicode",idnaref_IDNToUnicode);
   1579     testChaining("idnaref_toASCII",idnaref_toASCII, "idnaref_toUnicode", idnaref_toUnicode);
   1580 
   1581     testRootLabelSeparator( "idnaref_compare",idnaref_compare,
   1582                             "idnaref_IDNToASCII", idnaref_IDNToASCII,
   1583                             "idnaref_IDNToUnicode",idnaref_IDNToUnicode
   1584                             );
   1585     testChaining("idnaref_toASCII",idnaref_toASCII, "idnaref_toUnicode", idnaref_toUnicode);
   1586 }
   1587 
   1588 
   1589 void TestIDNA::TestDataFile(){
   1590      testData(*this);
   1591 }
   1592 TestIDNA::~TestIDNA(){
   1593     if(gPrep!=NULL){
   1594         delete gPrep;
   1595         gPrep = NULL;
   1596     }
   1597 }
   1598 
   1599 NamePrepTransform* TestIDNA::gPrep = NULL;
   1600 
   1601 NamePrepTransform* TestIDNA::getInstance(UErrorCode& status){
   1602     if(TestIDNA::gPrep == NULL){
   1603         UParseError parseError;
   1604         TestIDNA::gPrep = NamePrepTransform::createInstance(parseError, status);
   1605         if(TestIDNA::gPrep ==NULL){
   1606            //status = U_MEMORY_ALLOCATION_ERROR;
   1607            return NULL;
   1608         }
   1609     }
   1610     return TestIDNA::gPrep;
   1611 
   1612 }
   1613 #endif /* #if !UCONFIG_NO_IDNA */
   1614