summaryrefslogtreecommitdiffstats
path: root/src/lib-imap/test-imap-utf7.c
blob: 216eebf4a7f084407a7cb9ea846c7c133641fca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "unichar.h"
#include "imap-utf7.h"
#include "test-common.h"

static void test_imap_utf7_by_example(void)
{
	static const struct test {
		const char *utf8;
		const char *mutf7;
	} tests[] = {
		{ "&&x&&", "&-&-x&-&-" },
		{ "~peter/mail/\xe5\x8f\xb0\xe5\x8c\x97/\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e",
		  "~peter/mail/&U,BTFw-/&ZeVnLIqe-" },
		{ "tiet\xc3\xa4&j\xc3\xa4&", "tiet&AOQ-&-j&AOQ-&-" }, /* & is always encoded as &- */
		{ "p\xe4\xe4", NULL },
		{ NULL, "&" },
		{ NULL, "&Jjo" },
		{ NULL, "&Jjo!" },
		{ NULL, "&U,BTFw-&ZeVnLIqe-" } /* unnecessary shift */
	};
	string_t *dest, *dest2;
	unsigned int i;

	dest = t_str_new(256);
	dest2 = t_str_new(256);

	test_begin("imap mutf7 examples");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		str_truncate(dest, 0);
		if (tests[i].utf8 != NULL) {
			if (imap_utf8_to_utf7(tests[i].utf8, dest) < 0)
				test_assert_idx(tests[i].mutf7 == NULL, i);
			else
				test_assert_idx(null_strcmp(tests[i].mutf7, str_c(dest)) == 0, i);
		} else {
			/* invalid mUTF-7 - test that escaping works */
			str_truncate(dest2, 0);
			imap_utf7_to_utf8_escaped(tests[i].mutf7, "%", dest);
			imap_escaped_utf8_to_utf7(str_c(dest), '%', dest2);
			test_assert_idx(strcmp(tests[i].mutf7, str_c(dest2)) == 0, i);
		}
		if (tests[i].mutf7 != NULL) {
			str_truncate(dest, 0);
			if (imap_utf7_to_utf8(tests[i].mutf7, dest) < 0)
				test_assert_idx(tests[i].utf8 == NULL, i);
			else
				test_assert_idx(null_strcmp(tests[i].utf8, str_c(dest)) == 0, i);
			test_assert_idx(imap_utf7_is_valid(tests[i].mutf7) != (tests[i].utf8 == NULL), i);
		}
	}

	str_truncate(dest, 0);
	imap_utf7_to_utf8_escaped(".foo%", "%.", dest);
	test_assert_strcmp(str_c(dest), "%2efoo%25");

	str_truncate(dest, 0);
	test_assert(imap_escaped_utf8_to_utf7("%foo%2ebar", '%', dest) == 0);
	test_assert_strcmp(str_c(dest), "%foo.bar");

	test_end();
}

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);
			for (j = 0; j < i; j++) {
				if (j % 3 == 0)
					str_append_c(src, 'x');
				if (j % 5 == 0)
					str_append_c(src, '&');
				uni_ucs4_to_utf8_c(chr, src);
			}

			orig_src = t_strdup(str_c(src));
			str_truncate(src, 0);

			test_assert_idx(imap_utf8_to_utf7(orig_src, dest) == 0, chr*100+i);
			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)
{
	string_t *dest, *dest2;
	unsigned int i;

	test_begin("imap mutf7 non-utf16");
	dest = t_str_new(32);
	dest2 = t_str_new(32);
	for (i = 0; i <= 255; ++i) {
		/* Invalid, code a single 8-bit octet */
		const char csrc[] = {
			'&',
			mb64[i >> 2],
			mb64[(i & 3) << 4],
			'-',
			'\0'
		};
		test_assert_idx(!imap_utf7_is_valid(csrc), i);

		/* escaping can reverse the original string */
		str_truncate(dest, 0);
		str_truncate(dest2, 0);
		imap_utf7_to_utf8_escaped(csrc, "%", dest);
		imap_escaped_utf8_to_utf7(str_c(dest), '%', dest2);
		test_assert_idx(strcmp(csrc, str_c(dest2)) == 0, i);
	}
	for (i = 0; i <= 255; ++i) {
		/* Invalid, U+00E4 followed by a single octet */
		const char csrc[] = {
			'&',
			mb64[                       (0x00 >> 2)],
			mb64[((0x00 & 0x03) << 4) | (0xe4 >> 4)],
			mb64[((0xe4 & 0x0f) << 2) | (   i >> 6)],
			mb64[     i & 0x3f                     ],
			'-',
			'\0'
		};
		test_assert_idx(!imap_utf7_is_valid(csrc), i);

		/* escaping can reverse the original string */
		str_truncate(dest, 0);
		str_truncate(dest2, 0);
		imap_utf7_to_utf8_escaped(csrc, "%", dest);
		imap_escaped_utf8_to_utf7(str_c(dest), '%', dest2);
		test_assert_idx(strcmp(csrc, str_c(dest2)) == 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), 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), 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();
}

int main(void)
{
	static void (*const test_functions[])(void) = {
		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);
}