+static void TestLength(){
+ {
+ static const char* cl = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label";
+ UChar ul[128] = {'\0'};
+ UChar dest[256] = {'\0'};
+ /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an
+ IDNA prepared string (including xn--)that is exactly 63 bytes long */
+ UChar ul1[] = { 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
+ 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0x00AD, 0x034F, 0x1806, 0x180B,
+ 0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02,
+ 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B,
+ 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
+ 0xC138, 0x0041, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
+ 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
+ 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
+ 0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
+ 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
+ 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
+ 0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
+ 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
+ 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
+ 0xFE0F, 0xFEFF, 0x0000
+ };
+
+ int32_t len1 = UPRV_LENGTHOF(ul1)-1/*remove the null termination*/;
+ int32_t destLen = UPRV_LENGTHOF(dest);
+ UErrorCode status = U_ZERO_ERROR;
+ UParseError ps;
+ int32_t len = (int32_t)strlen(cl);
+ u_charsToUChars(cl, ul, len+1);
+ destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_ZERO_ERROR){
+ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = -1;
+ destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_ZERO_ERROR){
+ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = (int32_t)strlen(cl);
+ destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_LABEL_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = -1;
+ destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_LABEL_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_ZERO_ERROR){
+ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len1 = -1;
+ destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_ZERO_ERROR){
+ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
+ }
+ }
+ {
+ static const char* cl = "my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.ibm.com";
+ UChar ul[400] = {'\0'};
+ UChar dest[400] = {'\0'};
+ int32_t destLen = UPRV_LENGTHOF(dest);
+ UErrorCode status = U_ZERO_ERROR;
+ UParseError ps;
+ int32_t len = (int32_t)strlen(cl);
+ u_charsToUChars(cl, ul, len+1);
+
+ destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = -1;
+ destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = (int32_t)strlen(cl);
+ destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ destLen = UPRV_LENGTHOF(dest);
+ len = -1;
+ destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ uidna_compare(ul, len, ul, len, UIDNA_DEFAULT, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status));
+ }
+ uidna_compare(ul, -1, ul, -1, UIDNA_DEFAULT, &status);
+ if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
+ log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status));
+ }
+ }
+}
+static void TestJB5273(){
+ static const char INVALID_DOMAIN_NAME[] = "xn--m\\u00FCller.de";
+ UChar invalid_idn[25] = {'\0'};
+ int32_t len = u_unescape(INVALID_DOMAIN_NAME, invalid_idn, strlen(INVALID_DOMAIN_NAME));
+ UChar output[50] = {'\0'};
+ UErrorCode status = U_ZERO_ERROR;
+ UParseError prsError;
+ int32_t outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status);
+ (void)outLen; /* Suppress set but not used warning. */
+ if(U_FAILURE(status)){
+ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status);
+ if(U_FAILURE(status)){
+ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status);
+ if(U_FAILURE(status)){
+ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
+ }
+ status = U_ZERO_ERROR;
+ outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status);
+ if(U_FAILURE(status)){
+ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
+ }
+}
+
+/*
+ * Test the new (ICU 4.6/2010) C API that was added for UTS #46.
+ * Just an API test: Functionality is tested via C++ intltest.
+ */
+static void TestUTS46() {
+ static const UChar fA_sharps16[] = { 0x66, 0x41, 0xdf, 0 };
+ static const char fA_sharps8[] = { 0x66, 0x41, (char)0xc3, (char)0x9f, 0 };
+ static const UChar fa_sharps16[] = { 0x66, 0x61, 0xdf, 0 };
+ static const char fa_sharps8[] = { 0x66, 0x61, (char)0xc3, (char)0x9f, 0 };
+ static const UChar fass16[] = { 0x66, 0x61, 0x73, 0x73, 0 };
+ static const char fass8[] = { 0x66, 0x61, 0x73, 0x73, 0 };
+ static const UChar fA_BEL[] = { 0x66, 0x41, 7, 0 };
+ static const UChar fa_FFFD[] = { 0x66, 0x61, 0xfffd, 0 };
+
+ UChar dest16[10];
+ char dest8[10];
+ int32_t length;
+
+ UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+ UErrorCode errorCode = U_ZERO_ERROR;
+ UIDNA *uts46 = uidna_openUTS46(UIDNA_USE_STD3_RULES|UIDNA_NONTRANSITIONAL_TO_UNICODE,
+ &errorCode);
+ if(U_FAILURE(errorCode)) {
+ log_err_status(errorCode, "uidna_openUTS46() failed: %s\n", u_errorName(errorCode));
+ return;
+ }
+
+ /* These calls should succeed. */
+ length = uidna_labelToASCII(uts46, fA_sharps16, -1,
+ dest16, UPRV_LENGTHOF(dest16), &info, &errorCode);
+ if( U_FAILURE(errorCode) || length != 4 || 0 != u_memcmp(dest16, fass16, 5) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_labelToASCII() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToUnicode(uts46, fA_sharps16, u_strlen(fA_sharps16),
+ dest16, UPRV_LENGTHOF(dest16), &info, &errorCode);
+ if( U_FAILURE(errorCode) || length != 3 || 0 != u_memcmp(dest16, fa_sharps16, 4) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_labelToUnicode() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToASCII(uts46, fA_sharps16, u_strlen(fA_sharps16),
+ dest16, 4, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 4 || 0 != u_memcmp(dest16, fass16, 4) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_nameToASCII() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToUnicode(uts46, fA_sharps16, -1,
+ dest16, 3, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 3 || 0 != u_memcmp(dest16, fa_sharps16, 3) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_nameToUnicode() failed: %s\n", u_errorName(errorCode));
+ }
+
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToASCII_UTF8(uts46, fA_sharps8, -1,
+ dest8, UPRV_LENGTHOF(dest8), &info, &errorCode);
+ if( U_FAILURE(errorCode) || length != 4 || 0 != memcmp(dest8, fass8, 5) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_labelToASCII_UTF8() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToUnicodeUTF8(uts46, fA_sharps8, strlen(fA_sharps8),
+ dest8, UPRV_LENGTHOF(dest8), &info, &errorCode);
+ if( U_FAILURE(errorCode) || length != 4 || 0 != memcmp(dest8, fa_sharps8, 5) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_labelToUnicodeUTF8() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToASCII_UTF8(uts46, fA_sharps8, strlen(fA_sharps8),
+ dest8, 4, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 4 || 0 != memcmp(dest8, fass8, 4) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_nameToASCII_UTF8() failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToUnicodeUTF8(uts46, fA_sharps8, -1,
+ dest8, 4, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 4 || 0 != memcmp(dest8, fa_sharps8, 4) ||
+ !info.isTransitionalDifferent || info.errors != 0
+ ) {
+ log_err("uidna_nameToUnicodeUTF8() failed: %s\n", u_errorName(errorCode));
+ }
+
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToASCII(uts46, NULL, 0,
+ dest16, 0, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 0 ||
+ info.isTransitionalDifferent || info.errors != UIDNA_ERROR_EMPTY_LABEL
+ ) {
+ log_err("uidna_nameToASCII(empty) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToUnicode(uts46, fA_BEL, -1,
+ dest16, 3, &info, &errorCode);
+ if( errorCode != U_STRING_NOT_TERMINATED_WARNING ||
+ length != 3 || 0 != u_memcmp(dest16, fa_FFFD, 3) ||
+ info.isTransitionalDifferent || info.errors == 0
+ ) {
+ log_err("uidna_nameToUnicode(fa<BEL>) failed: %s\n", u_errorName(errorCode));
+ }
+
+ /* These calls should fail. */
+ errorCode = U_USELESS_COLLATOR_ERROR;
+ length = uidna_labelToASCII(uts46, fA_sharps16, -1,
+ dest16, UPRV_LENGTHOF(dest16), &info, &errorCode);
+ if(errorCode != U_USELESS_COLLATOR_ERROR) {
+ log_err("uidna_labelToASCII(failure) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToUnicode(uts46, fA_sharps16, u_strlen(fA_sharps16),
+ dest16, UPRV_LENGTHOF(dest16), NULL, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_labelToUnicode(UIDNAInfo=NULL) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToASCII(uts46, NULL, u_strlen(fA_sharps16),
+ dest16, 4, &info, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_nameToASCII(src=NULL) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToUnicode(uts46, fA_sharps16, -2,
+ dest16, 3, &info, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_nameToUnicode(length<-1) failed: %s\n", u_errorName(errorCode));
+ }
+
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToASCII_UTF8(uts46, fA_sharps8, -1,
+ NULL, UPRV_LENGTHOF(dest8), &info, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_labelToASCII_UTF8(dest=NULL) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_labelToUnicodeUTF8(uts46, fA_sharps8, strlen(fA_sharps8),
+ dest8, -1, &info, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_labelToUnicodeUTF8(capacity<0) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToASCII_UTF8(uts46, dest8, strlen(fA_sharps8),
+ dest8, 4, &info, &errorCode);
+ if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
+ log_err("uidna_nameToASCII_UTF8(src==dest!=NULL) failed: %s\n", u_errorName(errorCode));
+ }
+ errorCode = U_ZERO_ERROR;
+ length = uidna_nameToUnicodeUTF8(uts46, fA_sharps8, -1,
+ dest8, 3, &info, &errorCode);
+ if(errorCode != U_BUFFER_OVERFLOW_ERROR || length != 4) {
+ log_err("uidna_nameToUnicodeUTF8() overflow failed: %s\n", u_errorName(errorCode));
+ }
+
+ uidna_close(uts46);
+}
+