dovecot-2.2: lib-imap: test-imap-utf7 - comprehensive test suite

dovecot at dovecot.org dovecot at dovecot.org
Mon Jul 28 13:54:30 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/6dbce666f8d3
changeset: 17659:6dbce666f8d3
user:      Phil Carmody <phil at dovecot.fi>
date:      Mon Jul 28 16:49:47 2014 +0300
description:
lib-imap: test-imap-utf7 - comprehensive test suite
Several MUST NOTs or other standards violations were not being
trapped.

test_imap_utf7_unnecessary() tests:
  Modified BASE64 MUST NOT be used to represent any printing US-ASCII
  character which can represent itself.

test_imap_utf7_bad_ascii() tests:
  All other characters (octet values 0x00-0x1f and 0x7f-0xff) are
  represented in modified BASE64, ...

test_imap_utf7_non_utf16() tests that data containing a single straggling
octet is trapped.

test_imap_utf7_by_example() and test_imap_utf7_ucs4_cases() are just
the previous test suite split into 2 smaller tests.

Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib-imap/test-imap-utf7.c |  114 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 105 insertions(+), 9 deletions(-)

diffs (156 lines):

diff -r cfaeb58a0fbf -r 6dbce666f8d3 src/lib-imap/test-imap-utf7.c
--- a/src/lib-imap/test-imap-utf7.c	Mon Jul 28 16:49:47 2014 +0300
+++ b/src/lib-imap/test-imap-utf7.c	Mon Jul 28 16:49:47 2014 +0300
@@ -6,7 +6,7 @@
 #include "imap-utf7.h"
 #include "test-common.h"
 
-static void test_imap_utf7(void)
+static void test_imap_utf7_by_example(void)
 {
 	static struct test {
 		const char *utf8;
@@ -21,15 +21,12 @@
 		{ NULL, "&Jjo!" },
 		{ NULL, "&U,BTFw-&ZeVnLIqe-" }
 	};
-	string_t *src, *dest;
-	const char *orig_src;
-	unsigned int i, j;
-	unichar_t chr;
+	string_t *dest;
+	unsigned int i;
 
-	src = t_str_new(256);
 	dest = t_str_new(256);
 
-	test_begin("imap mutf7");
+	test_begin("imap mutf7 examples");
 	for (i = 0; i < N_ELEMENTS(tests); i++) {
 		if (tests[i].utf8 != NULL) {
 			str_truncate(dest, 0);
@@ -47,8 +44,21 @@
 			test_assert_idx(imap_utf7_is_valid(tests[i].mutf7) != (tests[i].utf8 == NULL), i);
 		}
 	}
+	test_end();
+}
 
-	for (chr = 0xffff; chr <= 0x10010; chr++) {
+static void test_imap_utf7_ucs4_cases(void)
+{
+	string_t *src, *dest;
+	const char *orig_src;
+	unsigned int i, j;
+	unichar_t chr;
+
+	src = t_str_new(256);
+	dest = t_str_new(256);
+
+	test_begin("imap mutf7 ucs4 cases");
+	for (chr = 0xffff; chr <= 0x10010; chr++) T_BEGIN {
 		for (i = 1; i <= 10; i++) {
 			str_truncate(src, 0);
 			str_truncate(dest, 0);
@@ -67,6 +77,88 @@
 			test_assert_idx(imap_utf7_to_utf8(str_c(dest), src) == 0, chr*100+i);
 			test_assert_idx(strcmp(str_c(src), orig_src) == 0, chr+100+i);
 		}
+	} T_END;
+	test_end();
+}
+
+static const char mb64[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
+static void test_imap_utf7_non_utf16(void)
+{
+	char csrc[1+4+1+1];
+	unsigned int i;
+
+	test_begin("imap mutf7 non-utf16");
+	for (i = 0; i <= 255; ++i) {
+		/* Invalid, code a single 8-bit octet */
+		csrc[2] = '&';
+		csrc[3] = mb64[i>>2];
+		csrc[4] = mb64[(i&3) << 4];
+		csrc[5] = '-';
+		csrc[6] = '\0';
+		test_assert_idx(imap_utf7_is_valid(csrc+2) == 0, i);
+	}
+	for (i = 0; i <= 255; ++i) {
+		/* Invalid, U+00E4 followed by a single octet */
+		csrc[0] = '&';
+		csrc[1] = mb64[                       (0x00 >> 2)];
+		csrc[2] = mb64[((0x00 & 0x03) << 4) | (0xe4 >> 4)];
+		csrc[3] = mb64[((0xe4 & 0x0f) << 2) | (   i >> 6)];
+		csrc[4] = mb64[     i & 0x3f                     ];
+		test_assert_idx(imap_utf7_is_valid(csrc) == 0, i);
+	}
+	test_end();
+}
+
+static void test_imap_utf7_bad_ascii(void)
+{
+	string_t *dest;
+	char csrc[1+1];
+	unsigned int i;
+
+	dest = t_str_new(256);
+
+	test_begin("imap mutf7 bad ascii");
+	for (i = 1; i <= 0x7f; ++i) {
+		if (i == ' ')
+			i = 0x7f;
+		csrc[0] = i;
+		csrc[1] = '\0';
+		test_assert_idx(imap_utf7_is_valid(csrc) == 0, i);
+		str_truncate(dest, 0);
+		test_assert_idx(imap_utf7_to_utf8(csrc, dest) < 0, i);
+	}
+	test_end();
+}
+
+static void test_imap_utf7_unnecessary(void)
+{
+	string_t *dest;
+	char csrc[1+3+1+1];
+	unsigned int i;
+
+	dest = t_str_new(256);
+
+	test_begin("imap mutf7 unnecessary");
+	for (i = 0x20; i < 0x7f; ++i) {
+		/* Create an invalid escaped encoding of a simple char or '&' */
+		csrc[0] = '&';
+		csrc[1] = mb64[                       (0x00 >> 2)];
+		csrc[2] = mb64[((0x00 & 0x03) << 4) | (   i >> 4)];
+		csrc[3] = mb64[((   i & 0x0f) << 2) |     0      ];
+		csrc[4] = '-';
+		csrc[5] = '\0';
+		test_assert_idx(imap_utf7_is_valid(csrc) == 0, i);
+		str_truncate(dest, 0);
+		test_assert_idx(imap_utf7_to_utf8(csrc, dest) < 0, i);
+
+		/* All self-coding characters must self-code */
+		if (i == '&')
+			continue;
+		csrc[0] = i;
+		csrc[1] = '\0';
+		str_truncate(dest, 0);
+		test_assert_idx(imap_utf8_to_utf7(csrc, dest) >= 0, i);
+		test_assert_idx(strcmp(csrc, str_c(dest)) == 0, i);
 	}
 	test_end();
 }
@@ -74,7 +166,11 @@
 int main(void)
 {
 	static void (*test_functions[])(void) = {
-		test_imap_utf7,
+		test_imap_utf7_by_example,
+		test_imap_utf7_ucs4_cases,
+		test_imap_utf7_non_utf16,
+		test_imap_utf7_bad_ascii,
+		test_imap_utf7_unnecessary,
 		NULL
 	};
 	return test_run(test_functions);


More information about the dovecot-cvs mailing list