dovecot-2.2: lib-imap: Added imap_utf7_is_valid()

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 5 17:50:10 EEST 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/a38a7b5fb195
changeset: 16455:a38a7b5fb195
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 05 17:48:48 2013 +0300
description:
lib-imap: Added imap_utf7_is_valid()

diffstat:

 src/lib-imap/imap-utf7.c      |  19 +++++++++
 src/lib-imap/imap-utf7.h      |   2 +
 src/lib-imap/test-imap-utf7.c |  83 +++++++++++++++---------------------------
 3 files changed, 51 insertions(+), 53 deletions(-)

diffs (153 lines):

diff -r 5593d6129712 -r a38a7b5fb195 src/lib-imap/imap-utf7.c
--- a/src/lib-imap/imap-utf7.c	Wed Jun 05 17:14:49 2013 +0300
+++ b/src/lib-imap/imap-utf7.c	Wed Jun 05 17:48:48 2013 +0300
@@ -271,3 +271,22 @@
 	}
 	return 0;
 }
+
+bool imap_utf7_is_valid(const char *src)
+{
+	const char *p;
+	int ret;
+
+	for (p = src; *p != '\0'; p++) {
+		if (*p == '&' || (unsigned char)*p >= 0x80) {
+			/* slow scan */
+			T_BEGIN {
+				string_t *tmp = t_str_new(128);
+				ret = imap_utf7_to_utf8(p, tmp);
+			} T_END;
+			if (ret < 0)
+				return FALSE;
+		}
+	}
+	return TRUE;
+}
diff -r 5593d6129712 -r a38a7b5fb195 src/lib-imap/imap-utf7.h
--- a/src/lib-imap/imap-utf7.h	Wed Jun 05 17:14:49 2013 +0300
+++ b/src/lib-imap/imap-utf7.h	Wed Jun 05 17:48:48 2013 +0300
@@ -8,5 +8,7 @@
 /* Convert IMAP-UTF-7 string to UTF-8. Returns 0 if ok, -1 if src isn't
    valid IMAP-UTF-7. */
 int imap_utf7_to_utf8(const char *src, string_t *dest);
+/* Returns TRUE if the string is valid IMAP-UTF-7 string. */
+bool imap_utf7_is_valid(const char *src);
 
 #endif
diff -r 5593d6129712 -r a38a7b5fb195 src/lib-imap/test-imap-utf7.c
--- a/src/lib-imap/test-imap-utf7.c	Wed Jun 05 17:14:49 2013 +0300
+++ b/src/lib-imap/test-imap-utf7.c	Wed Jun 05 17:48:48 2013 +0300
@@ -8,53 +8,46 @@
 
 static void test_imap_utf7(void)
 {
-	static const char *to_utf7[] = {
-		"&&x&&", "&-&-x&-&-",
-		"~peter/mail/台北/日本語", "~peter/mail/&U,BTFw-/&ZeVnLIqe-",
-		"tietäjä", "tiet&AOQ-j&AOQ-",
-		"p\xe4\xe4", NULL,
-		NULL
-	};
-	static const char *invalid_utf7[] = {
-		"&Jjo!",
-		"&U,BTFw-&ZeVnLIqe-",
-		NULL
+	static struct test {
+		const char *utf8;
+		const char *mutf7;
+	} tests[] = {
+		{ "&&x&&", "&-&-x&-&-" },
+		{ "~peter/mail/台北/日本語", "~peter/mail/&U,BTFw-/&ZeVnLIqe-" },
+		{ "tietäjä", "tiet&AOQ-j&AOQ-" },
+		{ "p\xe4\xe4", NULL },
+		{ NULL, "&" },
+		{ NULL, "&Jjo" },
+		{ NULL, "&Jjo!" },
+		{ NULL, "&U,BTFw-&ZeVnLIqe-" }
 	};
 	string_t *src, *dest;
 	const char *orig_src;
 	unsigned int i, j;
 	unichar_t chr;
-	bool success, all_success = TRUE;
 
 	src = t_str_new(256);
 	dest = t_str_new(256);
 
-	for (i = 0; to_utf7[i] != NULL; i += 2) {
-		str_truncate(dest, 0);
-		if (imap_utf8_to_utf7(to_utf7[i], dest) < 0)
-			success = to_utf7[i+1] == NULL;
-		else {
-			success = to_utf7[i+1] != NULL &&
-				strcmp(to_utf7[i+1], str_c(dest)) == 0;
+	test_begin("imap mutf7");
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		if (tests[i].utf8 != NULL) {
+			str_truncate(dest, 0);
+			if (imap_utf8_to_utf7(tests[i].utf8, dest) < 0)
+				test_assert(tests[i].mutf7 == NULL);
+			else
+				test_assert(null_strcmp(tests[i].mutf7, str_c(dest)) == 0);
 		}
-		if (!success) {
-			test_out(t_strdup_printf("imap_utf8_to_utf7(%d)", i/2),
-				 FALSE);
-			all_success = FALSE;
-		} else if (to_utf7[i+1] != NULL) {
+		if (tests[i].mutf7 != NULL) {
 			str_truncate(dest, 0);
-			if (imap_utf7_to_utf8(to_utf7[i+1], dest) < 0 ||
-			    strcmp(to_utf7[i], str_c(dest)) != 0) {
-				test_out(t_strdup_printf("imap_utf7_to_utf8(%d)", i/2),
-					 FALSE);
-				all_success = FALSE;
-			}
+			if (imap_utf7_to_utf8(tests[i].mutf7, dest) < 0)
+				test_assert(tests[i].utf8 == NULL);
+			else
+				test_assert(null_strcmp(tests[i].utf8, str_c(dest)) == 0);
+			test_assert(imap_utf7_is_valid(tests[i].mutf7) != (tests[i].utf8 == NULL));
 		}
 	}
-	if (all_success)
-		test_out("imap_utf8_to_utf7()", TRUE);
 
-	success = TRUE;
 	for (chr = 0xffff; chr <= 0x10010; chr++) {
 		for (i = 1; i <= 10; i++) {
 			str_truncate(src, 0);
@@ -70,28 +63,12 @@
 			orig_src = t_strdup(str_c(src));
 			str_truncate(src, 0);
 
-			if (imap_utf8_to_utf7(orig_src, dest) < 0)
-				success = FALSE;
-			else if (imap_utf7_to_utf8(str_c(dest), src) < 0)
-				success = FALSE;
-			else
-				success = strcmp(str_c(src), orig_src) == 0;
-			if (!success)
-				goto end;
+			test_assert(imap_utf8_to_utf7(orig_src, dest) == 0);
+			test_assert(imap_utf7_to_utf8(str_c(dest), src) == 0);
+			test_assert(strcmp(str_c(src), orig_src) == 0);
 		}
 	}
-end:
-	test_out("imap_utf7_to_utf8(reverse)", success);
-	for (i = 0; invalid_utf7[i] != NULL; i++) {
-		str_truncate(dest, 0);
-		if (imap_utf7_to_utf8(invalid_utf7[i], dest) == 0) {
-			test_out(t_strdup_printf("imap_utf7_to_utf8(invalid.%d)", i),
-				 FALSE);
-			all_success = FALSE;
-		}
-	}
-	if (all_success)
-		test_out("imap_utf7_to_utf8(invalid)", TRUE);
+	test_end();
 }
 
 int main(void)


More information about the dovecot-cvs mailing list