Blame tests/str-unicode.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2016 Red Hat, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Authors: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit Service 4684c1
 * under the terms of the GNU General Public License as published by
Packit Service 4684c1
 * the Free Software Foundation; either version 3 of the License, or
Packit Service 4684c1
 * (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU General Public License
Packit Service 4684c1
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit Service 4684c1
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
#include <stdarg.h>
Packit Service 4684c1
#include <stddef.h>
Packit Service 4684c1
#include <setjmp.h>
Packit Service 4684c1
#include <limits.h>
Packit Service 4684c1
#include <stdint.h>
Packit Service 4684c1
#include <stdlib.h>
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
#include <gnutls/gnutls.h>
Packit Service 4684c1
#include <cmocka.h>
Packit Service 4684c1
Packit Service 4684c1
#define MATCH_FUNC(fname, password, normalized) \
Packit Service 4684c1
static void fname(void **glob_state) \
Packit Service 4684c1
{ \
Packit Service 4684c1
	const char *pwd_normalized = normalized; \
Packit Service 4684c1
	gnutls_datum_t out; \
Packit Service 4684c1
	int ret = gnutls_utf8_password_normalize((uint8_t*)password, strlen(password), &out, 0); \
Packit Service 4684c1
	if (pwd_normalized == NULL) { /* expect failure */ \
Packit Service 4684c1
		assert_int_not_equal(ret, 0); \
Packit Service 4684c1
	} else { \
Packit Service 4684c1
		assert_int_equal(ret, 0); \
Packit Service 4684c1
		assert_int_equal(strcmp((char*)out.data, (char*)pwd_normalized), 0); \
Packit Service 4684c1
		gnutls_free(out.data); \
Packit Service 4684c1
	} \
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
#define INVALID_MATCH_FUNC(fname, password, normalized) \
Packit Service 4684c1
static void inv_##fname(void **glob_state) \
Packit Service 4684c1
{ \
Packit Service 4684c1
	const char *pwd_normalized = normalized; \
Packit Service 4684c1
	gnutls_datum_t out; \
Packit Service 4684c1
	int ret = gnutls_utf8_password_normalize((uint8_t*)password, strlen(password), &out, GNUTLS_UTF8_IGNORE_ERRS); \
Packit Service 4684c1
	if (pwd_normalized == NULL) { \
Packit Service 4684c1
		assert_int_not_equal(ret, 0); \
Packit Service 4684c1
	} else { \
Packit Service 4684c1
		assert_int_equal(ret, 0); \
Packit Service 4684c1
		assert_int_equal(strcmp((char*)out.data, (char*)pwd_normalized), 0); \
Packit Service 4684c1
		gnutls_free(out.data); \
Packit Service 4684c1
	} \
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
MATCH_FUNC(test_ascii, "correct horse battery staple", "correct horse battery staple");
Packit Service 4684c1
MATCH_FUNC(test_capitals, "Correct Horse Battery Staple", "Correct Horse Battery Staple");
Packit Service 4684c1
MATCH_FUNC(test_multilang, "\xCF\x80\xC3\x9F\xC3\xA5", "πßå");
Packit Service 4684c1
MATCH_FUNC(test_special_char, "\x4A\x61\x63\x6B\x20\x6F\x66\x20\xE2\x99\xA6\x73", "Jack of ♦s");
Packit Service 4684c1
MATCH_FUNC(test_space_replacement, "foo bar", "foo bar");
Packit Service 4684c1
MATCH_FUNC(test_invalid, "my cat is a \x09 by", NULL);
Packit Service 4684c1
MATCH_FUNC(test_normalization1, "char \x49\xCC\x87", "char \xC4\xB0");
Packit Service 4684c1
MATCH_FUNC(test_other_chars, "char \xc2\xbc", "char \xC2\xbc");
Packit Service 4684c1
MATCH_FUNC(test_spaces, "char \xe2\x80\x89\xe2\x80\x88 ", "char    ");
Packit Service 4684c1
MATCH_FUNC(test_symbols, "char \xe2\x98\xa3 \xe2\x99\xa3", "char \xe2\x98\xa3 \xe2\x99\xa3");
Packit Service 4684c1
MATCH_FUNC(test_compatibility, "char \xcf\x90\xe2\x84\xb5", "char \xcf\x90\xe2\x84\xb5");
Packit Service 4684c1
MATCH_FUNC(test_invalid_ignorable1, "my ignorable char is \xe2\x80\x8f", NULL);
Packit Service 4684c1
MATCH_FUNC(test_invalid_ignorable2, "my ignorable char is \xe1\x85\x9f", NULL);
Packit Service 4684c1
MATCH_FUNC(test_invalid_ignorable3, "my ignorable char is \xef\xbf\xbf", NULL);
Packit Service 4684c1
MATCH_FUNC(test_invalid_exception1, "my exception is \xc2\xb7", NULL); /* CONTEXTO - disallowed */
Packit Service 4684c1
MATCH_FUNC(test_invalid_exception2, "my exception is \xcf\x82", "my exception is ς"); /* PVALID */
Packit Service 4684c1
MATCH_FUNC(test_invalid_exception3, "my exception is \xd9\xa2", NULL); /* CONTEXT0/PVALID */
Packit Service 4684c1
MATCH_FUNC(test_invalid_exception4, "my exception is \xe3\x80\xae", NULL); /* CONTEXT0/DISALLOWED */
Packit Service 4684c1
MATCH_FUNC(test_invalid_join_control, "my exception is \xe2\x80\x8d", NULL);
Packit Service 4684c1
Packit Service 4684c1
INVALID_MATCH_FUNC(test_ascii, "correct horse battery staple", "correct horse battery staple");
Packit Service 4684c1
INVALID_MATCH_FUNC(test_special_char, "\x4A\x61\x63\x6B\x20\x6F\x66\x20\xE2\x99\xA6\x73", "Jack of ♦s");
Packit Service 4684c1
INVALID_MATCH_FUNC(test_invalid, "my cat is a \x09 by", "my cat is a \x09 by");
Packit Service 4684c1
INVALID_MATCH_FUNC(test_invalid_exception1, "my exception is \xc2\xb7", "my exception is ·");
Packit Service 4684c1
INVALID_MATCH_FUNC(test_invalid_exception3, "my exception is \xd9\xa2", "my exception is \xd9\xa2");
Packit Service 4684c1
INVALID_MATCH_FUNC(test_invalid_exception4, "my exception is \xe3\x80\xae", "my exception is \xe3\x80\xae"); /* CONTEXT0/DISALLOWED */
Packit Service 4684c1
INVALID_MATCH_FUNC(test_invalid_join_control, "my exception is \xe2\x80\x8d", "my exception is \xe2\x80\x8d");
Packit Service 4684c1
Packit Service 4684c1
int main(void)
Packit Service 4684c1
{
Packit Service 4684c1
	const struct CMUnitTest tests[] = {
Packit Service 4684c1
		cmocka_unit_test(test_ascii),
Packit Service 4684c1
		cmocka_unit_test(test_capitals),
Packit Service 4684c1
		cmocka_unit_test(test_multilang),
Packit Service 4684c1
		cmocka_unit_test(test_special_char),
Packit Service 4684c1
		cmocka_unit_test(test_space_replacement),
Packit Service 4684c1
		cmocka_unit_test(test_invalid),
Packit Service 4684c1
		cmocka_unit_test(test_normalization1),
Packit Service 4684c1
		cmocka_unit_test(inv_test_ascii),
Packit Service 4684c1
		cmocka_unit_test(inv_test_special_char),
Packit Service 4684c1
		cmocka_unit_test(inv_test_invalid),
Packit Service 4684c1
		cmocka_unit_test(test_other_chars),
Packit Service 4684c1
		cmocka_unit_test(test_spaces),
Packit Service 4684c1
		cmocka_unit_test(test_compatibility),
Packit Service 4684c1
		cmocka_unit_test(test_symbols),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_ignorable1),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_ignorable2),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_ignorable3),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_exception1),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_exception2),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_exception3),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_exception4),
Packit Service 4684c1
		cmocka_unit_test(test_invalid_join_control),
Packit Service 4684c1
		cmocka_unit_test(inv_test_invalid_exception1),
Packit Service 4684c1
		cmocka_unit_test(inv_test_invalid_exception3),
Packit Service 4684c1
		cmocka_unit_test(inv_test_invalid_exception4),
Packit Service 4684c1
		cmocka_unit_test(inv_test_invalid_join_control)
Packit Service 4684c1
	};
Packit Service 4684c1
	return cmocka_run_group_tests(tests, NULL, NULL);
Packit Service 4684c1
}