From dbea04f585a30d001b574317c068cd03a4fa332b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 6 Dec 2011 14:57:58 +0100 Subject: sss_utf8_tolower utility function+unit tests --- src/tests/util-tests.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'src/tests') diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c index 52d30277..33406dac 100644 --- a/src/tests/util-tests.c +++ b/src/tests/util-tests.c @@ -26,6 +26,7 @@ #include #include #include "util/util.h" +#include "util/sss_utf8.h" #include "tests/common.h" START_TEST(test_parse_args) @@ -312,6 +313,91 @@ START_TEST(test_size_t_overflow) } END_TEST +START_TEST(test_utf8_lowercase) +{ + const uint8_t munchen_utf8_upcase[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 }; + const uint8_t munchen_utf8_lowcase[] = { 'm', 0xC3, 0xBC, 'n', 'c', 'h', 'e', 'n', 0x0 }; + uint8_t *lcase; + size_t nlen; + + lcase = sss_utf8_tolower(munchen_utf8_upcase, + strlen((const char *)munchen_utf8_upcase), + &nlen); + fail_if(strlen((const char *) munchen_utf8_upcase) != nlen); /* This is not true for utf8 strings in general */ + fail_if(memcmp(lcase, munchen_utf8_lowcase, nlen)); + sss_utf8_free(lcase); +} +END_TEST + +START_TEST(test_utf8_talloc_lowercase) +{ + const uint8_t munchen_utf8_upcase[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 }; + const uint8_t munchen_utf8_lowcase[] = { 'm', 0xC3, 0xBC, 'n', 'c', 'h', 'e', 'n', 0x0 }; + uint8_t *lcase; + size_t nsize; + + TALLOC_CTX *test_ctx; + test_ctx = talloc_new(NULL); + fail_if(test_ctx == NULL); + + lcase = sss_tc_utf8_tolower(test_ctx, munchen_utf8_upcase, + strlen((const char *) munchen_utf8_upcase), + &nsize); + fail_if(memcmp(lcase, munchen_utf8_lowcase, nsize)); + talloc_free(test_ctx); +} +END_TEST + +START_TEST(test_utf8_talloc_str_lowercase) +{ + const uint8_t munchen_utf8_upcase[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 }; + const uint8_t munchen_utf8_lowcase[] = { 'm', 0xC3, 0xBC, 'n', 'c', 'h', 'e', 'n', 0x0 }; + char *lcase; + + TALLOC_CTX *test_ctx; + test_ctx = talloc_new(NULL); + fail_if(test_ctx == NULL); + + lcase = sss_tc_utf8_str_tolower(test_ctx, (const char *) munchen_utf8_upcase); + fail_if(memcmp(lcase, munchen_utf8_lowcase, strlen(lcase))); + talloc_free(test_ctx); +} +END_TEST + +START_TEST(test_utf8_caseeq) +{ + const uint8_t munchen_utf8_upcase[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 }; + const uint8_t munchen_utf8_lowcase[] = { 'm', 0xC3, 0xBC, 'n', 'c', 'h', 'e', 'n', 0x0 }; + const uint8_t czech_utf8_lowcase[] = { 0xC4, 0x8D, 'e', 'c', 'h', 0x0 }; + const uint8_t czech_utf8_upcase[] = { 0xC4, 0x8C, 'e', 'c', 'h', 0x0 }; + const uint8_t czech_utf8_lowcase_neg[] = { 0xC4, 0x8E, 'e', 'c', 'h', 0x0 }; + errno_t ret; + + ret = sss_utf8_case_eq(munchen_utf8_upcase, munchen_utf8_lowcase); + fail_unless(ret == EOK, "Latin 1 Supplement comparison failed\n"); + + ret = sss_utf8_case_eq(czech_utf8_upcase, czech_utf8_lowcase); + fail_unless(ret == EOK, "Latin Extended A comparison failed\n"); + + ret = sss_utf8_case_eq(czech_utf8_upcase, czech_utf8_lowcase_neg); + fail_if(ret == EOK, "Negative test succeeded\n"); +} +END_TEST + +START_TEST(test_utf8_check) +{ + const char *invalid = "ad\351la\357d"; + const uint8_t valid[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 }; + bool ret; + + ret = sss_utf8_check(valid, strlen((const char *) valid)); + fail_unless(ret == true, "Positive test failed\n"); + + ret = sss_utf8_check((const uint8_t *) invalid, strlen(invalid)); + fail_unless(ret == false, "Negative test succeeded\n"); +} +END_TEST + Suite *util_suite(void) { Suite *s = suite_create("util"); @@ -324,7 +410,17 @@ Suite *util_suite(void) tcase_add_test (tc_util, test_parse_args); tcase_set_timeout(tc_util, 60); + TCase *tc_utf8 = tcase_create("utf8"); + tcase_add_test (tc_util, test_utf8_lowercase); + tcase_add_test (tc_util, test_utf8_talloc_lowercase); + tcase_add_test (tc_util, test_utf8_talloc_str_lowercase); + tcase_add_test (tc_util, test_utf8_caseeq); + tcase_add_test (tc_util, test_utf8_check); + + tcase_set_timeout(tc_utf8, 60); + suite_add_tcase (s, tc_util); + suite_add_tcase (s, tc_utf8); return s; } -- cgit