#ifndef lint
static char *rcsid = "$Id: nameprep.tsy,v 1.1.1.1 2003/06/04 00:26:56 marka Exp $";
#endif

/*
 * Copyright (c) 2002 Japan Network Information Center.
 * All rights reserved.
 *  
 * By using this file, you agree to the terms and conditions set forth bellow.
 * 
 * 			LICENSE TERMS AND CONDITIONS 
 * 
 * The following License Terms and Conditions apply, unless a different
 * license is obtained from Japan Network Information Center ("JPNIC"),
 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
 * Chiyoda-ku, Tokyo 101-0047, Japan.
 * 
 * 1. Use, Modification and Redistribution (including distribution of any
 *    modified or derived work) in source and/or binary forms is permitted
 *    under this License Terms and Conditions.
 * 
 * 2. Redistribution of source code must retain the copyright notices as they
 *    appear in each source code file, this License Terms and Conditions.
 * 
 * 3. Redistribution in binary form must reproduce the Copyright Notice,
 *    this License Terms and Conditions, in the documentation and/or other
 *    materials provided with the distribution.  For the purposes of binary
 *    distribution the "Copyright Notice" refers to the following language:
 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
 * 
 * 4. The name of JPNIC may not be used to endorse or promote products
 *    derived from this Software without specific prior written approval of
 *    JPNIC.
 * 
 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <idn/nameprep.h>
#include <idn/log.h>
#include "testutil.h"

#define UCS4_NAME	0x304C	/* hiragana letter ga */
#define BUF_SIZE	4
/*
 * Sample string for `from' argument for map(),
 * and its expected outputs.
 */
static const unsigned long map_from[] = {
	0x0041,		/* latin capital letter a */
	0x0042,		/* latin capital letter b */
	UCS4_NAME,
	0x0000
};

static const unsigned long map_expected[] = {
	0x0061,		/* latin small letter a */
	0x0062,		/* latin small letter b */
	UCS4_NAME,
	0x0000
};

/*
 * Sample string for `from' argument for isprohibited().
 */
static const unsigned long check_from[4] = {
	UCS4_NAME,
	0x00A0, /* no-break space: prohibited character */
	0x0221, /* unassigned character */
	0x0000
};

#define FROM_UCS4NAME_OFFSET	0
#define FROM_PROH_OFFSET	1
#define FROM_UNAS_OFFSET	2

/*
 * Sample string for `from' argument for isunassigned().
 */
static const unsigned long check_from2[4] = {
	UCS4_NAME,
	0x0221, /* unassigned character */
	0x00A0, /* no-break space: prohibited character */
	0x0000
};

#define FROM2_UCS4NAME_OFFSET	0
#define FROM2_PROH_OFFSET	2
#define FROM2_UNAS_OFFSET	1

/*
 * Sample string for `from' argument for isvalidbidi().
 * (It is not a valid bidi label.)
 */
static const unsigned long bidi_from[4] = {
	0x05BE, /* hebrew punctuation maqaf */
	0x0041, /* latin capital letter a */
	0xFEFC, /* arabic ligature lam with alef final form */
	0x0000
};
#define BIDIFROM_OFFSET		1

/*
 * Empty string.
 */
static const unsigned long ucs4_nullstr[] = {
	0x0000
};

//--------------------------------------------------------------------
// Setups and Teardowns.
//--------------------------------------------------------------------

//# SETUP
//	group: generic
//--
//	Nothing happens.
{
	idn_result_t r;
	idn_nameprep_t handle = NULL;

}

//# SETUP
//	group: check
//--
//	Initialize the module and create contexts.
{
	idn_result_t r;
	idn_nameprep_t handle11 = NULL;

	r = idn_nameprep_create("RFC3491", &handle11);
	ASSERT_RESULT(r, idn_success);
}

//# TEARDOWN
//	group: check
//--
//	Destroy contexts.
{
	if (handle11 != NULL) {
		idn_nameprep_destroy(handle11);
	}
}

//# SETUP
//	group: quiet
//--
//	Set log level to `fatal' to supress log messages.
{
	int saved_log_level;

	saved_log_level = idn_log_getlevel();
	idn_log_setlevel(idn_log_level_fatal);
}

//# TEARDOWN
//	group: quiet
//--
//	Restore log level.
{
	idn_log_setlevel(saved_log_level);
}

//--------------------------------------------------------------------
// Testcases.
//--------------------------------------------------------------------

//# TESTCASE
//	title: idn_nameprep_create() - boundary condition
//	group: generic quiet
{
	r = idn_nameprep_create("", &handle);
	ASSERT_RESULT(r, idn_notfound);
}

//# TESTCASE
//	title: idn_nameprep_create() - version is NULL (current nameprep)
//	group: generic quiet
{
	unsigned long to[BUF_SIZE];
	const unsigned long *found;

	r = idn_nameprep_create(NULL, &handle);
	ASSERT_RESULT(r, idn_success);

	r = idn_nameprep_map(handle, map_from, to, BUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, map_expected);

	r = idn_nameprep_isunassigned(handle, check_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from + FROM_UNAS_OFFSET);

	r = idn_nameprep_isprohibited(handle, check_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from + FROM_PROH_OFFSET);

	r = idn_nameprep_isvalidbidi(handle, bidi_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, bidi_from + BIDIFROM_OFFSET);

	idn_nameprep_destroy(handle);
}

//# TESTCASE
//	title: idn_nameprep_create() - nameprep-01
//	group: generic quiet
{
	r = idn_nameprep_create("nameprep-01", &handle);
	ASSERT_RESULT(r, idn_notfound);
}

//# TESTCASE
//	title: idn_nameprep_create() - RFC3491
//	group: generic
{
	r = idn_nameprep_create("RFC3491", &handle);
	ASSERT_RESULT(r, idn_success);
	idn_nameprep_destroy(handle);
}

//# TESTCASE
//	title: idn_nameprep_map() - boundary condition
//	group: check
{
	unsigned long to[BUF_SIZE];

	r = idn_nameprep_map(handle11, ucs4_nullstr, to, 0);
	ASSERT_RESULT(r, idn_buffer_overflow);
	r = idn_nameprep_map(handle11, ucs4_nullstr, to, 1);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, ucs4_nullstr);
}

//# TESTCASE
//	title: idn_nameprep_map() - RFC3491
//	group: check
{
	unsigned long to[BUF_SIZE];

	r = idn_nameprep_map(handle11, map_from, to, 0);
	ASSERT_RESULT(r, idn_buffer_overflow);
	r = idn_nameprep_map(handle11, map_from, to, BUF_SIZE - 1);
	ASSERT_RESULT(r, idn_buffer_overflow);
	r = idn_nameprep_map(handle11, map_from, to, BUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, map_expected);
}

//# TESTCASE
//	title: idn_nameprep_isunassigned() - boundary condition
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isunassigned(handle11, ucs4_nullstr, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, NULL);
}

//# TESTCASE
//	title: idn_nameprep_isunassigned() - RFC3491
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isunassigned(handle11, check_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from + FROM_UNAS_OFFSET);

	r = idn_nameprep_isunassigned(handle11, check_from2, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from2 + FROM2_UNAS_OFFSET);
}

//# TESTCASE
//	title: idn_nameprep_isprohibited() - boundary condition
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isprohibited(handle11, ucs4_nullstr, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, NULL);
}

//# TESTCASE
//	title: idn_nameprep_isprohibited() - RFC3491
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isprohibited(handle11, check_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from + FROM_PROH_OFFSET);

	r = idn_nameprep_isprohibited(handle11, check_from2, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, check_from2 + FROM2_PROH_OFFSET);
}

//# TESTCASE
//	title: idn_nameprep_isvalidbidi() - boundary condition
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isvalidbidi(handle11, ucs4_nullstr, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, NULL);
}

//# TESTCASE
//	title: idn_nameprep_isvalidbidi() - RFC3491
//	group: check
{
	const unsigned long *found;

	r = idn_nameprep_isvalidbidi(handle11, bidi_from, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, bidi_from + BIDIFROM_OFFSET);

	r = idn_nameprep_isvalidbidi(handle11, check_from2, &found);
	ASSERT_RESULT(r, idn_success);
	ASSERT_PTR(found, NULL);
}
