]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-charset: Added a minimal unit test
authorTimo Sirainen <tss@iki.fi>
Sat, 16 May 2015 09:46:38 +0000 (12:46 +0300)
committerTimo Sirainen <tss@iki.fi>
Sat, 16 May 2015 09:46:38 +0000 (12:46 +0300)
src/lib-charset/Makefile.am
src/lib-charset/charset-iconv.c
src/lib-charset/test-charset.c [new file with mode: 0644]

index 5f1d08beec8d1373780e2eb671c990c651d3f20e..122d1dff1b858736d632a25b4c12cc396e493027 100644 (file)
@@ -1,7 +1,8 @@
 noinst_LTLIBRARIES = libcharset.la
 
 AM_CPPFLAGS = \
-       -I$(top_srcdir)/src/lib
+       -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-test
 
 libcharset_la_LIBADD = $(LTLIBICONV)
 libcharset_la_SOURCES = \
@@ -13,3 +14,23 @@ headers = \
 
 pkginc_libdir=$(pkgincludedir)
 pkginc_lib_HEADERS = $(headers)
+
+test_programs = \
+       test-charset
+
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+       ../lib-test/libtest.la \
+       ../lib/liblib.la
+test_deps = $(noinst_LTLIBRARIES) $(test_libs)
+
+test_charset_SOURCES = test-charset.c
+test_charset_LDADD = libcharset.la $(test_libs)
+test_charset_DEPENDENCIES = libcharset.la $(test_deps)
+
+check: check-am check-test
+check-test: all-am
+       for bin in $(test_programs); do \
+         if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \
+       done
index b93f3654c6e108f725f4f683e591eaa731c04214..09eb6a87724740a23363a4c38740a191bdb74936 100644 (file)
@@ -118,8 +118,7 @@ charset_to_utf8(struct charset_translation *t,
 
                if (result == CHARSET_RET_INVALID_INPUT) {
                        if (prev_invalid_pos != dest->used) {
-                               uni_ucs4_to_utf8_c(UNICODE_REPLACEMENT_CHAR,
-                                                  dest);
+                               str_append(dest, UNICODE_REPLACEMENT_CHAR_UTF8);
                                prev_invalid_pos = dest->used;
                        }
                        pos++;
diff --git a/src/lib-charset/test-charset.c b/src/lib-charset/test-charset.c
new file mode 100644 (file)
index 0000000..249eec6
--- /dev/null
@@ -0,0 +1,90 @@
+/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "str.h"
+#include "test-common.h"
+#include "charset-utf8.h"
+
+static void test_charset_is_utf8(void)
+{
+       test_begin("charset_is_utf8");
+       test_assert(charset_is_utf8("AScII"));
+       test_assert(charset_is_utf8("us-AScII"));
+       test_assert(charset_is_utf8("uTF8"));
+       test_assert(charset_is_utf8("uTF-8"));
+       test_end();
+}
+
+static void test_charset_utf8_common(const char *input_charset)
+{
+       struct {
+               const char *input;
+               const char *output;
+               enum charset_result result;
+       } tests[] = {
+               { "päÃ", "pä", CHARSET_RET_INCOMPLETE_INPUT },
+               { "päÃa", "pä"UNICODE_REPLACEMENT_CHAR_UTF8"a", CHARSET_RET_INVALID_INPUT }
+       };
+       string_t *str = t_str_new(128);
+       enum charset_result result;
+       unsigned int i;
+
+       for (i = 0; i < N_ELEMENTS(tests); i++) {
+               str_truncate(str, 0);
+               test_assert_idx(charset_to_utf8_str(input_charset, NULL,
+                                                   tests[i].input, str, &result) == 0, i);
+               test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
+               test_assert_idx(result == tests[i].result, i);
+       }
+}
+
+static void test_charset_utf8(void)
+{
+       test_begin("charset utf8");
+       test_charset_utf8_common("UTF-8");
+       test_end();
+}
+
+#ifdef HAVE_ICONV
+static void test_charset_iconv(void)
+{
+       struct {
+               const char *charset;
+               const char *input;
+               const char *output;
+               enum charset_result result;
+       } tests[] = {
+               { "ISO-8859-1", "p\xE4\xE4", "pää", CHARSET_RET_OK }
+       };
+       string_t *str = t_str_new(128);
+       enum charset_result result;
+       unsigned int i;
+
+       test_begin("charset iconv");
+       for (i = 0; i < N_ELEMENTS(tests); i++) {
+               str_truncate(str, 0);
+               test_assert_idx(charset_to_utf8_str(tests[i].charset, NULL,
+                                                   tests[i].input, str, &result) == 0, i);
+               test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
+               test_assert_idx(result == tests[i].result, i);
+       }
+       /* Use //IGNORE just to force handling to be done by iconv
+          instead of our own UTF-8 routines. */
+       test_charset_utf8_common("UTF-8//IGNORE");
+       test_end();
+}
+#endif
+
+int main(void)
+{
+       static void (*test_functions[])(void) = {
+               test_charset_is_utf8,
+               test_charset_utf8,
+#ifdef HAVE_ICONV
+               test_charset_iconv,
+#endif
+               NULL
+       };
+
+       return test_run(test_functions);
+}