From 2d34f67f2c45d259744570042a0fd78739617328 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 8 Nov 2016 15:24:00 +0100 Subject: tests: added unit tests for gnutls_utf8_password_normalize() --- tests/Makefile.am | 3 +- tests/str-unicode.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/str-unicode.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 499e8211b7..d323a4c97d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -125,12 +125,13 @@ endif if HAVE_CMOCKA CMOCKA_LDADD = $(COMMON_LDADD) $(CMOCKA_LIBS) -ctests += dtls-sliding-window ip-utils name-constraints-ip conv-utf8 +ctests += dtls-sliding-window ip-utils name-constraints-ip conv-utf8 str-unicode dtls_sliding_window_LDADD = $(CMOCKA_LDADD) ip_utils_LDADD = $(CMOCKA_LDADD) name_constraints_ip_LDADD = $(CMOCKA_LDADD) conv_utf8_LDADD = $(CMOCKA_LDADD) +str_unicode_LDADD = $(CMOCKA_LDADD) endif diff --git a/tests/str-unicode.c b/tests/str-unicode.c new file mode 100644 index 0000000000..274fb6e956 --- /dev/null +++ b/tests/str-unicode.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2016 Red Hat, Inc. + * + * Authors: Nikos Mavrogiannopoulos + * + * This file is part of GnuTLS. + * + * GnuTLS is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuTLS is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GnuTLS; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(HAVE_LIBUNISTRING) + +# include + +#define MATCH_FUNC(fname, password, normalized) \ +static void fname(void **glob_state) \ +{ \ + gnutls_datum_t out; \ + int ret = gnutls_utf8_password_normalize((uint8_t*)password, strlen(password), &out, 0); \ + if (normalized == NULL) { /* expect failure */ \ + assert_int_not_equal(ret, 0); \ + return; \ + } else { \ + assert_int_equal(ret, 0); \ + } \ + assert_int_equal(strcmp((char*)out.data, (char*)normalized), 0); \ + gnutls_free(out.data); \ +} + +MATCH_FUNC(test_ascii, "correct horse battery staple", "correct horse battery staple"); +MATCH_FUNC(test_capitals, "Correct Horse Battery Staple", "Correct Horse Battery Staple"); +MATCH_FUNC(test_multilang, "\xCF\x80\xC3\x9F\xC3\xA5", "πßå"); +MATCH_FUNC(test_special_char, "\x4A\x61\x63\x6B\x20\x6F\x66\x20\xE2\x99\xA6\x73", "Jack of ♦s"); +MATCH_FUNC(test_space_replacement, "foo bar", "foo bar"); +MATCH_FUNC(test_invalid, "my cat is a \x09by", NULL); +MATCH_FUNC(test_normalization1, "char \x49\xCC\x87", "char \xC4\xB0"); + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ascii), + cmocka_unit_test(test_capitals), + cmocka_unit_test(test_multilang), + cmocka_unit_test(test_special_char), + cmocka_unit_test(test_space_replacement), + cmocka_unit_test(test_invalid), + cmocka_unit_test(test_normalization1) + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} +#else +int main(void) +{ + gnutls_datum_t out; + int ret = gnutls_utf8_password_normalize((uint8_t*)"xxx", strlen("xxx"), &out, 0); + if (ret != GNUTLS_E_UNIMPLEMENTED_FEATURE) + exit(1); + exit(77); +} +#endif -- cgit v1.2.1