1--TEST--
2Exhaustive test of mUTF-7 (IMAP) encoding verification and conversion
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7include('encoding_tests.inc');
8mb_substitute_character(0x25); // '%'
9
10function utf16BE($utf8) {
11	return mb_convert_encoding($utf8, 'UTF-16BE', 'UTF-8');
12}
13
14function mBase64($str) {
15	return str_replace('=', '', str_replace('/', ',', base64_encode($str)));
16}
17
18function testValid($from, $to, $bothWays = true) {
19	testValidString($from, $to, 'UTF7-IMAP', 'UTF-8', $bothWays);
20}
21function testInvalid($from, $to) {
22	testInvalidString($from, $to, 'UTF7-IMAP', 'UTF-8');
23}
24
25/* An empty string is valid */
26testValid("", "");
27echo "Identification passes on empty string... good start!\n";
28
29/* Identification and conversion of ASCII characters (minus &) */
30for ($i = 0x20; $i <= 0x7E; $i++) {
31	if ($i == 0x26) // '&'
32		continue;
33	testValid(chr($i), chr($i));
34}
35echo "Testing all valid single-character ASCII strings... check!\n";
36
37/* Identification and conversion of non-ASCII characters */
38for ($i = 0; $i < 0x20; $i++)
39	testInvalid(chr($i), "%");
40for ($i = 0x7F; $i < 256; $i++)
41	testInvalid(chr($i), "%");
42echo "Non-ASCII characters convert to illegal char marker... yes!\n";
43
44/* Identification of '&' when Base-64 encoded */
45testValid("&" . mBase64(utf16BE("&")) . "-", "&", false);
46echo "& can be Base64-encoded... yes!\n";
47
48/* Identification of unterminated & section */
49identifyInvalidString("&", 'UTF7-IMAP');
50identifyInvalidString("abc&", 'UTF7-IMAP');
51identifyInvalidString("&" . mBase64(utf16BE("ハムサンドイッチ")), 'UTF7-IMAP');
52echo "Testing unterminated & sections... yep!\n";
53
54/* Identification of null shifts (& immediately after -)
55 *
56 * This is illegal according to the spec for mUTF-7 (IMAP), but currently we are letting
57 * it pass... among other things, this makes it possible to concatenate UTF-7-IMAP
58 * strings naively without the concatenated strings being treated as 'invalid'
59 *
60 * If ever we want to enforce this part of the spec, uncomment the following test */
61/*
62identifyInvalidString("&" . mBase64(utf16BE("肉包子")) . "-&" . mBase64(utf16BE("冰淇淋")) . "-", 'UTF7-IMAP');
63echo "Testing consecutive & sections which should have been merged... yep!\n";
64*/
65
66/* Conversion of Base64-encoded ASCII characters (excluding &)
67 * These should be treated as erroneous and mb_substitute_character should apply */
68for ($i = 0x20; $i <= 0x7E; $i++) {
69	if ($i == 0x26) // '&'
70		continue;
71	testInvalid("&" . mBase64(utf16BE(chr($i))) . "-", "%");
72}
73echo "Testing ASCII characters which are Base64-encoded... great!\n";
74
75/* Conversion of & encoded as &- */
76testValid("&-", "&");
77testValid("abc&-", "abc&");
78testValid("&-.&-", "&.&");
79echo "Testing valid strings which use '&-' for '&'... good!\n";
80
81/* Identification of & sections containing non-Base64 */
82
83/* We'll use 6 character strings as a test, since 6 UTF-16 characters is just enough
84 * to fit perfectly in Base64 encoding, with no padding */
85$testString = mBase64(utf16BE("我是打酱油的"));
86if (strlen($testString) != 16)
87	die("Erk!!");
88for ($i = 0; $i < 256; $i++) {
89	if ($i >= 0x30 && $i <= 0x39) // '0'..'9'
90		continue;
91	if ($i >= 0x41 && $i <= 0x5A) // 'A'..'Z'
92		continue;
93	if ($i >= 0x61 && $i <= 0x7A) // 'a'..'z'
94		continue;
95	if ($i == 0x2B || $i == 0x2C) // '+' or ','
96		continue;
97	if ($i == 0x2D) // '-'... this will be interpreted as ending the Base64 section
98		continue;
99	identifyInvalidString("&" . substr($testString, 0, 11) . chr($i) . "-", 'UTF7-IMAP');
100}
101echo "Identification fails when Base64 sections contain non-Base64 bytes... right!\n";
102
103/* Tell me, please, how many ways can UTF-16BE text get messed up?
104 * Why, that's elementary... */
105
106/* 1. The second half of a surrogate pair could come first, */
107$testString = mb_convert_encoding("\x00\x01\x04\x00", 'UTF-16BE', 'UTF-32BE');
108if (strlen($testString) != 4)
109	die("Ouch!");
110$testString = substr($testString, 2, 2) . substr($testString, 0, 2);
111identifyInvalidString("&" . mBase64($testString) . "-", 'UTF7-IMAP');
112/* (Or could appear by itself) */
113$testString = substr($testString, 0, 2);
114identifyInvalidString("&" . mBase64($testString) . "-", 'UTF7-IMAP');
115
116/* ...and we should detect this wherever it occurs */
117$singleChar = mb_convert_encoding("1", 'UTF-16BE', 'ASCII');
118$doubleChar = mb_convert_encoding("\x00\x01\x04\x01", 'UTF-16BE', 'UTF-32BE');
119if (strlen($doubleChar) != 4)
120	die("That was supposed to be a surrogate pair");
121identifyInvalidString("&" . mBase64($singleChar . $testString) . "-", 'UTF7-IMAP');
122identifyInvalidString("&" . mBase64($singleChar . $singleChar . $testString) . "-", 'UTF7-IMAP');
123identifyInvalidString("&" . mBase64($singleChar . $singleChar . $singleChar . $testString) . "-", 'UTF7-IMAP');
124identifyInvalidString("&" . mBase64($doubleChar . $testString) . "-", 'UTF7-IMAP');
125identifyInvalidString("&" . mBase64($singleChar . $doubleChar . $testString) . "-", 'UTF7-IMAP');
126identifyInvalidString("&" . mBase64($singleChar . $singleChar . $doubleChar . $testString) . "-", 'UTF7-IMAP');
127
128/* 2. The first half of a surrogate pair might be followed by an invalid 2nd part, */
129$testString = mb_convert_encoding("\x00\x01\x04\x00", 'UTF-16BE', 'UTF-32BE');
130$testString = substr($testString, 0, 2) . mb_convert_encoding("a", 'UTF-16BE', 'ASCII');
131identifyInvalidString("&" . mBase64($testString) . "-", 'UTF7-IMAP');
132
133/* ...and we should also detect that wherever it occurs... */
134identifyInvalidString("&" . mBase64($singleChar . $testString) . "-", 'UTF7-IMAP');
135identifyInvalidString("&" . mBase64($singleChar . $singleChar . $testString) . "-", 'UTF7-IMAP');
136identifyInvalidString("&" . mBase64($doubleChar . $testString) . "-", 'UTF7-IMAP');
137
138/* 3. The first half of a surrogate pair could come at the end of the string, */
139$testString = mb_convert_encoding("\x00\x01\x04\x00", 'UTF-16BE', 'UTF-32BE');
140identifyInvalidString("&" . mBase64(substr($testString, 0, 2)) . "-", 'UTF7-IMAP');
141identifyInvalidString("&" . mBase64($singleChar . substr($testString, 0, 2)) . "-", 'UTF7-IMAP');
142identifyInvalidString("&" . mBase64($singleChar . $singleChar . substr($testString, 0, 2)) . "-", 'UTF7-IMAP');
143
144/* 4. Or, it could have an odd number of bytes in it! */
145$testString = utf16BE("ドーナツ");
146$testString = substr($testString, 0, strlen($testString) - 1);
147identifyInvalidString("&" . mBase64($testString) . "-", 'UTF7-IMAP');
148
149/* And there is one bonus way to discombobulate your UTF-16BE when it is Base64-encoded...
150 * The Base64 might not decode to an integral number of bytes
151 * Or, equivalently... it might not be padded with zeroes (as the RFC requires) */
152$testString = utf16BE("☺⛑");
153if (strlen($testString) != 4)
154	die("No good");
155$encoded = mBase64($testString);
156if (strlen($encoded) != 6)
157	die("Don't like that");
158/* Mess up the padding by replacing the last Base64 character with ',',
159 * which represents 63 (a number with a 1 in the last bit) */
160identifyInvalidString("&" . substr($encoded, 0, strlen($encoded) - 1) . ",-", 'UTF7-IMAP');
161
162echo "Identification fails when UTF-16 text is invalid... no sweat!\n";
163
164/* OK, let's try valid Base64-encoded text now */
165
166/* 2-byte char */
167testValid("&" . mBase64(utf16BE("☺")) . "-", "☺");
168/* 2 + 2 */
169testValid("&" . mBase64(utf16BE("饺子")) . "-", "饺子");
170/* 2 + 2 + 2 */
171testValid("&" . mBase64(utf16BE("123")) . "-", "123");
172/* 2 + 2 + 2 + 2 */
173testValid("&" . mBase64(utf16BE("ᄚᄆᄇᄈ")) . "-", "ᄚᄆᄇᄈ");
174/* 4 */
175$longChar1 = mb_convert_encoding("\x00\x01\x04\x01", 'UTF-16BE', 'UTF-32BE');
176$longChar2 = mb_convert_encoding("\x00\x01\x04\x01", 'UTF-8', 'UTF-32BE');
177testValid("&" . mBase64($longChar1) . "-", $longChar2);
178/* 2 + 4 */
179testValid("&" . mBase64(utf16BE("饼") . $longChar1) . "-", "饼" . $longChar2);
180/* 4 + 2 */
181testValid("&" . mBase64($longChar1 . utf16BE("饼")) . "-", $longChar2 . "饼");
182/* 2 + 4 + 2 */
183testValid("&" . mBase64(utf16BE("☺") . $longChar1 . utf16BE("饼")) . "-", "☺" . $longChar2 . "饼");
184/* 2 + 2 + 4 */
185testValid("&" . mBase64(utf16BE("西瓜") . $longChar1) . "-", "西瓜" . $longChar2);
186/* 2 + 2 + 4 + 2 */
187testValid("&" . mBase64(utf16BE("西瓜") . $longChar1 . utf16BE("☺")) . "-", "西瓜" . $longChar2 . "☺");
188/* 2 + 2 + 4 + 4 */
189testValid("&" . mBase64(utf16BE("西瓜") . $longChar1 . $longChar1) . "-", "西瓜" . $longChar2 . $longChar2);
190/* 2 + 2 + 2 + 4 */
191testValid("&" . mBase64(utf16BE("西红柿") . $longChar1) . "-", "西红柿" . $longChar2);
192
193/* Multiple sections of valid ASCII _and_ Base64-encoded text */
194testValid("123&" . mBase64(utf16BE("123")) . "-abc&" . mBase64(utf16BE("☺")) . "-.", "123123abc☺.");
195
196/* If a & character appears right after a non-ASCII character, we must first close the Base64
197 * section and then emit &- */
198testValidString("☺&", "&Jjo-&-", "UTF-8", "UTF7-IMAP", false);
199testValidString("西瓜&", "&iX903A-&-", "UTF-8", "UTF7-IMAP", false);
200testValidString("西红柿&", "&iX9+omf,-&-", "UTF-8", "UTF7-IMAP", false);
201
202echo "Identification and conversion of valid text is working... perfect!\n";
203
204// Try illegal Unicode codepoint (> 0x10FFFF)
205convertInvalidString("\x00\x20\x00\x00", "%", "UCS-4BE", "UTF7-IMAP");
206
207// Test "long" illegal character markers
208mb_substitute_character("long");
209convertInvalidString("\x10", "%", "UTF7-IMAP", "UTF-8");
210convertInvalidString("\x80", "%", "UTF7-IMAP", "UTF-8");
211convertInvalidString("abc&", "abc%", "UTF7-IMAP", "UTF-8"); // The & starts a Base-64 coded section, which is OK... but there's no data in it
212convertInvalidString("&**-", "%*-", "UTF7-IMAP", "UTF-8"); // When we hit the first bad byte in a Base-64 coded section, it drops us back into the default mode, so the following characters are literal
213
214echo "Done!\n";
215?>
216--EXPECT--
217Identification passes on empty string... good start!
218Testing all valid single-character ASCII strings... check!
219Non-ASCII characters convert to illegal char marker... yes!
220& can be Base64-encoded... yes!
221Testing unterminated & sections... yep!
222Testing ASCII characters which are Base64-encoded... great!
223Testing valid strings which use '&-' for '&'... good!
224Identification fails when Base64 sections contain non-Base64 bytes... right!
225Identification fails when UTF-16 text is invalid... no sweat!
226Identification and conversion of valid text is working... perfect!
227Done!
228