|
ferencd@0
|
1 //
|
|
ferencd@0
|
2 // VMime library (http://www.vmime.org)
|
|
ferencd@0
|
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
|
|
ferencd@0
|
4 //
|
|
ferencd@0
|
5 // This program is free software; you can redistribute it and/or
|
|
ferencd@0
|
6 // modify it under the terms of the GNU General Public License as
|
|
ferencd@0
|
7 // published by the Free Software Foundation; either version 3 of
|
|
ferencd@0
|
8 // the License, or (at your option) any later version.
|
|
ferencd@0
|
9 //
|
|
ferencd@0
|
10 // This program is distributed in the hope that it will be useful,
|
|
ferencd@0
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
ferencd@0
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
ferencd@0
|
13 // General Public License for more details.
|
|
ferencd@0
|
14 //
|
|
ferencd@0
|
15 // You should have received a copy of the GNU General Public License along
|
|
ferencd@0
|
16 // with this program; if not, write to the Free Software Foundation, Inc.,
|
|
ferencd@0
|
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
ferencd@0
|
18 //
|
|
ferencd@0
|
19 // Linking this library statically or dynamically with other modules is making
|
|
ferencd@0
|
20 // a combined work based on this library. Thus, the terms and conditions of
|
|
ferencd@0
|
21 // the GNU General Public License cover the whole combination.
|
|
ferencd@0
|
22 //
|
|
ferencd@0
|
23
|
|
ferencd@0
|
24 #include "tests/testUtils.hpp"
|
|
ferencd@0
|
25
|
|
ferencd@0
|
26 #include "encoderTestUtils.hpp"
|
|
ferencd@0
|
27
|
|
ferencd@0
|
28
|
|
ferencd@0
|
29 VMIME_TEST_SUITE_BEGIN(qpEncoderTest)
|
|
ferencd@0
|
30
|
|
ferencd@0
|
31 VMIME_TEST_LIST_BEGIN
|
|
ferencd@0
|
32 VMIME_TEST(testQuotedPrintable)
|
|
ferencd@0
|
33 VMIME_TEST(testQuotedPrintable_SoftLineBreaks)
|
|
ferencd@0
|
34 VMIME_TEST(testQuotedPrintable_HardLineBreakEncode)
|
|
ferencd@0
|
35 VMIME_TEST(testQuotedPrintable_HardLineBreakDecode)
|
|
ferencd@0
|
36 VMIME_TEST(testQuotedPrintable_CRLF)
|
|
ferencd@0
|
37 VMIME_TEST(testQuotedPrintable_RFC2047)
|
|
ferencd@0
|
38 VMIME_TEST_LIST_END
|
|
ferencd@0
|
39
|
|
ferencd@0
|
40
|
|
ferencd@0
|
41 void testQuotedPrintable()
|
|
ferencd@0
|
42 {
|
|
ferencd@0
|
43 static const vmime::string testSuites[] =
|
|
ferencd@0
|
44 {
|
|
ferencd@0
|
45 // Test 1
|
|
ferencd@0
|
46 "",
|
|
ferencd@0
|
47
|
|
ferencd@0
|
48 "",
|
|
ferencd@0
|
49
|
|
ferencd@0
|
50 // Test 2
|
|
ferencd@0
|
51 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
ferencd@0
|
52
|
|
ferencd@0
|
53 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
ferencd@0
|
54
|
|
ferencd@0
|
55 // Test 3
|
|
ferencd@0
|
56 "0123456789012345678901234567890123456789012345678901234567890123456789012"
|
|
ferencd@0
|
57 "3456789012345678901234567890123456789012345678901234567890123456789012345"
|
|
ferencd@0
|
58 "6789",
|
|
ferencd@0
|
59
|
|
ferencd@0
|
60 "0123456789012345678901234567890123456789012345678901234567890123456789012=\r\n"
|
|
ferencd@0
|
61 "3456789012345678901234567890123456789012345678901234567890123456789012345=\r\n"
|
|
ferencd@0
|
62 "6789",
|
|
ferencd@0
|
63
|
|
ferencd@0
|
64 // Test 4
|
|
ferencd@0
|
65 vmime::string(
|
|
ferencd@0
|
66 "\x89\xe8\x24\x04\x2f\xe8\xff\xfb\xeb\xff\x90\xd7\x74\x8d\x00\x26\x89\x55"
|
|
ferencd@0
|
67 "\x83\xe5\x08\xec\x04\xc7\xa0\x24\x05\xa2\xe8\x08\x43\xee\x00\x00\xc0\x85"
|
|
ferencd@0
|
68 "\x0a\x74\xec\x89\xc3\x5d\xb6\x8d\x00\x00\x00\x00\x04\xc7\xa8\x24\x05\xa2"
|
|
ferencd@0
|
69 "\xe8\x08\x43\xd4\x00\x00\xe8\xeb\xf6\x89\x89\x55\x81\xe5\xa8\xec\x00\x00"
|
|
ferencd@0
|
70 "\x89\x00\xfc\x75\x75\x8b\x89\x08\xf8\x5d\xb9\xe8\xff\xff\x83\xff\x14\xfe"
|
|
ferencd@0
|
71 "\x47\x74\xc0\x31\x9d\x8d\xff\x68\xff\xff\x85\x89\xff\x68\xff\xff\x85\x8d"
|
|
ferencd@0
|
72 "\xff\x6c\xff\xff\x04\x89\xe8\x24\xfa\x50\xff\xff\x45\xc7\x00\xec\x00\x00"
|
|
ferencd@0
|
73 "\x31\x00\x89\xc0\x24\x44\x89\x08\x24\x5c\x89\x04\x24\x34\x87\xe8\xff\xf6"
|
|
ferencd@0
|
74 "\x89\xff\x24\x34\x2f\xe8\xff\xf9\x8b\xff\xf8\x5d\x75\x8b\x89\xfc\x5d\xec"
|
|
ferencd@0
|
75 "\xbe\xc3\x00\x13\x00\x00\xe7\xeb\xb6\x8d\x00\x00\x00\x00\x89\x55\x57\xe5"
|
|
ferencd@0
|
76 "\x53\x56\xec\x81\x01\xdc\x00\x00\x45\xbb\x05\x5c\x8b\x08\x0c\x55\xe4\x83"
|
|
ferencd@0
|
77 "\x8b\xf0\x89\x02\x24\x5c\xc7\x04\x24\x04\x00\x06\x00\x00\xec\xa3\x05\xa9"
|
|
ferencd@0
|
78 "\xe8\x08\xf7\x2a\xff\xff\x04\xc7\x46\x24\x05\x5c\xb9\x08\x5c\x50\x08\x05"
|
|
ferencd@0
|
79 "\x4c\x89\x04\x24\xf5\xe8\xff\xf7\xc7\xff\x24\x04\x5c\x46\x08\x05\xe9\xe8"
|
|
ferencd@0
|
80 "\xff\xf8\xc7\xff\x24\x04\x1d\x70\x08\x05\x55\xe8\x00\xbb\xb8\x00\x00\x01"
|
|
ferencd@0
|
81 "\x00\x00\xd2\x31\x08\xa3\x05\xa7\xb8\x08\x00\x01\x00\x00\x0c\xa3\x05\xa7",
|
|
ferencd@0
|
82 18 * 16),
|
|
ferencd@0
|
83
|
|
ferencd@0
|
84 "=89=E8$=04/=E8=FF=FB=EB=FF=90=D7t=8D=00&=89U=83=E5=08=EC=04=C7=A0$=05=A2=E8=\r\n"
|
|
ferencd@0
|
85 "=08C=EE=00=00=C0=85=0At=EC=89=C3]=B6=8D=00=00=00=00=04=C7=A8$=05=A2=E8=08=\r\n"
|
|
ferencd@0
|
86 "C=D4=00=00=E8=EB=F6=89=89U=81=E5=A8=EC=00=00=89=00=FCuu=8B=89=08=F8]=B9=E8=\r\n"
|
|
ferencd@0
|
87 "=FF=FF=83=FF=14=FEGt=C01=9D=8D=FFh=FF=FF=85=89=FFh=FF=FF=85=8D=FFl=FF=FF=04=\r\n"
|
|
ferencd@0
|
88 "=89=E8$=FAP=FF=FFE=C7=00=EC=00=001=00=89=C0$D=89=08$\\=89=04$4=87=E8=FF=F6=\r\n"
|
|
ferencd@0
|
89 "=89=FF$4/=E8=FF=F9=8B=FF=F8]u=8B=89=FC]=EC=BE=C3=00=13=00=00=E7=EB=B6=8D=00=\r\n"
|
|
ferencd@0
|
90 "=00=00=00=89UW=E5SV=EC=81=01=DC=00=00E=BB=05\\=8B=08=0CU=E4=83=8B=F0=89=02=\r\n"
|
|
ferencd@0
|
91 "$\\=C7=04$=04=00=06=00=00=EC=A3=05=A9=E8=08=F7*=FF=FF=04=C7F$=05\\=B9=08\\P=08=\r\n"
|
|
ferencd@0
|
92 "=05L=89=04$=F5=E8=FF=F7=C7=FF$=04\\F=08=05=E9=E8=FF=F8=C7=FF$=04=1Dp=08=05=\r\n"
|
|
ferencd@0
|
93 "U=E8=00=BB=B8=00=00=01=00=00=D21=08=A3=05=A7=B8=08=00=01=00=00=0C=A3=05=A7=\r\n"
|
|
ferencd@0
|
94 };
|
|
ferencd@0
|
95
|
|
ferencd@0
|
96
|
|
ferencd@0
|
97 for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i)
|
|
ferencd@0
|
98 {
|
|
ferencd@0
|
99 const vmime::string decoded = testSuites[i * 2];
|
|
ferencd@0
|
100 const vmime::string encoded = testSuites[i * 2 + 1];
|
|
ferencd@0
|
101
|
|
ferencd@0
|
102 std::ostringstream oss;
|
|
ferencd@0
|
103 oss << "[QP] Test " << (i + 1) << ": ";
|
|
ferencd@0
|
104
|
|
ferencd@0
|
105 // Encoding
|
|
ferencd@0
|
106 VASSERT_EQ(oss.str() + "encoding", encoded, encode("quoted-printable", decoded, 74));
|
|
ferencd@0
|
107
|
|
ferencd@0
|
108 // Decoding
|
|
ferencd@0
|
109 VASSERT_EQ(oss.str() + "decoding", decoded, decode("quoted-printable", encoded, 74));
|
|
ferencd@0
|
110
|
|
ferencd@0
|
111 // Multiple and successive encoding/decoding
|
|
ferencd@0
|
112 VASSERT_EQ(oss.str() + "multiple1", decoded,
|
|
ferencd@0
|
113 decode("quoted-printable",
|
|
ferencd@0
|
114 encode("quoted-printable", decoded)));
|
|
ferencd@0
|
115
|
|
ferencd@0
|
116 VASSERT_EQ(oss.str() + "multiple2", decoded,
|
|
ferencd@0
|
117 decode("quoted-printable",
|
|
ferencd@0
|
118 decode("quoted-printable",
|
|
ferencd@0
|
119 encode("quoted-printable",
|
|
ferencd@0
|
120 encode("quoted-printable", decoded)))));
|
|
ferencd@0
|
121
|
|
ferencd@0
|
122 VASSERT_EQ(oss.str() + "multiple3", decoded,
|
|
ferencd@0
|
123 decode("quoted-printable",
|
|
ferencd@0
|
124 decode("quoted-printable",
|
|
ferencd@0
|
125 decode("quoted-printable",
|
|
ferencd@0
|
126 encode("quoted-printable",
|
|
ferencd@0
|
127 encode("quoted-printable",
|
|
ferencd@0
|
128 encode("quoted-printable", decoded)))))));
|
|
ferencd@0
|
129
|
|
ferencd@0
|
130 VASSERT_EQ(oss.str() + "multiple4", decoded,
|
|
ferencd@0
|
131 decode("quoted-printable",
|
|
ferencd@0
|
132 decode("quoted-printable",
|
|
ferencd@0
|
133 decode("quoted-printable",
|
|
ferencd@0
|
134 decode("quoted-printable",
|
|
ferencd@0
|
135 encode("quoted-printable",
|
|
ferencd@0
|
136 encode("quoted-printable",
|
|
ferencd@0
|
137 encode("quoted-printable",
|
|
ferencd@0
|
138 encode("quoted-printable", decoded)))))))));
|
|
ferencd@0
|
139
|
|
ferencd@0
|
140 VASSERT(oss.str() + "encoded size",
|
|
ferencd@0
|
141 getEncoder("quoted-printable")->getEncodedSize(decoded.length())
|
|
ferencd@0
|
142 >= encode("quoted-printable", decoded).length());
|
|
ferencd@0
|
143
|
|
ferencd@0
|
144 VASSERT(oss.str() + "decoded size",
|
|
ferencd@0
|
145 getEncoder("quoted-printable")->getDecodedSize(encoded.length())
|
|
ferencd@0
|
146 >= decode("quoted-printable", encoded).length());
|
|
ferencd@0
|
147 }
|
|
ferencd@0
|
148 }
|
|
ferencd@0
|
149
|
|
ferencd@0
|
150 /** Tests Soft Line Breaks (RFC-2047/6.7(5). */
|
|
ferencd@0
|
151 void testQuotedPrintable_SoftLineBreaks()
|
|
ferencd@0
|
152 {
|
|
ferencd@0
|
153 VASSERT_EQ("1", "Now's the time=\r\n"
|
|
ferencd@0
|
154 " for all folk =\r\n"
|
|
ferencd@0
|
155 "to come to the=\r\n"
|
|
ferencd@0
|
156 " aid of their =\r\n"
|
|
ferencd@0
|
157 "country.",
|
|
ferencd@0
|
158 encode("quoted-printable", "Now's the time for all folk "
|
|
ferencd@0
|
159 "to come to the aid of their country.", 15));
|
|
ferencd@0
|
160 }
|
|
ferencd@0
|
161
|
|
ferencd@0
|
162 void testQuotedPrintable_HardLineBreakEncode()
|
|
ferencd@0
|
163 {
|
|
ferencd@0
|
164 const std::string data =
|
|
ferencd@0
|
165 "If you believe that truth=beauty,"
|
|
ferencd@0
|
166 " then surely mathematics\r\nis the most"
|
|
ferencd@0
|
167 " beautiful branch of philosophy.";
|
|
ferencd@0
|
168
|
|
ferencd@0
|
169 const std::string expected =
|
|
ferencd@0
|
170 "If you believe that truth=3Dbeauty=\r\n"
|
|
ferencd@0
|
171 ", then surely mathematics\r\n"
|
|
ferencd@0
|
172 "is the most beautiful branch of ph=\r\n"
|
|
ferencd@0
|
173 "ilosophy.";
|
|
ferencd@0
|
174
|
|
ferencd@0
|
175 vmime::propertySet encProps;
|
|
ferencd@0
|
176 encProps["text"] = true;
|
|
ferencd@0
|
177
|
|
ferencd@0
|
178 VASSERT_EQ("1", expected, encode("quoted-printable", data, 35, encProps));
|
|
ferencd@0
|
179 }
|
|
ferencd@0
|
180
|
|
ferencd@0
|
181 void testQuotedPrintable_HardLineBreakDecode()
|
|
ferencd@0
|
182 {
|
|
ferencd@0
|
183 const std::string expected =
|
|
ferencd@0
|
184 "If you believe that truth=beauty,"
|
|
ferencd@0
|
185 " then surely mathematics\r\nis the most"
|
|
ferencd@0
|
186 " beautiful branch of philosophy.";
|
|
ferencd@0
|
187
|
|
ferencd@0
|
188 const std::string data =
|
|
ferencd@0
|
189 "If you believe that truth=3Dbeauty=\r\n"
|
|
ferencd@0
|
190 ", then surely mathematics\r\n"
|
|
ferencd@0
|
191 "is the most beautiful branch of ph=\r\n"
|
|
ferencd@0
|
192 "ilosophy.";
|
|
ferencd@0
|
193
|
|
ferencd@0
|
194 VASSERT_EQ("1", expected, decode("quoted-printable", data, 35));
|
|
ferencd@0
|
195 }
|
|
ferencd@0
|
196
|
|
ferencd@0
|
197
|
|
ferencd@0
|
198 /** In text mode, ensure line breaks in QP-encoded text are represented
|
|
ferencd@0
|
199 * by a CRLF sequence, as per RFC-2047/6.7(4). */
|
|
ferencd@0
|
200 void testQuotedPrintable_CRLF()
|
|
ferencd@0
|
201 {
|
|
ferencd@0
|
202 vmime::propertySet encProps;
|
|
ferencd@0
|
203
|
|
ferencd@0
|
204 // in "text" mode
|
|
ferencd@0
|
205 encProps["text"] = true;
|
|
ferencd@0
|
206 VASSERT_EQ("text", "line1\r\nline2",
|
|
ferencd@0
|
207 encode("quoted-printable", "line1\r\nline2", 80, encProps));
|
|
ferencd@0
|
208
|
|
ferencd@0
|
209 // in "binary" mode
|
|
ferencd@0
|
210 encProps["text"] = false;
|
|
ferencd@0
|
211 VASSERT_EQ("binary", "line1=0D=0Aline2",
|
|
ferencd@0
|
212 encode("quoted-printable", "line1\r\nline2", 80, encProps));
|
|
ferencd@0
|
213 }
|
|
ferencd@0
|
214
|
|
ferencd@0
|
215 void testQuotedPrintable_RFC2047()
|
|
ferencd@0
|
216 {
|
|
ferencd@0
|
217 /*
|
|
ferencd@0
|
218 * The RFC (http://tools.ietf.org/html/rfc2047#section-5) says:
|
|
ferencd@0
|
219 *
|
|
ferencd@0
|
220 * In this case the set of characters that may be used in a "Q"-encoded
|
|
ferencd@0
|
221 * 'encoded-word' is restricted to: <upper and lower case ASCII
|
|
ferencd@0
|
222 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
|
|
ferencd@0
|
223 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
|
|
ferencd@0
|
224 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
|
|
ferencd@0
|
225 * 'special' by 'linear-white-space'.
|
|
ferencd@0
|
226 */
|
|
ferencd@0
|
227
|
|
ferencd@0
|
228 vmime::propertySet encProps;
|
|
ferencd@0
|
229 encProps["rfc2047"] = true;
|
|
ferencd@0
|
230
|
|
ferencd@0
|
231 // Ensure 'especials' are encoded
|
|
ferencd@0
|
232 VASSERT_EQ("especials.1", "=2C", encode("quoted-printable", ",", 10, encProps));
|
|
ferencd@0
|
233 VASSERT_EQ("especials.2", "=3B", encode("quoted-printable", ";", 10, encProps));
|
|
ferencd@0
|
234 VASSERT_EQ("especials.3", "=3A", encode("quoted-printable", ":", 10, encProps));
|
|
ferencd@0
|
235 VASSERT_EQ("especials.4", "=5F", encode("quoted-printable", "_", 10, encProps));
|
|
ferencd@0
|
236 VASSERT_EQ("especials.5", "=40", encode("quoted-printable", "@", 10, encProps));
|
|
ferencd@0
|
237 VASSERT_EQ("especials.6", "=28", encode("quoted-printable", "(", 10, encProps));
|
|
ferencd@0
|
238 VASSERT_EQ("especials.7", "=29", encode("quoted-printable", ")", 10, encProps));
|
|
ferencd@0
|
239 VASSERT_EQ("especials.8", "=3C", encode("quoted-printable", "<", 10, encProps));
|
|
ferencd@0
|
240 VASSERT_EQ("especials.9", "=3E", encode("quoted-printable", ">", 10, encProps));
|
|
ferencd@0
|
241 VASSERT_EQ("especials.10", "=5B", encode("quoted-printable", "[", 10, encProps));
|
|
ferencd@0
|
242 VASSERT_EQ("especials.11", "=5D", encode("quoted-printable", "]", 10, encProps));
|
|
ferencd@0
|
243 VASSERT_EQ("especials.12", "=22", encode("quoted-printable", "\"", 10, encProps));
|
|
ferencd@0
|
244 }
|
|
ferencd@0
|
245
|
|
ferencd@0
|
246 // TODO: UUEncode
|
|
ferencd@0
|
247
|
|
ferencd@0
|
248 VMIME_TEST_SUITE_END
|