5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: ucs4.tsy,v 1.1 2003/06/04 00:27:04 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2002 Japan Network Information Center.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * All rights reserved.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * By using this file, you agree to the terms and conditions set forth bellow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LICENSE TERMS AND CONDITIONS
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * The following License Terms and Conditions apply, unless a different
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * license is obtained from Japan Network Information Center ("JPNIC"),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Chiyoda-ku, Tokyo 101-0047, Japan.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 1. Use, Modification and Redistribution (including distribution of any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * modified or derived work) in source and/or binary forms is permitted
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * under this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 2. Redistribution of source code must retain the copyright notices as they
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * appear in each source code file, this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 3. Redistribution in binary form must reproduce the Copyright Notice,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * this License Terms and Conditions, in the documentation and/or other
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * materials provided with the distribution. For the purposes of binary
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * distribution the "Copyright Notice" refers to the following language:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 4. The name of JPNIC may not be used to endorse or promote products
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * derived from this Software without specific prior written approval of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Sample UTF8 and UCS4 strings.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic const char *utf8_string =
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\x7f" /* 0x0000007f */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\xdf\xbf" /* 0x000007ff */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\xef\xbf\xbf" /* 0x0000ffff */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\xf7\xbf\xbf\xbf" /* 0x001fffff */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\xfb\xbf\xbf\xbf\xbf" /* 0x03ffffff */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "\xfd\xbf\xbf\xbf\xbf\xbf"; /* 0x7fffffff */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic const unsigned long ucs4_string[] = {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews//--------------------------------------------------------------------
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// Setups and Teardowns.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews//--------------------------------------------------------------------
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: utf8-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long to[256];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t tolen = 256;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: ucs4-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char to[256];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t tolen = 256;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: quiet
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int saved_log_level;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews saved_log_level = idn_log_getlevel();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_log_setlevel(idn_log_level_fatal);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: quiet
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_log_setlevel(saved_log_level);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews//--------------------------------------------------------------------
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// Testcases.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews//--------------------------------------------------------------------
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call utf8toucs4()
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: utf8-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(utf8_string, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_UCS4STRING(to, ucs4_string);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call ucs4toutf8()
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: ucs4-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(ucs4_string, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_STRING(to, utf8_string);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call utf8toucs4() with empty from
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: utf8-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static unsigned long empty_ucs4_string[] = {0};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_UCS4STRING(to, empty_ucs4_string);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call ucs4toutf8() with empty from
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: ucs4-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static unsigned long empty_ucs4_string[] = {0};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(empty_ucs4_string, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_STRING(to, "");
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call utf8toucs4() with broken string
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: utf8-init quiet
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xfe" as the 1st byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xfe\xbf\xbf\xbf\xbf\xbf\xbf", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\x7f" as the 2nd byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xdf\x7f", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xc0" as the 2nd byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xdf\xc0", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\x7f" as the 3rd byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xef\xbf\x7f", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xc0" as the 3rd byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xef\xbf\xc0", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\x7f" as the 4th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xf7\xbf\xbf\x7f", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xc0" as the 4th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xf7\xbf\xbf\xc0", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\x7f" as the 5th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xfb\xbf\xbf\xbf\x7f", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xc0" as the 5th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xfb\xbf\xbf\xbf\xc0", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\x7f" as the 6th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xfd\xbf\xbf\xbf\xbf\x7f", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* "\xc0" as the 6th byte is out of range. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xfd\xbf\xbf\xbf\xbf\xc0", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* `from' contains surrogate pair */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4("\xed\xa0\x80\xed\xbf\xbf", to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: call ucs4toutf8() with broken string
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: ucs4-init quiet
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static unsigned long invalid_ucs4_string0[] = {0x80000000, 0};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static unsigned long invalid_ucs4_string1[] = {0xd800, 0xdfff, 0};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* 0x80000000 is out of range */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(invalid_ucs4_string0, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* `from' contains surrogate pair */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(invalid_ucs4_string1, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_invalid_encoding);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: buffer overrun test for utf8toucs4()
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: utf8-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(utf8_string, to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_ucs4_strlen(ucs4_string) + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_UCS4STRING(to, ucs4_string);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(utf8_string, to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_ucs4_strlen(ucs4_string));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(utf8_string, to, 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// title: buffer overrun test for ucs4toutf8()
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews// group: ucs4-init
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(ucs4_string, to, strlen(utf8_string) + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_STRING(to, utf8_string);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(ucs4_string, to, strlen(utf8_string));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(ucs4_string, to, 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ASSERT_RESULT(r, idn_buffer_overflow);