dovecot-2.2: lib: test printf_format_fix()

dovecot at dovecot.org dovecot at dovecot.org
Fri Jun 27 13:22:41 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/2c0b4244b935
changeset: 17527:2c0b4244b935
user:      Phil Carmody <phil at dovecot.fi>
date:      Fri Jun 27 16:12:40 2014 +0300
description:
lib: test printf_format_fix()
Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib/Makefile.am              |   1 +
 src/lib/test-lib.c               |   1 +
 src/lib/test-lib.h               |   1 +
 src/lib/test-printf-format-fix.c |  96 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 99 insertions(+), 0 deletions(-)

diffs (133 lines):

diff -r 77f21248d29e -r 2c0b4244b935 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Fri Jun 27 16:11:40 2014 +0300
+++ b/src/lib/Makefile.am	Fri Jun 27 16:12:40 2014 +0300
@@ -296,6 +296,7 @@
 	test-numpack.c \
 	test-ostream-file.c \
 	test-primes.c \
+	test-printf-format-fix.c \
 	test-priorityq.c \
 	test-seq-range-array.c \
 	test-str.c \
diff -r 77f21248d29e -r 2c0b4244b935 src/lib/test-lib.c
--- a/src/lib/test-lib.c	Fri Jun 27 16:11:40 2014 +0300
+++ b/src/lib/test-lib.c	Fri Jun 27 16:12:40 2014 +0300
@@ -31,6 +31,7 @@
 		test_numpack,
 		test_ostream_file,
 		test_primes,
+		test_printf_format_fix,
 		test_priorityq,
 		test_seq_range_array,
 		test_str,
diff -r 77f21248d29e -r 2c0b4244b935 src/lib/test-lib.h
--- a/src/lib/test-lib.h	Fri Jun 27 16:11:40 2014 +0300
+++ b/src/lib/test-lib.h	Fri Jun 27 16:12:40 2014 +0300
@@ -30,6 +30,7 @@
 void test_numpack(void);
 void test_ostream_file(void);
 void test_primes(void);
+void test_printf_format_fix(void);
 void test_priorityq(void);
 void test_seq_range_array(void);
 void test_str(void);
diff -r 77f21248d29e -r 2c0b4244b935 src/lib/test-printf-format-fix.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-printf-format-fix.c	Fri Jun 27 16:12:40 2014 +0300
@@ -0,0 +1,96 @@
+/* Copyright (c) 2001-2014 Dovecot authors, see the included COPYING file */
+
+/* Unit tests for printf-format-fix helper */
+
+#include "test-lib.h"
+#include "printf-format-fix.h"
+
+#include <string.h>
+
+struct format_fix_rewrites {
+	const char *input;
+	const char *output;
+	size_t      length;
+};
+
+static void test_unchanged()
+{
+	static const char *tests[] = {
+		"Hello world",
+		"Embedded %%, %u, %f, etc. are OK",
+		"%%doesn't cause confusion in %%m and %%n",
+	};
+	unsigned int i;
+
+	test_begin("printf_format_fix(safe)");
+	for (i = 0; i < N_ELEMENTS(tests); ++i) {
+		unsigned int len;
+		T_BEGIN {
+			test_assert_idx(printf_format_fix(tests[i])
+					== tests[i], i);
+			test_assert_idx(printf_format_fix_get_len(tests[i], &len)
+					== tests[i], i);
+			test_assert_idx(len == strlen(tests[i]), i);
+		} T_END;
+	}
+	test_end();
+}
+
+static void test_ok_changes()
+{
+	static const char *tests[] = {
+		"OK to have a trailing %m",
+		"%m can appear at the start too",
+		"Even %m in the middle with a confusing %%m elsewhere is OK",
+	};
+	unsigned int i;
+	const char *needle;
+	unsigned int needlen;
+	int old_errno = errno;
+
+	test_begin("printf_format_fix(rewrites)");
+
+	errno = EINVAL;
+	needle = strerror(errno);
+	test_assert(needle != NULL);
+	needlen = strlen(needle);
+
+	for (i = 0; i < N_ELEMENTS(tests); ++i) {
+		unsigned int len;
+		char const *chgd;
+		char const *insert;
+		unsigned int offs;
+
+		T_BEGIN {
+			chgd = printf_format_fix(tests[i]);
+			test_assert_idx(chgd != tests[i], i);
+			insert = strstr(chgd, needle);
+			test_assert_idx(insert != NULL, i);
+			offs = insert - chgd;
+			test_assert_idx(memcmp(chgd, tests[i], offs) == 0, i);
+			test_assert_idx(memcmp(chgd+offs, needle, needlen) == 0, i);
+			test_assert_idx(strcmp(chgd+offs+needlen, tests[i]+offs+2) == 0, i);
+
+			chgd = printf_format_fix_get_len(tests[i], &len);
+			test_assert_idx(chgd != tests[i], i);
+			test_assert_idx(len == strlen(chgd), i);
+			insert = strstr(chgd, needle);
+			test_assert_idx(insert != NULL, i);
+			offs = insert - chgd;
+			test_assert_idx(memcmp(chgd, tests[i], offs) == 0, i);
+			test_assert_idx(memcmp(chgd+offs, needle, needlen) == 0, i);
+			test_assert_idx(memcmp(chgd+offs+needlen, tests[i]+offs+2, len-needlen-offs) == 0, i);
+		} T_END;
+	}
+
+	errno = old_errno;
+
+	test_end();
+}
+
+void test_printf_format_fix()
+{
+	test_unchanged();
+	test_ok_changes();
+	/* want to test the panics too */
+}


More information about the dovecot-cvs mailing list