vmime/tests/parser/wordEncoderTest.cpp

175 lines
4.4 KiB
C++
Raw Normal View History

2013-02-25 09:42:47 +00:00
//
// VMime library (http://www.vmime.org)
2018-09-05 21:54:48 +00:00
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
2013-02-25 09:42:47 +00:00
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library. Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//
#include "tests/testUtils.hpp"
#include "vmime/wordEncoder.hpp"
2013-03-08 07:19:50 +00:00
VMIME_TEST_SUITE_BEGIN(wordEncoderTest)
2013-02-25 09:42:47 +00:00
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testGetNextChunk)
VMIME_TEST(testGetNextChunk_integral)
VMIME_TEST(testIsEncodingNeeded_ascii)
VMIME_TEST(testIsEncodingNeeded_withLanguage)
2013-02-25 09:42:47 +00:00
VMIME_TEST(testIsEncodingNeeded_specialChars)
VMIME_TEST(testGuessBestEncoding_QP)
VMIME_TEST(testGuessBestEncoding_B64)
VMIME_TEST(testEncodeQP_RFC2047)
VMIME_TEST_LIST_END
2018-09-05 21:54:48 +00:00
void testGetNextChunk() {
2013-02-25 09:42:47 +00:00
// An integral number of characters should be encoded
vmime::wordEncoder we(
"bufferfoobarbaz",
vmime::charset("utf-8"),
2018-09-05 21:54:48 +00:00
vmime::wordEncoder::ENCODING_AUTO
);
2013-02-25 09:42:47 +00:00
VASSERT_EQ("1", "buffer", we.getNextChunk(6));
VASSERT_EQ("2", "foo", we.getNextChunk(3));
VASSERT_EQ("3", "barbaz", we.getNextChunk(10));
}
2018-09-05 21:54:48 +00:00
void testGetNextChunk_integral() {
2013-02-25 09:42:47 +00:00
// An integral number of characters should be encoded
vmime::wordEncoder we(
"buffer\xc3\xa0plop",
vmime::charset("utf-8"),
2018-09-05 21:54:48 +00:00
vmime::wordEncoder::ENCODING_AUTO
);
2013-02-25 09:42:47 +00:00
VASSERT_EQ("1", "buffer=C3=A0", we.getNextChunk(7));
VASSERT_EQ("2", "plop", we.getNextChunk(10));
}
2018-09-05 21:54:48 +00:00
void testIsEncodingNeeded_ascii() {
2013-02-25 09:42:47 +00:00
vmime::generationContext ctx(vmime::generationContext::getDefaultContext());
ctx.setInternationalizedEmailSupport(false);
2018-09-05 21:54:48 +00:00
VASSERT_FALSE(
"ascii",
vmime::wordEncoder::isEncodingNeeded(
ctx, "ASCII-only buffer", vmime::charset("utf-8"), ""
)
);
VASSERT_TRUE(
"non-ascii",
vmime::wordEncoder::isEncodingNeeded(
ctx, "Buffer with some UTF-8 '\xc3\xa0'", vmime::charset("utf-8"), ""
)
);
}
2018-09-05 21:54:48 +00:00
void testIsEncodingNeeded_withLanguage() {
VASSERT_TRUE(
"ascii",
vmime::wordEncoder::isEncodingNeeded(
vmime::generationContext::getDefaultContext(),
"ASCII-only buffer",
vmime::charset("utf-8"),
"en"
)
);
2013-02-25 09:42:47 +00:00
}
2018-09-05 21:54:48 +00:00
void testIsEncodingNeeded_specialChars() {
VASSERT_TRUE(
"rfc2047",
vmime::wordEncoder::isEncodingNeeded(
vmime::generationContext::getDefaultContext(),
"foo bar =? foo bar",
vmime::charset("us-ascii"),
""
)
);
VASSERT_TRUE(
"new line 1",
vmime::wordEncoder::isEncodingNeeded(
vmime::generationContext::getDefaultContext(),
"foo bar \n foo bar",
vmime::charset("us-ascii"),
""
)
);
VASSERT_TRUE(
"new line 2",
vmime::wordEncoder::isEncodingNeeded(
vmime::generationContext::getDefaultContext(),
"foo bar \r foo bar",
vmime::charset("us-ascii"),
""
)
);
}
2013-02-25 09:42:47 +00:00
2018-09-05 21:54:48 +00:00
void testGuessBestEncoding_QP() {
2013-02-25 09:42:47 +00:00
2018-09-05 21:54:48 +00:00
VASSERT_EQ(
"1",
vmime::wordEncoder::ENCODING_QP,
vmime::wordEncoder::guessBestEncoding("ASCII only buffer", vmime::charset("us-ascii"))
);
2013-02-25 09:42:47 +00:00
}
2018-09-05 21:54:48 +00:00
void testGuessBestEncoding_B64() {
2013-02-25 09:42:47 +00:00
// >= 40% non-ASCII => Base64...
2018-09-05 21:54:48 +00:00
VASSERT_EQ(
"1",
vmime::wordEncoder::ENCODING_B64,
vmime::wordEncoder::guessBestEncoding("xxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8"))
);
2013-02-25 09:42:47 +00:00
// ...else Quoted-Printable
2018-09-05 21:54:48 +00:00
VASSERT_EQ(
"2",
vmime::wordEncoder::ENCODING_QP,
vmime::wordEncoder::guessBestEncoding("xxxxxx\xc3\xa0\xc3\xa0", vmime::charset("utf-8"))
);
2013-02-25 09:42:47 +00:00
}
2018-09-05 21:54:48 +00:00
void testEncodeQP_RFC2047() {
2013-02-25 09:42:47 +00:00
// When Quoted-Printable is used, it should be RFC-2047 QP encoding
vmime::wordEncoder we(
"buffer\xc3\xa0 foo_bar",
vmime::charset("utf-8"),
2018-09-05 21:54:48 +00:00
vmime::wordEncoder::ENCODING_AUTO
);
2013-02-25 09:42:47 +00:00
VASSERT_EQ("1", "buffer=C3=A0_foo=5Fbar", we.getNextChunk(100));
}
VMIME_TEST_SUITE_END