Mercurial > thymian
comparison 3rdparty/vmime/tests/parser/wordEncoderTest.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "tests/testUtils.hpp" | |
| 25 | |
| 26 #include "vmime/wordEncoder.hpp" | |
| 27 | |
| 28 | |
| 29 VMIME_TEST_SUITE_BEGIN(wordEncoderTest) | |
| 30 | |
| 31 VMIME_TEST_LIST_BEGIN | |
| 32 VMIME_TEST(testGetNextChunk) | |
| 33 VMIME_TEST(testGetNextChunk_integral) | |
| 34 VMIME_TEST(testIsEncodingNeeded_ascii) | |
| 35 VMIME_TEST(testIsEncodingNeeded_withLanguage) | |
| 36 VMIME_TEST(testIsEncodingNeeded_specialChars) | |
| 37 VMIME_TEST(testGuessBestEncoding_QP) | |
| 38 VMIME_TEST(testGuessBestEncoding_B64) | |
| 39 VMIME_TEST(testEncodeQP_RFC2047) | |
| 40 VMIME_TEST_LIST_END | |
| 41 | |
| 42 | |
| 43 void testGetNextChunk() | |
| 44 { | |
| 45 // An integral number of characters should be encoded | |
| 46 vmime::wordEncoder we( | |
| 47 "bufferfoobarbaz", | |
| 48 vmime::charset("utf-8"), | |
| 49 vmime::wordEncoder::ENCODING_AUTO); | |
| 50 | |
| 51 VASSERT_EQ("1", "buffer", we.getNextChunk(6)); | |
| 52 VASSERT_EQ("2", "foo", we.getNextChunk(3)); | |
| 53 VASSERT_EQ("3", "barbaz", we.getNextChunk(10)); | |
| 54 } | |
| 55 | |
| 56 void testGetNextChunk_integral() | |
| 57 { | |
| 58 // An integral number of characters should be encoded | |
| 59 vmime::wordEncoder we( | |
| 60 "buffer\xc3\xa0plop", | |
| 61 vmime::charset("utf-8"), | |
| 62 vmime::wordEncoder::ENCODING_AUTO); | |
| 63 | |
| 64 VASSERT_EQ("1", "buffer=C3=A0", we.getNextChunk(7)); | |
| 65 VASSERT_EQ("2", "plop", we.getNextChunk(10)); | |
| 66 } | |
| 67 | |
| 68 void testIsEncodingNeeded_ascii() | |
| 69 { | |
| 70 vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); | |
| 71 ctx.setInternationalizedEmailSupport(false); | |
| 72 | |
| 73 VASSERT_FALSE("ascii", vmime::wordEncoder::isEncodingNeeded | |
| 74 (ctx, "ASCII-only buffer", vmime::charset("utf-8"), "")); | |
| 75 | |
| 76 VASSERT_TRUE("non-ascii", vmime::wordEncoder::isEncodingNeeded | |
| 77 (ctx, "Buffer with some UTF-8 '\xc3\xa0'", vmime::charset("utf-8"), "")); | |
| 78 } | |
| 79 | |
| 80 void testIsEncodingNeeded_withLanguage() | |
| 81 { | |
| 82 VASSERT_TRUE("ascii", vmime::wordEncoder::isEncodingNeeded | |
| 83 (vmime::generationContext::getDefaultContext(), "ASCII-only buffer", vmime::charset("utf-8"), "en")); | |
| 84 } | |
| 85 | |
| 86 void testIsEncodingNeeded_specialChars() | |
| 87 { | |
| 88 VASSERT_TRUE("rfc2047", vmime::wordEncoder::isEncodingNeeded | |
| 89 (vmime::generationContext::getDefaultContext(), | |
| 90 "foo bar =? foo bar", vmime::charset("us-ascii"), "")); | |
| 91 | |
| 92 VASSERT_TRUE("new line 1", vmime::wordEncoder::isEncodingNeeded | |
| 93 (vmime::generationContext::getDefaultContext(), | |
| 94 "foo bar \n foo bar", vmime::charset("us-ascii"), "")); | |
| 95 | |
| 96 VASSERT_TRUE("new line 2", vmime::wordEncoder::isEncodingNeeded | |
| 97 (vmime::generationContext::getDefaultContext(), | |
| 98 "foo bar \r foo bar", vmime::charset("us-ascii"), "")); | |
| 99 } | |
| 100 | |
| 101 void testGuessBestEncoding_QP() | |
| 102 { | |
| 103 VASSERT_EQ("1", vmime::wordEncoder::ENCODING_QP, | |
| 104 vmime::wordEncoder::guessBestEncoding("ASCII only buffer", vmime::charset("us-ascii"))); | |
| 105 } | |
| 106 | |
| 107 void testGuessBestEncoding_B64() | |
| 108 { | |
| 109 // >= 40% non-ASCII => Base64... | |
| 110 VASSERT_EQ("1", vmime::wordEncoder::ENCODING_B64, | |
| 111 vmime::wordEncoder::guessBestEncoding("xxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8"))); | |
| 112 | |
| 113 // ...else Quoted-Printable | |
| 114 VASSERT_EQ("2", vmime::wordEncoder::ENCODING_QP, | |
| 115 vmime::wordEncoder::guessBestEncoding("xxxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8"))); | |
| 116 } | |
| 117 | |
| 118 void testEncodeQP_RFC2047() | |
| 119 { | |
| 120 // When Quoted-Printable is used, it should be RFC-2047 QP encoding | |
| 121 vmime::wordEncoder we( | |
| 122 "buffer\xc3\xa0 foo_bar", | |
| 123 vmime::charset("utf-8"), | |
| 124 vmime::wordEncoder::ENCODING_AUTO); | |
| 125 | |
| 126 VASSERT_EQ("1", "buffer=C3=A0_foo=5Fbar", we.getNextChunk(100)); | |
| 127 } | |
| 128 | |
| 129 VMIME_TEST_SUITE_END |
