#ifndef lint
static char *rcsid = "$Id: normalizer.tsy,v 1.1 2003/06/04 00:26:57 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/normalizer.h>
#include <idn/log.h>
#include <idn/ucs4.h>

#define BUF_SIZE	64
#define TOBUF_SIZE	4
#define ARRAY_SIZE	20
#define CONF_FILENAME	"test.map"

/*
 * Sample string for `from' argument of normalize(),
 * and its expected outputs.
 */
static const unsigned long from[4] = {
	0x304B,	/* hiragana letter ka */
	0x3099,	/* combining katakana-hiragana voiced sound mark */
	0x32D0,	/* circled katakana a */
	0x0000
};

static const unsigned long normalized_kc_str[3] = {
	0x304C,	/* hiragana letter ga */
	0x30A2,	/* katakana letter a */
	0x0000
};

static const unsigned long normalized_c_str[3] = {
	0x304C,	/* hiragana letter ga */
	0x32D0,	/* circled katakana a */
	0x0000
};

idn_result_t
test_proc(const unsigned long *from, unsigned long *to, size_t tolen)
{
	if (tolen > idn_ucs4_strlen(from)) {
		idn_ucs4_strcpy(to, from);
	} else {
		return (idn_buffer_overflow);
	}

	return (idn_success);
}

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

//# SETUP
//	group: noinit
//--
//	Do nothing
{
	idn_result_t r;
	const char *name;
}

//# SETUP
//	group: generic
//--
//	Initialize the module and create context.
{
	idn_result_t r;
	idn_normalizer_t ctx = NULL;

	r = idn_normalizer_initialize();
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_create(&ctx);
	ASSERT_RESULT(r, idn_success);
}

//# TEARDOWN
//	group: generic
//--
//	Destroy context.
{
	idn_normalizer_destroy(ctx);
}

//# SETUP
//	group: addall
//--
//	Initialize the module and create context.
{
	idn_result_t r;
	idn_normalizer_t ctx = NULL;
	char *names[ARRAY_SIZE];
	int i;

	for (i = 0; i < ARRAY_SIZE; i++) {
		names[i] = malloc(BUF_SIZE);
		if (names[i] == NULL) {
			ASSERT("malloc failed\n");
		}
	}

	strcpy(names[0], "RFC3491");
	strcpy(names[1], "unicode-form-kc");
	strcpy(names[2], "unicode-form-kc/3.2.0");
	strcpy(names[3], "RFC3491");
	strcpy(names[4], "unicode-form-kc");
	strcpy(names[5], "unicode-form-kc/3.2.0");
	strcpy(names[6], "RFC3491");
	strcpy(names[7], "unicode-form-kc");
	strcpy(names[8], "unicode-form-kc/3.2.0");
	strcpy(names[9], "RFC3491");
	strcpy(names[10], "unicode-form-kc");
	strcpy(names[11], "unicode-form-kc/3.2.0");
	strcpy(names[12], "RFC3491");
	strcpy(names[13], "unicode-form-kc");
	strcpy(names[14], "unicode-form-kc/3.2.0");
	strcpy(names[15], "RFC3491");
	strcpy(names[16], "unicode-form-kc");
	strcpy(names[17], "unicode-form-kc/3.2.0");
	strcpy(names[18], "RFC3491");
	strcpy(names[19], "unicode-form-kc");

	r = idn_normalizer_initialize();
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_create(&ctx);
	ASSERT_RESULT(r, idn_success);
}

//# TEARDOWN
//	group: addall
//--
//	Destroy context.
{
	idn_normalizer_destroy(ctx);
	for (i = 0; i < ARRAY_SIZE; i++) {
		free(names[i]);
	}
}

//# 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_normalizer_add() - boundary condition
//	group: generic quiet
{
	r = idn_normalizer_add(ctx, "");
	ASSERT_RESULT(r, idn_invalid_name);
}

//# TESTCASE
//	title: idn_normalizer_add() - builtin schemes
//	group: generic quiet
{
	r = idn_normalizer_add(ctx, "RFC3491");
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_add(ctx, "unicode-form-kc");
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_add(ctx, "unicode-form-kc/3.2.0");
	ASSERT_RESULT(r, idn_success);

	r = idn_normalizer_add(ctx, "nameprep-01");
	ASSERT_RESULT(r, idn_invalid_name);
}

//# TESTCASE
//	title: idn_normalizer_addall() - boundary condition
//	group: addall quiet
{
	strcpy(names[3], "");
	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
	ASSERT_RESULT(r, idn_invalid_name);
}

//# TESTCASE
//	title: idn_normalizer_addall() - nschemes is 0
//	group: addall quiet
{
	r = idn_normalizer_addall(ctx, (const char **)names, 0);
	ASSERT_RESULT(r, idn_success);
}

//# TESTCASE
//	title: idn_normalizer_addall() - add a lot of schemes
//	group: addall quiet
{
	unsigned long to[TOBUF_SIZE];

	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, normalized_kc_str);
}

//# TESTCASE
//	title: idn_normalizer_addall() - add same scheme repetedly
//	group: addall quiet
{
	int i;
	unsigned long to[TOBUF_SIZE];

	for (i = 0; i < ARRAY_SIZE; i++) {
		strcpy(names[i], "RFC3491");
	}
	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, normalized_kc_str);
}

//# TESTCASE
//	title: idn_normalizer_normalize() - schemes check - RFC3491
//	group: generic quiet
{
	unsigned long to[TOBUF_SIZE];

	r = idn_normalizer_add(ctx, "RFC3491");
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, normalized_kc_str);
}

//# TESTCASE
//	title: idn_normalizer_normalize() - schemes check - unicode-form-kc/3.2.0
//	group: generic quiet
{
	unsigned long to[TOBUF_SIZE];

	r = idn_normalizer_add(ctx, "unicode-form-kc/3.2.0");
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, normalized_kc_str);
}

//# TESTCASE
//	title: idn_normalizer_normalize() - context without procedure
//	group: generic quiet
{
	unsigned long to[TOBUF_SIZE];

	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, from);
}

//# TESTCASE
//	title: idn_normalizer_destroy(), idn_normalizer_incrref()
//	group:
{
	idn_result_t r;
	idn_normalizer_t ctx = NULL;

	r = idn_normalizer_initialize();
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_create(&ctx);
	ASSERT_RESULT(r, idn_success);
	idn_normalizer_incrref(ctx);
	idn_normalizer_destroy(ctx);
	idn_normalizer_destroy(ctx);
}

//# TESTCASE
//	title: idn_normalizer_register()
//	group: generic quiet
{
	unsigned long to[TOBUF_SIZE];

	r = idn_normalizer_register("test", test_proc);
	ASSERT_RESULT(r, idn_success);
	r = idn_normalizer_add(ctx, "test");
	ASSERT_RESULT(r, idn_success);

	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
	ASSERT_RESULT(r, idn_success);
	ASSERT_UCS4STRING(to, from);
}
