summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2011-12-06 14:57:58 +0100
committerStephen Gallagher <sgallagh@redhat.com>2011-12-16 14:46:16 -0500
commitdbea04f585a30d001b574317c068cd03a4fa332b (patch)
treef192eea6f1777f8442b625485f788bf2b411b5bc /src/tests
parentf7255e5d207800eb86003ec926951c0a6397aa04 (diff)
downloadsssd-dbea04f585a30d001b574317c068cd03a4fa332b.tar.gz
sssd-dbea04f585a30d001b574317c068cd03a4fa332b.tar.xz
sssd-dbea04f585a30d001b574317c068cd03a4fa332b.zip
sss_utf8_tolower utility function+unit tests
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/util-tests.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c
index 52d302778..33406dacf 100644
--- a/src/tests/util-tests.c
+++ b/src/tests/util-tests.c
@@ -26,6 +26,7 @@
#include <talloc.h>
#include <check.h>
#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;
}