annotate 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
rev   line source
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 "vmime/wordEncoder.hpp"
ferencd@0 27
ferencd@0 28
ferencd@0 29 VMIME_TEST_SUITE_BEGIN(wordEncoderTest)
ferencd@0 30
ferencd@0 31 VMIME_TEST_LIST_BEGIN
ferencd@0 32 VMIME_TEST(testGetNextChunk)
ferencd@0 33 VMIME_TEST(testGetNextChunk_integral)
ferencd@0 34 VMIME_TEST(testIsEncodingNeeded_ascii)
ferencd@0 35 VMIME_TEST(testIsEncodingNeeded_withLanguage)
ferencd@0 36 VMIME_TEST(testIsEncodingNeeded_specialChars)
ferencd@0 37 VMIME_TEST(testGuessBestEncoding_QP)
ferencd@0 38 VMIME_TEST(testGuessBestEncoding_B64)
ferencd@0 39 VMIME_TEST(testEncodeQP_RFC2047)
ferencd@0 40 VMIME_TEST_LIST_END
ferencd@0 41
ferencd@0 42
ferencd@0 43 void testGetNextChunk()
ferencd@0 44 {
ferencd@0 45 // An integral number of characters should be encoded
ferencd@0 46 vmime::wordEncoder we(
ferencd@0 47 "bufferfoobarbaz",
ferencd@0 48 vmime::charset("utf-8"),
ferencd@0 49 vmime::wordEncoder::ENCODING_AUTO);
ferencd@0 50
ferencd@0 51 VASSERT_EQ("1", "buffer", we.getNextChunk(6));
ferencd@0 52 VASSERT_EQ("2", "foo", we.getNextChunk(3));
ferencd@0 53 VASSERT_EQ("3", "barbaz", we.getNextChunk(10));
ferencd@0 54 }
ferencd@0 55
ferencd@0 56 void testGetNextChunk_integral()
ferencd@0 57 {
ferencd@0 58 // An integral number of characters should be encoded
ferencd@0 59 vmime::wordEncoder we(
ferencd@0 60 "buffer\xc3\xa0plop",
ferencd@0 61 vmime::charset("utf-8"),
ferencd@0 62 vmime::wordEncoder::ENCODING_AUTO);
ferencd@0 63
ferencd@0 64 VASSERT_EQ("1", "buffer=C3=A0", we.getNextChunk(7));
ferencd@0 65 VASSERT_EQ("2", "plop", we.getNextChunk(10));
ferencd@0 66 }
ferencd@0 67
ferencd@0 68 void testIsEncodingNeeded_ascii()
ferencd@0 69 {
ferencd@0 70 vmime::generationContext ctx(vmime::generationContext::getDefaultContext());
ferencd@0 71 ctx.setInternationalizedEmailSupport(false);
ferencd@0 72
ferencd@0 73 VASSERT_FALSE("ascii", vmime::wordEncoder::isEncodingNeeded
ferencd@0 74 (ctx, "ASCII-only buffer", vmime::charset("utf-8"), ""));
ferencd@0 75
ferencd@0 76 VASSERT_TRUE("non-ascii", vmime::wordEncoder::isEncodingNeeded
ferencd@0 77 (ctx, "Buffer with some UTF-8 '\xc3\xa0'", vmime::charset("utf-8"), ""));
ferencd@0 78 }
ferencd@0 79
ferencd@0 80 void testIsEncodingNeeded_withLanguage()
ferencd@0 81 {
ferencd@0 82 VASSERT_TRUE("ascii", vmime::wordEncoder::isEncodingNeeded
ferencd@0 83 (vmime::generationContext::getDefaultContext(), "ASCII-only buffer", vmime::charset("utf-8"), "en"));
ferencd@0 84 }
ferencd@0 85
ferencd@0 86 void testIsEncodingNeeded_specialChars()
ferencd@0 87 {
ferencd@0 88 VASSERT_TRUE("rfc2047", vmime::wordEncoder::isEncodingNeeded
ferencd@0 89 (vmime::generationContext::getDefaultContext(),
ferencd@0 90 "foo bar =? foo bar", vmime::charset("us-ascii"), ""));
ferencd@0 91
ferencd@0 92 VASSERT_TRUE("new line 1", vmime::wordEncoder::isEncodingNeeded
ferencd@0 93 (vmime::generationContext::getDefaultContext(),
ferencd@0 94 "foo bar \n foo bar", vmime::charset("us-ascii"), ""));
ferencd@0 95
ferencd@0 96 VASSERT_TRUE("new line 2", vmime::wordEncoder::isEncodingNeeded
ferencd@0 97 (vmime::generationContext::getDefaultContext(),
ferencd@0 98 "foo bar \r foo bar", vmime::charset("us-ascii"), ""));
ferencd@0 99 }
ferencd@0 100
ferencd@0 101 void testGuessBestEncoding_QP()
ferencd@0 102 {
ferencd@0 103 VASSERT_EQ("1", vmime::wordEncoder::ENCODING_QP,
ferencd@0 104 vmime::wordEncoder::guessBestEncoding("ASCII only buffer", vmime::charset("us-ascii")));
ferencd@0 105 }
ferencd@0 106
ferencd@0 107 void testGuessBestEncoding_B64()
ferencd@0 108 {
ferencd@0 109 // >= 40% non-ASCII => Base64...
ferencd@0 110 VASSERT_EQ("1", vmime::wordEncoder::ENCODING_B64,
ferencd@0 111 vmime::wordEncoder::guessBestEncoding("xxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8")));
ferencd@0 112
ferencd@0 113 // ...else Quoted-Printable
ferencd@0 114 VASSERT_EQ("2", vmime::wordEncoder::ENCODING_QP,
ferencd@0 115 vmime::wordEncoder::guessBestEncoding("xxxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8")));
ferencd@0 116 }
ferencd@0 117
ferencd@0 118 void testEncodeQP_RFC2047()
ferencd@0 119 {
ferencd@0 120 // When Quoted-Printable is used, it should be RFC-2047 QP encoding
ferencd@0 121 vmime::wordEncoder we(
ferencd@0 122 "buffer\xc3\xa0 foo_bar",
ferencd@0 123 vmime::charset("utf-8"),
ferencd@0 124 vmime::wordEncoder::ENCODING_AUTO);
ferencd@0 125
ferencd@0 126 VASSERT_EQ("1", "buffer=C3=A0_foo=5Fbar", we.getNextChunk(100));
ferencd@0 127 }
ferencd@0 128
ferencd@0 129 VMIME_TEST_SUITE_END