Blame tests/conv-utf8.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2016 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Authors: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation; either version 3 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit aea12f
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
#include <stdarg.h>
Packit aea12f
#include <stddef.h>
Packit aea12f
#include <setjmp.h>
Packit aea12f
#include <limits.h>
Packit aea12f
#include <stdint.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
#include <string.h>
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
#include <gnutls/x509.h>
Packit aea12f
Packit aea12f
#include <cmocka.h>
Packit aea12f
Packit aea12f
int _gnutls_utf8_to_ucs2(const void *data, size_t size,
Packit aea12f
			 gnutls_datum_t * output, unsigned be);
Packit aea12f
Packit aea12f
int _gnutls_ucs2_to_utf8(const void *data, size_t size,
Packit aea12f
			 gnutls_datum_t * output, unsigned be);
Packit aea12f
Packit aea12f
#define DEBUG
Packit aea12f
Packit aea12f
#ifdef DEBUG
Packit aea12f
static void PRINT(const char *str, unsigned char *val, unsigned int size)
Packit aea12f
{
Packit aea12f
	unsigned i;
Packit aea12f
	printf("%s", str);
Packit aea12f
	for (i=0;i
Packit aea12f
		printf("%.2x", val[i]);
Packit aea12f
	}
Packit aea12f
	printf("\n");
Packit aea12f
}
Packit aea12f
#else
Packit aea12f
#define PRINT(x, y, x)
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#define UTF8_MATCH(fname, utf8, utf16) \
Packit aea12f
static void fname(void **glob_state) \
Packit aea12f
{ \
Packit aea12f
	gnutls_datum_t out; \
Packit aea12f
	int ret = _gnutls_utf8_to_ucs2(utf8, strlen(utf8), &out, 1); \
Packit aea12f
	assert_int_equal(ret, 0); \
Packit aea12f
	if (out.size != sizeof(utf16)-1 || memcmp(utf16, out.data, out.size) != 0) { PRINT("got:      ", out.data, out.size); \
Packit aea12f
	PRINT("expected: ", (unsigned char*)utf16, sizeof(utf16)-1); } \
Packit aea12f
	assert_int_equal(out.size, sizeof(utf16)-1); \
Packit aea12f
	assert_memory_equal(utf16, out.data, out.size); \
Packit aea12f
	gnutls_free(out.data); \
Packit aea12f
}
Packit aea12f
Packit aea12f
#define UTF16_MATCH(fname, utf8, utf16) \
Packit aea12f
static void fname(void **glob_state) \
Packit aea12f
{ \
Packit aea12f
	gnutls_datum_t out; \
Packit aea12f
	int ret = _gnutls_ucs2_to_utf8(utf16, sizeof(utf16)-1, &out, 1); \
Packit aea12f
	assert_int_equal(ret, 0); \
Packit aea12f
	if (out.size != strlen(utf8) || memcmp(utf8, out.data, out.size) != 0) { PRINT("got:      ", out.data, out.size); \
Packit aea12f
	PRINT("expected: ", (unsigned char*)utf8, strlen(utf8)); } \
Packit aea12f
	assert_int_equal(out.size, strlen(utf8)); \
Packit aea12f
	assert_memory_equal(utf8, out.data, out.size); \
Packit aea12f
	gnutls_free(out.data); \
Packit aea12f
}
Packit aea12f
Packit aea12f
#define UTF8_FAIL(fname, utf8, utf8_size) \
Packit aea12f
static void fname(void **glob_state) \
Packit aea12f
{ \
Packit aea12f
	gnutls_datum_t out; \
Packit aea12f
	int ret = _gnutls_utf8_to_ucs2(utf8, utf8_size, &out, 1); \
Packit aea12f
	assert_int_not_equal(ret, 0); \
Packit aea12f
}
Packit aea12f
Packit aea12f
#define UTF16_FAIL(fname, utf16, utf16_size) \
Packit aea12f
static void fname(void **glob_state) \
Packit aea12f
{ \
Packit aea12f
	gnutls_datum_t out; \
Packit aea12f
	int ret = _gnutls_ucs2_to_utf8(utf16, utf16_size, &out, 1); \
Packit aea12f
	assert_int_not_equal(ret, 0); \
Packit aea12f
}
Packit aea12f
Packit aea12f
UTF8_MATCH(check_utf8_ok1, "abcd", "\x00\x61\x00\x62\x00\x63\x00\x64");
Packit aea12f
UTF8_MATCH(check_utf8_ok2, "ユーザー別サイト", "\x30\xE6\x30\xFC\x30\xB6\x30\xFC\x52\x25\x30\xB5\x30\xA4\x30\xC8");
Packit aea12f
UTF8_MATCH(check_utf8_ok3, "简体中文", "\x7B\x80\x4F\x53\x4E\x2D\x65\x87");
Packit aea12f
UTF8_MATCH(check_utf8_ok4, "Σὲ γνωρίζω ἀπὸ", "\x03\xA3\x1F\x72\x00\x20\x03\xB3\x03\xBD\x03\xC9\x03\xC1\x03\xAF\x03\xB6\x03\xC9\x00\x20\x1F\x00\x03\xC0\x1F\x78");
Packit aea12f
Packit aea12f
UTF16_MATCH(check_utf16_ok1, "abcd", "\x00\x61\x00\x62\x00\x63\x00\x64");
Packit aea12f
UTF16_MATCH(check_utf16_ok2, "ユーザー別サイト", "\x30\xE6\x30\xFC\x30\xB6\x30\xFC\x52\x25\x30\xB5\x30\xA4\x30\xC8");
Packit aea12f
UTF16_MATCH(check_utf16_ok3, "简体中文", "\x7B\x80\x4F\x53\x4E\x2D\x65\x87");
Packit aea12f
UTF16_MATCH(check_utf16_ok4, "Σὲ γνωρίζω ἀπὸ", "\x03\xA3\x1F\x72\x00\x20\x03\xB3\x03\xBD\x03\xC9\x03\xC1\x03\xAF\x03\xB6\x03\xC9\x00\x20\x1F\x00\x03\xC0\x1F\x78");
Packit aea12f
Packit aea12f
UTF8_FAIL(check_utf8_fail1, "\xfe\xff\xaa\x80\xff", 5);
Packit aea12f
UTF8_FAIL(check_utf8_fail2, "\x64\x00\x62\xf3\x64\x65", 6);
Packit aea12f
UTF16_FAIL(check_utf16_fail1, "\xd8\x00\xdb\xff\x00\x63\x00\x04", 8);
Packit aea12f
Packit aea12f
int main(void)
Packit aea12f
{
Packit aea12f
	const struct CMUnitTest tests[] = {
Packit aea12f
		cmocka_unit_test(check_utf8_fail1),
Packit aea12f
		cmocka_unit_test(check_utf8_fail2),
Packit aea12f
		cmocka_unit_test(check_utf16_fail1),
Packit aea12f
		cmocka_unit_test(check_utf8_ok1),
Packit aea12f
		cmocka_unit_test(check_utf8_ok2),
Packit aea12f
		cmocka_unit_test(check_utf8_ok3),
Packit aea12f
		cmocka_unit_test(check_utf8_ok4),
Packit aea12f
		cmocka_unit_test(check_utf16_ok1),
Packit aea12f
		cmocka_unit_test(check_utf16_ok2),
Packit aea12f
		cmocka_unit_test(check_utf16_ok3),
Packit aea12f
		cmocka_unit_test(check_utf16_ok4)
Packit aea12f
	};
Packit aea12f
	return cmocka_run_group_tests(tests, NULL, NULL);
Packit aea12f
}