Blame contrib/idn/idnkit-1.0-src/lib/tests/ucs4.tsy

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